diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIConst.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIConst.java index 4fc3a969cef..95a6e3299e2 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIConst.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIConst.java @@ -38,6 +38,10 @@ public class MIConst extends MIValue { return MIStringHandler.translateCString(cstring, true); } + public static String getString(String str) { + return MIStringHandler.translateCString(str, true); + } + @Override public String toString() { return getCString(); diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandler.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandler.java index a5a4a770583..c0b5c2f2fb7 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandler.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandler.java @@ -17,6 +17,9 @@ import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.text.ParseException; import java.util.EnumSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; /** * The MIStringHandler class provides several static functions to handle C and / or MI strings. @@ -25,47 +28,27 @@ import java.util.EnumSet; public class MIStringHandler { /** - * Defines special characters which are used within escape notations to represent a - * corresponding Unicode code point (i.e. character code). See the specialCodePoints - * list below on the same index for the corresponding code point. + * A map of special characters which are used within escape notations to represent a + * corresponding Unicode code point (i.e. character code). */ - private static char[] specialChars = new char[] { - 'a', // Alert (bell) character - 'b', // Backspace character - 'e', // GNU extension: Escape character - 'E', // same as 'e' - 'f', // Form feed character - 'n', // New line character - 'r', // Carriage return character - 't', // Horizontal tabulation character - 'v', // Vertical tabulation character - '\'', // Single quotation mark - '"', // Double quotation mark - '\\', // Backslash - '?' // Literal question mark - }; - - /** - * Defines special Unicode code points (i.e. character codes) which can be escaped by a - * corresponding special character. See the specialChars list above on the same index - * for the corresponding special character. - */ - private static int[] specialCodePoints = new int[] { - 0x07, // corresponds to \a - 0x08, // corresponds to \b - 0x1B, // corresponds to \e - 0x1B, // corresponds to \E - 0x0C, // corresponds to \f - 0x0A, // corresponds to \n - 0x0D, // corresponds to \r - 0x09, // corresponds to \t - 0x0B, // corresponds to \v - 0x27, // corresponds to \' - 0x22, // corresponds to \" - 0x5C, // corresponds to \\ - 0x3F // corresponds to \? - }; - + // Use a LinkedHashMap to preserve order, so as to get 'e' and not 'E' + private static Map fSpecialCharactersToCodePointMap = new LinkedHashMap(); + static { + fSpecialCharactersToCodePointMap.put('a', 0x07); // Alert (bell) character + fSpecialCharactersToCodePointMap.put('b', 0x08); // Backspace character + fSpecialCharactersToCodePointMap.put('e', 0x1B); // GNU extension: Escape character + fSpecialCharactersToCodePointMap.put('E', 0x1B); // same as 'e' + fSpecialCharactersToCodePointMap.put('f', 0x0C); // Form feed character + fSpecialCharactersToCodePointMap.put('n', 0x0A); // New line character + fSpecialCharactersToCodePointMap.put('r', 0x0D); // Carriage return character + fSpecialCharactersToCodePointMap.put('t', 0x09); // Horizontal tabulation character + fSpecialCharactersToCodePointMap.put('v', 0x0B); // Vertical tabulation character + fSpecialCharactersToCodePointMap.put('\'', 0x27); // Single quotation mark + fSpecialCharactersToCodePointMap.put('"', 0x22); // Double quotation mark + fSpecialCharactersToCodePointMap.put('\\', 0x5C); // Backslash + fSpecialCharactersToCodePointMap.put('?', 0x3F); // Literal question mark + } + /** * An internal helper enumeration which holds the current status while parsing an escaped * text sequence. @@ -139,12 +122,7 @@ public class MIStringHandler { * @return The test result. */ public static boolean isSpecialChar(char c) { - for (int i = 0; i < specialChars.length; i++) { - if (specialChars[i] == c) { - return true; - } - } - return false; + return fSpecialCharactersToCodePointMap.containsKey(c); } /** @@ -153,12 +131,7 @@ public class MIStringHandler { * @return The test result. */ public static boolean isSpecialCodePoint(int codePoint) { - for (int i = 0; i < specialCodePoints.length; i++) { - if (specialCodePoints[i] == codePoint) { - return true; - } - } - return false; + return fSpecialCharactersToCodePointMap.containsValue(codePoint); } /** @@ -169,11 +142,10 @@ public class MIStringHandler { * not a special character. */ public static int parseSpecialChar(char c) throws ParseException { - for (int i = 0; i < specialChars.length; i++) { - if (specialChars[i] == c) { - return specialCodePoints[i]; - } - } + Integer codePoint = fSpecialCharactersToCodePointMap.get(c); + if (codePoint != null) { + return codePoint; + } throw new ParseException("The given character '" + c + "' is not a special character.", 0); //$NON-NLS-1$ //$NON-NLS-2$ } @@ -185,9 +157,9 @@ public class MIStringHandler { * when it's not a special code point. */ public static char parseSpecialCodePoint(int codePoint) throws ParseException { - for (int i = 0; i < specialCodePoints.length; i++) { - if (specialCodePoints[i] == codePoint) { - return specialChars[i]; + for (Entry entry : fSpecialCharactersToCodePointMap.entrySet()) { + if (entry.getValue().equals(codePoint)) { + return entry.getKey(); } } throw new ParseException("The given Unicode code point " + codePoint + " is not a special code point.", 0); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/commands/Suite_Sessionless_Tests.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/commands/Suite_Sessionless_Tests.java index 0480c2f776b..de08ef198d9 100644 --- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/commands/Suite_Sessionless_Tests.java +++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/commands/Suite_Sessionless_Tests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Ericsson and others. + * Copyright (c) 2008, 2012 Ericsson and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.dsf.mi.service.command.commands; +import org.eclipse.cdt.dsf.mi.service.command.output.MIStringHandlerTests; import org.eclipse.cdt.dsf.mi.service.command.output.MIThreadTests; import org.eclipse.cdt.tests.dsf.gdb.framework.OnceOnlySuite; import org.eclipse.cdt.tests.dsf.gdb.tests.LaunchUtilsTest; @@ -27,7 +28,8 @@ import org.junit.runners.Suite; TestMIBreakInsertCommand.class, TestMICommandConstructCommand.class, MIThreadTests.class, - LaunchUtilsTest.class + LaunchUtilsTest.class, + MIStringHandlerTests.class /* Add your test class here */ }) public class Suite_Sessionless_Tests { diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandlerTests.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandlerTests.java index 8d1cf87dd08..af5671e970c 100644 --- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandlerTests.java +++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/dsf/mi/service/command/output/MIStringHandlerTests.java @@ -10,13 +10,15 @@ *******************************************************************************/ package org.eclipse.cdt.dsf.mi.service.command.output; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.text.ParseException; -import org.junit.Test; - -import static org.junit.Assert.*; import junit.framework.JUnit4TestAdapter; +import org.junit.Test; + public class MIStringHandlerTests { @Test public void testTranscodeString() { @@ -101,12 +103,20 @@ public class MIStringHandlerTests { assertEquals(MIStringHandler.isSpecialChar('i'), false); assertEquals(MIStringHandler.isSpecialChar('w'), false); - // Testing special chars. - assertEquals(MIStringHandler.isSpecialChar('a'), true); - assertEquals(MIStringHandler.isSpecialChar('n'), true); - assertEquals(MIStringHandler.isSpecialChar('t'), true); + // Testing special chars. + assertEquals(MIStringHandler.isSpecialChar('a'), true); + assertEquals(MIStringHandler.isSpecialChar('b'), true); + assertEquals(MIStringHandler.isSpecialChar('e'), true); + assertEquals(MIStringHandler.isSpecialChar('E'), true); + assertEquals(MIStringHandler.isSpecialChar('f'), true); + assertEquals(MIStringHandler.isSpecialChar('n'), true); + assertEquals(MIStringHandler.isSpecialChar('r'), true); + assertEquals(MIStringHandler.isSpecialChar('t'), true); + assertEquals(MIStringHandler.isSpecialChar('v'), true); assertEquals(MIStringHandler.isSpecialChar('\''), true); + assertEquals(MIStringHandler.isSpecialChar('"'), true); assertEquals(MIStringHandler.isSpecialChar('\\'), true); + assertEquals(MIStringHandler.isSpecialChar('?'), true); } @Test @@ -118,12 +128,20 @@ public class MIStringHandlerTests { assertEquals(MIStringHandler.isSpecialCodePoint(0x6E), false); // 'n' character // Testing special Unicode code points. - assertEquals(MIStringHandler.isSpecialCodePoint(0x07), true); // '\a' character - assertEquals(MIStringHandler.isSpecialCodePoint(0x0A), true); // '\n' character - assertEquals(MIStringHandler.isSpecialCodePoint(0x09), true); // '\t' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x07), true); // 'a' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x08), true); // 'b' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x1B), true); // 'e' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x1B), true); // 'E' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x0C), true); // 'f' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x0A), true); // 'n' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x0D), true); // 'r' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x09), true); // 't' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x0B), true); // 'v' character assertEquals(MIStringHandler.isSpecialCodePoint(0x27), true); // '\'' character + assertEquals(MIStringHandler.isSpecialCodePoint(0x22), true); // '"' character assertEquals(MIStringHandler.isSpecialCodePoint(0x5C), true); // '\\' character - } + assertEquals(MIStringHandler.isSpecialCodePoint(0x3F), true); // '?' character + } @Test public void testParseSpecialChar() { @@ -142,11 +160,20 @@ public class MIStringHandlerTests { try { // Testing special chars. - assertEquals(MIStringHandler.parseSpecialChar('a'), 0x07); - assertEquals(MIStringHandler.parseSpecialChar('n'), 0x0A); - assertEquals(MIStringHandler.parseSpecialChar('t'), 0x09); + assertEquals(MIStringHandler.parseSpecialChar('a'), 0x07); + assertEquals(MIStringHandler.parseSpecialChar('b'), 0x08); + assertEquals(MIStringHandler.parseSpecialChar('e'), 0x1B); + assertEquals(MIStringHandler.parseSpecialChar('E'), 0x1B); + assertEquals(MIStringHandler.parseSpecialChar('f'), 0x0C); + assertEquals(MIStringHandler.parseSpecialChar('n'), 0x0A); + assertEquals(MIStringHandler.parseSpecialChar('r'), 0x0D); + assertEquals(MIStringHandler.parseSpecialChar('t'), 0x09); + assertEquals(MIStringHandler.parseSpecialChar('v'), 0x0B); assertEquals(MIStringHandler.parseSpecialChar('\''), 0x27); + assertEquals(MIStringHandler.parseSpecialChar('"'), 0x22); assertEquals(MIStringHandler.parseSpecialChar('\\'), 0x5C); + assertEquals(MIStringHandler.parseSpecialChar('?'), 0x3F); + } catch (ParseException e) { fail("Parsing exception thrown."); //$NON-NLS-1$ } @@ -170,10 +197,18 @@ public class MIStringHandlerTests { // Testing special Unicode code points. assertEquals(MIStringHandler.parseSpecialCodePoint(0x07), 'a'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x08), 'b'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x1B), 'e'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x0C), 'f'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x0A), 'n'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x0D), 'r'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x09), 't'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x0B), 'v'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x27), '\''); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x22), '"'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x5C), '\\'); + assertEquals(MIStringHandler.parseSpecialCodePoint(0x3F), '?'); + } catch (ParseException e) { fail("Parsing exception thrown."); //$NON-NLS-1$ }