Optimize Name::cutFirst and Name::cutLast

This commit is contained in:
Alexander Nozik 2023-02-18 19:50:09 +03:00
parent 7d88f828d7
commit f83b759e75
2 changed files with 16 additions and 4 deletions

View File

@ -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

View File

@ -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())
}
}