1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.room.ext 18 19 import java.util.Locale 20 Stringnull21private fun String.toCamelCase(): String { 22 val split = this.split("_") 23 if (split.isEmpty()) return "" 24 if (split.size == 1) return split[0].capitalize(Locale.US) 25 return split.joinToCamelCase() 26 } 27 Stringnull28private fun String.toCamelCaseAsVar(): String { 29 val split = this.split("_") 30 if (split.isEmpty()) return "" 31 if (split.size == 1) return split[0] 32 return split.joinToCamelCaseAsVar() 33 } 34 joinToCamelCasenull35private fun List<String>.joinToCamelCase(): String = 36 when (size) { 37 0 -> throw IllegalArgumentException("invalid section size, cannot be zero") 38 1 -> this[0].toCamelCase() 39 else -> this.joinToString("") { it.toCamelCase() } 40 } 41 Listnull42private fun List<String>.joinToCamelCaseAsVar(): String = 43 when (size) { 44 0 -> throw IllegalArgumentException("invalid section size, cannot be zero") 45 1 -> this[0].toCamelCaseAsVar() 46 else -> get(0).toCamelCaseAsVar() + drop(1).joinToCamelCase() 47 } 48 49 private val javaCharRegex = "[^a-zA-Z0-9]".toRegex() 50 Stringnull51fun String.stripNonJava(): String { 52 return this.split(javaCharRegex).map(String::trim).joinToCamelCaseAsVar() 53 } 54 55 // TODO: Replace this with the function from the Kotlin stdlib once the API becomes stable Stringnull56fun String.capitalize(locale: Locale): String = 57 if (isNotEmpty() && this[0].isLowerCase()) { 58 substring(0, 1).uppercase(locale) + substring(1) 59 } else { 60 this 61 } 62 63 // TODO: Replace this with the function from the Kotlin stdlib once the API becomes stable Stringnull64fun String.decapitalize(locale: Locale): String = 65 if (isNotEmpty() && this[0].isUpperCase()) { 66 substring(0, 1).lowercase(locale) + substring(1) 67 } else { 68 this 69 } 70