]> Cypherpunks repositories - gostls13.git/commitdiff
math/big,regexp: implement the encoding.TextAppender interface
authorapocelipes <seve3r@outlook.com>
Thu, 15 Aug 2024 18:13:58 +0000 (18:13 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 15 Aug 2024 23:43:00 +0000 (23:43 +0000)
For #62384

Change-Id: I1557704c6a0f9c6f3b9aad001374dd5cdbc99065
GitHub-Last-Rev: c258d18ccedab5feeb481a2431d5647bde7e5c58
GitHub-Pull-Request: golang/go#68893
Reviewed-on: https://go-review.googlesource.com/c/go/+/605758
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>

12 files changed:
api/next/62384.txt
doc/next/6-stdlib/99-minor/math/big/62384.md [new file with mode: 0644]
doc/next/6-stdlib/99-minor/regexp/62384.md [new file with mode: 0644]
src/math/big/floatmarsh.go
src/math/big/floatmarsh_test.go
src/math/big/intmarsh.go
src/math/big/intmarsh_test.go
src/math/big/ratconv.go
src/math/big/ratmarsh.go
src/math/big/ratmarsh_test.go
src/regexp/all_test.go
src/regexp/regexp.go

index ece5d9fd801445dc0b1bb8e288c1b861954382d6..af7fc5363cca0d73ca5b5ac104bc29070b5924d7 100644 (file)
@@ -5,3 +5,7 @@ pkg encoding, type TextAppender interface, AppendText([]uint8) ([]uint8, error)
 pkg net/url, method (*URL) AppendBinary([]uint8) ([]uint8, error) #62384
 pkg log/slog, method (Level) AppendText([]uint8) ([]uint8, error) #62384
 pkg log/slog, method (*LevelVar) AppendText([]uint8) ([]uint8, error) #62384
+pkg math/big, method (*Float) AppendText([]uint8) ([]uint8, error) #62384
+pkg math/big, method (*Int) AppendText([]uint8) ([]uint8, error) #62384
+pkg math/big, method (*Rat) AppendText([]uint8) ([]uint8, error) #62384
+pkg regexp, method (*Regexp) AppendText([]uint8) ([]uint8, error) #62384
diff --git a/doc/next/6-stdlib/99-minor/math/big/62384.md b/doc/next/6-stdlib/99-minor/math/big/62384.md
new file mode 100644 (file)
index 0000000..4a94188
--- /dev/null
@@ -0,0 +1 @@
+[Float], [Int] and [Rat] now implement the [encoding.TextAppender] interface.
diff --git a/doc/next/6-stdlib/99-minor/regexp/62384.md b/doc/next/6-stdlib/99-minor/regexp/62384.md
new file mode 100644 (file)
index 0000000..9dcdbad
--- /dev/null
@@ -0,0 +1 @@
+[Regexp] now implements the [encoding.TextAppender] interface.
index 16be94697147ed0b71a2c6b86a5ddc89834b358e..39c22077db6c142a89f8ca022a02a2ff44f6d77c 100644 (file)
@@ -106,15 +106,21 @@ func (z *Float) GobDecode(buf []byte) error {
        return nil
 }
 
-// MarshalText implements the [encoding.TextMarshaler] interface.
+// AppendText implements the [encoding.TextAppender] interface.
 // Only the [Float] value is marshaled (in full precision), other
 // attributes such as precision or accuracy are ignored.
-func (x *Float) MarshalText() (text []byte, err error) {
+func (x *Float) AppendText(b []byte) ([]byte, error) {
        if x == nil {
-               return []byte("<nil>"), nil
+               return append(b, "<nil>"...), nil
        }
-       var buf []byte
-       return x.Append(buf, 'g', -1), nil
+       return x.Append(b, 'g', -1), nil
+}
+
+// MarshalText implements the [encoding.TextMarshaler] interface.
+// Only the [Float] value is marshaled (in full precision), other
+// attributes such as precision or accuracy are ignored.
+func (x *Float) MarshalText() (text []byte, err error) {
+       return x.AppendText(nil)
 }
 
 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
index 20def68a6d3c613a4ae89362fc8943837195b185..339cb537642c1becfe9c17d30b70895f0de02820 100644 (file)
@@ -171,3 +171,46 @@ func TestFloatGobDecodeInvalid(t *testing.T) {
                }
        }
 }
