diff --git a/helper/bytes.go b/helper/bytes.go deleted file mode 100644 index 4ffffb0..0000000 --- a/helper/bytes.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import "bytes" - -var doublequote = []byte{'"'} - -// Pre-define some comment characters. -var ( - CommentHash = []byte("#") - CommentSlashes = []byte("//") -) - -// RemoveLineComments is a simple function to remove the whole line comment -// that the first non-white character starts with comments, -// and the similar line tail comment. -func RemoveLineComments(data, comments []byte) []byte { - result := make([]byte, 0, len(data)) - for len(data) > 0 { - var line []byte - if index := bytes.IndexByte(data, '\n'); index == -1 { - line = data - data = nil - } else { - line = data[:index] - data = data[index+1:] - } - - orig := line - line = bytes.TrimLeft(line, " \t") - if len(line) == 0 || bytes.HasPrefix(line, comments) { - continue - } - - // Line Suffix Comment - if index := bytes.Index(orig, comments); index == -1 { - result = append(result, orig...) - } else if bytes.IndexByte(orig[index:], '"') == -1 { - result = append(result, bytes.TrimRight(orig[:index], " \t")...) - } else { - if bytes.Count(orig[:index], doublequote)%2 == 0 { - /* The case: ... "...." ... // the trailling comment containing ". */ - result = append(result, bytes.TrimRight(orig[:index], " \t")...) - } else { - /* "//" is contained in a string. */ - result = append(result, orig...) - } - } - result = append(result, '\n') - } - return result -} diff --git a/helper/bytes_test.go b/helper/bytes_test.go deleted file mode 100644 index e310b40..0000000 --- a/helper/bytes_test.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import "fmt" - -func ExampleRemoveLineComments() { - var slashOrig = []byte(` -// line comment 1 -1 - /// line comment 2 -2 ///// line tail comment 3 -3 - 4 - "//": "abc" - "abc" // the trailling comment containing " -5 -`) - - var hashOrig = []byte(` -# line comment 1 -1 - ## line comment 2 -2 #### line tail comment 3 -3 - 4 - "#": "abc" - "abc" # the trailling comment containing " -5 -`) - - fmt.Println("Hash Result:") - fmt.Println(string(RemoveLineComments(hashOrig, CommentHash))) - - fmt.Println("Slash Result:") - fmt.Println(string(RemoveLineComments(slashOrig, CommentSlashes))) - - // Output: - // Hash Result: - // 1 - // 2 - // 3 - // 4 - // "#": "abc" - // "abc" - // 5 - // - // Slash Result: - // 1 - // 2 - // 3 - // 4 - // "//": "abc" - // "abc" - // 5 -} diff --git a/helper/doc.go b/helper/doc.go deleted file mode 100644 index 1b284ba..0000000 --- a/helper/doc.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2021 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package helper provides some helpful functions. -package helper diff --git a/helper/json.go b/helper/json.go deleted file mode 100644 index 53f33fa..0000000 --- a/helper/json.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import ( - "encoding/json" - "io" -) - -// EncodeJSON encodes the value by json into w. -// -// NOTICE: it does not escape the problematic HTML characters. -func EncodeJSON(w io.Writer, value interface{}) error { - enc := json.NewEncoder(w) - enc.SetEscapeHTML(false) - return enc.Encode(value) -} diff --git a/helper/json_test.go b/helper/json_test.go deleted file mode 100644 index 255ab85..0000000 --- a/helper/json_test.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import ( - "bytes" - "strings" - "testing" -) - -func TestEncodeJSON(t *testing.T) { - buf := bytes.NewBuffer(nil) - if err := EncodeJSON(buf, `abc`); err != nil { - t.Error(err) - } else if s := strings.TrimSpace(buf.String()); s != `"abc"` { - t.Errorf("expect '%s', but got '%s'", `"abc"`, s) - } -} diff --git a/helper/must.go b/helper/must.go deleted file mode 100644 index 3f20f7f..0000000 --- a/helper/must.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -// Must panics if err is not nil -func Must(err error) { - if err != nil { - panic(err) - } -} diff --git a/helper/must_test.go b/helper/must_test.go deleted file mode 100644 index 80bc015..0000000 --- a/helper/must_test.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import ( - "errors" - "fmt" - "testing" -) - -func TestMust(t *testing.T) { - func() { - defer func() { - if r := recover(); r != nil { - t.Errorf("unexpect a panic, but got one: %v", r) - } - }() - Must(nil) - }() - - func() { - defer func() { - if r := recover(); r == nil { - t.Errorf("expect a panic, but got not") - } else if s := fmt.Sprint(r); s != "test" { - t.Errorf("expect '%s', but got '%s'", "test", s) - } - }() - Must(errors.New("test")) - }() -} diff --git a/helper/strings.go b/helper/strings.go deleted file mode 100644 index 0acb6c8..0000000 --- a/helper/strings.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import ( - "unsafe" - - "math/rand" -) - -var intn = rand.Intn - -// Pre-define some charsets to generate the random string. -var ( - NumCharset = "0123456789" - HexCharset = NumCharset + "abcdefABCDEF" - HexLowerCharset = NumCharset + "abcdef" - HexUpperCharset = NumCharset + "ABCDEF" - - AlphaLowerCharset = "abcdefghijklmnopqrstuvwxyz" - AlphaUpperCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - AlphaCharset = AlphaLowerCharset + AlphaUpperCharset - - AlphaNumLowerCharset = NumCharset + AlphaLowerCharset - AlphaNumUpperCharset = NumCharset + AlphaUpperCharset - AlphaNumCharset = NumCharset + AlphaCharset - - DefaultCharset = AlphaNumLowerCharset -) - -// RandomString generates a random string with the length from the given charsets. -func RandomString(n int, charset string) string { - buf := make([]byte, n) - Random(buf, charset) - return string(buf) // TODO: use unsafe.String?? -} - -// Random generates a random string with the length from the given charsets into buf. -func Random(buf []byte, charset string) { - nlen := len(charset) - for i, _len := 0, len(buf); i < _len; i++ { - buf[i] = charset[intn(nlen)] - } -} - -// String converts the value b from []byte to string. -// -// NOTICE: b must not be modified. -func String(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) -} - -// Bytes converts the value s from string to []byte. -// -// NOTICE: s must not be modified. -func Bytes(s string) []byte { - return unsafe.Slice(unsafe.StringData(s), len(s)) -} diff --git a/helper/strings_go1.22.go b/helper/strings_go1.22.go deleted file mode 100644 index 5f2a5cf..0000000 --- a/helper/strings_go1.22.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2024 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build go1.22 -// +build go1.22 - -package helper - -import "math/rand/v2" - -func init() { intn = rand.IntN } diff --git a/helper/strings_test.go b/helper/strings_test.go deleted file mode 100644 index 4ec3059..0000000 --- a/helper/strings_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2023 xgfone -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package helper - -import ( - "strings" - "testing" -) - -func BenchmarkRandomString(b *testing.B) { - b.RunParallel(func(p *testing.PB) { - for p.Next() { - RandomString(8, DefaultCharset) - } - }) -} - -func TestRandomString(t *testing.T) { - s := RandomString(8, DefaultCharset) - if len(s) != 8 { - t.Errorf("expect the string length %d, but got %d", 8, len(s)) - } - - for _, b := range s { - if !strings.ContainsRune(DefaultCharset, b) { - t.Errorf("unknown the charset '%s'", string(b)) - } - } -} diff --git a/http/handler/response.go b/http/handler/response.go index 1114592..ee001a4 100644 --- a/http/handler/response.go +++ b/http/handler/response.go @@ -20,8 +20,8 @@ import ( "net/http" "sync" - "github.com/xgfone/go-apiserver/helper" "github.com/xgfone/go-apiserver/http/header" + "github.com/xgfone/go-toolkit/jsonx" ) // JSONResponder is an interface to send the http response to client. @@ -37,7 +37,7 @@ func JSON(w http.ResponseWriter, code int, v interface{}) (err error) { } buf := getBuilder() - if err = helper.EncodeJSON(buf, v); err == nil { + if err = jsonx.EncodeJSON(buf, v); err == nil { header.SetContentType(w.Header(), header.MIMEApplicationJSONCharsetUTF8) w.WriteHeader(code) _, err = buf.WriteTo(w) diff --git a/http/middleware/requestid/request_id.go b/http/middleware/requestid/request_id.go index 51959e5..9fe8782 100644 --- a/http/middleware/requestid/request_id.go +++ b/http/middleware/requestid/request_id.go @@ -19,8 +19,8 @@ package requestid import ( "net/http" - "github.com/xgfone/go-apiserver/helper" "github.com/xgfone/go-apiserver/http/header" + "github.com/xgfone/go-toolkit/random" ) // Generate is used to generate a request id for the http request. @@ -29,7 +29,7 @@ import ( var Generate func(*http.Request) string = generate func generate(*http.Request) string { - return helper.RandomString(24, helper.AlphaNumCharset) + return random.String(24, random.AlphaNumCharset) } // RequestId is a http middleware to set the request header "X-Request-Id" diff --git a/http/reqresp/context.go b/http/reqresp/context.go index f531fa0..28d35c3 100644 --- a/http/reqresp/context.go +++ b/http/reqresp/context.go @@ -29,12 +29,12 @@ import ( "sync" "time" - "github.com/xgfone/go-apiserver/helper" "github.com/xgfone/go-apiserver/http/handler" "github.com/xgfone/go-apiserver/http/header" "github.com/xgfone/go-apiserver/result" "github.com/xgfone/go-binder" "github.com/xgfone/go-defaults" + "github.com/xgfone/go-toolkit/unsafex" ) func init() { @@ -255,7 +255,7 @@ func (c *Context) getDataString(key string, required bool) string { case string: return v case []byte: - return helper.String(v) + return unsafex.String(v) case time.Duration: return v.String() case time.Time: