예제 #1
0
    public static bool ContainsForbiddenNameCharacters(this string s)
    {
        int[] chars = StringInfo.ParseCombiningCharacters(s);
        int   n     = chars.Length;

        for (int i = 0; i < n; i++)
        {
            UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(s, chars[i]);
            if (uc == UnicodeCategory.LowercaseLetter ||
                uc == UnicodeCategory.UppercaseLetter ||
                uc == UnicodeCategory.TitlecaseLetter ||
                uc == UnicodeCategory.SpaceSeparator ||
                uc == UnicodeCategory.DecimalDigitNumber
                )
            {
                // character is ok, do nothing
            }
            else
            {
                // character is not ok
                return(true);
            }
        }

        return(false);
    }
예제 #2
0
        private static IEnumerable <TextElementSegment> TextElementSegmentsCore(string input)
        {
            int[] elementOffsets = StringInfo.ParseCombiningCharacters(input);

            int lastOffset = -1;

            foreach (int offset in elementOffsets)
            {
                if (lastOffset != -1)
                {
                    int elementLength          = offset - lastOffset;
                    TextElementSegment segment = CreateSegment(lastOffset, elementLength);
                    yield return(segment);
                }

                lastOffset = offset;
            }

            if (lastOffset != -1)
            {
                int lastSegmentLength = input.Length - lastOffset;

                TextElementSegment segment = CreateSegment(lastOffset, lastSegmentLength);
                yield return(segment);
            }
        }
예제 #3
0
    public bool NegTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest1: The argument string is a null reference");

        try
        {
            string str    = null;
            int[]  result = StringInfo.ParseCombiningCharacters(str);
            TestLibrary.TestFramework.LogError("101", "The ArgumentNullException was not thrown as expected");
            retVal = false;
        }
        catch (ArgumentNullException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
        /// <summary>
        /// Get the encoded binary value of the given character as a
        /// <see cref="T:System.Int32" /> value.
        /// </summary>
        /// <param name="s">The string element to encode.</param>
        /// <returns>An unsigned 32-bit integer representing the
        /// encoding of the character.</returns>
        /// <exception cref="T:System.ArgumentException">System.ArgumentException</exception>
        /// <exception cref="T:System.ArgumentNullException"></exception>
        public int GetEncodedValue(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                throw new ArgumentNullException();
            }

            if (_currentMap.ContainsKey(s))
            {
                return(_currentMap[s]);
            }

            var stringInfo = StringInfo.ParseCombiningCharacters(s);

            if (stringInfo.Length > 1)
            {
                throw new ArgumentException(s);
            }

            var charbytes = GetCharBytes(s);

            if (charbytes.Length < 4)
            {
                Array.Resize(ref charbytes, 4);
            }
            return(BitConverter.ToInt32(charbytes, 0));
        }
예제 #5
0
        public TGlyph FindGlyphForCharacterAtIndex(TFont font, int index, string str)
        {
            var unicodeIndices = StringInfo.ParseCombiningCharacters(str);
            int start          = 0;
            int end            = str.Length;

            foreach (var unicodeIndex in unicodeIndices)
            {
                if (unicodeIndex <= index)
                {
                    start = unicodeIndex;
                }
                else
                {
                    end = unicodeIndex;
                    break;
                }
            }
            int length = end - start;

            TGlyph[] glyphs = new TGlyph[length];
            char[]   chars  = str.Substring(start, length).ToCharArray();
            font.CtFont.GetGlyphsForCharacters(chars, glyphs, length);
            return(glyphs[0]);
        }
