public void TestCaptureGroupMatcher() { var m = Matcher.CaptureGroup("g", Matcher.Repeat(Matcher.Char('a'), 1)); { var c = new MatchContent("abc", 0, MatchDirection.Forward); Assert.Equal(1, m.Match(c)); var g = c.GetGroup("g"); Assert.NotNull(g); Assert.Equal(0, g.Value.StartIndex); Assert.Equal(1, g.Value.Count); Assert.Equal("a", g.Value.GetValue()); } { var c = new MatchContent("abc", 0, MatchDirection.Backward); Assert.Equal(Matcher.NotMatch, m.Match(c)); var g = c.GetGroup("g"); Assert.Null(g); } { var c = new MatchContent("abc", 1, MatchDirection.Forward); Assert.Equal(Matcher.NotMatch, m.Match(c)); var g = c.GetGroup("g"); Assert.Null(g); } { var c = new MatchContent("abc", 1, MatchDirection.Backward); Assert.Equal(1, m.Match(c)); var g = c.GetGroup("g"); Assert.NotNull(g); Assert.Equal(0, g.Value.StartIndex); Assert.Equal(1, g.Value.Count); Assert.Equal("a", g.Value.GetValue()); } { var c = new MatchContent("aaa", 0, MatchDirection.Forward); Assert.Equal(3, m.Match(c)); var g = c.GetGroup("g"); Assert.NotNull(g); Assert.Equal(0, g.Value.StartIndex); Assert.Equal(3, g.Value.Count); Assert.Equal("aaa", g.Value.GetValue()); } { var c = new MatchContent("aaa", 3, MatchDirection.Backward); Assert.Equal(3, m.Match(c)); var g = c.GetGroup("g"); Assert.NotNull(g); Assert.Equal(0, g.Value.StartIndex); Assert.Equal(3, g.Value.Count); Assert.Equal("aaa", g.Value.GetValue()); } }
public override int Match(MatchContent content) { var g = content.GetGroup(_groupName); if (g == null) { return(NotMatch); } var text = g.Value.GetValue(); if (content.Length < text.Length) { return(NotMatch); } for (int i = 0; i < text.Length; i++) { if (content[i] != text[i]) { return(NotMatch); } } return(text.Length); }
public override int Match(MatchContent content) { var g = content.GetGroup(_groupName); if (g == null) { return(NotMatch); } var text = g.Value.GetValue(); if (content.Length < text.Length) { return(NotMatch); } if (content.Direction == MatchDirection.Forward) { for (int i = 0; i < text.Length; i++) { if (content[i] != text[i]) { return(NotMatch); } } } else { for (int i = 0; i < text.Length; i++) { if (content[i] != text[text.Length - 1 - i]) { return(NotMatch); } } } return(text.Length); }