string-literals

How to transform Go string literal code to its value?

≡放荡痞女 提交于 2019-12-24 01:16:12
问题 I'm walking around syntax tree in Go, trying to find all calls to some particular function and then get its string argument (it's a file name, and should be string literal, not any other identifier). I'm succeeded with this, and now I have ast.BasicLit node with Kind == token.STRING , but its value is Go code, not a value of the string that it should have. I found question that answers how to do reverse transformation - from string to go code it represents: golang: given a string, output an

Why not allowing std::string initialization from array of chars?

扶醉桌前 提交于 2019-12-23 22:22:08
问题 In C++ you can initialize an std::string object from a char * and a const char * and this implicitly assumes that the string will end at first NUL character found after the pointer. In C++ string literals are however arrays and a template constructor could be used to get the correct size even if the string literal contains embedded NUL s. See for example the following toy implementation: #include <stdio.h> #include <string.h> #include <vector> #include <string> struct String { std::vector

Why can I construct a string with multiple string literals? [duplicate]

不问归期 提交于 2019-12-23 19:06:05
问题 This question already has answers here : Why allow concatenation of string literals? (10 answers) Closed 5 years ago . #include <iostream> #include <string> int main() { std::string str = "hello " "world" "!"; std::cout << str; } The following compiles, runs, and prints: hello world! see live It seems as though the string literals are being concatenated together, but interestingly this can not be done with operator + : #include <iostream> #include <string> int main() { std::string str =

C++ compare two string literals

三世轮回 提交于 2019-12-23 10:08:57
问题 When comparing a string literal with another string literal with the == operator (or != ), is the result well defined? For example, are the following guaranteed to hold? assert("a" == "a"); assert("a" != "b"); Please don't say stuff like "use std::string" instead. I just want to know this specific case. 回答1: "a" == "a" This expression may yield true or false ; there are no guarantees. The two "a" string literals may occupy the same storage or they may exist at two different locations in

How to detect a string literal with type_traits?

喜你入骨 提交于 2019-12-23 07:33:06
问题 How do I reliably static_assert on anything that isn't a string literal? For example, in the following code, I've attempted to wrap the standard assert macro but statically reject anything for the message that's not a string literal (since anything but a string literal will not be displayed at runtime when the assert triggers). #include <cassert> #include <string> #include <type_traits> #define my_assert(test, message)\ static_assert(\ (\ !std::is_pointer<decltype(message)>::value &&\ !std:

How to detect a string literal with type_traits?

孤人 提交于 2019-12-23 07:32:22
问题 How do I reliably static_assert on anything that isn't a string literal? For example, in the following code, I've attempted to wrap the standard assert macro but statically reject anything for the message that's not a string literal (since anything but a string literal will not be displayed at runtime when the assert triggers). #include <cassert> #include <string> #include <type_traits> #define my_assert(test, message)\ static_assert(\ (\ !std::is_pointer<decltype(message)>::value &&\ !std:

how java handles string literals [duplicate]

风格不统一 提交于 2019-12-23 01:47:28
问题 This question already has answers here : What is the Java string pool and how is “s” different from new String(“s”)? [duplicate] (5 answers) Closed 6 years ago . in java, i have created 2 string literals having same value String a = "Hello"; String b = "Hello"; now both of them should have same reference System.out.println(a==n); // returns true but when i do b+=" World"; System.out.println(a==b); // returns false Now i have 2 questions here 1. why a and b are not referencing to same object

Address length of a string literal

≯℡__Kan透↙ 提交于 2019-12-22 10:49:04
问题 I see that on Linux systems with GCC the address of string literals seems to be much smaller than for other variables. For instance the following code generates the o/p shown below it. #include <stdio.h> int main() { char *str1 = "Mesg 1"; char *str2 = "Mesg 2"; char str3[] = "Mesg 3"; char str4[] = "Mesg 4"; printf("str1 = %p\n", (void *) str1); printf("str2 = %p\n", (void *) str2); printf("&str3 = %p\n", (void *) str3); printf("&str4 = %p\n", (void *) str4); return 0; } Output: str1 =

Search cost of string interning and declaration of literal strings

南楼画角 提交于 2019-12-22 05:33:12
问题 Two Questions. When we declare literal strings, we search whether there is the same string in string pool of heap. Is this also an interning (method intern of class String )? In my thought, each literal string declaration needs a binary search or something so it costs at least log(n) when n is number of existing strings in the pool. And if there are many strings in the pool, it may be high cost. (maybe tradeoff of searching cost and memory?) On this point of view, it might be dangerous to

If char*s are read only, why can I overwrite them?

我们两清 提交于 2019-12-22 01:56:06
问题 My course taught me that char*s are static/read only so I thought that would mean you can't edit them after you have defined them. But when I run: char* fruit = "banana"; printf("fruit is %s\n", fruit); fruit = "apple"; printf("fruit is %s\n", fruit); Then it compiles fine and gives me: fruit is banana fruit is apple Why? Have I misunderstood what it means to be read-only? Sorry if this is obvious but I'm new to coding and I can't find the answer online. 回答1: The presented code snippet does