Skip to content
Merged
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
311 changes: 85 additions & 226 deletions src/main/java/com/github/sttk/stringcase/StringCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -795,86 +795,93 @@ public static String snakeCaseWithKeep(String input, String kept) {
}

/**
* Converts a string to train case.
* Converts the input string to train case with the specified options.
*
* This method takes a string as its argument, then returns a string of which
* the case style is train case.
*
* This method targets the upper and lower cases of ASCII alphabets for
* capitalization, and all characters except ASCII alphabets and ASCII
* numbers are eliminated as word separators.
*
* <pre>{@code
* String train = StringCase.trainCase("fooBarBaz");
* // => "Foo-Bar-Baz"
* }</pre>
*
* @param input A string to be converted.
* @param input The input string.
* @param opts The options with specifies the ways of case conversion.
* @return A string converted to train case.
*/
public static String trainCase(String input) {
var result = new CodepointBuffer(input.length() * 2);
public static String trainCaseWithOptions(String input, Options opts) {
var result = new CodepointBuffer(input.length());

final int HYPHEN = 0x2d;

enum ChIs {
FirstOfStr,
NextOfUpper,
NextOfContdUpper,
NextOfSepMark,
NextOfKeepedMark,
Others,
}
var flag = ChIs.FirstOfStr;

int[] sepChs = null;
if (opts.separators != null && !opts.separators.isEmpty()) {
sepChs = opts.separators.codePoints().toArray();
Arrays.sort(sepChs);
}

int[] keptChs = null;
if (opts.keep != null && !opts.keep.isEmpty()) {
keptChs = opts.keep.codePoints().toArray();
Arrays.sort(keptChs);
}

for (int ch : input.codePoints().toArray()) {
if (Ascii.isUpperCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
if (flag == ChIs.FirstOfStr) {
result.append(ch);
flag = ChIs.NextOfUpper;
break;
case ChIs.NextOfUpper:
case ChIs.NextOfContdUpper:
} else if (flag == ChIs.NextOfUpper || flag == ChIs.NextOfContdUpper ||
(!opts.separateAfterNonAlphabets && flag == ChIs.NextOfKeptMark)) {
result.append(Ascii.toLowerCase(ch));
flag = ChIs.NextOfContdUpper;
break;
default:
} else {
result.append(HYPHEN, ch);
flag = ChIs.NextOfUpper;
break;
}
} else if (Ascii.isLowerCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
if (flag == ChIs.FirstOfStr) {
result.append(Ascii.toUpperCase(ch));
break;
case ChIs.NextOfContdUpper:
} else if (flag == ChIs.NextOfContdUpper) {
int prev = result.last();
if (Ascii.isLowerCase(prev)) {
prev = Ascii.toUpperCase(prev);
}
result.replaceLast(HYPHEN, prev, ch);
break;
case ChIs.NextOfSepMark:
case ChIs.NextOfKeepedMark:
} else if (flag == ChIs.NextOfSepMark ||
(opts.separateAfterNonAlphabets && flag == ChIs.NextOfKeptMark)) {
result.append(HYPHEN, Ascii.toUpperCase(ch));
break;
default:
result.append(ch);
break;
}
flag = ChIs.Others;
} else if (Ascii.isDigit(ch)) {
if (flag == ChIs.NextOfSepMark) {
result.append(HYPHEN, ch);
} else {
result.append(ch);
}
flag = ChIs.NextOfKeepedMark;
flag = ChIs.Others;
} else {
if (flag != ChIs.FirstOfStr) {
flag = ChIs.NextOfSepMark;
var isKeptChar = false;
if (Ascii.isDigit(ch)) {
isKeptChar = true;
} else if (sepChs != null) {
if (Arrays.binarySearch(sepChs, ch) < 0) {
isKeptChar = true;
}
} else if (keptChs != null) {
if (Arrays.binarySearch(keptChs, ch) >= 0) {
isKeptChar = true;
}
}

if (isKeptChar) {
if (opts.separateBeforeNonAlphabets) {
if (flag == ChIs.FirstOfStr || flag == ChIs.NextOfKeptMark) {
result.append(ch);
} else {
result.append(HYPHEN, ch);
}
} else {
if (flag != ChIs.NextOfSepMark) {
result.append(ch);
} else {
result.append(HYPHEN, ch);
}
}
flag = ChIs.NextOfKeptMark;
} else {
if (flag != ChIs.FirstOfStr) {
flag = ChIs.NextOfSepMark;
}
}
}
}
Expand All @@ -883,191 +890,43 @@ enum ChIs {
}

/**
* Converts a string to train case using the specified characters as
* separators.
*
* This method takes a string as its argument, then returns a string of which
* the case style is train case.
*
* This method targets only the upper and lower cases of ASCII alphabets for
* capitalization, and the characters specified as the second argument of
* this method are regarded as word separators and are replaced to hyphens.
*
* <pre>{@code
* String train = StringCase.trainCaseWithSep("foo-Bar100%Baz", "- ");
* // => "Foo-Bar100%-Baz"
* }</pre>
* Converts the input string to train case.
* <p>
* It treats the end of a sequence of non-alphabetical characters as a word boundary, but not
* the beginning.
*
* @param input A string to be converted.
* @param seps A string that consists of characters that are word
* separators.
* @param input The input string.
* @return A string converted to train case.
*/
public static String trainCaseWithSep(String input, String seps) {
var result = new CodepointBuffer(input.length() * 2);

final int HYPHEN = 0x2d;

var sepChs = seps.codePoints().toArray();
Arrays.sort(sepChs);

enum ChIs {
FirstOfStr,
NextOfUpper,
NextOfContdUpper,
NextOfSepMark,
NextOfKeepedMark,
Others,
}
var flag = ChIs.FirstOfStr;

for (int ch : input.codePoints().toArray()) {
if (Arrays.binarySearch(sepChs, ch) >= 0) {
if (flag != ChIs.FirstOfStr) {
flag = ChIs.NextOfSepMark;
}
} else if (Ascii.isUpperCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
result.append(ch);
flag = ChIs.NextOfUpper;
break;
case ChIs.NextOfUpper:
case ChIs.NextOfContdUpper:
result.append(Ascii.toLowerCase(ch));
flag = ChIs.NextOfContdUpper;
break;
default:
result.append(HYPHEN, ch);
flag = ChIs.NextOfUpper;
break;
}
} else if (Ascii.isLowerCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
result.append(Ascii.toUpperCase(ch));
break;
case ChIs.NextOfContdUpper:
int prev = result.last();
if (Ascii.isLowerCase(prev)) {
prev = Ascii.toUpperCase(prev);
}
result.replaceLast(HYPHEN, prev, ch);
break;
case ChIs.NextOfSepMark:
case ChIs.NextOfKeepedMark:
result.append(HYPHEN, Ascii.toUpperCase(ch));
break;
default:
result.append(ch);
break;
}
flag = ChIs.Others;
} else {
if (flag == ChIs.NextOfSepMark) {
result.append(HYPHEN, ch);
} else {
result.append(ch);
}
flag = ChIs.NextOfKeepedMark;
}
}

return result.toString();
public static String trainCase(String input) {
return trainCaseWithOptions(input, new Options(false, true, null, null));
}

/**
* Converts a string to train case using characters other than the specified
* characters as separators.
*
* This method takes a string as its argument, then returns a string of which
* the case style is train case.
* Converts the input string to train case with the specified separator characters.
*
* This method targets only the upper and lower cases of ASCII alphabets for
* capitalization, and the characters other than the specified characters as
* the second argument of this method are regarded as word separators and are
* replaced to hyphens.
* @param input The input string.
* @param seps The symbol characters to be treated as separators.
* @return A string converted to train case.
*
* <pre>{@code
* String train = StringCase.trainCaseWithKeep("foo-bar100%baz", "%");
* // => "Foo-Bar100%-Baz"
* }</pre>
* @deprecated Should use {@link #trainCaseWithOptions} instead
*/
@Deprecated
public static String trainCaseWithSep(String input, String seps) {
return trainCaseWithOptions(input, new Options(false, true, seps, null));
}

/**
* Converts the input string to train case with the specified characters to be kept.
*
* @param input A string to be converted.
* @param keeped A string that consists of characters that are not word
* separators.
* @param input The input string.
* @param kept The symbol characters not to be treated as separators.
* @return A string converted to train case.
*
* @deprecated Should use {@link #trainCaseWithOptions} instead
*/
public static String trainCaseWithKeep(String input, String keeped) {
var result = new CodepointBuffer(input.length() * 2);

final int HYPHEN = 0x2d;

var keepChs = keeped.codePoints().toArray();
Arrays.sort(keepChs);

enum ChIs {
FirstOfStr,
NextOfUpper,
NextOfContdUpper,
NextOfSepMark,
NextOfKeepedMark,
Others,
}
var flag = ChIs.FirstOfStr;

for (int ch : input.codePoints().toArray()) {
if (Ascii.isUpperCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
result.append(ch);
flag = ChIs.NextOfUpper;
break;
case ChIs.NextOfUpper:
case ChIs.NextOfContdUpper:
result.append(Ascii.toLowerCase(ch));
flag = ChIs.NextOfContdUpper;
break;
default:
result.append(HYPHEN, ch);
flag = ChIs.NextOfUpper;
break;
}
} else if (Ascii.isLowerCase(ch)) {
switch (flag) {
case ChIs.FirstOfStr:
result.append(Ascii.toUpperCase(ch));
break;
case ChIs.NextOfContdUpper:
int prev = result.last();
if (Ascii.isLowerCase(prev)) {
prev = Ascii.toUpperCase(prev);
}
result.replaceLast(HYPHEN, prev, ch);
break;
case ChIs.NextOfSepMark:
case ChIs.NextOfKeepedMark:
result.append(HYPHEN, Ascii.toUpperCase(ch));
break;
default:
result.append(ch);
break;
}
flag = ChIs.Others;
} else if (Ascii.isDigit(ch) || Arrays.binarySearch(keepChs, ch) >= 0) {
if (flag == ChIs.NextOfSepMark) {
result.append(HYPHEN, ch);
} else {
result.append(ch);
}
flag = ChIs.NextOfKeepedMark;
} else {
if (flag != ChIs.FirstOfStr) {
flag = ChIs.NextOfSepMark;
}
}
}

return result.toString();
@Deprecated
public static String trainCaseWithKeep(String input, String kept) {
return trainCaseWithOptions(input, new Options(false, true, null, kept));
}
}
Loading