regex: Refactor predefined character classes.

This commit is contained in:
Ilya Matveev
2017-06-16 16:19:11 +07:00
committed by ilmat192
parent 9c81f9b402
commit bfcc0fc0d0
3 changed files with 304 additions and 356 deletions
@@ -9,21 +9,15 @@ fun codePointToString(codePoint: Int): String {
return fromCharArray(charArray, 0, charArray.size) return fromCharArray(charArray, 0, charArray.size)
} }
fun box() {}
// TODO: Here is a performance problem: an execution of this test requires much more time than it in Kotlin/JVM. // TODO: Here is a performance problem: an execution of this test requires much more time than it in Kotlin/JVM.
fun box1() { fun box() {
// Regression for HARMONY-3145 // Regression for HARMONY-3145
var p = Regex("(\\p{all})+") var p = Regex("(\\p{all})+")
var res = true var res = true
var cnt = 0 var cnt = 0
var s: String var s: String
for (i in 0..1114111) { for (i in 0..1114111) {
if (i % 200000 == 0) {
println(i)
}
s = codePointToString(i) s = codePointToString(i)
// if (!s.matches(p.toString().toRegex())) { TODO: Uncomment when caching is done.
if (!s.matches(p)) { if (!s.matches(p)) {
cnt++ cnt++
res = false res = false
@@ -37,11 +31,7 @@ fun box1() {
cnt = 0 cnt = 0
for (i in 0..1114111) { for (i in 0..1114111) {
if (i % 200000 == 0) {
println(i)
}
s = codePointToString(i) s = codePointToString(i)
// if (!s.matches(p.toString().toRegex())) { TODO: Uncomment when caching is done.
if (!s.matches(p)) { if (!s.matches(p)) {
cnt++ cnt++
res = false res = false
@@ -65,13 +65,12 @@ internal abstract class AbstractCharClass : SpecialToken() {
* Returns BitSet representing this character class or `null` * Returns BitSet representing this character class or `null`
* if this character class does not have character representation; * if this character class does not have character representation;
*/ */
// TODO: @C++?. Or implement a BitSet
open internal val bits: BitSet? open internal val bits: BitSet?
get() = null get() = null
fun hasLowHighSurrogates(): Boolean { fun hasLowHighSurrogates(): Boolean {
return if (altSurrogates) return if (altSurrogates)
lowHighSurrogates.nextClearBit(0) != -1 // TODO: What if the bitset is empty? lowHighSurrogates.nextClearBit(0) != -1
else else
lowHighSurrogates.nextSetBit(0) != -1 lowHighSurrogates.nextSetBit(0) != -1
} }
@@ -81,7 +80,6 @@ internal abstract class AbstractCharClass : SpecialToken() {
open val instance: AbstractCharClass open val instance: AbstractCharClass
get() = this get() = this
// TODO: refactor
val surrogates: AbstractCharClass by lazy { val surrogates: AbstractCharClass by lazy {
val result = object : AbstractCharClass() { val result = object : AbstractCharClass() {
override fun contains(ch: Int): Boolean { override fun contains(ch: Int): Boolean {
@@ -98,7 +96,6 @@ internal abstract class AbstractCharClass : SpecialToken() {
} }
// TODO: refactor
val withoutSurrogates: AbstractCharClass by lazy { val withoutSurrogates: AbstractCharClass by lazy {
val result = object : AbstractCharClass() { val result = object : AbstractCharClass() {
override fun contains(ch: Int): Boolean { override fun contains(ch: Int): Boolean {
@@ -109,7 +106,6 @@ internal abstract class AbstractCharClass : SpecialToken() {
else else
false false
return this@AbstractCharClass.contains(ch) && !containslHS return this@AbstractCharClass.contains(ch) && !containslHS
} }
} }
@@ -125,7 +121,6 @@ internal abstract class AbstractCharClass : SpecialToken() {
* Although this method will not alternate all the already set characters, * Although this method will not alternate all the already set characters,
* just overall meaning of the class. * just overall meaning of the class.
*/ */
// TODO: replace with a property
fun setNegative(value: Boolean): AbstractCharClass { fun setNegative(value: Boolean): AbstractCharClass {
if (alt xor value) { if (alt xor value) {
alt = !alt alt = !alt
@@ -141,8 +136,7 @@ internal abstract class AbstractCharClass : SpecialToken() {
return alt return alt
} }
// TODO: replace getValue with property access? internal abstract class CachedCharClass {
internal abstract class LazyCharClass {
private val posValue: AbstractCharClass by lazy { computeValue() } private val posValue: AbstractCharClass by lazy { computeValue() }
private val negValue: AbstractCharClass by lazy { computeValue().setNegative(true) } private val negValue: AbstractCharClass by lazy { computeValue().setNegative(true) }
@@ -150,147 +144,113 @@ internal abstract class AbstractCharClass : SpecialToken() {
protected abstract fun computeValue(): AbstractCharClass protected abstract fun computeValue(): AbstractCharClass
} }
internal class LazyDigit : LazyCharClass() { internal class CachedDigit : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add('0', '9')
return CharClass().add('0', '9')
}
} }
// TODO: DO we need it? But we have LazyCharCLass.negValue. May be it's connected with mayContainSupplCodepoints? internal class CachedNonDigit : CachedCharClass() {
// TODO: Don't use bitmaps in preset classes? override fun computeValue(): AbstractCharClass =
internal class LazyNonDigit : LazyCharClass() { CharClass().add('0', '9').setNegative(true).apply { mayContainSupplCodepoints = true }
override fun computeValue(): AbstractCharClass {
val result = CharClass().add('0', '9').setNegative(true)
result.mayContainSupplCodepoints = true
return result
}
} }
internal class LazySpace : LazyCharClass() { internal class CachedSpace : CachedCharClass() {
override fun computeValue(): AbstractCharClass {
/* 9-13 - \t\n\x0B\f\r; 32 - ' ' */ /* 9-13 - \t\n\x0B\f\r; 32 - ' ' */
return CharClass().add(9, 13).add(32) override fun computeValue(): AbstractCharClass = CharClass().add(9, 13).add(32)
}
} }
internal class LazyNonSpace : LazyCharClass() { internal class CachedNonSpace : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass =
val result = LazySpace().getValue(negative = true) CachedSpace().getValue(negative = true).apply { mayContainSupplCodepoints = true }
result.mayContainSupplCodepoints = true
return result
}
} }
internal class LazyWord : LazyCharClass() { internal class CachedWord : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add('a', 'z').add('A', 'Z').add('0', '9').add('_')
return CharClass().add('a', 'z').add('A', 'Z').add('0', '9')
.add('_')
}
} }
internal class LazyNonWord : LazyCharClass() { internal class CachedNonWord : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass =
val result = LazyWord().getValue(negative = true) CachedWord().getValue(negative = true).apply { mayContainSupplCodepoints = true }
result.mayContainSupplCodepoints = true
return result
}
} }
// TODO: It is only for latin in Harmony. internal class CachedLower : CachedCharClass() {
internal class LazyLower : LazyCharClass() { override fun computeValue(): AbstractCharClass = CharClass().add('a', 'z')
override fun computeValue(): AbstractCharClass {
return CharClass().add('a', 'z')
}
} }
internal class LazyUpper : LazyCharClass() { internal class CachedUpper : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add('A', 'Z')
return CharClass().add('A', 'Z')
}
} }
internal class LazyASCII : LazyCharClass() { internal class CachedASCII : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add(0x00, 0x7F)
return CharClass().add(0x00, 0x7F)
}
} }
internal class LazyAlpha : LazyCharClass() { internal class CachedAlpha : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add('a', 'z').add('A', 'Z')
return CharClass().add('a', 'z').add('A', 'Z')
}
} }
internal class LazyAlnum : LazyCharClass() { internal class CachedAlnum : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass =
// TODO: Get rid of the cast? (CachedAlpha().getValue(negative = false) as CharClass).add('0', '9')
return (LazyAlpha().getValue(negative = false) as CharClass).add('0', '9')
}
} }
internal class LazyPunct : LazyCharClass() { internal class CachedPunct : CachedCharClass() {
override fun computeValue(): AbstractCharClass {
/* Punctuation !"#$%&'()*+,-./:;<=>?@ [\]^_` {|}~ */ /* Punctuation !"#$%&'()*+,-./:;<=>?@ [\]^_` {|}~ */
return CharClass().add(0x21, 0x40).add(0x5B, 0x60).add(0x7B, override fun computeValue(): AbstractCharClass = CharClass().add(0x21, 0x40).add(0x5B, 0x60).add(0x7B, 0x7E)
0x7E)
}
} }
internal class LazyGraph : LazyCharClass() { internal class CachedGraph : CachedCharClass() {
override fun computeValue(): AbstractCharClass {
/* plus punctuation */ /* plus punctuation */
return (LazyAlnum().getValue(negative = false) as CharClass) override fun computeValue(): AbstractCharClass =
(CachedAlnum().getValue(negative = false) as CharClass)
.add(0x21, 0x40) .add(0x21, 0x40)
.add(0x5B, 0x60) .add(0x5B, 0x60)
.add(0x7B, 0x7E) .add(0x7B, 0x7E)
} }
internal class CachedPrint : CachedCharClass() {
override fun computeValue(): AbstractCharClass =
(CachedGraph().getValue(negative = true) as CharClass).add(0x20)
} }
internal class LazyPrint : LazyCharClass() { internal class CachedBlank : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass = CharClass().add(' ').add('\t')
return (LazyGraph().getValue(negative = true) as CharClass).add(0x20) }
internal class CachedCntrl : CachedCharClass() {
override fun computeValue(): AbstractCharClass = CharClass().add(0x00, 0x1F).add(0x7F)
}
internal class CachedXDigit : CachedCharClass() {
override fun computeValue(): AbstractCharClass = CharClass().add('0', '9').add('a', 'f').add('A', 'F')
}
internal class CachedRange(var start: Int, var end: Int) : CachedCharClass() {
override fun computeValue(): AbstractCharClass =
object: AbstractCharClass() {
override fun contains(ch: Int): Boolean = alt xor (ch in start..end)
}.apply {
if (end >= Char.MIN_SUPPLEMENTARY_CODE_POINT) {
mayContainSupplCodepoints = true
}
val minSurrogate = Char.MIN_SURROGATE.toInt()
val maxSurrogate = Char.MAX_SURROGATE.toInt()
// There is an intersection with surrogate characters.
if (end >= minSurrogate && start <= maxSurrogate && start <= end) {
val surrogatesStart = maxOf(start, minSurrogate) - minSurrogate
val surrogatesEnd = minOf(end, maxSurrogate) - minSurrogate
lowHighSurrogates.set(surrogatesStart..surrogatesEnd)
}
} }
} }
internal class LazyBlank : LazyCharClass() { internal class CachedSpecialsBlock : CachedCharClass() {
override fun computeValue(): AbstractCharClass { public override fun computeValue(): AbstractCharClass = CharClass().add(0xFEFF, 0xFEFF).add(0xFFF0, 0xFFFD)
return CharClass().add(' ').add('\t')
}
} }
internal class LazyCntrl : LazyCharClass() { internal class CachedCategoryScope(
override fun computeValue(): AbstractCharClass {
return CharClass().add(0x00, 0x1F).add(0x7F)
}
}
internal class LazyXDigit : LazyCharClass() {
override fun computeValue(): AbstractCharClass {
return CharClass().add('0', '9').add('a', 'f').add('A', 'F')
}
}
internal class LazyRange(var start: Int, var end: Int) : LazyCharClass() {
public override fun computeValue(): AbstractCharClass {
val chCl = CharClass().add(start, end)
return chCl
}
}
internal class LazySpecialsBlock : LazyCharClass() {
public override fun computeValue(): AbstractCharClass {
return CharClass().add(0xFEFF, 0xFEFF).add(0xFFF0, 0xFFFD)
}
}
internal class LazyCategoryScope(
val category: Int, val category: Int,
val mayContainSupplCodepoints: Boolean, val mayContainSupplCodepoints: Boolean,
val containsAllSurrogates: Boolean = false) : LazyCharClass() { val containsAllSurrogates: Boolean = false) : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass {
val result = UnicodeCategoryScope(category) val result = UnicodeCategoryScope(category)
@@ -303,10 +263,10 @@ internal abstract class AbstractCharClass : SpecialToken() {
} }
} }
internal class LazyCategory( internal class CachedCategory(
val category: Int, val category: Int,
val mayContainSupplCodepoints: Boolean, val mayContainSupplCodepoints: Boolean,
val containsAllSurrogates: Boolean = false) : LazyCharClass() { val containsAllSurrogates: Boolean = false) : CachedCharClass() {
override fun computeValue(): AbstractCharClass { override fun computeValue(): AbstractCharClass {
val result = UnicodeCategory(category) val result = UnicodeCategory(category)
@@ -318,236 +278,229 @@ internal abstract class AbstractCharClass : SpecialToken() {
} }
} }
// -----------------------------------------------------------------
// Static methods and predefined classes
// -----------------------------------------------------------------
companion object { companion object {
//Char.MAX_SURROGATE - Char.MIN_SURROGATE + 1 //Char.MAX_SURROGATE - Char.MIN_SURROGATE + 1
var SURROGATE_CARDINALITY = 2048 const val SURROGATE_CARDINALITY = 2048
var space: LazyCharClass = LazySpace() private var classCache: MutableMap<String, CachedCharClass>? = null
var digit: LazyCharClass = LazyDigit()
/** /**
* character classes generated from * Character classes.
* http://www.unicode.org/reports/tr18/ * See http://www.unicode.org/reports/tr18/, http://www.unicode.org/Public/4.1.0/ucd/Blocks.txt
* http://www.unicode.org/Public/4.1.0/ucd/Blocks.txt
*/ */
// TODO: @C++ Harmony uses ListResourceBundle class here. TODO: perfrom a effector lookup on the C++ side. // TODO: Make a faster implementation.
// Temporary solution: when fun createClass(name: String) : CachedCharClass =
// TODO: @C++. Or make it in code. Or do something else. when (name) {
// We can lazily create objects of char classes in C++ hashmap or Array. "Lower" -> CachedLower()
// The main point of it is laziness "Upper" -> CachedUpper()
fun getClass(name: String) : LazyCharClass { "ASCII" -> CachedASCII()
"Alpha" -> CachedAlpha()
return when (name) { "Digit" -> CachedDigit()
"Alnum" -> CachedAlnum()
"Lower" -> LazyLower() "Punct" -> CachedPunct()
"Upper" -> LazyUpper() "Graph" -> CachedGraph()
"ASCII" -> LazyASCII() "Print" -> CachedPrint()
"Alpha" -> LazyAlpha() "Blank" -> CachedBlank()
"Digit" -> digit "Cntrl" -> CachedCntrl()
"Alnum" -> LazyAlnum() "XDigit" -> CachedXDigit()
"Punct" -> LazyPunct() "Space" -> CachedSpace()
"Graph" -> LazyGraph() "w" -> CachedWord()
"Print" -> LazyPrint() "W" -> CachedNonWord()
"Blank" -> LazyBlank() "s" -> CachedSpace()
"Cntrl" -> LazyCntrl() "S" -> CachedNonSpace()
"XDigit" -> LazyXDigit() "d" -> CachedDigit()
"Space" -> space "D" -> CachedNonDigit()
"w" -> LazyWord() "BasicLatin" -> CachedRange(0x0000, 0x007F)
"W" -> LazyNonWord() "Latin-1Supplement" -> CachedRange(0x0080, 0x00FF)
"s" -> space "LatinExtended-A" -> CachedRange(0x0100, 0x017F)
"S" -> LazyNonSpace() "LatinExtended-B" -> CachedRange(0x0180, 0x024F)
"d" -> digit "IPAExtensions" -> CachedRange(0x0250, 0x02AF)
"D" -> LazyNonDigit() "SpacingModifierLetters" -> CachedRange(0x02B0, 0x02FF)
"BasicLatin" -> LazyRange(0x0000, 0x007F) "CombiningDiacriticalMarks" -> CachedRange(0x0300, 0x036F)
"Latin-1Supplement" -> LazyRange(0x0080, 0x00FF) "Greek" -> CachedRange(0x0370, 0x03FF)
"LatinExtended-A" -> LazyRange(0x0100, 0x017F) "Cyrillic" -> CachedRange(0x0400, 0x04FF)
"LatinExtended-B" -> LazyRange(0x0180, 0x024F) "CyrillicSupplement" -> CachedRange(0x0500, 0x052F)
"IPAExtensions" -> LazyRange(0x0250, 0x02AF) "Armenian" -> CachedRange(0x0530, 0x058F)
"SpacingModifierLetters" -> LazyRange(0x02B0, 0x02FF) "Hebrew" -> CachedRange(0x0590, 0x05FF)
"CombiningDiacriticalMarks" -> LazyRange(0x0300, 0x036F) "Arabic" -> CachedRange(0x0600, 0x06FF)
"Greek" -> LazyRange(0x0370, 0x03FF) "Syriac" -> CachedRange(0x0700, 0x074F)
"Cyrillic" -> LazyRange(0x0400, 0x04FF) "ArabicSupplement" -> CachedRange(0x0750, 0x077F)
"CyrillicSupplement" -> LazyRange(0x0500, 0x052F) "Thaana" -> CachedRange(0x0780, 0x07BF)
"Armenian" -> LazyRange(0x0530, 0x058F) "Devanagari" -> CachedRange(0x0900, 0x097F)
"Hebrew" -> LazyRange(0x0590, 0x05FF) "Bengali" -> CachedRange(0x0980, 0x09FF)
"Arabic" -> LazyRange(0x0600, 0x06FF) "Gurmukhi" -> CachedRange(0x0A00, 0x0A7F)
"Syriac" -> LazyRange(0x0700, 0x074F) "Gujarati" -> CachedRange(0x0A80, 0x0AFF)
"ArabicSupplement" -> LazyRange(0x0750, 0x077F) "Oriya" -> CachedRange(0x0B00, 0x0B7F)
"Thaana" -> LazyRange(0x0780, 0x07BF) "Tamil" -> CachedRange(0x0B80, 0x0BFF)
"Devanagari" -> LazyRange(0x0900, 0x097F) "Telugu" -> CachedRange(0x0C00, 0x0C7F)
"Bengali" -> LazyRange(0x0980, 0x09FF) "Kannada" -> CachedRange(0x0C80, 0x0CFF)
"Gurmukhi" -> LazyRange(0x0A00, 0x0A7F) "Malayalam" -> CachedRange(0x0D00, 0x0D7F)
"Gujarati" -> LazyRange(0x0A80, 0x0AFF) "Sinhala" -> CachedRange(0x0D80, 0x0DFF)
"Oriya" -> LazyRange(0x0B00, 0x0B7F) "Thai" -> CachedRange(0x0E00, 0x0E7F)
"Tamil" -> LazyRange(0x0B80, 0x0BFF) "Lao" -> CachedRange(0x0E80, 0x0EFF)
"Telugu" -> LazyRange(0x0C00, 0x0C7F) "Tibetan" -> CachedRange(0x0F00, 0x0FFF)
"Kannada" -> LazyRange(0x0C80, 0x0CFF) "Myanmar" -> CachedRange(0x1000, 0x109F)
"Malayalam" -> LazyRange(0x0D00, 0x0D7F) "Georgian" -> CachedRange(0x10A0, 0x10FF)
"Sinhala" -> LazyRange(0x0D80, 0x0DFF) "HangulJamo" -> CachedRange(0x1100, 0x11FF)
"Thai" -> LazyRange(0x0E00, 0x0E7F) "Ethiopic" -> CachedRange(0x1200, 0x137F)
"Lao" -> LazyRange(0x0E80, 0x0EFF) "EthiopicSupplement" -> CachedRange(0x1380, 0x139F)
"Tibetan" -> LazyRange(0x0F00, 0x0FFF) "Cherokee" -> CachedRange(0x13A0, 0x13FF)
"Myanmar" -> LazyRange(0x1000, 0x109F) "UnifiedCanadianAboriginalSyllabics" -> CachedRange(0x1400, 0x167F)
"Georgian" -> LazyRange(0x10A0, 0x10FF) "Ogham" -> CachedRange(0x1680, 0x169F)
"HangulJamo" -> LazyRange(0x1100, 0x11FF) "Runic" -> CachedRange(0x16A0, 0x16FF)
"Ethiopic" -> LazyRange(0x1200, 0x137F) "Tagalog" -> CachedRange(0x1700, 0x171F)
"EthiopicSupplement" -> LazyRange(0x1380, 0x139F) "Hanunoo" -> CachedRange(0x1720, 0x173F)
"Cherokee" -> LazyRange(0x13A0, 0x13FF) "Buhid" -> CachedRange(0x1740, 0x175F)
"UnifiedCanadianAboriginalSyllabics" -> LazyRange(0x1400, 0x167F) "Tagbanwa" -> CachedRange(0x1760, 0x177F)
"Ogham" -> LazyRange(0x1680, 0x169F) "Khmer" -> CachedRange(0x1780, 0x17FF)
"Runic" -> LazyRange(0x16A0, 0x16FF) "Mongolian" -> CachedRange(0x1800, 0x18AF)
"Tagalog" -> LazyRange(0x1700, 0x171F) "Limbu" -> CachedRange(0x1900, 0x194F)
"Hanunoo" -> LazyRange(0x1720, 0x173F) "TaiLe" -> CachedRange(0x1950, 0x197F)
"Buhid" -> LazyRange(0x1740, 0x175F) "NewTaiLue" -> CachedRange(0x1980, 0x19DF)
"Tagbanwa" -> LazyRange(0x1760, 0x177F) "KhmerSymbols" -> CachedRange(0x19E0, 0x19FF)
"Khmer" -> LazyRange(0x1780, 0x17FF) "Buginese" -> CachedRange(0x1A00, 0x1A1F)
"Mongolian" -> LazyRange(0x1800, 0x18AF) "PhoneticExtensions" -> CachedRange(0x1D00, 0x1D7F)
"Limbu" -> LazyRange(0x1900, 0x194F) "PhoneticExtensionsSupplement" -> CachedRange(0x1D80, 0x1DBF)
"TaiLe" -> LazyRange(0x1950, 0x197F) "CombiningDiacriticalMarksSupplement" -> CachedRange(0x1DC0, 0x1DFF)
"NewTaiLue" -> LazyRange(0x1980, 0x19DF) "LatinExtendedAdditional" -> CachedRange(0x1E00, 0x1EFF)
"KhmerSymbols" -> LazyRange(0x19E0, 0x19FF) "GreekExtended" -> CachedRange(0x1F00, 0x1FFF)
"Buginese" -> LazyRange(0x1A00, 0x1A1F) "GeneralPunctuation" -> CachedRange(0x2000, 0x206F)
"PhoneticExtensions" -> LazyRange(0x1D00, 0x1D7F) "SuperscriptsandSubscripts" -> CachedRange(0x2070, 0x209F)
"PhoneticExtensionsSupplement" -> LazyRange(0x1D80, 0x1DBF) "CurrencySymbols" -> CachedRange(0x20A0, 0x20CF)
"CombiningDiacriticalMarksSupplement" -> LazyRange(0x1DC0, 0x1DFF) "CombiningMarksforSymbols" -> CachedRange(0x20D0, 0x20FF)
"LatinExtendedAdditional" -> LazyRange(0x1E00, 0x1EFF) "LetterlikeSymbols" -> CachedRange(0x2100, 0x214F)
"GreekExtended" -> LazyRange(0x1F00, 0x1FFF) "NumberForms" -> CachedRange(0x2150, 0x218F)
"GeneralPunctuation" -> LazyRange(0x2000, 0x206F) "Arrows" -> CachedRange(0x2190, 0x21FF)
"SuperscriptsandSubscripts" -> LazyRange(0x2070, 0x209F) "MathematicalOperators" -> CachedRange(0x2200, 0x22FF)
"CurrencySymbols" -> LazyRange(0x20A0, 0x20CF) "MiscellaneousTechnical" -> CachedRange(0x2300, 0x23FF)
"CombiningMarksforSymbols" -> LazyRange(0x20D0, 0x20FF) "ControlPictures" -> CachedRange(0x2400, 0x243F)
"LetterlikeSymbols" -> LazyRange(0x2100, 0x214F) "OpticalCharacterRecognition" -> CachedRange(0x2440, 0x245F)
"NumberForms" -> LazyRange(0x2150, 0x218F) "EnclosedAlphanumerics" -> CachedRange(0x2460, 0x24FF)
"Arrows" -> LazyRange(0x2190, 0x21FF) "BoxDrawing" -> CachedRange(0x2500, 0x257F)
"MathematicalOperators" -> LazyRange(0x2200, 0x22FF) "BlockElements" -> CachedRange(0x2580, 0x259F)
"MiscellaneousTechnical" -> LazyRange(0x2300, 0x23FF) "GeometricShapes" -> CachedRange(0x25A0, 0x25FF)
"ControlPictures" -> LazyRange(0x2400, 0x243F) "MiscellaneousSymbols" -> CachedRange(0x2600, 0x26FF)
"OpticalCharacterRecognition" -> LazyRange(0x2440, 0x245F) "Dingbats" -> CachedRange(0x2700, 0x27BF)
"EnclosedAlphanumerics" -> LazyRange(0x2460, 0x24FF) "MiscellaneousMathematicalSymbols-A" -> CachedRange(0x27C0, 0x27EF)
"BoxDrawing" -> LazyRange(0x2500, 0x257F) "SupplementalArrows-A" -> CachedRange(0x27F0, 0x27FF)
"BlockElements" -> LazyRange(0x2580, 0x259F) "BraillePatterns" -> CachedRange(0x2800, 0x28FF)
"GeometricShapes" -> LazyRange(0x25A0, 0x25FF) "SupplementalArrows-B" -> CachedRange(0x2900, 0x297F)
"MiscellaneousSymbols" -> LazyRange(0x2600, 0x26FF) "MiscellaneousMathematicalSymbols-B" -> CachedRange(0x2980, 0x29FF)
"Dingbats" -> LazyRange(0x2700, 0x27BF) "SupplementalMathematicalOperators" -> CachedRange(0x2A00, 0x2AFF)
"MiscellaneousMathematicalSymbols-A" -> LazyRange(0x27C0, 0x27EF) "MiscellaneousSymbolsandArrows" -> CachedRange(0x2B00, 0x2BFF)
"SupplementalArrows-A" -> LazyRange(0x27F0, 0x27FF) "Glagolitic" -> CachedRange(0x2C00, 0x2C5F)
"BraillePatterns" -> LazyRange(0x2800, 0x28FF) "Coptic" -> CachedRange(0x2C80, 0x2CFF)
"SupplementalArrows-B" -> LazyRange(0x2900, 0x297F) "GeorgianSupplement" -> CachedRange(0x2D00, 0x2D2F)
"MiscellaneousMathematicalSymbols-B" -> LazyRange(0x2980, 0x29FF) "Tifinagh" -> CachedRange(0x2D30, 0x2D7F)
"SupplementalMathematicalOperators" -> LazyRange(0x2A00, 0x2AFF) "EthiopicExtended" -> CachedRange(0x2D80, 0x2DDF)
"MiscellaneousSymbolsandArrows" -> LazyRange(0x2B00, 0x2BFF) "SupplementalPunctuation" -> CachedRange(0x2E00, 0x2E7F)
"Glagolitic" -> LazyRange(0x2C00, 0x2C5F) "CJKRadicalsSupplement" -> CachedRange(0x2E80, 0x2EFF)
"Coptic" -> LazyRange(0x2C80, 0x2CFF) "KangxiRadicals" -> CachedRange(0x2F00, 0x2FDF)
"GeorgianSupplement" -> LazyRange(0x2D00, 0x2D2F) "IdeographicDescriptionCharacters" -> CachedRange(0x2FF0, 0x2FFF)
"Tifinagh" -> LazyRange(0x2D30, 0x2D7F) "CJKSymbolsandPunctuation" -> CachedRange(0x3000, 0x303F)
"EthiopicExtended" -> LazyRange(0x2D80, 0x2DDF) "Hiragana" -> CachedRange(0x3040, 0x309F)
"SupplementalPunctuation" -> LazyRange(0x2E00, 0x2E7F) "Katakana" -> CachedRange(0x30A0, 0x30FF)
"CJKRadicalsSupplement" -> LazyRange(0x2E80, 0x2EFF) "Bopomofo" -> CachedRange(0x3100, 0x312F)
"KangxiRadicals" -> LazyRange(0x2F00, 0x2FDF) "HangulCompatibilityJamo" -> CachedRange(0x3130, 0x318F)
"IdeographicDescriptionCharacters" -> LazyRange(0x2FF0, 0x2FFF) "Kanbun" -> CachedRange(0x3190, 0x319F)
"CJKSymbolsandPunctuation" -> LazyRange(0x3000, 0x303F) "BopomofoExtended" -> CachedRange(0x31A0, 0x31BF)
"Hiragana" -> LazyRange(0x3040, 0x309F) "CJKStrokes" -> CachedRange(0x31C0, 0x31EF)
"Katakana" -> LazyRange(0x30A0, 0x30FF) "KatakanaPhoneticExtensions" -> CachedRange(0x31F0, 0x31FF)
"Bopomofo" -> LazyRange(0x3100, 0x312F) "EnclosedCJKLettersandMonths" -> CachedRange(0x3200, 0x32FF)
"HangulCompatibilityJamo" -> LazyRange(0x3130, 0x318F) "CJKCompatibility" -> CachedRange(0x3300, 0x33FF)
"Kanbun" -> LazyRange(0x3190, 0x319F) "CJKUnifiedIdeographsExtensionA" -> CachedRange(0x3400, 0x4DB5)
"BopomofoExtended" -> LazyRange(0x31A0, 0x31BF) "YijingHexagramSymbols" -> CachedRange(0x4DC0, 0x4DFF)
"CJKStrokes" -> LazyRange(0x31C0, 0x31EF) "CJKUnifiedIdeographs" -> CachedRange(0x4E00, 0x9FFF)
"KatakanaPhoneticExtensions" -> LazyRange(0x31F0, 0x31FF) "YiSyllables" -> CachedRange(0xA000, 0xA48F)
"EnclosedCJKLettersandMonths" -> LazyRange(0x3200, 0x32FF) "YiRadicals" -> CachedRange(0xA490, 0xA4CF)
"CJKCompatibility" -> LazyRange(0x3300, 0x33FF) "ModifierToneLetters" -> CachedRange(0xA700, 0xA71F)
"CJKUnifiedIdeographsExtensionA" -> LazyRange(0x3400, 0x4DB5) "SylotiNagri" -> CachedRange(0xA800, 0xA82F)
"YijingHexagramSymbols" -> LazyRange(0x4DC0, 0x4DFF) "HangulSyllables" -> CachedRange(0xAC00, 0xD7A3)
"CJKUnifiedIdeographs" -> LazyRange(0x4E00, 0x9FFF) "HighSurrogates" -> CachedRange(0xD800, 0xDB7F)
"YiSyllables" -> LazyRange(0xA000, 0xA48F) "HighPrivateUseSurrogates" -> CachedRange(0xDB80, 0xDBFF)
"YiRadicals" -> LazyRange(0xA490, 0xA4CF) "LowSurrogates" -> CachedRange(0xDC00, 0xDFFF)
"ModifierToneLetters" -> LazyRange(0xA700, 0xA71F) "PrivateUseArea" -> CachedRange(0xE000, 0xF8FF)
"SylotiNagri" -> LazyRange(0xA800, 0xA82F) "CJKCompatibilityIdeographs" -> CachedRange(0xF900, 0xFAFF)
"HangulSyllables" -> LazyRange(0xAC00, 0xD7A3) "AlphabeticPresentationForms" -> CachedRange(0xFB00, 0xFB4F)
"HighSurrogates" -> LazyRange(0xD800, 0xDB7F) "ArabicPresentationForms-A" -> CachedRange(0xFB50, 0xFDFF)
"HighPrivateUseSurrogates" -> LazyRange(0xDB80, 0xDBFF) "VariationSelectors" -> CachedRange(0xFE00, 0xFE0F)
"LowSurrogates" -> LazyRange(0xDC00, 0xDFFF) "VerticalForms" -> CachedRange(0xFE10, 0xFE1F)
"PrivateUseArea" -> LazyRange(0xE000, 0xF8FF) "CombiningHalfMarks" -> CachedRange(0xFE20, 0xFE2F)
"CJKCompatibilityIdeographs" -> LazyRange(0xF900, 0xFAFF) "CJKCompatibilityForms" -> CachedRange(0xFE30, 0xFE4F)
"AlphabeticPresentationForms" -> LazyRange(0xFB00, 0xFB4F) "SmallFormVariants" -> CachedRange(0xFE50, 0xFE6F)
"ArabicPresentationForms-A" -> LazyRange(0xFB50, 0xFDFF) "ArabicPresentationForms-B" -> CachedRange(0xFE70, 0xFEFF)
"VariationSelectors" -> LazyRange(0xFE00, 0xFE0F) "HalfwidthandFullwidthForms" -> CachedRange(0xFF00, 0xFFEF)
"VerticalForms" -> LazyRange(0xFE10, 0xFE1F) "all" -> CachedRange(0x00, 0x10FFFF)
"CombiningHalfMarks" -> LazyRange(0xFE20, 0xFE2F) "Specials" -> CachedSpecialsBlock()
"CJKCompatibilityForms" -> LazyRange(0xFE30, 0xFE4F) "Cn" -> CachedCategory(CharCategory.UNASSIGNED.value, true)
"SmallFormVariants" -> LazyRange(0xFE50, 0xFE6F) "IsL" -> CachedCategoryScope(0x3E, true)
"ArabicPresentationForms-B" -> LazyRange(0xFE70, 0xFEFF) "Lu" -> CachedCategory(CharCategory.UPPERCASE_LETTER.value, true)
"HalfwidthandFullwidthForms" -> LazyRange(0xFF00, 0xFFEF) "Ll" -> CachedCategory(CharCategory.LOWERCASE_LETTER.value, true)
"all" -> LazyRange(0x00, 0x10FFFF) "Lt" -> CachedCategory(CharCategory.TITLECASE_LETTER.value, false)
"Specials" -> LazySpecialsBlock() "Lm" -> CachedCategory(CharCategory.MODIFIER_LETTER.value, false)
"Cn" -> LazyCategory(CharCategory.UNASSIGNED.value, true) "Lo" -> CachedCategory(CharCategory.OTHER_LETTER.value, true)
"IsL" -> LazyCategoryScope(0x3E, true) "IsM" -> CachedCategoryScope(0x1C0, true)
"Lu" -> LazyCategory(CharCategory.UPPERCASE_LETTER.value, true) "Mn" -> CachedCategory(CharCategory.NON_SPACING_MARK.value, true)
"Ll" -> LazyCategory(CharCategory.LOWERCASE_LETTER.value, true) "Me" -> CachedCategory(CharCategory.ENCLOSING_MARK.value, false)
"Lt" -> LazyCategory(CharCategory.TITLECASE_LETTER.value, false) "Mc" -> CachedCategory(CharCategory.COMBINING_SPACING_MARK.value, true)
"Lm" -> LazyCategory(CharCategory.MODIFIER_LETTER.value, false) "N" -> CachedCategoryScope(0xE00, true)
"Lo" -> LazyCategory(CharCategory.OTHER_LETTER.value, true) "Nd" -> CachedCategory(CharCategory.DECIMAL_DIGIT_NUMBER.value, true)
"IsM" -> LazyCategoryScope(0x1C0, true) "Nl" -> CachedCategory(CharCategory.LETTER_NUMBER.value, true)
"Mn" -> LazyCategory(CharCategory.NON_SPACING_MARK.value, true) "No" -> CachedCategory(CharCategory.OTHER_NUMBER.value, true)
"Me" -> LazyCategory(CharCategory.ENCLOSING_MARK.value, false) "IsZ" -> CachedCategoryScope(0x7000, false)
"Mc" -> LazyCategory(CharCategory.COMBINING_SPACING_MARK.value, true) "Zs" -> CachedCategory(CharCategory.SPACE_SEPARATOR.value, false)
"N" -> LazyCategoryScope(0xE00, true) "Zl" -> CachedCategory(CharCategory.LINE_SEPARATOR.value, false)
"Nd" -> LazyCategory(CharCategory.DECIMAL_DIGIT_NUMBER.value, true) "Zp" -> CachedCategory(CharCategory.PARAGRAPH_SEPARATOR.value, false)
"Nl" -> LazyCategory(CharCategory.LETTER_NUMBER.value, true) "IsC" -> CachedCategoryScope(0xF0000, true, true)
"No" -> LazyCategory(CharCategory.OTHER_NUMBER.value, true) "Cc" -> CachedCategory(CharCategory.CONTROL.value, false)
"IsZ" -> LazyCategoryScope(0x7000, false) "Cf" -> CachedCategory(CharCategory.FORMAT.value, true)
"Zs" -> LazyCategory(CharCategory.SPACE_SEPARATOR.value, false) "Co" -> CachedCategory(CharCategory.PRIVATE_USE.value, true)
"Zl" -> LazyCategory(CharCategory.LINE_SEPARATOR.value, false) "Cs" -> CachedCategory(CharCategory.SURROGATE.value, false, true)
"Zp" -> LazyCategory(CharCategory.PARAGRAPH_SEPARATOR.value, false) "IsP" -> CachedCategoryScope(1 shl CharCategory.DASH_PUNCTUATION.value or
"IsC" -> LazyCategoryScope(0xF0000, true, true)
"Cc" -> LazyCategory(CharCategory.CONTROL.value, false)
"Cf" -> LazyCategory(CharCategory.FORMAT.value, true)
"Co" -> LazyCategory(CharCategory.PRIVATE_USE.value, true)
"Cs" -> LazyCategory(CharCategory.SURROGATE.value, false, true)
"IsP" -> LazyCategoryScope(1 shl CharCategory.DASH_PUNCTUATION.value or
(1 shl CharCategory.START_PUNCTUATION.value) or (1 shl CharCategory.START_PUNCTUATION.value) or
(1 shl CharCategory.END_PUNCTUATION.value) or (1 shl CharCategory.END_PUNCTUATION.value) or
(1 shl CharCategory.CONNECTOR_PUNCTUATION.value) or (1 shl CharCategory.CONNECTOR_PUNCTUATION.value) or
(1 shl CharCategory.OTHER_PUNCTUATION.value) or (1 shl CharCategory.OTHER_PUNCTUATION.value) or
(1 shl CharCategory.INITIAL_QUOTE_PUNCTUATION.value) or (1 shl CharCategory.INITIAL_QUOTE_PUNCTUATION.value) or
(1 shl CharCategory.FINAL_QUOTE_PUNCTUATION.value), true) (1 shl CharCategory.FINAL_QUOTE_PUNCTUATION.value), true)
"Pd" -> LazyCategory(CharCategory.DASH_PUNCTUATION.value, false) "Pd" -> CachedCategory(CharCategory.DASH_PUNCTUATION.value, false)
"Ps" -> LazyCategory(CharCategory.START_PUNCTUATION.value, false) "Ps" -> CachedCategory(CharCategory.START_PUNCTUATION.value, false)
"Pe" -> LazyCategory(CharCategory.END_PUNCTUATION.value, false) "Pe" -> CachedCategory(CharCategory.END_PUNCTUATION.value, false)
"Pc" -> LazyCategory(CharCategory.CONNECTOR_PUNCTUATION.value, false) "Pc" -> CachedCategory(CharCategory.CONNECTOR_PUNCTUATION.value, false)
"Po" -> LazyCategory(CharCategory.OTHER_PUNCTUATION.value, true) "Po" -> CachedCategory(CharCategory.OTHER_PUNCTUATION.value, true)
"IsS" -> LazyCategoryScope(0x7E000000, true) "IsS" -> CachedCategoryScope(0x7E000000, true)
"Sm" -> LazyCategory(CharCategory.MATH_SYMBOL.value, true) "Sm" -> CachedCategory(CharCategory.MATH_SYMBOL.value, true)
"Sc" -> LazyCategory(CharCategory.CURRENCY_SYMBOL.value, false) "Sc" -> CachedCategory(CharCategory.CURRENCY_SYMBOL.value, false)
"Sk" -> LazyCategory(CharCategory.MODIFIER_SYMBOL.value, false) "Sk" -> CachedCategory(CharCategory.MODIFIER_SYMBOL.value, false)
"So" -> LazyCategory(CharCategory.OTHER_SYMBOL.value, true) "So" -> CachedCategory(CharCategory.OTHER_SYMBOL.value, true)
"Pi" -> LazyCategory(CharCategory.INITIAL_QUOTE_PUNCTUATION.value, false) "Pi" -> CachedCategory(CharCategory.INITIAL_QUOTE_PUNCTUATION.value, false)
"Pf" -> LazyCategory(CharCategory.FINAL_QUOTE_PUNCTUATION.value, false) "Pf" -> CachedCategory(CharCategory.FINAL_QUOTE_PUNCTUATION.value, false)
else -> throw PatternSyntaxException("No such character class") else -> throw PatternSyntaxException("No such character class")
} }
}
fun intersects(ch1: Int, ch2: Int): Boolean { fun intersects(ch1: Int, ch2: Int): Boolean = ch1 == ch2
return ch1 == ch2 fun intersects(cc: AbstractCharClass, ch: Int): Boolean = cc.contains(ch)
}
fun intersects(cc: AbstractCharClass, ch: Int): Boolean { fun intersects(cc1: AbstractCharClass, cc2: AbstractCharClass): Boolean {
return cc.contains(ch) if (cc1.bits == null || cc2.bits == null) {
}
fun intersects(cc1: AbstractCharClass,
cc2: AbstractCharClass): Boolean {
if (cc1.bits == null || cc2.bits == null)
return true return true
}
return cc1.bits!!.intersects(cc2.bits!!) return cc1.bits!!.intersects(cc2.bits!!)
} }
fun getPredefinedClass(name: String, negative: Boolean): AbstractCharClass { fun getPredefinedClass(name: String, negative: Boolean): AbstractCharClass {
return (getClass(name)).getValue(negative) var cache = classCache
if (cache == null) {
cache = mutableMapOf()
classCache = cache
}
var cachedClass = cache[name]
if (cachedClass == null) {
cachedClass = createClass(name)
cache[name] = cachedClass
}
return cachedClass.getValue(negative)
} }
} }
} }
@@ -22,9 +22,7 @@ package kotlin.text.regex
* *
* @author Nikolay A. Kuznetsov * @author Nikolay A. Kuznetsov
*/ */
/* // TODO: replace the implementation with one using BitSet for first 256 symbols and a hash table / tree for the rest of UTF.
* TODO: replace the implementation with one using BitSet for first 256 symbols and a hash table / tree for the rest of UTF.
*/
internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = false) : AbstractCharClass() { internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = false) : AbstractCharClass() {
var invertedSurrogates = false var invertedSurrogates = false
@@ -38,7 +36,6 @@ internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = fa
*/ */
var inverted = false var inverted = false
// TODO: May be we can get rid of it?
var hideBits = false var hideBits = false
internal var bits_ = BitSet() internal var bits_ = BitSet()
@@ -155,9 +152,6 @@ internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = fa
} }
} }
// Some of charclasses hides its bits // Some of charclasses hides its bits
// Looks like hideBits and nonBitSet are used when we add another class which have no bitmask.
// TODO: We can potentially remove nonBitSet and hideBits. What is alt?
// TODO: The same for or operation.
} else { } else {
val curAlt = alt val curAlt = alt
@@ -222,17 +216,28 @@ internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = fa
fun add(start: Int, end: Int): CharClass { fun add(start: Int, end: Int): CharClass {
if (start > end) if (start > end)
throw IllegalArgumentException() throw IllegalArgumentException()
//no intersection with surrogate characters val minSurrogate = Char.MIN_SURROGATE.toInt()
if (!ignoreCase && (end < Char.MIN_SURROGATE.toInt() || start > Char.MAX_SURROGATE.toInt())) { val maxSurrogate = Char.MAX_SURROGATE.toInt()
if (!inverted) { if (ignoreCase) {
bits_.set(start, end + 1) // TODO: Make a faster implementation.
} else {
bits_.clear(start, end + 1)
}
} else {
for (i in start..end) { for (i in start..end) {
add(i) add(i)
} }
} else {
// No intersection with surrogate characters.
if (end < minSurrogate || start > maxSurrogate) {
bits_.set(start, end + 1, !inverted)
} else {
val surrogatesStart = maxOf(start, minSurrogate)
val surrogatesEnd = minOf(end, maxSurrogate)
bits_.set(start, end + 1, !inverted)
lowHighSurrogates.set(surrogatesStart - minSurrogate,
surrogatesEnd - minSurrogate + 1,
!invertedSurrogates)
if (!mayContainSupplCodepoints && end >= Char.MIN_SUPPLEMENTARY_CODE_POINT) {
mayContainSupplCodepoints = true
}
}
} }
return this return this
} }
@@ -489,7 +494,7 @@ internal class CharClass(val ignoreCase: Boolean = false, negative: Boolean = fa
*/ */
override operator fun contains(ch: Int): Boolean { override operator fun contains(ch: Int): Boolean {
if (nonBitSet == null) { if (nonBitSet == null) {
return this.alt xor bits_.get(ch) // TODO alt xor bits. It must make sense. return alt xor bits_.get(ch)
} else { } else {
return alt xor nonBitSet!!.contains(ch) return alt xor nonBitSet!!.contains(ch)
} }