Skip to content

Commit ecacc4d

Browse files
authored
Add contains operator to check if codepoint belongs to specified group (#4)
1 parent 35eb0fd commit ecacc4d

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

Diff for: library/src/commonMain/kotlin/io/github/optimumcode/karacteristics/extensions.kt

+9
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@ public val CodePoint.category: CodepointCategory
1212
return CodepointCategory.entries.first { it.characterData.contains(this) }
1313
}
1414

15+
public operator fun CodepointCategory.contains(codepoint: CodePoint): Boolean = characterData.contains(codepoint)
16+
1517
public val CodePoint.bidirectionalClass: CodepointBidirectionalClass
1618
get() {
1719
requireCodepointInRange()
1820
return CodepointBidirectionalClass.entries.first { it.characterData.contains(this) }
1921
}
2022

23+
public operator fun CodepointBidirectionalClass.contains(codepoint: CodePoint): Boolean =
24+
characterData.contains(codepoint)
25+
2126
public val CodePoint.joiningType: CodepointJoiningType
2227
get() {
2328
requireCodepointInRange()
2429
return CodepointJoiningType.entries.first { it.contains(this) }
2530
}
2631

32+
public operator fun CodepointJoiningType.contains(codepoint: CodePoint): Boolean = contains(codepoint)
33+
2734
public val CodePoint.derivedProperty: CodepointDerivedProperty
2835
get() {
2936
requireCodepointInRange()
3037
return CodepointDerivedProperty.entries.first { it.contains(this) }
3138
}
3239

40+
public operator fun CodepointDerivedProperty.contains(codepoint: CodePoint): Boolean = contains(codepoint)
41+
3342
private fun CodePoint.requireCodepointInRange() {
3443
require(this in 0..MAX_CODE_POINT) {
3544
"code point must be in [0, 0x${MAX_CODE_POINT.toString(16).uppercase()}]"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.optimumcode.karacteristics.test.extension
2+
3+
import io.github.optimumcode.karacteristics.CodepointBidirectionalClass
4+
import io.github.optimumcode.karacteristics.CodepointCategory
5+
import io.github.optimumcode.karacteristics.CodepointDerivedProperty
6+
import io.github.optimumcode.karacteristics.CodepointJoiningType
7+
import io.github.optimumcode.karacteristics.bidirectionalClass
8+
import io.github.optimumcode.karacteristics.category
9+
import io.github.optimumcode.karacteristics.contains
10+
import io.github.optimumcode.karacteristics.derivedProperty
11+
import io.github.optimumcode.karacteristics.joiningType
12+
import io.kotest.assertions.asClue
13+
import io.kotest.core.spec.style.FunSpec
14+
import io.kotest.matchers.shouldBe
15+
16+
class ContainsTest :
17+
FunSpec(
18+
{
19+
val codepoint = 0x20
20+
test("category contains") {
21+
codepoint.category.asClue {
22+
(codepoint in CodepointCategory.SPACE_SEPARATOR) shouldBe true
23+
}
24+
}
25+
26+
test("bidirectional class contains") {
27+
codepoint.bidirectionalClass.asClue {
28+
(codepoint in CodepointBidirectionalClass.WHITE_SPACE) shouldBe true
29+
}
30+
}
31+
32+
test("joining type contains") {
33+
codepoint.joiningType.asClue {
34+
(codepoint in CodepointJoiningType.NON_JOINING) shouldBe true
35+
}
36+
}
37+
38+
test("derived property contains") {
39+
codepoint.derivedProperty.asClue {
40+
(codepoint in CodepointDerivedProperty.DISALLOWED) shouldBe true
41+
}
42+
}
43+
},
44+
)

Diff for: library/src/nonWasmWasiTest/kotlin/io/github/optimumcode/karacteristics/test/extension/NegativeTest.kt

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
package io.github.optimumcode.karacteristics
1+
package io.github.optimumcode.karacteristics.test.extension
22

3+
import io.github.optimumcode.karacteristics.CodePoint
4+
import io.github.optimumcode.karacteristics.CodepointBidirectionalClass
5+
import io.github.optimumcode.karacteristics.CodepointCategory
6+
import io.github.optimumcode.karacteristics.CodepointDerivedProperty
7+
import io.github.optimumcode.karacteristics.CodepointJoiningType
8+
import io.github.optimumcode.karacteristics.bidirectionalClass
9+
import io.github.optimumcode.karacteristics.category
10+
import io.github.optimumcode.karacteristics.contains
11+
import io.github.optimumcode.karacteristics.derivedProperty
12+
import io.github.optimumcode.karacteristics.joiningType
13+
import io.kotest.assertions.asClue
314
import io.kotest.assertions.throwables.shouldThrow
415
import io.kotest.core.spec.style.FunSpec
516
import io.kotest.matchers.shouldBe
@@ -24,5 +35,30 @@ class NegativeTest :
2435
}.message shouldBe "code point must be in [0, 0x10FFFF]"
2536
}
2637
}
38+
39+
val codepoint = 0x20
40+
test("category not contains") {
41+
codepoint.category.asClue {
42+
(codepoint in CodepointCategory.SPACING_MARK) shouldBe false
43+
}
44+
}
45+
46+
test("bidirectional class not contains") {
47+
codepoint.bidirectionalClass.asClue {
48+
(codepoint in CodepointBidirectionalClass.ARABIC_LETTER) shouldBe false
49+
}
50+
}
51+
52+
test("joining type not contains") {
53+
codepoint.joiningType.asClue {
54+
(codepoint in CodepointJoiningType.DUAL_JOINING) shouldBe false
55+
}
56+
}
57+
58+
test("derived property not contains") {
59+
codepoint.derivedProperty.asClue {
60+
(codepoint in CodepointDerivedProperty.UNASSIGNED) shouldBe false
61+
}
62+
}
2763
},
2864
)

0 commit comments

Comments
 (0)