Skip to content

Commit

Permalink
Use bitwise operations
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin authored Jan 19, 2024
1 parent 020eebb commit c7331b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/main/java/net/datafaker/providers/base/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,23 @@ public long numberBetween(long min, long max) {
* @param strict whether or not the generated value should have exactly <code>numberOfDigits</code>
*/
public long randomNumber(int numberOfDigits, boolean strict) {
long max = pow(10, numberOfDigits);
if (numberOfDigits <= 0) {
return faker.random().nextInt(1);
}
long min = pow(10, numberOfDigits - 1);
if (strict) {
long min = max / 10;
long max = min * 10;
return faker.random().nextLong(max - min) + min;
}

return faker.random().nextLong(max);
return faker.random().nextLong(min * 10);
}

private long pow(long value, int d) {
if (d == 0) return 1;
if (d == 1) return value;
if (d % 2 == 0) {
long pow = pow(value, d / 2);
if ((d & 1) == 0) {
long pow = pow(value, d >> 1);
return pow * pow;
} else {
return value * pow(value, d - 1);
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,13 @@ public String csv(int limit, String... columnExpressions) {
* Generates csv based on input.
*/
public String csv(String delimiter, char quote, boolean withHeader, int limit, String... columnExpressions) {
if (columnExpressions.length % 2 != 0) {
if ((columnExpressions.length & 1) == 1) {
throw new IllegalArgumentException("Total number of column names and column values should be even");
}
Field<String, String>[] fields = new Field[columnExpressions.length / 2];
Field<String, String>[] fields = new Field[columnExpressions.length >> 1];
for (int i = 0; i < columnExpressions.length; i += 2) {
final int index = i;
fields[i / 2] = Field.field(columnExpressions[index], () -> columnExpressions[index + 1]);
fields[i >> 1] = Field.field(columnExpressions[index], () -> columnExpressions[index + 1]);
}
Schema<String, String> schema = Schema.of(fields);
return CsvTransformer.<String>builder().separator(delimiter).quote(quote).header(withHeader)
Expand All @@ -496,7 +496,7 @@ public String csv(String delimiter, char quote, boolean withHeader, int limit, S
* Generates json based on input.
*/
public String json(String... fieldExpressions) {
if (fieldExpressions.length % 2 != 0) {
if ((fieldExpressions.length & 1) == 1) {
throw new IllegalArgumentException("Total number of field names and field values should be even");
}

Expand Down Expand Up @@ -559,7 +559,7 @@ protected String resolveExpression(String expression, Object current, ProviderRe
for (int i = 0; i < expressions.length; i++) {
// odd are expressions, even are not expressions, just strings
final String expr = expressions[i];
if (i % 2 == 0) {
if ((i & 1) == 0) {
if (!expr.isEmpty()) {
result.append(expr);
}
Expand Down Expand Up @@ -607,7 +607,7 @@ private String[] splitArguments(String arguments) {
cnt++;
i++;
}
if (cnt % 2 == 1) {
if ((cnt & 1) == 1) {
result.add(arguments.substring(start, i - 1).replace("''", "'"));
argsStarted = false;
}
Expand All @@ -627,20 +627,21 @@ private String[] splitExpressions(String expression, int cnt, int length) {
if (result != null) {
return result;
}
List<String> list = new ArrayList<>(2 * cnt + 1);
List<String> list = new ArrayList<>((cnt << 1) + 1);
boolean isExpression = false;
int start = 0;
int quoteCnt = 0;
for (int i = 0; i < length; i++) {
final char c = expression.charAt(i);
if (isExpression) {
if (expression.charAt(i) == '}' && quoteCnt % 2 == 0) {
if (c == '}' && (quoteCnt & 1) == 0) {
list.add(expression.substring(start, i));
start = i + 1;
isExpression = false;
} else if (expression.charAt(i) == '\'') {
} else if (c == '\'') {
quoteCnt++;
}
} else if (i < length - 2 && expression.charAt(i) == '#' && expression.charAt(i + 1) == '{') {
} else if (i < length - 2 && c == '#' && expression.charAt(i + 1) == '{') {
list.add(expression.substring(start, i));
isExpression = true;
start = i + 2;
Expand Down

0 comments on commit c7331b2

Please sign in to comment.