Creates a string containing the given character issue - swift3

In the Apple Documentation :
init(_: Character)
Creates a string containing the given character.
c: The character to convert to a string.
Declaration
init(_ c: Character)
I try to create a string with a character using string init but I fail.I don't understand the declaration above.I want to create a string including "k".
Here is example:
String(_ c: "k") // fails
String("k" c: Character) // fails
What is the correct way acoording to declaration above.I don't understand what _: means in the declaration.
Can someone explain what the declaration means in human language ?

init(_: Character)
^
means the parameter doesn't have an external name. So when you call that initializer you simply put the value, without a label.
Like this
let char: Character = "A"
let word = String(char)

Related

How can I used regular expressions to find all lines of source code defining a default arguments for a function?

I want to find lines of code which declare functions with default arguments, such as:
int sum(int a, int b=10, int c=20);
I was thinking I would look for:
The first part of the matched pattern is exactly one left-parenthesis "("
The second part of string is one or more of any character excluding "="
exactly one equals-sign "="
a non-equal-sign
one or more characters except right parenthesis ")"
")"
The following is my attempt:
([^=]+=[^=][^)]+)
I would like to avoid matching condition-clauses for if-statements and while-loops.
For example,
int x = 5;
if (x = 10) {
x = 7;
}
Our regex should find functions with default arguments in any one of python, Java, or C++. Let us not assume that function declarations end with semi-colon, or begin with a data-type
Try this:
\([^)]*\w+\s+\w+\s*=[^),][^)]*\)
See live demo.
It looks for words chars (the param type), space(s), word chars (the param name), optional space(s), then an equals sign.
Add ".*" to each end to match the whole line.
Please check this one:
\(((?:\w+\s+[\w][\w\s=]*,*\s*){1,})\)
The above expression matches the parameter list and returns it as $1 (Group 1), in case it is needed for further processing.
demo here

Alert deprecated: Stdlib.String.set

The following code returns an error and says that the syntax is deprecated. What is the correct way to change a character in a string?
let hello = "Hello!" ;;
hello.[1] <- 'a' ;;
Alert deprecated: Stdlib.String.set
Use Bytes.set instead.
Error: This expression has type string but an expression was expected of type
bytes
Strings are immutable (or at least soon they will be), so you can't change their contents. You can, of course, create a copy of a string with the one character different, e.g.,
let with_nth_char m c =
String.mapi (fun i b -> if i = m then c else b)
and
# with_nth_char 1 'E' "hello";;
- : string = "hEllo"
But if you need to change characters in an array then you shouldn't use the string data type but instead rely on bytes which is a type for mutable strings. You can use Bytes.of_strings and Bytes.to_string to translate strings to bytes and vice verse.

How do you call a batch file with an argument that has quotes, using system()

For example, in the command line this works (the 1st argument has quotes but the 2nd argument doesn't):
"test.bat" "a" b
i.e it know that "a" is the 1st argument and b is the second
but using system() it doesn't work:
system("test.bat" "a" b)
this also doesn't work:
system("test.bat" \"a\" b)
This is gonna be simplest if we use a raw string literal. A raw string literal is a way of writing a string in c++ where nothing gets escaped. Let's look at an example:
char const* myCommand = R"(test.bat "a" b)";
The R at the beginning indicates that it's a raw string literal, and if you call system(myCommand), it will be exactly equivalent to typing
$ test.bat "a" b
into the command line. Now, suppose you want to escape the quotes on the command line:
$ test.bat \"a\" b
With a raw string literal, this is simple:
char const* myCommand = R"(test.bat \"a\" b)";
system(myCommand);
Or, alternatively:
system(R"(test.bat \"a\" b)");
Hope this helps!
A bit more info on raw string literals: Raw string literals are a great feature, and they basically allow you to copy+paste any text directly into your program. They begin with R, followed by a quote and a parenthesis. Only the stuff inside the parenthesis gets included. Examples:
using std::string;
string a = R"(Hello)"; // a == "Hello"
Begin and end with "raw":
string b = R"raw(Hello)raw"; // b == "Hello"
Begin and end with "foo"
string c = R"foo(Hello)foo"; // c == "Hello"
Begin and end with "x"
string d = R"x(Hello)x"; // d == "Hello"
The important thing is that we begin and end the literal with the same string of letters (called the delimiter), followed by the parenthesis. This ensures we never have a reason to escape something inside the raw string literal, because we can always change the delimiter so that it's not something found inside the string.
I got it to work now:
system(R"(C:\"to erase\test.bat" "a")");
I found the answer: system("test.bat" ""a"" b);
or more precisely: system("\"test.bat\" ""a"" b");
So the answer is to escape the quotes with a double quote

c++ get the character i from a wstring

I have a small problem, but I can't figure out why..
I have a std::wstring text with a value of L"test"
I then try to print its first character like this:
OutputDebugString(&text[0]);
but the result is test..
when I use OutputDebugString(&text[1]);
The result is est
I thought that array access should give me the character at a specified location..
Could anybody tell me what I should do or am doing wrong?
I also tried .at(i); with the same result.
Got it:
wchar_t st = text[0];
OutputDebugString(&st);
Alex Reinking stated that this a a better and more safe solution: (as the string then contains a null terminator)
wchar_t st[3] = { text[0], 0x0 };
OutputDebugString(&st[0]);
Thanks for the help
It's because, in memory, the string looks something like this:
V-- &text[0]
addr: 0x80000000 0x80000001 0x80000002 0x80000003 0x80000004
text: t, e, s, t, 0x00
^-- text[0]
So when you ask for the address of text[1] what you get is:
V-- &text[1]
addr: 0x80000001 0x80000002 0x80000003 0x80000004
text: e, s, t, 0x00
^-- text[1]
Which leaves you with e,x,t,NULL or the string "ext". The function you're calling will use all the characters up until the terminator.
A string is a series of characters followed by a null terminator.
The OutputDebugString function (and most functions in C and in WinAPI which take strings) accept a pointer to the first character of such a string. The the function keeps printing characters from that location and subsequent locations until it hist the null terminator.
If you only want to act on a single character you either need to call a function which expects a single character, or build a string of length 1 containing that character and a null terminator.
OutputDebugString takes a string, therefore it will start with the address you have given and go all the way until it hits a NULL. To address your specific issue you have to write your own function that will take one character from the string, then put it into a new string, then output that new string.
OutputDebugStringW(text.c_str()); should do what you want

Return value of std.regex.regex?

I'm trying to write a function that takes an input string, a regex (made by std.regex.regex from a rawstring) and an error message string, and attempt to match something from the input string using the regex, displaying the error message if there are no matches. I came up with the following signature so far:
string check_for_match (string input, Regex r, string error_message)
However, this doesn't seem to work, as the compiler complains, saying:
struct std.regex.Regex(Char) is used as a type
So what should I use instead?
It'll compile if you change Regex to Regex!char.
The reason is that Regex is a template that can use any character size: char for UTF-8 patterns, wchar for UTF-16, or dchar for UTF-32. The compiler is saying you need to create a type by passing the required Char argument there to use it here.
Since you are working with string, which is made up of chars, Regex!char is the type to use.
string check_for_match (string input, Regex!char r, string error_message) { return null; }