EscapeString writes to p the properly escaped XML equivalent of the plain text data s.
(s string)
| 1930 | // EscapeString writes to p the properly escaped XML equivalent |
| 1931 | // of the plain text data s. |
| 1932 | func (p *printer) EscapeString(s string) { |
| 1933 | var esc []byte |
| 1934 | last := 0 |
| 1935 | for i := 0; i < len(s); { |
| 1936 | r, width := utf8.DecodeRuneInString(s[i:]) |
| 1937 | i += width |
| 1938 | switch r { |
| 1939 | case '"': |
| 1940 | esc = esc_quot |
| 1941 | case '\'': |
| 1942 | esc = esc_apos |
| 1943 | case '&': |
| 1944 | esc = esc_amp |
| 1945 | case '<': |
| 1946 | esc = esc_lt |
| 1947 | case '>': |
| 1948 | esc = esc_gt |
| 1949 | case '\t': |
| 1950 | esc = esc_tab |
| 1951 | case '\n': |
| 1952 | esc = esc_nl |
| 1953 | case '\r': |
| 1954 | esc = esc_cr |
| 1955 | default: |
| 1956 | if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) { |
| 1957 | esc = esc_fffd |
| 1958 | break |
| 1959 | } |
| 1960 | continue |
| 1961 | } |
| 1962 | p.WriteString(s[last : i-width]) |
| 1963 | p.Write(esc) |
| 1964 | last = i |
| 1965 | } |
| 1966 | p.WriteString(s[last:]) |
| 1967 | } |
| 1968 | |
| 1969 | // Escape is like EscapeText but omits the error return value. |
| 1970 | // It is provided for backwards compatibility with Go 1.0. |
no test coverage detected