Replace a substring in a string in liquidsoap - liquidsoap

Liquidsoap provides the string.replace function. But how should I use it? It seems to expect a function as the second argument that does the replacing.
I'd like to do something like this:
str = "Hello world."
str = string.replace(pattern="w", "W", str)
# str == "Hello World."

string.replace indeed expects a function for the first unlabeled parameter. The return value of that function will be used as a replacement.
Example:
str = "Hello world."
str = string.replace(pattern="w", (fun(_) -> "W"), str)
# str == "Hello World."
The inline arrow function fun(_) -> "W" is a function that will always return "W".

Related

Swift NSAlert() argument print string with spaces

How do a print the array below to display the full string?
alert.informativeText = CommandLine.arguments[4]
This prints fine if it's one word. Argument Test
Test
However if the argument is This Is A Test. It still prints just the first word:
Test
Full Code:
CommandLine.arguments[1]{
func dialogOKCancel(title: String) -> Bool
{
let alert = NSAlert()
alert.informativeText = CommandLine.arguments[4]
alert.alertStyle = NSAlert.Style.warning
alert.addButton(withTitle: "Dismiss")
return alert.runModal() == NSApplication.ModalResponse.alertFirstButtonReturn
}
_ = dialogOKCancel(title: "")
The space character is the argument separator in the command line.
You have to quote the string
/path/to/executable 'This is A Test'
or to escape
/path/to/executable This\ is\ A\ Test
the spaces in the string when calling the executable.
Command line arguments are always split with spaces. Although "This Is A Test" might look like a single argument, it is four arguments - "This", "Is", "A" and "Test".
What you are trying to do seems to be printing all the arguments. In that case, you should drop the first element of CommandLine.arguments (that's the command itself) and then join the strings:
alert.informativeText = CommandLine.arguments.dropFirst().joined(separator: " ")

How to prove "copy-on-write" on String type in Swift

As the title said, I tried to prove myself that COW(copy on write) is supported for String in Swift. But I cannot find a proof. I proved the COW on Array and Dictionary after trying the following codes:
func address(of object: UnsafeRawPointer) -> String {
let addr = Int(bitPattern: object)
return String(format: "%p", addr)
}
var xArray = [20, 30, 40, 50, 60]
var yArray = xArray
// These two addresses were the same
address(of: xArray)
address(of: yArray)
yArray[0] = 200
// The address of yArray got changed
address(of: yArray)
But for String type, it was not working.
var xString = "Hello World"
var yString = xString
// These two addresses were different
address(of: xString)
address(of: yString)
And I dumped the test function from the official Swift code repo.
func _rawIdentifier(s: String) -> (UInt, UInt) {
let tripe = unsafeBitCast(s, to: (UInt, UInt, UInt).self)
let minusCount = (tripe.0, tripe.2)
return minusCount
}
But this function seems to only cast the actual value pointed to not the address. So two different String variables with the same value would have the same rawIdentifier. Still cannot prove COW to me.
var xString = "Hello World"
var yString = "Hello" + " World"
// These two rawIdentifiers were the same
_rawIdentifier(s: xString)
_rawIdentifier(s: yString)
So how does COW work on String type in Swift?
The compiler creates only a single storage for both
"Hello World" and "Hello" + " World".
You can verify that for example by examining the assembly code
obtained from
swiftc -emit-assembly cow.swift
which defines only a single string literal
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.asciz "Hello World"
As soon as the string is mutated, the address of the string storage
buffer (the first member of that "magic" tuple, actually _baseAddress
of struct _StringCore, defined in StringCore.swift) changes:
var xString = "Hello World"
var yString = "Hello" + " World"
print(_rawIdentifier(s: xString)) // (4300325536, 0)
print(_rawIdentifier(s: yString)) // (4300325536, 0)
yString.append("!")
print(_rawIdentifier(s: yString)) // (4322384560, 4322384528)
And why does your
func address(of object: UnsafeRawPointer) -> String
function show the same values for xArray and yArray, but
not for xString and yString?
Passing an array to a function taking a unsafe pointer passes the
address of the first array element, that is the same for both
arrays if they share the storage.
Passing a string to a function taking an unsafe pointer passes a
pointer to a temporary UTF-8 representation of the string.
That address can be different in each call, even for the same string.
This behavior is documented in the "Using Swift with Cocoa and
Objective-C" reference for UnsafePointer<T> arguments, but apparently
works the same for UnsafeRawPointer arguments.

Print in Swift 3

i would like to know what's the different between these two way to print the object in Swift.
The result seems identical.
var myName : String = "yohoo"
print ("My name is \(myName).")
print ("My name is ", myName, ".")
There is almost no functional difference, the comma simply inputs a space either before or after the string.
let name = "John"
// both print "Hello John"
print("Hello", name)
print("Hello \(name)")
You can use the \(variable) syntax to create interpolated strings, which are then printed just as you input them. However, the print(var1,var2) syntax has some "facilities":
It automatically adds a space in between each two variables, and that is called separator
You can customise your separator based on the context, for example:
var hello = "Hello"
var world = "World!"
print(hello,world,separator: "|") // prints "Hello|World!"
print(hello,world,separator: "\\//") // prints "Hello\\//World!"
No difference between the two
var favoriteFood: String = "Pizza" //favoriteFood = Pizza
//both print the same thing
print("My favorite food is", favoriteFood)
print("My favorite food is \(favoriteFood)")

How to get an integer as user input in SML

I'm trying to get a number from a user. That number is then used to
call another function randomList(n) which takes the given number and
uses it. I keep getting the error exception option. I've read that
adding SOME to the variable declaration or valOf can fix this issue,
but it is not working for me. What do I need to change?
fun getNumber() =
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
randomList(i)
end;
getNumber();
The issue here is that fun getNumber() only encompasses the print statement on the following line. You need to enclose the print and the let within parenthesis if you want them to both be a part of getNumber().
For example, the following code compiles and echos the input integer that is passed in via stdin:
fun getNumber() = (
print "Please enter the number of integers: ";
let
val str = valOf (TextIO.inputLine TextIO.stdIn)
val i : int = valOf (Int.fromString str)
in
print(Int.toString(i))
end
);
getNumber();

Print String using variadic params without comma, newline and brackets in Swift

While I was trying to use Swift's String(format: format, args) method, I found out that I cannot print the formatted string directly without newline(\n) and brackets being added.
So for example, I have a code like this:
func someFunc(string: String...) {
print(String(format: "test %#", string))
}
let string = "string1"
someFunc(string, "string2")
the result would be:
"test (\n string1,\n string2\n)\n"
However, I intend to deliver the result like this:
"test string1 string2"
How can I make the brackets and \n not being printed?
Since string parameter is a sequence, you can use joinWithSeparator on it, like this:
func someFunc(string: String...) {
print(string.joinWithSeparator(" "))
}
someFunc("quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog")
You will first need to concatenate your list of input strings, otherwise it will print as a list, hence the commas and parentheses. You can additionally strip out the newline characters and whitespace from the ends of the string using a character set.
var stringConcat : String = ''
for stringEntry in string {
stringConcat += stringEntry.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
Then just print out the stringConcat.