move ComposedData to Kotlin

and add method to create ComposedData for a given word
This commit is contained in:
Helium314 2025-05-21 18:13:58 +02:00
parent d9a779a66e
commit e32a0c8e98
2 changed files with 55 additions and 56 deletions

View file

@ -1,56 +0,0 @@
/*
* Copyright (C) 2014 The Android Open Source Project
* modified
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
*/
package helium314.keyboard.latin.common;
import androidx.annotation.NonNull;
/**
* An immutable class that encapsulates a snapshot of word composition data.
*/
public class ComposedData {
@NonNull
public final InputPointers mInputPointers;
public final boolean mIsBatchMode;
@NonNull
public final String mTypedWord;
public ComposedData(@NonNull final InputPointers inputPointers, final boolean isBatchMode,
@NonNull final String typedWord) {
mInputPointers = inputPointers;
mIsBatchMode = isBatchMode;
mTypedWord = typedWord;
}
/**
* Copy the code points in the typed word to a destination array of ints.
*
* If the array is too small to hold the code points in the typed word, nothing is copied and
* -1 is returned.
*
* @param destination the array of ints.
* @return the number of copied code points.
*/
public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
@NonNull final int[] destination) {
// lastIndex is exclusive
final int lastIndex = mTypedWord.length()
- StringUtils.getTrailingSingleQuotesCount(mTypedWord);
if (lastIndex <= 0) {
// The string is empty or contains only single quotes.
return 0;
}
// The following function counts the number of code points in the text range which begins
// at index 0 and extends to the character at lastIndex.
final int codePointSize = Character.codePointCount(mTypedWord, 0, lastIndex);
if (codePointSize > destination.length) {
return -1;
}
return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWord, 0,
lastIndex, true /* downCase */);
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (C) 2014 The Android Open Source Project
* modified
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
*/
package helium314.keyboard.latin.common
import helium314.keyboard.latin.WordComposer
/** An immutable class that encapsulates a snapshot of word composition data. */
class ComposedData(
@JvmField val mInputPointers: InputPointers,
@JvmField val mIsBatchMode: Boolean,
@JvmField val mTypedWord: String
) {
/**
* Copy the code points in the typed word to a destination array of ints.
*
* If the array is too small to hold the code points in the typed word, nothing is copied and
* -1 is returned.
*
* @param destination the array of ints.
* @return the number of copied code points.
*/
fun copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
destination: IntArray
): Int {
// lastIndex is exclusive
val lastIndex = (mTypedWord.length - StringUtils.getTrailingSingleQuotesCount(mTypedWord))
if (lastIndex <= 0) {
return 0 // The string is empty or contains only single quotes.
}
// The following function counts the number of code points in the text range which begins
// at index 0 and extends to the character at lastIndex.
val codePointSize = Character.codePointCount(mTypedWord, 0, lastIndex)
if (codePointSize > destination.size) {
return -1
}
return StringUtils.copyCodePointsAndReturnCodePointCount(
destination, mTypedWord, 0, lastIndex, true
)
}
companion object {
fun createForWord(word: String): ComposedData {
val codePoints = StringUtils.toCodePointArray(word)
val coordinates = CoordinateUtils.newCoordinateArray(codePoints.size)
for (i in codePoints.indices) {
CoordinateUtils.setXYInArray(coordinates, i, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE)
}
return WordComposer().apply { setComposingWord(codePoints, coordinates) }.composedDataSnapshot
}
}
}