Skip to content

Commit bea917d

Browse files
fixing bugs and doing small optim
1 parent dce815d commit bea917d

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ public static <K, V> GenericScope<K, V> newBidirectional(Map<K, V> variablesMap,
10241024
*
10251025
* @since 1.3.5
10261026
*/
1027-
public static <K, V> GenericScope<K, V> intoBidirectional(GenericScope<K, V> scopeToMakeBidirectional, Map<K, V> variablesMap, List<V> values)
1027+
public static <K, V, S extends GenericScope<K, V>> S intoBidirectional(S scopeToMakeBidirectional, Map<K, V> variablesMap, List<V> values)
10281028
{
10291029
return intoBidirectional(scopeToMakeBidirectional, variablesMap, values, null);
10301030
}
@@ -1039,7 +1039,7 @@ public static <K, V> GenericScope<K, V> intoBidirectional(GenericScope<K, V> sco
10391039
*
10401040
* @since 1.3.5
10411041
*/
1042-
public static <K, V> GenericScope<K, V> intoBidirectional(GenericScope<K, V> scopeToMakeBidirectional, Map<K, V> variablesMap, List<V> values, GenericScope<?, ?> parent)
1042+
public static <K, V, S extends GenericScope<K, V>> S intoBidirectional(S scopeToMakeBidirectional, Map<K, V> variablesMap, List<V> values, GenericScope<?, ?> parent)
10431043
{
10441044
scopeToMakeBidirectional.variables = variablesMap;
10451045
scopeToMakeBidirectional.values = values;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ public <T> T toObject(Class<T> objClass) throws Exception
164164
return toObject(objClass, getProtocols());
165165
}
166166

167+
@SuppressWarnings("unchecked")
168+
@Override
169+
public <V> GenericScope<String, V> transform(Function<Object, V> trans, boolean includeSubScopes)
170+
{
171+
Serializer transformed = (Serializer) super.transform(trans, includeSubScopes);
172+
transformed.parsers = getParsers();
173+
transformed.protocols = getProtocols();
174+
return (GenericScope<String, V>) transformed;
175+
}
176+
167177
/**
168178
* @see Serializer#into(Object, Serializer, String...)
169179
*/

SerialX-core/src/main/java/org/ugp/serialx/converters/DataParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface DataParser
4545

4646
/**
4747
* @param myHomeRegistry | Registry where this parser is registered provided by {@link DataParser#parseObj(Registry, String, boolean, Class[], Object...)} otherwise it demands on implementation (it should not be null)!
48-
* @param str | Source string!
48+
* @param str | Source string, preferably trimed!
4949
* @param args | Some additional args. This can be anything and it demands on implementation of DataParser. Default SerialX API implementation will provide one optional argument with {@link Scope} that value was loaded from!
5050
*
5151
* @return Object that was parsed from obtained string. Special return types are {@link DataParser#VOID} and {@link DataParser#CONTINUE}. Continue will ignore this parser and jump to another one in registry.

SerialX-core/src/main/java/org/ugp/serialx/converters/NumberConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
112112
int len;
113113
if ((len = arg.length()) > 0)
114114
{
115-
char ch0 = arg.charAt(0);
116-
if (ch0 == '+' || ch0 == '-' || ch0 == '.' || (ch0 >= '0' && ch0 <= '9'))
115+
char ch0;
116+
if ((ch0 = arg.charAt(0)) >= '0' && ch0 <= '9' || (ch0 == '+' || ch0 == '-' || ch0 == '.') && len > 1 && arg.charAt(1) > '*')
117117
{
118118
Number num;
119119
if ((num = numberOf(arg, ch0, --len, 10, 0)) != null)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
6969
op0Index--;
7070
}
7171

72-
String vars[] = splitValues(arg, op0Index, 0, 1, new char[] {'?'}, '=', ':'), valStr;
72+
String vars[] = splitValues(arg, op0Index, 0, 1, new char[] {'?', '<', '>', '!'}, '=', ':'), valStr;
7373

7474
Object val = null;
7575
int iVal = vars.length-1;

SerialX-operators/src/main/java/org/ugp/serialx/converters/operators/ArithmeticOperators.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
4242
int len;
4343
if ((len = str.length()) > 2 && isExpression(str, len, operators))
4444
{
45-
for (int i; (i = indexOfNotInObj(str, 0, len, len, true, '+', '-')+1) < len && isOneOf(str.charAt(i), '+', '-'); ) // Handle duplicates of [+-]{2}
46-
str = fastReplace(fastReplace(fastReplace(fastReplace(str, "-+", "-"), "+-", "-"), "--", "+"), "++", "+");
45+
for (int i = 0; i < len; ) // Format duplicates of [+-]{2}
46+
if (isOneOf(str.charAt(i++), '+', '-') && i < len && isOneOf(str.charAt(i), '+', '-'))
47+
{
48+
len = (str = fastReplace(fastReplace(fastReplace(fastReplace(str, "-+", "-"), "+-", "-"), "--", "+"), "++", "+")).length();
49+
i--;
50+
}
4751

48-
List<?>[] terms = getAndParseTerms(str, myHomeRegistry, args, getClass(), operators);
52+
List<?>[] terms = getAndParseTerms(str, len, myHomeRegistry, args, getClass(), operators);
4953
ArrayList<Object> cofs = (ArrayList<Object>) terms[0];
5054
if (cofs.size() <= 1)
5155
return cofs.get(0);
@@ -383,6 +387,7 @@ public static Object pow(Object cof, Object cof2, int sign)
383387

384388
/**
385389
* @param str | String to split!
390+
* @param to | Ending index of parsing, exclusive. Everything from this index to the end will be ignored (should be str.length())!
386391
* @param registryForParsers | Registry to use for parsing operands!
387392
* @param argsForParsers | Arguments for the parse method!
388393
* @param classToIgnore | Parser to ignore (should be class of the caller if possible)!
@@ -393,14 +398,14 @@ public static Object pow(Object cof, Object cof2, int sign)
393398
* @since 1.3.7 (originally getTerms since 1.3.0)
394399
*/
395400
@SuppressWarnings("unchecked")
396-
public static List<?>[] getAndParseTerms(String str, ParserRegistry registryForParsers, Object[] argsForParsers, Class<? extends DataParser> classToIgnore, char... oprs)
401+
public static List<?>[] getAndParseTerms(String str, int to, ParserRegistry registryForParsers, Object[] argsForParsers, Class<? extends DataParser> classToIgnore, char... oprs)
397402
{
398403
List<Object>[] ret = new List[] {new ArrayList<Object>(), new LinkedList<String>()}; //cofs, ops
399404

400-
int i = 0, startIndex = 0, type = 0, len = str.length();
401-
for (; i < len && isOneOf(str.charAt(i), oprs); i++); //in case of start cof sign
405+
int i = 0, startIndex = 0, type = 0;
406+
for (; i < to && isOneOf(str.charAt(i), oprs); i++); //in case of start cof sign
402407

403-
for (int quote = 0, brackets = 0, lastType = type; i < len; i++)
408+
for (int quote = 0, brackets = 0, lastType = type; i < to; i++)
404409
{
405410
char ch = str.charAt(i);
406411
if (ch == '\"')
@@ -426,7 +431,7 @@ else if ((ch | ' ') == '}')
426431
lastType = type;
427432
}
428433

429-
String s = str.substring(startIndex, len).trim();
434+
String s = str.substring(startIndex, to).trim();
430435
if (!s.isEmpty())
431436
ret[type].add(type == 0 ? registryForParsers.parse(s, false, classToIgnore, argsForParsers) : s);
432437
// System.err.println(ret[0] + "\n" + ret[1] + "\n");

SerialX-operators/src/main/java/org/ugp/serialx/converters/operators/NegationOperator.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@ public class NegationOperator implements DataParser
1616
@Override
1717
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
1818
{
19-
int ch, len = str.length();
20-
if (len > 0 && ((ch = str.charAt(0)) == '!' || ch == '-'))
19+
int len, type;
20+
if ((len = str.length()) < 2)
21+
return CONTINUE;
22+
if ((type = str.charAt(0)) == '!' || type == '-')
2123
{
2224
int negCount = 1;
23-
for (int i = negCount; i < len; i++)
24-
if ((ch = str.charAt(i)) == '!' || ch == '-')
25-
negCount++;
26-
else
27-
break;
25+
for (int ch; negCount < len && ((ch = str.charAt(negCount)) == '!' || ch == '-'); negCount++);
2826

29-
Object obj = myHomeRegistry.parse(str.substring(negCount), args);
27+
Object obj = myHomeRegistry.parse(str.substring(negCount).trim(), false, getClass(), args);
3028
if (negCount % 2 == 0)
3129
return obj;
32-
Object neg = ch == '!' ? logicalNotOperator(obj) : notOperator(obj);
30+
31+
Object neg = type == '!' ? logicalNotOperator(obj) : notOperator(obj);
3332
if (obj == neg && !(obj instanceof Number && ((Number) obj).intValue() == 0))
3433
LogProvider.instance.logErr("Unable to nagete \"" + obj + "\" because object of \"" + obj.getClass().getName() + "\" cant be negated!", null);
3534
return neg;

0 commit comments

Comments
 (0)