|
@@ -15,7 +15,11 @@
|
|
*/
|
|
*/
|
|
package com.baomidou.mybatisplus.toolkit;
|
|
package com.baomidou.mybatisplus.toolkit;
|
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Collection;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
@@ -45,7 +49,7 @@ public class StringUtils {
|
|
public static final String PLACE_HOLDER = "{%s}";
|
|
public static final String PLACE_HOLDER = "{%s}";
|
|
|
|
|
|
private static boolean separatorBeforeDigit = false;
|
|
private static boolean separatorBeforeDigit = false;
|
|
- private static boolean separatorAfterDigit = true;
|
|
|
|
|
|
+ private static boolean separatorAfterDigit = true;
|
|
|
|
|
|
|
|
|
|
private StringUtils() {
|
|
private StringUtils() {
|
|
@@ -613,84 +617,85 @@ public class StringUtils {
|
|
/**
|
|
/**
|
|
* 驼峰转连字符
|
|
* 驼峰转连字符
|
|
* <pre>
|
|
* <pre>
|
|
- * StringUtils.camelToHyphen( "managerAdminUserService" ) = manager-admin-user-service
|
|
|
|
- * </pre>
|
|
|
|
- * @see <a href="https://github.com/krasa/StringManipulation">document</a>
|
|
|
|
|
|
+ * StringUtils.camelToHyphen( "managerAdminUserService" ) = manager-admin-user-service
|
|
|
|
+ * </pre>
|
|
|
|
+ *
|
|
* @param input
|
|
* @param input
|
|
* @return 以'-'分隔
|
|
* @return 以'-'分隔
|
|
|
|
+ * @see <a href="https://github.com/krasa/StringManipulation">document</a>
|
|
*/
|
|
*/
|
|
public static String camelToHyphen(String input) {
|
|
public static String camelToHyphen(String input) {
|
|
- return wordsToHyphenCase(wordsAndHyphenAndCamelToConstantCase( input ));
|
|
|
|
|
|
+ return wordsToHyphenCase(wordsAndHyphenAndCamelToConstantCase(input));
|
|
}
|
|
}
|
|
-
|
|
|
|
- private static String wordsAndHyphenAndCamelToConstantCase ( String input ) {
|
|
|
|
|
|
+
|
|
|
|
+ private static String wordsAndHyphenAndCamelToConstantCase(String input) {
|
|
boolean _betweenUpperCases = false;
|
|
boolean _betweenUpperCases = false;
|
|
- boolean containsLowerCase = containsLowerCase( input );
|
|
|
|
-
|
|
|
|
- StringBuilder buf = new StringBuilder();
|
|
|
|
- char previousChar = ' ';
|
|
|
|
- char[] chars = input.toCharArray();
|
|
|
|
- for ( int i = 0 ; i < chars.length ; i++ ) {
|
|
|
|
- char c = chars[i];
|
|
|
|
- boolean isUpperCaseAndPreviousIsUpperCase = ( Character.isUpperCase( previousChar ) ) && ( Character.isUpperCase(
|
|
|
|
- c ) );
|
|
|
|
- boolean isUpperCaseAndPreviousIsLowerCase = ( Character.isLowerCase( previousChar ) ) && ( Character.isUpperCase(
|
|
|
|
- c ) );
|
|
|
|
-
|
|
|
|
- boolean previousIsWhitespace = Character.isWhitespace( previousChar );
|
|
|
|
- boolean lastOneIsNotUnderscore = ( buf.length() > 0 ) && ( buf.charAt( buf.length() - 1 ) != '_' );
|
|
|
|
- boolean isNotUnderscore = c != '_';
|
|
|
|
- if ( ( lastOneIsNotUnderscore ) && ( ( isUpperCaseAndPreviousIsLowerCase ) || ( previousIsWhitespace ) || ( ( _betweenUpperCases ) && ( containsLowerCase ) && ( isUpperCaseAndPreviousIsUpperCase ) ) ) ) {
|
|
|
|
- buf.append( "_" );
|
|
|
|
- } else if ( ( ( separatorAfterDigit ) && ( Character.isDigit( previousChar ) ) && ( Character.isLetter( c ) ) ) || ( ( separatorBeforeDigit ) && ( Character
|
|
|
|
- .isDigit( c ) ) && ( Character.isLetter( previousChar ) ) ) ) {
|
|
|
|
- buf.append( '_' );
|
|
|
|
|
|
+ boolean containsLowerCase = containsLowerCase(input);
|
|
|
|
+
|
|
|
|
+ StringBuilder buf = new StringBuilder();
|
|
|
|
+ char previousChar = ' ';
|
|
|
|
+ char[] chars = input.toCharArray();
|
|
|
|
+ for (int i = 0; i < chars.length; i++) {
|
|
|
|
+ char c = chars[i];
|
|
|
|
+ boolean isUpperCaseAndPreviousIsUpperCase = (Character.isUpperCase(previousChar)) && (Character.isUpperCase(
|
|
|
|
+ c));
|
|
|
|
+ boolean isUpperCaseAndPreviousIsLowerCase = (Character.isLowerCase(previousChar)) && (Character.isUpperCase(
|
|
|
|
+ c));
|
|
|
|
+
|
|
|
|
+ boolean previousIsWhitespace = Character.isWhitespace(previousChar);
|
|
|
|
+ boolean lastOneIsNotUnderscore = (buf.length() > 0) && (buf.charAt(buf.length() - 1) != '_');
|
|
|
|
+ boolean isNotUnderscore = c != '_';
|
|
|
|
+ if ((lastOneIsNotUnderscore) && ((isUpperCaseAndPreviousIsLowerCase) || (previousIsWhitespace) || ((_betweenUpperCases) && (containsLowerCase) && (isUpperCaseAndPreviousIsUpperCase)))) {
|
|
|
|
+ buf.append("_");
|
|
|
|
+ } else if (((separatorAfterDigit) && (Character.isDigit(previousChar)) && (Character.isLetter(c))) || ((separatorBeforeDigit) && (Character
|
|
|
|
+ .isDigit(c)) && (Character.isLetter(previousChar)))) {
|
|
|
|
+ buf.append('_');
|
|
}
|
|
}
|
|
- if ( ( shouldReplace( c ) ) && ( lastOneIsNotUnderscore ) ) {
|
|
|
|
- buf.append( '_' );
|
|
|
|
- } else if ( ( ! Character.isWhitespace( c ) ) && ( ( isNotUnderscore ) || ( lastOneIsNotUnderscore ) ) ) {
|
|
|
|
- buf.append( Character.toUpperCase( c ) );
|
|
|
|
|
|
+ if ((shouldReplace(c)) && (lastOneIsNotUnderscore)) {
|
|
|
|
+ buf.append('_');
|
|
|
|
+ } else if ((!Character.isWhitespace(c)) && ((isNotUnderscore) || (lastOneIsNotUnderscore))) {
|
|
|
|
+ buf.append(Character.toUpperCase(c));
|
|
}
|
|
}
|
|
previousChar = c;
|
|
previousChar = c;
|
|
}
|
|
}
|
|
- if ( Character.isWhitespace( previousChar ) ) {
|
|
|
|
- buf.append( "_" );
|
|
|
|
|
|
+ if (Character.isWhitespace(previousChar)) {
|
|
|
|
+ buf.append("_");
|
|
}
|
|
}
|
|
return buf.toString();
|
|
return buf.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public static boolean containsLowerCase ( String s ) {
|
|
|
|
- for ( char c : s.toCharArray() ) {
|
|
|
|
- if ( Character.isLowerCase( c ) ) {
|
|
|
|
|
|
+ public static boolean containsLowerCase(String s) {
|
|
|
|
+ for (char c : s.toCharArray()) {
|
|
|
|
+ if (Character.isLowerCase(c)) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- private static boolean shouldReplace ( char c ) {
|
|
|
|
- return ( c == '.' ) || ( c == '_' ) || ( c == '-' );
|
|
|
|
|
|
+ private static boolean shouldReplace(char c) {
|
|
|
|
+ return (c == '.') || (c == '_') || (c == '-');
|
|
}
|
|
}
|
|
|
|
|
|
- private static String wordsToHyphenCase ( String s ) {
|
|
|
|
- StringBuilder buf = new StringBuilder();
|
|
|
|
- char lastChar = 'a';
|
|
|
|
- for ( char c : s.toCharArray() ) {
|
|
|
|
- if ( ( Character.isWhitespace( lastChar ) ) && ( ! Character.isWhitespace( c ) ) && ( '-' != c ) && ( buf.length() > 0 ) && ( buf
|
|
|
|
- .charAt( buf.length() - 1 ) != '-' ) ) {
|
|
|
|
- buf.append( "-" );
|
|
|
|
|
|
+ private static String wordsToHyphenCase(String s) {
|
|
|
|
+ StringBuilder buf = new StringBuilder();
|
|
|
|
+ char lastChar = 'a';
|
|
|
|
+ for (char c : s.toCharArray()) {
|
|
|
|
+ if ((Character.isWhitespace(lastChar)) && (!Character.isWhitespace(c)) && ('-' != c) && (buf.length() > 0) && (buf
|
|
|
|
+ .charAt(buf.length() - 1) != '-')) {
|
|
|
|
+ buf.append("-");
|
|
}
|
|
}
|
|
- if ( '_' == c ) {
|
|
|
|
- buf.append( '-' );
|
|
|
|
- } else if ( '.' == c ) {
|
|
|
|
- buf.append( '-' );
|
|
|
|
- } else if ( ! Character.isWhitespace( c ) ) {
|
|
|
|
- buf.append( Character.toLowerCase( c ) );
|
|
|
|
|
|
+ if ('_' == c) {
|
|
|
|
+ buf.append('-');
|
|
|
|
+ } else if ('.' == c) {
|
|
|
|
+ buf.append('-');
|
|
|
|
+ } else if (!Character.isWhitespace(c)) {
|
|
|
|
+ buf.append(Character.toLowerCase(c));
|
|
}
|
|
}
|
|
lastChar = c;
|
|
lastChar = c;
|
|
}
|
|
}
|
|
- if ( Character.isWhitespace( lastChar ) ) {
|
|
|
|
- buf.append( "-" );
|
|
|
|
|
|
+ if (Character.isWhitespace(lastChar)) {
|
|
|
|
+ buf.append("-");
|
|
}
|
|
}
|
|
return buf.toString();
|
|
return buf.toString();
|
|
}
|
|
}
|