// Splits a string into lines based on static private IEnumerable <StringSlice> SplitIntoLines(IBlockParsingRules inRules, StringSlice inString) { if (inRules.CustomLineSplitter != null) { return(inString.EnumeratedSplit(inRules.CustomLineSplitter, StringSplitOptions.None)); } return(inString.EnumeratedSplit(inRules.LineDelimiters, StringSplitOptions.None)); }
static public void TestStringHash64() { StringHashing.ClearReverseLookup(); int collisionCount = 0; StringHashing.SetOnCollision((a, b, s, h) => collisionCount++); for (int i = -1000; i <= 1000; ++i) { new StringHash64(i.ToString()); } string allText = File.ReadAllText("Assets/Editor/words.txt"); foreach (var word in StringSlice.EnumeratedSplit(allText, new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)) { word.Hash64(); } if (collisionCount > 0) { Debug.LogErrorFormat("[StringTests] {0} collisions with hash size 16", collisionCount); } StringHashing.SetOnCollision(null); }
/// <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); } }
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> /// Attempts to evaluate if all conditions described by the given string are true. /// </summary> static public bool TryEvaluate(this IVariantResolver inResolver, object inContext, StringSlice inEvalData, IMethodCache inInvoker = null) { if (inEvalData.IsWhitespace) { return(true); } StringSlice.ISplitter splitter = QuoteAwareSplitter ?? (QuoteAwareSplitter = new StringUtils.ArgsList.Splitter(false)); VariantComparison comp; foreach (var group in inEvalData.EnumeratedSplit(splitter, StringSplitOptions.RemoveEmptyEntries)) { if (!VariantComparison.TryParse(group, out comp) || !comp.Evaluate(inResolver, inContext, inInvoker)) { return(false); } } return(true); }
/// <summary> /// Attempts to apply one or more modifications, described by the given string. /// </summary> static public bool TryModify(this IVariantResolver inResolver, object inContext, StringSlice inModifyData, IMethodCache inInvoker = null) { if (inModifyData.IsWhitespace) { return(true); } StringSlice.ISplitter splitter = QuoteAwareSplitter ?? (QuoteAwareSplitter = new StringUtils.ArgsList.Splitter(false)); bool bSuccess = true; VariantModification mod; foreach (var group in inModifyData.EnumeratedSplit(splitter, StringSplitOptions.RemoveEmptyEntries)) { if (!VariantModification.TryParse(group, out mod) || !mod.Execute(inResolver, inContext, inInvoker)) { bSuccess = false; } } return(bSuccess); }
static private Type LocateReflectedObjectType(UnityEngine.Object inRoot, string inPath) { if (string.IsNullOrEmpty(inPath)) { return(inRoot.GetType()); } inPath = inPath.Replace("Array.data", ""); object obj = inRoot; Type type = inRoot.GetType(); foreach (var pathSegment in StringSlice.EnumeratedSplit(inPath, PathSplitChars, StringSplitOptions.None)) { if (obj == null) { break; } StringSlice fieldName = pathSegment; Type objType = obj.GetType(); int arrayIdx = -1; // capture array if (pathSegment.EndsWith(']')) { int elementIndexStart = pathSegment.IndexOf('['); int length = pathSegment.Length - elementIndexStart - 1; StringSlice elementIndexSlice = pathSegment.Substring(elementIndexStart + 1, length); arrayIdx = Convert.ToInt32(elementIndexSlice.ToString()); fieldName = pathSegment.Substring(0, elementIndexStart); } FieldInfo field = objType.GetField(fieldName.ToString(), InstanceBindingFlags); if (field == null) { return(null); } obj = field.GetValue(obj); if (arrayIdx >= 0) { IEnumerable enumerable = obj as IEnumerable; if (enumerable == null) { return(null); } type = enumerable.GetType().GetEnumerableType(); IEnumerator enumerator = enumerable.GetEnumerator(); for (int i = 0; i <= arrayIdx; ++i) { if (!enumerator.MoveNext()) { return(null); } } obj = enumerator.Current; } else { type = field.FieldType; } } return(obj != null?obj.GetType() : type); }