Skip to content

Commit c1c47ae

Browse files
authored
Merge pull request #486 from jawkio/476-implement-awk-strnum-semantics-for-input-derived-numeric-strings
Implement AWK strnum semantics for input-derived numeric strings
2 parents 432e75d + 09908ba commit c1c47ae

16 files changed

Lines changed: 786 additions & 417 deletions

src/jmh/java/io/jawk/jrt/JRTCompare2Benchmark.java

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ public class JRTCompare2Benchmark {
5757
private Object stringLeft;
5858
private Object stringRightEqual;
5959
private Object stringRightGreater;
60-
private Object numericStringLeft;
61-
private Object numericStringRightEqual;
62-
private Object numericStringRightGreater;
60+
private Object plainNumericStringLeft;
61+
private Object plainNumericStringRightEqual;
62+
private Object plainNumericStringRightGreater;
6363
private Object nonNumericString;
64+
private Object strNumLeft;
65+
private Object strNumRightEqual;
66+
private Object strNumRightGreater;
67+
private Object nonNumericStrNum;
6468

6569
/**
6670
* Initializes benchmark operands as mutable state fields so the benchmark body
@@ -78,10 +82,14 @@ public void setup() {
7882
this.stringLeft = "alpha";
7983
this.stringRightEqual = "alpha";
8084
this.stringRightGreater = "bravo";
81-
this.numericStringLeft = "123";
82-
this.numericStringRightEqual = "123.0";
83-
this.numericStringRightGreater = "456";
85+
this.plainNumericStringLeft = "123";
86+
this.plainNumericStringRightEqual = "123.0";
87+
this.plainNumericStringRightGreater = "456";
8488
this.nonNumericString = "2x";
89+
this.strNumLeft = new StrNum("123");
90+
this.strNumRightEqual = new StrNum("123.0");
91+
this.strNumRightGreater = new StrNum("456");
92+
this.nonNumericStrNum = new StrNum("2x");
8593
}
8694

8795
/**
@@ -155,33 +163,66 @@ public boolean stringLessThan() {
155163
}
156164

157165
/**
158-
* Measures equality for two numeric string operands.
166+
* Measures equality for two plain numeric-looking {@link String} operands.
159167
*
160168
* @return the comparison result
161169
*/
162170
@Benchmark
163-
public boolean numericStringEquals() {
164-
return JRT.compare2(this.numericStringLeft, this.numericStringRightEqual, 0);
171+
public boolean plainNumericStringEquals() {
172+
return JRT.compare2(this.plainNumericStringLeft, this.plainNumericStringRightEqual, 0);
165173
}
166174

167175
/**
168-
* Measures less-than comparison for two numeric string operands.
176+
* Measures less-than comparison for two plain numeric-looking {@link String}
177+
* operands.
169178
*
170179
* @return the comparison result
171180
*/
172181
@Benchmark
173-
public boolean numericStringLessThan() {
174-
return JRT.compare2(this.numericStringLeft, this.numericStringRightGreater, -1);
182+
public boolean plainNumericStringLessThan() {
183+
return JRT.compare2(this.plainNumericStringLeft, this.plainNumericStringRightGreater, -1);
175184
}
176185

177186
/**
178-
* Measures equality for a boxed {@link Long} and a numeric string operand.
187+
* Measures equality for a boxed {@link Long} and a plain numeric-looking
188+
* {@link String} operand.
179189
*
180190
* @return the comparison result
181191
*/
182192
@Benchmark
183-
public boolean mixedLongNumericStringEquals() {
184-
return JRT.compare2(this.longLeft, this.numericStringRightEqual, 0);
193+
public boolean mixedLongPlainNumericStringEquals() {
194+
return JRT.compare2(this.longLeft, this.plainNumericStringRightEqual, 0);
195+
}
196+
197+
/**
198+
* Measures equality for two input-derived numeric string operands.
199+
*
200+
* @return the comparison result
201+
*/
202+
@Benchmark
203+
public boolean strNumEquals() {
204+
return JRT.compare2(this.strNumLeft, this.strNumRightEqual, 0);
205+
}
206+
207+
/**
208+
* Measures less-than comparison for two input-derived numeric string operands.
209+
*
210+
* @return the comparison result
211+
*/
212+
@Benchmark
213+
public boolean strNumLessThan() {
214+
return JRT.compare2(this.strNumLeft, this.strNumRightGreater, -1);
215+
}
216+
217+
/**
218+
* Measures equality for a boxed {@link Long} and an input-derived numeric
219+
* string operand.
220+
*
221+
* @return the comparison result
222+
*/
223+
@Benchmark
224+
public boolean mixedLongStrNumEquals() {
225+
return JRT.compare2(this.longLeft, this.strNumRightEqual, 0);
185226
}
186227

187228
/**
@@ -194,4 +235,15 @@ public boolean mixedLongNumericStringEquals() {
194235
public boolean mixedLongNonNumericStringLessThan() {
195236
return JRT.compare2(this.longLeft, this.nonNumericString, -1);
196237
}
238+
239+
/**
240+
* Measures fallback string comparison for a numeric operand and a nonnumeric
241+
* input-derived string.
242+
*
243+
* @return the comparison result
244+
*/
245+
@Benchmark
246+
public boolean mixedLongNonNumericStrNumLessThan() {
247+
return JRT.compare2(this.longLeft, this.nonNumericStrNum, -1);
248+
}
197249
}

src/jmh/java/io/jawk/jrt/JRTHotPathBenchmark.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public class JRTHotPathBenchmark {
6363
private Object zeroLong;
6464
private Object zeroDouble;
6565
private Object zeroString;
66+
private Object zeroStrNum;
67+
private Object nonZeroStrNum;
68+
private Object nonNumericStrNum;
6669
private Object uninitialized;
6770
private double integralDouble;
6871
private double fractionalDouble;
@@ -87,6 +90,9 @@ public void setup() {
8790
this.zeroLong = Long.valueOf(0L);
8891
this.zeroDouble = Double.valueOf(0.0D);
8992
this.zeroString = "0";
93+
this.zeroStrNum = new StrNum("0");
94+
this.nonZeroStrNum = new StrNum("123");
95+
this.nonNumericStrNum = new StrNum("2x");
9096
this.uninitialized = new UninitializedObject();
9197
this.integralDouble = 123456789D;
9298
this.fractionalDouble = 123456.75D;
@@ -166,6 +172,28 @@ public double toDoubleNonNumericString() {
166172
return JRT.toDouble(this.nonNumericString);
167173
}
168174

175+
/**
176+
* Measures {@link JRT#toDouble(Object)} for an input-derived fully numeric
177+
* string.
178+
*
179+
* @return converted value
180+
*/
181+
@Benchmark
182+
public double toDoubleStrNumNumeric() {
183+
return JRT.toDouble(this.nonZeroStrNum);
184+
}
185+
186+
/**
187+
* Measures {@link JRT#toDouble(Object)} for an input-derived numeric-prefix
188+
* string.
189+
*
190+
* @return converted value
191+
*/
192+
@Benchmark
193+
public double toDoubleStrNumNumericPrefix() {
194+
return JRT.toDouble(this.nonNumericStrNum);
195+
}
196+
169197
/**
170198
* Measures {@link JRT#toDouble(Object)} for an empty string.
171199
*
@@ -336,6 +364,37 @@ public boolean toBooleanStringZero() {
336364
return this.jrt.toBoolean(this.zeroString);
337365
}
338366

367+
/**
368+
* Measures {@link JRT#toBoolean(Object)} for an input-derived zero string.
369+
*
370+
* @return converted value
371+
*/
372+
@Benchmark
373+
public boolean toBooleanStrNumZero() {
374+
return this.jrt.toBoolean(this.zeroStrNum);
375+
}
376+
377+
/**
378+
* Measures {@link JRT#toBoolean(Object)} for an input-derived non-zero string.
379+
*
380+
* @return converted value
381+
*/
382+
@Benchmark
383+
public boolean toBooleanStrNumNonZero() {
384+
return this.jrt.toBoolean(this.nonZeroStrNum);
385+
}
386+
387+
/**
388+
* Measures {@link JRT#toBoolean(Object)} for an input-derived nonnumeric
389+
* string.
390+
*
391+
* @return converted value
392+
*/
393+
@Benchmark
394+
public boolean toBooleanStrNumNonNumeric() {
395+
return this.jrt.toBoolean(this.nonNumericStrNum);
396+
}
397+
339398
/**
340399
* Measures {@link JRT#toBoolean(Object)} for an uninitialized runtime value.
341400
*

src/main/java/io/jawk/Cli.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,7 @@ private static void addVariable(AwkSettings settings, String keyValue) {
382382
}
383383
String name = m.group(1);
384384
String valueString = m.group(2);
385-
Object value;
386-
try {
387-
value = Integer.parseInt(valueString);
388-
} catch (NumberFormatException nfe) {
389-
try {
390-
value = Double.parseDouble(valueString);
391-
} catch (NumberFormatException nfe2) {
392-
value = valueString;
393-
}
394-
}
395-
settings.putVariable(name, value);
385+
settings.putVariable(name, valueString);
396386
}
397387

398388
/**

0 commit comments

Comments
 (0)