From 28198e76438ec5318eb04fed331f3ea37da8fff2 Mon Sep 17 00:00:00 2001 From: Han Yin Date: Tue, 19 Aug 2025 11:25:02 -0700 Subject: [PATCH] util: split FormatUtils into multiple utils for better readability --- .../com/example/llama/util/FormatUtils.kt | 80 ------------------ .../com/example/llama/util/LocaleUtils.kt | 81 +++++++++++++++++++ .../java/com/example/llama/util/TableUtils.kt | 38 +++++++++ 3 files changed, 119 insertions(+), 80 deletions(-) create mode 100644 examples/llama.android/app/src/main/java/com/example/llama/util/LocaleUtils.kt create mode 100644 examples/llama.android/app/src/main/java/com/example/llama/util/TableUtils.kt diff --git a/examples/llama.android/app/src/main/java/com/example/llama/util/FormatUtils.kt b/examples/llama.android/app/src/main/java/com/example/llama/util/FormatUtils.kt index 08077e3a6e..6527fa365a 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/util/FormatUtils.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/util/FormatUtils.kt @@ -46,83 +46,3 @@ fun formatContextLength(contextLength: Int): String { else -> contextLength.toString() } } - - -/** - * Maps ISO 639-1 language codes into ISO 3166-1 alpha-2 country codes, and then map to Emoji. - * - */ -fun languageCodeToFlagEmoji(languageCode: String): String? { - val countryCode = LANGUAGE_TO_COUNTRY[languageCode.lowercase()] ?: return null - - return countryCodeToFlagEmoji(countryCode) -} - -/** - * Formats ISO 3166-1 alpha-2 country code into corresponding Emoji. - */ -private fun countryCodeToFlagEmoji(countryCode: String): String? { - if (countryCode.length != 2) return null - - // Convert each character to a Regional Indicator Symbol - val firstChar = Character.codePointAt(countryCode.uppercase(), 0) - 'A'.code + 0x1F1E6 - val secondChar = Character.codePointAt(countryCode.uppercase(), 1) - 'A'.code + 0x1F1E6 - - return String(Character.toChars(firstChar)) + String(Character.toChars(secondChar)) -} - -private val LANGUAGE_TO_COUNTRY by lazy { - mapOf( - "af" to "ZA", - "am" to "ET", - "ar" to "SA", - "bg" to "BG", - "bn" to "BD", - "cs" to "CZ", - "da" to "DK", - "de" to "DE", - "el" to "GR", - "en" to "US", - "es" to "ES", - "et" to "EE", - "fa" to "IR", - "fi" to "FI", - "fil" to "PH", - "fr" to "FR", - "he" to "IL", - "hi" to "IN", - "hr" to "HR", - "hu" to "HU", - "id" to "ID", - "it" to "IT", - "ja" to "JP", - "kn" to "IN", - "ko" to "KR", - "lt" to "LT", - "lv" to "LV", - "ml" to "IN", - "mr" to "IN", - "ms" to "MY", - "nl" to "NL", - "no" to "NO", - "pa" to "IN", - "pl" to "PL", - "pt" to "PT", - "ro" to "RO", - "ru" to "RU", - "sk" to "SK", - "sl" to "SI", - "sr" to "RS", - "sv" to "SE", - "sw" to "KE", - "ta" to "LK", - "te" to "IN", - "th" to "TH", - "tr" to "TR", - "uk" to "UA", - "ur" to "PK", - "vi" to "VN", - "zh" to "CN", - "zu" to "ZA", - ) -} diff --git a/examples/llama.android/app/src/main/java/com/example/llama/util/LocaleUtils.kt b/examples/llama.android/app/src/main/java/com/example/llama/util/LocaleUtils.kt new file mode 100644 index 0000000000..a3226c4197 --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/util/LocaleUtils.kt @@ -0,0 +1,81 @@ +package com.example.llama.util + + +/** + * Maps ISO 639-1 language codes into ISO 3166-1 alpha-2 country codes, and then map to Emoji. + * + */ +fun languageCodeToFlagEmoji(languageCode: String): String? { + val countryCode = LANGUAGE_TO_COUNTRY[languageCode.lowercase()] ?: return null + + return countryCodeToFlagEmoji(countryCode) +} + +/** + * Formats ISO 3166-1 alpha-2 country code into corresponding Emoji. + */ +private fun countryCodeToFlagEmoji(countryCode: String): String? { + if (countryCode.length != 2) return null + + // Convert each character to a Regional Indicator Symbol + val firstChar = Character.codePointAt(countryCode.uppercase(), 0) - 'A'.code + 0x1F1E6 + val secondChar = Character.codePointAt(countryCode.uppercase(), 1) - 'A'.code + 0x1F1E6 + + return String(Character.toChars(firstChar)) + String(Character.toChars(secondChar)) +} + +private val LANGUAGE_TO_COUNTRY by lazy { + mapOf( + "af" to "ZA", + "am" to "ET", + "ar" to "SA", + "bg" to "BG", + "bn" to "BD", + "cs" to "CZ", + "da" to "DK", + "de" to "DE", + "el" to "GR", + "en" to "US", + "es" to "ES", + "et" to "EE", + "fa" to "IR", + "fi" to "FI", + "fil" to "PH", + "fr" to "FR", + "he" to "IL", + "hi" to "IN", + "hr" to "HR", + "hu" to "HU", + "id" to "ID", + "it" to "IT", + "ja" to "JP", + "kn" to "IN", + "ko" to "KR", + "lt" to "LT", + "lv" to "LV", + "ml" to "IN", + "mr" to "IN", + "ms" to "MY", + "nl" to "NL", + "no" to "NO", + "pa" to "IN", + "pl" to "PL", + "pt" to "PT", + "ro" to "RO", + "ru" to "RU", + "sk" to "SK", + "sl" to "SI", + "sr" to "RS", + "sv" to "SE", + "sw" to "KE", + "ta" to "LK", + "te" to "IN", + "th" to "TH", + "tr" to "TR", + "uk" to "UA", + "ur" to "PK", + "vi" to "VN", + "zh" to "CN", + "zu" to "ZA", + ) +} diff --git a/examples/llama.android/app/src/main/java/com/example/llama/util/TableUtils.kt b/examples/llama.android/app/src/main/java/com/example/llama/util/TableUtils.kt new file mode 100644 index 0000000000..69aa77008f --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/util/TableUtils.kt @@ -0,0 +1,38 @@ +package com.example.llama.util + + +/** + * A basic + */ +data class MarkdownTableData( + val headers: List, + val rows: List> +) { + val columnCount: Int get() = headers.size + val rowCount: Int get() = rows.size +} + +/** + * Formats llama-bench's markdown output into structured [MarkdownTableData] + */ +fun parseMarkdownTableFiltered( + markdown: String, + keepColumns: Set +): MarkdownTableData { + val lines = markdown.trim().lines().filter { it.startsWith("|") } + if (lines.size < 2) return MarkdownTableData(emptyList(), emptyList()) + + val rawHeaders = lines[0].split("|").map { it.trim() }.filter { it.isNotEmpty() } + val keepIndices = rawHeaders.mapIndexedNotNull { index, name -> + if (name in keepColumns) index else null + } + + val headers = keepIndices.map { rawHeaders[it] } + + val rows = lines.drop(2).map { line -> + val cells = line.split("|").map { it.trim() }.filter { it.isNotEmpty() } + keepIndices.map { cells.getOrElse(it) { "" } } + } + + return MarkdownTableData(headers, rows) +}