Skip to content

Commit

Permalink
Merge pull request #20 from sirkon/issue-19
Browse files Browse the repository at this point in the history
issue-19 | fix
  • Loading branch information
sirkon authored Mar 22, 2019
2 parents b7cd26b + 9260d4d commit c8cb63d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
12 changes: 10 additions & 2 deletions internal/generator/gogen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,24 @@ func (g *Generator) PassHeadCharacters(char string) error {
if err := g.regVar(counter, "int"); err != nil {
return err
}
if err := g.regVar(value, "byte"); err != nil {
itemType := "byte"
if g.useString {
itemType = "rune"
}
if err := g.regVar(value, itemType); err != nil {
return err
}
g.body.Append(srcobj.Literal("\n"))
g.body.Append(srcobj.Comment(fmt.Sprintf("Pass all characters %s at the rest start", char)))
rest := g.rest()
if g.useString {
rest = srcobj.NewCall("string", rest)
}
g.body.Append(
srcobj.For{
I: counter,
Value: value,
Container: g.rest(),
Container: rest,
Body: srcobj.If{
Expr: srcobj.OperatorNEq(srcobj.Raw(value), srcobj.Raw(char)),
Then: srcobj.Break,
Expand Down
4 changes: 3 additions & 1 deletion testing/common.lde
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ Rule =

BeforeLookup = .."abc" Data(string);

CheckPrefix = @"abc" Data(string);
CheckPrefix = @"abc" Data(string);

PassHeadingStringRegression = *'#' Data(string);
28 changes: 28 additions & 0 deletions testing/common_lde.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,31 @@ func (p *CheckPrefix) Extract(line string) (bool, error) {
p.Rest = p.Rest[len(p.Rest):]
return true, nil
}

// PassHeadingStringRegression ...
type PassHeadingStringRegression struct {
Rest string
Data string
}

// Extract ...
func (p *PassHeadingStringRegression) Extract(line string) (bool, error) {
p.Rest = line
var headPassCounter int
var headPassValue rune

// Pass all characters '#' at the rest start
for headPassCounter, headPassValue = range string(p.Rest) {
if headPassValue != '#' {
break
}
}
if headPassCounter > 0 {
p.Rest = p.Rest[headPassCounter:]
}

// Take the rest as Data(string)
p.Data = p.Rest
p.Rest = p.Rest[len(p.Rest):]
return true, nil
}
13 changes: 13 additions & 0 deletions testing/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ func TestCheckPrefix(t *testing.T) {
require.Equal(t, "abc123", e.Data)
require.Equal(t, "", e.Rest)
}

func TestPassHeadingStringRegression(t *testing.T) {
data := "#################################3 123"
var e PassHeadingStringRegression
if ok, err := e.Extract(data); !ok {
if err != nil {
t.Fatal(err)
}
require.True(t, ok)
}
require.Equal(t, "3 123", e.Data)
require.Equal(t, "", e.Rest)
}

0 comments on commit c8cb63d

Please sign in to comment.