예제 #6
0
        /// <summary>
        /// UTF-16からShiftJISに文字列を変換し、文字位置の変換テーブルを生成する。
        /// 変換後のShiftJIS文字列と変換テーブルにはヌル終端の分の要素も含まれる。
        /// </summary>
        /// <param name="unicode_string">UTF-16文字列</param>
        /// <param name="shiftjis_string">ShiftJIS文字列</param>
        /// <param name="shiftjis_positions">ShiftJISのバイト位置と文字位置の変換テーブル</param>
        private static void UnicodeToShiftJis(string unicode_string, out byte[] shiftjis_string, out int[] shiftjis_positions)
        {
            // 文字位置とUTF-16上でのワード位置の変換テーブルを取得し、
            // ShiftJIS上でのバイト位置とUTF-16上でのワード位置の変換テーブルを計算する
            Encoding encoding = Encoding.GetEncoding(932);

            byte[] shiftjis_string_internal = encoding.GetBytes(unicode_string);
            int    shiftjis_length          = shiftjis_string_internal.Length;

            shiftjis_positions = new int[shiftjis_length + 1];
            char[] unicode_char_array = unicode_string.ToArray();
            int[]  unicode_indexes    = StringInfo.ParseCombiningCharacters(unicode_string);
            int    char_count         = unicode_indexes.Length;
            int    shiftjis_index     = 0;

            for (int char_index = 0; char_index < char_count; char_index++)
            {
                int unicode_index  = unicode_indexes[char_index];
                int unicode_count  = (((char_index + 1) < char_count) ? unicode_indexes[char_index + 1] : unicode_string.Length) - unicode_index;
                int shiftjis_count = encoding.GetByteCount(unicode_char_array, unicode_index, unicode_count);
                for (int offset = 0; offset < shiftjis_count; offset++)
                {
                    shiftjis_positions[shiftjis_index + offset] = char_index;
                }
                shiftjis_index += shiftjis_count;
            }
            shiftjis_positions[shiftjis_length] = char_count;

            // ヌル終端を付け加える
            shiftjis_string = new byte[shiftjis_length + 1];
            Buffer.BlockCopy(shiftjis_string_internal, 0, shiftjis_string, 0, shiftjis_length);
            shiftjis_string[shiftjis_length] = 0;
        }
        public static NSMutableAttributedString ToNsAttributedString(this AttributedGlyphRun <TFont, TGlyph> glyphRun)
        {
            var font           = glyphRun.Font;
            var text           = glyphRun.Text;
            var unicodeIndexes = StringInfo.ParseCombiningCharacters(text);
            var attributes     = new CTStringAttributes
            {
                ForegroundColorFromContext = true,
                Font = font.CtFont
            };
            var attributedString = new NSMutableAttributedString(text, attributes);
            var kernedGlyphs     = glyphRun.KernedGlyphs;

            for (int i = 0; i < kernedGlyphs.Length; i++)
            {
                var kern = kernedGlyphs[i].KernAfterGlyph;
                if (kern != 0)
                {
                    var endIndex = (i < unicodeIndexes.Length - 1) ? unicodeIndexes[i + 1] : text.Length;
                    var range    = new NSRange(unicodeIndexes[i], endIndex - unicodeIndexes[i]);
                    attributedString.AddAttribute(CTStringAttributeKey.KerningAdjustment, new NSNumber(kern), range);
                }
            }
            return(attributedString);
        }
