Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 24B7A200C92 for ; Mon, 12 Jun 2017 14:45:09 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 200E1160BD9; Mon, 12 Jun 2017 12:45:09 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4126E160BEC for ; Mon, 12 Jun 2017 14:45:08 +0200 (CEST) Received: (qmail 71041 invoked by uid 500); 12 Jun 2017 12:45:07 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 70652 invoked by uid 99); 12 Jun 2017 12:45:06 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Jun 2017 12:45:06 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id B7ADF1A03B9 for ; Mon, 12 Jun 2017 12:45:05 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.202 X-Spam-Level: X-Spam-Status: No, score=-99.202 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id tgeT3K8xuolv for ; Mon, 12 Jun 2017 12:45:04 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id C43FB5FBDF for ; Mon, 12 Jun 2017 12:45:03 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 9430BE0984 for ; Mon, 12 Jun 2017 12:45:02 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 8A32521E13 for ; Mon, 12 Jun 2017 12:45:00 +0000 (UTC) Date: Mon, 12 Jun 2017 12:45:00 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (TEXT-85) Create CaseUtils class. Add toCamelCase MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Mon, 12 Jun 2017 12:45:09 -0000 [ https://issues.apache.org/jira/browse/TEXT-85?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16046516#comment-16046516 ] ASF GitHub Bot commented on TEXT-85: ------------------------------------ Github user chtompki commented on a diff in the pull request: https://github.com/apache/commons-text/pull/46#discussion_r121380327 --- Diff: src/main/java/org/apache/commons/text/CaseUtils.java --- @@ -0,0 +1,140 @@ +/* + * 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; + +import java.util.HashSet; +import java.util.Set; + +/** + *

Case manipulation operations on Strings that contain words.

+ * + *

This class tries to handle null input gracefully. + * An exception will not be thrown for a null input. + * Each method documents its behaviour in more detail.

+ * + * @since 1.0 + */ +public class CaseUtils { + + /** + *

CaseUtils instances should NOT be constructed in + * standard programming. Instead, the class should be used as + * CaseUtils.toCamelCase("foo bar", true, new char[]{'-'});.

+ *

+ *

This constructor is public to permit tools that require a JavaBean + * instance to operate.

+ */ + public CaseUtils() { + super(); + } + + // Camel Case + //----------------------------------------------------------------------- + /** + *

Converts all the delimiter separated words in a String into camelCase, + * that is each word is made up of a titlecase character and then a series of + * lowercase characters. The

+ * + *

The delimiters represent a set of characters understood to separate words. + * The first non-delimiter character after a delimiter will be capitalized. The first String + * character may or may not be capitalized and it's determined by the user input for capitalizeFirstLetter + * variable.

+ * + *

A null input String returns null. + * Capitalization uses the Unicode title case, normally equivalent to + * upper case.

+ * + *
    +     * CaseUtils.toCamelCase(null, false)                                 = null
    +     * CaseUtils.toCamelCase("", false, *)                                = ""
    +     * CaseUtils.toCamelCase(*, false, null)                              = *
    +     * CaseUtils.toCamelCase(*, true, new char[0])                        = *
    +     * CaseUtils.toCamelCase("To.Camel.Case", false, new char[]{'.'})     = "toCamelCase"
    +     * CaseUtils.toCamelCase(" to @ Camel case", true, new char[]{'@'})   = "toCamelCase"
    +     * CaseUtils.toCamelCase(" @to @ Camel case", false, new char[]{'@'}) = "toCamelCase"
    +     * 
+ * + * @param str the String to be converted to camelCase, may be null + * @param capitalizeFirstLetter boolean that determines if the first character of first word should be title case. + * @param delimiters set of characters to determine capitalization, null and/or empty array means whitespace + * @return camelCase of String, null if null String input + */ + public static String toCamelCase(String str, boolean capitalizeFirstLetter, final char... delimiters) { + if (StringUtils.isEmpty(str)) { + return str; + } + str = str.toLowerCase(); + int strLen = str.length(); + int [] newCodePoints = new int[strLen]; + int outOffset = 0; + Set delimiterSet = generateDelimiterSet(delimiters); + boolean capitalizeNext = false; + if (capitalizeFirstLetter) { + capitalizeNext = true; + } + for (int index = 0; index < strLen;) { + final int codePoint = str.codePointAt(index); + + if (delimiterSet.contains(codePoint)) { + capitalizeNext = true; + if (outOffset == 0) { + capitalizeNext = false; + } + index += Character.charCount(codePoint); + } else if (capitalizeNext || (outOffset == 0 && capitalizeFirstLetter)) { + int titleCaseCodePoint = Character.toTitleCase(codePoint); --- End diff -- +1 on operating on `int`s. > Create CaseUtils class. Add toCamelCase > --------------------------------------- > > Key: TEXT-85 > URL: https://issues.apache.org/jira/browse/TEXT-85 > Project: Commons Text > Issue Type: Improvement > Reporter: Rob Tompkins > Assignee: Rob Tompkins > > Based on the conversation here: > http://markmail.org/message/7nvizsbykvxpr7g5 > We wish to have a {{toCamelCase}} method. The suggestion is to create a {{CaseUtils}} class. > I wonder if we should think about deprecating the case management in {{WordUtils}} and move it over? Maybe, maybe not. > I would think our method signature would look something like: > {code} > String toCamelCase(String str, char delimiter, boolean capitalizeFirstLetter) > {code} > potentially with {{String}} replaced with {{CharSequence}}. > Lastly, {{WordUtils.capitalizeFully(String str, final char... delimiters)}} might be a good starting point. https://github.com/apache/commons-text/blob/master/src/main/java/org/apache/commons/text/WordUtils.java#L467-L499 -- This message was sent by Atlassian JIRA (v6.4.14#64029)