aboutsummaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-05 21:27:17 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-05 21:27:17 +0800
commite0635b47c2f30719e1ea026812af85c988632c0e (patch)
treef112904c018dc294cf6f902423745f1f1932449c /misc
parentExport symbols from database.go (diff)
downloadforge-e0635b47c2f30719e1ea026812af85c988632c0e.tar.gz
forge-e0635b47c2f30719e1ea026812af85c988632c0e.tar.zst
forge-e0635b47c2f30719e1ea026812af85c988632c0e.zip
Move things to internal/v0.1.23
Diffstat (limited to 'misc')
-rw-r--r--misc/misc.go21
-rw-r--r--misc/unsafe.go20
-rw-r--r--misc/url.go154
3 files changed, 0 insertions, 195 deletions
diff --git a/misc/misc.go b/misc/misc.go
deleted file mode 100644
index ee0fd7a..0000000
--- a/misc/misc.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package misc
-
-import "strings"
-
-func FirstOrPanic[T any](v T, err error) T {
- if err != nil {
- panic(err)
- }
- return v
-}
-
-// sliceContainsNewlines returns true if and only if the given slice contains
-// one or more strings that contains newlines.
-func SliceContainsNewlines(s []string) bool {
- for _, v := range s {
- if strings.Contains(v, "\n") {
- return true
- }
- }
- return false
-}
diff --git a/misc/unsafe.go b/misc/unsafe.go
deleted file mode 100644
index 6c2192f..0000000
--- a/misc/unsafe.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import "unsafe"
-
-// StringToBytes converts a string to a byte slice without copying the string.
-// Memory is borrowed from the string.
-// The resulting byte slice must not be modified in any form.
-func StringToBytes(s string) (bytes []byte) {
- return unsafe.Slice(unsafe.StringData(s), len(s))
-}
-
-// BytesToString converts a byte slice to a string without copying the bytes.
-// Memory is borrowed from the byte slice.
-// The source byte slice must not be modified.
-func BytesToString(b []byte) string {
- return unsafe.String(unsafe.SliceData(b), len(b))
-}
diff --git a/misc/url.go b/misc/url.go
deleted file mode 100644
index b77d8ce..0000000
--- a/misc/url.go
+++ /dev/null
@@ -1,154 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import (
- "errors"
- "net/http"
- "net/url"
- "strings"
-)
-
-var (
- ErrDupRefSpec = errors.New("duplicate ref spec")
- ErrNoRefSpec = errors.New("no ref spec")
-)
-
-// getParamRefTypeName looks at the query parameters in an HTTP request and
-// returns its ref name and type, if any.
-func GetParamRefTypeName(request *http.Request) (retRefType, retRefName string, err error) {
- rawQuery := request.URL.RawQuery
- queryValues, err := url.ParseQuery(rawQuery)
- if err != nil {
- return
- }
- done := false
- for _, refType := range []string{"commit", "branch", "tag"} {
- refName, ok := queryValues[refType]
- if ok {
- if done {
- err = ErrDupRefSpec
- return
- }
- done = true
- if len(refName) != 1 {
- err = ErrDupRefSpec
- return
- }
- retRefName = refName[0]
- retRefType = refType
- }
- }
- if !done {
- err = ErrNoRefSpec
- }
- return
-}
-
-// ParseReqURI parses an HTTP request URL, and returns a slice of path segments
-// and the query parameters. It handles %2F correctly.
-func ParseReqURI(requestURI string) (segments []string, params url.Values, err error) {
- path, paramsStr, _ := strings.Cut(requestURI, "?")
-
- segments, err = PathToSegments(path)
- if err != nil {
- return
- }
-
- params, err = url.ParseQuery(paramsStr)
- return
-}
-
-func PathToSegments(path string) (segments []string, err error) {
- segments = strings.Split(strings.TrimPrefix(path, "/"), "/")
-
- for i, segment := range segments {
- segments[i], err = url.PathUnescape(segment)
- if err != nil {
- return
- }
- }
-
- return
-}
-
-// RedirectDir returns true and redirects the user to a version of the URL with
-// a trailing slash, if and only if the request URL does not already have a
-// trailing slash.
-func RedirectDir(writer http.ResponseWriter, request *http.Request) bool {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- if !strings.HasSuffix(path, "/") {
- http.Redirect(writer, request, path+"/"+rest, http.StatusSeeOther)
- return true
- }
- return false
-}
-
-// RedirectNoDir returns true and redirects the user to a version of the URL
-// without a trailing slash, if and only if the request URL has a trailing
-// slash.
-func RedirectNoDir(writer http.ResponseWriter, request *http.Request) bool {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- if strings.HasSuffix(path, "/") {
- http.Redirect(writer, request, strings.TrimSuffix(path, "/")+rest, http.StatusSeeOther)
- return true
- }
- return false
-}
-
-// RedirectUnconditionally unconditionally redirects the user back to the
-// current page while preserving query parameters.
-func RedirectUnconditionally(writer http.ResponseWriter, request *http.Request) {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- http.Redirect(writer, request, path+rest, http.StatusSeeOther)
-}
-
-// SegmentsToURL joins URL segments to the path component of a URL.
-// Each segment is escaped properly first.
-func SegmentsToURL(segments []string) string {
- for i, segment := range segments {
- segments[i] = url.PathEscape(segment)
- }
- return strings.Join(segments, "/")
-}
-
-// AnyContain returns true if and only if ss contains a string that contains c.
-func AnyContain(ss []string, c string) bool {
- for _, s := range ss {
- if strings.Contains(s, c) {
- return true
- }
- }
- return false
-}