Skip to content

Commit d680da4

Browse files
SessionHero01SessionHero01
authored andcommitted
Added binding to utf16 truncate
1 parent 01bceb9 commit d680da4

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

library/src/main/cpp/util.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,18 @@ Java_network_loki_messenger_libsession_1util_Config_free(JNIEnv *env, jobject th
323323
delete config;
324324
}
325325
}
326+
327+
extern "C"
328+
JNIEXPORT jint JNICALL
329+
Java_network_loki_messenger_libsession_1util_util_Util_lengthForCodepoints(JNIEnv *env,
330+
jobject thiz,
331+
jstring str,
332+
jint max_codepoints) {
333+
return jni_utils::run_catching_cxx_exception_or_throws<jint>(env, [=]() {
334+
jni_utils::JavaCharsRef str_ref(env, str);
335+
return session::utf16_len_for_codepoints(
336+
{ reinterpret_cast<const char16_t *>(str_ref.chars()), str_ref.size() },
337+
max_codepoints
338+
);
339+
});
340+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package network.loki.messenger.libsession_util.util
2+
3+
data class ConfigPush(val messages: List<Bytes>, val seqNo: Long, val obsoleteHashes: List<String>)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package network.loki.messenger.libsession_util.util
2+
3+
import androidx.annotation.Keep
4+
5+
data class KeyPair(val pubKey: Bytes, val secretKey: Bytes) {
6+
@Keep
7+
constructor(pubKey: ByteArray, secretKey: ByteArray)
8+
: this(Bytes(pubKey), Bytes(secretKey))
9+
}

library/src/main/java/network/loki/messenger/libsession_util/util/Utils.kt renamed to library/src/main/java/network/loki/messenger/libsession_util/util/UserPic.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package network.loki.messenger.libsession_util.util
22

3-
data class ConfigPush(val messages: List<Bytes>, val seqNo: Long, val obsoleteHashes: List<String>)
4-
53
data class UserPic(val url: String, val key: Bytes) {
64
constructor(url: String, key: ByteArray)
75
:this(url, Bytes(key))
@@ -13,9 +11,4 @@ data class UserPic(val url: String, val key: Bytes) {
1311
// Convenience method to get the key as a ByteArray from native side
1412
val keyAsByteArray: ByteArray
1513
get() = key.data
16-
}
17-
18-
data class KeyPair(val pubKey: Bytes, val secretKey: Bytes) {
19-
constructor(pubKey: ByteArray, secretKey: ByteArray)
20-
: this(Bytes(pubKey), Bytes(secretKey))
21-
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package network.loki.messenger.libsession_util.util
2+
3+
import network.loki.messenger.libsession_util.LibSessionUtilCApi
4+
5+
object Util : LibSessionUtilCApi() {
6+
private external fun lengthForCodepoints(str: String, maxCodepoints: Int): Int
7+
8+
fun truncateCodepoints(str: String, maxCodepoints: Int): String =
9+
str.take(lengthForCodepoints(str, maxCodepoints))
10+
}

0 commit comments

Comments
 (0)