Skip to content

Commit

Permalink
Merge pull request #30 from curious-odd-man/dev
Browse files Browse the repository at this point in the history
Release 1.1
  • Loading branch information
curious-odd-man committed Apr 25, 2020
2 parents a395511 + b173f42 commit a3a9b63
Show file tree
Hide file tree
Showing 29 changed files with 803 additions and 240 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ script: "mvn clean test"

after_success:
- bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
- cat performance.txt
65 changes: 38 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,44 @@ Build status:

## Try it now!!!

Note: latest version in maven is imported there. See supported syntax below for details.
Note: latest RELEASE version is imported there. See supported syntax below for details.

Follow the link to Online IDE with already created simple project: [JDoodle](https://www.jdoodle.com/a/1Ni2)
Follow the link to Online IDE with already created simple project: [JDoodle](https://www.jdoodle.com/a/1NCw)

Enter your pattern and see the results.


## Usage

Maven dependency (latest available version):
### Maven dependency

#### latest RELEASE:
```
<dependency>
<groupId>com.github.curious-odd-man</groupId>
<artifactId>rgxgen</artifactId>
<version>1.1</version>
</dependency>
```
#### latest SNAPSHOT:
```
<repositories>
<repository>
<id>snapshots-repository</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
// ....
<dependency>
<groupId>com.github.curious-odd-man</groupId>
<artifactId>rgxgen</artifactId>
<version>0.9</version>
<version>1.1-SNAPSHOT</version>
</dependency>
```

Code:
### Code:
```
RgxGen rgxGen = new RgxGen("[^0-9]*[12]?[0-9]{1,2}[^0-9]*"); // Create generator
String s = rgxGen.generate(); // Generate new random value
Expand All @@ -42,26 +61,7 @@ StringIterator uniqueStrings = rgxGen.iterateUnique(); // Iterate
## Supported syntax

<details>
<summary><b>Latest version in maven</b></summary>

```
. - any symbol
? - one or zero occurrences
+ - one or more occurrences
* - zero or more occurrences
\d - a digit. Equivalent to [0-9]
{2} and {1,2} - repeatitions. NOTE {1,} not supported yet
[] - single character from ones that are inside brackets. [a-zA-Z] (dash) also supported
() - to group multiple characters for the repetitions
(a|b) - alternatives
escape character \ - to escape special characters (use \\ to generate single \ character)
```

Any other character are treated as simple characters and are generated as is, thought allowed to escape them.
</details>

<details>
<summary><b>Latest dev version</b></summary>
<summary><b>Latest RELEASE</b></summary>

| Pattern | Description |
| ---------: |-------------|
Expand All @@ -71,8 +71,8 @@ Any other character are treated as simple characters and are generated as is, th
| `*` | Zero or more occurrences |
| `\d` | A digit. Equivalent to `[0-9]` |
| `\D` | Not a digit. Equivalent to `[^0-9]` |
| `\s` | Space, tab or newline |
| `\S` | Anything, but space, tab or newline |
| `\s` | Carriage Return, Space, Tab, Newline, Vertical Tab, Form Feed |
| `\S` | Anything, but Carriage Return, Space, Tab, Newline, Vertical Tab, Form Feed |
| `\w` | Any word character. Equivalent to `[a-zA-Z0-9_]` |
| `\W` | Anything but a word character. Equivalent to `[^a-zA-Z0-9_]` |
| `\i` | Places same value as capture group with index `i`. `i` is any integer number. |
Expand All @@ -88,6 +88,17 @@ Any other character are treated as simple characters and are generated as is, th

Any other character are treated as simple characters and are generated as is, thought allowed to escape them.

Fixed issue [#23.](https://github.com/curious-odd-man/RgxGen/issues/23) Metasequences inside square brackets.
Fixed issue [#18.](https://github.com/curious-odd-man/RgxGen/issues/18). Reproducible random sequences.
```java
RgxGen rgxGen = new RgxGen("[^0-9]*[12]?[0-9]{1,2}[^0-9]*"); // Create generator
Random rnd = new Random(1234)
String s = rgxGen.generate(rnd); // Generate first value
String s1 = rgxGen.generate(rnd); // Generate second value
String s2 = rgxGen.generate(rnd); // Generate third value
// On each launch s, s1 and s2 will be the same
```

</details>

## Limitations
Expand Down
3 changes: 3 additions & 0 deletions performance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Benchmark Mode Cnt Score Error Units
PerformanceTests.generateTest avgt 5 88.374 ± 4.447 us/op
PerformanceTests.generateUniqueTest avgt 5 223167.433 ± 31382.610 us/op
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.curious-odd-man</groupId>
<artifactId>rgxgen</artifactId>
<version>1.0</version>
<version>1.1</version>

<packaging>jar</packaging>

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/github/curiousoddman/rgxgen/RgxGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.curiousoddman.rgxgen.parsing.dflt.DefaultTreeBuilder;

import java.math.BigInteger;
import java.util.Random;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -95,4 +96,16 @@ public String generate() {
aNode.visit(gv);
return gv.getString();
}

/**
* Generate random string from the pattern.
*
* @param random random to use for the generation.
* @return generated string.
*/
public String generate(Random random) {
GenerationVisitor gv = new GenerationVisitor(random);
aNode.visit(gv);
return gv.getString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,15 @@
import org.slf4j.LoggerFactory;

public class Group implements Node {

private static final Logger LOGGER = LoggerFactory.getLogger(Group.class);

private final Node aNode;
private final int aGroupIndex;
private final boolean aIsCapturable;

public Group(Node node) {
this(-1, node);
}
private final Node aNode;
private final int aGroupIndex;

public Group(int index, Node node) {
LOGGER.trace("Crating idx = '{}' from '{}'", index, node);
aNode = node;
aGroupIndex = index;
aIsCapturable = index != -1;
}

public boolean isCapture() {
return aIsCapturable;
}

public int getIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public enum TYPE {
* Range of symbols
*/
public static class SymbolRange {
public static final SymbolRange SMALL_LETTERS = new SymbolSet.SymbolRange('a', 'z');
public static final SymbolRange CAPITAL_LETTERS = new SymbolSet.SymbolRange('A', 'Z');
public static final SymbolRange DIGITS = new SymbolSet.SymbolRange('0', '9');

private final int aFrom;
private final int aTo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,32 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Random;

public class GenerationVisitor implements NodeVisitor {
private final StringBuilder aStringBuilder = new StringBuilder();
private final Map<Integer, String> aGroupValues = new HashMap<>();
private final Random aRandom;

public GenerationVisitor() {
this(new Random());
}

public GenerationVisitor(Random random) {
aRandom = random;
}

@Override
public void visit(SymbolSet node) {
String[] allSymbols = node.getSymbols();
int idx = ThreadLocalRandom.current()
.nextInt(allSymbols.length);
int idx = aRandom.nextInt(allSymbols.length);
aStringBuilder.append(allSymbols[idx]);
}

@Override
public void visit(Choice node) {
Node[] nodes = node.getNodes();
int idx = ThreadLocalRandom.current()
.nextInt(nodes.length);
int idx = aRandom.nextInt(nodes.length);
nodes[idx].visit(this);
}

Expand All @@ -53,8 +60,7 @@ public void visit(Repeat node) {
int max = node.getMax() == -1 ? 100 : node.getMax();
int repeat = node.getMin() >= max ?
node.getMin() :
ThreadLocalRandom.current()
.nextInt(node.getMin(), max + 1);
node.getMin() + aRandom.nextInt(max + 1 - node.getMin());

for (long i = 0; i < repeat; ++i) {
node.getNode()
Expand All @@ -73,11 +79,11 @@ public void visit(Sequence node) {
public void visit(NotSymbol notSymbol) {
String value = notSymbol.getSubPattern()
.pattern();
String result = Util.randomString(value);
String result = Util.randomString(aRandom, value);
while (!notSymbol.getSubPattern()
.matcher(value)
.matches()) {
result = Util.randomString(result);
result = Util.randomString(aRandom, result);
}

aStringBuilder.append(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void visit(GroupRef groupRef) {
|| !(aParentNode instanceof Repeat)) {
// Do nothing. It does not add new unique values.
} else {
// When repeated multiple times - it adds as much unique values as it is repeated. So we should add 1 (it will be used in Repeat for calcuation).
// When repeated multiple times - it adds as much unique values as it is repeated. So we should add 1 (it will be used in Repeat for calculation).
// E.g. (a|b)\1{2,3} - captured value of group is repeated either 2 or 3 times - it gives 2 unique values.
aCount = aCount.add(BigInteger.ONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class ChoiceIterator extends StringIterator {
private final StringIterator[] aIterators;

private int aCurrentIteratorIndex = 0;
private int aCurrentIteratorIndex;

public ChoiceIterator(StringIterator[] iterators) {
aIterators = iterators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class PermutationsIterator extends StringIterator {
private final StringIterator[] aIterators;
Expand All @@ -44,12 +42,6 @@ public PermutationsIterator(List<Supplier<StringIterator>> iteratorsSuppliers) {
aGeneratedParts[0] = null;
}

public PermutationsIterator(int length, String[] values) {
this(IntStream.range(0, length)
.mapToObj(i -> ((Supplier<StringIterator>) () -> new ArrayIterator(values)))
.collect(Collectors.toList()));
}

@Override
public boolean hasNext() {
return aGeneratedParts[0] == null || Arrays.stream(aIterators)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class ReferenceIterator extends StringIterator {

private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceIterator.class);

private StringIterator aOther = null;
private StringIterator aOther;
private boolean hasNext = true;
private String aLast = null;
private String aLast;

public void setOther(StringIterator other) {
aOther = other;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String next() {

/**
* This method returns correct value only on top level iterator.
* For other iterators 2 steps are requied - next() and then current().
* For other iterators 2 steps are required - next() and then current().
*
* @return next String.
*/
Expand Down
Loading

0 comments on commit a3a9b63

Please sign in to comment.