Skip to content

Commit ed04a5b

Browse files
committed
add eu.timepit.refined.W for Scala 3
eliminate duplicated code
1 parent 71cfd8a commit ed04a5b

File tree

5 files changed

+87
-109
lines changed

5 files changed

+87
-109
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package eu.timepit.refined
2+
3+
import scala.annotation.compileTimeOnly
4+
import scala.compiletime.ops.int
5+
import scala.compiletime.ops.long
6+
import scala.compiletime.ops.string.CharAt
7+
import scala.compiletime.ops.string.Length
8+
import scala.compiletime.ops.string.Substring
9+
import scala.language.dynamics
10+
11+
@compileTimeOnly("Illegal reference")
12+
object W extends Dynamic {
13+
type StringTail[A <: String] = Substring[A, 1, Length[A]]
14+
15+
type StringLast[A <: String] = CharAt[A, int.-[Length[A], 1]]
16+
17+
type Literal[A <: String] = CharAt[A, 0] match {
18+
case '"' =>
19+
// String
20+
StringLast[A] match {
21+
case '"' =>
22+
Substring[A, 1, int.-[Length[A], 1]]
23+
}
24+
case '\'' =>
25+
// Char
26+
CharAt[A, 2] match {
27+
case '\'' =>
28+
Length[A] match {
29+
case 3 =>
30+
CharAt[A, 1]
31+
}
32+
}
33+
case _ =>
34+
// TODO support another types
35+
// e.g. Double, Float
36+
StringLast[A] match {
37+
case 'L' =>
38+
// Long
39+
StringToLong[Substring[A, 0, int.-[Length[A], 1]]]
40+
case _ =>
41+
// Int
42+
long.ToInt[StringToLong[A]]
43+
}
44+
}
45+
46+
type StringToLong[Input <: String] <: Long = CharAt[Input, 0] match {
47+
case '-' =>
48+
long.Negate[Loop[StringTail[Input], 0L]]
49+
case _ =>
50+
Loop[Input, 0L]
51+
}
52+
53+
type CharToLong[C <: Char] <: Long = C match {
54+
case '0' => 0L
55+
case '1' => 1L
56+
case '2' => 2L
57+
case '3' => 3L
58+
case '4' => 4L
59+
case '5' => 5L
60+
case '6' => 6L
61+
case '7' => 7L
62+
case '8' => 8L
63+
case '9' => 9L
64+
}
65+
66+
type Loop[Input <: String, Acc <: Long] <: Long = Length[Input] match {
67+
case 0 =>
68+
Acc
69+
case _ =>
70+
Loop[
71+
StringTail[Input],
72+
long.+[
73+
long.*[10L, Acc],
74+
CharToLong[
75+
CharAt[Input, 0]
76+
]
77+
]
78+
]
79+
}
80+
81+
def selectDynamic(
82+
selector: String
83+
): None.type { type T = Literal[selector.type] } =
84+
None.asInstanceOf[
85+
None.type { type T = Literal[selector.type] }
86+
]
87+
}

modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/digests.scala

Lines changed: 0 additions & 48 deletions
This file was deleted.

modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/time.scala

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)