Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/com/googlecode/yatspec/parsing/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@

public class Text {
private static final Regex wordDelimiter = Regex.regex(Strings.toString(Text.class.getResourceAsStream("wordDelimiter.regex")));
private static final Regex wordNumberDelimiter = Regex.regex("([a-z])([0-9])"); // detect boundaries where letters meet numbers
private static final Pattern spaceRemover = Pattern.compile("(?<!^)[\\t\\x0B\\f ]+"); // don't replace new lines
private static final Regex quotedStrings = regex("\"[^\"]+\"");

private static final Callable1<MatchResult, CharSequence> wordNumberDelimiterReplacer = new Callable1<MatchResult, CharSequence>() {
public String call(MatchResult matchResult) {
// for example "d4" -> "d 4"
return matchResult.group(1) + " " + matchResult.group(2);
}
};

private static final Callable1<MatchResult, CharSequence> wordDelimiterReplacer = new Callable1<MatchResult, CharSequence>() {
public String call(MatchResult matchResult) {
// " $1 $2"
Expand All @@ -27,7 +35,8 @@ private static String filterNull(String value) {

private static final Callable1<CharSequence, CharSequence> wordifier = new Callable1<CharSequence, CharSequence>() {
public CharSequence call(CharSequence text) {
return wordDelimiter.findMatches(text).replace(wordDelimiterReplacer);
String wordified = wordDelimiter.findMatches(text).replace(wordDelimiterReplacer);
return wordNumberDelimiter.findMatches(wordified).replace(wordNumberDelimiterReplacer);
}
};

Expand Down
6 changes: 6 additions & 0 deletions test/com/googlecode/yatspec/parsing/TextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import static org.hamcrest.Matchers.is;

public class TextTest {

@Test
public void numberAfterWordHasASpace() {
assertThat(Text.wordify("word404NewWord"), is("Word 404 new word"));
}

@Test
public void insertsASingleSpacesBetweenCapitalsAndTrims() {
assertThat(Text.wordify("replaceAWithB;"), is("Replace a with b"));
Expand Down