Return value of std.regex.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; }

Related

Chars exadecimal to string conversion

I need a function that convert hexadecimal char pointer to string:
ex:
std::string Myfunction(char* hexacode)
{
std::string output;
//
//
return output;
}
std::string Result = Myfunction("\x55\x8B\xEC\x83\xEC\x14\x53\x56\x8B\x75\x0C");
In short I need to convert in string this parameter or similar.
Because in the output the backslash is a option can be a solution replace backslash with slash if is too complicate keep the backslash.
Many thanks !!
If you want the user to input the string, you don't have to care about anything, string escaping only happens for constant strings in the code file.
If you don't want the constant strings in the code file to be escaped, no function conversion needed, just do this:
"\\x55\\x8B\\xEC\\x83\\xEC\\x14\\x53\\x56\\x8B\\x75\\x0C"

Error C2026: string too big, trailing characters truntraced

i have a big problem and i dont know how to fix it...
I want to decode a very long Base64 encoded string (980.000 Chars) but every time when i to debug it i get this error :
Error C2026: string too big, trailing characters truntraced
I tried this but i can only compare 2 strings throught this method
char* toHash1 = "LONG BASE 64 Code";
char* toHash2 = "LONG BASE 64 Code";
if (true) {
sprintf_s(output, outputSize, "%s", base64_decode(toHash1 =+ toHash2).c_str());
}
Anyone know how i can get it to work?
As documented here, you can only have about 2048 characters in a string literal when using MSVC. You can get up to 65535 characters by concatenation, but since this is still too short, you cannot use string literals here.
One solution would be reading the string from a file into some allocated char buffer. I do not know of any such limits for gcc and clang, so trying to use them instead of MSVC could solve this too.
You can first convert your string to hex and then can include it like this,
char data[] = {0xde,0xad,0xbe,0xef}; //example
And than can use it like a string, append null terminator if needed to.

Expression: string iterators incompatiable, when calling "SetDllDirectory"

I'm receiving following error:
Debug Assertion Failed!
Expression: string iterators incompatible
When trying to run such a code:
std::string string_Dir(){return ".\\Dir\\";}
std::wstring wstring_Dir=std::wstring(
string_Dir().begin()
,string_Dir().end()
);
SetDllDirectory(wstring_Dir.c_str());
Does someone know why
BTW: I followed this.
You are calling string_Dir() twice and then using iterators from different std::string objects to initialize your std::wstring. That is why you are getting an incompatibility error. You must use iterators from the same std::string object, so call string_Dir() once and assign the return value to a variable:
std::string dir = string_Dir();
std::wstring wstring_Dir(dir.begin(), dir.end());
SetDllDirectory(wstring_Dir.c_str());
// or better: SetDllDirectoryW(wstring_Dir.c_str());
That being said, you are not converting from ANSI to UTF-16, so this code will only work correctly if string_Dir() returns a std::string that contains only 7bit ASCII characters. It will fail if the std::string contains any non-ASCII 8bit characters.
There is a simpler solution - you can call SetDllDirectoryA() instead. You don't need the std::wstring, and the OS can do the ANSI-to-UTF16 conversion for you:
SetDllDirectoryA(string_Dir().c_str());
According to the documentation, the value in the function call is supposed to be LPCTSTR instead of LPCTWSTR.

Swtich case in strings

I have an error while I compile my code.
Error Output:
main.cpp: 35:16: error: switch quantity not an integer
I dont know why. The code is as follows:
int Values(string letter) {
switch( tolower(letter) ) {
case 'a' : a.setTotal();
break;
Regards
A string is not a char, it's an object representing an array of chars.
Instead of passing in a string to that function, you should pass in a char.
tolower(int) exists in C++ and it was meant to take characters, not strings.
Here's another way to fix this that needs less changes to your code:
Since I'm pretty sure that letter is going to be a string of length 1 judging by the name, you can change your switch statement to access letter[0], which will be the first character in the string. You would also have to pass letter[0] to tolower.
letter is string, so tolower(letter) produce a string. But your case is character ('a'). Additionally it is impossible in C/C++ to use switch for string.
user this line instead:
switch( tolower(letter.data[0]) )
The switch inscrutvion takes only enumerated types(enum), chars(char) or integers (int, unsigned, long)
It looks like you want to work with a single character.
Change your function's parameter:
int Values(string letter)
to this:
int Values(char letter)
And the switch statement will work.

C++/CX - I need to pass a Platform::String into a method that takes a const char*?

I'm new to c++ (I'm a c# developer).
I have an SQLite wrapper class that requires you to pass in a database name as a const char* , however I only have it as a Platform::String (after doing a file search).
I cant seem to find a way to convert the Platform::String to const char*.
Ive seen another question on StackOverflow that explain why it isnt straight-forward, but no sample code or end-to-end solution.
Can anyone help me ?
Thanks
Disclaimer: I know little about C++/CX, and I'm basing the answer on the documentation here.
The String class contains 16-bit Unicode characters, so you can't directly get a pointer to 8-bit char-typed characters; you'll need to convert the contents.
If the string is known to only contain ASCII characters, then you can convert it directly:
String s = whatever();
std::string narrow(s.Begin(), s.End());
function_requiring_cstring(narrow.c_str());
Otherwise, the string will need translating, which gets rather hairy. The following might do the right thing, converting the wide characters to multi-byte sequences of narrow characters:
String s = whatever();
std::wstring wide(s.Begin(), s.End());
std::vector<char> buffer(s.Length()+1); // We'll need at least that much
for (;;) {
size_t length = std::wcstombs(buffer.data(), wide.c_str(), buffer.size());
if (length == buffer.size()) {
buffer.resize(buffer.size()*2);
} else {
buffer.resize(length+1);
break;
}
}
function_requiring_cstring(buffer.data());
Alternatively, you may find it easier to ignore Microsoft's ideas about how strings should be handled, and use std::string instead.