From f83b759e7590286608bf65e496128153baf38857 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 18 Feb 2023 19:50:09 +0300 Subject: [PATCH] Optimize Name::cutFirst and Name::cutLast --- .../space/kscience/dataforge/names/Name.kt | 4 ++-- .../space/kscience/dataforge/names/NameTest.kt | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/Name.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/Name.kt index 2bad537f..c24cf58d 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/Name.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/Name.kt @@ -120,12 +120,12 @@ public operator fun Name.get(i: Int): NameToken = tokens[i] /** * The reminder of the name after last element is cut. For empty name return itself. */ -public fun Name.cutLast(): Name = Name(tokens.dropLast(1)) +public fun Name.cutLast(): Name = Name(tokens.subList(0, tokens.size - 1)) /** * The reminder of the name after first element is cut. For empty name return itself. */ -public fun Name.cutFirst(): Name = Name(tokens.drop(1)) +public fun Name.cutFirst(): Name = Name(tokens.subList(1, tokens.size)) public val Name.length: Int get() = tokens.size diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt index decc1b3a..a5bdf3fc 100644 --- a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt @@ -20,7 +20,7 @@ class NameTest { } @Test - fun comparisonTest(){ + fun comparisonTest() { val name1 = Name.parse("token1.token2.token3") val name2 = Name.parse("token1.token2") val name3 = Name.parse("token3") @@ -30,7 +30,7 @@ class NameTest { } @Test - fun escapeTest(){ + fun escapeTest() { val escapedName = Name.parse("token\\.one.token2") val unescapedName = "token\\.one.token2".asName() @@ -38,4 +38,16 @@ class NameTest { assertEquals(1, unescapedName.length) assertEquals(escapedName, Name.parse(escapedName.toString())) } + + @Test + fun cutFirst() { + val name = Name.parse("a.b.c") + assertEquals("b.c".parseAsName(), name.cutFirst()) + } + + @Test + fun cutLast() { + val name = Name.parse("a.b.c") + assertEquals("a.b".parseAsName(), name.cutLast()) + } } \ No newline at end of file