@@ -392,11 +392,13 @@ public Appendable GenerateComment(Appendable source, ParserRegistry registry, Ob
392392 *
393393 * @return This scope after loading data from reader (you most likely want to return "this")!
394394 *
395+ * @throws IOException When reading the reader fails...
396+ *
395397 * @since 1.3.2
396398 */
397399 @ SuppressWarnings ("unchecked" )
398400 @ Override
399- public <S extends Scope > S LoadFrom (Reader reader , Object ... args )
401+ public <S extends Scope > S LoadFrom (Reader reader , Object ... args ) throws IOException
400402 {
401403 boolean formatRequired = true ;
402404
@@ -412,91 +414,140 @@ public <S extends Scope> S LoadFrom(Reader reader, Object... args)
412414 List <Object > objs = splitAndParse (str , args );
413415 addAll (objs );
414416
415- //double t0 = System.nanoTime();
416- /*Registry<DataParser> reg = DataParser.REGISTRY;
417- String lastLine = null;
418- int quote = 0, multLineCom = -1, brackets = 0;
419- try
417+ if (parent == null )
418+ getImports ().removeImportsOf (this );
419+ return (S ) this ;
420+ }
421+
422+ public void readAndParse (Reader reader ) throws IOException
423+ {
424+ final char endl = System .lineSeparator ().charAt (0 );
425+
426+ // StringBuilder str = readAndFormat(reader, formatRequired); // TODO could be improved...
427+ // List<Object> objs = splitAndParse(str, args);
428+ // addAll(objs);
429+
430+ StringBuilder sb = new StringBuilder ();
431+ char [] chars = new char [128 *2 ];
432+
433+ for (int charsRead , oldCh = 0 , state = 0 , brackets ; (charsRead = reader .read (chars )) != -1 ; )
420434 {
421- BufferedReader lineReader = new BufferedReader(reader);
422- //String blanks = new String(new char[] {32, 9, 10, 12, 13});
423- for (String line = lineReader.readLine(); line != null; line = lineReader.readLine())
435+ int i = 0 ;
436+ switch (state ) // Complete the unfinished action from prev iter based on state // <- Too much repetitive code already, and pain in the ass to work around with, consider other solution
424437 {
425- for (int i = 0, len = line.length(), com = -1, lastIndex = 0; i < len; i++)
426- {
427- char ch = line.charAt(i);
428- if (ch == '/' && i < len-1 && line.charAt(i+1) == '/')
429- com++;
430- else if (multLineCom <= -1 && ch == '"')
431- quote++;
432-
433- boolean notString = quote % 2 == 0;
434- if (multLineCom > -1 || com > -1) //Is comment
438+ case '/' : // Handle ongoing comments...
439+ do if (chars [i ] == endl ) // Skip all until endl
435440 {
436- if (multLineCom > 0 && ch == '*' && i < len-1 && line.charAt(++i) == '/')
437- com = multLineCom = -1;
441+ oldCh = state = 0 ;
442+ i ++;
443+ break ;
438444 }
439- else if (notString && ch == '/' && i < len-1 && line.charAt(i+1) == '*')
440- i += multLineCom = 1;
441- /*else if (notString && blanks.indexOf(ch) > -1)
445+ while (++i < charsRead );
446+
447+ break ;
448+ case '*' :
449+ do if (chars [i ] == '*' && (++i < charsRead ? chars [i ] : reader .read ()) == '/' ) // Skip all until */
442450 {
443- if ((chBefore = i > 0 ? line.charAt(i-1) : 0) != ';' && chBefore != '{' && blanks.indexOf(chBefore) <= -1)
444- sb.append(" ");
445- }*/
446- /*else if (notString && i < str.length()-1 && (ch == '!' && str.charAt(i+1) == '!' || ch == '-' && str.charAt(i+1) == '-' || ch == '+' && str.charAt(i+1) == '+'))
447- i++;*
448- else
451+ oldCh = state = 0 ;
452+ i ++;
453+ break ;
454+ }
455+ while (++i < charsRead );
456+
457+ break ;
458+
459+ case '\"' :
460+ case '\'' :
461+ do // Append all chars until end of str/char
449462 {
450- if (notString)
463+ char ch ;
464+ sb .append (ch = chars [i ]);
465+ if (ch == state )
451466 {
452- if (ch | ' ') == '{' || ch == '[')
453- brackets++;
454- else if (ch == '}' || ch == ']')
467+ state = 0 ;
468+ i ++;
469+ break ;
470+ }
471+ }
472+ while (++i < charsRead );
473+
474+ break ;
475+ // case ' ':
476+ // do if (chars[i] > 32) // Skip all blanks until next non-blank
477+ // {
478+ // state = 0;
479+ // //i++;
480+ // break;
481+ // }
482+ // while (++i < charsRead);
483+ //
484+ // break;
485+ }
486+
487+ charsLoop : for (; i < charsRead ; i ++)
488+ {
489+ int ch = chars [i ];
490+ if (ch < 33 ) // Handle blanks, skip unnecessary
491+ {
492+ if (oldCh > 32 ) // First blank (in possible row)
493+ sb .append (oldCh /*= state*/ = ' ' );
494+ continue ;
495+ }
496+
497+ if (oldCh == '/' ) // Skip the comments...
498+ {
499+ if (ch == '/' ) // //
500+ {
501+ for (state = '/' ; ++i < charsRead ; ); // Skip all until endl
502+ if (chars [i ] == endl )
455503 {
456- if (brackets > 0)
457- brackets--;
458- else
459- throw new IllegalArgumentException("Missing opening bracket in: " + line);
504+ oldCh = state = 0 ;
505+ continue charsLoop ;
460506 }
461- else if (brackets == 0 && (ch == ';' || ch == ','))
507+ }
508+ else if (ch == '*' ) // /*
509+ {
510+ for (state = '*' ; ++i < charsRead ; ) // Skip all until */
511+ if (chars [i ] == '*' && (++i < charsRead ? chars [i ] : reader .read ()) == '/' ) // */
462512 {
463- //System.out.println(lastIndex + " " + i);
464- String str = line.substring(lastIndex == 0 ? 0 : lastIndex + 1, lastIndex = i);
465- //System.out.println(str);
466- if (!(str = str.trim()).isEmpty())
467- {
468- Object obj = ParseObjectHandleNull(reg, str, true, result);
469- if (obj != VOID)
470- result.add(obj);
471- }
513+ oldCh = state = 0 ;
514+ continue charsLoop ;
472515 }
516+ }
517+
518+ continue ;
519+ }
520+
521+ if (ch == '\"' || ch == '\'' )
522+ {
523+ sb .append (ch );
524+ for (state = ch ; ++i < charsRead ; ) // Append all chars until end of str/char
525+ {
526+ sb .append (ch = chars [i ]);
527+ if (ch == state )
528+ {
529+ state = 0 ;
530+ break ;
473531 }
474532 }
475533 }
476- lastLine = line;
534+ else if ((ch | ' ' ) == '{' )
535+ {
536+ for (brackets = 1 ; ++i < charsRead && brackets != 0 ; )
537+ {
538+ if ((ch = (chars [i ] | ' ' )) == '{' )
539+ brackets ++;
540+ else if (ch == '}' )
541+ brackets --;
542+ else if (ch == '"' )
543+ while (++i < charsRead && chars [i ] != '"' );
544+ }
545+ }
546+
547+ oldCh = ch ;
477548 }
478- lineReader.close();
479- }
480- catch (IOException e)
481- {
482- e.printStackTrace();
483549 }
484- //double t = System.nanoTime();
485- //System.out.println((t-t0)/1000000);
486550
487- if (lastLine != null && indexOfNotInObj(lastLine, ';', ',') <= -1)
488- {
489- if (!(lastLine = lastLine.trim()).isEmpty())
490- {
491- Object obj = ParseObjectHandleNull(reg, lastLine, true, result);
492- if (obj != VOID)
493- result.add(obj);
494- }
495- }*/
496-
497- if (parent == null )
498- getImports ().removeImportsOf (this );
499- return (S ) this ;
500551 }
501552
502553 /**
@@ -589,7 +640,7 @@ else if (ch == '}' || ch == ']')
589640 *
590641 * @since 1.3.2
591642 */
592- protected List <Object > splitAndParse (StringBuilder formattedStr , Object ... parserArgs )
643+ public List <Object > splitAndParse (StringBuilder formattedStr , Object ... parserArgs )
593644 {
594645 List <Object > result = new ArrayList <>();
595646
@@ -624,6 +675,7 @@ else if (ch == '"')
624675 else if ((ch == ';' || ch == ',' )/* || (brackets == 1 && (isBracketSplit = ch == '}' || ch == ']'))*/ )
625676 {
626677 String str = formattedStr .substring (lastIndex == 0 ? 0 : lastIndex + 1 , lastIndex = i /*+ (isBracketSplit ? 1 : 0)*/ ).trim ();
678+ System .out .println (str + "\n " );
627679 if (!str .isEmpty ())
628680 {
629681 Object obj = parseObject (reg , str , parserArgs );
@@ -635,6 +687,7 @@ else if (ch == '"')
635687 }
636688
637689 String str = formattedStr .substring (lastIndex == 0 ? 0 : lastIndex + 1 , len ).trim ();
690+ System .out .println (str + "\n " );
638691 if (!str .isEmpty ())
639692 {
640693 Object obj = parseObject (reg , str , parserArgs );
0 commit comments