Ejemplo n.º 1
0
        private string CountAndReplaceIndentSpaces(string line, out int count)
        {
            StringBuilder sb = new StringBuilder();

            count = 0;

            for (int i = 0; i < line.Length; i++)
            {
                char c = line[i];
                if (c == ' ')
                {
                    sb.Append(' ');
                    count++;
                }
                else if (c == '\t')
                {
                    int newCount = CharHelper.AddTab(count);
                    sb.Append(' ', newCount - count);
                    count = newCount;
                }
                else
                {
                    sb.Append(line, i, line.Length - i);
                    break;
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public SendSmsResult SendSms(SmsBase sms)
        {
            try
            {
                var paramsUrlStr = UrlHelper.BuildQuery(sms.Render.ReplaceVariables, "utf8");
                var getUrl       = sms.SendUrl + "?" + paramsUrlStr;
                var getString    = UrlHelper.HttpGet(getUrl);
                //var getString = "";

                var xmlContents = CharHelper.GetLookupTable(getString);

                foreach (KeyValuePair <string, string> kv in xmlContents)
                {
                    if (kv.Key.Equals("reason") && kv.Value.Equals("操作成功"))
                    {
                        return(SendSmsResult.Success);
                    }
                    else
                    {
                        sms.ErrorMsg = kv.Value;
                        return(SendSmsResult.Fail);
                    }
                }
                return(SendSmsResult.Fail);
            }
            catch
            {
                return(SendSmsResult.Fail);
            }
        }
Ejemplo n.º 3
0
        private void ReadDigits(ref TextPosition end, bool isPreviousDigit)
        {
            bool isDigit;

            while ((isDigit = CharHelper.IsDigit(_c)) || _c == '_')
            {
                if (isDigit)
                {
                    _textBuilder.AppendUtf32(_c);
                    isPreviousDigit = true;
                }
                else if (!isPreviousDigit)
                {
                    AddError("An underscore `_` must follow a digit and not another `_`", _position, _position);
                }
                else
                {
                    isPreviousDigit = false;
                }
                end = _position;
                NextChar();
            }

            if (!isPreviousDigit)
            {
                AddError("Missing a digit after a trailing underscore `_`", _position, _position);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Indicates whether the given letter is a valid non-first letter for a nickname.
 /// </summary>
 private static bool IsNicknameLetterValid(char letter)
 {
     return(CharHelper.IsBasicLetter(letter) ||
            char.IsDigit(letter) ||
            SpecialNicknameChars.Contains(letter) ||
            letter == SpecialNoBeginningNicknameChar);
 }
Ejemplo n.º 5
0
        private static bool StartsWithRtlCharacter(StringSlice slice)
        {
            for (int i = slice.Start; i <= slice.End; i++)
            {
                if (slice[i] < 128)
                {
                    continue;
                }

                int rune;
                if (CharHelper.IsHighSurrogate(slice[i]) && i < slice.End && CharHelper.IsLowSurrogate(slice[i + 1]))
                {
                    Debug.Assert(char.IsSurrogatePair(slice[i], slice[i + 1]));
                    rune = char.ConvertToUtf32(slice[i], slice[i + 1]);
                }
                else
                {
                    rune = slice[i];
                }

                if (CharHelper.IsRightToLeft(rune))
                {
                    return(true);
                }

                if (CharHelper.IsLeftToRight(rune))
                {
                    return(false);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
 public void IsBasicLetter_Others()
 {
     foreach (char c in "_-{}[]^~!?|/")
     {
         Assert.IsFalse(CharHelper.IsBasicLetter(c));
     }
 }
Ejemplo n.º 7
0
 public void IsBasicLetter_Accents()
 {
     foreach (char c in "àäâáîôöüù")
     {
         Assert.IsFalse(CharHelper.IsBasicLetter(c));
     }
 }
Ejemplo n.º 8
0
 public void IsBasicLetter_Digits()
 {
     for (char c = '0'; c <= '9'; c++)
     {
         Assert.IsFalse(CharHelper.IsBasicLetter(c));
     }
 }
Ejemplo n.º 9
0
    protected override bool TryValidateAccessKeyInternal(byte[] accessKey, out ValidationStatus status, out string?message)
    {
        //https://cloud.google.com/storage/docs/authentication/hmackeys

        //Spec: A 40-character Base-64 encoded string that is linked to a specific access ID.
        if (accessKey.Length != 40)
        {
            status  = ValidationStatus.WrongLength;
            message = "40";
            return(false);
        }

        //Check that it is actually base64
        foreach (byte b in accessKey)
        {
            char c = (char)b;

            if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, 'A', 'Z') || CharHelper.InRange(c, '0', '9') || CharHelper.OneOf(c, '/', '+', '='))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 10
0
        /// <summary>
        /// Parses the indentation from the current position in the line, updating <see cref="StartBeforeIndent"/>,
        /// <see cref="ColumnBeforeIndent"/>, <see cref="Start"/> and <see cref="Column"/> accordingly
        /// taking into account space taken by tabs.
        /// </summary>
        public void ParseIndent()
        {
            var c = CurrentChar;
            var previousStartBeforeIndent  = StartBeforeIndent;
            var startBeforeIndent          = Start;
            var previousColumnBeforeIndent = ColumnBeforeIndent;
            var columnBeforeIndent         = Column;

            while (c != '\0')
            {
                if (c == '\t')
                {
                    Column = CharHelper.AddTab(Column);
                }
                else if (c == ' ')
                {
                    Column++;
                }
                else
                {
                    break;
                }
                c = Line.NextChar();
            }
            if (columnBeforeIndent == Column)
            {
                StartBeforeIndent  = previousStartBeforeIndent;
                ColumnBeforeIndent = previousColumnBeforeIndent;
            }
            else
            {
                StartBeforeIndent  = startBeforeIndent;
                ColumnBeforeIndent = columnBeforeIndent;
            }
        }
Ejemplo n.º 11
0
    protected override bool TryValidateKeyIdInternal(string keyId, out ValidationStatus status, out string?message)
    {
        //https://cloud.google.com/storage/docs/authentication/hmackeys

        //Google service keys are 61. User keys are 24
        if (keyId.Length != 61 && keyId.Length != 24)
        {
            status  = ValidationStatus.WrongLength;
            message = "24 / 61";
            return(false);
        }

        foreach (char c in keyId)
        {
            if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, 'A', 'Z') || CharHelper.InRange(c, '0', '9'))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 12
0
        public void TestCollapseSpace()
        {
            var result = CharHelper.CollapseWhitespaces("");

            Assert.AreEqual("", result);

            result = CharHelper.CollapseWhitespaces("test1");
            Assert.AreEqual("test1", result);

            result = CharHelper.CollapseWhitespaces("test1 ");
            Assert.AreEqual("test1 ", result);

            result = CharHelper.CollapseWhitespaces(" test1");
            Assert.AreEqual(" test1", result);

            result = CharHelper.CollapseWhitespaces(" test1  test2  ");
            Assert.AreEqual(" test1 test2 ", result);

            result = CharHelper.CollapseWhitespaces(" \n \f \r \t ");
            Assert.AreEqual(" ", result);

            result = CharHelper.CollapseWhitespaces("\n \f \r \t ");
            Assert.AreEqual(" ", result);

            result = CharHelper.CollapseWhitespaces("\ntest1\n test2\n ");
            Assert.AreEqual(" test1 test2 ", result);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Appends the specified line to this instance.
        /// </summary>
        /// <param name="slice">The slice.</param>
        /// <param name="column">The column.</param>
        /// <param name="line">The line.</param>
        /// <param name="sourceLinePosition"></param>
        public void AppendLine(ref StringSlice slice, int column, int line, int sourceLinePosition)
        {
            if (Lines.Lines == null)
            {
                Lines = new StringLineGroup(4);
            }

            var stringLine = new StringLine(ref slice, line, column, sourceLinePosition);

            // Regular case, we are not in the middle of a tab
            if (slice.CurrentChar != '\t' || !CharHelper.IsAcrossTab(column))
            {
                Lines.Add(ref stringLine);
            }
            else
            {
                // We need to expand tabs to spaces
                var builder = StringBuilderCache.Local();
                for (int i = column; i < CharHelper.AddTab(column); i++)
                {
                    builder.Append(' ');
                }
                builder.Append(slice.Text, slice.Start + 1, slice.Length - 1);
                stringLine.Slice = new StringSlice(builder.ToString());
                Lines.Add(ref stringLine);
            }
        }
Ejemplo n.º 14
0
    protected override bool TryValidateKeyIdInternal(string keyId, out ValidationStatus status, out string?message)
    {
        //B2 master keys are 12. Application keys are 25
        if (keyId.Length != 12 && keyId.Length != 25)
        {
            status  = ValidationStatus.WrongLength;
            message = "12 / 25";
            return(false);
        }

        foreach (char c in keyId)
        {
            if (CharHelper.InRange(c, 'a', 'f') || CharHelper.InRange(c, '0', '9'))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 15
0
    protected override bool TryValidateObjectKeyInternal(string objectKey, ObjectKeyValidationMode mode, out ValidationStatus status, out string?message)
    {
        //https://www.backblaze.com/b2/docs/files.html

        //Spec: Names can be pretty much any UTF-8 string up to 1024 bytes long
        if (objectKey.Length < 1 || Encoding.UTF8.GetByteCount(objectKey) > 1024)
        {
            status  = ValidationStatus.WrongLength;
            message = "1-1024";
            return(false);
        }

        foreach (char c in objectKey)
        {
            //Spec: No character codes below 32 are allowed. DEL characters (127) are not allowed
            if (CharHelper.InRange(c, (char)0, (char)31) || c == (char)127)
            {
                status  = ValidationStatus.WrongFormat;
                message = c.ToString();
                return(false);
            }
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 16
0
    protected override bool TryValidateKeyIdInternal(string keyId, out ValidationStatus status, out string?message)
    {
        if (keyId.Length != 20)
        {
            status  = ValidationStatus.WrongLength;
            message = null;
            return(false);
        }

        foreach (char c in keyId)
        {
            if (CharHelper.InRange(c, 'A', 'Z') || CharHelper.InRange(c, '0', '9'))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 17
0
    protected bool TryValidateBucketDns(string input, out ValidationStatus status, out string?message)
    {
        int curPos = 0;
        int end    = input.Length;

        do
        {
            //find the dot or hit the end
            int newPos = curPos;
            while (newPos < end)
            {
                if (input[newPos] == '.')
                {
                    break;
                }

                ++newPos;
            }

            if (curPos == newPos || newPos - curPos > 63)
            {
                message = "1-63";
                status  = ValidationStatus.WrongLength;
                return(false);
            }

            char start = input[curPos];

            if (!CharHelper.InRange(start, 'a', 'z') && !CharHelper.InRange(start, '0', '9'))
            {
                message = start.ToString();
                status  = ValidationStatus.WrongFormat;
                return(false);
            }

            curPos++;

            //check the label content
            while (curPos < newPos)
            {
                char c = input[curPos++];

                if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, '0', '9') || c == '-')
                {
                    continue;
                }

                message = c.ToString();
                status  = ValidationStatus.WrongFormat;
                return(false);
            }

            ++curPos;
        } while (curPos < end);

        message = null;
        status  = ValidationStatus.Ok;
        return(true);
    }
Ejemplo n.º 18
0
 private void CheckCharacter(char32 c)
 {
     // The character 0xFFFD is the replacement character and we assume that something went wrong when reading the input
     if (!CharHelper.IsValidUnicodeScalarValue(c) || c == 0xFFFD)
     {
         AddError($"The character `{c}` is an invalid UTF8 character", _current.Position, _current.Position);
     }
 }
Ejemplo n.º 19
0
 public EBTStatus IsControlByHostPlayer()
 {
     if (CharHelper.IsHostCtrlActor(ref m_wrapper.actorPtr))
     {
         return(EBTStatus.BT_SUCCESS);
     }
     return(EBTStatus.BT_FAILURE);
 }
Ejemplo n.º 20
0
    protected override bool TryValidateBucketNameInternal(string bucketName, BucketNameValidationMode mode, out ValidationStatus status, out string?message)
    {
        //Source: https://wasabi.com/wp-content/themes/wasabi/docs/User_Guide/topics/Creating_a_Bucket.htm

        //Spec: A bucket name can consist of 3 to 63 characters
        if (bucketName.Length < 3 || bucketName.Length > 63)
        {
            status  = ValidationStatus.WrongLength;
            message = "3-63";
            return(false);
        }

        //Spec: lowercase letters, numbers, periods, and dashes.
        foreach (char c in bucketName)
        {
            if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, '0', '9') || CharHelper.OneOf(c, '.', '-'))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        //Spec: The name must begin with a lower­case letter or number.
        char start = bucketName[0];

        if (!CharHelper.InRange(start, 'a', 'z') && !CharHelper.InRange(start, '0', '9'))
        {
            status  = ValidationStatus.WrongFormat;
            message = start.ToString();
            return(false);
        }

        //Spec: The name must begin with a lower­case letter or number.
        char end = bucketName[bucketName.Length - 1];

        if (!CharHelper.InRange(end, 'a', 'z') && !CharHelper.InRange(end, '0', '9'))
        {
            status  = ValidationStatus.WrongFormat;
            message = end.ToString();
            return(false);
        }

        //Spec: name cannot be formatted as an IP address (123.45.678.90)
        if (_ipRegex.IsMatch(bucketName))
        {
            status  = ValidationStatus.WrongFormat;
            message = bucketName;
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 21
0
        /// <summary>
        /// Unwind any previous indent from the current character back to the first space.
        /// </summary>
        public void UnwindAllIndents()
        {
            // Find the previous first space on the current line
            var previousStart = Line.Start;

            for (; Line.Start > originalLineStart; Line.Start--)
            {
                var c = Line.PeekCharAbsolute(Line.Start - 1);
                if (c == 0)
                {
                    break;
                }
                if (!c.IsSpaceOrTab())
                {
                    break;
                }
            }
            var targetStart = Line.Start;

            // Nothing changed? Early exit
            if (previousStart == targetStart)
            {
                return;
            }

            // TODO: factorize the following code with what is done with GoToColumn

            // If we have found the first space, we need to recalculate the correct column
            Line.Start         = originalLineStart;
            Column             = 0;
            ColumnBeforeIndent = 0;
            StartBeforeIndent  = originalLineStart;

            for (; Line.Start < targetStart; Line.Start++)
            {
                var c = Line.Text[Line.Start];
                if (c == '\t')
                {
                    Column = CharHelper.AddTab(Column);
                }
                else
                {
                    if (!c.IsSpaceOrTab())
                    {
                        ColumnBeforeIndent = Column + 1;
                        StartBeforeIndent  = Line.Start + 1;
                    }

                    Column++;
                }
            }

            // Reset the indent
            ColumnBeforeIndent = Column;
            StartBeforeIndent  = Start;
        }
Ejemplo n.º 22
0
    protected override bool TryValidateBucketNameInternal(string bucketName, BucketNameValidationMode mode, out ValidationStatus status, out string?message)
    {
        //https://cloud.google.com/storage/docs/naming-buckets

        //Spec: Bucket names must contain 3-63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters.
        if (bucketName.Length < 3 || bucketName.Length > 63)
        {
            status  = ValidationStatus.WrongLength;
            message = "3-63";
            return(false);
        }

        //Spec: Bucket names must start and end with a number or letter.
        char start = bucketName[0];
        char end   = bucketName[bucketName.Length - 1];

        if (!CharHelper.InRange(start, 'a', 'z') && !CharHelper.InRange(start, '0', '9'))
        {
            status  = ValidationStatus.WrongFormat;
            message = start.ToString();
            return(false);
        }

        if (!CharHelper.InRange(end, 'a', 'z') && !CharHelper.InRange(end, '0', '9'))
        {
            status  = ValidationStatus.WrongFormat;
            message = end.ToString();
            return(false);
        }

        //Spec: Bucket names must contain only lowercase letters, numbers, dashes (-), underscores (_), and dots (.). Spaces are not allowed. Names containing dots require verification.
        foreach (char c in bucketName)
        {
            if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, '0', '9') || CharHelper.OneOf(c, '-', '_', '.'))
            {
                continue;
            }

            status  = ValidationStatus.WrongFormat;
            message = c.ToString();
            return(false);
        }

        //Spec: Bucket names cannot begin with the "goog" prefix.
        //Spec: Bucket names cannot contain "google" or close misspellings, such as "g00gle".
        if (bucketName.StartsWith("goog", StringComparison.Ordinal) || bucketName.Contains("google") || bucketName.Contains("g00gle"))
        {
            status  = ValidationStatus.ReservedName;
            message = bucketName;
            return(false);
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 23
0
        private IEnumerable <char> ToRobberish(char c)
        {
            yield return(c);

            if (CharHelper.IsConsonant(c))
            {
                yield return('o');

                yield return(c.ToString().ToLower().First());
            }
        }
Ejemplo n.º 24
0
        private IEnumerable <char> SkipEncodedChars(string encoded)
        {
            for (var i = 0; i < encoded.Length; i++)
            {
                yield return(encoded[i]);

                if (CharHelper.IsConsonant(encoded[i]) && i < encoded.Length - 1 && encoded[i + 1] == 'o')
                {
                    i += 2;
                }
            }
        }
Ejemplo n.º 25
0
        public void IsBasicLetter_BasicLetters()
        {
            for (char c = 'a'; c <= 'z'; c++)
            {
                Assert.IsTrue(CharHelper.IsBasicLetter(c));
            }

            for (char c = 'A'; c <= 'Z'; c++)
            {
                Assert.IsTrue(CharHelper.IsBasicLetter(c));
            }
        }
Ejemplo n.º 26
0
 public static bool IsNameStartChar(char c, char d)
 {
     if (CharHelper.IsHighSurrogate(c) && CharHelper.IsLowSurrogate(d))
     {
         int codepoint = CharHelper.ConvertToUtf32(c, d);
         return(codepoint >= 0x10000 && codepoint <= 0xeffff);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 27
0
        public void FullShapeToAsciiDigitTest()
        {
            char ch       = '0';
            char expected = '0';
            char actual   = CharHelper.FullShapeToAsciiDigit(ch);

            Assert.AreEqual(expected, actual, "Huanlin.Text.CharHelper.FullShapeToAsciiDigit 測試失敗: " + ch.ToString());

            ch       = '9';
            expected = '9';
            actual   = CharHelper.FullShapeToAsciiDigit(ch);
            Assert.AreEqual(expected, actual, "Huanlin.Text.CharHelper.FullShapeToAsciiDigit 測試失敗: " + ch.ToString());
        }
Ejemplo n.º 28
0
        private void ReadKey()
        {
            var start = _position;
            var end   = _position;

            while (CharHelper.IsKeyContinue(_c))
            {
                end = _position;
                NextChar();
            }

            _token = new SyntaxTokenValue(TokenKind.BasicKey, start, end);
        }
Ejemplo n.º 29
0
    protected override bool TryValidateObjectKeyInternal(string objectKey, ObjectKeyValidationMode mode, out ValidationStatus status, out string?message)
    {
        //Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html

        //Spec: The name for a key is a sequence of Unicode characters whose UTF-8 encoding is at most 1,024 bytes long.
        if (objectKey.Length < 1 || Constants.Utf8NoBom.GetByteCount(objectKey) > 1024)
        {
            status  = ValidationStatus.WrongLength;
            message = "1-1024";
            return(false);
        }

        //Spec: You can use any UTF-8 character in an object key name. However, using certain characters in key names can cause problems with some applications and protocols.
        foreach (char c in objectKey)
        {
            //Spec: Safe characters
            if (CharHelper.InRange(c, 'a', 'z') || CharHelper.InRange(c, 'A', 'Z') || CharHelper.InRange(c, '0', '9'))
            {
                continue;
            }

            //Spec: Safe characters
            if (CharHelper.OneOf(c, '/', '!', '-', '_', '.', '*', '\'', '(', ')'))
            {
                continue;
            }

            if (mode == ObjectKeyValidationMode.DefaultStrict)
            {
                //Spec: Characters that might require special handling
                if (CharHelper.OneOf(c, '&', '$', '@', '=', ';', ':', '+', ' ', ',', '?') || CharHelper.InRange(c, (char)0, (char)31) || c == (char)127)
                {
                    status  = ValidationStatus.WrongFormat;
                    message = c.ToString();
                    return(false);
                }

                //Spec: Characters to avoid
                if (CharHelper.OneOf(c, '\\', '{', '}', '^', '%', '`', '[', ']', '"', '<', '>', '~', '#', '|'))
                {
                    status  = ValidationStatus.WrongFormat;
                    message = c.ToString();
                    return(false);
                }
            }
        }

        status  = ValidationStatus.Ok;
        message = null;
        return(true);
    }
Ejemplo n.º 30
0
        /// <summary>
        /// Returns the next character in the line being processed. Update <see cref="Start"/> and <see cref="Column"/>.
        /// </summary>
        /// <returns>The next character or `\0` if end of line is reached</returns>
        public char NextChar()
        {
            var c = Line.CurrentChar;

            if (c == '\t')
            {
                Column = CharHelper.AddTab(Column);
            }
            else
            {
                Column++;
            }
            return(Line.NextChar());
        }