pkg/strings: fix MinRunes
Change-Id: I8d0d39dc8206dcf47f9120ab7cec52107fe928e9
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3105
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/builtin_test.go b/cue/builtin_test.go
index 573da3f..73d3fd7 100644
--- a/cue/builtin_test.go
+++ b/cue/builtin_test.go
@@ -304,8 +304,23 @@
test("strings", `strings.MinRunes(1) & "e"`),
`"e"`,
}, {
- test("strings", `strings.MinRunes(0) & "e"`),
- `_|_(invalid value "e" (does not satisfy strings.MinRunes(0)))`,
+ test("strings", `strings.MaxRunes(0) & "e"`),
+ `_|_(invalid value "e" (does not satisfy strings.MaxRunes(0)))`,
+ }, {
+ test("strings", `strings.MaxRunes(0) & ""`),
+ `""`,
+ }, {
+ test("strings", `strings.MinRunes(3) & "hello"`),
+ `"hello"`,
+ }, {
+ test("strings", `strings.MaxRunes(10) & "hello"`),
+ `"hello"`,
+ }, {
+ test("strings", `strings.MaxRunes(3) & "hello"`),
+ `_|_(invalid value "hello" (does not satisfy strings.MaxRunes(3)))`,
+ }, {
+ test("strings", `strings.MinRunes(10) & "hello"`),
+ `_|_(invalid value "hello" (does not satisfy strings.MinRunes(10)))`,
}, {
test("struct", `struct.MinFields(0) & ""`),
`_|_(conflicting values MinFields (0) and "" (mismatched types struct and string))`,
diff --git a/cue/builtins.go b/cue/builtins.go
index 33262ef..bdb686b 100644
--- a/cue/builtins.go
+++ b/cue/builtins.go
@@ -2025,10 +2025,11 @@
Params: []kind{stringKind, intKind},
Result: boolKind,
Func: func(c *callCtxt) {
- s, max := c.string(0), c.int(1)
+ s, min := c.string(0), c.int(1)
c.ret = func() interface{} {
- return len([]rune(s)) <= max
+ fmt.Println(len([]rune(s)))
+ return len([]rune(s)) >= min
}()
},
}, {
diff --git a/pkg/strings/manual.go b/pkg/strings/manual.go
index de3436d..508466d 100644
--- a/pkg/strings/manual.go
+++ b/pkg/strings/manual.go
@@ -56,11 +56,11 @@
// MinRunes reports whether the number of runes (Unicode codepoints) in a string
// is at least a certain minimum. MinRunes can be used a a field constraint to
// except all strings for which this property holds.
-func MinRunes(s string, max int) bool {
+func MinRunes(s string, min int) bool {
// TODO: CUE strings cannot be invalid UTF-8. In case this changes, we need
// to use the following conversion to count properly:
// s, _ = unicodeenc.UTF8.NewDecoder().String(s)
- return len([]rune(s)) <= max
+ return len([]rune(s)) >= min
}
// MaxRunes reports whether the number of runes (Unicode codepoints) in a string