File: quote.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: makefile: 3
file content (39 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download | duplicates (2)
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
package textutils

import "bytes"

const (
	DOUBLE_QUOTE = '"'
	SINGLE_QUOTE = '\''
)

func SurroundBy(content []byte, chars []byte) []byte {
	content = append(chars, content...)
	content = append(content, chars...)
	return content
}
func SurroundByQuotes(content []byte) []byte {
	if len(content) == 0 {
		return nil
	}

	containsDoubleQuote := bytes.ContainsRune(content, DOUBLE_QUOTE)
	containsSingleQuote := bytes.ContainsRune(content, SINGLE_QUOTE)

	if containsDoubleQuote && containsSingleQuote {
		// Escape all quotes
		content = bytes.ReplaceAll(content, []byte(`"`), []byte(`\"`))

		// Surround the content by double quotes
		return SurroundBy(content, []byte(`"`))
	}
	if containsDoubleQuote {
		// Since it contains double quotes (but no single quotes)
		// we can surround it by single quotes
		return SurroundBy(content, []byte(`'`))
	}

	// It may contain single quotes, but definitely no double quotes,
	// so we can safely surround it by double quotes.
	return SurroundBy(content, []byte(`"`))
}