public static IEnumerable <Utf8String> Split(this Utf8String src, byte delemeter) { var start = 0; var p = new Utf8Iterator(src.Bytes); while (p.MoveNext()) { if (p.Current == delemeter) { if (p.BytePosition - start == 0) { yield return(default(Utf8String)); } else { yield return(src.Subbytes(start, p.BytePosition - start)); } start = p.BytePosition + 1; } } if (start < p.BytePosition) { yield return(src.Subbytes(start, p.BytePosition - start)); } }
public static UInt64 ToUInt64(this Utf8String src) { UInt64 value = 0; var p = new Utf8Iterator(src.Bytes); while (p.MoveNext()) { var b = p.Current; switch (b) { case 0x30: value = (UInt64)(value * 10); break; case 0x31: value = (UInt64)(value * 10 + 1); break; case 0x32: value = (UInt64)(value * 10 + 2); break; case 0x33: value = (UInt64)(value * 10 + 3); break; case 0x34: value = (UInt64)(value * 10 + 4); break; case 0x35: value = (UInt64)(value * 10 + 5); break; case 0x36: value = (UInt64)(value * 10 + 6); break; case 0x37: value = (UInt64)(value * 10 + 7); break; case 0x38: value = (UInt64)(value * 10 + 8); break; case 0x39: value = (UInt64)(value * 10 + 9); break; default: throw new ArgumentOutOfRangeException(); } } return(value); }
public static Utf8String From(string src, Byte[] bytes) { var required = src.Sum(c => Utf8Iterator.ByteLengthFromChar(c)); if (required > bytes.Length) { throw new OverflowException(); } int pos = 0; foreach (var c in src) { if (c <= Utf8Iterator.Mask7) { // 1bit bytes[pos++] = (byte)c; } else if (c <= Utf8Iterator.Mask11) { // 2bit bytes[pos++] = (byte)(Utf8Iterator.Head2 | Utf8Iterator.Mask5 & (c >> 6)); bytes[pos++] = (byte)(Utf8Iterator.Head1 | Utf8Iterator.Mask6 & (c)); } else { // 3bit bytes[pos++] = (byte)(Utf8Iterator.Head3 | Utf8Iterator.Mask4 & (c >> 12)); bytes[pos++] = (byte)(Utf8Iterator.Head1 | Utf8Iterator.Mask6 & (c >> 6)); bytes[pos++] = (byte)(Utf8Iterator.Head1 | Utf8Iterator.Mask6 & (c)); } } return(new Utf8String(new ArraySegment <byte>(bytes, 0, pos))); }
public static bool TrySearchAscii(this Utf8String src, Byte target, int start, out int pos) { var p = new Utf8Iterator(src.Bytes, start); while (p.MoveNext()) { var b = p.Current; if (b <= 0x7F) { // ascii if (b == target /*'\"'*/) { // closed pos = p.BytePosition; return(true); } else if (b == '\\') { // escaped switch ((char)p.Second) { case '"': // fall through case '\\': // fall through case '/': // fall through case 'b': // fall through case 'f': // fall through case 'n': // fall through case 'r': // fall through case 't': // fall through // skip next p.MoveNext(); break; case 'u': // unicode // skip next 4 p.MoveNext(); p.MoveNext(); p.MoveNext(); p.MoveNext(); break; default: // unkonw escape throw new ParserException("unknown escape: " + p.Second); } } } } pos = -1; return(false); }
public static Int32 ToInt32(this Utf8String src) { Int32 value = 0; Int32 sign = 1; var p = new Utf8Iterator(src.Bytes); bool isFirst = true; while (p.MoveNext()) { var b = p.Current; if (isFirst) { isFirst = false; if (b == '-') { sign = -1; continue; } } switch (b) { case 0x30: value = value * 10; break; case 0x31: value = value * 10 + 1; break; case 0x32: value = value * 10 + 2; break; case 0x33: value = value * 10 + 3; break; case 0x34: value = value * 10 + 4; break; case 0x35: value = value * 10 + 5; break; case 0x36: value = value * 10 + 6; break; case 0x37: value = value * 10 + 7; break; case 0x38: value = value * 10 + 8; break; case 0x39: value = value * 10 + 9; break; default: throw new ArgumentOutOfRangeException(); } } return(value * sign); }