diff options
author | Mark Spruiell <mes@zeroc.com> | 2009-12-10 13:41:49 -0800 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2009-12-10 13:41:49 -0800 |
commit | 47bc7e93cb03a077c0eef8a2cc871cead1212c00 (patch) | |
tree | 477aa3c5c77a5b458abc19b92c3ca12360c996a5 /cs/src/Ice/StringUtil.cs | |
parent | Squashed commit of the following: (diff) | |
download | ice-47bc7e93cb03a077c0eef8a2cc871cead1212c00.tar.bz2 ice-47bc7e93cb03a077c0eef8a2cc871cead1212c00.tar.xz ice-47bc7e93cb03a077c0eef8a2cc871cead1212c00.zip |
bug 4355 - improve parse exception messages
Diffstat (limited to 'cs/src/Ice/StringUtil.cs')
-rw-r--r-- | cs/src/Ice/StringUtil.cs | 81 |
1 files changed, 38 insertions, 43 deletions
diff --git a/cs/src/Ice/StringUtil.cs b/cs/src/Ice/StringUtil.cs index dd5c1d584ca..9db9c17c697 100644 --- a/cs/src/Ice/StringUtil.cs +++ b/cs/src/Ice/StringUtil.cs @@ -180,7 +180,8 @@ namespace IceUtilInternal { if((int)special[i] < 32 || (int)special[i] > 126) { - throw new System.ArgumentException("special characters must be in ASCII range 32-126", "special"); + throw new System.ArgumentException("special characters must be in ASCII range 32-126", + "special"); } } } @@ -197,11 +198,22 @@ namespace IceUtilInternal return result.ToString(); } - private static char checkChar(char c) + private static char checkChar(string s, int pos) { + char c = s[pos]; if(!(c >= 32 && c <= 126)) { - throw new System.ArgumentException("illegal input character"); + string msg; + if(pos > 0) + { + msg = "character after `" + s.Substring(0, pos) + "'"; + } + else + { + msg = "first character"; + } + msg += " is not a printable ASCII character (ordinal " + (int)c + ")"; + throw new System.ArgumentException(msg); } return c; } @@ -222,13 +234,13 @@ namespace IceUtilInternal if(s[start] != '\\') { - c = checkChar(s[start++]); + c = checkChar(s, start++); } else { if(start + 1 == end) { - throw new System.ArgumentException("trailing backslash in argument"); + throw new System.ArgumentException("trailing backslash"); } switch(s[++start]) { @@ -278,7 +290,7 @@ namespace IceUtilInternal case '6': case '7': { - int oct = 0; + int val = 0; for(int j = 0; j < 3 && start < end; ++j) { int charVal = s[start++] - '0'; @@ -287,18 +299,20 @@ namespace IceUtilInternal --start; break; } - oct = oct * 8 + charVal; + val = val * 8 + charVal; } - if(oct > 255) + if(val > 255) { - throw new System.ArgumentException("octal value out of range", "s"); + string msg = "octal value \\" + System.Convert.ToString(val, 8) + " (" + val + + ") is out of range"; + throw new System.ArgumentException(msg, "s"); } - c = System.Convert.ToChar(oct); + c = System.Convert.ToChar(val); break; } default: { - c = checkChar(s[start++]); + c = checkChar(s, start++); break; } } @@ -320,44 +334,25 @@ namespace IceUtilInternal } // - // Remove escape sequences added by escapeString. + // Remove escape sequences added by escapeString. Throws System.ArgumentException + // for an invalid input string. // - public static bool unescapeString(string s, int start, int end, out string result) + public static string unescapeString(string s, int start, int end) { - if(start < 0) - { - throw new System.ArgumentException("start offset must be >= 0", "start"); - } - if(end > s.Length) - { - throw new System.ArgumentException("end offset must be <= s.Length", "end"); - } - if(start > end) - { - throw new System.ArgumentException("start offset must be <= end offset"); - } - - result = null; - try - { - StringBuilder sb = new StringBuilder(); - decodeString(s, start, end, sb); - string decodedString = sb.ToString(); + Debug.Assert(start >= 0 && start <= end && end <= s.Length); - byte[] arr = new byte[decodedString.Length]; - for(int i = 0; i < arr.Length; ++i) - { - arr[i] = (byte)decodedString[i]; - } + StringBuilder sb = new StringBuilder(); + decodeString(s, start, end, sb); + string decodedString = sb.ToString(); - UTF8Encoding utf8 = new UTF8Encoding(false, true); - result = utf8.GetString(arr); - return true; - } - catch(System.Exception) + byte[] arr = new byte[decodedString.Length]; + for(int i = 0; i < arr.Length; ++i) { - return false; + arr[i] = (byte)decodedString[i]; } + + UTF8Encoding utf8 = new UTF8Encoding(false, true); + return utf8.GetString(arr); // May raise ArgumentException. } public static int checkQuote(string s) |