4 * License : The MIT License
\r
5 * Copyright(c) 2008 olyutorskii
\r
8 package jp.sourceforge.jindolf;
\r
10 import java.util.regex.Matcher;
\r
15 public final class StringUtils{
\r
17 private static final int SUPLEN = 5;
\r
23 private StringUtils(){
\r
26 throw new AssertionError();
\r
31 * 正規表現にマッチした領域を数値化する。
\r
33 * @param matcher Matcher
\r
34 * @param groupIndex 前方指定グループ番号
\r
36 * @throws IndexOutOfBoundsException 不正なグループ番号
\r
38 public static int parseInt(CharSequence seq,
\r
41 throws IndexOutOfBoundsException {
\r
42 return parseInt(seq,
\r
43 matcher.start(groupIndex),
\r
44 matcher.end(groupIndex) );
\r
52 public static int parseInt(CharSequence seq){
\r
53 return parseInt(seq, 0, seq.length());
\r
59 * @param startPos 範囲開始位置
\r
60 * @param endPos 範囲終了位置
\r
62 * @throws IndexOutOfBoundsException 不正な位置指定
\r
64 public static int parseInt(CharSequence seq, int startPos, int endPos)
\r
65 throws IndexOutOfBoundsException{
\r
68 for(int pos = startPos; pos < endPos; pos++){
\r
69 char ch = seq.charAt(pos);
\r
70 int digit = Character.digit(ch, 10);
\r
71 if(digit < 0) break;
\r
81 * 「abcdefg」→「abc…efg」
\r
85 public static CharSequence suppressString(CharSequence str){
\r
86 String result = str.toString();
\r
87 result = result.replaceAll("[\u0020\\t\\n\\r\u3000]", "");
\r
88 if(result.length() <= SUPLEN * 2) return result;
\r
89 int len = result.length();
\r
90 String head = result.substring(0, SUPLEN);
\r
91 String tail = result.substring(len - SUPLEN, len);
\r
92 result = head + "…" + tail;
\r
97 * ある文字列の末尾が別の文字列に一致するか判定する。
\r
98 * @see String#endsWith(String)
\r
99 * @param target 判定対象
\r
101 * @return 一致すればtrue
\r
102 * @throws java.lang.NullPointerException 引数がnull
\r
104 public static boolean isTerminated(CharSequence target,
\r
106 throws NullPointerException{
\r
107 if(target == null || term == null) throw new NullPointerException();
\r
109 int targetLength = target.length();
\r
110 int termLength = term .length();
\r
112 int offset = targetLength - termLength;
\r
113 if(offset < 0) return false;
\r
115 for(int pos = 0; pos < termLength; pos++){
\r
116 char targetch = target.charAt(offset + pos);
\r
117 char termch = term .charAt(0 + pos);
\r
118 if(targetch != termch) return false;
\r
126 * @see CharSequence#subSequence(int,int)
\r
127 * @param seq1 サブシーケンス1
\r
128 * @param start1 開始インデックス1
\r
129 * @param end1 終了インデックス1
\r
130 * @param seq2 サブシーケンス2
\r
131 * @param start2 開始インデックス2
\r
132 * @param end2 終了インデックス2
\r
133 * @return サブシーケンス1の方が小さければ負、大きければ正、等しければ0
\r
134 * @throws IndexOutOfBoundsException 不正なインデックス指定
\r
136 public static int compareSubSequence(
\r
137 CharSequence seq1, int start1, int end1,
\r
138 CharSequence seq2, int start2, int end2 )
\r
139 throws IndexOutOfBoundsException{
\r
144 if(pos1 >= end1) break;
\r
145 if(pos2 >= end2) break;
\r
146 char ch1 = seq1.charAt(pos1);
\r
147 char ch2 = seq2.charAt(pos2);
\r
148 int diff = ch1 - ch2;
\r
149 if(diff != 0) return diff;
\r
154 int length1 = end1 - start1;
\r
155 int length2 = end2 - start2;
\r
157 if(length1 == length2) return 0;
\r
158 if(length1 < length2) return -1;
\r
163 * 文字シーケンスとサブシーケンスを比較する。
\r
164 * @see CharSequence#subSequence(int,int)
\r
165 * @param seq1 文字シーケンス
\r
166 * @param seq2 サブシーケンス
\r
167 * @param start2 開始インデックス
\r
168 * @param end2 終了インデックス
\r
169 * @return 文字シーケンスの方が小さければ負、大きければ正、等しければ0
\r
170 * @throws IndexOutOfBoundsException 不正なインデックス指定
\r
172 public static int compareSubSequence(
\r
174 CharSequence seq2, int start2, int end2 )
\r
175 throws IndexOutOfBoundsException{
\r
176 int result = compareSubSequence(seq1, 0, seq1.length(),
\r
177 seq2, start2, end2 );
\r
181 // TODO 文字エンコーダ・デコーダ処理の一本化。
\r
182 // TODO 文字エンコーダ・デコーダのカスタム化。「~」対策など。
\r