string-literals

Does javascript have literal strings?

℡╲_俬逩灬. 提交于 2019-11-26 20:23:29
问题 In c# and ruby and many other languages you can denote a string as to not need escaping. in c# its like this string s = @"\whatever\this\is"; the results are when printed \whatever\this\is my question is, is this supported in any form in javascript? 回答1: Short answer: No Long answer: Noooooooooooooooooooooooooo 回答2: I don't know what you're getting at, but one way to get around the problem of escaping (etc) is use a trick that John Resig seems to like a lot. You include <script> blocks in a

How do I encode Unicode character codes in a PowerShell string literal?

十年热恋 提交于 2019-11-26 19:56:26
问题 How can I encode the Unicode character U+0048 (H), say, in a PowerShell string? In C# I would just do this: "\u0048" , but that doesn't appear to work in PowerShell. 回答1: Replace '\u' with '0x' and cast it to System.Char: PS > [char]0x0048 H You can also use the "$()" syntax to embed a Unicode character into a string: PS > "Acme$([char]0x2122) Company" AcmeT Company Where T is PowerShell's representation of the character for non-registered trademarks. 回答2: According to the documentation,

python: SyntaxError: EOL while scanning string literal

大城市里の小女人 提交于 2019-11-26 19:38:23
I have the above-mentioned error in s1="some very long string............" Does anyone know what I am doing wrong? aaronasterling You are not putting a " before the end of the line. Use """ if you want to do this: """ a very long string ...... ....that can span multiple lines """ Chris H I had this problem - I eventually worked out that the reason was that I'd included \ characters in the string. If you have any of these, "escape" them with \\ and it should work fine. (Assuming you don't have/want line breaks in your string...) How long is this string really? I suspect there is a limit to how

Why are string literals l-value while all other literals are r-value?

北城余情 提交于 2019-11-26 18:48:54
C++03 5.1 Primary expressions §2: A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are rvalues. What is the rationale behind this? As I understand, string literals are objects, while all other literals are not.And an l-value always refers to an object. But the question then is why are string literals objects while all other literals are not? This rationale seems to me more like an egg or chicken problem. I understand the answer to this may be related to hardware architecture rather than C/C++ as programming languages,

Computing length of a C string at compile time. Is this really a constexpr?

浪尽此生 提交于 2019-11-26 17:27:26
I'm trying to compute the length of a string literal at compile time. To do so I'm using following code: #include <cstdio> int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf("%d %d", length("abcd"), length("abcdefgh")); } Everything works as expected, the program prints 4 and 8. The assembly code generated by clang shows that the results are computed at compile time: 0x100000f5e: leaq 0x35(%rip), %rdi ; "%d %d" 0x100000f65: movl $0x4, %esi 0x100000f6a: movl $0x8, %edx 0x100000f6f: xorl %eax, %eax 0x100000f71: callq 0x100000f7a ; symbol stub for

Restrict passed parameter to a string literal

纵然是瞬间 提交于 2019-11-26 17:19:36
问题 I have a class to wrap string literals and calculate the size at compile time. The constructor looks like this: template< std::size_t N > Literal( const char (&literal)[N] ); // used like this Literal greet( "Hello World!" ); printf( "%s, length: %d", greet.c_str(), greet.size() ); There is problem with the code however. The following code compiles and I would like to make it an error. char broke[] = { 'a', 'b', 'c' }; Literal l( broke ); Is there a way to restrict the constructor so that it

Why is passing a string literal into a char* argument only sometimes a compiler error?

旧街凉风 提交于 2019-11-26 17:14:44
问题 I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off. Then I got a whole bunch of errors of the form "Cannot convert const char* to char* in argmuent 3 of function foo". So, I went through and made a whole lot of changes to fix those. However, today, the program CRASHED because the literal "" was getting passed into a function that was expecting a char*, and was setting the 0th

Why does gcc allow char array initialization with string literal larger than array?

两盒软妹~` 提交于 2019-11-26 17:10:54
问题 int main() { char a[7] = "Network"; return 0; } A string literal in C is terminated internally with a nul character . So, the above code should give a compilation error since the actual length of the string literal Network is 8 and it cannot fit in a char[7] array. However, gcc (even with -Wall ) on Ubuntu compiles this code without any error or warning. Why does gcc allow this and not flag it as compilation error? gcc only gives a warning (still no error!) when the char array size is smaller

C -> sizeof string is always 8

半腔热情 提交于 2019-11-26 14:40:11
问题 #include "usefunc.h" //don't worry about this -> lib I wrote int main() { int i; string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array given[0] = "a"; printf("Please enter words separated by RETs...\n"); for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++) { given[i] = GetLine(); /* if (sizeof(given[i]) > sizeof(longest)) { longest = given[i]; } */ printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!! } printf("%s", longest); } Why does it

How to use Macro argument as string literal?

旧城冷巷雨未停 提交于 2019-11-26 13:50:44
问题 I am trying to figure out how to write a macro that will pass both a string literal representation of a variable name along with the variable itself into a function. For example given the following function. void do_something(string name, int val) { cout << name << ": " << val << endl; } I would want to write a macro so I can do this: int my_val = 5; CALL_DO_SOMETHING(my_val); Which would print out: my_val: 5 I tried doing the following: #define CALL_DO_SOMETHING(VAR) do_something("VAR", VAR)