From a0def2969ce8ddda241c92dfeaa316e51fcfbb51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=A4fer?= Date: Sun, 15 May 2022 16:57:24 +0200 Subject: [PATCH] Add special error message for lang file decoding edge case, improve lang file encoder constructor --- .../gradle/plugin/i18n/io/LangFileDecoder.kt | 7 +++++-- .../gradle/plugin/i18n/io/LangFileEncoder.kt | 21 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileDecoder.kt b/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileDecoder.kt index ca2dcc69..d5a9edb9 100644 --- a/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileDecoder.kt +++ b/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileDecoder.kt @@ -7,9 +7,9 @@ package org.openstreetmap.josm.gradle.plugin.i18n.io public class LangFileDecoder(baseLanguageBytes: ByteArray): I18nFileDecoder { public companion object { - private const val ERROR_MESSAGE_NONBASE_UNTRANSLATED = "This is not a base language!" + + private const val ERROR_MESSAGE_NONBASE_UNTRANSLATED = "This is not a base language! " + "There is a string in this supposed base language that indicates it is not translated from the base language (which does not make sense)." - private const val ERROR_MESSAGE_NONBASE_SAME_AS_BASE = "This is not a base language!" + + private const val ERROR_MESSAGE_NONBASE_SAME_AS_BASE = "This is not a base language! " + "There is a string in this supposed base language that indicates it is the same as in the base language (which does not make sense)." /** @@ -102,6 +102,9 @@ public class LangFileDecoder(baseLanguageBytes: ByteArray): I18nFileDecoder { resultList.add(transformToListElement(currentBaseString, null)) } else -> { + require(currentByteIndex + stringLength <= bytes.size) { + "String length is too long! $stringLength bytes starting at offset $currentByteIndex does not fit in a file of ${bytes.size} bytes" + } val msgstr = MsgStr(bytes.sliceArray(currentByteIndex until currentByteIndex + stringLength).decodeToString()) if (currentBaseString == null) { resultList.add(transformToListElement(MsgId(msgstr), null)) diff --git a/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileEncoder.kt b/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileEncoder.kt index da9f40fe..0bead247 100644 --- a/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileEncoder.kt +++ b/i18n/src/commonMain/kotlin/org/openstreetmap/josm/gradle/plugin/i18n/io/LangFileEncoder.kt @@ -4,7 +4,11 @@ package org.openstreetmap.josm.gradle.plugin.i18n.io * Encoder for the *.lang file format used by JOSM. * @param baseMsgIds the [MsgId]s in the base language for which translations will be encoded */ -public class LangFileEncoder(baseMsgIds: List): I18nFileEncoder { +public class LangFileEncoder( + singularMsgIds: List, + private val pluralMsgIds: List +): I18nFileEncoder { + private val singularMsgIds = singularMsgIds.minus(GETTEXT_HEADER_MSGID) public companion object { /** * The language code `en` for English, which is considered to be the base language by default, @@ -15,14 +19,13 @@ public class LangFileEncoder(baseMsgIds: List): I18nFileEncoder { private val SINGULAR_PLURAL_SEPARATOR: List = listOf(0xFF, 0xFF).map { it.toByte() } } - private val singularMsgIds: List - private val pluralMsgIds: List - - init { - val baseMsgIdPartitions = baseMsgIds.distinct().minus(GETTEXT_HEADER_MSGID).sorted().partition { it.id.strings.size <= 1 } - singularMsgIds = baseMsgIdPartitions.first - pluralMsgIds = baseMsgIdPartitions.second - } + private constructor(singularPluralMsgIds: Pair, List>): this( + singularPluralMsgIds.first, + singularPluralMsgIds.second + ) + public constructor(baseMsgIds: Collection): this( + baseMsgIds.distinct().sorted().partition { it.id.strings.size <= 1 } + ) /** * Converts the [MsgId]s of the base language (see constructor parameter) to the *.lang file format.