elharo commented on code in PR #450: URL: https://github.com/apache/commons-text/pull/450#discussion_r1320746730
########## src/main/java/org/apache/commons/text/cases/PascalCase.java: ########## @@ -0,0 +1,119 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.CharUtils; +import org.apache.commons.lang3.StringUtils; + +/** + * Case implementation which parses and formats strings of the form 'MyPascalString' + * <p> + * PascalCase is a case where tokens are delimited by uppercase ASCII characters. Each parsed token + * <b>must</b> begin with an uppercase character, but the case of the remaining token characters is + * ignored and returned as-is. + * </p> + */ +public final class PascalCase implements Case { + + /** constant reusable instance of this case. */ + public static final PascalCase INSTANCE = new PascalCase(); + + /** + * Constructs a new PascalCase instance. + */ + private PascalCase() { + } + + /** + * Parses a PascalCase string into tokens. + * <p> + * String characters are iterated over and any time an upper case ASCII character is + * encountered, that character is considered to be the start of a new token, with the character + * itself included in the token. This method should never return empty tokens. The first + * character of the string must be an uppercase ASCII character. No further restrictions are + * placed on string contents. + * </p> + * @param string The Pascal Cased string to parse + * @return the list of tokens found in the string + * @throws IllegalArgumentException if the string does not begin with an uppercase ASCII alpha character + */ + @Override + public List<String> parse(String string) { + List<String> tokens = new ArrayList<>(); + if (string.length() == 0) { + return tokens; + } + if (!CharUtils.isAsciiAlphaUpper(string.charAt(0))) { + throw new IllegalArgumentException("Character '" + string.charAt(0) + "' at index 0 must be ASCII uppercase"); + } + int strLen = string.length(); + int[] tokenCodePoints = new int[strLen]; + int tokenCodePointsOffset = 0; + for (int i = 0; i < string.length();) { + final int codePoint = string.codePointAt(i); + if (CharUtils.isAsciiAlphaUpper((char) codePoint)) { + if (tokenCodePointsOffset > 0) { + tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset)); + tokenCodePoints = new int[strLen]; + tokenCodePointsOffset = 0; + } + tokenCodePoints[tokenCodePointsOffset++] = codePoint; + i += Character.charCount(codePoint); + } else { + tokenCodePoints[tokenCodePointsOffset++] = codePoint; + i += Character.charCount(codePoint); + } + } + tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset)); + return tokens; + } + + /** + * Formats string tokens into a Pascal Case string. + * <p> + * Iterates the tokens and formats each one into a Pascal Case token. The first character of + * the token must be an ASCII alpha character. This character is forced upper case in the + * output. The remaining alpha characters of the token are forced lowercase. Any other + * characters in the token are returned as-is. Empty tokens are not supported. + * </p> + * @param tokens The string tokens to be formatted into Pascal Case + * @return The Pascal Case formatted string Review Comment: the Pascal ########## src/main/java/org/apache/commons/text/cases/Case.java: ########## @@ -0,0 +1,52 @@ +/* + * 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; + +import java.util.List; + +/** + * Handles formatting and parsing tokens to/from a String. For most implementations tokens returned + * by the parse method should abide by any restrictions present in the format method. i.e. Calling Review Comment: i.e. Calling --> That is, calling ########## src/main/java/org/apache/commons/text/cases/CamelCase.java: ########## @@ -0,0 +1,121 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.CharUtils; +import org.apache.commons.lang3.StringUtils; + +/** + * Case implementation that parses and formats strings of the form 'myCamelCase' + * <p> + * This case separates tokens on uppercase ASCII alpha characters. Each token begins with an Review Comment: Have we talked about whether it's necessary to limit this to ASCII yet? ########## src/main/java/org/apache/commons/text/cases/CamelCase.java: ########## @@ -0,0 +1,121 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.CharUtils; +import org.apache.commons.lang3.StringUtils; + +/** + * Case implementation that parses and formats strings of the form 'myCamelCase' + * <p> + * This case separates tokens on uppercase ASCII alpha characters. Each token begins with an + * uppercase ASCII alpha character, except the first token, which begins with a lowercase ASCII + * alpha character. + * </p> + */ +public final class CamelCase implements Case { + + /** constant reusable instance of this case. */ + public static final CamelCase INSTANCE = new CamelCase(); + + /** + * Constructs new CamelCase instance. + */ + private CamelCase() { + super(); + } + + /** + * Parses string tokens from a Camel Case formatted string. + * <p> + * Parses each character of the string parameter and creates new tokens when uppercase ASCII + * letters are encountered. The uppercase letter is considered part of the new token. The very + * first character of the string is an exception to this rule and must be a lowercase ASCII + * character. This method places no other restrictions on the content of the string. <br> + * Note: This method should never produce empty tokens. + * </p> + * @param string camel case formatted string to parse + * @return list of tokens parsed from the string + * @throws IllegalArgumentException if the string does not begin with a lowercase ASCII alpha character + */ + @Override + public List<String> parse(String string) { + List<String> tokens = new ArrayList<>(); + if (string.length() == 0) { + return tokens; + } + if (!CharUtils.isAsciiAlphaLower(string.charAt(0))) { + throw new IllegalArgumentException("Character '" + string.charAt(0) + "' at index 0 must be an ASCII lowercase letter"); + } + int strLen = string.length(); + int[] tokenCodePoints = new int[strLen]; + int tokenCodePointsOffset = 0; + for (int i = 0; i < string.length();) { + final int codePoint = string.codePointAt(i); + if (CharUtils.isAsciiAlphaUpper((char) codePoint)) { + if (tokenCodePointsOffset > 0) { + tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset)); + tokenCodePoints = new int[strLen]; + tokenCodePointsOffset = 0; + } + tokenCodePoints[tokenCodePointsOffset++] = codePoint; + i += Character.charCount(codePoint); + } else { + tokenCodePoints[tokenCodePointsOffset++] = codePoint; + i += Character.charCount(codePoint); + } + } + tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset)); + return tokens; + } + + /** + * Formats tokens into a Camel Case string. + * <p> + * Iterates over tokens and creates a camel case formatted string. Each token must begin with an + * ASCII letter, which will be converted to uppercase in the output, except for the very first token, + * which will have a lowercase first character. The remaining characters in all tokens will be + * converted to lowercase. This Case does not support empty tokens.<br> Review Comment: delete the br tag -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
