Read A String Exactly As It Is in Haskell - haskell

My program is something like that:
func = do
text <- getLine
return text
If I read line \123\456, the result is, naturally, \\123\\456.
How can I obtain \123\456 as the result?

Based on the discussion in comments, it looks like you want to parse the string as if it was a string literal, except that it is not surrounded by quotes.
We can make use of of read :: Read a => String -> a here that for a string parses it as if it was a string literal to a string. The only problem is that this string literal is surrounded by double quotes (").
We can thus add these quotes, and work with:
read ('"' : text ++ "\"") :: String
Not every string text is however per se a valid string literal, so the above might fail. For example if the text contains a double quote itself, that is not directly preceded by a backslash (\).

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.

Does a python string include the quotation marks?

For ('bobby'), is the string here 'bobby' or just bobby? I've tried to research into it but the other questions people ask are more complicated. I only want to know whether a full python string includes or doesn't include the '' marks.
If you are declaring a string, you need the quotation marks, like this example:
a = "Hello"
However, if you are just talking about the string itself, the quotations are not part of it. If I were to print variable a that I declared above, the output would be Hello, not "Hello".
print(a) -> Hello
A string is enclosed within the quotation mark, it does not mean that quotations are included in the string. The quotations are given just to tell the compiler that it is a string data type.
Ex -> "Hello"
'Hello'
But if you include double or single quotes inside single or double quotes in python respectively, then the inner quotation will be treated as a string.
Ex -> 'Ram said, "I love apples."'
"Ram said, 'I love apples.'"

In Swift how to obtain the "invisible" escape characters in a string variable into another variable

In Swift I can create a String variable such as this:
let s = "Hello\nMy name is Jack!"
And if I use s, the output will be:
Hello
My name is Jack!
(because the \n is a linefeed)
But what if I want to programmatically obtain the raw characters in the s variable? As in if I want to actually do something like:
let sRaw = s.raw
I made the .raw up, but something like this. So that the literal value of sRaw would be:
Hello\nMy name is Jack!
and it would literally print the string, complete with literal "\n"
Thank you!
The newline is the "raw character" contained in the string.
How exactly you formed the string (in this case from a string literal with an escape sequence in source code) is not retained (it is only available in the source code, but not preserved in the resulting program). It would look exactly the same if you read it from a file, a database, the concatenation of multiple literals, a multi-line literal, a numeric escape sequence, etc.
If you want to print newline as \n you have to convert it back (by doing text replacement) -- but again, you don't know if the string was really created from such a literal.
You can do this with escaped characters such as \n:
let secondaryString = "really"
let s = "Hello\nMy name is \(secondaryString) Jack!"
let find = Character("\n")
let r = String(s.characters.split(find).joinWithSeparator(["\\","n"]))
print(r) // -> "Hello\nMy name is really Jack!"
However, once the string s is generated the \(secondaryString) has already been interpolated to "really" and there is no trace of it other than the replaced word. I suppose if you already know the interpolated string you could search for it and replace it with "\\(secondaryString)" to get the result you want. Otherwise it's gone.

Replace a char with double qoute

I am constructing json code syntax in a string. The format needs to be {"field":"somedata"} and so on. The problem is that I need to use a string and the code that I wrote is:
astring=astring + "{\"field\":\"somedata\"}"
The problem is that when I save the string as a textfile the backslashes is also saved and disqualifies this as json.
The I tried to use a diff char (a dog) as a replacement for \" and tried to replace the dog using:
res_str=res_str.stringByReplacingOccurrencesOfString("🐶", withString: "\"")
But the backslash was included. And finally I tried to
res_str=res_str.stringByReplacingOccurrencesOfString("🐶", withString: String(UnicodeScalar(34)))
But the backslash is included. In PHP for example I could have used single quote as outer string separator but that isn't allowed in Swift.
Any ideas?

What is the r#""# operator in Rust?

I saw the operator r#"" in Rust but I can't find what it does. It came in handy for creating JSON:
let var1 = "test1";
let json = r#"{"type": "type1", "type2": var1}"#;
println!("{}", json) // => {"type2": "type1", "type2": var1}
What's the name of the operator r#""? How do I make var1 evaluate?
I can't find what it does
It has to do with string literals and raw strings. I think it is explained pretty well in this part of the documentation, in the code block that is posted there you can see what it does:
"foo"; r"foo"; // foo
"\"foo\""; r#""foo""#; // "foo"
"foo #\"# bar";
r##"foo #"# bar"##; // foo #"# bar
"\x52"; "R"; r"R"; // R
"\\x52"; r"\x52"; // \x52
It negates the need to escape special characters inside the string.
The r character at the start of a string literal denotes a raw string literal. It's not an operator, but rather a prefix.
In a normal string literal, there are some characters that you need to escape to make them part of the string, such as " and \. The " character needs to be escaped because it would otherwise terminate the string, and the \ needs to be escaped because it is the escape character.
In raw string literals, you can put an arbitrary number of # symbols between the r and the opening ". To close the raw string literal, you must have a closing ", followed by the same number of # characters as there are at the start. With zero or more # characters, you can put literal \ characters in the string (\ characters do not have any special meaning). With one or more # characters, you can put literal " characters in the string. If you need a " followed by a sequence of # characters in the string, just use the same number of # characters plus one to delimit the string. For example: r##"foo #"# bar"## represents the string foo #"# bar. The literal doesn't stop at the quote in the middle, because it's only followed by one #, whereas the literal was started with two #.
To answer the last part of your question, there's no way to have a string literal that evaluates variables in the current scope. Some languages, such as PHP, support that, but not Rust. You should consider using the format! macro instead. Note that for JSON, you'll still need to double the braces, even in a raw string literal, because the string is interpreted by the macro.
fn main() {
let var1 = "test1";
let json = format!(r#"{{"type": "type1", "type2": {}}}"#, var1);
println!("{}", json) // => {"type2": "type1", "type2": test1}
}
If you need to generate a lot of JSON, there are many crates that will make it easier for you. In particular, with serde_json, you can define regular Rust structs or enums and have them serialized automatically to JSON.
The first time I saw this weird notation is in glium tutorials (old crate for graphics management) and is used to "encapsulate" and pass GLSL code (GL Shading language) to shaders of the GPU
https://github.com/glium/glium/blob/master/book/tuto-02-triangle.md
As far as I understand, it looks like the content of r#...# is left untouched, it is not interpreted in any way. Hence raw string.

Resources