public Stref(Stringe stringe, string str) { String = str; Chares = new Chare[str.Length]; Bases = new bool[str.Length]; var elems = StringInfo.GetTextElementEnumerator(str); while (elems.MoveNext()) { Bases[elems.ElementIndex] = true; } var length = str.Length; int line = 1; int col = 1; for (int i = 0; i < length; i++) { if (str[i] == '\n') { line++; col = 1; } else if (Bases[i]) // Advance column only for non-combining characters { col++; } Chares[i] = new Chare(stringe, str[i], i, line, col); } }
internal Chare(Stringe source, char c, int offset) { _src = source; _character = c; _offset = offset; _line = _column = 0; }
private Stringe(Stringe parent, int relativeOffset, int length) { _stref = parent._stref; _offset = parent._offset + relativeOffset; _length = length; _substring = null; }
/// <summary> /// Determines whether the current stringe is a substringe of the specified parent stringe. /// </summary> /// <param name="parent">The parent stringe to compare to.</param> /// <returns></returns> public bool IsSubstringeOf(Stringe parent) { if (_stref != parent._stref) { return(false); } return(_offset >= parent._offset && _offset + _length <= parent._offset + parent._length); }
internal Stringe(Stringe value) { _stref = value._stref; _offset = value._offset; _length = value._length; _offset = value._offset; _substring = value._substring; }
internal Chare(Stringe source, char c, int offset, int line, int col) { _src = source; _character = c; _offset = offset; _line = line; _column = col; }
/// <summary> /// Tokenizes the input stringe and enumerates the resulting tokens. /// </summary> /// <param name="input">The input to tokenize.</param> /// <returns></returns> public IEnumerable <Token <T> > Tokenize(Stringe input) { var reader = new StringeReader(input); while (!reader.EndOfStringe) { yield return(reader.ReadToken(this)); } }
internal Stringe(Stringe value) { _stref = value._stref; _offset = value._offset; _length = value._length; _line = value._line; _column = value._column; _offset = value._offset; _substring = value._substring; }
/// <summary> /// Returns a stringe whose endpoints are the specified stringes. The stringes must both belong to the same parent string. /// </summary> /// <param name="a">The first stringe.</param> /// <param name="b">The second stringe.</param> /// <returns></returns> public static Stringe Range(Stringe a, Stringe b) { if (a == null) { throw new ArgumentNullException("a"); } if (b == null) { throw new ArgumentNullException("b"); } if (a._stref != b._stref) { throw new ArgumentException("The stringes do not belong to the same parent."); } if (a == b) { return(a); } if (a.IsSubstringeOf(b)) { return(b); } if (b.IsSubstringeOf(a)) { return(a); } // Right side of A intersects left side of B. if (a._offset > b._offset && a._offset + a._length < b._offset + b._length) { return(a.Substringe(0, b._offset + b._length - a._offset)); } // Left side of A intersects right side of B. if (a._offset < b._offset + b._length && a._offset > b._offset) { return(b.Substringe(0, a._offset + a._length - b._offset)); } // A is to the left of B. if (a._offset + a._length <= b._offset) { return(a.Substringe(0, b._offset + b._length - a._offset)); } // B is to the left of A. if (b._offset + b._length <= a._offset) { return(b.Substringe(0, a._offset + a._length - b._offset)); } return(null); }
/// <summary> /// Indicates whether the specified regular expression matches the input at the reader's current position, and outputs the result. /// </summary> /// <param name="regex">The regular expression to test for.</param> /// <param name="result">The stringe to output the result to.</param> /// <returns></returns> public bool IsNext(Regex regex, out Stringe result) { if (regex == null) { throw new ArgumentNullException("regex"); } result = null; var match = regex.Match(_stringe.Value, _pos); if (!match.Success || match.Index != _pos) { return(false); } result = _stringe.Substringe(_pos, match.Length); return(true); }
private Stringe(Stringe parent, int relativeOffset, int length) { _stref = parent._stref; _offset = parent._offset + relativeOffset; _length = length; _substring = null; // Calculate line/col _line = parent._line; _column = parent._column; // If the offset is to the left, the line/col is already calculated. Fetch it from the Chare cache. if (relativeOffset < 0) { _line = _stref.Chares[_offset].Line; _column = _stref.Chares[_offset].Column; return; } if (relativeOffset == 0) { return; // Do nothing if the offset is the same } int aOffset; for (int i = 0; i < relativeOffset; i++) { aOffset = parent._offset + i; if (_stref.String[aOffset] == '\n') { _line++; _column = 1; } else if (_stref.Bases[i]) // Advance column only for non-combining characters { _column++; } if (_stref.Chares[aOffset] == null) { _stref.Chares[aOffset] = new Chare(parent, _stref.String[aOffset], aOffset, _line, _column); } } }
/// <summary> /// Indicates whether the specified stringe is null or empty. /// </summary> /// <param name="stringe">The stringe to test.</param> /// <returns></returns> public static bool IsNullOrEmpty(Stringe stringe) { return(stringe == null || stringe.Length == 0); }
/// <summary> /// Returns an empty stringe based on the position of another stringe. /// </summary> /// <param name="basis">The basis stringe to get position info from.</param> /// <returns></returns> public static Stringe Empty(Stringe basis) { return(new Stringe(basis, 0, 0)); }
public static Match Match(this Regex regex, Stringe value, int startat = 0) { return(regex.Match(value.Value, startat)); }
public Token(T id, Stringe value) : base(value) { _id = id; }
/// <summary> /// Creates a new StringeReader instance using the specified stringe as input. /// </summary> /// <param name="value">The stringe to use as input.</param> public StringeReader(Stringe value) { _stringe = value; _pos = 0; }
/// <summary> /// Creates a new StringeReader instance using the specified string as input. /// </summary> /// <param name="value">The string to use as input. This will be converted to a root-level stringe.</param> public StringeReader(string value) { _stringe = value.ToStringe(); _pos = 0; }