]> 127.0.0.1 Git - part/.git/commitdiff
UnescapeUnicode v0.9.0
authorqydysky <qydysky@foxmail.com>
Tue, 10 May 2022 11:03:31 +0000 (19:03 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 10 May 2022 11:03:31 +0000 (19:03 +0800)
String.go [deleted file]
strings/Strings.go [new file with mode: 0644]
strings/Strings_test.go [new file with mode: 0644]

diff --git a/String.go b/String.go
deleted file mode 100644 (file)
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 (file)
index 0000000..6838d2a
--- /dev/null
@@ -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 (file)
index 0000000..2d070f9
--- /dev/null
@@ -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)
+}