コード例 #1
0
ファイル: PathInfo.cs プロジェクト: hutaoshu/Handlebars.Net
        internal PathInfo(
            PathType pathType,
            string path,
            bool isValidHelperLiteral,
            int contextChangeCount,
            IReadOnlyList <PathSegment> segments)
        {
            IsValidHelperLiteral = isValidHelperLiteral;
            HasValue             = pathType != PathType.Empty;
            _path = path;

            _hashCode = (_path.GetHashCode() * 397) ^ HasValue.GetHashCode();

            if (!HasValue)
            {
                return;
            }

            IsVariable    = pathType == PathType.Variable;
            IsInversion   = pathType == PathType.Inversion;
            IsBlockHelper = pathType == PathType.BlockHelper;
            IsBlockClose  = pathType == PathType.BlockClose;

            ContextChangeDepth = contextChangeCount;
            HasContextChange   = ContextChangeDepth > 0;

            var plainSegments = segments.Where(o => !o.IsContextChange && o.IsNotEmpty).ToArray();

            IsThis     = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == "." || plainSegments.Any(o => o.IsThis);
            IsPureThis = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == ".";

            var segment = plainSegments.SingleOrDefault(o => !o.IsThis);

            if (!segment.IsNotEmpty)
            {
                IsPureThis  = true;
                TrimmedPath = ".";
                PathChain   = ArrayEx.Empty <ChainSegment>();
                return;
            }

            PathChain = segment.PathChain;
            var lastIndex = segment.PathChain.Length - 1;

            using (var container = StringBuilderPool.Shared.Use())
            {
                for (int index = 0; index < PathChain.Length; index++)
                {
                    container.Value.Append(PathChain[index].TrimmedValue);
                    if (index != lastIndex)
                    {
                        container.Value.Append('.');
                    }
                }

                TrimmedPath = container.Value.ToString();
            }

            _trimmedHashCode = TrimmedPath.GetHashCode();
        }
コード例 #2
0
        private PathInfo(
            PathType pathType,
            string path,
            bool isValidHelperLiteral,
            PathSegment[] segments)
        {
            IsValidHelperLiteral = isValidHelperLiteral;
            HasValue             = pathType != PathType.Empty;
            Path = path;

            _hashCode = (Path.GetHashCode() * 397) ^ HasValue.GetHashCode();

            if (!HasValue)
            {
                return;
            }

            IsVariable    = pathType == PathType.Variable;
            IsInversion   = pathType == PathType.Inversion;
            IsBlockHelper = pathType == PathType.BlockHelper;
            IsBlockClose  = pathType == PathType.BlockClose;

            var plainSegments = segments.Where(o => !o.IsParent && o.IsNotEmpty).ToArray();

            IsThis     = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == "." || plainSegments.Any(o => o.IsThis);
            IsPureThis = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == ".";
            Segments   = segments;

            using var container = StringBuilderPool.Shared.Use();
            var builder = container.Value;

            var segmentsLastIndex = Segments.Length - 1;

            for (var segmentIndex = 0; segmentIndex <= segmentsLastIndex; segmentIndex++)
            {
                var segment            = Segments[segmentIndex];
                var pathChainLastIndex = segment.PathChain.Length - 1;
                var pathChain          = segment.PathChain;
                for (var pathChainIndex = 0; pathChainIndex <= pathChainLastIndex; pathChainIndex++)
                {
                    builder.Append(pathChain[pathChainIndex].TrimmedValue);
                    if (pathChainIndex != pathChainLastIndex)
                    {
                        builder.Append('.');
                    }
                }

                if (segmentIndex != segmentsLastIndex)
                {
                    builder.Append('/');
                }
            }

            TrimmedPath = builder.ToString();

            _trimmedHashCode          = TrimmedPath.GetHashCode();
            _trimmedInvariantHashCode = TrimmedPath.ToLowerInvariant().GetHashCode();
        }
コード例 #3
0
ファイル: PathInfo.cs プロジェクト: lanicon/Handlebars.Net
        internal PathInfo(
            bool hasValue,
            string path,
            bool isValidHelperLiteral,
            PathSegment[] segments
            )
        {
            IsValidHelperLiteral = isValidHelperLiteral;
            HasValue             = hasValue;
            _path = path;

            unchecked
            {
                _hashCode = (_path.GetHashCode() * 397) ^ HasValue.GetHashCode();
            }

            if (!HasValue)
            {
                return;
            }

            IsVariable    = path.StartsWith("@");
            IsInversion   = path.StartsWith("^");
            IsBlockHelper = path.StartsWith("#");
            IsBlockClose  = path.StartsWith("/");

            ContextChangeDepth = segments?.Count(o => o.IsContextChange) ?? 0;
            HasContextChange   = ContextChangeDepth > 0;
            var plainSegments = segments?.Where(o => !o.IsContextChange && !string.IsNullOrEmpty(o.ToString())).ToArray() ?? ArrayEx.Empty <PathSegment>();

            IsThis     = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == "." || plainSegments.Any(o => o.IsThis);
            IsPureThis = string.Equals(path, "this", StringComparison.OrdinalIgnoreCase) || path == ".";

            var segment = plainSegments.SingleOrDefault(o => !o.IsThis);

            if (segment == null)
            {
                IsPureThis  = true;
                TrimmedPath = ".";
                PathChain   = ArrayEx.Empty <ChainSegment>();
                return;
            }

            TrimmedPath = string.Join(".", segment.PathChain.Select(o => o.TrimmedValue));
            PathChain   = segment.PathChain;

            unchecked
            {
                _trimmedHashCode = TrimmedPath.GetHashCode();
            }
        }