gocloud.dev: gocloud.dev/internal/escape Index | Files

package escape

import "gocloud.dev/internal/escape"

Package escape includes helpers for escaping and unescaping strings.


Package Files



const NonUTF8String = "\xbd\xb2"

NonUTF8String is a string for which utf8.ValidString returns false.


var WeirdStrings = map[string]string{
    "fwdslashes":          "foo/bar/baz",
    "repeatedfwdslashes":  "foo//bar///baz",
    "dotdotslash":         "../foo/../bar/../../baz../",
    "backslashes":         "foo\\bar\\baz",
    "repeatedbackslashes": "..\\foo\\\\bar\\\\\\baz",
    "dotdotbackslash":     "..\\foo\\..\\bar\\..\\..\\baz..\\",
    "quote":               "foo\"bar\"baz",
    "spaces":              "foo bar baz",
    "startwithdigit":      "12345",
    "unicode":             strings.Repeat("☺", 3),

    "ascii-1": makeASCIIString(0, 16),
    "ascii-2": makeASCIIString(16, 32),
    "ascii-3": makeASCIIString(32, 48),
    "ascii-4": makeASCIIString(48, 64),
    "ascii-5": makeASCIIString(64, 80),
    "ascii-6": makeASCIIString(80, 96),
    "ascii-7": makeASCIIString(96, 112),
    "ascii-8": makeASCIIString(112, 128),

WeirdStrings are unusual/weird strings for use in testing escaping. The keys are descriptive strings, the values are the weird strings.

func HexEscape Uses

func HexEscape(s string, shouldEscape func(s []rune, i int) bool) string

HexEscape returns s, with all runes for which shouldEscape returns true escaped to "__0xXXX__", where XXX is the hex representation of the rune value. For example, " " would escape to "__0x20__".

Non-UTF-8 strings will have their non-UTF-8 characters escaped to unicode.ReplacementChar; the original value is lost. Please file an issue if you need non-UTF8 support.

Note: shouldEscape takes the whole string as a slice of runes and an index. Passing it a single byte or a single rune doesn't provide enough context for some escape decisions; for example, the caller might want to escape the second "/" in "//" but not the first one. We pass a slice of runes instead of the string or a slice of bytes because some decisions will be made on a rune basis (e.g., encode all non-ASCII runes).

func HexUnescape Uses

func HexUnescape(s string) string

HexUnescape reverses HexEscape.

func IsASCIIAlphanumeric Uses

func IsASCIIAlphanumeric(r rune) bool

IsASCIIAlphanumeric returns true iff r is alphanumeric: a-z, A-Z, 0-9.

func URLEscape Uses

func URLEscape(s string) string

URLEscape uses url.PathEscape to escape s.

func URLUnescape Uses

func URLUnescape(s string) string

URLUnescape reverses URLEscape using url.PathUnescape. If the unescape returns an error, it returns s.

Package escape imports 4 packages (graph) and is imported by 8 packages. Updated 2019-02-27. Refresh now. Tools for package owners.