Skip to content

Commit

Permalink
cue/literal: use strconv.IsGraphic
Browse files Browse the repository at this point in the history
This code was a direct copy of strconv.IsGraphic with no changes,
so there isn't a need to duplicate it.
We already used strconv.IsPrint in this Go file,
and we already used both APIs in other Go packages in our module.

While here, as suggested by Matthew, use parentheses to make the
boolean logic clear with respect to operator precedence.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I4d4f6416e8a265fee89b3ed357fdb3d4771d0e7f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198060
Reviewed-by: Matthew Sackman <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Jul 19, 2024
1 parent efdb072 commit acdd41f
Showing 1 changed file with 1 addition and 49 deletions.
50 changes: 1 addition & 49 deletions cue/literal/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (f *Form) appendEscapedRune(buf []byte, r rune) []byte {
buf = append(buf, byte(r))
return buf
}
} else if strconv.IsPrint(r) || f.graphicOnly && isInGraphicList(r) {
} else if strconv.IsPrint(r) || (f.graphicOnly && strconv.IsGraphic(r)) {
buf = utf8.AppendRune(buf, r)
return buf
}
Expand Down Expand Up @@ -319,51 +319,3 @@ func (f *Form) requiredHashCount(s string) int {
}
return hashCount
}

// isInGraphicList reports whether the rune is in the isGraphic list. This separation
// from IsGraphic allows quoteWith to avoid two calls to IsPrint.
// Should be called only if IsPrint fails.
func isInGraphicList(r rune) bool {
// We know r must fit in 16 bits - see makeisprint.go.
if r > 0xFFFF {
return false
}
rr := uint16(r)
i := bsearch16(isGraphic, rr)
return i < len(isGraphic) && rr == isGraphic[i]
}

// bsearch16 returns the smallest i such that a[i] >= x.
// If there is no such i, bsearch16 returns len(a).
func bsearch16(a []uint16, x uint16) int {
i, j := 0, len(a)
for i < j {
h := i + (j-i)/2
if a[h] < x {
i = h + 1
} else {
j = h
}
}
return i
}

// isGraphic lists the graphic runes not matched by IsPrint.
var isGraphic = []uint16{
0x00a0,
0x1680,
0x2000,
0x2001,
0x2002,
0x2003,
0x2004,
0x2005,
0x2006,
0x2007,
0x2008,
0x2009,
0x200a,
0x202f,
0x205f,
0x3000,
}

0 comments on commit acdd41f

Please sign in to comment.