You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BufferExtensions.cs 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Text;
  3. using System.Text.Utf8;
  4. namespace Discord.Serialization
  5. {
  6. public static class BufferExtensions
  7. {
  8. private static readonly ParsedFormat _numberFormat = new ParsedFormat('D');
  9. public static bool ParseBool(this ReadOnlySpan<byte> text)
  10. {
  11. if (PrimitiveParser.TryParseBoolean(text, out bool result, out int ignored, SymbolTable.InvariantUtf8))
  12. return result;
  13. throw new SerializationException("Failed to parse Boolean");
  14. }
  15. public static sbyte ParseInt8(this ReadOnlySpan<byte> text)
  16. {
  17. if (PrimitiveParser.TryParseSByte(text, out sbyte result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  18. return result;
  19. throw new SerializationException("Failed to parse Int8");
  20. }
  21. public static short ParseInt16(this ReadOnlySpan<byte> text)
  22. {
  23. if (PrimitiveParser.TryParseInt16(text, out short result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  24. return result;
  25. throw new SerializationException("Failed to parse Int16");
  26. }
  27. public static int ParseInt32(this ReadOnlySpan<byte> text)
  28. {
  29. if (PrimitiveParser.TryParseInt32(text, out int result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  30. return result;
  31. throw new SerializationException("Failed to parse Int32");
  32. }
  33. public static long ParseInt64(this ReadOnlySpan<byte> text)
  34. {
  35. if (PrimitiveParser.TryParseInt64(text, out long result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  36. return result;
  37. throw new SerializationException("Failed to parse Int64");
  38. }
  39. public static byte ParseUInt8(this ReadOnlySpan<byte> text)
  40. {
  41. if (PrimitiveParser.TryParseByte(text, out byte result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  42. return result;
  43. throw new SerializationException("Failed to parse UInt8");
  44. }
  45. public static ushort ParseUInt16(this ReadOnlySpan<byte> text)
  46. {
  47. if (PrimitiveParser.TryParseUInt16(text, out ushort result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  48. return result;
  49. throw new SerializationException("Failed to parse UInt16");
  50. }
  51. public static uint ParseUInt32(this ReadOnlySpan<byte> text)
  52. {
  53. if (PrimitiveParser.TryParseUInt32(text, out uint result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  54. return result;
  55. throw new SerializationException("Failed to parse UInt32");
  56. }
  57. public static ulong ParseUInt64(this ReadOnlySpan<byte> text)
  58. {
  59. if (PrimitiveParser.TryParseUInt64(text, out ulong result, out int ignored, _numberFormat, SymbolTable.InvariantUtf8))
  60. return result;
  61. throw new SerializationException("Failed to parse UInt64");
  62. }
  63. public static char ParseChar(this ReadOnlySpan<byte> text)
  64. {
  65. string str = ParseString(text);
  66. if (str.Length == 1)
  67. return str[0];
  68. throw new SerializationException("Failed to parse Char");
  69. }
  70. public static string ParseString(this ReadOnlySpan<byte> text) => new Utf8String(text).ToString();
  71. public static float ParseSingle(this ReadOnlySpan<byte> text)
  72. {
  73. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  74. return (float)result;
  75. throw new SerializationException("Failed to parse Single");
  76. }
  77. public static double ParseDouble(this ReadOnlySpan<byte> text)
  78. {
  79. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  80. return (double)result;
  81. throw new SerializationException("Failed to parse Double");
  82. }
  83. public static decimal ParseDecimal(this ReadOnlySpan<byte> text)
  84. {
  85. if (PrimitiveParser.TryParseDecimal(text, out decimal result, out int ignored, SymbolTable.InvariantUtf8))
  86. return result;
  87. throw new SerializationException("Failed to parse Decimal");
  88. }
  89. public static DateTime ParseDateTime(this ReadOnlySpan<byte> text)
  90. {
  91. string str = ParseString(text);
  92. if (DateTime.TryParse(str, out var result)) //TODO: Improve perf
  93. return result;
  94. throw new SerializationException("Failed to parse DateTime");
  95. }
  96. public static DateTimeOffset ParseDateTimeOffset(this ReadOnlySpan<byte> text)
  97. {
  98. string str = ParseString(text);
  99. if (DateTimeOffset.TryParse(str, out var result)) //TODO: Improve perf
  100. return result;
  101. throw new SerializationException("Failed to parse DateTimeOffset");
  102. }
  103. }
  104. }