string-literals

Searching For String Literals

夙愿已清 提交于 2019-11-27 02:38:17
问题 In the quest for localization I need to find all the string literals littered amongst our source code. I was looking for a way to script this into a post-modification source repository check. (I.E. after some one checks something in have a box setup to check this stat) I'll probably use NAnt and CruiseControl or something to handle the management of the CVS (Well StarTeam in my case :( ) But do you know of any scriptable (or command line) utility to accurately cycle through source code

What is the rationale for parenthesis in C++11's raw string literals R“(…)”?

百般思念 提交于 2019-11-27 00:09:24
问题 There is a very convenient feature introduced in C++11 called raw string literals, which are strings with no escape characters. And instead of writing this: regex mask("\\t[0-9]+\\.[0-9]+\\t\\\\SUB"); You can simply write this: regex mask(R"(\t[0-9]+\.[0-9]+\t\\SUB)"); Quite more readable. However, note extra parenthesis around the string one have to place to define a raw string literal. My question is, why do we even need these? For me it looks quite ugly and illogical. Here are the cons

Can a string literal be subscripted in a constant expression?

馋奶兔 提交于 2019-11-26 23:13:41
问题 This is valid, because a constexpr expression is allowed to take the value of "a glvalue of literal type that refers to a non-volatile object defined with constexpr, or that refers to a sub-object of such an object" (§5.19/2): constexpr char str[] = "hello, world"; constexpr char e = str[1]; However, it would seem that string literals do not fit this description: constexpr char e = "hello, world"[1]; // error: literal is not constexpr 2.14.5/8 describes the type of string literals: Ordinary

Concat two `const char` string literals

☆樱花仙子☆ 提交于 2019-11-26 22:41:41
Is it possible to concat two string literals using a constexpr ? Or put differently, can one eliminate macros in code like: #define nl(str) str "\n" int main() { std::cout << nl("usage: foo") nl("print a message") ; return 0; } Update : There is nothing wrong with using "\n" , however I would like to know whether one can use constexpr to replace those type of macros. Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr functions and even operators. However, The compiler is not required to perform constant initialization of any object other

Assigning string literals to char*

天涯浪子 提交于 2019-11-26 21:52:12
问题 Is the following code legal, deprecated or illegal in C++0x? char* p = "foobar"; I originally asked this question here as a comment. 回答1: The conversion char* p = "foobar"; is deprecated in C++98/C++03, and has been removed (that is, §4.2/2 removed) in C++0x. So, the code is not valid in C++0x. However, MinGW g++ 4.4.1 still only emits a warning, not error. C++98/C++03 §4.2/2 (which is removed in C++0x): A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue

Lifetime of a string literal returned by a function

本秂侑毒 提交于 2019-11-26 21:00:06
Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun() , where is "Some text!!" stored (I think it may be in some static area of ROM) and what is its scope lifetime? Will the memory pointed by retStr be occupied throughout the program or be released once the block A exits? The C++ Standard does not say where string literals should be stored. It does however guarantee that their lifetime is the lifetime of the program. Your code is therefore valid. The "Some text!

C/C++, can you #include a file into a string literal? [duplicate]

浪子不回头ぞ 提交于 2019-11-26 20:51:28
This question already has an answer here: “#include” a text file in a C program as a char[] 16 answers I have a C++ source file and a Python source file. I'd like the C++ source file to be able to use the contents of the Python source file as a big string literal. I could do something like this: char* python_code = " #include "script.py" " But that won't work because there need to be \'s at the end of each line. I could manually copy and paste in the contents of the Python code and surround each line with quotes and a terminating \n, but that's ugly. Even though the python source is going to

How to get double quotes into a string literal?

喜你入骨 提交于 2019-11-26 20:36:48
I have the following output created using a printf() statement: printf("She said time flies like an arrow, but fruit flies like a banana."); but I want to put the actual quotation in double-quotes, so the output is She said "time flies like an arrow, but fruit flies like a banana". without interfering with the double-quotes used to wrap the string literal in the printf() statement. How can I do this? Mark Byers Escape the quotes with backslashes: printf("She said \"time flies like an arrow, but fruit flies like a banana\"."); There are special escape characters that you can use in string

Best way to convert string to array of object in javascript?

£可爱£侵袭症+ 提交于 2019-11-26 20:29:17
I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea? alcuadrado I think that the best way of doing this, as Douglas Crockford (one of the biggests gurus of JavaScript) suggests in here is using the JSON native parser , as it is not only faster than the eval(), it's also more secure. Native JSON parser is already available in: Firefox 3.5+ IE 8+ Opera 10.5+ Safari Safari 4.0.3+ Chrome (don't know which version) And Crockford has made a safe fallback in javascript, called json2.js , which is an adaption

GCC 4.7 Source Character Encoding and Execution Character Encoding For String Literals?

我与影子孤独终老i 提交于 2019-11-26 20:27:28
问题 Does GCC 4.7 on Linux/x86_64 have a default character encoding by which it validates and decodes the contents of string literals in C source files? Is this configurable? Further, when linking the string data from string literals into the data section of the output does it have a default execution character encoding? Is this configurable? In any configuration is it possible to have a source character encoding that differs from the execution character encoding? (That is will gcc ever transcode