Skip to content
Open
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
b64a0e0
initial implementation of Cases api
Aug 15, 2023
0de50a2
spelling
Aug 21, 2023
47c244f
spelling
Aug 21, 2023
ff8c791
private util method, spelling
Aug 21, 2023
31bd896
class description
Aug 21, 2023
f8cbd57
loop
Aug 21, 2023
d741a00
private/protected methods, spelling
Aug 21, 2023
67ccbb8
use ArrayList
Aug 21, 2023
b3a3368
doc formatting
Aug 21, 2023
f98d277
doc spelling
Aug 21, 2023
76de865
@throws docs, spelling
Aug 21, 2023
de03d81
checkstyle violations
Aug 21, 2023
dd0394b
doc formatting
Aug 21, 2023
1695051
javadoc formatting
Sep 11, 2023
1cb481f
implemented unicode casing logic
Oct 2, 2023
8833ae2
order of if/else
Oct 2, 2023
c328233
Revert "order of if/else"
Oct 2, 2023
eff0529
spelling
Oct 3, 2023
b32035f
grammar
Oct 4, 2023
0280d33
Improved implementation to better handle unicode cases
Oct 19, 2023
52e6441
renamed class for clarity
Oct 19, 2023
23912de
javadoc comment
Oct 19, 2023
670f84e
renamed variable for clarity
Oct 19, 2023
c24c802
Util class no longer needed, can place methods directly in base class
Oct 19, 2023
3c1d7a8
javadocs
Oct 19, 2023
8cb9291
grammar and spelling
Oct 20, 2023
cbfbad7
separate error messages
Oct 20, 2023
bbde93f
use list for delimiters since collection size is likely small
Oct 24, 2023
8f5acb0
use isempty
Oct 24, 2023
9aba7a3
allow null delimiters
Oct 24, 2023
fe070e8
allow null when parsing
Oct 24, 2023
f92cae9
Updated package info
Oct 24, 2023
6613073
javadoc formatting
Oct 24, 2023
d3964ee
package protected constructors
Oct 24, 2023
145b4e4
javadoc
Oct 24, 2023
d585716
utils class to hold static references
Oct 30, 2023
daa58d2
moved tests to correct case methods
Oct 31, 2023
8db51dd
javadocs
Oct 31, 2023
7fc8770
javadocs
Oct 31, 2023
89eca00
renamed class
Oct 31, 2023
8aa6edb
added string matcher that matches on any Unicode upper case tokens
Nov 1, 2023
8b2ced4
allow setting whether to omit the delimiter from the token
theshoeshiner Nov 28, 2023
992ac13
test omit delimiter flag
theshoeshiner Nov 28, 2023
a50975c
converted cases api to use StringTokenizer and TokenStringifier logic
theshoeshiner Nov 28, 2023
1406d30
throw unsupported from uppercase matcher
theshoeshiner Nov 28, 2023
6747348
licenses
theshoeshiner Nov 28, 2023
61bf2e5
dont need to check when there is not enough room left for constant
theshoeshiner Nov 29, 2023
05422f1
tests for token factory and stringifier
theshoeshiner Nov 29, 2023
58f05e3
formatting
theshoeshiner Nov 29, 2023
5481f1d
spelling
theshoeshiner Nov 29, 2023
25236f1
licenses
theshoeshiner Nov 29, 2023
9e2bcd1
Merge branch 'apache:master' into cases
theshoeshiner Nov 29, 2023
ce5d2e2
Merge branch 'master' of
Mar 20, 2024
f91a1a2
corrected merge
Mar 20, 2024
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
30 changes: 27 additions & 3 deletions src/main/java/org/apache/commons/text/StringTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ public static StringTokenizer getTSVInstance(final String input) {
/** Whether to ignore empty tokens. */
private boolean ignoreEmptyTokens = true;

/** Whether to omit delimiter matches from output. */
private boolean omitDelimiterMatches = true;

/**
* Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer, but with no text to
* tokenize.
Expand Down Expand Up @@ -751,8 +754,11 @@ private int readNextToken(final char[] srcChars, int start, final int len, final
// handle empty token
final int delimLen = getDelimiterMatcher().isMatch(srcChars, start, start, len);
if (delimLen > 0) {
addToken(tokenList, StringUtils.EMPTY);
return start + delimLen;
//empty token is not possible if we are including delimiters in token
if (omitDelimiterMatches) {
addToken(tokenList, StringUtils.EMPTY);
return start + delimLen;
}
}

// handle found token
Expand Down Expand Up @@ -826,7 +832,14 @@ private int readWithQuotes(final char[] srcChars, final int start, final int len
if (delimLen > 0) {
// return condition when end of token found
addToken(tokenList, workArea.substring(0, trimStart));
return pos + delimLen;
if (omitDelimiterMatches) {
return pos + delimLen;
} else {
//increment position only if we found a new delimiter
if (pos > start) {
return pos;
}
}
}

// check for quote, and thus back into quoting mode
Expand Down Expand Up @@ -1021,6 +1034,17 @@ public StringTokenizer setIgnoreEmptyTokens(final boolean ignoreEmptyTokens) {
return this;
}

/**
* Sets whether the tokenizer should omit the delimiter matches from the output tokens. Default is true.
*
* @param omitDelimiterMatches whether delimiter matches are omitted
* @return this, to enable chaining
*/
public StringTokenizer setOmitDelimiterMatches(final boolean omitDelimiterMatches) {
this.omitDelimiterMatches = omitDelimiterMatches;
return this;
}

/**
* Sets the quote character to use.
* <p>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/apache/commons/text/TokenFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text;

public interface TokenFormatter {
String format(char[] prior, int tokenIndex, char[] token);
}
130 changes: 130 additions & 0 deletions src/main/java/org/apache/commons/text/TokenFormatterFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text;

import org.apache.commons.lang3.StringUtils;

public class TokenFormatterFactory {

/**
* Token formatter that returns the token as is.
*/
public static class NoOpFormatter implements TokenFormatter {
@Override
public String format(char[] prior, int tokenIndex, char[] token) {
return new String(token);
}

}

/**
* Token formatter that always returns a constant string, and optionally checks the passed in token
* for the constant and throws an error when found.
*/
public static class ConstantTokenFormatter implements TokenFormatter {

/**
* The constant to return.
*/
private char[] constant;

/**
* Whether or not to throw an exception if the constant is found.
*/
private boolean failOnConstantFound = true;

public ConstantTokenFormatter(char constant) {
this(new char[] {constant}, true);
}

public ConstantTokenFormatter(char constant, boolean failOnConstantFound) {
this(new char[] {constant}, failOnConstantFound);
}

public ConstantTokenFormatter(String constant) {
this(constant, true);
}

public ConstantTokenFormatter(String constant, boolean failOnConstantFound) {
this(constant.toCharArray(), failOnConstantFound);
}

public ConstantTokenFormatter(char[] constant, boolean failOnConstantFound) {
this.constant = constant;
this.failOnConstantFound = failOnConstantFound;
}

@Override
public String format(char[] prior, int tokenIndex, char[] token) {
if (failOnConstantFound) {
for (int i = 0; i < token.length; i++) {
boolean match = false;
int t = i;
for (int j = 0; j < constant.length; j++) {
if (token[t] == constant[j]) {
match = true;
} else {
match = false;
break;
}
t++;
}
if (match) {
throw new IllegalArgumentException("Token " + tokenIndex + " contains illegal character '" + new String(constant) + "' at index " + t);
}
}
}

return new String(constant);
}

/**
* Set whether to check the token for the constant.
* @param checkTokenForConstant whether to check.
*/
public void setFailOnConstantFound(boolean checkTokenForConstant) {
this.failOnConstantFound = checkTokenForConstant;
}

}

/**
* Reuseable NoOpFormatter instance.
*/
private static final NoOpFormatter NOOP_FORMATTER = new NoOpFormatter();

/**
* Reuseable Empty String formatter instance.
*/
private static final ConstantTokenFormatter EMPTY_STRING_FORMATTER = new ConstantTokenFormatter(StringUtils.EMPTY, false);

public static NoOpFormatter noOpFormatter() {
return NOOP_FORMATTER;
}

public static ConstantTokenFormatter constantFormatter(char[] constant, boolean failOnConstant) {
return new ConstantTokenFormatter(constant, failOnConstant);
}

public static ConstantTokenFormatter constantFormatter(char constant, boolean failOnConstant) {
return new ConstantTokenFormatter(constant, failOnConstant);
}

public static ConstantTokenFormatter emptyFormatter() {
return EMPTY_STRING_FORMATTER;
}
}
97 changes: 97 additions & 0 deletions src/main/java/org/apache/commons/text/TokenStringifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text;

