From: qydysky Date: Tue, 10 May 2022 11:03:31 +0000 (+0800) Subject: UnescapeUnicode X-Git-Tag: v0.9.0 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=d5979c0e04d3aefbabde3fa65118cdf2936a2225;p=part%2F.git UnescapeUnicode --- diff --git a/String.go b/String.go deleted file mode 100644 index be0b269..0000000 --- a/String.go +++ /dev/null @@ -1,30 +0,0 @@ -package part - -import ( - "math/rand" - "time" - "bytes" -) - -type stringl struct{} - -func Stringf() *stringl { - return &stringl{} -} - -func (t *stringl)Rand(typel,leng int) string { - source := "0123456789" - if typel > 0 {source+="abcdefghijklmnopqrstuvwxyz"} - if typel > 1 {source+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"} - - Letters := []rune(source) - LettersL := len(Letters) - r := rand.New(rand.NewSource(time.Now().UnixNano())) - var bb bytes.Buffer - bb.Grow(leng) - for i := 0; i < leng; i++ { - bb.WriteRune(Letters[r.Intn(LettersL)]) - } - return bb.String() - -} diff --git a/strings/Strings.go b/strings/Strings.go new file mode 100644 index 0000000..6838d2a --- /dev/null +++ b/strings/Strings.go @@ -0,0 +1,42 @@ +package part + +import ( + "bytes" + "math/rand" + "strconv" + "strings" + "time" +) + +const ( + Number RandType = 0 + lowNumber RandType = 1 + uppNumber RandType = 2 +) + +type RandType int + +func Rand(typel RandType, leng int) string { + source := "0123456789" + if typel > 0 { + source += "abcdefghijklmnopqrstuvwxyz" + } + if typel > 1 { + source += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + } + + Letters := []rune(source) + LettersL := len(Letters) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + var bb bytes.Buffer + bb.Grow(leng) + for i := 0; i < leng; i++ { + bb.WriteRune(Letters[r.Intn(LettersL)]) + } + return bb.String() + +} + +func UnescapeUnicode(raw string) (string, error) { + return strconv.Unquote(strings.Replace(strconv.Quote(raw), `\\u`, `\u`, -1)) +} diff --git a/strings/Strings_test.go b/strings/Strings_test.go new file mode 100644 index 0000000..2d070f9 --- /dev/null +++ b/strings/Strings_test.go @@ -0,0 +1,17 @@ +package part + +import ( + "testing" +) + +func Test_Rand(t *testing.T) { + t.Log(Rand(Number, 14)) + t.Log(Rand(lowNumber, 14)) + t.Log(Rand(uppNumber, 14)) +} + +func Test_UnescapeUnicode(t *testing.T) { + s, e := UnescapeUnicode("\uB155, \uC138\uC0C1(\u4E16\u4E0A). \u263a") + t.Log(s) + t.Log(e) +}