package redradishes.decoder.parser;

import java.nio.ByteBuffer;
import java.nio.charset.CharsetDecoder;
import java.util.function.Function;

public class StringParser implements Parser<CharSequence> {
  public static final Parser<CharSequence> STRING_PARSER = new StringParser();
  private static final int READING = 0;
  private static final int WAITING_FOR_LF = 1;

  private StringParser() {
  }

  @Override
  public <U> U parse(ByteBuffer buffer, Function<? super CharSequence, U> resultHandler,
      PartialHandler<? super CharSequence, U> partialHandler, CharsetDecoder charsetDecoder) {
    return doParse(buffer, resultHandler, partialHandler, new StringBuilder(), READING);
  }

  private static <U> U doParse(ByteBuffer buffer, Function<? super CharSequence, U> resultHandler,
      PartialHandler<? super CharSequence, U> partialHandler, StringBuilder stringBuilder, int state) {
    while (buffer.hasRemaining()) {
      byte b = buffer.get();
      switch (state) {
        case READING:
          if (b == '\r') {
            state = WAITING_FOR_LF;
          } else {
            stringBuilder.append((char) b);
          }
          break;
        case WAITING_FOR_LF:
          if (b == '\n') {
            return resultHandler.apply(stringBuilder);
          } else {
            throw new IllegalStateException("LF is expected");
          }
      }
    }
    int state1 = state;
    return partialHandler.partial(new Parser<CharSequence>() {
      @Override
      public <U1> U1 parse(ByteBuffer buffer, Function<? super CharSequence, U1> resultHandler,
          PartialHandler<? super CharSequence, U1> partialHandler, CharsetDecoder charsetDecoder) {
        return doParse(buffer, resultHandler, partialHandler, stringBuilder, state1);
      }
    });
  }
}

@@ -133,16 +133,52 @@
 unction;
+%0Aimport java.util.function.Supplier;
 %0A%0Apublic
@@ -235,87 +235,8 @@
 %3E %7B%0A
-  public static final Parser%3CCharSequence%3E STRING_PARSER = new StringParser();%0A
   pr
@@ -322,345 +322,395 @@
  1;%0A
-%0A
   p
-rivate StringParser() %7B%0A  %7D%0A%0A  @Override%0A  public %3CU%3E U parse(ByteBuffer buffer, Function%3C? super CharSequence, U%3E resultHandler,%0A      PartialHandler%3C? super CharSequence, U%3E partialHandler, CharsetDecoder charsetDecoder) %7B%0A    return doParse(buffer, resultHandler, partialHandler, new StringBuilder(), READING);%0A  %7D%0A%0A  private stat
+ublic static final Parser%3CCharSequence%3E STRING_PARSER = new StringParser(StringBuilder::new, READING);%0A  private final Supplier%3CStringBuilder%3E stringBuilderSupplier;%0A  private final int state0;%0A%0A  private StringParser(Supplier%3CStringBuilder%3E stringBuilderSupplier, int state0) %7B%0A    this.stringBuilderSupplier = stringBuilderSupplier;%0A    this.state0 = state0;%0A  %7D%0A%0A  @Override%0A  publ
 ic %3C
@@ -714,19 +714,17 @@
 c %3CU%3E U 
-doP
+p
 arse(Byt
@@ -854,49 +854,127 @@
 er, 
-StringBuilder stringBuilder, int state) %7B
+CharsetDecoder charsetDecoder) %7B%0A    StringBuilder stringBuilder = stringBuilderSupplier.get();%0A    int state = state0;
 %0A   
@@ -1458,32 +1458,8 @@
   %7D%0A
-    int state1 = state;%0A
     
@@ -1496,306 +1496,26 @@
 new 
-Parser%3CCharSequence%3E() %7B%0A      @Override%0A      public %3CU1%3E U1 parse(ByteBuffer buffer, Function%3C? super CharSequence, U1%3E resultHandler,%0A          PartialHandler%3C? super CharSequence, U1%3E partialHandler, CharsetDecoder charsetDecoder) %7B%0A        return doParse(buffer, resultHandler, partialHandler,
+StringParser(() -%3E
  str
@@ -1535,25 +1535,9 @@
 tate
-1);%0A      %7D%0A    %7D
+)
 );%0A 

Simplify StringParser