Skip to content

Commit d8ea78c

Browse files
committed
Added TwoCharacterMatcher.
1 parent 3bfcbc8 commit d8ea78c

File tree

4 files changed

+194
-117
lines changed

4 files changed

+194
-117
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ organization := "hr.element.doit"
22

33
name := "doit-csv"
44

5-
version := "0.1.2"
5+
version := "0.1.3"
66

77

88
// ### Build settings ###

src/main/scala/hr/element/doit/csv/CSVWriter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.annotation.tailrec
44

55
import java.io._
66

7-
class CSVWriter(config: CSVConfig, writer: Writer) {
7+
class CSVWriter(val config: CSVConfig, writer: Writer) {
88
val quoteLen = config.quotes.length
99

1010
val escapes =

src/main/scala/hr/element/doit/csv/SlidingMatcher.scala

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,45 @@ class TwoCharacterMatcher(
6262
newLine: Array[Char])
6363
extends SlidingMatcher {
6464

65-
case class Res(val arr: Array[Char],
66-
val matchMsg: Smr)
65+
var buff: Option[Char] = None
66+
case class Res(
67+
val arr: Array[Char],
68+
val matchMsg: Smr)
69+
{
70+
val is = ((x: Char ) => if (arr.length==2) (arr(0) == buff.get && arr(1) == x)
71+
else (arr(0) == x))
72+
}
6773
val rh = Vector(Res(quotes, Quote),
6874
Res(delimiter, Delimiter),
6975
Res(newLine, NewLine))
76+
val expected = ((mode: SmrMode) => rh.filter(x => mode(x.matchMsg) != Ignore))
77+
val oneCharFind = ((read: Char) => rh.filter(x => x.arr.length == 1).find(_.is(read)))
7078

71-
var buff: Option[Int] = None
72-
73-
def consume(read: Char, mode: SmrMode) = {
74-
rh.filter(x => mode(x.matchMsg) != Ignore)
75-
76-
Cooldown
79+
def consume(read: Char, mode: SmrMode): Smr = {
80+
buff match {
81+
case Some(x) =>
82+
expected(mode).find( _.is(read)) match {
83+
case Some(someRes) =>
84+
if(someRes.arr.length == 2)
85+
buff = None
86+
someRes.matchMsg
87+
case None =>
88+
buff = Some(read)
89+
ReadCh(x)
90+
}
91+
case None =>
92+
oneCharFind(read) match {
93+
case None =>
94+
buff = Some(read)
95+
Cooldown
96+
case Some(y) => y.matchMsg
97+
}
98+
}
7799
}
78100
def flush(): Array[Char] =
79101
buff match {
80-
case Some(x) => x
102+
case Some(x) => Array(x)
103+
case None => Array.empty
81104

82105
}
83106

Lines changed: 160 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,160 @@
1-
//package hr.element.doit.csv
2-
//package test
3-
//
4-
//import org.scalatest._
5-
//import org.scalatest.matchers._
6-
//
7-
//import scala.util.Random
8-
//
9-
//import java.security.SecureRandom
10-
//import java.math.BigInteger
11-
//import java.io._
12-
//
13-
//class ExampleSuite2 extends GivenWhenThen
14-
// with ShouldMatchers
15-
// with FeatureSpec {
16-
//
17-
// scenario("Easy Test"){
18-
// val outFileName = "/home/marin/doit/csvs/test2.csv"
19-
// val factory = CSVFactory.
20-
// setDelimiter("1234").
21-
// setNewLine("56").
22-
// setQuotes("asdfgh")
23-
//
24-
// val oS= new FileOutputStream(outFileName)
25-
// val writer = factory.getWriter( oS)
26-
// //val header = IndexedSeq("first", "second", "third")
27-
// val line = IndexedSeq("1\"25asdfgh435426546;","2656543542" , "gfsgfsgfsgfsd3" )
28-
//
29-
// //writer.write(header.toArray)
30-
// writer.write(line.toArray)
31-
// //writer.close()
32-
// oS.close()
33-
//
34-
//
35-
// val reader = factory.getReader(
36-
// new FileInputStream(outFileName))
37-
//
38-
// //val rea = reader.readLn()
39-
//
40-
// reader.foreach(_.foreach(println(_)))
41-
// }
42-
////
43-
// scenario("Simple withHeader Test.") {
44-
// val outFileName = "/home/marin/doit/csvs/test1.csv"
45-
// val startCase = 6066
46-
// val endCase = 6067
47-
// val strSize = 50
48-
// val numOfQuotes = (i: Int) => i / 70
49-
// val header = IndexedSeq("first", "second", "third")
50-
// val rowMod = (i: Int) => (i)
51-
// val colMod = header.length //(i:Int ) =>(i / 60)
52-
//
53-
// for (i <- startCase to endCase) {
54-
// val t = new Random(i - 1)
55-
// val factory = CSVFactory.
56-
// setDelimiter(t.nextString(i / 70)).
57-
// setNewLine(t.nextString(i / 60)).
58-
// setQuotes(t.nextString(i / 20))
59-
//
60-
// if (valid(factory)) {
61-
// val oS= new FileOutputStream(outFileName)
62-
// val writer = factory.getWriter(oS)
63-
// writer.write(header.toArray)
64-
// val rand = new Random(i)
65-
// for (j <- 1 to rowMod(i)) {
66-
// val str = for (k <- 1 to colMod)
67-
// yield getRandStr(rand, numOfQuotes(i), factory.quotes, strSize)
68-
// val arr = str.toArray
69-
// writer.write(arr)
70-
// }
71-
// oS.close()
72-
// val time = System.currentTimeMillis()
73-
// val r = new Random(i)
74-
// val f = new File(outFileName)
75-
// info("Starting test :" + i + ", File size: " + f.length())
76-
// val reader = factory.getReaderWithHeaders(new FileInputStream(f))
77-
// reader.foreach(
78-
// _.foreach {
79-
// _ should equal(
80-
// getRandStr(r, numOfQuotes(i), factory.quotes, strSize))
81-
// })
82-
// info("time: "+(System.currentTimeMillis() - time))
83-
// } else {}
84-
// }
85-
// }
86-
//
87-
// def getRandStr(r: Random, i: Int, quotes: String, strSize: Int) = {
88-
// val str = new StringBuilder(
89-
// r.nextString(r.nextInt().abs % strSize + 4))
90-
// val l = str.length
91-
// for (l <- 1 to i) {
92-
// str.insert((r.nextInt().abs % l + 3), quotes)
93-
// }
94-
// str.result
95-
// }
96-
//
97-
// def valid(fac: CSVFactory) = {
98-
// if (fac.delimiter.contains(fac.quotes) ||
99-
// fac.delimiter.contains(fac.newLine) ||
100-
// fac.quotes.contains(fac.newLine) ||
101-
// fac.quotes.contains(fac.delimiter) ||
102-
// fac.newLine.contains(fac.quotes) ||
103-
// fac.newLine.contains(fac.delimiter))
104-
// false else true
105-
// }
106-
//}
1+
package hr.element.doit.csv
2+
package test
3+
4+
import org.scalatest._
5+
import org.scalatest.matchers._
6+
import scala.util.Random
7+
import java.security.SecureRandom
8+
import java.math.BigInteger
9+
import java.io._
10+
import java.nio.charset.Charset
11+
12+
class ExampleSuite2 extends GivenWhenThen
13+
with ShouldMatchers
14+
with FeatureSpec {
15+
16+
scenario("Easy Test"){
17+
val outFileName = "test2.csv"
18+
val factory = CSVConfig.
19+
setDelimiter("1234").
20+
setNewLine("56").
21+
setQuotes("asdfgh").
22+
setEncoding(Charset.forName("utf8"))
23+
24+
val oS= new FileOutputStream(outFileName)
25+
val writer = factory.getWriter( oS)
26+
val header = IndexedSeq("first", "second", "third")
27+
val line = IndexedSeq("1\"25asdfgh435426546;","2656543542" , "gfsgfsgfsgfsd3" )
28+
29+
30+
writer.write(header.toArray)
31+
writer.write(line.toArray)
32+
33+
oS.close()
34+
35+
36+
val reader = factory.getReaderWithHeaders(
37+
new FileInputStream(outFileName))
38+
39+
40+
41+
//reader.header.map(_) should equal (Array(1,2,3))
42+
}
43+
44+
scenario("Simple withHeader Test, TwoCharMatcher") {
45+
val outFileName = "test1.csv"
46+
val testRange = 1166 to 1167
47+
val strSize = 20
48+
val numOfQuotes = (i: Int) => i / 70
49+
val header = IndexedSeq("first", "second", "third")
50+
val rowMod = (i: Int) => (i)
51+
val colMod = header.length //(i:Int ) =>(i / 60)
52+
val quotes ="23"
53+
val newLine="7"
54+
val delimiter ="9"
55+
for (i <- testRange) {
56+
val t = new Random(i - 1)
57+
val factory = CSVConfig.
58+
setDelimiter(t.nextString(i % 2)).
59+
setNewLine(t.nextString(i % 2)).
60+
setQuotes(t.nextString(i % 2)).
61+
setEncoding(Charset.forName("utf8"))
62+
63+
val oS= new FileOutputStream(outFileName)
64+
val writer = factory.getWriter(oS)
65+
if (valid(writer.config)) {
66+
67+
writer.write(header.toArray)
68+
val rand = new Random(i)
69+
for (j <- 1 to rowMod(i)) {
70+
val str = for (k <- 1 to colMod)
71+
yield getRandStr(rand, numOfQuotes(i), quotes, strSize)
72+
val arr = str.toArray
73+
writer.write(arr)
74+
}
75+
oS.close()
76+
val time = System.currentTimeMillis()
77+
78+
val r = new Random(i)
79+
val f = new File(outFileName)
80+
info("Starting test :" + i + ", File size: " + f.length)
81+
val reader = factory.getReaderWithHeaders(new FileInputStream(f))
82+
reader.foreach{x =>
83+
x.foreach {
84+
_ should equal(
85+
getRandStr(r, numOfQuotes(i), quotes, strSize))
86+
}
87+
}
88+
info("time: "+(System.currentTimeMillis() - time))
89+
} else {}
90+
}
91+
}
92+
93+
scenario("Simple Test with Headers and CyclicMatcher"){
94+
val outFileName = "test1.csv"
95+
val testRange = 566 to 567
96+
val strSize = 20
97+
val numOfQuotes = (i: Int) => i / 70
98+
val header = IndexedSeq("first", "second", "third")
99+
val rowMod = (i: Int) => (i)
100+
val colMod = header.length //(i:Int ) =>(i / 60)
101+
102+
for (i <- testRange) {
103+
val t = new Random(i - 1)
104+
val factory = CSVConfig.
105+
setDelimiter(t.nextString(i / 70)).
106+
setNewLine(t.nextString(i / 60)).
107+
setQuotes(t.nextString(i / 20)).
108+
setEncoding(Charset.forName("utf8"))
109+
110+
val oS= new FileOutputStream(outFileName)
111+
val writer = factory.getWriter(oS)
112+
if (valid(writer.config)) {
113+
114+
writer.write(header.toArray)
115+
val rand = new Random(i)
116+
for (j <- 1 to rowMod(i)) {
117+
val str = for (k <- 1 to colMod)
118+
yield getRandStr(rand, numOfQuotes(i), writer.config.quotes, strSize)
119+
val arr = str.toArray
120+
writer.write(arr)
121+
}
122+
oS.close()
123+
val time = System.currentTimeMillis()
124+
125+
val r = new Random(i)
126+
val f = new File(outFileName)
127+
info("Starting test :" + i + ", File size: " + f.length)
128+
val reader = factory.getReaderWithHeaders(new FileInputStream(f))
129+
reader.foreach{x =>
130+
//println("y0 R")
131+
x.foreach {
132+
//println("y0")
133+
_ should equal(
134+
getRandStr(r, numOfQuotes(i), writer.config.quotes, strSize))
135+
}
136+
}
137+
info("time: "+(System.currentTimeMillis() - time))
138+
} else {}
139+
}
140+
}
141+
def getRandStr(r: Random, i: Int, quotes: String, strSize: Int) = {
142+
val str = new StringBuilder(
143+
r.nextString(r.nextInt().abs % strSize + 4))
144+
val l = str.length
145+
for (l <- 1 to i) {
146+
str.insert((r.nextInt().abs % l + 3), quotes)
147+
}
148+
str.result
149+
}
150+
151+
def valid(fac: CSVConfig) = {
152+
if (fac.delimiter.contains(fac.quotes) ||
153+
fac.delimiter.contains(fac.newLine) ||
154+
fac.quotes.contains(fac.newLine) ||
155+
fac.quotes.contains(fac.delimiter) ||
156+
fac.newLine.contains(fac.quotes) ||
157+
fac.newLine.contains(fac.delimiter))
158+
false else true
159+
}
160+
}

0 commit comments

Comments
 (0)