Skip to content

Commit

Permalink
Implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonpi committed Oct 4, 2024
1 parent f8e286c commit 0c3dc30
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions Sources/GRPCProtobufCodeGen/CamelCaser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,26 @@

package struct CamelCaser {
/// Converts a string from upper camel case to lower camel case.
package static func toLowerCamelCase(_ s: String) -> String {
if s.isEmpty { return "" }

let indexOfFirstLowerCase = s.firstIndex(where: { $0 != "_" && $0.lowercased() == String($0) })
package static func toLowerCamelCase(_ input: String) -> String {
guard let indexOfFirstLowercase = input.firstIndex(where: { $0.isLowercase }) else {
return input.lowercased()
}

if let indexOfFirstLowerCase {
if indexOfFirstLowerCase == s.startIndex {
// `s` already begins with a lower case letter. As in: "importCSV".
return s
} else if indexOfFirstLowerCase == s.index(after: s.startIndex) {
// The second character in `s` is lower case. As in: "ImportCSV".
return s[s.startIndex].lowercased() + s[indexOfFirstLowerCase...] // -> "importCSV"
} else {
// The first lower case character is further within `s`. Tentatively, `s` begins with one or
// more abbreviations. Therefore, the last encountered upper case character could be the
// beginning of the next word. As in: "FOOBARImportCSV".
if indexOfFirstLowercase == input.startIndex {
// `input` already begins with a lower case letter. As in: "importCSV".
return input
} else if indexOfFirstLowercase == input.index(after: input.startIndex) {
// The second character in `input` is lower case. As in: "ImportCSV".
return input[input.startIndex].lowercased() + input[indexOfFirstLowercase...] // -> "importCSV"
} else {
// The first lower case character is further within `input`. Tentatively, `input` begins
// with one or more abbreviations. Therefore, the last encountered upper case character
// could be the beginning of the next word. As in: "FOOBARImportCSV".

let leadingAbbreviation = s[..<s.index(before: indexOfFirstLowerCase)]
let followingWords = s[s.index(before: indexOfFirstLowerCase)...]
let leadingAbbreviation = input[..<input.index(before: indexOfFirstLowercase)]
let followingWords = input[input.index(before: indexOfFirstLowercase)...]

return leadingAbbreviation.lowercased() + followingWords // -> "foobarImportCSV"
}
} else {
// `s` did not contain any lower case letter.
return s.lowercased()
return leadingAbbreviation.lowercased() + followingWords // -> "foobarImportCSV"
}
}
}

0 comments on commit 0c3dc30

Please sign in to comment.