+
+func TestFloatAppendText(t *testing.T) {
+       for _, test := range floatVals {
+               for _, sign := range []string{"", "+", "-"} {
+                       for _, prec := range []uint{0, 1, 2, 10, 53, 64, 100, 1000} {
+                               if prec > 53 && testing.Short() {
+                                       continue
+                               }
+                               x := sign + test
+                               var tx Float
+                               _, _, err := tx.SetPrec(prec).Parse(x, 0)
+                               if err != nil {
+                                       t.Errorf("parsing of %s (prec = %d) failed (invalid test case): %v", x, prec, err)
+                                       continue
+                               }
+                               buf := make([]byte, 4, 32)
+                               b, err := tx.AppendText(buf)
+                               if err != nil {
+                                       t.Errorf("marshaling of %v (prec = %d) failed: %v", &tx, prec, err)
+                                       continue
+                               }
+                               var rx Float
+                               rx.SetPrec(prec)
+                               if err := rx.UnmarshalText(b[4:]); err != nil {
+                                       t.Errorf("unmarshaling of %v (prec = %d) failed: %v", &tx, prec, err)
+                                       continue
+                               }
+                               if rx.Cmp(&tx) != 0 {
+                                       t.Errorf("AppendText of %v (prec = %d) failed: got %v want %v", &tx, prec, &rx, &tx)
+                               }
+                       }
+               }
+       }
+}
+
+func TestFloatAppendTextNil(t *testing.T) {
+       var x *Float
+       buf := make([]byte, 4, 16)
+       data, _ := x.AppendText(buf)
+       if string(data[4:]) != "<nil>" {
+               t.Errorf("got %q, want <nil>", data[4:])
+       }
+}
index 56eeefb884d8c26f77509b2f8898c3f45f574365..858ca0faba58906b0a0ef3452686ad31780174da 100644 (file)
@@ -45,12 +45,14 @@ func (z *Int) GobDecode(buf []byte) error {
        return nil
 }
 
+// AppendText implements the [encoding.TextAppender] interface.
+func (x *Int) AppendText(b []byte) (text []byte, err error) {
+       return x.Append(b, 10), nil
+}
+
 // MarshalText implements the [encoding.TextMarshaler] interface.
 func (x *Int) MarshalText() (text []byte, err error) {
-       if x == nil {
-               return []byte("<nil>"), nil
-       }
-       return x.abs.itoa(x.neg, 10), nil
+       return x.AppendText(nil)
 }
 
 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
index 8e7d29f9ddc71994f81195707b63acc34be2cd32..681f3dd9468e4a84aad30c363f811480fa7428b8 100644 (file)
@@ -132,3 +132,36 @@ func TestIntXMLEncoding(t *testing.T) {
                }
        }
 }
