Skip to content

Instantly share code, notes, and snippets.

@shiroyuki
Last active September 21, 2015 15:15
Show Gist options
  • Save shiroyuki/9bd847237b619f643a54 to your computer and use it in GitHub Desktop.
Save shiroyuki/9bd847237b619f643a54 to your computer and use it in GitHub Desktop.
A work-in-progress patch on supporting multiple results to deal with FindAllStringSubmatches.
diff --git a/re/re.go b/re/re.go
index d438921..a2e8f39 100644
--- a/re/re.go
+++ b/re/re.go
@@ -1,46 +1,46 @@
// Package re provides a simplified Regular Expression class
package re
-import "fmt" // for debugging
+//import "fmt" // for debugging
import "regexp"
-type Result struct {
+type SimpleResult struct {
ItemList []string
Dictionary map[string]string
}
-func NewResult(items []string, dictionary map[string]string) Result {
- return Result{
+func NewSimpleResult(items []string, dictionary map[string]string) Result {
+ return SimpleResult{
ItemList: items,
Dictionary: dictionary,
}
}
-func (self *Result) Index(i int) *string {
+func (self *SimpleResult) Index(i int) *string {
value := self.ItemList[i]
return &value
}
-func (self *Result) Key(k string) *string {
+func (self *SimpleResult) Key(k string) *string {
value := self.Dictionary[k]
return &value
}
-func (self *Result) HasAny() bool {
+func (self *SimpleResult) HasAny() bool {
return self.Count() > 0
}
-func (self *Result) Count() int {
+func (self *SimpleResult) Count() int {
return self.CountIndices() + self.CountKeys()
}
-func (self *Result) CountIndices() int {
+func (self *SimpleResult) CountIndices() int {
return len(self.ItemList)
}
-func (self *Result) CountKeys() int {
+func (self *SimpleResult) CountKeys() int {
return len(self.Dictionary)
}
@@ -52,25 +52,35 @@ func Compile(pattern string) Expression {
return Expression{regexp.MustCompile(pattern)}
}
-func (self *Expression) Search(content string) Result {
+func (self *Expression) Search(content string) SimpleResult {
+ matches := self.Internal.FindStringSubmatch(content)
+
+ return self.convertSingleMatchToResult(matches)
+}
+
+func (self *Expression) SearchAll(content string) /* Result */ {
+ matches := self.Internal.FindAllStringSubmatch(content, -1)
+
+ panic("Not yet implemented")
+}
+
+func (self *Expression) convertSingleMatchToSimpleResult(matches []string) SimpleResult {
var itemList []string
var nextIndex int
var dictionary map[string]string
- matches := self.Internal.FindStringSubmatch(content)
-
nextIndex = 0
dictionary = make(map[string]string)
itemList = make([]string, len(matches)) // allocate the memory to the maximum length first.
- fmt.Println(content, matches)
+ //fmt.Println(content, matches)
if len(matches) == 0 {
- return NewResult(itemList, dictionary)
+ return NewSimpleResult(itemList, dictionary)
}
for i, name := range self.Internal.SubexpNames() {
- fmt.Println("I", i, "K", name, "KL", len(name), "V", matches[i])
+ //fmt.Println("I", i, "K", name, "KL", len(name), "V", matches[i])
value := matches[i]
@@ -85,9 +95,9 @@ func (self *Expression) Search(content string) Result {
dictionary[name] = matches[i]
}
- fmt.Println(itemList[:nextIndex], dictionary)
+ //fmt.Println(itemList[:nextIndex], dictionary)
- return NewResult(
+ return NewSimpleResult(
itemList[:nextIndex], // minimize the memory usage by trimming itemList.
dictionary,
)
diff --git a/web/routing.go b/web/routing.go
index a250922..d88d6d5 100644
--- a/web/routing.go
+++ b/web/routing.go
@@ -13,6 +13,7 @@
// requestPath := route.For(params) // expected: /user/shiroyuki
package web
+import "fmt"
import tori_re "../re"
type Route struct {
@@ -39,6 +40,7 @@ func NewRoute(pattern string, reversible bool) Route {
func (self *Route) GetCompiledPattern() *tori_re.Expression {
var compiled tori_re.Expression
var simpleRoutingPattern tori_re.Expression
+ var matches tori_re.Result
if self.RePattern != nil {
return self.RePattern
@@ -53,7 +55,10 @@ func (self *Route) GetCompiledPattern() *tori_re.Expression {
}
// Handle a reversible route.
- simpleRoutingPattern.Search(self.Pattern)
+ simpleRoutingPattern = tori_re.Compile("<[^>]+>")
+ matches = simpleRoutingPattern.Search(self.Pattern)
+
+ fmt.Println(matches.ItemList)
return self.RePattern
}
diff --git a/web/routing_test.go b/web/routing_test.go
index bcbc41b..561fdc7 100644
--- a/web/routing_test.go
+++ b/web/routing_test.go
@@ -31,15 +31,29 @@ func TestWebRoutingRouteNewRouteNormal(t *testing.T) {
localTestWebRoutingRouteNewRoute(t, "/", false)
}
-func TestWebRoutingReversibleRouteGetCompiledPattern(t *testing.T) {
- t.Skip()
+func TestWebRoutingReversibleRouteGetCompiledPatternSimple(t *testing.T) {
var assertion tameshigiri.Assertion
var givenPattern string
var newRoute Route
assertion = tameshigiri.NewAssertion(t)
- givenPattern = "/user/{alias}"
+ givenPattern = "/user/<alias>"
+ newRoute = NewRoute(givenPattern, true)
+
+ compiled := newRoute.GetCompiledPattern()
+
+ assertion.IsTrue(compiled != nil, "Unexpected compiled pattern")
+}
+
+func TestWebRoutingReversibleRouteGetCompiledPatternComplex(t *testing.T) {
+ var assertion tameshigiri.Assertion
+ var givenPattern string
+ var newRoute Route
+
+ assertion = tameshigiri.NewAssertion(t)
+
+ givenPattern = "/user/<alias>/<association>"
newRoute = NewRoute(givenPattern, true)
compiled := newRoute.GetCompiledPattern()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment