Apache Lucene open-source search software
import com.ibm.icu.lang.UCharacter
import com.ibm.icu.lang.UScript
import com.ibm.icu.util.VersionInfo
import java.nio.file.Paths
import java.util.Locale
def icuVersion = VersionInfo.ICU_VERSION.toString()
def unicodeVersion = UCharacter.getUnicodeVersion().toString()
def outputFile = Paths.get(args[0])
def generateSwitchCases() {
StringBuilder sb = new StringBuilder()
int lastScript = -1
int count = 0
for (int c = 0; c <= Character.MAX_CODE_POINT; c++) {
int icuFolded = UCharacter.foldCase(c, true)
int jdkLower = Character.toLowerCase(c)
if (icuFolded != jdkLower) {
int script = UScript.getScript(c)
String scriptName = UScript.getName(script)
if (script != lastScript) {
if (count > 0) {
sb.append("\n")
}
sb.append("\n // ${scriptName}")
lastScript = script
}
String fromChar = new String(Character.toChars(c))
String toChar = new String(Character.toChars(icuFolded))
sb.append(String.format(Locale.ROOT, "\n case 0x%04X:\n return 0x%04X; // %s → %s", c, icuFolded, fromChar, toChar))
count++
}
}
return [sb.toString(), count]
}
def (switchCases, caseCount) = generateSwitchCases()
def code = """\
// DO NOT EDIT THIS FILE! Use "gradlew regenerateSimpleCaseFolding tidy" to recreate.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.util;
/**
* Switch-based Unicode simple case folding for the ${caseCount} codepoints where folding differs from {@link
* Character#toLowerCase(int)}. Generated using ICU4J v${icuVersion}, unicode version: ${unicodeVersion}.
*
* @see <a href="https://www.unicode.org/Public/latest/ucd/CaseFolding.txt">Unicode Case Folding
* mappings</a>
*/
final class SimpleCaseFolding {
private SimpleCaseFolding() {}
/**
* Returns the simple case fold of {@code c}, falling back to {@link Character#toLowerCase(int)}
* for codepoints where fold equals lowercase.
*/
static int fold(int c) {
switch (c) {${switchCases}
default:
return Character.toLowerCase(c);
}
}
}
"""
outputFile.setText(code, "UTF-8")