Wrote MarshalText() and UnmarshalText() to implement TextMarshaler and TextUnmarshaler
This commit is contained in:
		| @@ -31,6 +31,22 @@ func (re Reg) String() string { | |||||||
| 	return re.str | 	return re.str | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // MarshalText implements [encoding.TextMarshaler]. The output is equivalent to that of [Reg.String]. | ||||||
|  | // Any flags passed as arguments (including calling [Reg.Longest]) are lost. | ||||||
|  | func (re *Reg) MarshalText() ([]byte, error) { | ||||||
|  | 	return []byte(re.String()), nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // UnmarshalText implements [encoding.TextUnmarshaler]. It calls [Reg.Compile] on the given byte-slice. If it returns successfully, | ||||||
|  | // then the result of the compilation is stored in re. The result of [Reg.Compile] is returned. | ||||||
|  | func (re *Reg) UnmarshalText(text []byte) error { | ||||||
|  | 	newReg, err := Compile(string(text)) | ||||||
|  | 	if err == nil { | ||||||
|  | 		*re = newReg | ||||||
|  | 	} | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
| func (re *Reg) Longest() { | func (re *Reg) Longest() { | ||||||
| 	re.preferLongest = true | 	re.preferLongest = true | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user