/**
* Takes a collection of String tokens and combines them into a single String.
* <p>
* This class functions as the inverse of {@link org.apache.commons.text.StringTokenizer}. All tokens are formatted
* by a {@link TokenFormatter} which allows fine grained control over the final output.
* </p>
*/
public class TokenStringifier {

/**
* The formatter for the delimiter.
*/
private TokenFormatter delimiterFormatter;

/**
* The formatter for the tokens.
*/
private TokenFormatter tokenFormatter;

/**
* Builder used to hold formatted tokens.
*/
private StringBuilder builder;

/**
* The final string.
*/
private String string;

/**
* The tokens to turn into a String.
*/
private Iterable<String> tokens;

public TokenStringifier(TokenFormatter delimiterFormatter, TokenFormatter tokenFormatter) {
super();
this.delimiterFormatter = delimiterFormatter;
this.tokenFormatter = tokenFormatter;
}

public void reset(Iterable<String> tokens) {
this.tokens = tokens;
this.string = null;
this.builder = null;
}

public TokenStringifier() {
tokenFormatter = TokenFormatterFactory.noOpFormatter();
delimiterFormatter = TokenFormatterFactory.noOpFormatter();
}

private void stringify() {
builder = new StringBuilder();
char[] priorToken = null;
int i = 0;
for (String token : tokens) {
char[] tokenChars = token.toCharArray();
if (i > 0) {
String delimiter = delimiterFormatter.format(priorToken, i, tokenChars);
if (delimiter != null) {
builder.append(delimiter);
}
}
String formatted = tokenFormatter.format(priorToken, i, tokenChars);
if (formatted != null) {
builder.append(formatted);
}
i++;
}
string = builder.toString();
}

public String getString() {
if (string == null) {
stringify();
}
return string;
}
}
40 changes: 40 additions & 0 deletions src/main/java/org/apache/commons/text/cases/CamelCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text.cases;

/**
* Case implementation that parses and formats strings of the form 'myCamelCase'
* <p>
* CamelCase is a case where tokens are delimited by upper case Unicode characters. The very first
* token should begin with a lower case character, and any subsequent tokens begin with an
* upper case character. All remaining characters will be lower case or non cased.
* </p>
*/
public final class CamelCase extends UpperCaseDelimitedCase {

/** Constant reusable instance of this case. */
public static final CamelCase INSTANCE = new CamelCase();

/**
* Constructs new CamelCase instance.
*/

private CamelCase() {
super(true);
}

}
Loading