OSDN Git Service

add irregular SJIS case test
authorOlyutorskii <olyutorskii@users.osdn.me>
Thu, 1 Jun 2017 10:01:53 +0000 (19:01 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Thu, 1 Jun 2017 10:01:53 +0000 (19:01 +0900)
src/main/java/jp/sourceforge/jindolf/parser/ContentBuilderSJ.java
src/main/java/jp/sourceforge/jindolf/parser/SjisDecoder.java
src/test/java/jp/sourceforge/jindolf/parser/SjisDecoderTest.java

index bcb14a8..50c3cc5 100644 (file)
@@ -96,21 +96,34 @@ public class ContentBuilderSJ extends ContentBuilder{
     @Override
     public void decodingError(byte[] errorArray, int offset, int length)
             throws DecodeException{
+        DecodedContent text = getContent();
+
+        switch(length){
+        case 1:
+            text.addDecodeError(errorArray[0]);
+            return;
+        case 2:
+            text.addDecodeError(errorArray[0], errorArray[1]);
+            return;
+        default:
+            break;
+        }
+
         int limit = offset + length;
         for(int bpos = offset; bpos < limit; bpos++){
             byte bval = errorArray[bpos];
 
             if(this.hasByte1st){
                 if(ShiftJis.isShiftJIS2ndByte(bval)){   // 文字集合エラー
-                    getContent().addDecodeError(this.byte1st, bval);
+                    text.addDecodeError(this.byte1st, bval);
                     this.hasByte1st = false;
                 }else if(ShiftJis.isShiftJIS1stByte(bval)){
-                    getContent().addDecodeError(this.byte1st);
+                    text.addDecodeError(this.byte1st);
                     this.byte1st = bval;
                     this.hasByte1st = true;
                 }else{
-                    getContent().addDecodeError(this.byte1st);
-                    getContent().addDecodeError(bval);
+                    text.addDecodeError(this.byte1st);
+                    text.addDecodeError(bval);
                     this.hasByte1st = false;
                 }
             }else{
@@ -118,10 +131,9 @@ public class ContentBuilderSJ extends ContentBuilder{
                     this.byte1st = bval;
                     this.hasByte1st = true;
                 }else{
-                    getContent().addDecodeError(bval);
+                    text.addDecodeError(bval);
                 }
             }
-
         }
 
         return;
index eb19be5..3353baa 100644 (file)
@@ -47,6 +47,7 @@ public class SjisDecoder extends StreamDecoder{
         return;
     }
 
+
     /**
      * 1バイトのエラーをUnmap系2バイトエラーに統合できないか試す。
      *
@@ -57,7 +58,7 @@ public class SjisDecoder extends StreamDecoder{
      *
      * @param result デコード異常系
      * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。
-     * @throws IOException 入力エラー
+     * @throws IOException 追加入力エラー
      */
     private CoderResult modify1ByteError(CoderResult result)
             throws IOException {
@@ -110,10 +111,10 @@ public class SjisDecoder extends StreamDecoder{
         byte next = inbuffer.get(nextPos);
 
         CoderResult newResult;
-        if( ! ShiftJis.isShiftJIS(curr, next) ){
-            newResult = CoderResult.malformedForLength(1);
-        }else{
+        if( ShiftJis.isShiftJIS(curr, next) ){
             newResult = result;
+        }else{
+            newResult = CoderResult.malformedForLength(1);
         }
 
         return newResult;
index 3074561..55271c8 100644 (file)
@@ -146,6 +146,41 @@ public class SjisDecoderTest {
             // GOOD
         }
 
+        sjd = new SjisDecoder(4, 10);
+        sjd.setDecodeHandler(handler);
+        is = byteIs("41:8540:42");
+        handler.clear();
+        sjd.decode(is);
+        assertEquals("[ST][CH]A[ER]8540[CH]B[EN]", handler.toString());
+
+        sjd = new SjisDecoder(4, 10);
+        sjd.setDecodeHandler(handler);
+        is = byteIs("414243:8540:44");
+        handler.clear();
+        sjd.decode(is);
+        assertEquals("[ST][CH]ABC[ER]8540[CH]D[EN]", handler.toString());
+
+        sjd = new SjisDecoder(4, 10);
+        sjd.setDecodeHandler(handler);
+        is = byteIs("414243:85");
+        handler.clear();
+        sjd.decode(is);
+        assertEquals("[ST][CH]ABC[ER]85[EN]", handler.toString());
+
+        sjd = new SjisDecoder(4, 10);
+        sjd.setDecodeHandler(handler);
+        is = byteIs("41:ff32:42");
+        handler.clear();
+        sjd.decode(is);
+        assertEquals("[ST][CH]A[ER]ff[CH]2B[EN]", handler.toString());
+
+        sjd = new SjisDecoder(4, 10);
+        sjd.setDecodeHandler(handler);
+        is = byteIs("414243:ff32:44");
+        handler.clear();
+        sjd.decode(is);
+        assertEquals("[ST][CH]ABC[ER]ff[CH]2D[EN]", handler.toString());
+
         return;
     }