StringInfo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // System.Globalization.StringInfo.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc.
  8. // (C) 2004 Novell, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. namespace System.Globalization {
  34. [Serializable]
  35. #if NET_2_0
  36. [System.Runtime.InteropServices.ComVisible(true)]
  37. #endif
  38. public class StringInfo {
  39. public StringInfo()
  40. {
  41. }
  42. public static string GetNextTextElement(string str)
  43. {
  44. if(str == null || str.Length == 0) {
  45. throw new ArgumentNullException("string is null");
  46. }
  47. return(GetNextTextElement (str, 0));
  48. }
  49. public static string GetNextTextElement(string str, int index)
  50. {
  51. if(str == null) {
  52. throw new ArgumentNullException("string is null");
  53. }
  54. if(index < 0 || index >= str.Length) {
  55. throw new ArgumentOutOfRangeException ("Index is not valid");
  56. }
  57. /* Find the next base character, surrogate
  58. * pair or combining character sequence
  59. */
  60. char ch = str[index];
  61. UnicodeCategory cat = char.GetUnicodeCategory (ch);
  62. if (cat == UnicodeCategory.Surrogate) {
  63. /* Check that it's a high surrogate
  64. * followed by a low surrogate
  65. */
  66. if (ch >= 0xD800 && ch <= 0xDBFF) {
  67. if ((index + 1) < str.Length &&
  68. str[index + 1] >= 0xDC00 &&
  69. str[index + 1] <= 0xDFFF) {
  70. /* A valid surrogate pair */
  71. return(str.Substring (index, 2));
  72. } else {
  73. /* High surrogate on its own */
  74. return(new String (ch, 1));
  75. }
  76. } else {
  77. /* Low surrogate on its own */
  78. return(new String (ch, 1));
  79. }
  80. } else {
  81. /* Look for a base character, which
  82. * may or may not be followed by a
  83. * series of combining characters
  84. */
  85. if (cat == UnicodeCategory.NonSpacingMark ||
  86. cat == UnicodeCategory.SpacingCombiningMark ||
  87. cat == UnicodeCategory.EnclosingMark) {
  88. /* Not a base character */
  89. return(new String (ch, 1));
  90. }
  91. int count = 1;
  92. while (index + count < str.Length) {
  93. cat = char.GetUnicodeCategory (str[index + count]);
  94. if (cat != UnicodeCategory.NonSpacingMark &&
  95. cat != UnicodeCategory.SpacingCombiningMark &&
  96. cat != UnicodeCategory.EnclosingMark) {
  97. /* Finished the sequence */
  98. break;
  99. }
  100. count++;
  101. }
  102. return(str.Substring (index, count));
  103. }
  104. }
  105. public static TextElementEnumerator GetTextElementEnumerator(string str)
  106. {
  107. if(str == null || str.Length == 0) {
  108. throw new ArgumentNullException("string is null");
  109. }
  110. return(new TextElementEnumerator (str, 0));
  111. }
  112. public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
  113. {
  114. if(str == null) {
  115. throw new ArgumentNullException("string is null");
  116. }
  117. if(index < 0 || index >= str.Length) {
  118. throw new ArgumentOutOfRangeException ("Index is not valid");
  119. }
  120. return(new TextElementEnumerator (str, index));
  121. }
  122. public static int[] ParseCombiningCharacters(string str)
  123. {
  124. if(str == null) {
  125. throw new ArgumentNullException("string is null");
  126. }
  127. ArrayList indices = new ArrayList (str.Length);
  128. TextElementEnumerator tee = GetTextElementEnumerator (str);
  129. tee.Reset ();
  130. while(tee.MoveNext ()) {
  131. indices.Add (tee.ElementIndex);
  132. }
  133. return((int[])indices.ToArray (typeof (int)));
  134. }
  135. }
  136. }