Skip to content

Commit a6d5112

Browse files
prep for optimizations
1 parent db4d7a0 commit a6d5112

File tree

6 files changed

+174
-85
lines changed

6 files changed

+174
-85
lines changed

SerialX-core/src/main/java/org/ugp/serialx/Serializer.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,13 @@ public OutputStream SerializeTo(OutputStream outputStream, Object... args) throw
353353
* @return Unserialized objects and variables in {@link Scope} or empty {@link Scope} if string is empty.
354354
* You can use negative indexes to get objects from back of this array since 1.1.0 (-1 = last element)!
355355
*
356-
* @throws FileNotFoundException if file does not exist!
356+
* @throws FileNotFoundException if file does not exist or cannot be read!
357357
*
358358
* @since 1.0.0
359359
*
360360
* @see Serializer#LoadFrom(Reader, Object...)
361361
*/
362-
public <S extends Scope> S LoadFrom(File file, Object... parserArgs) throws FileNotFoundException
362+
public <S extends Scope> S LoadFrom(File file, Object... parserArgs) throws IOException
363363
{
364364
return LoadFrom(new FileReader(file), parserArgs);
365365
}
@@ -371,11 +371,13 @@ public <S extends Scope> S LoadFrom(File file, Object... parserArgs) throws File
371371
* @return Unserialized objects and variables in {@link Scope} or empty {@link Scope} if string is empty.
372372
* You can use negative indexes to get objects from back of this array since 1.1.0 (-1 = last element)!
373373
*
374+
* @throws IOException When reading the char sequence fails...
375+
*
374376
* @since 1.2.5
375377
*
376378
* @see Serializer#LoadFrom(Reader, Object...)
377379
*/
378-
public <S extends Scope> S LoadFrom(CharSequence str, Object... parserArgs)
380+
public <S extends Scope> S LoadFrom(CharSequence str, Object... parserArgs) throws IOException
379381
{
380382
return LoadFrom(new StringReader(str.toString()), parserArgs);
381383
}
@@ -387,11 +389,13 @@ public <S extends Scope> S LoadFrom(CharSequence str, Object... parserArgs)
387389
* @return Unserialized objects and variables in {@link Scope} or empty {@link Scope} if string is empty.
388390
* You can use negative indexes to get objects from back of this array since 1.1.0 (-1 = last element)!
389391
*
392+
* @throws IOException When reading the input stream fails...
393+
*
390394
* @since 1.3.2
391395
*
392396
* @see Serializer#LoadFrom(Reader, Object...)
393397
*/
394-
public <S extends Scope> S LoadFrom(InputStream stream, Object... parserArgs)
398+
public <S extends Scope> S LoadFrom(InputStream stream, Object... parserArgs) throws IOException
395399
{
396400
return LoadFrom(new InputStreamReader(stream), parserArgs);
397401
}
@@ -401,9 +405,11 @@ public <S extends Scope> S LoadFrom(InputStream stream, Object... parserArgs)
401405
*
402406
* @return This scope after loading data from reader (you most likely want to return "this")!
403407
*
408+
* @throws IOException When reading the reader fails...
409+
*
404410
* @since 1.2.5
405411
*/
406-
public <S extends Scope> S LoadFrom(Reader reader)
412+
public <S extends Scope> S LoadFrom(Reader reader) throws IOException
407413
{
408414
return LoadFrom(reader, true);
409415
}
@@ -414,9 +420,11 @@ public <S extends Scope> S LoadFrom(Reader reader)
414420
*
415421
* @return This scope after loading data from reader (you most likely want to return "this")!
416422
*
423+
* @throws IOException When reading the reader fails...
424+
*
417425
* @since 1.3.2
418426
*/
419-
public abstract <S extends Scope> S LoadFrom(Reader reader, Object... parserArgs);
427+
public abstract <S extends Scope> S LoadFrom(Reader reader, Object... parserArgs) throws IOException;
420428

421429
/**
422430
* @return Clone of this {@link Serializer} without variables and values, protocols and parser will remain same!

SerialX-juss/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>org.ugp.serialx</groupId>
1212
<artifactId>serialx-juss</artifactId>
13-
<version>1.3.8</version>
13+
<version>1.3.9-SNAPSHOT</version>
1414

1515
<name>SerialX juss</name>
1616
<description>SerialX support for Java Universal Serial Script data format, custom default format of SerialX!</description>

SerialX-juss/src/main/java/org/ugp/serialx/juss/JussSerializer.java

Lines changed: 123 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

SerialX-juss/src/main/java/org/ugp/serialx/juss/converters/ObjectConverter.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Base64;
1010

1111
import org.ugp.serialx.GenericScope;
12+
import org.ugp.serialx.LogProvider;
1213
import org.ugp.serialx.Scope;
1314
import org.ugp.serialx.Serializer;
1415
import org.ugp.serialx.converters.DataParser;
@@ -113,9 +114,22 @@ public Object parse(ParserRegistry myHomeRegistry, String str, Object... compile
113114
scope = getPreferredSerializer();
114115
}
115116

116-
compilerArgs = compilerArgs.clone();
117-
compilerArgs[0] = false; //No extra formating...
118-
return str.isEmpty() ? scope : scope.LoadFrom(new StringReader(str), compilerArgs);
117+
if (compilerArgs.length > 0)
118+
{
119+
compilerArgs = compilerArgs.clone();
120+
compilerArgs[0] = false; //No extra formating...
121+
}
122+
123+
if (str.isEmpty())
124+
return scope;
125+
try
126+
{
127+
return scope.LoadFrom(new StringReader(str), compilerArgs);
128+
}
129+
catch (IOException e)
130+
{
131+
LogProvider.instance.logErr("Unable to parse the scope because:", e); // Should not occur...
132+
}
119133
}
120134
}
121135
return CONTINUE;

SerialX-operators/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>org.ugp.serialx</groupId>
1212
<artifactId>serialx-operators</artifactId>
13-
<version>1.3.8</version>
13+
<version>1.3.9-SNAPSHOT</version>
1414

1515
<name>SerialX operators</name>
1616
<description>This modul contains basic operators contained in almost every programing language...</description>

0 commit comments

Comments
 (0)