| 1 |
// Copyright 2015 Huan Du. All rights reserved. |
| 2 |
// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
package xstrings |
| 5 |
|
| 6 |
import ( |
| 7 |
"math/rand" |
| 8 |
"unicode" |
| 9 |
"unicode/utf8" |
| 10 |
) |
| 11 |
|
| 12 |
// ToCamelCase is to convert words separated by space, underscore and hyphen to camel case. |
| 13 |
// |
| 14 |
// Some samples. |
| 15 |
// |
| 16 |
// "some_words" => "someWords" |
| 17 |
// "http_server" => "httpServer" |
| 18 |
// "no_https" => "noHttps" |
| 19 |
// "_complex__case_" => "_complex_Case_" |
| 20 |
// "some words" => "someWords" |
| 21 |
// "GOLANG_IS_GREAT" => "golangIsGreat" |
| 22 |
func ToCamelCase(str string) string { |
| 23 |
return toCamelCase(str, false) |
| 24 |
} |
| 25 |
|
| 26 |
// ToPascalCase is to convert words separated by space, underscore and hyphen to pascal case. |
| 27 |
// |
| 28 |
// Some samples. |
| 29 |
// |
| 30 |
// "some_words" => "SomeWords" |
| 31 |
// "http_server" => "HttpServer" |
| 32 |
// "no_https" => "NoHttps" |
| 33 |
// "_complex__case_" => "_Complex_Case_" |
| 34 |
// "some words" => "SomeWords" |
| 35 |
// "GOLANG_IS_GREAT" => "GolangIsGreat" |
| 36 |
func ToPascalCase(str string) string { |
| 37 |
return toCamelCase(str, true) |
| 38 |
} |
| 39 |
|
| 40 |
func toCamelCase(str string, isBig bool) string { |
| 41 |
if len(str) == 0 { |
| 42 |
return "" |
| 43 |
} |
| 44 |
|
| 45 |
buf := &stringBuilder{} |
| 46 |
var isFirstRuneUpper bool |
| 47 |
var r0, r1 rune |
| 48 |
var size int |
| 49 |
|
| 50 |
// leading connector will appear in output. |
| 51 |
for len(str) > 0 { |
| 52 |
r0, size = utf8.DecodeRuneInString(str) |
| 53 |
str = str[size:] |
| 54 |
|
| 55 |
if !isConnector(r0) { |
| 56 |
isFirstRuneUpper = unicode.IsUpper(r0) |
| 57 |
|
| 58 |
if isBig { |
| 59 |
r0 = unicode.ToUpper(r0) |
| 60 |
} else { |
| 61 |
r0 = unicode.ToLower(r0) |
| 62 |
} |
| 63 |
|
| 64 |
break |
| 65 |
} |
| 66 |
|
| 67 |
buf.WriteRune(r0) |
| 68 |
} |
| 69 |
|
| 70 |
if len(str) == 0 { |
| 71 |
// A special case for a string contains only 1 rune. |
| 72 |
if size != 0 { |
| 73 |
buf.WriteRune(r0) |
| 74 |
} |
| 75 |
|
| 76 |
return buf.String() |
| 77 |
} |
| 78 |
|
| 79 |
for len(str) > 0 { |
| 80 |
r1 = r0 |
| 81 |
r0, size = utf8.DecodeRuneInString(str) |
| 82 |
str = str[size:] |
| 83 |
|
| 84 |
if isConnector(r0) && isConnector(r1) { |
| 85 |
buf.WriteRune(r1) |
| 86 |
continue |
| 87 |
} |
| 88 |
|
| 89 |
if isConnector(r1) { |
| 90 |
isFirstRuneUpper = unicode.IsUpper(r0) |
| 91 |
r0 = unicode.ToUpper(r0) |
| 92 |
} else { |
| 93 |
if isFirstRuneUpper { |
| 94 |
if unicode.IsUpper(r0) { |
| 95 |
r0 = unicode.ToLower(r0) |
| 96 |
} else { |
| 97 |
isFirstRuneUpper = false |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
buf.WriteRune(r1) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if isFirstRuneUpper && !isBig { |
| 106 |
r0 = unicode.ToLower(r0) |
| 107 |
} |
| 108 |
|
| 109 |
buf.WriteRune(r0) |
| 110 |
return buf.String() |
| 111 |
} |
| 112 |
|
| 113 |
// ToSnakeCase can convert all upper case characters in a string to |
| 114 |
// snake case format. |
| 115 |
// |
| 116 |
// Some samples. |
| 117 |
// |
| 118 |
// "FirstName" => "first_name" |
| 119 |
// "HTTPServer" => "http_server" |
| 120 |
// "NoHTTPS" => "no_https" |
| 121 |
// "GO_PATH" => "go_path" |
| 122 |
// "GO PATH" => "go_path" // space is converted to underscore. |
| 123 |
// "GO-PATH" => "go_path" // hyphen is converted to underscore. |
| 124 |
// "http2xx" => "http_2xx" // insert an underscore before a number and after an alphabet. |
| 125 |
// "HTTP20xOK" => "http_20x_ok" |
| 126 |
// "Duration2m3s" => "duration_2m3s" |
| 127 |
// "Bld4Floor3rd" => "bld4_floor_3rd" |
| 128 |
func ToSnakeCase(str string) string { |
| 129 |
return camelCaseToLowerCase(str, '_') |
| 130 |
} |
| 131 |
|
| 132 |
// ToKebabCase can convert all upper case characters in a string to |
| 133 |
// kebab case format. |
| 134 |
// |
| 135 |
// Some samples. |
| 136 |
// |
| 137 |
// "FirstName" => "first-name" |
| 138 |
// "HTTPServer" => "http-server" |
| 139 |
// "NoHTTPS" => "no-https" |
| 140 |
// "GO_PATH" => "go-path" |
| 141 |
// "GO PATH" => "go-path" // space is converted to '-'. |
| 142 |
// "GO-PATH" => "go-path" // hyphen is converted to '-'. |
| 143 |
// "http2xx" => "http-2xx" // insert an underscore before a number and after an alphabet. |
| 144 |
// "HTTP20xOK" => "http-20x-ok" |
| 145 |
// "Duration2m3s" => "duration-2m3s" |
| 146 |
// "Bld4Floor3rd" => "bld4-floor-3rd" |
| 147 |
func ToKebabCase(str string) string { |
| 148 |
return camelCaseToLowerCase(str, '-') |
| 149 |
} |
| 150 |
|
| 151 |
func camelCaseToLowerCase(str string, connector rune) string { |
| 152 |
if len(str) == 0 { |
| 153 |
return "" |
| 154 |
} |
| 155 |
|
| 156 |
buf := &stringBuilder{} |
| 157 |
wt, word, remaining := nextWord(str) |
| 158 |
|
| 159 |
for len(remaining) > 0 { |
| 160 |
if wt != connectorWord { |
| 161 |
toLower(buf, wt, word, connector) |
| 162 |
} |
| 163 |
|
| 164 |
prev := wt |
| 165 |
last := word |
| 166 |
wt, word, remaining = nextWord(remaining) |
| 167 |
|
| 168 |
switch prev { |
| 169 |
case numberWord: |
| 170 |
for wt == alphabetWord || wt == numberWord { |
| 171 |
toLower(buf, wt, word, connector) |
| 172 |
wt, word, remaining = nextWord(remaining) |
| 173 |
} |
| 174 |
|
| 175 |
if wt != invalidWord && wt != punctWord && wt != connectorWord { |
| 176 |
buf.WriteRune(connector) |
| 177 |
} |
| 178 |
|
| 179 |
case connectorWord: |
| 180 |
toLower(buf, prev, last, connector) |
| 181 |
|
| 182 |
case punctWord: |
| 183 |
// nothing. |
| 184 |
|
| 185 |
default: |
| 186 |
if wt != numberWord { |
| 187 |
if wt != connectorWord && wt != punctWord { |
| 188 |
buf.WriteRune(connector) |
| 189 |
} |
| 190 |
|
| 191 |
break |
| 192 |
} |
| 193 |
|
| 194 |
if len(remaining) == 0 { |
| 195 |
break |
| 196 |
} |
| 197 |
|
| 198 |
last := word |
| 199 |
wt, word, remaining = nextWord(remaining) |
| 200 |
|
| 201 |
// consider number as a part of previous word. |
| 202 |
// e.g. "Bld4Floor" => "bld4_floor" |
| 203 |
if wt != alphabetWord { |
| 204 |
toLower(buf, numberWord, last, connector) |
| 205 |
|
| 206 |
if wt != connectorWord && wt != punctWord { |
| 207 |
buf.WriteRune(connector) |
| 208 |
} |
| 209 |
|
| 210 |
break |
| 211 |
} |
| 212 |
|
| 213 |
// if there are some lower case letters following a number, |
| 214 |
// add connector before the number. |
| 215 |
// e.g. "HTTP2xx" => "http_2xx" |
| 216 |
buf.WriteRune(connector) |
| 217 |
toLower(buf, numberWord, last, connector) |
| 218 |
|
| 219 |
for wt == alphabetWord || wt == numberWord { |
| 220 |
toLower(buf, wt, word, connector) |
| 221 |
wt, word, remaining = nextWord(remaining) |
| 222 |
} |
| 223 |
|
| 224 |
if wt != invalidWord && wt != connectorWord && wt != punctWord { |
| 225 |
buf.WriteRune(connector) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
toLower(buf, wt, word, connector) |
| 231 |
return buf.String() |
| 232 |
} |
| 233 |
|
| 234 |
func isConnector(r rune) bool { |
| 235 |
return r == '-' || r == '_' || unicode.IsSpace(r) |
| 236 |
} |
| 237 |
|
| 238 |
type wordType int |
| 239 |
|
| 240 |
const ( |
| 241 |
invalidWord wordType = iota |
| 242 |
numberWord |
| 243 |
upperCaseWord |
| 244 |
alphabetWord |
| 245 |
connectorWord |
| 246 |
punctWord |
| 247 |
otherWord |
| 248 |
) |
| 249 |
|
| 250 |
func nextWord(str string) (wt wordType, word, remaining string) { |
| 251 |
if len(str) == 0 { |
| 252 |
return |
| 253 |
} |
| 254 |
|
| 255 |
var offset int |
| 256 |
remaining = str |
| 257 |
r, size := nextValidRune(remaining, utf8.RuneError) |
| 258 |
offset += size |
| 259 |
|
| 260 |
if r == utf8.RuneError { |
| 261 |
wt = invalidWord |
| 262 |
word = str[:offset] |
| 263 |
remaining = str[offset:] |
| 264 |
return |
| 265 |
} |
| 266 |
|
| 267 |
switch { |
| 268 |
case isConnector(r): |
| 269 |
wt = connectorWord |
| 270 |
remaining = remaining[size:] |
| 271 |
|
| 272 |
for len(remaining) > 0 { |
| 273 |
r, size = nextValidRune(remaining, r) |
| 274 |
|
| 275 |
if !isConnector(r) { |
| 276 |
break |
| 277 |
} |
| 278 |
|
| 279 |
offset += size |
| 280 |
remaining = remaining[size:] |
| 281 |
} |
| 282 |
|
| 283 |
case unicode.IsPunct(r): |
| 284 |
wt = punctWord |
| 285 |
remaining = remaining[size:] |
| 286 |
|
| 287 |
for len(remaining) > 0 { |
| 288 |
r, size = nextValidRune(remaining, r) |
| 289 |
|
| 290 |
if !unicode.IsPunct(r) { |
| 291 |
break |
| 292 |
} |
| 293 |
|
| 294 |
offset += size |
| 295 |
remaining = remaining[size:] |
| 296 |
} |
| 297 |
|
| 298 |
case unicode.IsUpper(r): |
| 299 |
wt = upperCaseWord |
| 300 |
remaining = remaining[size:] |
| 301 |
|
| 302 |
if len(remaining) == 0 { |
| 303 |
break |
| 304 |
} |
| 305 |
|
| 306 |
r, size = nextValidRune(remaining, r) |
| 307 |
|
| 308 |
switch { |
| 309 |
case unicode.IsUpper(r): |
| 310 |
prevSize := size |
| 311 |
offset += size |
| 312 |
remaining = remaining[size:] |
| 313 |
|
| 314 |
for len(remaining) > 0 { |
| 315 |
r, size = nextValidRune(remaining, r) |
| 316 |
|
| 317 |
if !unicode.IsUpper(r) { |
| 318 |
break |
| 319 |
} |
| 320 |
|
| 321 |
prevSize = size |
| 322 |
offset += size |
| 323 |
remaining = remaining[size:] |
| 324 |
} |
| 325 |
|
| 326 |
// it's a bit complex when dealing with a case like "HTTPStatus". |
| 327 |
// it's expected to be splitted into "HTTP" and "Status". |
| 328 |
// Therefore "S" should be in remaining instead of word. |
| 329 |
if len(remaining) > 0 && isAlphabet(r) { |
| 330 |
offset -= prevSize |
| 331 |
remaining = str[offset:] |
| 332 |
} |
| 333 |
|
| 334 |
case isAlphabet(r): |
| 335 |
offset += size |
| 336 |
remaining = remaining[size:] |
| 337 |
|
| 338 |
for len(remaining) > 0 { |
| 339 |
r, size = nextValidRune(remaining, r) |
| 340 |
|
| 341 |
if !isAlphabet(r) || unicode.IsUpper(r) { |
| 342 |
break |
| 343 |
} |
| 344 |
|
| 345 |
offset += size |
| 346 |
remaining = remaining[size:] |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
case isAlphabet(r): |
| 351 |
wt = alphabetWord |
| 352 |
remaining = remaining[size:] |
| 353 |
|
| 354 |
for len(remaining) > 0 { |
| 355 |
r, size = nextValidRune(remaining, r) |
| 356 |
|
| 357 |
if !isAlphabet(r) || unicode.IsUpper(r) { |
| 358 |
break |
| 359 |
} |
| 360 |
|
| 361 |
offset += size |
| 362 |
remaining = remaining[size:] |
| 363 |
} |
| 364 |
|
| 365 |
case unicode.IsNumber(r): |
| 366 |
wt = numberWord |
| 367 |
remaining = remaining[size:] |
| 368 |
|
| 369 |
for len(remaining) > 0 { |
| 370 |
r, size = nextValidRune(remaining, r) |
| 371 |
|
| 372 |
if !unicode.IsNumber(r) { |
| 373 |
break |
| 374 |
} |
| 375 |
|
| 376 |
offset += size |
| 377 |
remaining = remaining[size:] |
| 378 |
} |
| 379 |
|
| 380 |
default: |
| 381 |
wt = otherWord |
| 382 |
remaining = remaining[size:] |
| 383 |
|
| 384 |
for len(remaining) > 0 { |
| 385 |
r, size = nextValidRune(remaining, r) |
| 386 |
|
| 387 |
if size == 0 || isConnector(r) || isAlphabet(r) || unicode.IsNumber(r) || unicode.IsPunct(r) { |
| 388 |
break |
| 389 |
} |
| 390 |
|
| 391 |
offset += size |
| 392 |
remaining = remaining[size:] |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
word = str[:offset] |
| 397 |
return |
| 398 |
} |
| 399 |
|
| 400 |
func nextValidRune(str string, prev rune) (r rune, size int) { |
| 401 |
var sz int |
| 402 |
|
| 403 |
for len(str) > 0 { |
| 404 |
r, sz = utf8.DecodeRuneInString(str) |
| 405 |
size += sz |
| 406 |
|
| 407 |
if r != utf8.RuneError { |
| 408 |
return |
| 409 |
} |
| 410 |
|
| 411 |
str = str[sz:] |
| 412 |
} |
| 413 |
|
| 414 |
r = prev |
| 415 |
return |
| 416 |
} |
| 417 |
|
| 418 |
func toLower(buf *stringBuilder, wt wordType, str string, connector rune) { |
| 419 |
buf.Grow(buf.Len() + len(str)) |
| 420 |
|
| 421 |
if wt != upperCaseWord && wt != connectorWord { |
| 422 |
buf.WriteString(str) |
| 423 |
return |
| 424 |
} |
| 425 |
|
| 426 |
for len(str) > 0 { |
| 427 |
r, size := utf8.DecodeRuneInString(str) |
| 428 |
str = str[size:] |
| 429 |
|
| 430 |
if isConnector(r) { |
| 431 |
buf.WriteRune(connector) |
| 432 |
} else if unicode.IsUpper(r) { |
| 433 |
buf.WriteRune(unicode.ToLower(r)) |
| 434 |
} else { |
| 435 |
buf.WriteRune(r) |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// SwapCase will swap characters case from upper to lower or lower to upper. |
| 441 |
func SwapCase(str string) string { |
| 442 |
var r rune |
| 443 |
var size int |
| 444 |
|
| 445 |
buf := &stringBuilder{} |
| 446 |
|
| 447 |
for len(str) > 0 { |
| 448 |
r, size = utf8.DecodeRuneInString(str) |
| 449 |
|
| 450 |
switch { |
| 451 |
case unicode.IsUpper(r): |
| 452 |
buf.WriteRune(unicode.ToLower(r)) |
| 453 |
|
| 454 |
case unicode.IsLower(r): |
| 455 |
buf.WriteRune(unicode.ToUpper(r)) |
| 456 |
|
| 457 |
default: |
| 458 |
buf.WriteRune(r) |
| 459 |
} |
| 460 |
|
| 461 |
str = str[size:] |
| 462 |
} |
| 463 |
|
| 464 |
return buf.String() |
| 465 |
} |
| 466 |
|
| 467 |
// FirstRuneToUpper converts first rune to upper case if necessary. |
| 468 |
func FirstRuneToUpper(str string) string { |
| 469 |
if str == "" { |
| 470 |
return str |
| 471 |
} |
| 472 |
|
| 473 |
r, size := utf8.DecodeRuneInString(str) |
| 474 |
|
| 475 |
if !unicode.IsLower(r) { |
| 476 |
return str |
| 477 |
} |
| 478 |
|
| 479 |
buf := &stringBuilder{} |
| 480 |
buf.WriteRune(unicode.ToUpper(r)) |
| 481 |
buf.WriteString(str[size:]) |
| 482 |
return buf.String() |
| 483 |
} |
| 484 |
|
| 485 |
// FirstRuneToLower converts first rune to lower case if necessary. |
| 486 |
func FirstRuneToLower(str string) string { |
| 487 |
if str == "" { |
| 488 |
return str |
| 489 |
} |
| 490 |
|
| 491 |
r, size := utf8.DecodeRuneInString(str) |
| 492 |
|
| 493 |
if !unicode.IsUpper(r) { |
| 494 |
return str |
| 495 |
} |
| 496 |
|
| 497 |
buf := &stringBuilder{} |
| 498 |
buf.WriteRune(unicode.ToLower(r)) |
| 499 |
buf.WriteString(str[size:]) |
| 500 |
return buf.String() |
| 501 |
} |
| 502 |
|
| 503 |
// Shuffle randomizes runes in a string and returns the result. |
| 504 |
// It uses default random source in `math/rand`. |
| 505 |
func Shuffle(str string) string { |
| 506 |
if str == "" { |
| 507 |
return str |
| 508 |
} |
| 509 |
|
| 510 |
runes := []rune(str) |
| 511 |
index := 0 |
| 512 |
|
| 513 |
for i := len(runes) - 1; i > 0; i-- { |
| 514 |
index = rand.Intn(i + 1) |
| 515 |
|
| 516 |
if i != index { |
| 517 |
runes[i], runes[index] = runes[index], runes[i] |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
return string(runes) |
| 522 |
} |
| 523 |
|
| 524 |
// ShuffleSource randomizes runes in a string with given random source. |
| 525 |
func ShuffleSource(str string, src rand.Source) string { |
| 526 |
if str == "" { |
| 527 |
return str |
| 528 |
} |
| 529 |
|
| 530 |
runes := []rune(str) |
| 531 |
index := 0 |
| 532 |
r := rand.New(src) |
| 533 |
|
| 534 |
for i := len(runes) - 1; i > 0; i-- { |
| 535 |
index = r.Intn(i + 1) |
| 536 |
|
| 537 |
if i != index { |
| 538 |
runes[i], runes[index] = runes[index], runes[i] |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
return string(runes) |
| 543 |
} |
| 544 |
|
| 545 |
// Successor returns the successor to string. |
| 546 |
// |
| 547 |
// If there is one alphanumeric rune is found in string, increase the rune by 1. |
| 548 |
// If increment generates a "carry", the rune to the left of it is incremented. |
| 549 |
// This process repeats until there is no carry, adding an additional rune if necessary. |
| 550 |
// |
| 551 |
// If there is no alphanumeric rune, the rightmost rune will be increased by 1 |
| 552 |
// regardless whether the result is a valid rune or not. |
| 553 |
// |
| 554 |
// Only following characters are alphanumeric. |
| 555 |
// - a - z |
| 556 |
// - A - Z |
| 557 |
// - 0 - 9 |
| 558 |
// |
| 559 |
// Samples (borrowed from ruby's String#succ document): |
| 560 |
// |
| 561 |
// "abcd" => "abce" |
| 562 |
// "THX1138" => "THX1139" |
| 563 |
// "<<koala>>" => "<<koalb>>" |
| 564 |
// "1999zzz" => "2000aaa" |
| 565 |
// "ZZZ9999" => "AAAA0000" |
| 566 |
// "***" => "**+" |
| 567 |
func Successor(str string) string { |
| 568 |
if str == "" { |
| 569 |
return str |
| 570 |
} |
| 571 |
|
| 572 |
var r rune |
| 573 |
var i int |
| 574 |
carry := ' ' |
| 575 |
runes := []rune(str) |
| 576 |
l := len(runes) |
| 577 |
lastAlphanumeric := l |
| 578 |
|
| 579 |
for i = l - 1; i >= 0; i-- { |
| 580 |
r = runes[i] |
| 581 |
|
| 582 |
if ('a' <= r && r <= 'y') || |
| 583 |
('A' <= r && r <= 'Y') || |
| 584 |
('0' <= r && r <= '8') { |
| 585 |
runes[i]++ |
| 586 |
carry = ' ' |
| 587 |
lastAlphanumeric = i |
| 588 |
break |
| 589 |
} |
| 590 |
|
| 591 |
switch r { |
| 592 |
case 'z': |
| 593 |
runes[i] = 'a' |
| 594 |
carry = 'a' |
| 595 |
lastAlphanumeric = i |
| 596 |
|
| 597 |
case 'Z': |
| 598 |
runes[i] = 'A' |
| 599 |
carry = 'A' |
| 600 |
lastAlphanumeric = i |
| 601 |
|
| 602 |
case '9': |
| 603 |
runes[i] = '0' |
| 604 |
carry = '0' |
| 605 |
lastAlphanumeric = i |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
// Needs to add one character for carry. |
| 610 |
if i < 0 && carry != ' ' { |
| 611 |
buf := &stringBuilder{} |
| 612 |
buf.Grow(l + 4) // Reserve enough space for write. |
| 613 |
|
| 614 |
if lastAlphanumeric != 0 { |
| 615 |
buf.WriteString(str[:lastAlphanumeric]) |
| 616 |
} |
| 617 |
|
| 618 |
buf.WriteRune(carry) |
| 619 |
|
| 620 |
for _, r = range runes[lastAlphanumeric:] { |
| 621 |
buf.WriteRune(r) |
| 622 |
} |
| 623 |
|
| 624 |
return buf.String() |
| 625 |
} |
| 626 |
|
| 627 |
// No alphanumeric character. Simply increase last rune's value. |
| 628 |
if lastAlphanumeric == l { |
| 629 |
runes[l-1]++ |
| 630 |
} |
| 631 |
|
| 632 |
return string(runes) |
| 633 |
} |