+
+func TestIntAppendText(t *testing.T) {
+       for _, test := range encodingTests {
+               for _, sign := range []string{"", "+", "-"} {
+                       x := sign + test
+                       var tx Int
+                       tx.SetString(x, 10)
+                       buf := make([]byte, 4, 32)
+                       b, err := tx.AppendText(buf)
+                       if err != nil {
+                               t.Errorf("marshaling of %s failed: %s", &tx, err)
+                               continue
+                       }
+                       var rx Int
+                       if err := rx.UnmarshalText(b[4:]); err != nil {
+                               t.Errorf("unmarshaling of %s failed: %s", &tx, err)
+                               continue
+                       }
+                       if rx.Cmp(&tx) != 0 {
+                               t.Errorf("AppendText of %s failed: got %s want %s", &tx, &rx, &tx)
+                       }
+               }
+       }
+}
+
+func TestIntAppendTextNil(t *testing.T) {
+       var x *Int
+       buf := make([]byte, 4, 16)
+       data, _ := x.AppendText(buf)
+       if string(data[4:]) != "<nil>" {
+               t.Errorf("got %q, want <nil>", data[4:])
+       }
+}
index dd99aecdc04af0982deb588205e9b0a5cb509f55..12f9888c3701ce446a99a6aa1c9b425d8966e06d 100644 (file)
@@ -299,12 +299,13 @@ func scanExponent(r io.ByteScanner, base2ok, sepOk bool) (exp int64, base int, e
 
 // String returns a string representation of x in the form "a/b" (even if b == 1).
 func (x *Rat) String() string {
-       return string(x.marshal())
+       return string(x.marshal(nil))
 }
 
-// marshal implements String returning a slice of bytes
-func (x *Rat) marshal() []byte {
-       var buf []byte
+// marshal implements [Rat.String] returning a slice of bytes.
+// It appends the string representation of x in the form "a/b" (even if b == 1) to buf,
+// and returns the extended buffer.
+func (x *Rat) marshal(buf []byte) []byte {
        buf = x.a.Append(buf, 10)
        buf = append(buf, '/')
        if len(x.b.abs) != 0 {
index 69628294531ff8c63f5c93aa6988ef843be8dc83..0457fc9517de2911285f4faa92c09b600a792d89 100644 (file)
@@ -68,12 +68,17 @@ func (z *Rat) GobDecode(buf []byte) error {
        return nil
 }
 
-// MarshalText implements the [encoding.TextMarshaler] interface.
-func (x *Rat) MarshalText() (text []byte, err error) {
+// AppendText implements the [encoding.TextAppender] interface.
+func (x *Rat) AppendText(b []byte) ([]byte, error) {
        if x.IsInt() {
-               return x.a.MarshalText()
+               return x.a.AppendText(b)
        }
-       return x.marshal(), nil
+       return x.marshal(b), nil
+}
+
+// MarshalText implements the [encoding.TextMarshaler] interface.
+func (x *Rat) MarshalText() (text []byte, err error) {
+       return x.AppendText(nil)
 }
 
 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
index 15c933efa6d43bb14926abf82dfb83285b042601..7d139bcad8efa77351bf0fd1cb2fec4a98b9030b 100644 (file)
@@ -136,3 +136,26 @@ func TestRatGobDecodeShortBuffer(t *testing.T) {
                }
        }
 }
+
+func TestRatAppendText(t *testing.T) {
+       for _, num := range ratNums {
+               for _, denom := range ratDenoms {
+                       var tx Rat
+                       tx.SetString(num + "/" + denom)
+                       buf := make([]byte, 4, 32)
+                       b, err := tx.AppendText(buf)
+                       if err != nil {
+                               t.Errorf("marshaling of %s failed: %s", &tx, err)
+                               continue
+                       }
+                       var rx Rat
+                       if err := rx.UnmarshalText(b[4:]); err != nil {
+                               t.Errorf("unmarshaling of %s failed: %s", &tx, err)
+                               continue
+                       }
+                       if rx.Cmp(&tx) != 0 {
+                               t.Errorf("AppendText of %s failed: got %s want %s", &tx, &rx, &tx)
+                       }
+               }
+       }
+}
index c9c046b61d09beeb9848e2db4b9a755d6e8c3b3f..ead184d2868b025d3409a0261ddd158ae1596156 100644 (file)
@@ -965,6 +965,21 @@ func TestUnmarshalText(t *testing.T) {
                if unmarshaled.String() != goodRe[i] {
                        t.Errorf("UnmarshalText returned unexpected value: %s", unmarshaled.String())
                }
+
+               buf := make([]byte, 4, 32)
+               marshalAppend, err := re.AppendText(buf)
+               if err != nil {
+                       t.Errorf("regexp %#q failed to marshal: %s", re, err)
+                       continue
+               }
+               marshalAppend = marshalAppend[4:]
+               if err := unmarshaled.UnmarshalText(marshalAppend); err != nil {
+                       t.Errorf("regexp %#q failed to unmarshal: %s", re, err)
+                       continue
+               }
+               if unmarshaled.String() != goodRe[i] {
+                       t.Errorf("UnmarshalText returned unexpected value: %s", unmarshaled.String())
+               }
        }
        t.Run("invalid pattern", func(t *testing.T) {
                re := new(Regexp)
index e06099425ebcaf7800c9c0d5cd3d460771ae7547..253415fb6a44c6edef7c2af5d43054441332231f 100644 (file)
@@ -1277,14 +1277,22 @@ func (re *Regexp) Split(s string, n int) []string {
        return strings
 }
 
-// MarshalText implements [encoding.TextMarshaler]. The output
+// AppendText implements [encoding.TextAppender]. The output
 // matches that of calling the [Regexp.String] method.
 //
 // Note that the output is lossy in some cases: This method does not indicate
 // POSIX regular expressions (i.e. those compiled by calling [CompilePOSIX]), or
 // those for which the [Regexp.Longest] method has been called.
+func (re *Regexp) AppendText(b []byte) ([]byte, error) {
+       return append(b, re.String()...), nil
+}
+
+// MarshalText implements [encoding.TextMarshaler]. The output
+// matches that of calling the [Regexp.AppendText] method.
+//
+// See [Regexp.AppendText] for more information.
 func (re *Regexp) MarshalText() ([]byte, error) {
-       return []byte(re.String()), nil
+       return re.AppendText(nil)
 }
 
 // UnmarshalText implements [encoding.TextUnmarshaler] by calling