1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

Bug 307311: Add back method to MIConst to avoid breaking API, use a map for the special characters and unicode codepoints, add more JUnit tests, add the JUnit tests to the Sessionless suite.

This commit is contained in:
Marc Khouzam 2012-02-01 22:48:40 -05:00
parent a974434ec3
commit fdd2d8570d
4 changed files with 89 additions and 76 deletions

View file

@ -38,6 +38,10 @@ public class MIConst extends MIValue {
return MIStringHandler.translateCString(cstring, true); return MIStringHandler.translateCString(cstring, true);
} }
public static String getString(String str) {
return MIStringHandler.translateCString(str, true);
}
@Override @Override
public String toString() { public String toString() {
return getCString(); return getCString();

View file

@ -17,6 +17,9 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.text.ParseException; import java.text.ParseException;
import java.util.EnumSet; 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. * 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 { public class MIStringHandler {
/** /**
* Defines special characters which are used within escape notations to represent a * A map of special characters which are used within escape notations to represent a
* corresponding Unicode code point (i.e. character code). See the specialCodePoints * corresponding Unicode code point (i.e. character code).
* list below on the same index for the corresponding code point.
*/ */
private static char[] specialChars = new char[] { // Use a LinkedHashMap to preserve order, so as to get 'e' and not 'E'
'a', // Alert (bell) character private static Map<Character,Integer> fSpecialCharactersToCodePointMap = new LinkedHashMap<Character,Integer>();
'b', // Backspace character static {
'e', // GNU extension: Escape character fSpecialCharactersToCodePointMap.put('a', 0x07); // Alert (bell) character
'E', // same as 'e' fSpecialCharactersToCodePointMap.put('b', 0x08); // Backspace character
'f', // Form feed character fSpecialCharactersToCodePointMap.put('e', 0x1B); // GNU extension: Escape character
'n', // New line character fSpecialCharactersToCodePointMap.put('E', 0x1B); // same as 'e'
'r', // Carriage return character fSpecialCharactersToCodePointMap.put('f', 0x0C); // Form feed character
't', // Horizontal tabulation character fSpecialCharactersToCodePointMap.put('n', 0x0A); // New line character
'v', // Vertical tabulation character fSpecialCharactersToCodePointMap.put('r', 0x0D); // Carriage return character
'\'', // Single quotation mark fSpecialCharactersToCodePointMap.put('t', 0x09); // Horizontal tabulation character
'"', // Double quotation mark fSpecialCharactersToCodePointMap.put('v', 0x0B); // Vertical tabulation character
'\\', // Backslash fSpecialCharactersToCodePointMap.put('\'', 0x27); // Single quotation mark
'?' // Literal question mark fSpecialCharactersToCodePointMap.put('"', 0x22); // Double quotation mark
}; fSpecialCharactersToCodePointMap.put('\\', 0x5C); // Backslash
fSpecialCharactersToCodePointMap.put('?', 0x3F); // 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 \?
};
/** /**
* An internal helper enumeration which holds the current status while parsing an escaped * An internal helper enumeration which holds the current status while parsing an escaped
* text sequence. * text sequence.
@ -139,12 +122,7 @@ public class MIStringHandler {
* @return The test result. * @return The test result.
*/ */
public static boolean isSpecialChar(char c) { public static boolean isSpecialChar(char c) {
for (int i = 0; i < specialChars.length; i++) { return fSpecialCharactersToCodePointMap.containsKey(c);
if (specialChars[i] == c) {
return true;
}
}
return false;
} }
/** /**
@ -153,12 +131,7 @@ public class MIStringHandler {
* @return The test result. * @return The test result.
*/ */
public static boolean isSpecialCodePoint(int codePoint) { public static boolean isSpecialCodePoint(int codePoint) {
for (int i = 0; i < specialCodePoints.length; i++) { return fSpecialCharactersToCodePointMap.containsValue(codePoint);
if (specialCodePoints[i] == codePoint) {
return true;
}
}
return false;
} }
/** /**
@ -169,11 +142,10 @@ public class MIStringHandler {
* not a special character. * not a special character.
*/ */
public static int parseSpecialChar(char c) throws ParseException { public static int parseSpecialChar(char c) throws ParseException {
for (int i = 0; i < specialChars.length; i++) { Integer codePoint = fSpecialCharactersToCodePointMap.get(c);
if (specialChars[i] == c) { if (codePoint != null) {
return specialCodePoints[i]; return codePoint;
} }
}
throw new ParseException("The given character '" + c + "' is not a special character.", 0); //$NON-NLS-1$ //$NON-NLS-2$ 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. * when it's not a special code point.
*/ */
public static char parseSpecialCodePoint(int codePoint) throws ParseException { public static char parseSpecialCodePoint(int codePoint) throws ParseException {
for (int i = 0; i < specialCodePoints.length; i++) { for (Entry<Character, Integer> entry : fSpecialCharactersToCodePointMap.entrySet()) {
if (specialCodePoints[i] == codePoint) { if (entry.getValue().equals(codePoint)) {
return specialChars[i]; 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$ throw new ParseException("The given Unicode code point " + codePoint + " is not a special code point.", 0); //$NON-NLS-1$ //$NON-NLS-2$

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -10,6 +10,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.dsf.mi.service.command.commands; 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.dsf.mi.service.command.output.MIThreadTests;
import org.eclipse.cdt.tests.dsf.gdb.framework.OnceOnlySuite; import org.eclipse.cdt.tests.dsf.gdb.framework.OnceOnlySuite;
import org.eclipse.cdt.tests.dsf.gdb.tests.LaunchUtilsTest; import org.eclipse.cdt.tests.dsf.gdb.tests.LaunchUtilsTest;
@ -27,7 +28,8 @@ import org.junit.runners.Suite;
TestMIBreakInsertCommand.class, TestMIBreakInsertCommand.class,
TestMICommandConstructCommand.class, TestMICommandConstructCommand.class,
MIThreadTests.class, MIThreadTests.class,
LaunchUtilsTest.class LaunchUtilsTest.class,
MIStringHandlerTests.class
/* Add your test class here */ /* Add your test class here */
}) })
public class Suite_Sessionless_Tests { public class Suite_Sessionless_Tests {

View file

@ -10,13 +10,15 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.dsf.mi.service.command.output; 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 java.text.ParseException;
import org.junit.Test;
import static org.junit.Assert.*;
import junit.framework.JUnit4TestAdapter; import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
public class MIStringHandlerTests { public class MIStringHandlerTests {
@Test @Test
public void testTranscodeString() { public void testTranscodeString() {
@ -101,12 +103,20 @@ public class MIStringHandlerTests {
assertEquals(MIStringHandler.isSpecialChar('i'), false); assertEquals(MIStringHandler.isSpecialChar('i'), false);
assertEquals(MIStringHandler.isSpecialChar('w'), false); assertEquals(MIStringHandler.isSpecialChar('w'), false);
// Testing special chars. // Testing special chars.
assertEquals(MIStringHandler.isSpecialChar('a'), true); assertEquals(MIStringHandler.isSpecialChar('a'), true);
assertEquals(MIStringHandler.isSpecialChar('n'), true); assertEquals(MIStringHandler.isSpecialChar('b'), true);
assertEquals(MIStringHandler.isSpecialChar('t'), 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); assertEquals(MIStringHandler.isSpecialChar('\\'), true);
assertEquals(MIStringHandler.isSpecialChar('?'), true);
} }
@Test @Test
@ -118,12 +128,20 @@ public class MIStringHandlerTests {
assertEquals(MIStringHandler.isSpecialCodePoint(0x6E), false); // 'n' character assertEquals(MIStringHandler.isSpecialCodePoint(0x6E), false); // 'n' character
// Testing special Unicode code points. // Testing special Unicode code points.
assertEquals(MIStringHandler.isSpecialCodePoint(0x07), true); // '\a' character assertEquals(MIStringHandler.isSpecialCodePoint(0x07), true); // 'a' character
assertEquals(MIStringHandler.isSpecialCodePoint(0x0A), true); // '\n' character assertEquals(MIStringHandler.isSpecialCodePoint(0x08), true); // 'b' character
assertEquals(MIStringHandler.isSpecialCodePoint(0x09), true); // '\t' 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(0x27), true); // '\'' character
assertEquals(MIStringHandler.isSpecialCodePoint(0x22), true); // '"' character
assertEquals(MIStringHandler.isSpecialCodePoint(0x5C), true); // '\\' character assertEquals(MIStringHandler.isSpecialCodePoint(0x5C), true); // '\\' character
} assertEquals(MIStringHandler.isSpecialCodePoint(0x3F), true); // '?' character
}
@Test @Test
public void testParseSpecialChar() { public void testParseSpecialChar() {
@ -142,11 +160,20 @@ public class MIStringHandlerTests {
try { try {
// Testing special chars. // Testing special chars.
assertEquals(MIStringHandler.parseSpecialChar('a'), 0x07); assertEquals(MIStringHandler.parseSpecialChar('a'), 0x07);
assertEquals(MIStringHandler.parseSpecialChar('n'), 0x0A); assertEquals(MIStringHandler.parseSpecialChar('b'), 0x08);
assertEquals(MIStringHandler.parseSpecialChar('t'), 0x09); 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('\''), 0x27);
assertEquals(MIStringHandler.parseSpecialChar('"'), 0x22);
assertEquals(MIStringHandler.parseSpecialChar('\\'), 0x5C); assertEquals(MIStringHandler.parseSpecialChar('\\'), 0x5C);
assertEquals(MIStringHandler.parseSpecialChar('?'), 0x3F);
} catch (ParseException e) { } catch (ParseException e) {
fail("Parsing exception thrown."); //$NON-NLS-1$ fail("Parsing exception thrown."); //$NON-NLS-1$
} }
@ -170,10 +197,18 @@ public class MIStringHandlerTests {
// Testing special Unicode code points. // Testing special Unicode code points.
assertEquals(MIStringHandler.parseSpecialCodePoint(0x07), 'a'); 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(0x0A), 'n');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x0D), 'r');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x09), 't'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x09), 't');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x0B), 'v');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x27), '\''); assertEquals(MIStringHandler.parseSpecialCodePoint(0x27), '\'');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x22), '"');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x5C), '\\'); assertEquals(MIStringHandler.parseSpecialCodePoint(0x5C), '\\');
assertEquals(MIStringHandler.parseSpecialCodePoint(0x3F), '?');
} catch (ParseException e) { } catch (ParseException e) {
fail("Parsing exception thrown."); //$NON-NLS-1$ fail("Parsing exception thrown."); //$NON-NLS-1$
} }