Skip to content

Commit b2ebb76

Browse files
committed
comments
1 parent 04357b2 commit b2ebb76

File tree

11 files changed

+124
-56
lines changed

11 files changed

+124
-56
lines changed

.codecov.yml

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

.travis.yml

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

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
[![Build Status](https://travis-ci.com/gekomad/scala-regex-collection.svg?branch=master)](https://travis-ci.com/gekomad/scala-regex-collection) [![codecov.io](http://codecov.io/github/gekomad/scala-regex-collection/coverage.svg?branch=master)](http://codecov.io/github/gekomad/scala-regex-collection?branch=master)
2-
1+
[![Build Status](https://travis-ci.com/gekomad/scala-regex-collection.svg?branch=master)](https://travis-ci.com/gekomad/scala-regex-collection)
32

43
Scala regex collection
54
=====================
65

76
Scala-regex-collection is a pure scala regex collection
87
## Add the library to your project
9-
`libraryDependencies += "com.github.gekomad" %% "scala-regex-collection" % "1.0.0"`
8+
`libraryDependencies += "com.github.gekomad" %% "scala-regex-collection" % "1.0.1"`
109

1110
## Using Library
1211

@@ -123,6 +122,9 @@ Coordinates
123122
- [Coordinate1](https://github.com/gekomad/scala-regex-collection/wiki/Coordinate1) (45°23'36.0" N 10°33'48.0" E)
124123
- [Coordinate2](https://github.com/gekomad/scala-regex-collection/wiki/Coordinate2) (12:12:12.223546"N - 15:17:6"S - 12°30'23.256547"S)
125124

125+
Programming
126+
127+
- [Comments](https://github.com/gekomad/scala-regex-collection/wiki/Comments) (/* foo */)
126128

127129
## Use the library
128130

@@ -274,4 +276,4 @@ See the License for the specific language governing permissions and limitations
274276

275277
## Special Thanks ##
276278

277-
To [regexlib.com](http://regexlib.com)
279+
To [regexlib.com](http://regexlib.com)

build.sbt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
name := "scala-regex-collection"
22

3-
version := "1.0.0"
3+
version := "1.0.1"
44

5-
scalaVersion := "2.13.0"
5+
scalaVersion := "2.13.1"
66
organization := "com.github.gekomad"
77

8-
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
8+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0-M2" % Test
99

10-
crossScalaVersions := Seq("2.10.7", "2.11.12", "2.12.6", "2.12.8","2.13.0")
10+
crossScalaVersions := Seq("2.10.7", "2.11.12", "2.12.6", "2.12.8", "2.13.0", "2.13.1")
11+
12+
scalacOptions ++= Seq(
13+
"-deprecation",
14+
"-encoding", "UTF-8",
15+
"-language:postfixOps",
16+
"-feature",
17+
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
18+
"-Xcheckinit", // Wrap field accessors to throw an exception on uninitialized access.
19+
"-Ywarn-dead-code", // Warn when dead code is identified.
20+
"-explaintypes", // Explain type errors in more detail.
21+
"-Xfatal-warnings"
22+
)
1123

1224
publishTo := sonatypePublishTo.value
1325

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.2.8
1+
sbt.version = 1.3.6

project/plugins.sbt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
22
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1")
3-
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.0")

src/main/scala/com/github/gekomad/regexcollection/Validator.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import scala.util.Try
66
import scala.util.matching.Regex
77

88
object Validate {
9-
implicit def validateIgnoreCase[A](a: String)(implicit f: Validator[A]): Option[String] = f.validateIgnoreCase(a)
10-
implicit def validate[A](a: String)(implicit f: Validator[A]): Option[String] = f.validateCaseSensitive(a)
11-
implicit def regexp[A](implicit f: Validator[A]): String = f.regexp
12-
implicit def findAllIgnoreCase[A](a: String)(implicit f: Validator[A]): List[String] = f.findAllIgnoreCase(a)
13-
implicit def findAll[A](a: String)(implicit f: Validator[A]): List[String] = f.findAllCaseSensitive(a)
14-
implicit def findFirstIgnoreCase[A](a: String)(implicit f: Validator[A]): Option[String] = f.findFirstIgnoreCase(a)
15-
implicit def findFirst[A](a: String)(implicit f: Validator[A]): Option[String] = f.findFirstCaseSensitive(a)
9+
def validateIgnoreCase[A](a: String)(implicit f: Validator[A]): Option[String] = f.validateIgnoreCase(a)
10+
def validate[A](a: String)(implicit f: Validator[A]): Option[String] = f.validateCaseSensitive(a)
11+
def regexp[A](implicit f: Validator[A]): String = f.regexp
12+
def findAllIgnoreCase[A](a: String)(implicit f: Validator[A]): List[String] = f.findAllIgnoreCase(a)
13+
def findAll[A](a: String)(implicit f: Validator[A]): List[String] = f.findAllCaseSensitive(a)
14+
def findFirstIgnoreCase[A](a: String)(implicit f: Validator[A]): Option[String] = f.findFirstIgnoreCase(a)
15+
def findFirst[A](a: String)(implicit f: Validator[A]): Option[String] = f.findFirstCaseSensitive(a)
1616
}
1717

1818
trait Email
@@ -95,6 +95,8 @@ trait Celsius
9595
trait Fahrenheit
9696
trait HtmlHref
9797

98+
trait Comments
99+
98100
object Collection {
99101

100102
import java.time._
@@ -107,16 +109,16 @@ object Collection {
107109
def findAllCaseSensitive(a: String): List[String]
108110
def findFirstIgnoreCase(a: String): Option[String]
109111
def findFirstCaseSensitive(a: String): Option[String]
110-
val regexp: String
112+
def regexp: String
111113
}
112114

113115
object Validator {
114116
def apply[A](reg: String): Validator[A] = new Validator[A] {
115117
override val regexp: String = reg
116118
val regex: Regex = reg.r
117-
val regexIgn: Regex = ("(?i)" + reg).r
118-
val regexExact: Regex = ('^' + reg + '$').r
119-
val regexExactIgn: Regex = ("^(?i)" + reg + '$').r
119+
val regexIgn: Regex = s"(?i)$reg".r
120+
val regexExact: Regex = s"^$reg$$".r
121+
val regexExactIgn: Regex = s"^(?i)$reg$$".r
120122

121123
override def validateIgnoreCase(a: String): Option[String] = regexExactIgn.findFirstMatchIn(a).map(_ => a)
122124
override def validateCaseSensitive(a: String): Option[String] = regexExact.findFirstMatchIn(a).map(_ => a)
@@ -261,6 +263,7 @@ object Collection {
261263
implicit val validatorTime: Validator[Time] =
262264
Validator[Time]("""(([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)|(^([0-9]|[1][0-9]|[2][0-3])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2}))""")
263265

266+
implicit val validatorComment: Validator[Comments] = Validator[Comments]("""(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)""")
264267
implicit val validatorLocalDateTime: Validator[LocalDateTime] = Validator[LocalDateTime]((a: String) => Try(LocalDateTime.parse(a, ISO_LOCAL_DATE_TIME)).toOption.map(_ => a))
265268

266269
implicit val validatorLocalDate: Validator[LocalDate] = Validator[LocalDate]((a: String) => Try(LocalDate.parse(a, ISO_LOCAL_DATE)).toOption.map(_ => a))

src/test/scala/FindAll.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import org.scalatest.FunSuite
1+
import org.scalatest.funsuite.AnyFunSuite
22

3-
class FindAll extends FunSuite {
3+
class FindAll extends AnyFunSuite {
44

55
test("Email") {
66
import com.github.gekomad.regexcollection.Email
@@ -72,4 +72,10 @@ class FindAll extends FunSuite {
7272
assert(findAllIgnoreCase[Bar]("bar abc@google.com hi hello Bar@yahoo.com 123 bar@foo.com") == List("Bar@yahoo.com", "bar@foo.com"))
7373
assert(findAll[Bar]("bar abc@google.com hi hello Bar@yahoo.com 123 bar@foo.com") == List("Bar@yahoo.com"))
7474
}
75+
76+
test("Comments") {
77+
import com.github.gekomad.regexcollection.Comments
78+
import com.github.gekomad.regexcollection.Validate.findAll
79+
assert(findAll[Comments]("""/*foo*/ bar /*baz*/ 10% dg 55% """) == List("/*foo*/", "/*baz*/"))
80+
}
7581
}

src/test/scala/FindFirst.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import org.scalatest.FunSuite
1+
import org.scalatest.funsuite.AnyFunSuite
22

3-
class FindFirst extends FunSuite {
3+
class FindFirst extends AnyFunSuite {
44

55
test("Email") {
66
import com.github.gekomad.regexcollection.Email
@@ -72,4 +72,10 @@ class FindFirst extends FunSuite {
7272
assert(findFirstIgnoreCase[Bar]("bar abc@google.com hi hello bar@yahoo.com 123 Bar@foo.com") == Some("bar@yahoo.com"))
7373
assert(findFirst[Bar]("bar abc@google.com hi hello bar@yahoo.com 123 Bar@foo.com") == Some("Bar@foo.com"))
7474
}
75+
76+
test("Comments") {
77+
import com.github.gekomad.regexcollection.Comments
78+
import com.github.gekomad.regexcollection.Validate.findFirst
79+
assert(findFirst[Comments]("/*foo*/ bar /*baz*/ 10% dg 55% ") == Some("/*foo*/"))
80+
}
7581
}

src/test/scala/ReadFile.scala

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
3+
class ReadFile extends AnyFunSuite {
4+
5+
ignore("Extract from files") {
6+
import scala.io.Source
7+
import com.github.gekomad.regexcollection._
8+
import com.github.gekomad.regexcollection.Validate.findAll
9+
import java.io.File
10+
import scala.util.Try
11+
def getListOfFiles(dir: String)(f : File => Unit): Unit = {
12+
def go(dir: File): Unit= dir match {
13+
case d if d.exists && d.isDirectory && d.canRead=>
14+
val files = d.listFiles.filter(a => a.canRead && a.isFile).toList
15+
val dirs = dir.listFiles.filter(_.isDirectory).toList
16+
files.foreach(f)
17+
dirs.foreach(go)
18+
case _ => ()
19+
}
20+
go(new File(dir))
21+
}
22+
23+
getListOfFiles("/") { filename =>
24+
Try {
25+
for (line <- Source.fromFile(filename).getLines) {
26+
findAll[Email](line).foreach(a => println(s"$filename: [Email] $a"))
27+
findAll[Time24](line).foreach(a => println(s"$filename: [Time24] $a"))
28+
findAll[ApacheError](line).foreach(a => println(s"$filename: [ApacheError] $a"))
29+
findAll[BitcoinAdd](line).foreach(a => println(s"$filename: [BitcoinAdd] $a"))
30+
findAll[Celsius](line).foreach(a => println(s"$filename: [Celsius] $a"))
31+
findAll[Comments](line).foreach(a => println(s"$filename: [Comments] $a"))
32+
findAll[Coordinate](line).foreach(a => println(s"$filename: [Coordinate] $a"))
33+
findAll[Coordinate1](line).foreach(a => println(s"$filename: [Coordinate1] $a"))
34+
findAll[Coordinate2](line).foreach(a => println(s"$filename: [Coordinate2] $a"))
35+
findAll[Cron](line).foreach(a => println(s"$filename: [Cron] $a"))
36+
findAll[DMY](line).foreach(a => println(s"$filename: [DMY] $a"))
37+
findAll[DMY2](line).foreach(a => println(s"$filename: [DMY2] $a"))
38+
findAll[DMY3](line).foreach(a => println(s"$filename: [DMY3] $a"))
39+
findAll[UUID](line).foreach(a => println(s"$filename: [UUID] $a"))
40+
findAll[Youtube](line).foreach(a => println(s"$filename: [Youtube] $a"))
41+
42+
}
43+
}
44+
}
45+
}
46+
47+
}

src/test/scala/Validate.scala

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import org.scalatest.FunSuite
1+
import org.scalatest.funsuite.AnyFunSuite
22

3-
class Validate extends FunSuite {
3+
import scala.util.{Failure, Success}
4+
5+
class Validate extends AnyFunSuite {
46

57
test("Email") {
68
import com.github.gekomad.regexcollection.Validate.{regexp, validate}
@@ -954,17 +956,30 @@ class Validate extends FunSuite {
954956
import com.github.gekomad.regexcollection.Collection.Validator
955957
import scala.util.Try
956958

957-
implicit val validator = Validator[Foo]((a: String) => {
958-
val even = for {
959-
i <- Try(a.toInt)
960-
if (i % 2 == 0)
961-
} yield Some(a)
962-
even.getOrElse(None)
963-
})
959+
implicit val validator = Validator[Foo]((a: String) =>
960+
Try(a.toInt) match {
961+
case Failure(_) => None
962+
case Success(i) => if (i % 2 == 0) Option(a) else None
963+
}
964+
)
964965

965966
assert(validate[Foo]("42") == Some("42"))
967+
assert(validate[Foo]("21") == None)
966968
assert(validate[Foo]("hello") == None)
967969

968970
}
969971

972+
test("Comments") {
973+
import com.github.gekomad.regexcollection.Comments
974+
import com.github.gekomad.regexcollection.Validate.validate
975+
assert(validate[Comments]("hi") == None)
976+
assert(validate[Comments]("/*hi") == None)
977+
assert(validate[Comments]("/*hi*/") == Some("/*hi*/"))
978+
assert(validate[Comments](
979+
"""/*hi
980+
|foo * */""".stripMargin) == Some(
981+
"""/*hi
982+
|foo * */""".stripMargin))
983+
}
984+
970985
}

0 commit comments

Comments
 (0)