string-literals

Performance of String literals vs constants for Session[…] dictionary keys

会有一股神秘感。 提交于 2019-12-01 15:10:38
Session[Constant] vs Session["String Literal"] Performance I'm retrieving user-specific data like ViewData["CartItems"] = Session["CartItems"]; with a string literal for keys on every request. Should I be using constants for this? If yes, how should I go about implementing frequently used string literals and will it significantly affect performance on a high-traffic site? Related question does not address ASP.NET MVC or Session . The reason to use constants has to do with maintainability, not performance. Performance is about the same either way. With a string literal, you can never tell

What happens with adjacent string literal concatenation when there is a modifier(L, u8, etc.)

笑着哭i 提交于 2019-12-01 14:23:16
问题 It is valid in C and C++ to break a string literal because the preprocessor or the compiler will concatenate adjacent string literals. const char *zStr = "a" "b"; // valid What happens when string literals are prefixed with L (wide characters), u (UTF-16), U (UTF-32), u8 (UTF-8), and raw string literals ( R"foo(this is a "raw string literal" with double quotes)foo" )? For example, is the following allowed: const wchar_t *zStr = L"a" "b"; // valid? 回答1: In C++0x your example is valid according

Why it is possible to assign string to character pointer in C but not an integer value to an integer pointer

旧巷老猫 提交于 2019-12-01 08:11:32
why in the below code int *p = 22 will give compile time error and ptr will print the value successfully . int main() { /*taking a character pointer and assigning a string to it*/ char *ptr = "Stackoverflow" ; //correct /*taking a int pointer and assigning a string to it*/ int *p = 22 ; //incorrect printf("%s",ptr); // correct and print printf("%d",p); //incorrect and give compile time error. return 0; } If you have a character array as for example char s[] = "Stackoverflow"; then the array designator used in expressions it is converted to pointer to its first element. So you may write char

Raw string literals and file codification

旧时模样 提交于 2019-12-01 07:09:04
C++11 introduced the raw string literals which can be pretty useful to represent quoted strings, literals with lots of special symbols like windows file paths, regex expressions etc... std::string path = R"(C:\teamwork\new_project\project1)"; // no tab nor newline! std::string quoted = R"("quoted string")"; std::string expression = R"([\w]+[ ]+)"; This raw string literals can also be combined with encoding prefixes ( u8 , u , U , or L ), but, when no encoding prefix is specified, does the file encoding matters?, lets suppose that I have this code: auto message = R"(Pick up a card)"; // raw

Compilation of string literals

筅森魡賤 提交于 2019-12-01 06:48:03
Why can two string literals separated by a space, tab or "\n" be compiled without an error? int main() { char * a = "aaaa" "bbbb"; } "aaaa" is a char* "bbbb" is a char* There is no specific concatenation rule to process two string literals. And obviously the following code gives an error during compilation: #include <iostream> int main() { char * a = "aaaa"; char * b = "bbbb"; std::cout << a b; } Is this concatenation common to all compilers? Where is the null termination of "aaaa"? Is "aaaabbbb" a continuous block of RAM? If you see e.g. this translation phase reference in phase 6 it does:

Compilation of string literals

给你一囗甜甜゛ 提交于 2019-12-01 05:05:47
问题 Why can two string literals separated by a space, tab or "\n" be compiled without an error? int main() { char * a = "aaaa" "bbbb"; } "aaaa" is a char* "bbbb" is a char* There is no specific concatenation rule to process two string literals. And obviously the following code gives an error during compilation: #include <iostream> int main() { char * a = "aaaa"; char * b = "bbbb"; std::cout << a b; } Is this concatenation common to all compilers? Where is the null termination of "aaaa"? Is

How does file encoding affect C++11 string literals?

人走茶凉 提交于 2019-12-01 03:32:00
You can write UTF-8/16/32 string literals in C++11 by prefixing the string literal with u8 / u / U respectively. How must the compiler interpret a UTF-8 file that has non-ASCII characters inside of these new types of string literals? I understand the standard does not specify file encodings, and that fact alone would make the interpretation of non-ASCII characters inside source code completely undefined behavior, making the feature just a tad less useful. I understand you can still escape single unicode characters with \uNNNN , but that is not very readable for, say, a full Russian, or French

Does C support raw string literals?

限于喜欢 提交于 2019-12-01 03:24:43
C++11 added support for raw string literals, such as: R"foo(A " weird \" string)foo" Does C have such a thing? If so, in what version of the standard? C11? If not, does anyone know if it is being planed and if any compilers support it? Does C have such a thing? If so, in what version of the standard? C11? C (C90, C99, C11) does not support this feature or any other similar feature. If not, does anyone know if it is being planed I have no idea, but usually there is a strong resistance of C committee to include new features in C. and if any compilers support it? I just tested it and it is

Are hard-coded STRINGS ever acceptable?

痴心易碎 提交于 2019-12-01 00:40:47
Similar to Is hard-coding literals ever acceptable? , but I'm specifically thinking of "magic strings" here. On a large project, we have a table of configuration options like these: Name Value ---- ----- FOO_ENABLED Y BAR_ENABLED N ... (Hundreds of them). The common practice is to call a generic function to test an option like this: if (config_options.value('FOO_ENABLED') == 'Y') ... (Of course, this same option may need to be checked in many places in the system code.) When adding a new option, I was considering adding a function to hide the "magic string" like this: if (config_options.foo

Adding a string to the verbatim string literal

落爺英雄遲暮 提交于 2019-11-30 22:57:33
I have a path that is named defaultPath I want to add it into this verbatim string literal but can quite get the quotes around it. @"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\Data""" I was trying to add +defaultPath to replace Data. So lets say I have a folder name Data.Apple I want the output to be "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple" But when I have been doing it for the past half hour I have been getting "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\"Data.Apple or "C:\Mavro\MavBridge\Server