public void StringSlice_Basics() { StringSlice slice = "Hello, there!"; Assert.Equal(13, slice.Length); Assert.Equal('H', slice[0]); Assert.Equal(' ', slice[6]); Assert.True(slice.StartsWith('H')); Assert.False(slice.StartsWith('h')); Assert.True(slice.StartsWith("Hello")); Assert.False(slice.StartsWith("hello")); Assert.True(slice.StartsWith("hello", StringComparison.OrdinalIgnoreCase)); slice = slice.Substring(7, 3); Assert.Equal(3, slice.Length); Assert.Equal(0, slice.CompareTo("the")); StringBuilder result = new StringBuilder(); slice.AppendTo(result); Assert.Equal(3, result.Length); Assert.Equal("the", result.ToString()); slice = slice.Substring(3); Assert.Equal(0, slice.Length); }
/// <summary> /// Attempts to parse a modification. /// </summary> static public bool TryParse(StringSlice inData, out VariantModification outModification) { inData = inData.Trim(); VariantModifyOperator op = default(VariantModifyOperator); int operatorIdx = -1; int operatorLength = 0; VariantUtils.TryFindOperator(inData, '=', VariantModifyOperator.Set, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, '+', VariantModifyOperator.Add, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, '-', VariantModifyOperator.Subtract, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, '*', VariantModifyOperator.Multiply, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, '/', VariantModifyOperator.Divide, ref operatorIdx, ref op, ref operatorLength); if (operatorIdx < 0) { outModification = default(VariantModification); return(false); } outModification.Operator = op; StringSlice key = inData.Substring(0, operatorIdx); StringSlice operand = inData.Substring(operatorIdx + 1); if (!TableKeyPair.TryParse(key, out outModification.VariableKey)) { outModification = default(VariantModification); return(false); } return(VariantOperand.TryParse(operand, out outModification.Operand)); }
static private void SetReflectedObject(UnityEngine.Object inRoot, string inPath, object inValue) { inPath = inPath.Replace("Array.data", ""); // locate the parent object StringSlice path = inPath; int lastDot = path.LastIndexOf('.'); if (lastDot < 0) { return; } StringSlice parentPath = path.Substring(0, lastDot); StringSlice fieldName = path.Substring(lastDot + 1); object parentObject = LocateReflectedObject(inRoot, parentPath.ToString()); if (parentObject == null) { return; } Type objType = parentObject.GetType(); int arrayIdx = -1; // capture array if (fieldName.EndsWith(']')) { int elementIndexStart = fieldName.IndexOf('['); int length = fieldName.Length - elementIndexStart - 1; StringSlice elementIndexSlice = fieldName.Substring(elementIndexStart + 1, length); arrayIdx = Convert.ToInt32(elementIndexSlice.ToString()); fieldName = fieldName.Substring(0, elementIndexStart); } FieldInfo field = objType.GetField(fieldName.ToString(), InstanceBindingFlags); if (field == null) { return; } if (arrayIdx >= 0) { IList list = field.GetValue(parentObject) as IList; if (list != null) { list[arrayIdx] = inValue; } return; } field.SetValue(parentObject, inValue); }
/// <summary> /// Returns the branch name for source control. /// </summary> static public string GetSourceControlBranchName() { try { if (Directory.Exists(".git")) { string headData = File.ReadAllText(".git/HEAD"); foreach (var line in StringSlice.EnumeratedSplit(headData, new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) { TagData tag = TagData.Parse(line, TagStringParser.RichTextDelimiters); if (tag.Id == "ref") { StringSlice data = tag.Data; if (data.StartsWith("refs/heads/")) { data = data.Substring(11); } return(data.ToString()); } } } // TODO: More version control types? svn? return(null); } catch (Exception e) { Debug.LogException(e); return(null); } }
public void Substring() { var str = new StringSlice("hello cruel world!"); var sub = str.Substring(6, 5); Assert.That(sub.ToString(), Is.EqualTo("cruel")); }
public void IndexOfString() { var str = new StringSlice("hello cruel world!"); Assert.That(str.IndexOf("cru"), Is.EqualTo(6)); str = str.Substring(5); Assert.That(str.IndexOf("rld"), Is.EqualTo(9)); }
public void IndexOfAny() { var str = new StringSlice("hello cruel world!"); Assert.That(str.IndexOfAny(new[] { 'r', 'w', 'd' }), Is.EqualTo(7)); str = str.Substring(5); Assert.That(str.IndexOfAny(new[] { 'o', 'w', 'd' }), Is.EqualTo(7)); }
public void IndexOfChar() { var str = new StringSlice("hello cruel world!"); Assert.That(str.IndexOf('r'), Is.EqualTo(7)); str = str.Substring(5); Assert.That(str.IndexOf('o'), Is.EqualTo(8)); }
/// <summary> /// Attempts to parse a StringSlice into a Variant Key. /// Valid formats are: /// <list type="bullet"> /// <item>variableId</item> /// <item>tableId:variableId</item> /// </list> /// </summary> static public bool TryParse(StringSlice inSource, out TableKeyPair outKey) { if (inSource.IsEmpty) { outKey = default(TableKeyPair); return(false); } inSource = inSource.Trim(); int operatorIdx = inSource.IndexOf(TableOperator); if (operatorIdx >= 0) { int variantIdx = operatorIdx + TableOperator.Length; if (variantIdx >= inSource.Length) { outKey = default(TableKeyPair); return(false); } StringSlice tableId = inSource.Substring(0, operatorIdx).TrimEnd(); StringSlice variantId = inSource.Substring(variantIdx).TrimStart(); if (!VariantUtils.IsValidIdentifier(tableId) || !VariantUtils.IsValidIdentifier(variantId)) { outKey = default(TableKeyPair); return(false); } outKey = new TableKeyPair(tableId, variantId); return(true); } if (!VariantUtils.IsValidIdentifier(inSource)) { outKey = default(TableKeyPair); return(false); } outKey = new TableKeyPair(inSource); return(true); }
static private string GetLocationFromTrace(string inTrace) { foreach (var line in StringSlice.EnumeratedSplit(inTrace, StringUtils.DefaultNewLineChars, StringSplitOptions.RemoveEmptyEntries)) { int atIndex = line.IndexOf(" (at "); if (atIndex > 0) { StringSlice method = line.Substring(0, atIndex).Trim(); StringSlice location = line.Substring(atIndex + 5); location = location.Substring(0, location.Length - 1).Trim(); // ignore locations with < or >, these are internal and not helpfuls if (location.Contains('<') || location.Contains('>')) { continue; } int param = method.IndexOf('('); if (param > 0) { method = method.Substring(0, param).Trim(); } int lineNum = 0; int colon = location.IndexOf(':'); if (colon > 0) { StringSlice lineNumStr = location.Substring(colon + 1); lineNum = StringParser.ParseInt(lineNumStr); location = location.Substring(0, colon).Trim(); } int lastSlash = location.LastIndexOf('/'); if (lastSlash >= 0) { location = location.Substring(lastSlash + 1); } return(string.Format("{0} @{1}:{2}", method, lastSlash, lineNum)); } } return(StackTraceDisabledMessage); }
/// <summary> /// Finds a conversion matching the longest version of the given <paramref name="text"/> from the left. /// </summary> /// <param name="text">The text to find a matching input conversion for.</param> /// <returns>The best matching input conversion.</returns> /// <seealso cref="MultiReplacementEntry"/> internal MultiReplacementEntry FindLargestMatchingConversion(StringSlice text) { for (var searchLength = text.Length; searchLength > 0; searchLength--) { MultiReplacementEntry entry = null; if (replacements.TryGetValue(text.Substring(0, searchLength), out entry)) { return(entry); } } return(null); }
/// <summary> /// Returns the parent property. /// </summary> static public SerializedProperty FindPropertyParent(this SerializedProperty inProperty) { StringSlice path = inProperty.propertyPath.Replace("Array.data", ""); int lastDot = path.LastIndexOf('.'); if (lastDot < 0) { return(null); } StringSlice parentPath = path.Substring(0, lastDot); return(inProperty.serializedObject.FindProperty(parentPath.ToString())); }
private void EmptyChecks(StringSlice empty) { // Length should be zero Assert.Equal(0, empty.Length); // StartsWith should fail gracefully Assert.False(empty.StartsWith(':')); Assert.False(empty.StartsWith((StringSlice)":")); // CompareTo should fail gracefully Assert.Equal(-1, empty.CompareTo(":")); // AppendTo shouldn't do anything StringBuilder result = new StringBuilder(); empty.AppendTo(result); Assert.Equal(0, result.Length); // Substring(0) should work StringSlice other = empty.Substring(0); }
/// <summary> /// Returns if the given string contains any text or rich text tags. /// </summary> static public bool ContainsText(StringSlice inString, IDelimiterRules inDelimiters, IEnumerable <string> inTextTags = null) { bool bTrackRichText = inDelimiters.RichText; bool bTrackTags = !bTrackRichText || !HasSameDelims(inDelimiters, RichTextDelimiters); int length = inString.Length; int charIdx = 0; int richStart = -1; int tagStart = -1; int copyStart = 0; while (charIdx < length) { if (bTrackRichText) { if (inString.AttemptMatch(charIdx, "<")) { richStart = charIdx; } else if (inString.AttemptMatch(charIdx, ">")) { return(true); } } if (bTrackTags) { if (inString.AttemptMatch(charIdx, inDelimiters.TagStartDelimiter)) { tagStart = charIdx; } else if (inString.AttemptMatch(charIdx, inDelimiters.TagEndDelimiter)) { StringSlice check = inString.Substring(copyStart, tagStart - copyStart); if (!check.IsWhitespace) { return(true); } if (inTextTags != null) { TagData data = TagData.Parse(inString.Substring(tagStart, charIdx - tagStart + 1), inDelimiters); foreach (var tag in inTextTags) { if (data.Id == tag) { return(true); } } } copyStart = charIdx + 1; tagStart = -1; richStart = -1; } } ++charIdx; } StringSlice finalCheck = inString.Substring(copyStart, length - copyStart); return(!finalCheck.IsWhitespace); }
public static int FindParamPosition(CompilerState State, StringSlice Code) { var Ok = false; var Handlers = State.Language.GlobalHandlers; var RSM = new ResultSkippingManager(Handlers, Code); var Language = State.Language; var LangRoot = Language.Root; var CastRecognizer = Language.Root.GetObject <CastRecognizer>(false); while (RSM.Loop()) { var Pos = RSM.Current; var Chr = Code[Pos]; if (!Helper.IsIdChar(Chr)) { if (!Ok) { continue; } if (Chr == '(') { var Brackets = Code.Substring(Pos); if (CastRecognizer != null && CastRecognizer.IsCastString(State, Brackets)) { return(Pos); } if (Brackets.GetBracketPos(false, Handlers) == Brackets.Length - 1) { return(Pos); } } else if (char.IsWhiteSpace(Chr)) { // ambiguous: Print (Obj is object).ToString() var After = Code.Substring(Pos + 1).Trim(); var AfterChr = After.Length > 0 ? After[0] : '\0'; if (Helper.IsIdChar(AfterChr) || AfterChr == '(' || AfterChr == '[') { return(Pos); } } if (Code.SubstringEquals(Pos, LangRoot.OnlyRight, LangRoot.OnlyRightSkip, false, new IdCharCheck(true)).Position != -1) { return(Pos); } for (var i = 0; i < Language.ExprRecognizers.Length; i++) { var Rec = Language.ExprRecognizers[i] as SkippedBetweenRecognizer; if (Rec != null && Code.SubstringEquals(Pos, Rec.Operators, Rec.Skip).Index != -1) { return(Pos); } } } else { Ok = true; } } return(-1); }
static private LineResult ParseLine(ref ParseState ioState, StringSlice ioLine) { StringSlice lineContents = ioLine.TrimStart(BlockParser.TrimCharsWithSpace).TrimEnd(BlockParser.TrimCharsWithoutSpace); StringSlice lineComment = StringSlice.Empty; if (lineContents.IsEmpty) { if (ioState.CurrentState != BlockState.InData) { return(LineResult.Empty); } } int commentIdx = lineContents.IndexOf(ioState.Rules.CommentPrefix); if (commentIdx >= 0) { lineComment = lineContents.Substring(commentIdx + ioState.Rules.CommentPrefix.Length).TrimStart(BlockParser.TrimCharsWithSpace); lineContents = lineContents.Substring(0, commentIdx).TrimEnd(BlockParser.TrimCharsWithSpace); } try { bool bSuccess = true; bool bProcessedCommand = false; if (!lineContents.IsEmpty) { for (int i = 0; i < ioState.PrefixPriorities.Length && !bProcessedCommand; ++i) { PrefixType type = ioState.PrefixPriorities[i]; switch (type) { case PrefixType.BlockId: { if (lineContents.StartsWith(ioState.Rules.BlockIdPrefix)) { lineContents = lineContents.Substring(ioState.Rules.BlockIdPrefix.Length); bSuccess &= TryStartBlock(ref ioState, lineContents); bProcessedCommand = true; } break; } case PrefixType.BlockMeta: { if (ShouldCheckMeta(ref ioState) && lineContents.StartsWith(ioState.Rules.BlockMetaPrefix)) { lineContents = lineContents.Substring(ioState.Rules.BlockMetaPrefix.Length); bSuccess &= TryEvaluateMeta(ref ioState, lineContents); bProcessedCommand = true; } break; } case PrefixType.BlockHeaderEnd: { if (lineContents.StartsWith(ioState.Rules.BlockHeaderEndPrefix)) { lineContents = lineContents.Substring(ioState.Rules.BlockHeaderEndPrefix.Length); bSuccess &= TryEndHeader(ref ioState, lineContents); bProcessedCommand = true; } break; } case PrefixType.BlockContent: { if (lineContents.StartsWith(ioState.Rules.BlockContentPrefix)) { lineContents = lineContents.Substring(ioState.Rules.BlockContentPrefix.Length); bSuccess &= TryAddContent(ref ioState, lineContents); bProcessedCommand = true; } break; } case PrefixType.BlockEnd: { if (lineContents.StartsWith(ioState.Rules.BlockEndPrefix)) { lineContents = lineContents.Substring(ioState.Rules.BlockEndPrefix.Length); bSuccess &= TryEndBlock(ref ioState, lineContents); bProcessedCommand = true; } break; } case PrefixType.PackageMeta: { if (ShouldCheckPackageMeta(ref ioState) && lineContents.StartsWith(ioState.Rules.PackageMetaPrefix)) { lineContents = lineContents.Substring(ioState.Rules.PackageMetaPrefix.Length); bSuccess &= TryEvaluatePackage(ref ioState, lineContents); bProcessedCommand = true; } break; } } } } if (!bProcessedCommand) { if (ioState.CurrentState == BlockState.InData && ioState.Rules.RequireExplicitBlockContent && !string.IsNullOrEmpty(ioState.Rules.BlockContentPrefix)) { BlockParser.LogError(ioState.Position, "Cannot add content '{0}', must have content prefix '{1}'", lineContents, ioState.Rules.BlockContentPrefix); bSuccess = false; } else if (!lineContents.IsEmpty || ioState.CurrentState == BlockState.InData) { bSuccess &= TryAddContent(ref ioState, lineContents); } } if (!lineComment.IsEmpty) { bSuccess &= TryAddComment(ref ioState, lineComment); } ioState.Error |= !bSuccess; return(bSuccess ? LineResult.Error : LineResult.NoError); } catch (Exception e) { UnityEngine.Debug.LogException(e); return(LineResult.Exception); } }
/// <summary> /// Parses a tag's contents into data. /// </summary> static public void Parse(StringSlice inSlice, IDelimiterRules inDelimiters, out TagData outTagData) { if (inDelimiters == null) { throw new ArgumentNullException("inDelimiters"); } StringSlice tag = inSlice; tag = tag.Trim(MinimalWhitespaceChars); bool bRemovedTagBoundaries = false; if (tag.StartsWith(inDelimiters.TagStartDelimiter)) { tag = tag.Substring(inDelimiters.TagStartDelimiter.Length); bRemovedTagBoundaries = true; } if (tag.EndsWith(inDelimiters.TagEndDelimiter)) { tag = tag.Substring(0, tag.Length - inDelimiters.TagEndDelimiter.Length); bRemovedTagBoundaries = true; } if (bRemovedTagBoundaries) { tag = tag.Trim(MinimalWhitespaceChars); } if (inSlice.Length == 0) { outTagData = default(TagData); return; } ClosingTagState closeState = 0; char endDelim = inDelimiters.RegionCloseDelimiter; if (endDelim != 0) { if (tag.StartsWith(endDelim)) { closeState |= ClosingTagState.Start; tag = tag.Substring(1); } if (tag.EndsWith(endDelim)) { closeState |= ClosingTagState.End; tag = tag.Substring(0, tag.Length - 1); } } if (closeState != 0) { tag = tag.Trim(MinimalWhitespaceChars); } char[] dataDelims = inDelimiters.TagDataDelimiters; int dataDelimIdx = tag.Length; foreach (var delim in dataDelims) { int idx = tag.IndexOf(delim); if (idx >= 0 && idx < dataDelimIdx) { dataDelimIdx = idx; if (idx <= 0) { break; } } } if (dataDelimIdx >= tag.Length) { outTagData.Id = tag; outTagData.Data = StringSlice.Empty; } else { outTagData.Id = tag.Substring(0, dataDelimIdx).TrimEnd(MinimalWhitespaceChars); outTagData.Data = tag.Substring(dataDelimIdx).TrimStart(dataDelims).TrimStart(MinimalWhitespaceChars); } outTagData.m_CloseState = closeState; }
/// <summary> /// Attempts to parse a comparison. /// </summary> static public bool TryParse(StringSlice inData, out VariantComparison outComparison) { inData = inData.Trim(); VariantCompareOperator op = VariantCompareOperator.True; int operatorIdx = -1; int operatorLength = 0; VariantUtils.TryFindOperator(inData, EqualsOperator, VariantCompareOperator.EqualTo, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, NotEqualOperator, VariantCompareOperator.NotEqualTo, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, GreaterThanOrEqualToOperator, VariantCompareOperator.GreaterThanOrEqualTo, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, LessThanOrEqualToOperator, VariantCompareOperator.LessThanOrEqualTo, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, GreaterThanOperator, VariantCompareOperator.GreaterThan, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, LessThanOperator, VariantCompareOperator.LessThan, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, NotOperator, VariantCompareOperator.False, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, ShortEqualsOperator, VariantCompareOperator.EqualTo, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, ExistsOperator, VariantCompareOperator.Exists, ref operatorIdx, ref op, ref operatorLength); VariantUtils.TryFindOperator(inData, DoesNotExistOperator, VariantCompareOperator.DoesNotExist, ref operatorIdx, ref op, ref operatorLength); if (operatorIdx < 0) { op = VariantCompareOperator.True; } outComparison.Operator = op; StringSlice idSlice = StringSlice.Empty; StringSlice operandSlice = StringSlice.Empty; bool bRequiresOperand = true; switch (op) { case VariantCompareOperator.False: { idSlice = inData.Substring(1).TrimStart(); bRequiresOperand = false; break; } case VariantCompareOperator.True: { idSlice = inData; bRequiresOperand = false; break; } case VariantCompareOperator.Exists: { idSlice = inData.Substring(0, operatorIdx).TrimEnd(); bRequiresOperand = false; break; } case VariantCompareOperator.DoesNotExist: { idSlice = inData.Substring(0, operatorIdx).TrimEnd(); bRequiresOperand = false; break; } default: { idSlice = inData.Substring(0, operatorIdx).TrimEnd(); operandSlice = inData.Substring(operatorIdx + operatorLength).TrimStart(); break; } } if (!VariantOperand.TryParse(idSlice, out outComparison.Left)) { outComparison = default(VariantComparison); return(false); } if (!bRequiresOperand) { outComparison.Right = default(VariantOperand); return(true); } return(VariantOperand.TryParse(operandSlice, out outComparison.Right)); }
public static StringSlice SliceNextPart(ref StringSlice text) { StringSlice s = text.TrimStart(MyWhitespace); if (s.Length > 0) { if (s[0] == '(' || s[0] == ')' || s[0] == ',' || s[0] == ';') { StringSlice result = s.Substring(0, 1); text = s.Substring(1); return(result); } if (s[0] == '\'') { bool prevsquot = false; for (int i = 1; ; i++) { if (i >= s.Length) { if (prevsquot) { StringSlice result = s; text = StringSlice.Prepare(); return(result); } throw new Exception("Expected terminating single quote: " + s); } if (s[i] == '\'') { if (prevsquot) { prevsquot = false; } else { prevsquot = true; } } else if (prevsquot) { if (s[i] == ' ') { StringSlice result = s.Substring(0, i); text = s.Substring(i + 1); return(result); } else // Text directly after. { StringSlice result = s.Substring(0, i); text = s.Substring(i); return(result); } } } } } for (int i = 0; ; i++) { if (i >= s.Length) { StringSlice result = s; text = StringSlice.Prepare(); return(result); } if (char.IsWhiteSpace(s[i])) { StringSlice result = s.Substring(0, i); text = s.Substring(i + 1); return(result); } if (!char.IsLetterOrDigit(s[i]) && '_' != s[i] && '.' != s[i]) { if (i > 0) { StringSlice result = s.Substring(0, i); text = s.Substring(i); return(result); } { i++; // Return this symbol. StringSlice result = s.Substring(0, i); text = s.Substring(i); return(result); } } } }