@@ -39,7 +39,7 @@ import java.lang.Character.isJavaIdentifierStart
39
39
* @author Martin Hauner
40
40
*/
41
41
fun toCamelCase (src : String ): String {
42
- return joinCamelCase(joinSingleCharWords( splitAtWordBreaks(src) ))
42
+ return joinCamelCase(splitAtWordBreaks(src))
43
43
}
44
44
45
45
/* *
@@ -81,10 +81,9 @@ fun toClass(src: String): String {
81
81
* @author Martin Hauner
82
82
*/
83
83
fun toEnum (src : String ): String {
84
- return joinEnum(joinSingleCharWords( splitAtWordBreaks(src) ))
84
+ return joinEnum(splitAtWordBreaks(src))
85
85
}
86
86
87
-
88
87
/* *
89
88
* joins the given words to a single camel case string.
90
89
*
@@ -95,7 +94,7 @@ fun toEnum(src: String): String {
95
94
*
96
95
* @author Martin Hauner
97
96
*/
98
- private fun joinCamelCase (words : ArrayList <String >): String {
97
+ private fun joinCamelCase (words : List <String >): String {
99
98
val sb = StringBuilder ()
100
99
101
100
words.forEachIndexed { idx, p ->
@@ -121,7 +120,7 @@ private fun joinCamelCase(words: ArrayList<String>): String {
121
120
*
122
121
* @author Martin Hauner
123
122
*/
124
- private fun joinEnum (words : ArrayList <String >): String {
123
+ private fun joinEnum (words : List <String >): String {
125
124
val result = words.joinToString(" _" ) { it.toUpperCase() }
126
125
127
126
if (result.isEmpty()) {
@@ -131,42 +130,6 @@ private fun joinEnum(words: ArrayList<String>): String {
131
130
return result
132
131
}
133
132
134
- /* *
135
- * joins two words if at least one has only a single character.
136
- *
137
- * this tries to avoid identifiers with multiple uppercase characters in a row.
138
- *
139
- * @param words a list of words
140
- * @return a list of words
141
- *
142
- * @author Martin Hauner
143
- */
144
- private fun joinSingleCharWords (words : List <String >): ArrayList <String > {
145
- val merged = ArrayList <String >()
146
- val current = StringBuilder ()
147
-
148
- words.forEachIndexed { idx, p ->
149
- if (idx == 0 ) {
150
- current.append(p)
151
- } else {
152
- if (current.last().isUpperCase() && (current.length == 1 || p.length == 1 )) {
153
- current.append(p)
154
- } else {
155
- merged.add(current.toString())
156
- current.clear()
157
- current.append(p)
158
- }
159
- }
160
- }
161
-
162
-
163
- if (current.isNotEmpty()) {
164
- merged.add(current.toString())
165
- }
166
-
167
- return merged
168
- }
169
-
170
133
/* *
171
134
* splits the given string at the word breaks.
172
135
*
@@ -182,7 +145,7 @@ private fun splitAtWordBreaks(src: String): List<String> {
182
145
val trimmed = src.trimInvalidStart()
183
146
trimmed.forEachIndexed { idx, c ->
184
147
185
- if (idx == 0 || c.isNoWordBreak( )) {
148
+ if (idx == 0 || ! trimmed.isWordBreak(idx )) {
186
149
current.append(c)
187
150
return @forEachIndexed
188
151
}
@@ -219,13 +182,33 @@ private fun StringBuilder.appendValid(c: Char): StringBuilder {
219
182
return this
220
183
}
221
184
185
+ private fun String.isWordBreak (idx : Int ): Boolean {
186
+ return this .isForcedBreak(idx)
187
+ || this .isCaseBreak(idx)
188
+ }
189
+
190
+ private fun String.isForcedBreak (idx : Int ): Boolean {
191
+ return this [idx].isWordBreak()
192
+ }
193
+
194
+ // detect existing camel case word breaks
195
+ private fun String.isCaseBreak (idx : Int ): Boolean {
196
+ if (idx == 0 )
197
+ return false
198
+
199
+ val prev = this [idx - 1 ]
200
+ val curr = this [idx]
201
+
202
+ return prev.isLowerCase()
203
+ && curr.isUpperCase()
204
+ }
205
+
222
206
private fun Char.isNoWordBreak (): Boolean {
223
207
return ! isWordBreak()
224
208
}
225
209
226
210
private fun Char.isWordBreak (): Boolean {
227
211
return isWordBreakChar(this )
228
- || this .isUpperCase()
229
212
|| ! isJavaIdentifierPart(this )
230
213
}
231
214
0 commit comments