Why does golang bytes.Buffer behave in such way? - string

I recently faced a problem, where I'm writing to a byte.Buffer using a writer. But when I do String() on that byte.Buffer I'm getting an unexpected output (extra pair of double quotes added). Can you please help me understand it?
Here is a code snippet of my problem! I just need help understanding why each word is surrounded by a double quote.
func main() {
var csvBuffer bytes.Buffer
wr := csv.NewWriter(&csvBuffer)
data := []string{`{"agent":"python-requests/2.19.1","api":"/packing-slip/7123"}`}
err := wr.Write(data)
if err != nil {
fmt.Println("WARNING: unable to write ", err)
}
wr.Flush()
fmt.Println(csvBuffer.String())
}
Output:
{""agent"":""python-requests/2.19.1"",""api"":""/packing-slip/7123""}

In CSV double quotes (") are escaped as 2 double quotes. That's what you see.
You encode a single string value which contains double quotes, so all those are replaced with 2 double quotes.
When decoded, the result will contain 1 double quotes of course:
r := csv.NewReader(&csvBuffer)
rec, err := r.Read()
fmt.Println(rec, err)
Outputs (try it on the Go Playground):
[{"agent":"python-requests/2.19.1","api":"/packing-slip/7e0a05b3"}] <nil>
Quoting from package doc of encoding/csv:
Within a quoted-field a quote character followed by a second quote character is considered a single quote.
"the ""word"" is true","a ""quoted-field"""
results in
{`the "word" is true`, `a "quoted-field"`}
In CSV, the following are equivalent:
one,two
and
"one","two"
Now if the values would contain double quotes, that would indicate the end of the value. CSV handles this by substituting double quotes with 2 of them. The value one"1 is encoded as one""1 in CSV, e.g.:
"one""1","two""2"

Related

Is it possible to use a "plain" long string?

In Julia, you can't store a string like that:
str = "\mwe"
Because there is a backslash. So the following allows you to prevent that:
str = "\\mwe"
The same occurs for "$, \n" and many other symbols. My question is, given that you have a extremely long string of thousands of characters and this is not very convenient to treat all the different cases even with a search and replace (Ctrl+H), is there a way to assign it directly to a variable?
Maybe the following (which I tried) gives an idea of what I'd like:
str = """\$$$ \\\nn\nn\m this is a very long and complicated (\n^$" string"""
Here """ is not suitable, what should I use instead?
Quick answer: raw string literals like raw"\$$$ \\\nn..." will get you most of the way there.
Raw string literals allow you to put nearly anything you like between quotes and Julia will keep the characters as typed with no replacements, expansions, or interpolations. That means you can do this sort of thing easily:
a = raw"\mwe"
#assert codepoint(a[1]) == 0x5c # Unicode point for backslash
b = raw"$(a)"
#assert codepoint(b[1]) == 0x25 # Unicode point for dollar symbol
The problem is always the delimiters that define where the string begins and ends. You have to have some way of telling Julia what is included in the string literal and what is not, and Julia uses double inverted commas to do that, meaning if you want double inverted commas in your string literal, you still have to escape those:
c = raw"\"quote" # note the backslashe
#assert codepoint(c[1]) == 0x22 # Unicode point for double quote marks
If this bothers you, you can combine triple quotes with raw, but then if you want to represent literal triple quotes in your string, you still have to escape those:
d = raw""""quote""" # the three quotes at the beginning and three at the end delimit the string, the fourth is read literally
#assert codepoint(d[1]) == 0x22 # Unicode point for double quote marks
e = raw"""\"\"\"""" # In triple quoted strings, you do not need to escape the backslash
#assert codeunits(e) == [0x22, 0x22, 0x22] # Three Unicode double quote marks
If this bothers you, you can try to write a macro that avoids these limitations, but you will always end up having to tell Julia where you want to start processing a string literal and where you want to end processing a string literal, so you will always have to choose some way to delimit the string literal from the rest of the code and escape that delimiter within the string.
Edit: You don't need to escape backslashes in raw string literals in order to include quotation marks in the string, you just need to escape the quotes. But if you want a literal backslash followed by a literal quotation mark, you have to escape both:
f = raw"\"quote"
#assert codepoint(f[1]) == 0x22 # double quote marks
g = raw"\\\"quote" # note the three backslashes
#assert codepoint(g[1]) == 0x5c # backslash
#assert codepoint(g[2]) == 0x22 # double quote marks
If you escape the backslash and not the quote marks, Julia will get confused:
h = raw"\\"quote"
# ERROR: syntax: cannot juxtapose string literal
This is explained in the caveat in the documentation.

Creating a substring in go creates a new kind of symbol

I am comparing strings and there is the following:
Please note that the " in front of NEW are different.
Now when calling my function like this:
my_func(a[18:], b[18:])
The resulting strings are surprisingly:
What do I have to do to cut this weird symbol away and why is it behaving like this?
Because that type of quote is a multibyte character, and you are splitting the string in the middle of a character. What you could do is convert to an []rune and then convert back:
https://play.golang.org/p/pw42sEwRTZd
s := "H界llo"
fmt.Println(s[1:3]) // ��
fmt.Println(string([]rune(s)[1:3])) // 界l
Another option is the utf8string package:
package main
import "golang.org/x/exp/utf8string"
func main() {
s := utf8string.NewString(` 'Not Available') “NEW CREDIT" FROM customers;`)
t := s.Slice(18, s.RuneCount())
println(t == `“NEW CREDIT" FROM customers;`)
}
https://pkg.go.dev/golang.org/x/exp/utf8string

Golang convert integer to unicode character

Given the following input:
intVal := 2612
strVal := "2612"
What is a mechanism for mapping to the associated unicode value as a string.
For example, the following code prints "☒"
fmt.Println("\u2612")
But the following does not work:
fmt.Println("\\u" + strVal)
I researched runes, strconv, and unicode/utf8 but was unable to find a suitable conversion strategy.
2612 is not the integer value of the unicode rune, the integer value of \u2612 is 9746. The string "2612" is the hex value of the rune, so parse it as a hex number and convert it to a rune.
i, err := strconv.ParseInt(strVal, 16, 32)
if err != nil {
log.Fatal(err)
}
r := rune(i)
fmt.Println(string(r))
https://play.golang.org/p/t_e6AfbKQq
This one works:
fmt.Println("\u2612")
Because an interpreted string literal is specified in the source code, and the compiler will unquote (interpret) it. It is not the fmt package that processes this unquoting.
This doesn't work:
fmt.Println("\\u" + strVal)
Because again an interpreted string literal is used which will be resolved to a string value \u, and then it will be concatenated with the value of the local variable strVal which is 2612, so the final string value will be \u2612. But this is not an interpreted string literal, this is the "final" result. This won't be processed / unquoted further.
Alternatively to JimB's answer, you may also use strconv.Unquote() which does an unquoting similar to what the compiler does.
See this example:
// The original that works:
s := "\u2612"
fmt.Println(s, []byte(s))
// Using strconv.Unquote():
strVal := "2612"
s2, err := strconv.Unquote(`"\u` + strVal + `"`)
fmt.Println(s2, []byte(s2), err)
fmt.Println(s == s2)
Output (try it on the Go Playground):
☒ [226 152 146]
☒ [226 152 146] <nil>
true
Something to note here: We want to unquote the \u2612 text by strconv.Unquote(), but Unquote() requires that the string to be unquoted to be in quotes ("Unquote interprets s as a single-quoted, double-quoted, or backquoted Go string literal..."), that's why we pre- and postpended it with a quotation mark.

How to put double quotes into Swift String

I am writing some codes that deals with string with double quote in Swift. Here is what I've done so far:
func someMethod {
let string = "String with \"Double Quotes\""
dealWithString(string)
}
func dealWithString(input: String) {
// I placed a breakpoint here.
}
When I run the codes the breakpoint stopped there as usual but when I input the following into the debugger:
print input
This is what I get:
(String) $R0 = "String with \"Double Quotes\""
I got this string with the backslashes. But if I tried to remove the backslashes from the source, it will give me compile error. Is there a workaround for this?
You are doing everything right. Backslash is used as an escape character to insert double quotes into Swift string precisely in the way that you use it.
The issue is the debugger. Rather than printing the actual value of the string, it prints the value as a string literal, i.e. enclosed in double quotes, with all special characters properly escaped escaped.
If you use print(input) in your code, you would see the string that you expect, i.e. with escape characters expanded and no double quotes around them.
Newer versions of Swift support an alternate delimiter syntax that lets you embed special characters without escaping. Add one or more # symbols before and after the opening and closing quotes, like so:
#"String with "Double Quotes""#
Be careful, though, because other common escapes require those extra # symbols, too.
#"This is not a newline: \n"#
#"This is a newline: \#n"#
You can read more about this at Extended String Delimiters at swift.org.
extension CustomStringConvertible {
var inspect: String {
if self is String {
return "\"\(self)\""
} else {
return self.description
}
}
}
let word = "Swift"
let s = "This is \(word.inspect)"

Golang: Issues replacing newlines in a string from a text file

I've been trying to have a File be read, which will then put the read material into a string. Then the string will get split by line into multiple strings:
absPath, _ := filepath.Abs("../Go/input.txt")
data, err := ioutil.ReadFile(absPath)
if err != nil {
panic(err)
}
input := string(data)
The input.txt is read as:
a
strong little bird
with a very
big heart
went
to school one day and
forgot his food at
home
However,
re = regexp.MustCompile("\\n")
input = re.ReplaceAllString(input, " ")
turns the text into a mangled mess of:
homeot his food atand
I'm not sure how replacing newlines can mess up so badly to the point where the text inverts itself
I guess that you are running the code using Windows. Observe that if you print out the length of the resulting string, it will show something over 100 characters. The reason is that Windows uses not only newlines (\n) but also carriage returns (\r) - so a newline in Windows is actually \r\n, not \n. To properly filter them out of your string, use:
re := regexp.MustCompile(`\r?\n`)
input = re.ReplaceAllString(input, " ")
The backticks will make sure that you don't need to quote the backslashes in the regular expression. I used the question mark for the carriage return to make sure that your code works on other platforms as well.
I do not think that you need to use regex for such an easy task. This can be achieved with just
absPath, _ := filepath.Abs("../Go/input.txt")
data, _ := ioutil.ReadFile(absPath)
input := string(data)
strings.Replace(input, "\n","",-1)
example of removing \n

Resources