Swift Command Line Tool - replacing regex in a long string is very slow - swift

I'm writing a small command line tool in Xcode using swift. I have a string that's about 80 lines long and it contains something like this (possibly multiple times):
#if true
This should be here
#else
This should not be here
#endif
I want to replace all occurrences of this with This should be here, or whatever is written between #if true and #else.
(I'm doing the same for #if false)
I'm doing it like this:
let contents = try String(contentsOf: myFileURL)
contents = contents.replacingOccurrences(of: "#if true\n((.*|\n)*)#else\n(?:(.*|\n)*)#endif", with: "$1", options: .regularExpression)
This technically works, but only if contents is really small, otherwise it takes way too long (I quit the program after a minute).
Why is this so incredibly slow? Is the regex expression too complicated (regex101.com also can't do it on a large string)? I wouldn't know how to simplify it. Is there a better way to do what I'm trying to do?
Thank you for any help

Related

Powershell 5.1 - the filename or extension is too long. How to split 1 command with dynamic arguments into more sequential calls?

Problem:
In powershell 5.1 I run a command, myProgram, and pass to its -itemsToProcess flag a comma separated string list (of dynamic length), $commaSeparatedList, as arguments. There are often too many characters, sometimes 125000 characters, or more, or less, in $commaSeparatedList which can cause the error shown below.
$commaSeparatedList = 'file1,file2,file3,file4 ... fileX'
myProgram -itemsToProcess $commaSeparatedList
Question:
How might I avoid the error? How might i split this into multiple calls such that the error is never thrown? The calls must be sequential too, not in parallel.
The pseudo code setup above works fine/succeeds when $commaSeparatedList is short in character length , however if its too long it crashes/fails/errors:
"the filename or extension is too long"
For example, the dynamically generated $commaSeparatedList has been 125k characters and could potentially be longer or shorter.
how might we detect and avoid the error? Perhaps somehow split it into multiple myProgram -itemsToProcess calls to avoid the error? What would that look like?
Thanks for the comments, I split the input into smaller strings and made more calls

Autohotkey / AHK - String as function parameter without " "

Is there any way to define function parameter as string by default, so I could send them without using double quotes?
If you know C++ and want to compile your own version of AHK from the source code, or if you want to make an other script which will insert in the quotes automatically after the fact, then yes. Otherwise no.
And I really don't know why you'd want to do this. Seems terrible in my opinion.
Maybe you're used to legacy AHK? If so, you really should let go of that. Maybe about 10 years ago it was fine to use.

Swift's readLine() is nil when I enter a long string in STDIN

I am trying to solve this HackerRank's problem about dynamic programming. I think I have come with a solution, probably not very efficient but still, I am trying.
I submitted my code and it failed to pass a large test case, so I am trying to test it myself using that test case. The problem is when I enter the input data Xcode doesn't respond, it doesn't crash, but it doesn't continue the execution of the code.
First I had this code to read a single line that contains n space-separated integers, which in this case is 68,738.
let arr = readLine()!.characters.split(" ").map({ Int(String($0))! })
After a few time (several seconds, maybe even minutes) the code crashed saying that it found nil while unwrapping an optional value.
So I tried and split that instruction as follow:
let input = readLine()!
let arr = input.characters.split(" ").map({ Int(String($0))! })
Here I would expect the code to crash in the second line, trying to map the input string to an array of integers. But the code crashed while trying to readLine(). The input string was 370,112 long.
I also tried to use this code in order to at least get the string input:
let input = readLine()
let arr = input!.characters.split(" ").map({ Int(String($0))! })
But input is nil. I assume here that the input string was too long, but shouldn't be 2,147,483,648 on a 32-bytes CPU? I guess that's enough space, right?
I googled to find if there'd be any limit in readLine() but found nothing. I would try to solve this problem in another language but I'd really like to make it in Swift. Is there something I am not seeing?
readLine() is a wrapper around the stdio getline function, and that function requires
only that a newline character occurs within the first SSIZE_MAX characters of the input. On the 64-bit OS X platform, SSIZE_MAX
is 2^63 - 1 which means that this is only a theoretical limitation.
So readLine() is not the problem, it can read arbitrarily long lines as long as they fit into your computers memory.
But it seems that you can not paste more than 1023 characters into the
Xcode debugger console. (Edit: This was also observed at Read a very long console input in C++).
Running the program in a Terminal with input redirection from a file is one option to solve the problem:
$ ./myProgram < /path/to/inputData.txt
Another option is to add
freopen("/path/to/your/inputData.txt", "r", stdin)
at the beginning of the Swift program. This redirects the standard
input to read from the given file.
The advantage of this method is that you can still debug your program
in Xcode.
As mentioned by #MartinR, the problem was I was trying to test this with Xcode, which seems to have some limits for input strings. I tried from the terminal and it worked just the way it should.

Subscript multiple characters in Julia variable name?

I can write:
x\_m<TAB> = 5
to get x subscript m as a variable name in Julia. What if I want to subscript a word instead of a single character? This
x\_max<TAB> = 5
doesn't work. However,
x\_m<TAB>\_a<TAB>\_x<TAB> = 5
does work, it's just very uncomfortable. Is there a better way?
As I noted in my comment, not all ASCII characters exist as unicode super- or sub-scripts. In addition, another difficulty in generalizing this tab completion will be determining what \_phi<TAB> should mean: is it ₚₕᵢ or ᵩ? Finally, I'll note that since these characters are cobbled together from different ranges for different uses they look pretty terrible when used together.
A simple hack to support common words you use would be to add them piecemeal to the Base.REPLCompletions.latex_symbols dictionary:
Base.REPLCompletions.latex_symbols["\\_max"] = "ₘₐₓ"
Base.REPLCompletions.latex_symbols["\\_min"] = "ₘᵢₙ"
You can put these additions in your .juliarc.jl file to load them every time on startup. While it may be possible to get a comprehensive solution, it'll take much more work.
Since Julia 1.6 this works for subscripts (\_) and superscripts(\^) in the Julia REPL.
x\_maxTAB will print out like this: xₘₐₓ.
x\^maxTAB will print out like this: xᵐᵃˣ.

How to understand this perl multi-line writing command

I am trying to understand the perl commands below:
$my = << EOU;
This is an example.
Example too.
EOU
What is the name of this way? Could somebody can explain more about this "multi-line writing" command?
Essentially the syntax is allowing you to put anything unique as a marker so that it won't conflict with your contents. You can do this:
$my = <<ABCDEFG;
This is an example.
Example too.
BLAH
ABCDEFG
Everything between "This.." and "BLAH" will be assigned to the variable. Note that you shouldn't have a space after the << symbols otherwise you will get a syntax error. It helps avoid adding CR characters, or append (.) everywhere, and useful when passing data into another application (eg. ftp session). Here Documents is the correct term for this.
Everything between <<EOU and EOU is a multi-line, non-escapable, string. It's nothing fancy, think of them as start and end quote marks with nothing inside requiring escapes to be literally what you typed...