Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,15 @@
|
|||
object TokenizeStringWithEscaping0 extends App {
|
||||
|
||||
val (markerSpE,markerSpF) = ("\ufffe" , "\uffff")
|
||||
|
||||
def tokenize(str: String, sep: String, esc: String): Array[String] = {
|
||||
|
||||
val s0 = str.replace( esc + esc, markerSpE).replace(esc + sep, markerSpF)
|
||||
val s = if (s0.last.toString == esc) s0.replace(esc, "") + esc else s0.replace(esc, "")
|
||||
s.split(sep.head).map (_.replace(markerSpE, esc).replace(markerSpF, sep))
|
||||
}
|
||||
|
||||
def str = "one^|uno||three^^^^|four^^^|^cuatro|"
|
||||
|
||||
tokenize(str, "|", "^").foreach(it => println(if (it.isEmpty) "<empty token>" else it))
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import scala.annotation.tailrec
|
||||
|
||||
object TokenizeStringWithEscaping1 extends App {
|
||||
|
||||
def tokenize(str: String, sep: String, esc: String): Seq[String] = {
|
||||
@tailrec
|
||||
def loop(accu: Seq[String], s: String): Seq[String] = {
|
||||
def append2StringInList(char: String): Seq[String] =
|
||||
accu.init :+ (accu.last + char)
|
||||
|
||||
s.length match {
|
||||
case 0 => accu
|
||||
case 1 => if (s.head.toString == sep) accu :+ "" else append2StringInList(s)
|
||||
case _ => (s.head.toString, s.tail.head.toString) match {
|
||||
case c@((`esc`, `sep`) | (`esc`, `esc`)) => loop(append2StringInList(c._2), s.tail.tail)
|
||||
case (`sep`, _) => loop(accu :+ "", s.tail)
|
||||
case (`esc`, _) => loop(accu, s.tail)
|
||||
case (sub, _) => loop(append2StringInList(sub.head.toString), s.tail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loop(Seq(""), str)
|
||||
}
|
||||
|
||||
def str = "one^|uno||three^^^^|four^^^|^cuatro|"
|
||||
|
||||
tokenize(str, "|", "^")
|
||||
.foreach(it =>
|
||||
println(
|
||||
f"[length:${it.length}%3d] ${if (it.isEmpty) "<empty token>" else it}"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue