string-literals

How to “instanceof” a primitive string (string literal) in JavaScript [duplicate]

给你一囗甜甜゛ 提交于 2019-12-05 08:33:52
问题 This question already has answers here : Why does instanceof return false for some literals? (10 answers) Closed 6 years ago . In JavaScript, I can declare a string in the following ways; var a = "Hello World"; var b = new String("Hello World"); but a is not an instance of String... console.log(a instanceof String); //false; console.log(b instanceof String); //true; So how do you find the type or " instanceof " a string literal? Can JavaScript be forced to create a new String() for every

Search cost of string interning and declaration of literal strings

烈酒焚心 提交于 2019-12-05 06:19:40
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 declare mant literal strings. How significant is this searching cost and why java is designed in this way

Quoting YAML (for Travis CI)

谁说胖子不能爱 提交于 2019-12-05 00:45:07
How would I escape a whole line in YAML? I want to have json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}' in a list, but I can't get it to parse into a string. I can put single quotes around the whole line, but then I would have to escape every single quote in my string, making it very hard to read. The string will be run as a bash command in Travis CI The most elegant solution is to use the literal style | indicator, with the - modifier to strip the final newline. That way there are no extra quotes necessary. If this scalar happens to be the only thing in a

Raw literal strings in Julia

坚强是说给别人听的谎言 提交于 2019-12-05 00:21:06
In Python one can write r"a\nb" in order to prevent the \n from being interpreted as an escape sequence for newline. Is there something similar in Julia? And what about string interpolation like "$variable" , is there a way to prevent it? I know one can simply write "a\\nb" and "\$variable" in Julia, but I would like to write a lot of LaTeX strings without having to care to proper escape every backslash \ and dollar $ characters... (In Julia, r"..." creates a regular expression.) From Julia 0.6 we have raw"\a$variable" Ok, I just found out that since one can easily create non-standard string

Python convert string literals to strings

匆匆过客 提交于 2019-12-04 22:59:43
I want to convert a string literal like r"r'\nasdf'" to a string ( '\\nasdf' in this case). Another case: r"'\nasdf'" to '\nasdf' . I hope you get it. This is important, because I have a parser of python scripts, that wants to know the exact contents of a string literal. Is eval a clever solution? The string literals are filtered before (with tokenize ) and should not cause security liabilities. Aren't there any nobler solutions than evaluating a literal? A parser library maybe? Edit : Added other examples, to avoid misunderstandings. You want the ast module : >>> import ast >>> raw = r"r'

Using strings in switch statements - where do we stand with C++17?

笑着哭i 提交于 2019-12-04 17:09:36
问题 Every one of us has (probably) had the childhood dream of writing: switch(my_std_string) { case "foo": do_stuff(); break; case "bar": do_other_stuff(); break; default: just_give_up(); } but this is not possible, as is explained in the answers to this question from the olden days (2009): Why switch statement cannot be applied on strings? Since then we've seen the advent of C++11, which lets us go as far as: switch (my_hash::hash(my_std_string)) { case "foo"_hash: do_stuff(); break; case "bar"

Macro for static std::string object from literal

戏子无情 提交于 2019-12-04 15:59:23
问题 Suppose I need to call a function foo that takes a const std::string reference from a great number of places in my code: int foo(const std::string&); .. foo("bar"); .. foo("baz"); Calling a function with a string literal like this will create temporary std::string objects, copying the literal each time. Unless I'm mistaken, compilers won't optimize this by creating a static std::string object per literal that can be reused for subsequent calls. I know that g++ has advanced string pool

String literal still ending up with double slashes? [duplicate]

人走茶凉 提交于 2019-12-04 06:15:18
问题 This question already has answers here : Why is \ character being doubled in my @ string? (3 answers) Closed 4 years ago . I have a small piece of code: public static void Write(string filename){ string time = DateTime.Now.ToString("hh:mm tt"); int date = int.Parse(DateTime.Now.ToString("yyyyMMdd")); string path = @"C:\Users\Public\" + filename; } If I debug and stop just after path is set it looks like “C:\\\Users\\\Public\\\filename.txt”. Can anyone tell me why it has the double slashes? Is

Struct vs string literals? Read only vs read-write? [duplicate]

假如想象 提交于 2019-12-04 05:04:49
This question already has answers here : Closed 12 months ago . Why are compound literals in C modifiable (1 answer) Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? (17 answers) Does the C99 standard permit writing to compound literals (structs)? It seems it doesn't provide writing to literal strings. I ask about this because it says in C Programming: A Modern Approach, 2nd Edition on Page 406. Q. Allowing a pointer to a compound literal would seem to make it possible to modify the literal. Is that the case? A. Yes. Compound literals

pointers and string literals

自闭症网瘾萝莉.ら 提交于 2019-12-04 04:30:08
问题 Many a times I have seen the following statements: char* ch = "Hello" cout<<ch; The output I get is " Hello ". I know that ch points to the first character of the string "Hello" and that "Hello" is a string literal and stored in read only memory. Since, ch stores the address of the first character in the string literal, so shouldn't the statement, cout<<ch; give the output " the address of a character " because, it is a pointer variable? Instead it prints the string literal itself. Moreover,