View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.conversion;
2   
3   import com.github.mygreen.supercsv.util.ArgUtils;
4   
5   /**
6    * 文字の幅によって区別してパディングする。
7    * <p>半角は長さ1、全角は長さ2としてカウントして処理します。</p>
8    * <p>サロゲートペアは長さ2としてカウントします。</p>
9    *
10   * @since 2.1
11   * @author T.TSUCHIE
12   *
13   */
14  public class CharWidthPaddingProcessor extends AbstractPaddingOperator {
15  
16      @Override
17      public int count(int codePoint) {
18          if(Character.charCount(codePoint) >= 2) {
19              // サロゲートペアの文字=全角として2文字としてカウントする。
20              return 2;
21          }
22  
23          final char c = (char)codePoint;
24          if(c <= '\u007e'    // 英数字
25                  || c == '\u00a5'    // \記号
26                  || c == '\u203e'    // ~記号
27                  || (c >= '\uff61' && c <= '\uff9f') // 半角カナ
28                  ) {
29              return 1;
30          } else {
31              return 2;
32          }
33      }
34  
35      @Override
36      public int count(final String text) {
37  
38          ArgUtils.notNull(text, "text");
39  
40          int count=0;
41  
42          final int length = text.length();
43          for(int i=0, codePoint=0; i < length; i+=Character.charCount(codePoint)) {
44              codePoint = text.codePointAt(i);
45              count += count(codePoint);
46          }
47  
48          return count;
49      }
50  
51  
52  }