diff --git a/GRDB/Record/EncodableRecord.swift b/GRDB/Record/EncodableRecord.swift index bf1f22b0e..aa00ffae8 100644 --- a/GRDB/Record/EncodableRecord.swift +++ b/GRDB/Record/EncodableRecord.swift @@ -428,65 +428,9 @@ public enum DatabaseKeyEncodingStrategy { case .useDefaultKeys: return key.stringValue case .convertToSnakeCase: - return Self.convertToSnakeCase(key.stringValue) + return key.stringValue.convertToSnakeCase() case let .custom(column): return column(key) } } - - /// This function is taken directly from Apple's open source JSONEncoder. - /// https://github.com/apple/swift-corelibs-foundation/blob/eec4b26deee34edb7664ddd9c1222492a399d122/Sources/Foundation/JSONEncoder.swift#L127 - private static func convertToSnakeCase(_ stringKey: String) -> String { - guard !stringKey.isEmpty else { return stringKey } - - var words: [Range] = [] - // The general idea of this algorithm is to split words on transition from lower to upper case, then on - // transition of >1 upper case characters to lowercase - // - // myProperty -> my_property - // myURLProperty -> my_url_property - // - // We assume, per Swift naming conventions, that the first character of the key is lowercase. - var wordStart = stringKey.startIndex - var searchRange = stringKey.index(after: wordStart)..1 capital letters. Turn those into a word, stopping at the capital before - // the lower case character. - let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound) - words.append(upperCaseRange.lowerBound.. String { - guard !stringKey.isEmpty else { return stringKey } - - var words: [Range] = [] - // The general idea of this algorithm is to split words on transition from lower to upper case, then - // on transition of >1 upper case characters to lowercase - // - // myProperty -> my_property - // myURLProperty -> my_url_property - // - // We assume, per Swift naming conventions, that the first character of the key is lowercase. - var wordStart = stringKey.startIndex - var searchRange = stringKey.index(after: wordStart)..1 capital letters. Turn those into a word, stopping at the capital - // before the lower case character. - let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound) - words.append(upperCaseRange.lowerBound.. String { + guard !isEmpty else { return self } + + var words: [Range] = [] + // The general idea of this algorithm is to split words on transition + // from lower to upper case, then on transition of >1 upper case + // characters to lowercase + // + // myProperty -> my_property + // myURLProperty -> my_url_property + // + // We assume, per Swift naming conventions, that the first character of + // the key is lowercase. + var wordStart = startIndex + var searchRange = index(after: wordStart)..1 capital letters. Turn those into a + // word, stopping at the capital before the lower case character. + let beforeLowerIndex = index(before: lowerCaseRange.lowerBound) + words.append(upperCaseRange.lowerBound..