Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/TextFormat.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/TextFormat.cs
r3857 r4068 34 34 35 35 using System; 36 using System.Collections; 36 37 using System.Collections.Generic; 37 38 using System.Globalization; … … 39 40 using System.Text; 40 41 using Google.ProtocolBuffers.Descriptors; 41 using System.Collections;42 42 43 43 namespace Google.ProtocolBuffers { … … 87 87 if (field.IsRepeated) { 88 88 // Repeated field. Print each element. 89 foreach (object element in (IEnumerable) 89 foreach (object element in (IEnumerable)value) { 90 90 PrintSingleField(field, element, generator); 91 91 } … … 102 102 && field.FieldType == FieldType.Message 103 103 && field.IsOptional 104 104 // object equality (TODO(jonskeet): Work out what this comment means!) 105 105 && field.ExtensionScope == field.MessageType) { 106 106 generator.Print(field.MessageType.FullName); … … 150 150 // The simple Object.ToString converts using the current culture. 151 151 // We want to always use the invariant culture so it's predictable. 152 generator.Print(((IConvertible) 152 generator.Print(((IConvertible)value).ToString(CultureInfo.InvariantCulture)); 153 153 break; 154 154 case FieldType.Bool: 155 155 // Explicitly use the Java true/false 156 generator.Print((bool) 156 generator.Print((bool)value ? "true" : "false"); 157 157 break; 158 158 159 159 case FieldType.String: 160 160 generator.Print("\""); 161 generator.Print(EscapeText((string) 161 generator.Print(EscapeText((string)value)); 162 162 generator.Print("\""); 163 163 break; 164 164 165 165 case FieldType.Bytes: { 166 generator.Print("\"");167 generator.Print(EscapeBytes((ByteString)value));168 generator.Print("\"");169 break;170 }166 generator.Print("\""); 167 generator.Print(EscapeBytes((ByteString)value)); 168 generator.Print("\""); 169 break; 170 } 171 171 172 172 case FieldType.Enum: { 173 generator.Print(((EnumValueDescriptor)value).Name);174 break;175 }173 generator.Print(((EnumValueDescriptor)value).Name); 174 break; 175 } 176 176 177 177 case FieldType.Message: 178 178 case FieldType.Group: 179 Print((IMessage) 179 Print((IMessage)value, generator); 180 180 break; 181 181 } … … 220 220 221 221 internal static ulong ParseUInt64(string text) { 222 return (ulong) 222 return (ulong)ParseInteger(text, false, true); 223 223 } 224 224 … … 228 228 229 229 internal static uint ParseUInt32(string text) { 230 return (uint) 230 return (uint)ParseInteger(text, false, false); 231 231 } 232 232 233 233 internal static int ParseInt32(string text) { 234 return (int) 234 return (int)ParseInteger(text, true, false); 235 235 } 236 236 … … 269 269 } 270 270 } 271 271 272 272 /// <summary> 273 273 /// Parses an integer in hex (leading 0x), decimal (no prefix) or octal (leading 0). … … 298 298 // We should be able to use Convert.ToUInt64 for all cases. 299 299 result = radix == 10 ? ulong.Parse(text) : Convert.ToUInt64(text, radix); 300 } catch (OverflowException) { 300 } 301 catch (OverflowException) { 301 302 // Convert OverflowException to FormatException so there's a single exception type this method can throw. 302 303 string numberDescription = string.Format("{0}-bit {1}signed integer", isLong ? 64 : 32, isSigned ? "" : "un"); … … 310 311 throw new FormatException("Number out of range for " + numberDescription + ": " + original); 311 312 } 312 return -((long) 313 } else { 314 ulong max = isSigned 315 ? (isLong ? (ulong) 313 return -((long)result); 314 } else { 315 ulong max = isSigned 316 ? (isLong ? (ulong)long.MaxValue : int.MaxValue) 316 317 : (isLong ? ulong.MaxValue : uint.MaxValue); 317 318 if (result > max) { … … 319 320 throw new FormatException("Number out of range for " + numberDescription + ": " + original); 320 321 } 321 return (long) 322 return (long)result; 322 323 } 323 324 } … … 384 385 switch (b) { 385 386 // C# does not use \a or \v 386 case 0x07: builder.Append("\\a" 387 case (byte)'\b': builder.Append("\\b" 388 case (byte)'\f': builder.Append("\\f" 389 case (byte)'\n': builder.Append("\\n" 390 case (byte)'\r': builder.Append("\\r" 391 case (byte)'\t': builder.Append("\\t" 392 case 0x0b: builder.Append("\\v" 387 case 0x07: builder.Append("\\a"); break; 388 case (byte)'\b': builder.Append("\\b"); break; 389 case (byte)'\f': builder.Append("\\f"); break; 390 case (byte)'\n': builder.Append("\\n"); break; 391 case (byte)'\r': builder.Append("\\r"); break; 392 case (byte)'\t': builder.Append("\\t"); break; 393 case 0x0b: builder.Append("\\v"); break; 393 394 case (byte)'\\': builder.Append("\\\\"); break; 394 395 case (byte)'\'': builder.Append("\\\'"); break; 395 case (byte)'"' 396 case (byte)'"': builder.Append("\\\""); break; 396 397 default: 397 398 if (b >= 0x20 && b < 128) { 398 builder.Append((char) 399 builder.Append((char)b); 399 400 } else { 400 401 builder.Append('\\'); 401 builder.Append((char) 402 builder.Append((char) 403 builder.Append((char) 402 builder.Append((char)('0' + ((b >> 6) & 3))); 403 builder.Append((char)('0' + ((b >> 3) & 7))); 404 builder.Append((char)('0' + (b & 7))); 404 405 } 405 406 break; … … 421 422 } 422 423 if (c != '\\') { 423 result[pos++] = (byte) 424 result[pos++] = (byte)c; 424 425 continue; 425 426 } … … 433 434 // Octal escape. 434 435 int code = ParseDigit(c); 435 if (i + 1 < input.Length && IsOctal(input[i +1])) {436 if (i + 1 < input.Length && IsOctal(input[i + 1])) { 436 437 i++; 437 438 code = code * 8 + ParseDigit(input[i]); 438 439 } 439 if (i + 1 < input.Length && IsOctal(input[i +1])) {440 if (i + 1 < input.Length && IsOctal(input[i + 1])) { 440 441 i++; 441 442 code = code * 8 + ParseDigit(input[i]); 442 443 } 443 result[pos++] = (byte) 444 result[pos++] = (byte)code; 444 445 } else { 445 446 switch (c) { 446 447 case 'a': result[pos++] = 0x07; break; 447 case 'b': result[pos++] = (byte) 448 case 'f': result[pos++] = (byte) 449 case 'n': result[pos++] = (byte) 450 case 'r': result[pos++] = (byte) 451 case 't': result[pos++] = (byte) 448 case 'b': result[pos++] = (byte)'\b'; break; 449 case 'f': result[pos++] = (byte)'\f'; break; 450 case 'n': result[pos++] = (byte)'\n'; break; 451 case 'r': result[pos++] = (byte)'\r'; break; 452 case 't': result[pos++] = (byte)'\t'; break; 452 453 case 'v': result[pos++] = 0x0b; break; 453 case '\\': result[pos++] = (byte) 454 case '\'': result[pos++] = (byte) 455 case '"': result[pos++] = (byte) 454 case '\\': result[pos++] = (byte)'\\'; break; 455 case '\'': result[pos++] = (byte)'\''; break; 456 case '"': result[pos++] = (byte)'\"'; break; 456 457 457 458 case 'x': 458 459 // hex escape 459 460 int code; 460 if (i + 1 < input.Length && IsHex(input[i +1])) {461 if (i + 1 < input.Length && IsHex(input[i + 1])) { 461 462 i++; 462 463 code = ParseDigit(input[i]); … … 464 465 throw new FormatException("Invalid escape sequence: '\\x' with no digits"); 465 466 } 466 if (i + 1 < input.Length && IsHex(input[i +1])) {467 if (i + 1 < input.Length && IsHex(input[i + 1])) { 467 468 ++i; 468 469 code = code * 16 + ParseDigit(input[i]); … … 636 637 637 638 case FieldType.Enum: { 638 EnumDescriptor enumType = field.EnumType; 639 640 if (tokenizer.LookingAtInteger()) { 641 int number = tokenizer.ConsumeInt32(); 642 value = enumType.FindValueByNumber(number); 643 if (value == null) { 644 throw tokenizer.CreateFormatExceptionPreviousToken( 645 "Enum type \"" + enumType.FullName + 646 "\" has no value with number " + number + "."); 639 EnumDescriptor enumType = field.EnumType; 640 641 if (tokenizer.LookingAtInteger()) { 642 int number = tokenizer.ConsumeInt32(); 643 value = enumType.FindValueByNumber(number); 644 if (value == null) { 645 throw tokenizer.CreateFormatExceptionPreviousToken( 646 "Enum type \"" + enumType.FullName + 647 "\" has no value with number " + number + "."); 648 } 649 } else { 650 String id = tokenizer.ConsumeIdentifier(); 651 value = enumType.FindValueByName(id); 652 if (value == null) { 653 throw tokenizer.CreateFormatExceptionPreviousToken( 654 "Enum type \"" + enumType.FullName + 655 "\" has no value named \"" + id + "\"."); 656 } 647 657 } 648 } else { 649 String id = tokenizer.ConsumeIdentifier(); 650 value = enumType.FindValueByName(id); 651 if (value == null) { 652 throw tokenizer.CreateFormatExceptionPreviousToken( 653 "Enum type \"" + enumType.FullName + 654 "\" has no value named \"" + id + "\"."); 655 } 658 659 break; 656 660 } 657 658 break;659 }660 661 661 662 case FieldType.Message:
Note: See TracChangeset
for help on using the changeset viewer.