-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
63 lines (52 loc) · 1.1 KB
/
string.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package jaws
import (
"html"
"html/template"
"sync"
)
// String wraps a mutex and a string, and implements jaws.StringSetter and jaws.HtmlGetter.
// String.JawsGetHtml() will escape the string before returning it.
type String struct {
mu sync.Mutex
Value string
}
func (s *String) Set(val string) {
s.mu.Lock()
s.Value = val
s.mu.Unlock()
}
func (s *String) Swap(val string) (old string) {
s.mu.Lock()
old, s.Value = s.Value, val
s.mu.Unlock()
return
}
func (s *String) Get() (val string) {
s.mu.Lock()
val = s.Value
s.mu.Unlock()
return
}
func (s *String) String() string {
return s.Get()
}
func (s *String) JawsGetHtml(*Element) (val template.HTML) {
val = template.HTML(html.EscapeString(s.Get())) // #nosec G203
return
}
func (s *String) JawsGetString(*Element) string {
return s.String()
}
func (s *String) JawsSetString(e *Element, val string) (err error) {
if s.Swap(val) == val {
err = ErrValueUnchanged
}
return
}
func (s *String) MarshalText() ([]byte, error) {
return []byte(s.Get()), nil
}
func (s *String) UnmarshalText(b []byte) (err error) {
s.Set(string(b))
return
}