예제 #8
0
        private void ValidateString(SchemaReport report)
        {
            if (report == null || report.Schema == null)
                return;

            JsonSchema schema = report.Schema;

            if (!TestType(report, JsonSchemaType.String))
                return;

            string value = _reader.Value.ToString();

            if (schema.MaximumLength != null || schema.MinimumLength != null)
            {
                int[] unicodeString = StringInfo.ParseCombiningCharacters(value);

                if (schema.MaximumLength != null && unicodeString.Length > schema.MaximumLength)
                    RaiseError("String '{0}' exceeds maximum length of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.MaximumLength), report);

                if (schema.MinimumLength != null && unicodeString.Length < schema.MinimumLength)
                    RaiseError("String '{0}' is less than minimum length of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.MinimumLength), report);
            }

            if (schema.Pattern != null)
            {
                if (!Regex.IsMatch(value, schema.Pattern))
                    RaiseError("String '{0}' does not match regex pattern '{1}'.".FormatWith(CultureInfo.InvariantCulture, value, schema.Pattern), report);
            }
        }
예제 #9
0
    public static string Reverse(this string s)
    {
        var info      = new StringInfo(s);
        var charArray = new char[s.Length];
        var teIndices = StringInfo.ParseCombiningCharacters(s).Reverse();

        int j = 0;

        foreach (var i in  teIndices)
        {
            if (char.IsHighSurrogate(s[i]))
            {
                charArray[j] = s[i];
                j++;
                charArray[j] = s[i + 1];
            }
            else
            {
                charArray[j] = s[i];
            }
            j++;
        }

        return(new string(charArray));
    }
예제 #10
0
        static int GetLengthInTextElements(string textData)
        {
            int[] indexes =
                StringInfo.ParseCombiningCharacters(textData);

            return(indexes.Length);
        }
예제 #11
0
        public static string get_flag(string str)
        {
            int[] array = StringInfo.ParseCombiningCharacters(str);

            int num = array.Length;

            string[] array2 = new string[num];
            int      num2   = 0;
            int      num3   = num - 1;

            if (num3 >= num2)
            {
                do
                {
                    if (num2 == array.Length - 1)
                    {
                        array2[num2] = str.Substring(num2);
                    }
                    else
                    {
                        int num4 = array[num2];
                        array2[num2] = str.Substring(num4, array[num2 + 1] - num4);
                    }
                    num2++;
                }while (num2 != num3 + 1);
            }
            string[] array3 = array2;
            Array.Reverse(array3);
            return(string.Join("", array3));
        }
예제 #12
0
    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2: The argument string is an empty string");

        try
        {
            int[] result = StringInfo.ParseCombiningCharacters(string.Empty);
            if (result == null)
            {
                TestLibrary.TestFramework.LogError("003", "The result is not the value as expected");
                retVal = false;
            }
            if (result.Length != 0)
            {
                TestLibrary.TestFramework.LogError("004", "The result is not the value as expected");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("005", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
        public static NSMutableAttributedString ToNsAttributedString(this AttributedGlyphRun <TFont, TGlyph> glyphRun)
        {
            var font           = glyphRun.Font;
            var text           = glyphRun.Text.ToString();
            var unicodeIndexes = StringInfo.ParseCombiningCharacters(text);
            var attributes     = new CTStringAttributes
            {
                ForegroundColorFromContext = true,
                Font = font.CtFont
            };
            var attributedString = new NSMutableAttributedString(text, attributes);
            var kernedGlyphs     = glyphRun.GlyphInfos;

            for (int i = 0; i < kernedGlyphs.Count; i++)
            {
                var endIndex = (i < unicodeIndexes.Length - 1) ? unicodeIndexes[i + 1] : text.Length;
                var range    = new NSRange(unicodeIndexes[i], endIndex - unicodeIndexes[i]);
                if (kernedGlyphs[i].KernAfterGlyph is var kern && !(kern is 0))
                {
                    attributedString.AddAttribute(CTStringAttributeKey.KerningAdjustment, new NSNumber(kern), range);
                }
                if (kernedGlyphs[i].Foreground is Structures.Color foreground)
                {
                    attributedString.AddAttribute(CTStringAttributeKey.ForegroundColor, ObjCRuntime.Runtime.GetNSObject(foreground.ToCgColor().Handle), range);
                }
            }
            return(attributedString);
        }
예제 #14
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            ShowPhoneticFileNames();

            string str       = "ㄅ"; //𩗴";
            int    codePoint = Char.ConvertToUtf32(str, 0);

            int[] info = StringInfo.ParseCombiningCharacters(str);
            if (codePoint >= 0x3105 && codePoint <= 0x3129) // ㄅㄆㄇㄈ
            {
                Text = "Y";
            }
            if (codePoint >= 0x4e00 && codePoint <= 0x9fcb)
            {
                Text = "Y";
            }
            else if (codePoint >= 0x3400 && codePoint <= 0x4db5)
            {
                Text = "Y";
            }
            else if (codePoint >= 0x20000 && codePoint <= 0x2a2d6)
            {
                Text = "Y";
            }
            else if (codePoint >= 0x2a700 && codePoint <= 0x2b734)
            {
                Text = "Y";
            }
        }
        public void Print()
        {
            int spaces;

            if (ConsoleHelper.SupportsCombining)
            {
                int[] textElements = StringInfo.ParseCombiningCharacters(Regex.CodePointsToString());
                spaces = 0;
                while (spaces + 1 < textElements.Length && Index > textElements[spaces] && Index > textElements[spaces + 1])
                {
                    spaces++;
                }
                if (spaces + 1 < textElements.Length && textElements[spaces + 1] == Index)
                {
                    spaces++;
                }
            }
            else
            {
                spaces = Index;
            }
            Console.Error.WriteLine($@"{ExpectedMessage}
{Regex.CodePointsToString()}
{new string(' ', spaces)}^");
        }
예제 #16
0
파일: Lexer.cs 프로젝트: mwh/kernan
        private Token lexCloseBracket()
        {
            int start = index;

            advanceIndex();
            UnicodeCategory cat  = validateChar();
            char            c    = code[index];
            string          cStr = StringInfo.GetNextTextElement(code, index);

            while (UnicodeLookup.CloseBrackets.Contains(cStr) ||
                   isOperatorCharacter(c, cat))
            {
                advanceIndex();
                cat  = validateChar();
                c    = code[index];
                cStr = StringInfo.GetNextTextElement(code, index);
                if (code.Substring(start, index - start) == "]]")
                {
                    return(new RGenericToken(moduleName, line, column));
                }
            }
            string bracket = code.Substring(start, index - start);

            int[]  indices   = StringInfo.ParseCombiningCharacters(bracket);
            int    lastIndex = indices[indices.Length - 1];
            string l         = StringInfo.GetNextTextElement(bracket, lastIndex);
            // For ease, any ) characters at the end of a closing bracket
            // are removed from the token.
            int sub            = 0;
            int blen           = bracket.Length;
            int graphemeOffset = indices.Length - 1;

            while (l == ")" || l == ".")
            {
                sub++;
                graphemeOffset--;
                lastIndex = indices[graphemeOffset];
                l         = StringInfo.GetNextTextElement(bracket, lastIndex);
            }
            if (sub > 0)
            {
                // Reset index to before the )s so that they will be
                // lexed as the following tokens.
                index  -= sub;
                bracket = bracket.Substring(0, bracket.Length - sub);
            }
            if (!UnicodeLookup.CloseBrackets.Contains(l))
            {
                reportError("L0009",
                            new Dictionary <string, string>
                {
                    { "char", l }
                },
                            "Invalid character at end of bracket sequence");
            }
            return(new CloseBracketToken(moduleName, line, column, bracket));
        }
        public void TestNullReference()
        {
            string str = null;

            Assert.Throws <ArgumentNullException>(() =>
            {
                int[] result = StringInfo.ParseCombiningCharacters(str);
            });
        }
예제 #18
0
        /// <summary>
        /// Adds a character mapping to translate from source to object
        /// </summary>
        /// <param name="mapping">The text element, or range of text elements to map.</param>
        /// <param name="code">The code of the mapping.</param>
        /// <exception cref="System.ArgumentException">System.ArgumentException</exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public void Map(string mapping, int code)
        {
            // the default encoding cannot be changed
            if (_currentMap == _maps.First().Value)
            {
                return;
            }

            if (string.IsNullOrEmpty(mapping))
            {
                throw new ArgumentNullException();
            }

            var stringInfo = StringInfo.ParseCombiningCharacters(mapping);

            if (stringInfo.Length > 1)
            {
                if (stringInfo.Length > 2)
                {
                    throw new ArgumentException(mapping);
                }

                var first = char.ConvertToUtf32(mapping, stringInfo.First());
                var last  = char.ConvertToUtf32(mapping, stringInfo.Last());

                if (first > last)
                {
                    throw new ArgumentException(mapping);
                }

                while (first <= last)
                {
                    var firstAsStr = char.ConvertFromUtf32(first);
                    if (_currentMap.ContainsKey(firstAsStr))
                    {
                        _currentMap[firstAsStr] = code++;
                    }
                    else
                    {
                        _currentMap.Add(firstAsStr, code++);
                    }
                    first++;
                }
            }
            else
            {
                if (_currentMap.ContainsKey(mapping))
                {
                    _currentMap[mapping] = code;
                }
                else
                {
                    _currentMap.Add(mapping, code);
                }
            }
        }
예제 #19
0
        static void EnumTextElementIndexes(string s)
        {
            string output = string.Empty;

            int[] textElemIndex = StringInfo.ParseCombiningCharacters(s);
            for (int i = 0; i < textElemIndex.Length; i++)
            {
                output += string.Format("Character {0} starts at index{1}{2}", i, textElemIndex[i], Environment.NewLine);
            }
            MessageBox.Show(output, "Result of ParseCombiningCharacters");
        }
예제 #20
0
        public IEnumerable <ushort> FindGlyphs(TFont font, string str)
        {
            // not completely sure this is correct. Need an actual
            // example of a composed character sequence coming from LaTeX.
            var unicodeIndexes = StringInfo.ParseCombiningCharacters(str);

            foreach (var index in unicodeIndexes)
            {
                yield return(FindGlyphForCharacterAtIndex(font, index, str));
            }
        }
예제 #21
0
        public void TestCharacterRanges()
        {
            string foo     = "\u0104\u0301Hello\u0104\u0304  world";
            int    length  = foo.Length;
            var    unicode = new UnicodeEncoding();

            byte[] encodeFoo = unicode.GetBytes(foo);
            int[]  fooInfo   = StringInfo.ParseCombiningCharacters(foo);
            Assert.DoesNotContain(1, fooInfo);
            Assert.DoesNotContain(8, fooInfo);
        }
예제 #22
0
 private void update()
 {
     if (text.Length > 0)
     {
         graphemeIndices = StringInfo.ParseCombiningCharacters(
             text.ToString());
     }
     else
     {
         graphemeIndices = emptyArray;
     }
 }
예제 #23
0
        private static void EnumTextElementIndexes(string s)
        {
            var output     = string.Empty;
            var characters = StringInfo.ParseCombiningCharacters(s);

            for (var i = 0; i < characters.Length; i++)
            {
                output += string.Format("Character {0} starts at index {1}{2}", i, characters[i], Environment.NewLine);
            }

            Console.WriteLine(output);
        }
예제 #24
0
 // private initialization method
 private void Initialize(string input)
 {
     try
     {
         myPos    = 0;
         myString = input;
         myIndex  = StringInfo.ParseCombiningCharacters(myString);
     }
     catch (ArgumentNullException)
     {
         throw (new StringWalkerException());
     }
 }
예제 #25
0
        private static void EnumTextElementIndexes(String s)
        {
            String output = String.Empty;

            Int32[] textElemIndex = StringInfo.ParseCombiningCharacters(s);
            for (Int32 i = 0; i < textElemIndex.Length; i++)
            {
                output += String.Format(
                    "Character {0} starts at index {1}{2}",
                    i, textElemIndex[i], Environment.NewLine);
            }
            Console.WriteLine(output);
        }
        private static string EnumTextElementIndexes(string s)
        {
            var output        = string.Empty;
            var textElemIndex = StringInfo.ParseCombiningCharacters(s);

            for (var i = 0; i < textElemIndex.Length; i++)
            {
                output +=
                    $"Character {i} starts at index {textElemIndex[i]}{Environment.NewLine}";
            }

            return(output);
        }
예제 #27
0
            public void ProcessSurrogateFonts(int start, int length)
            {
                string text = this.Text;

                if (text != null)
                {
                    int[] numArray = StringInfo.ParseCombiningCharacters(text);
                    if (numArray.Length != text.Length)
                    {
                        for (int i = 0; i < numArray.Length; i++)
                        {
                            if ((numArray[i] >= start) && (numArray[i] < (start + length)))
                            {
                                string str2 = null;
                                char   ch   = text[numArray[i]];
                                char   ch2  = '\0';
                                if ((numArray[i] + 1) < text.Length)
                                {
                                    ch2 = text[numArray[i] + 1];
                                }
                                if (((ch >= 0xd800) && (ch <= 0xdbff)) && ((ch2 >= 0xdc00) && (ch2 <= 0xdfff)))
                                {
                                    int num2 = ((ch / '@') - 0x360) + 1;
                                    System.Drawing.Font font = this._fallbackFonts[num2] as System.Drawing.Font;
                                    if (font == null)
                                    {
                                        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\LanguagePack\SurrogateFallback"))
                                        {
                                            if (key != null)
                                            {
                                                str2 = (string)key.GetValue("Plane" + num2);
                                                if (!string.IsNullOrEmpty(str2))
                                                {
                                                    font = new System.Drawing.Font(str2, base.Font.Size, base.Font.Style);
                                                }
                                                this._fallbackFonts[num2] = font;
                                            }
                                        }
                                    }
                                    if (font != null)
                                    {
                                        int num3 = (i == (numArray.Length - 1)) ? (text.Length - numArray[i]) : (numArray[i + 1] - numArray[i]);
                                        base.Select(numArray[i], num3);
                                        base.SelectionFont = font;
                                    }
                                }
                            }
                        }
                    }
                }
            }
예제 #28
0
    private bool VerificationHelper(string str, int[] expected, string errorno)
    {
        bool retVal = true;

        int[] result = StringInfo.ParseCombiningCharacters(str);

        if (!compare <int>(result, expected))
        {
            TestLibrary.TestFramework.LogError(errorno, "The result is not the value as expected");
            retVal = false;
        }

        return(retVal);
    }
        public IEnumerable <TextElementSegment> TextElementSegments([PexAssumeNotNull] string input)
        {
            // arrange
            int[] compiningCharacters = StringInfo.ParseCombiningCharacters(input);

            // act
            IEnumerable <TextElementSegment> result = CommonStringExtensions.TextElementSegments(input);

            // assert
            PexAssert.IsNotNull(result);
            PexAssert.AreEqual(compiningCharacters.Count(), result.Count());

            return(result);
        }
예제 #30
0
 public static bool ContainsLetter(this string s)
 {
     if (s != null)
     {
         foreach (var index in StringInfo.ParseCombiningCharacters(s))
         {
             var uc = CharUnicodeInfo.GetUnicodeCategory(s, index);
             if (uc == UnicodeCategory.LowercaseLetter || uc == UnicodeCategory.UppercaseLetter || uc == UnicodeCategory.TitlecaseLetter || uc == UnicodeCategory.ModifierLetter || uc == UnicodeCategory.OtherLetter)
             {
                 return(true);
             }
         }
     }
     return(false);
 }