public decimal ToDecimal() { if (_decimalValue.HasValue) { return _decimalValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out decimal value, out _, 'f')) { _decimalValue = value; return value; } throw new InvalidFormatException(); }
public byte ToByte() { if (_byteValue.HasValue) { return(_byteValue.Value); } if (Utf8Parser.TryParse(AsSpan(), out byte value, out _)) { _byteValue = value; return(value); } throw new InvalidFormatException(); }
public float ToSingle() { if (_floatValue.HasValue) { return _floatValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out float value, out _, 'f')) { _floatValue = value; return value; } throw new InvalidFormatException(); }
public double ToDouble() { if (_doubleValue.HasValue) { return _doubleValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out double value, out _, 'f')) { _doubleValue = value; return value; } throw new InvalidFormatException(); }
public uint ToUInt32() { if (_uIntValue.HasValue) { return _uIntValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out uint value, out _)) { _uIntValue = value; return value; } throw new InvalidFormatException(); }
public ulong ToUInt64() { if (_uLongValue.HasValue) { return _uLongValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out ulong value, out _)) { _uLongValue = value; return value; } throw new InvalidFormatException(); }
public short ToInt16() { if (_shortValue.HasValue) { return _shortValue.Value; } if (Utf8Parser.TryParse(AsSpan(), out short value, out _)) { _shortValue = value; return value; } throw new InvalidFormatException(); }
public long ToInt64() { if (_longValue.HasValue) { return(_longValue.Value); } if (Utf8Parser.TryParse(AsSpan(), out long value, out _)) { _longValue = value; return(value); } throw new InvalidFormatException(); }
public int ToInt32() { if (_intValue.HasValue) { return(_intValue.Value); } if (Utf8Parser.TryParse(AsSpan(), out int value, out _)) { _intValue = value; return(value); } throw new InvalidFormatException(); }
unsafe void UnsafeRead(ReadOnlySpan <byte> all) { var _uint32 = sizeof(UInt32); var header = all.Slice(0, sizeof(sbyte) * 80); var triangles = all.Slice(sizeof(sbyte) * 80, sizeof(uint)); var text = Utf8ReadOnlySpanHelper.GetString(header); if (!Utf8Parser.TryParse(triangles, out UInt32 value, out var _)) { throw new Exception("Can't read float"); } }
public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) { // try to parse number directly from bytes ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; if (Utf8Parser.TryParse(span, out long number, out int bytesConsumed) && span.Length == bytesConsumed) return number; // try to parse from a string if the above failed, this covers cases with other escaped/UTF characters if (Int64.TryParse(reader.GetString(), out number)) return number; } // fallback to default handling return reader.GetInt64(); }
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.String) { ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); } if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumTimeSpanFormatLength, MaximumEscapedTimeSpanFormatLength)) { ThrowHelper.ThrowFormatException(DataType.TimeSpan); } scoped ReadOnlySpan <byte> source; if (!reader.HasValueSequence && !reader.ValueIsEscaped) { source = reader.ValueSpan; } else { Span <byte> stackSpan = stackalloc byte[MaximumEscapedTimeSpanFormatLength]; int bytesWritten = reader.CopyString(stackSpan); source = stackSpan.Slice(0, bytesWritten); } byte firstChar = source[0]; if (!JsonHelpers.IsDigit(firstChar) && firstChar != '-') { // Note: Utf8Parser.TryParse allows for leading whitespace so we // need to exclude that case here. ThrowHelper.ThrowFormatException(DataType.TimeSpan); } bool result = Utf8Parser.TryParse(source, out TimeSpan tmpValue, out int bytesConsumed, 'c'); // Note: Utf8Parser.TryParse will return true for invalid input so // long as it starts with an integer. Example: "2021-06-18" or // "1$$$$$$$$$$". We need to check bytesConsumed to know if the // entire source was actually valid. if (!result || source.Length != bytesConsumed) { ThrowHelper.ThrowFormatException(DataType.TimeSpan); } return(tmpValue); }
private bool ConsumeString(byte first) { if (!BEncodingHelpers.IsDigit(first)) { return(false); } ReadOnlySpan <byte> localSpan = _buffer.Slice(Consumed); int index = localSpan.IndexOf(BEncodingConstants.ColonSeparator); if (index == -1) { return(false); } if (first == '0') { if (index != 1) { return(false); } Consumed += 2; ValueSpan = default; } else { if (!Utf8Parser.TryParse(localSpan.Slice(0, index), out int length, out int bytesConsumed) || bytesConsumed != index) { return(false); } int consumed = Consumed + index + 1 + length; if ((uint)consumed > (uint)_buffer.Length) { return(false); } Consumed = consumed; ValueSpan = localSpan.Slice(index + 1, length); } TokenType = BEncodingTokenType.String; return(true); }
private bool ReadNumber() { ReadOnlySpan <byte> localBuffer = this.buffer.Slice(this.Position); int position = 0; bool isDouble = false; for (var remaining = localBuffer.Length; remaining > 0; remaining--, position++) { if (this.IsDelimiter(localBuffer, position, remaining)) { break; } var val = localBuffer[position]; if (val == '.' || val == 'e' || val == 'E') { isDouble = true; } } this.ValueSpan = localBuffer.Slice(0, position); this.AddPosition(position); if (this.ValueSpan.Length > 0) { var last = this.ValueSpan[this.ValueSpan.Length - 1]; if (last == 'f' || last == 'F' || last == 'd' || last == 'D') { isDouble = true; } } if (isDouble) { this.AtomType = TinyhandAtomType.Value_Double; var ret = Utf8Parser.TryParse(this.ValueSpan, out double result, out int bytesConsumed); this.ValueDouble = result; return(ret); } else { this.AtomType = TinyhandAtomType.Value_Long; // var ret = this.ReadInt64(this.ValueSpan, out var result); var ret = Utf8Parser.TryParse(this.ValueSpan, out long result, out int bytesConsumed); this.ValueLong = result; return(ret); } }
internal bool TryGetGuidCore(out Guid value) { ReadOnlySpan <byte> span = stackalloc byte[0]; int maximumLength = _stringHasEscaping ? JsonConstants.MaximumEscapedGuidLength : JsonConstants.MaximumFormatGuidLength; if (HasValueSequence) { long sequenceLength = ValueSequence.Length; if (sequenceLength > maximumLength) { value = default; return(false); } Debug.Assert(sequenceLength <= JsonConstants.MaximumEscapedGuidLength); Span <byte> stackSpan = stackalloc byte[_stringHasEscaping ? JsonConstants.MaximumEscapedGuidLength : JsonConstants.MaximumFormatGuidLength]; ValueSequence.CopyTo(stackSpan); span = stackSpan.Slice(0, (int)sequenceLength); } else { if (ValueSpan.Length > maximumLength) { value = default; return(false); } span = ValueSpan; } if (_stringHasEscaping) { return(JsonReaderHelper.TryGetEscapedGuid(span, out value)); } Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); if (span.Length == JsonConstants.MaximumFormatGuidLength && Utf8Parser.TryParse(span, out Guid tmp, out _, 'D')) { value = tmp; return(true); } value = default; return(false); }
private static LegacyGrainId FromGrainIdInternal(GrainId id) { var typeSpan = id.Type.AsSpan(); if (typeSpan.Length != GrainTypePrefix.LegacyGrainPrefix.Length + 16) { return(null); } if (!typeSpan.StartsWith(GrainTypePrefix.LegacyGrainPrefixBytes.Span)) { return(null); } typeSpan = typeSpan.Slice(GrainTypePrefix.LegacyGrainPrefix.Length, 16); if (!Utf8Parser.TryParse(typeSpan, out ulong typeCodeData, out var len, 'X') || len < 16) { return(null); } string keyExt = null; var keySpan = id.Key.Value.Span; if (keySpan.Length < 32) { return(null); } if (!Utf8Parser.TryParse(keySpan.Slice(0, 16), out ulong n0, out len, 'X') || len < 16) { return(null); } if (!Utf8Parser.TryParse(keySpan.Slice(16, 16), out ulong n1, out len, 'X') || len < 16) { return(null); } if (keySpan.Length > 32) { if (keySpan[32] != '+') { return(null); } keyExt = keySpan.Slice(33).GetUtf8String(); } return(FindOrCreateGrainId(UniqueKey.NewKey(n0, n1, typeCodeData, keyExt))); }
private void AppendContentLength(ReadOnlySpan <byte> value) { if (_contentLength.HasValue) { BadHttpRequestException.Throw(RequestRejectionReason.MultipleContentLengths); } if (!Utf8Parser.TryParse(value, out long parsed, out var consumed) || parsed < 0 || consumed != value.Length) { BadHttpRequestException.Throw(RequestRejectionReason.InvalidContentLength, value.GetRequestHeaderStringNonNullCharacters(_useLatin1)); } _contentLength = parsed; }
static void ParseInt32BufferReaderRaw() { foreach (var iteration in Benchmark.Iterations) { var buffer = new ReadOnlyBuffer <byte>(s_data); var reader = BufferReader.Create(buffer); using (iteration.StartMeasurement()) { while (Utf8Parser.TryParse(reader.CurrentSegment.Slice(reader.ConsumedBytes), out int value, out int consumed)) { reader.Advance(consumed + 1); } } } }
static void ParseInt32Utf8Parser() { var data = new ReadOnlySpan <byte>(s_data); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { int totalConsumed = 0; while (Utf8Parser.TryParse(data.Slice(totalConsumed), out int value, out int consumed)) { totalConsumed += consumed + 1; } } } }
public bool TryGetUInt32(Utf8Span property, out uint value) { var jsonProperty = new JsonProperty(this, property); if (!_properties.TryGetValue(jsonProperty, out JsonValue jsonValue)) { value = default; return(false); } if (jsonValue.Type != JsonValueType.Number) { throw new InvalidOperationException(); } return(Utf8Parser.TryParse(jsonValue.Value.Bytes, out value, out _)); }
static void ParseInt32ReadableBufferReader() { foreach (var iteration in Benchmark.Iterations) { var buffer = ReadableBuffer.Create(s_data); var reader = new ReadableBufferReader(buffer); using (iteration.StartMeasurement()) { while (Utf8Parser.TryParse(reader.Span.Slice(reader.ConsumedBytes), out int value, out int consumed)) { reader.Skip(consumed + 1); } } } }
public static float ParseFloat(ReadOnlySpan <char> str) { Span <byte> bytes = stackalloc byte[str.Length]; int numBytes = Encoding.UTF8.GetBytes(str, bytes); if (numBytes > str.Length) { throw new InvalidOperationException(); } if (!Utf8Parser.TryParse(bytes, out float value, out int _)) { throw new InvalidOperationException(); } return(value); }
public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) { ReadOnlySpan <byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; if (Utf8Parser.TryParse(span, out long number, out int bytesConsumed) && span.Length == bytesConsumed) { return(number); } if (Int64.TryParse(reader.GetString(), out number)) { return(number); } } return(reader.GetInt64()); }
protected override Guid ParseLiteral(StringValueNode valueSyntax) { if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid g, out _, _format[0])) { return(g); } if (!_enforceFormat && Guid.TryParse(valueSyntax.Value, out g)) { return(g); } throw new SerializationException( TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()), this); }
public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed) { return(number); } var data = reader.GetString(); throw new InvalidOperationException($"'{data}' is not a correct expected value!") { Source = "LongToStringJsonConverter" }; }
private static void PrimitiveParserByteSpanToBool(string text) { byte[] utf8ByteArray = Text.Encoding.UTF8.GetBytes(text); ReadOnlySpan <byte> utf8ByteSpan = new ReadOnlySpan <byte>(utf8ByteArray); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < TestHelper.LoadIterations; i++) { Utf8Parser.TryParse(utf8ByteSpan, out bool value, out int bytesConsumed); } } } }
private void AppendContentLength(ReadOnlySpan <byte> value) { if (_contentLength.HasValue) { KestrelBadHttpRequestException.Throw(RequestRejectionReason.MultipleContentLengths); } if (!Utf8Parser.TryParse(value, out long parsed, out var consumed) || parsed < 0 || consumed != value.Length) { KestrelBadHttpRequestException.Throw(RequestRejectionReason.InvalidContentLength, value.GetRequestHeaderString(HeaderNames.ContentLength, EncodingSelector)); } _contentLength = parsed; }
private void AppendContentLength(Span <byte> value) { if (_contentLength.HasValue) { BadProtoRequestException.Throw(RequestRejectionReason.MultipleContentLengths); } if (!Utf8Parser.TryParse(value, out long parsed, out var consumed) || parsed < 0 || consumed != value.Length) { BadProtoRequestException.Throw(RequestRejectionReason.InvalidContentLength, value.GetAsciiOrUTF8StringNonNullCharacters()); } _contentLength = parsed; }
/// <summary> /// Returns the <see cref="Guid"/> representation of a grain primary key. /// </summary> internal static bool TryGetGuidKey(this GrainId grainId, out Guid key) { var keyString = grainId.Key.AsSpan(); if (keyString.Length > 32 && keyString[32] == (byte)'+') { keyString = keyString.Slice(0, 32); } else if (keyString.Length != 32) { key = default; return(false); } return(Utf8Parser.TryParse(keyString, out key, out var len, 'N') && len == 32); }
internal static unsafe Guid Read(ref ProtoReader.State state) { // note: we can't use a stackalloc-span here because the compiler doesn't trust // state, which is a ref-local (and can store spans), not to store it; since we *don't* // do that, we can be evil byte *ptr = stackalloc byte[MAX_LENGTH]; var available = state.ReadBytes(new Span <byte>(ptr, MAX_LENGTH)); char standardFormat; switch (available.Length) { case 0: return(Guid.Empty); case 16: // treat as big-endian bytes // expand those bytes to hex, backwards so we don't overwrite live data int write = 32; for (int i = 15; i >= 0; i--) { var val = ptr[i]; ptr[--write] = ToHex(val & 0b1111); ptr[--write] = ToHex((val >> 4) & 0b1111); } available = new Span <byte>(ptr, 32); standardFormat = 'N'; break; case 32: // no hyphens standardFormat = 'N'; break; case 36: // hyphens standardFormat = 'D'; break; default: ThrowHelper.Format($"Unexpected Guid length: {available.Length}"); return(default); } if (!(Utf8Parser.TryParse(available, out Guid guid, out int bytes, standardFormat) && bytes == available.Length)) { ThrowHelper.Format($"Failed to read Guid: '{Encoding.UTF8.GetString(ptr, available.Length)}'"); } return(guid);