File: strings.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (93 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ini

import (
	"strings"
)

func trimProfileComment(s string) string {
	r, _, _ := strings.Cut(s, "#")
	r, _, _ = strings.Cut(r, ";")
	return r
}

func trimPropertyComment(s string) string {
	r, _, _ := strings.Cut(s, " #")
	r, _, _ = strings.Cut(r, " ;")
	r, _, _ = strings.Cut(r, "\t#")
	r, _, _ = strings.Cut(r, "\t;")
	return r
}

// assumes no surrounding comment
func splitProperty(s string) (string, string, bool) {
	equalsi := strings.Index(s, "=")
	coloni := strings.Index(s, ":") // LEGACY: also supported for property assignment
	sep := "="
	if equalsi == -1 || coloni != -1 && coloni < equalsi {
		sep = ":"
	}

	k, v, ok := strings.Cut(s, sep)
	if !ok {
		return "", "", false
	}
	return strings.TrimSpace(k), strings.TrimSpace(v), true
}

// assumes no surrounding comment, whitespace, or profile brackets
func splitProfile(s string) (string, string) {
	var first int
	for i, r := range s {
		if isLineSpace(r) {
			if first == 0 {
				first = i
			}
		} else {
			if first != 0 {
				return s[:first], s[i:]
			}
		}
	}
	if first == 0 {
		return "", s // type component is effectively blank
	}
	return "", ""
}

func isLineSpace(r rune) bool {
	return r == ' ' || r == '\t'
}

func unquote(s string) string {
	if isSingleQuoted(s) || isDoubleQuoted(s) {
		return s[1 : len(s)-1]
	}
	return s
}

// applies various legacy conversions to property values:
//   - remote wrapping single/doublequotes
//   - expand escaped quote and newline sequences
func legacyStrconv(s string) string {
	s = unquote(s)
	s = strings.ReplaceAll(s, `\"`, `"`)
	s = strings.ReplaceAll(s, `\'`, `'`)
	s = strings.ReplaceAll(s, `\n`, "\n")
	return s
}

func isSingleQuoted(s string) bool {
	return hasAffixes(s, "'", "'")
}

func isDoubleQuoted(s string) bool {
	return hasAffixes(s, `"`, `"`)
}

func isBracketed(s string) bool {
	return hasAffixes(s, "[", "]")
}

func hasAffixes(s, left, right string) bool {
	return strings.HasPrefix(s, left) && strings.HasSuffix(s, right)
}