Small fixup for #1081.

This commit is contained in:
Qi Xiao 2020-08-13 22:14:16 +01:00
parent 1124c10b56
commit 0c72fcd5b1
2 changed files with 16 additions and 7 deletions

View File

@ -120,23 +120,32 @@ func fromCodepoints(nums ...int) (string, error) {
return "", errs.OutOfRange{
What: "codepoint",
ValidLow: 0, ValidHigh: unicode.MaxRune,
Actual: strconv.Itoa(num)}
Actual: hex(num),
}
}
if !utf8.ValidRune(rune(num)) {
return "", errs.BadValue{
What: "argument to str:from-codepoints",
Valid: "valid Unicode codepoint",
Actual: "0x" + strconv.FormatInt(int64(num), 16)}
Actual: hex(num),
}
}
b.WriteRune(rune(num))
}
return b.String(), nil
}
func hex(i int) string {
if i < 0 {
return "-0x" + strconv.FormatInt(-int64(i), 16)
}
return "0x" + strconv.FormatInt(int64(i), 16)
}
//elvdoc:fn from-utf8-bytes
//
// ```elvish
// str:from-from-utf8-bytes $number...
// str:from-utf8-bytes $number...
// ```
//
// Outputs a string consisting of the given Unicode bytes. Example:
@ -372,7 +381,7 @@ func split(fm *eval.Frame, opts maxOpt, sep, s string) {
// str:to-codepoints $string
// ```
//
// Output value of each codepoint in `$string`, in hexadecimal. Examples:
// Outputs value of each codepoint in `$string`, in hexadecimal. Examples:
//
// ```elvish-transcript
// ~> str:to-codepoints a
@ -413,7 +422,7 @@ func toCodepoints(fm *eval.Frame, s string) {
// str:to-utf8-bytes $string
// ```
//
// Output value of each byte in `$string`, in hexadecimal. Examples:
// Outputs value of each byte in `$string`, in hexadecimal. Examples:
//
// ```elvish-transcript
// ~> str:to-utf8-bytes a

View File

@ -39,11 +39,11 @@ func TestStr(t *testing.T) {
That(`str:from-codepoints -0x1`).ThrowsCause(errs.OutOfRange{
What: "codepoint",
ValidLow: 0, ValidHigh: unicode.MaxRune,
Actual: strconv.Itoa(-1)}),
Actual: "-0x1"}),
That(fmt.Sprintf(`str:from-codepoints 0x%x`, unicode.MaxRune+1)).ThrowsCause(errs.OutOfRange{
What: "codepoint",
ValidLow: 0, ValidHigh: unicode.MaxRune,
Actual: strconv.Itoa(unicode.MaxRune + 1)}),
Actual: hex(unicode.MaxRune + 1)}),
That(`str:from-codepoints 0xd800`).ThrowsCause(errs.BadValue{
What: "argument to str:from-codepoints",
Valid: "valid Unicode codepoint",