Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void StartsWithPartialString()
        {
            var raw = "hello world";
            var str = new StringSlice(raw, 6, raw.Length);

            Assert.That(str.StartsWith("world"), Is.True);
            Assert.That(str.StartsWith("hello"), Is.False);
        }
Ejemplo n.º 3
0
        public void StartsWithFullString()
        {
            var raw = "hello world";
            var str = new StringSlice(raw);

            Assert.That(str.StartsWith("hello"), Is.True);
            Assert.That(str.StartsWith("world"), Is.False);
        }
Ejemplo n.º 4
0
        public void StartsWithBeyondEnd()
        {
            var raw = "hello world";
            var str = new StringSlice(raw, 6, 9);

            Assert.That(str.StartsWith("world"), Is.False);
        }
Ejemplo n.º 5
0
        /// <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);
            }
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        /// <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;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Attempts to parse a string slice into a variant.
        /// </summary>
        static public bool TryParse(StringSlice inSlice, bool inbAllowImplicitHash, out Variant outValue)
        {
            inSlice = inSlice.Trim();

            if (inSlice.Length <= 0 || inSlice.Equals("null", true))
            {
                outValue = Variant.Null;
                return(true);
            }

            if (inSlice.StartsWith(StringHashing.CustomHashPrefix) || inSlice.StartsWith(StringHashing.StringPrefix) ||
                (inSlice.Length >= 2 && inSlice.StartsWith('"') && inSlice.EndsWith('"')))
            {
                StringHash32 hash;
                if (StringHash32.TryParse(inSlice, out hash))
                {
                    outValue = hash;
                    return(true);
                }
            }

            bool bBoolVal;

            if (StringParser.TryParseBool(inSlice, out bBoolVal))
            {
                outValue = new Variant(bBoolVal);
                return(true);
            }

            int intVal;

            if (StringParser.TryParseInt(inSlice, out intVal))
            {
                outValue = new Variant(intVal);
                return(true);
            }

            uint uintVal;

            if (StringParser.TryParseUInt(inSlice, out uintVal))
            {
                outValue = new Variant(uintVal);
                return(true);
            }

            float floatVal;

            if (StringParser.TryParseFloat(inSlice, out floatVal))
            {
                outValue = new Variant(floatVal);
                return(true);
            }

            if (inbAllowImplicitHash)
            {
                StringHash32 hash;
                if (StringHash32.TryParse(inSlice, out hash))
                {
                    outValue = hash;
                    return(true);
                }
            }

            outValue = default(Variant);
            return(false);
        }