Skip to content

Commit 65cdf3c

Browse files
feat(*): code identifyMarkedArea & use it in automaton
- will now allow counting backwards from the lastIndex - will never yield empty string as entity's value
1 parent c67a8aa commit 65cdf3c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/automaton.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
/* eslint-disable no-console */
3434
/* eslint-disable guard-for-in */
35-
var composePatterns = require( './compose-patterns.js' );
35+
const composePatterns = require( './compose-patterns.js' );
36+
const identifyMarkedArea = require( './identify-marked-area.js' );
3637

3738
const eosTokenN = 2070000;
3839
const eosTokenX = '$%^EoS^%$';
@@ -172,7 +173,7 @@ var simpleFSM = function ( cache, token2Ignore ) {
172173
if ( mark ) {
173174
// Update last element of `mark` to simplifies computations during fsm
174175
// execution. Update must happen as a deep copy & not directly!
175-
markedStates[ state ] = [ mark[ 0 ], ( length - mark[ 1 ] - 1 ) ];
176+
markedStates[ state ] = identifyMarkedArea( mark, length );
176177
}
177178

178179
if ( customProperty !== undefined ) {
@@ -261,8 +262,14 @@ var simpleFSM = function ( cache, token2Ignore ) {
261262

262263
var customProperty = customPropertyAtStates[ m0 ];
263264
if ( mark ) {
265+
// `match[ 1 ]` will now point to the index of the token where
266+
// the entity should end because `match[ 0 ]` is pointing to the index
267+
// of the token of detected entity's start and by adding `mark[ 1 ]`
268+
// i.e. the `lastIndex` to it, we get the required value.
269+
match[ 1 ] = match[ 0 ] + mark[ 1 ];
270+
// For `match[ 0 ]`, simply adding `mark[ 0 ]` i.e. `firstIndex` yields
271+
// the desried value.
264272
match[ 0 ] += mark[ 0 ];
265-
match[ 1 ] -= mark[ 1 ];
266273
}
267274

268275
// Removed `customProperty !== undefined &&` check while coding pos experiment

src/identify-marked-area.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const identifyMarkedArea = function ( mark, length ) {
2+
// Length Minus 1.
3+
const lm1 = length - 1;
4+
let [ firstIndex, lastIndex ] = mark;
5+
6+
if ( firstIndex < 0 ) firstIndex += length;
7+
firstIndex = Math.max( firstIndex, 0 );
8+
if ( firstIndex > lm1 ) firstIndex = 0;
9+
10+
if ( lastIndex < 0 ) lastIndex += length;
11+
lastIndex = Math.min( lastIndex, lm1 );
12+
if ( lastIndex < firstIndex ) lastIndex = lm1;
13+
14+
return [ firstIndex, lastIndex ];
15+
}; // identifyMarkedArea()
16+
17+
module.exports = identifyMarkedArea;

0 commit comments

Comments
 (0)