Skip to content

Commit

Permalink
-- multibyte UTF8 playground
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Sep 24, 2023
1 parent 163c14e commit d252064
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 14 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sbt.Keys.*
import sbtbuildinfo.BuildInfoOption
import scala.collection.Seq

Global / onChangedBuildSource := ReloadOnSourceChanges

Expand Down
62 changes: 62 additions & 0 deletions ce3/src/main/scala/utf8/Playground.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package utf8

/** multibyte UTF8 playground */
object Playground extends App {

def toBin(x: Byte): String = {
val chars = Array.ofDim[Byte](8)
(0 to 7)
.foreach { bit =>
chars(7 - bit) = ('0' + ((x >> bit) & 1)).toByte
}
new String(chars)
}

def mkColored(s: String, n: Int): String =
new StringBuilder(Console.RED)
.append(s.substring(0, n))
.append(Console.RESET)
.append(s.substring(n))
.toString()

def describeUtfContent(utf: String): Unit = {
val bytes: Array[Byte] = utf.getBytes
val per_char = bytes.length / utf.codePoints().count().toInt
val dec: Array[Int] = bytes.map(b => b & 0xff)
val hex: Array[String] = bytes.map(b => "%02X".format(b))
val bin: Array[String] = bytes.zipWithIndex.map { case (b, i) =>
mkColored(
toBin(b),
(per_char, i % per_char) match {
case (1, _) => 1
case (x, 0) => x + 1
case (x, _) if x < 8 => 2
case _ => 0
}
)
}

def groupGt1[A](xs: Iterable[A]) =
per_char match {
case 1 => xs.mkString("[", ", ", "]")
case _ => xs.grouped(per_char).map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]")
}

printf("content: `%s`\n", utf)
printf("length: %d\n", utf.chars().count())
printf("bytes length: %d\n", bytes.length)
printf("bytes per char:%d\n", per_char)
printf("bytes decimal: %s\n", groupGt1(dec))
printf("bytes hex: %s\n", groupGt1(hex))
printf("bytes bin: %s\n", groupGt1(bin))
println("-" * 50)
}

Seq(
"hello",
"Привет",
"नमस्ते",
"😀🤪😐🙄"
).foreach(describeUtfContent)

}
16 changes: 16 additions & 0 deletions plain2/src/main/scala/interview/general/Q3_Syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package interview.general

object Q3_Syntax extends App {

implicit class IntWithOptionSyntax(x: Int) {
def +(yo: Option[Int]): Int =
yo match {
case Some(y) => x + y
case None => x
}
}

val x: Int = 10 + Some(33)
println(x)

}
47 changes: 47 additions & 0 deletions plain2/src/main/scala/interview/general/TTT.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package interview.general

object TTT {

trait A {
def a1: Int
def a2: String
}

new A {
override val a1: Int = 1
override val a2: String = "hello"
}

class C(val a1: Int, val a2: String) extends A {
def m1: Int = ???
}
object C {
def apply(a1: Int, a2: String) = new C(a1, a2)
}


new C(1, "hello")
C.apply(1, "hello")
C(1, "hello")

val f = new Function[Int, String] {
override def apply(v1: Int): String = v1.toString
}

f.apply(3)
f(3)

val a: Array[Int] = Array(1,2,3,4,5)
a.apply(2)
a(2)

"hello".apply(3)
"hello"(3)







}
15 changes: 6 additions & 9 deletions project/CompilerOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import ScalacOpts.*
*/
object CompilerOptions {

val javacOptions = Seq(
"-source",
"11",
"-target",
"11",
)
val javacOptions: Seq[String] =
Seq("-source", "11") ++
Seq("-target", "11") ++
Seq(
)

val scalacOptions = Seq(
encoding,
UTF8,
val scalacOptions: Seq[String] = utf8 ++ Seq(
feature,
deprecation,
unchecked,
Expand Down
5 changes: 2 additions & 3 deletions project/ScalacOpts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ object ScalacOpts {

/** default project encoding
*/
val encoding = "-encoding"
val UTF8 = "UTF-8"
val utf8: Seq[String] = Seq("-encoding", "UTF-8")

/** Emit warning and location for usages of features that should be imported explicitly.
*/
val feature = "-feature"

/** Emit warning and location for usages of deprecated APIs.
*/
val deprecation = "-deprecation"
val deprecation = "-deprecation"
val lintDeprecation = "-Xlint:deprecation"

/** Enable additional warnings where generated code depends on assumptions.
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.5
sbt.version=1.9.6

0 comments on commit d252064

Please sign in to comment.