示例#1
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Text.Normalizer.#ctor(System.String,PeterO.Text.Normalization)"]/*'/>
 public Normalizer(string str, Normalization form) {
   this.nci = new NormalizingCharacterInput(str, form);
 }
 private static bool NormalizeAndCheckString(
   string charString,
   int start,
   int length,
   Normalization form) {
   int i = start;
   var norm = new NormalizingCharacterInput(
  charString,
  start,
  length,
  form);
   var ch = 0;
   while ((ch = norm.ReadChar()) >= 0) {
     int c = charString[i];
     if ((c & 0x1ffc00) == 0xd800 && i + 1 < charString.Length &&
         charString[i + 1] >= 0xdc00 && charString[i + 1] <= 0xdfff) {
       // Get the Unicode code point for the surrogate pair
       c = 0x10000 + ((c - 0xd800) << 10) + (charString[i + 1] - 0xdc00);
       ++i;
     } else if ((c & 0x1ff800) == 0xd800) {
       // unpaired surrogate
       c = 0xfffd;
     }
     ++i;
     if (c != ch) {
       return false;
     }
   }
   return i == start + length;
 }
    public void TestRead() {
      var nci = new NormalizingCharacterInput("test");
      try {
        nci.Read(null, 0, 0);
        Assert.Fail("Should have failed");
      } catch (ArgumentNullException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      try {
        nci.Read(new int[] { 't' }, -1, 1);
        Assert.Fail("Should have failed");
      } catch (ArgumentException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      try {
        nci.Read(new int[] { 't' }, 5, 1);
        Assert.Fail("Should have failed");
      } catch (ArgumentException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      try {
        nci.Read(new int[] { 't' }, 0, -1);
        Assert.Fail("Should have failed");
      } catch (ArgumentException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      try {
        nci.Read(new int[] { 't' }, 0, 5);
        Assert.Fail("Should have failed");
      } catch (ArgumentException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      try {
        nci.Read(new int[] { 't', 't' }, 1, 2);
        Assert.Fail("Should have failed");
      } catch (ArgumentException) {
        Console.Write(String.Empty);
} catch (Exception ex) {
        Assert.Fail(ex.ToString());
        throw new InvalidOperationException(String.Empty, ex);
      }
      Assert.AreEqual(1, nci.Read(new int[] { 't', 't' }, 1, 1));
    }
 public static IList<int> GetChars(string str, Normalization form)
 {
     if (str == null) {
       throw new ArgumentNullException("str");
     }
     IList<int> ret = new List<int>();
     int ch;
     var input = new NormalizingCharacterInput(str, form);
     while ((ch = input.ReadChar()) >= 0) {
        ret.Add(ch);
     }
     return ret;
 }