/** * Sets the field delimiter string. * * @param delim the delimiter string to use * @return this, to enable chaining */ public StrTokenizer setDelimiterString(final String delim) { return setDelimiterMatcher(StrMatcher.stringMatcher(delim)); }
/** * Sets the variable prefix to use. * <p> * The variable prefix is the character or characters that identify the * start of a variable. This method allows a string prefix to be easily set. * * @param prefix the prefix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the prefix is null */ public StrSubstitutor setVariablePrefix(final String prefix) { if (prefix == null) { throw new IllegalArgumentException("Variable prefix must not be null!"); } return setVariablePrefixMatcher(StrMatcher.stringMatcher(prefix)); }
/** * Sets the variable suffix to use. * <p> * The variable suffix is the character or characters that identify the * end of a variable. This method allows a string suffix to be easily set. * * @param suffix the suffix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the suffix is null */ public StrSubstitutor setVariableSuffix(final String suffix) { if (suffix == null) { throw new IllegalArgumentException("Variable suffix must not be null!"); } return setVariableSuffixMatcher(StrMatcher.stringMatcher(suffix)); }
/** * Sets the variable default value delimiter to use. * <p> * The variable default value delimiter is the character or characters that delimit the * variable name and the variable default value. This method allows a string * variable default value delimiter to be easily set. * <p> * If the <code>valueDelimiter</code> is null or empty string, then the variable default * value resolution becomes disabled. * * @param valueDelimiter the variable default value delimiter string to use, may be null or empty * @return this, to enable chaining * @since 3.2 */ public StrSubstitutor setValueDelimiter(final String valueDelimiter) { if (StringUtils.isEmpty(valueDelimiter)) { setValueDelimiterMatcher(null); return this; } return setValueDelimiterMatcher(StrMatcher.stringMatcher(valueDelimiter)); }
@Test public void testStringMatcher_String() { final StrMatcher matcher = StrMatcher.stringMatcher("bc"); assertEquals(0, matcher.isMatch(BUFFER2, 0)); assertEquals(2, matcher.isMatch(BUFFER2, 1)); assertEquals(0, matcher.isMatch(BUFFER2, 2)); assertEquals(0, matcher.isMatch(BUFFER2, 3)); assertEquals(0, matcher.isMatch(BUFFER2, 4)); assertEquals(0, matcher.isMatch(BUFFER2, 5)); assertSame(StrMatcher.noneMatcher(), StrMatcher.stringMatcher("")); assertSame(StrMatcher.noneMatcher(), StrMatcher.stringMatcher(null)); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryCount() { StrBuilder sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1); assertEquals("-x--y-", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 0); assertEquals("aaxaaaayaa", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 1); assertEquals("-xaaaayaa", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 2); assertEquals("-x-aayaa", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 3); assertEquals("-x--yaa", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 4); assertEquals("-x--y-", sb.toString()); sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 5); assertEquals("-x--y-", sb.toString()); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryReplace() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0, sb.length(), -1); assertEquals("abcbccba", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(), -1); assertEquals("ab-c-a", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb.length(), -1); assertEquals("ab+++c+++a", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(), -1); assertEquals("abca", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), null, 0, sb.length(), -1); assertEquals("abca", sb.toString()); }
@Test public void testMatcherIndices() { // remember that the API contract is tight for the isMatch() method // all the onus is on the caller, so invalid inputs are not // the concern of StrMatcher, and are not bugs final StrMatcher matcher = StrMatcher.stringMatcher("bc"); assertEquals(2, matcher.isMatch(BUFFER2, 1, 1, BUFFER2.length)); assertEquals(2, matcher.isMatch(BUFFER2, 1, 0, 3)); assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, 2)); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() { StrBuilder sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 0, -1); assertEquals("aaxaaaayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 2, -1); assertEquals("-xaaaayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 3, -1); assertEquals("-xaaaayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 4, -1); assertEquals("-xaaaayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 5, -1); assertEquals("-x-aayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 6, -1); assertEquals("-x-aayaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 7, -1); assertEquals("-x--yaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 8, -1); assertEquals("-x--yaa", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 9, -1); assertEquals("-x--yaa", sb.toString());
@Test public void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() { StrBuilder sb = new StrBuilder("aaxaaaayaa"); sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, sb.length(), -1); assertEquals("-x--y-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 1, sb.length(), -1); assertEquals("aax--y-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, sb.length(), -1); assertEquals("aax--y-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 3, sb.length(), -1); assertEquals("aax--y-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 4, sb.length(), -1); assertEquals("aaxa-ay-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 5, sb.length(), -1); assertEquals("aaxaa-y-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 6, sb.length(), -1); assertEquals("aaxaaaay-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 7, sb.length(), -1); assertEquals("aaxaaaay-", sb.toString()); sb.replace(StrMatcher.stringMatcher("aa"), "-", 8, sb.length(), -1); assertEquals("aaxaaaay-", sb.toString());
@Test public void testContains_StrMatcher() { StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); assertTrue(sb.contains(StrMatcher.charMatcher('a'))); assertTrue(sb.contains(StrMatcher.stringMatcher("pq"))); assertTrue(sb.contains(StrMatcher.charMatcher('z'))); assertFalse(sb.contains(StrMatcher.stringMatcher("zy"))); assertFalse(sb.contains((StrMatcher) null)); sb = new StrBuilder(); assertFalse(sb.contains(A_NUMBER_MATCHER)); sb.append("B A1 C"); assertTrue(sb.contains(A_NUMBER_MATCHER)); }
@Test public void testBasicTrimmed2() { final String input = "a: b :"; final StrTokenizer tok = new StrTokenizer(input, ':'); tok.setTrimmerMatcher(StrMatcher.stringMatcher(" ")); tok.setIgnoreEmptyTokens(false); tok.setEmptyTokenAsNull(true); assertEquals("a", tok.next()); assertEquals("b", tok.next()); assertNull(tok.next()); assertFalse(tok.hasNext()); }
@Test public void testBasicIgnoreTrimmed3() { final String input = "IGNOREaIGNORE: IGNORE bIGNOREc IGNORE : IGNORE "; final StrTokenizer tok = new StrTokenizer(input, ':'); tok.setIgnoredMatcher(StrMatcher.stringMatcher("IGNORE")); tok.setIgnoreEmptyTokens(false); tok.setEmptyTokenAsNull(true); assertEquals("a", tok.next()); assertEquals(" bc ", tok.next()); assertEquals(" ", tok.next()); assertFalse(tok.hasNext()); }
@Test public void testBasicIgnoreTrimmed4() { final String input = "IGNOREaIGNORE: IGNORE 'bIGNOREc'IGNORE'd' IGNORE : IGNORE "; final StrTokenizer tok = new StrTokenizer(input, ':', '\''); tok.setIgnoredMatcher(StrMatcher.stringMatcher("IGNORE")); tok.setTrimmerMatcher(StrMatcher.trimMatcher()); tok.setIgnoreEmptyTokens(false); tok.setEmptyTokenAsNull(true); assertEquals("a", tok.next()); assertEquals("bIGNOREcd", tok.next()); assertNull(tok.next()); assertFalse(tok.hasNext()); }
@Test public void testBasicIgnoreTrimmed1() { final String input = "a: bIGNOREc : "; final StrTokenizer tok = new StrTokenizer(input, ':'); tok.setIgnoredMatcher(StrMatcher.stringMatcher("IGNORE")); tok.setTrimmerMatcher(StrMatcher.trimMatcher()); tok.setIgnoreEmptyTokens(false); tok.setEmptyTokenAsNull(true); assertEquals("a", tok.next()); assertEquals("bc", tok.next()); assertNull(tok.next()); assertFalse(tok.hasNext()); }
@Test public void testBasicIgnoreTrimmed2() { final String input = "IGNOREaIGNORE: IGNORE bIGNOREc IGNORE : IGNORE "; final StrTokenizer tok = new StrTokenizer(input, ':'); tok.setIgnoredMatcher(StrMatcher.stringMatcher("IGNORE")); tok.setTrimmerMatcher(StrMatcher.trimMatcher()); tok.setIgnoreEmptyTokens(false); tok.setEmptyTokenAsNull(true); assertEquals("a", tok.next()); assertEquals("bc", tok.next()); assertNull(tok.next()); assertFalse(tok.hasNext()); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replace(null, "x", 0, sb.length(), -1); assertEquals("abcbccba", sb.toString()); sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1); assertEquals("xbcbccbx", sb.toString()); sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1); assertEquals("xbxcxx", sb.toString()); sb = new StrBuilder("A1-A2A3-A4"); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("***-******-***", sb.toString()); sb = new StrBuilder(); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("", sb.toString()); }
sb.replaceAll(StrMatcher.charMatcher('d'), null); assertEquals("bcbccb", sb.toString()); sb.replaceAll(StrMatcher.stringMatcher("cb"), "-"); assertEquals("b-c-", sb.toString()); sb.replaceAll(StrMatcher.stringMatcher("X"), "012345678901234567"); assertEquals("Dear 012345678901234567, hello 012345678901234567.", sb.toString());
@Test public void testReplaceFirst_StrMatcher_String() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replaceFirst((StrMatcher) null, null); assertEquals("abcbccba", sb.toString()); sb.replaceFirst((StrMatcher) null, "anything"); assertEquals("abcbccba", sb.toString()); sb.replaceFirst(StrMatcher.noneMatcher(), null); assertEquals("abcbccba", sb.toString()); sb.replaceFirst(StrMatcher.noneMatcher(), "anything"); assertEquals("abcbccba", sb.toString()); sb.replaceFirst(StrMatcher.charMatcher('x'), "y"); assertEquals("abcbccba", sb.toString()); sb.replaceFirst(StrMatcher.charMatcher('a'), "d"); assertEquals("dbcbccba", sb.toString()); sb.replaceFirst(StrMatcher.charMatcher('d'), null); assertEquals("bcbccba", sb.toString()); sb.replaceFirst(StrMatcher.stringMatcher("cb"), "-"); assertEquals("b-ccba", sb.toString()); sb = new StrBuilder("abcba"); sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx"); assertEquals("axbxcba", sb.toString()); sb = new StrBuilder("bb"); sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx"); assertEquals("xbxb", sb.toString()); sb = new StrBuilder("A1-A2A3-A4"); sb.replaceFirst(A_NUMBER_MATCHER, "***"); assertEquals("***-A2A3-A4", sb.toString()); }
/** * Sets the field delimiter string. * * @param delim the delimiter string to use * @return this, to enable chaining */ public StrTokenizer setDelimiterString(final String delim) { return setDelimiterMatcher(StrMatcher.stringMatcher(delim)); }