From 0c3dc30649393acd9c5e2967ca8e794e1718cd77 Mon Sep 17 00:00:00 2001 From: cnkwocha Date: Fri, 4 Oct 2024 16:55:30 +0100 Subject: [PATCH] Implement feedback --- Sources/GRPCProtobufCodeGen/CamelCaser.swift | 39 +++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Sources/GRPCProtobufCodeGen/CamelCaser.swift b/Sources/GRPCProtobufCodeGen/CamelCaser.swift index eaee81f..67efd44 100644 --- a/Sources/GRPCProtobufCodeGen/CamelCaser.swift +++ b/Sources/GRPCProtobufCodeGen/CamelCaser.swift @@ -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[.. "foobarImportCSV" - } - } else { - // `s` did not contain any lower case letter. - return s.lowercased() + return leadingAbbreviation.lowercased() + followingWords // -> "foobarImportCSV" } } }