aboutsummaryrefslogtreecommitdiff
path: root/http_template_funcs.go
blob: 11e4fadf5d1911868cdae76e44ba1581214d5585 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package forge

import (
	"net/url"
	"strings"
)

// These are all trivial functions that are intended to be used in HTML
// templates.

// FirstLine returns the first line of a string.
func FirstLine(s string) string {
	before, _, _ := strings.Cut(s, "\n")
	return before
}

// PathEscape escapes the input as an URL path segment.
func PathEscape(s string) string {
	return url.PathEscape(s)
}

// QueryEscape escapes the input as an URL query segment.
func QueryEscape(s string) string {
	return url.QueryEscape(s)
}

// Dereference dereferences a pointer.
func Dereference[T any](p *T) T {
	return *p
}

// DereferenceOrZero dereferences a pointer. If the pointer is nil, the zero
// value of its associated type is returned instead.
func DereferenceOrZero[T any](p *T) T {
	if p != nil {
		return *p
	}
	var z T
	return z
}

// Minus subtracts two numbers.
func Minus(a, b int) int {
	return a - b
}