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 17private fun String.toCamelCase(): String { 18 val split = this.split("_") 19 if (split.isEmpty()) return "" 20 if (split.size == 1) return split[0].capitalize() 21 return split.joinToCamelCase() 22} 23 24private fun String.toCamelCaseAsVar(): String { 25 val split = this.split("_") 26 if (split.isEmpty()) return "" 27 if (split.size == 1) return split[0] 28 return split.joinToCamelCaseAsVar() 29} 30 31private fun List<String>.joinToCamelCase(): String = when (size) { 32 0 -> throw IllegalArgumentException("invalid section size, cannot be zero") 33 1 -> this[0].toCamelCase() 34 else -> this.joinToString("") { it.toCamelCase() } 35} 36 37private fun List<String>.joinToCamelCaseAsVar(): String = when (size) { 38 0 -> throw IllegalArgumentException("invalid section size, cannot be zero") 39 1 -> this[0].toCamelCaseAsVar() 40 else -> get(0).toCamelCaseAsVar() + drop(1).joinToCamelCase() 41} 42 43private val javaCharRegex = "[^a-zA-Z0-9]".toRegex() 44fun String.stripNonJava(): String { 45 return this.split(javaCharRegex) 46 .map(String::trim) 47 .joinToCamelCaseAsVar() 48} 49