I have a string that comes from the standard input in Xcode, using Swift. When I type in my string, I write something like this :
He told me "Hello maaan"
What I see when I print my message after putting a breakpoint is this :
"He told me \"Hello maaan""
Now, the quotes at the start and the end can be ignored, but what's that backslash? If I run this code in the console
po message.contains("\\")
it returns me false. The thing is that I need to run a regex on that string and the regex fails because of that \ char. What's the solution?
EDIT
If I transform my string to a NSString, the console prints this :
He told me "Hello maaan
In order for the compiler to ignore the quotation marks you'll need to add the backslashes before both.
So, you'll need to format your string as so:
var string = " He told me: \"Hello Man\"."
print(string)
Related
The goal is to be able to launch my Command Line Tool and have it automatically resize the terminal window. I found this code to help with that:
#discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
You can use it like this:
shell("ls")
Which will make the terminal call the 'ls' command as soon as it runs
Commands like this work great, but the command I need to run is (which will resize the terminal window)
printf '\e[8;50;100t'
But if I do
shell("printf", "'\e[8;50;100t'")
I get an error saying 'Invalid escape sequence in literal'. I understand why it is giving this, but I don't know how to work around it. I've tried adding an extra backslash but then it won't actually execute the command, it will just print 'e[8;50;100t' to the terminal.
How can I work around this issue?
The problem is that you're not actually running a shell, so the escaped form \e is not being interpreted as the single character that you want. You need to directly include the correct character in the string. One way to do that is with a Unicode escape (see the "Special Characters" heading there): \u{XX}, where XX is the hexadecimal for the code point you want.
The \e character is originally from ASCII, and its hex value is 1B. Therefore:
shell("printf", "'\u{1B}[8;50;100t'")
will pass through the string that you need.
Trying to pass a large string to a shell script using a breakpoint in Xcode
let value = Array(repeating: "a", count: 1500).joined()
let string = "{\"key\": \"\(value)\"}"
Unfortunately, the string is being truncated. Is this limitation documented and can it be overcome?
It's been nearly a year since you asked this, and I'm not sure if it will solve your question, but I've recently had a similar problem so thought I'd share my solution.
I had two issues:
LLDB was truncating any arguments to my shell script (and string variables printed in the console using po foo) to 1023 characters. I believe this is the issue to which your question relates.
Xcode was incorrectly confusing a comma , in my string as a separator for multiple arguments (e.g. passing foo, bar, and baz as arguments to the script wouldn't work correctly if any of the variables contained a , as Xcode would try to create another argument).
So, firstly, the LLDB issue...
It seems that by default LLDB has a limit on the character length that it will print to the console (or pass to a shell script via a breakpoint argument) of around 1023 characters. You can easily change this to something larger by setting another breakpoint before the breakpoint that uses your variable and running (lldb) set set target.max-string-summary-length 10000 in the console. This can be a bit annoying so I created a ~/.lldbinit file and placed set set target.max-string-summary-length 10000 in there instead so I don't have to keep setting it in the console.
Secondly, the comma issue...
Inside the Edit breakpoint... menu that you provided a screenshot of above there is the option to not only provide a path to a script but to also provide arguments. I can see from your question that you provided the argument #string#. For my script, I was passing multiple arguments, which Xcode allows you to do using a comma separated list, e.g. #foo#, #bar#, #baz#. Each of these arguments was a string.
I noticed that sometimes one or more of these strings would truncate if they contained a comma: ,.
So the string:
{ "num_friends" : "7" }
would be passed to my script as expected. But the string:
{ "num_friends" : "7", "num_deleted_friends" : "1" }
would truncate and would be passed to my script as two separate arguments. It seems that Xcode would split any string with a , even when entered using #string#.
I validated this in my script by simply using something like:
for var in "$#"
do
echo "$var"
echo "===="
done
Where $# expands to contain each argument. From this I could see that #string# was being correctly passed to my script but separated as multiple arguments wherever there was a ,. So if #string# contained a comma my script would print:
#"{ \"num_friends\" : \"7\"
====
\"num_deleted_friends\" : \"1\" }"
instead of what I expected which was:
#"{ \"num_friends\" : \"7\", \"num_deleted_friends\" : \"1\" }"
So it seems like it might be a bug in how Xcode passes strings inside # expressions in the breakpoint editor window.
My crude solution has been to just replace any commas with another character and then replace them back again inside my script. There's probably a better way to do this but I don't require it for my needs.
I am trying to import a string from the unix shell to the program space of specman.
The string i want to import contains quotation marks ("") - for example "hi".
in these cases, the string is not parsed properly . for example
suppose i want to 'echo' some string with quotation marks, i would do the following:
%> echo echo \"\"hi\"\"
will output
""hi""
but if i use the following program, written in e:
<'
extend sys {
run() is also{
print output_from("echo \"\"hi\"\"");
stop_run();
};
};
'>
i get the following output:
output_from("echo \"\"hi\"\"") =
0. "hi"
as you can see - quotation marks are gone. the ones that we see here are coming from the default printing of list values.
I'm not familiar with the output_from action, but I assume it treats the input string as a shell command.
By writing "echo \"\"hi\"\"" what you will essentially get is a string containing echo ""hi"". This is because the \ will be "eaten up" (it's an escape character in e as well). The resulting string is what will be executed, which if you try in the shell will also output the same thing. Try adding an escaped \ as well. I don't have the possibility to start Specman anytime soon so you'll have to try it out.
To test my hypothesis:
// just to see what happens with your original string
var some_string : string = "echo \"\"hi\"\"";
print some_string; // should output echo ""hi""
To try out my solution do something like this:
// might need to fiddle with the escaping here
var some_other_string : string = "echo \\\"\\\"hi\\\"\\\"";
print some_other_string; // should output echo \"\"hi\"\"
You're passing your string through multiple string interpreters. First Specman's, then your shell's string interpreter.
You can debug getting your string through Specman's interpreter first by printing out command you want to pass to the shell first
message(None,"echo [...]")`
Once the printed command looks like it would when you execute it on the shell, then it is ready to be put into output_from command. You can build up the shell command using normal Specman string manipulation functions.
Please could anyone explain me why the following happens:
"Fancy string - Hor""ray"
# outputs correctly (only one double quote): Fancy string - Hor"ray
'Hor"ray'.Replace('"', '""')
# outputs correctly (two double quotes): Hor""ray
"Fancy string - $('Hor"ray'.Replace('"', '"'+'"'))"
#outputs correctly (two double quotes): Hor""ray
"Fancy string - $('Hor"ray'.Replace('"', '""'))"
# outputs INCORRECTLY (only one double quote): Fancy string - Hor"ray
In my opinion, developers would intuitively expect, that within "$(inline expressions)" Powershell would treat text as statements and won't interfere with the last argument of Replace('"', '""') converting it into '"' (unless the statement interpreter decides to do so).
Do I miss something here?
This appears to be a bug in how PowerShell parses expandable string literals.
From §2.3.5.2 on string literals in the PowerShell 3.0 Language Specification, the expandable-string-literal explicitly rejects the $( sequence so that sub-expression parsing will occur instead.
So it seems reasonable to expect $('""') to parse consistently, whether or not it happens to be embedded in a string literal. And clearly sub-expressions are parsed separately, since they support values that would be illegal on their own in an expandable string (e.g. you can write "$('"')" or "$('`""')", where " '"' " or " '`""' " would fail).
However, comparing the AST from [Management.Automation.Language.Parser]::ParseInput for both $('""') and "$('""')", we get two different results. Both have a final StringConstantExpressionAst element with an Extent of '""', but the Value for the stand-alone sub-expression is "" while the Value for the embedded sub-expression is ".
Its because the inline expression is evaluated and its string value is then placed in the string and evaluated.
#Breaking "Fancy string - $('Hor"ray'.Replace('"', '""'))" down
#This inline expression is evaluated first
$('Hor"ray'.Replace('"', '""'))
#giving
Hor""ray
#That value is then interpreted as part of the string
"Fancy string - Hor""ray"
#giving
Fancy string - Hor"ray
This is exactly what I would expect to see. The inline expression evaluated and its resulting value then being used.
Could this not be done by simply using things like below:
`'$($RemFiles[$i].FullName)`'
`"$($RemFiles[$i].FullName)`"
Use the backtick and either a single or double quote to then prevent powershell from using this as an open comment, thus putting the actual symbol in...
The above outputs:
'F:\portable\adobe'
or
"F:\portable\adobe"
I noticed that it seems you are telling it to literally add two double quotes rather than just using the backtick to force it. Therefore telling it to add nothing surely :S So could you change this to something like this:
"Fancy string - $('Hor"ray'.Replace('"', '`"`"'))"
Though that may make 3, as you have one present in Hor"ray anyway.
Just got to test it, was busy with something:
PS D:\> "Fancy string - $('Hor"ray'.Replace('"', '`"`"'))"
Fancy string - Hor""ray
Scroll down on the site below to find out about the backtick and how/where it can be used.
http://www.neolisk.com/techblog/powershell-specialcharactersandtokens
In scala, "here docs" is begin and end in 3 "
val str = """Hi,everyone"""
But what if the string contains the """? How to output Hi,"""everyone?
Since unicode escaping via \u0022 in multi-line string literals won’t help you, because they would be evaluated as the very same three quotes, your only chance is to concatenate like so:
"""Hi, """+"""""""""+"""everyone"""
The good thing is, that the scala compiler is smart enough to fix this and thus it will make one single string out of it when compiling.
At least, that’s what scala -print says.
object o {
val s = """Hi, """+"""""""""+"""everyone"""
val t = "Hi, \"\"\"everyone"
}
and using scala -print →
Main$$anon$1$o.this.s = "Hi, """everyone";
Main$$anon$1$o.this.t = "Hi, """everyone";
Note however, that you can’t input it that way. The format which scala -print outputs seems to be for internal usage only.
Still, there might be some easier, more straightforward way of doing this.
It's a totally hack that I posted on a similar question, but it works here too: use Scala's XML structures as an intermediate format.
val str = <a>Hi,"""everyone</a> text
This will give you a string with three double quotation marks.
you can't
scala heredocs are raw strings and don't use any escape codes
if you need tripple quotes in a string use string-concatenation add them
You can't using the triple quotes, as far as I know. In the spec, section 1.3.5, states:
A multi-line string literal is a sequence of characters enclosed in triple quotes
""" ... """. The sequence of characters is arbitrary, except that it may contain
three or more consuctive quote characters only at the very end. Characters must
not necessarily be printable; newlines or other control characters are also permitted.
Unicode escapes work as everywhere else, but none of the escape sequences in
(§1.3.6) is interpreted.
So if you want to output three quotes in a string, you can still use the single quote string with escaping:
scala> val s = "Hi, \"\"\"everyone"
s: java.lang.String = Hi, """everyone