public void OnValueChange() { if (field != null) { string inStr = field.text; if (inStr.Length != 0) { System.Text.StringBuilder retStr = new System.Text.StringBuilder(); System.Globalization.TextElementEnumerator tee = System.Globalization.StringInfo.GetTextElementEnumerator(inStr); tee.Reset(); while (tee.MoveNext()) { // 1文字取得 var te = tee.GetTextElement(); // 1文字が2つ以上のcharからなる場合は、サロゲートペアと判断 if (1 < te.Length) { // 文字列から除去 } else { retStr = retStr.Append(te); } } // InputFieldに返す field.text = retStr.ToString(); } } }
/// <summary>Returns the indexes of each base character, high surrogate, or control character within the specified string.</summary> /// <returns>An array of integers that contains the zero-based indexes of each base character, high surrogate, or control character within the specified string.</returns> /// <param name="str">The string to search. </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="str" /> is null. </exception> public static int[] ParseCombiningCharacters(string str) { if (str == null) { throw new ArgumentNullException("string is null"); } ArrayList arrayList = new ArrayList(str.Length); TextElementEnumerator textElementEnumerator = StringInfo.GetTextElementEnumerator(str); textElementEnumerator.Reset(); while (textElementEnumerator.MoveNext()) { arrayList.Add(textElementEnumerator.ElementIndex); } return((int[])arrayList.ToArray(typeof(int))); }
public static String ToEncode(IntPtr code, IntPtr nNormalizeForm) { String text = ""; try { if ((Int32)(dynamic)Hm.Macro.Var["selecting"] == 1) { text = Hm.Edit.SelectedText; } else { text = Hm.Edit.TotalText; } int lineCnt = text.Count(c => c == '\n'); if (lineCnt < 2) { text += "\n\n"; } } catch (Exception ex0) { return(ex0.GetType() + ":" + ex0.Message + "\n元のファイルにバイナリが混在していないかを確認してください。"); } String normalize_text = ""; try { if (nNormalizeForm.ToInt32() == 2) { normalize_text = text.Normalize(); } else if (nNormalizeForm.ToInt32() == 1) { StringBuilder sb = new StringBuilder(500 * 1024); // 500k程度をデフォルトとして確保 //TextElementEnumeratorを作成する System.Globalization.TextElementEnumerator tee = System.Globalization.StringInfo.GetTextElementEnumerator(text); //読み取る位置をテキストの先頭にする tee.Reset(); //1文字ずつ取得する while (tee.MoveNext()) { //1文字取得する string te = tee.GetTextElement(); //1文字が2つ以上のCharから成る場合は、サロゲートペアか結合文字列と判断する if (te.Length > 1) { // ノーマライズして足す sb.Append(te.Normalize()); /* * //サロゲートペアか調べる * if (te.Length == 2 && char.IsSurrogatePair(te, 0)) * { * Console.WriteLine("サロゲートペア「{0}」が「{1}」の位置にあります。", * te, tee.ElementIndex); * } * else * { * //サロゲートペアでない場合は結合文字列と判断する * Console.WriteLine("結合文字列「{0}」が「{1}」の位置にあります。", * te, tee.ElementIndex); * } */ } else { // 普通に足す sb.Append(te); } } normalize_text = sb.ToString(); } else { normalize_text = text; } } catch (Exception ex1) { return(ex1.GetType() + ":" + ex1.Message + "\n元のファイルにバイナリが混在していないかを確認してください。"); } try { // 独自に実装したフォールバックを指定してEncodingを取得 var encode = Encoding.GetEncoding(code.ToInt32(), new HmEncoderScalarValueFallback(), DecoderFallback.ReplacementFallback); var bytes = encode.GetBytes(normalize_text); // 再び文字列に戻して return(encode.GetString(bytes)); } catch (Exception ex2) { return(ex2.GetType() + ":" + ex2.Message + "\n指定の「#ToEncodeCodePage」の値等が正しいか、よく確認してください。"); } }