Example #1
0
        public static unsafe string Repeat(this string s, int count)
        {
            if (s == null)
            {
                Throw.ArgumentNullException(Argument.s);
            }
            if (count < 0)
            {
                Throw.ArgumentOutOfRangeException(Argument.count);
            }

            if (s.Length == 0 || count == 1)
            {
                return(s);
            }
            if (count == 0)
            {
                return(String.Empty);
            }

            string result = new String('\0', count * s.Length);

            fixed(char *pResult = result)
            {
                var sb = new MutableStringBuilder(pResult, result.Length);

                for (int i = 0; i < count; i++)
                {
                    sb.Append(s);
                }
            }

            return(result);
        }
Example #2
0
        public static unsafe string ToHexValuesString(this byte[] bytes, string separator = null)
        {
            if (bytes == null)
            {
                Throw.ArgumentNullException(Argument.bytes);
            }
            bool useSeparator = !String.IsNullOrEmpty(separator);

            if (useSeparator)
            {
                // ReSharper disable once ForCanBeConvertedToForeach - it used to be an Any call but has been refactored due to performance
                for (int i = 0; i < separator.Length; i++)
                {
                    char c = separator[i];
                    if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')
                    {
                        Throw.ArgumentException(Argument.separator, Res.ByteArrayExtensionsSeparatorInvalidHex);
                    }
                }
            }

            int bytesLength = bytes.Length;

            if (bytesLength == 0)
            {
                return(String.Empty);
            }

            int    len    = (bytesLength << 1) + (useSeparator ? (bytesLength - 1) * separator.Length : 0);
            string result = new String('\0', len);

            fixed(char *pResult = result)
            {
                var sb = new MutableStringBuilder(pResult, len);

                // ReSharper disable once ForCanBeConvertedToForeach - performance
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (useSeparator && sb.Length != 0)
                    {
                        sb.Append(separator);
                    }
                    sb.AppendHex(bytes[i]);
                }
            }

            return(result);
        }
Example #3
0
        public static unsafe string ToDecimalValuesString(this byte[] bytes, string separator = ", ")
        {
            if (bytes == null)
            {
                Throw.ArgumentNullException(Argument.bytes);
            }
            if (separator == null)
            {
                Throw.ArgumentNullException(Argument.separator);
            }

            if (separator.Length == 0 || separator.Any(c => c >= '0' && c <= '9'))
            {
                Throw.ArgumentException(Argument.separator, Res.ByteArrayExtensionsSeparatorInvalidDec);
            }

            var buf = new ArraySection <char>(bytes.Length * (3 + separator.Length), false);

            try
            {
                fixed(char *pBuf = buf)
                {
                    var result = new MutableStringBuilder(pBuf, buf.Length);

                    // ReSharper disable once ForCanBeConvertedToForeach - intended, performance
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        if (result.Length != 0)
                        {
                            result.Append(separator);
                        }
                        result.Append(bytes[i]);
                    }

                    return(result.ToString());
                }
            }
            finally
            {
                buf.Release();
            }
        }
Example #4
0
        private static unsafe string Split(string text, int lineLength, int indentSize, char indentChar, bool indentSingleLine)
        {
            // single line
            if (lineLength <= 0 || text.Length <= lineLength)
            {
                if (!indentSingleLine || indentSize <= 0)
                {
                    return(text);
                }
                return(text.PadLeft(text.Length + indentSize, indentChar));
            }

            int lineCount = (int)Math.Ceiling((double)text.Length / lineLength);
            int len       = text.Length + lineCount * indentSize + (lineCount - 1) * Environment.NewLine.Length;
            var result    = new String('\0', len);

            fixed(char *pResult = result)
            {
                var sb = new MutableStringBuilder(pResult, len);

                int pos;

                for (pos = 0; pos < text.Length - lineLength; pos += lineLength)
                {
                    sb.Append(indentChar, indentSize);
                    sb.Append(text, pos, lineLength);
                    sb.AppendLine();
                }

                sb.Append(indentChar, indentSize);
                sb.Append(text, pos, text.Length - pos);
                Debug.Assert(sb.Length == sb.Capacity, "Wrong length initialization");
            }

            return(result);
        }
        public static unsafe string GetRelativePath(string target, string baseDirectory, bool isCaseSensitive)
        {
            if (target == null)
            {
                Throw.ArgumentNullException(Argument.target);
            }
            if (target.Length == 0)
            {
                Throw.ArgumentException(Argument.target, Res.ArgumentEmpty);
            }
            if (baseDirectory == null)
            {
                Throw.ArgumentNullException(Argument.baseDirectory);
            }
            if (baseDirectory.Length == 0)
            {
                Throw.ArgumentException(Argument.baseDirectory, Res.ArgumentEmpty);
            }

            target        = Path.GetFullPath(target);
            baseDirectory = Path.GetFullPath(baseDirectory);

            var srcDir = new StringSegmentInternal(baseDirectory);

            srcDir.TrimEnd(Path.DirectorySeparatorChar);
            List <StringSegmentInternal> basePathParts = srcDir.Split(Path.DirectorySeparatorChar);

            var dstDir = new StringSegmentInternal(target);

            dstDir.TrimEnd(Path.DirectorySeparatorChar);
            List <StringSegmentInternal> targetPathParts = dstDir.Split(Path.DirectorySeparatorChar);

            int commonPathDepth = 0;
            int len             = Math.Min(basePathParts.Count, targetPathParts.Count);

            if (isCaseSensitive)
            {
                for (int i = 0; i < len; i++)
                {
                    if (!basePathParts[i].Equals(targetPathParts[i]))
                    {
                        break;
                    }
                    commonPathDepth += 1;
                }
            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    if (!basePathParts[i].EqualsOrdinalIgnoreCase(targetPathParts[i]))
                    {
                        break;
                    }
                    commonPathDepth += 1;
                }
            }

            // no common parts
            if (commonPathDepth == 0)
            {
                return(target);
            }

            int baseOnlyCount   = basePathParts.Count - commonPathDepth;
            int targetPathCount = targetPathParts.Count;

            len = baseOnlyCount > 0 ? baseOnlyCount * 2 + (baseOnlyCount - 1) : 0; // .. and path separators
            for (int i = commonPathDepth; i < targetPathCount; i++)
            {
                if (len > 0)
                {
                    len += 1;
                }
                len += targetPathParts[i].Length;
            }

            string result = new String('\0', len);

            fixed(char *pResult = result)
            {
                var sb = new MutableStringBuilder(pResult, len);

                for (int i = 0; i < baseOnlyCount; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(Path.DirectorySeparatorChar);
                    }
                    sb.Append("..");
                }

                for (int i = commonPathDepth; i < targetPathCount; i++)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(Path.DirectorySeparatorChar);
                    }
                    sb.Append(targetPathParts[i]);
                }
            }

            if (result.Length == 0)
            {
                return(".");
            }
            return(result);
        }