17
17
18
18
package org .apache .daffodil .runtime1 .processors .dfa
19
19
20
- import scala .collection .mutable . ArrayBuffer
20
+ import scala .collection .compat . immutable . ArraySeq
21
21
22
22
import org .apache .daffodil .runtime1 .dsom .DPathCompileInfo
23
23
import org .apache .daffodil .runtime1 .processors .CharDelim
@@ -44,14 +44,14 @@ object CreateDelimiterDFA {
44
44
outputNewLine : String
45
45
): DFADelimiter = {
46
46
47
- val allStates : ArrayBuffer [State ] = ArrayBuffer .empty
47
+ val allStates : Array [State ] = new Array [ State ](delimiter.length)
48
48
49
49
buildTransitions(delimiter, allStates, false )
50
50
51
51
val unparseValue = delimiter.map { _.unparseValue(outputNewLine) }.mkString
52
52
new DFADelimiterImplUnparse (
53
53
delimType,
54
- allStates.reverse.toArray ,
54
+ allStates.reverse,
55
55
delimiterStr,
56
56
unparseValue,
57
57
ci.schemaFileLocation
@@ -70,13 +70,13 @@ object CreateDelimiterDFA {
70
70
ignoreCase : Boolean
71
71
): DFADelimiter = {
72
72
73
- val allStates : ArrayBuffer [State ] = ArrayBuffer .empty
73
+ val allStates : Array [State ] = new Array [ State ](delimiter.length)
74
74
75
75
buildTransitions(delimiter, allStates, ignoreCase)
76
76
77
77
new DFADelimiterImpl (
78
78
delimType,
79
- allStates.reverse.toArray ,
79
+ allStates.reverse,
80
80
delimiterStr,
81
81
ci.schemaFileLocation
82
82
)
@@ -95,7 +95,7 @@ object CreateDelimiterDFA {
95
95
val d = new Delimiter ()
96
96
d.compileDelimiter(delimiterStr, ignoreCase)
97
97
val db = d.delimBuf
98
- apply(delimType, ci, db , delimiterStr, ignoreCase)
98
+ apply(delimType, ci, ArraySeq .unsafeWrapArray(db) , delimiterStr, ignoreCase)
99
99
}
100
100
101
101
/**
@@ -111,7 +111,7 @@ object CreateDelimiterDFA {
111
111
val d = new Delimiter ()
112
112
d.compileDelimiter(delimiterStr, false )
113
113
val db = d.delimBuf
114
- apply(delimType, ci, db , delimiterStr, outputNewLine)
114
+ apply(delimType, ci, ArraySeq .unsafeWrapArray(db) , delimiterStr, outputNewLine)
115
115
}
116
116
117
117
/**
@@ -147,7 +147,7 @@ object CreateDelimiterDFA {
147
147
d : DelimBase ,
148
148
nextState : Int ,
149
149
stateNum : Int ,
150
- allStates : ArrayBuffer [State ],
150
+ allStates : Array [State ],
151
151
ignoreCase : Boolean
152
152
): DelimStateBase = {
153
153
@@ -176,7 +176,7 @@ object CreateDelimiterDFA {
176
176
177
177
private def buildTransitions (
178
178
delim : Seq [DelimBase ],
179
- allStates : ArrayBuffer [State ],
179
+ allStates : Array [State ],
180
180
ignoreCase : Boolean
181
181
): State = {
182
182
assert(! delim.isEmpty)
@@ -186,26 +186,33 @@ object CreateDelimiterDFA {
186
186
private def buildTransitions (
187
187
nextState : DelimStateBase ,
188
188
delim : Seq [DelimBase ],
189
- allStates : ArrayBuffer [State ],
189
+ allStates : Array [State ],
190
190
ignoreCase : Boolean
191
191
): State = {
192
192
193
- if (delim.isEmpty && nextState != null ) {
194
- // We are initial state
195
- nextState.stateName = " StartState" // "PTERM0"
196
- return nextState
193
+ var i = 0
194
+ var remainingDelim = delim
195
+ var currentNextState = nextState
196
+
197
+ while (remainingDelim.nonEmpty) {
198
+ val currentState = getState(
199
+ remainingDelim.head,
200
+ if (currentNextState == null ) DFA .FinalState else currentNextState.stateNum,
201
+ remainingDelim.length - 1 ,
202
+ allStates,
203
+ ignoreCase
204
+ )
205
+ remainingDelim = remainingDelim.tail
206
+ allStates(i) = currentState
207
+ currentNextState = currentState
208
+ i += 1
197
209
}
198
210
199
- val currentState = getState(
200
- delim(0 ),
201
- if (nextState == null ) DFA .FinalState else nextState.stateNum,
202
- delim.length - 1 ,
203
- allStates,
204
- ignoreCase
205
- )
206
- val rest = delim.tail
211
+ if (currentNextState != null ) {
212
+ // We are initial state
213
+ currentNextState.stateName = " StartState" // "PTERM0"
214
+ }
207
215
208
- allStates += currentState
209
- return buildTransitions(currentState, rest, allStates, ignoreCase)
216
+ currentNextState
210
217
}
211
218
}
0 commit comments