Przeglądaj źródła

New: Normalization

Leonardo Di Donato 8 lat temu
rodzic
commit
db8ec4985f
2 zmienionych plików z 14 dodań i 23 usunięć
  1. 13 23
      urn.go
  2. 1 0
      urn_test.go

+ 13 - 23
urn.go

@@ -1,7 +1,6 @@
 package urn
 
 import (
-	"fmt"
 	"strings"
 
 	pcre "github.com/gijsbers/go-pcre"
@@ -16,7 +15,7 @@ var re = `^
 $`
 
 var pattern = pcre.MustCompile(re, pcre.EXTENDED)
-var hexrepr = pcre.MustCompile("[%][a-f0-9]{2}", pcre.CASELESS)
+var hexrepr = pcre.MustCompile("[%][A-F0-9]{2}", 0)
 
 // URN represents an Uniform Resource Name.
 //
@@ -61,32 +60,23 @@ func (u *URN) String() string {
 
 // Normalize is ...
 func (u *URN) Normalize() *URN {
-	matcher := hexrepr.MatcherString(u.SS, 0)
-	results := matcher.ExtractString()
-
-	fmt.Println("results> ", results)
-	// find all hex within u.SS
-	// lowercase any match
-	// lowercase u.ID
-	// reconstruct string
+	norm := ""
+	ss := u.SS
+	matcher := hexrepr.MatcherString(ss, 0)
+	for matcher.MatchString(ss, 0) {
+		indexes := matcher.Index()
+		from := indexes[0]
+		to := indexes[1]
+		norm += ss[:from] + strings.ToLower(ss[from:to])
+		ss = ss[to:]
+	}
+	norm += ss
 
 	return &URN{
 		ID: strings.ToLower(u.ID),
-		// SS: ...
-	}
-}
-
-/*
-func matchAll(re pcre.Regexp, subject []byte, flags int) [][]byte {
-	m := re.Matcher(subject, 0)
-	all := [][]byte{}
-	for m.Match(subject, flags) {
-		all = append(all, subject[m.ovector[0]:m.ovector[1]])
-		subject = subject[m.ovector[1]:]
+		SS: norm,
 	}
-	return all
 }
-*/
 
 // Equal is ...
 func (u *URN) Equal(x *URN) bool {

+ 1 - 0
urn_test.go

@@ -14,6 +14,7 @@ func TestUrnParse(t *testing.T) {
 			require.True(t, tt.ok, herror(ii, tt))
 			require.Equal(t, tt.obj.ID, urn.ID, herror(ii, tt))
 			require.Equal(t, tt.obj.SS, urn.SS, herror(ii, tt))
+			// (todo) > test normalized version vs expected one
 		} else {
 			require.False(t, tt.ok, herror(ii, tt))
 			require.Empty(t, urn, herror(ii, tt))