stringification

Use the preprocessor to convert code into a string

风流意气都作罢 提交于 2019-12-11 04:55:20
问题 Disclaimer: I am not a C programmer. I have recently seen a friend's project. Due to reasons I don't understand, he writes code in a string which is compiled at runtime. This results in something like: char x[] = "int y = 5; printf(\"%i\", y)"; run_this_code(x); Which is horrible to use because Visual Studio doesn't step in and do syntax highlighting etc. Using some preprocessor abuse, it is possible to do trick Visual Studio into thinking you're writing real code and then having the

Preprocessor Stringizing Operator with String Literal Prefixes

孤人 提交于 2019-12-10 21:49:45
问题 So I want to do the traditional thing to do with the stringizing operator in a macro: #define FOO(x) foo(#x, (x)) However I need to use a string literal prefix: http://en.cppreference.com/w/cpp/language/string_literal Which is a problem because if I need a UTF-32 string literal I try to do this: #define FOO(x) foo(U#x, (x)) But gcc 4.9.2 complains: error: 'U' was not declared in this scope Is there a way to make the compiler treat the U as a prefix to the stringized macro variable? 回答1: Yes,

C preprocessor stringification weirdness

半腔热情 提交于 2019-12-10 13:59:19
问题 I am defining a macro that evaluates to a constant string, holding the filename and the line number, for logging purposes. It works fine, but I just can't figure out why 2 additional macros are needed - STRINGIFY and TOSTRING , when intuition suggests simply __FILE__ ":" #__LINE__ . #include <stdio.h> #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define THIS_ORIGIN (__FILE__ ":" TOSTRING(__LINE__)) int main (void) { /* correctly prints "test.c:9" */ printf("%s", THIS_ORIGIN);

Convert a jQuery object and contents to string?

这一生的挚爱 提交于 2019-12-10 10:59:55
问题 I'm grabbing various jquery objects to put into an array and later spit out as HTML. I'm trying to convert the jQuery object to a text string so it can be spit out as HTML later. I'm using this technique at the moment: console.log($myObject.clone().wrap('<div></div>').html()); However, that only appears to be grabbing the contents of my object. For example, if $myObject is <h2>My Title</h2> the above only returns 'My Title' (no H2 tags). I've also trying using .text() but get the same result.

jQuery JSON.stringify not getting the entire JSON string to store to cookie

心已入冬 提交于 2019-12-08 09:06:43
问题 I’m trying to set scores in a cookie with a JSON string… var json = JSON.stringify({ s:{score:2000,name:"Michael"}, s:{score:1000,name:"Tito"}, s:{score:500,name:"Jackie"}, s:{score:100,name:"Marlon"}, s:{score:10,name:"Jermain"} }); alert(json); $.cookies.set('highScores',json,30*24); The alert is saying: {"s":"{score":2000,"name":"Michael"}} …and not the entire object. How do I get the whole object to be a JSON string? 回答1: It is because in your json you are using the same key s for all

Convert member variable name to a string

风流意气都作罢 提交于 2019-12-08 06:50:59
问题 Can I enumerate char* members of a class (or struct) in C++? If so can I print the variables names as strings? Using pre-processor? I have a class with all const char* members. It would be good if there was an elegant way to enumerate each member variable and check name as a string against a string key I am given. Here is the sort of code which could be used? Can anyone think of a way to do this? class configitems { public: configitems() : host(0), colour(0) {} const char* host; const char*

What is the # for when formatting using %s

淺唱寂寞╮ 提交于 2019-12-07 05:30:10
问题 I came across this example of an assertion and was wondering what the # is for: #define ASSERT( x ) if ( !( x ) ) { \ int *p = NULL; \ DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \ *p=1; \ } 回答1: It is the "stringize" preprocessing operator. It takes the tokens passed as the argument to the macro parameter x and turns them into a string literal. #define ASSERT(x) #x ASSERT(a b c d) // is replaced by "a b c d" 回答2: #x is the stringification directive #define Stringify(x) #x means

How to add a modifier to a quoted regular (qr) expression

谁说我不能喝 提交于 2019-12-06 01:59:48
问题 Is there an easy way to add regex modifiers such as 'i' to a quoted regular expression? For example: $pat = qr/F(o+)B(a+)r/; $newpat = $pat . 'i'; # This doesn't work The only way I can think of is to print "$pat\n" and get back (?-xism:F(o+)B(a+)r) and try to remove the 'i' in ?-xism: with a substitution 回答1: You cannot put the flag inside the result of qr that you already have, because it’s protected. Instead, use this: $pat = qr/F(o+)B(a+)r/i; 回答2: You can modify an existing regex as if it

Stringify macro with GNU gfortran

こ雲淡風輕ζ 提交于 2019-12-05 15:25:01
How can I stringify a preprocessor macro with GNU gfortran? I would like to pass a macro definition to GNU gfortran which will then be used as a string in the code. Effectively I would like to do this: program test implicit none character (len=:), allocatable :: astring astring = MYMACRO write (*, *) astring end program test and then build with: gfortran -DMYMACRO=hello test.F90 I tried creating various macro, for example: #define STRINGIFY_(x) #x #define STRINGIFY(x) STRINGIFY_(x) ... astring = STRINGIFY(MYMACRO) but this doesn't work with the gfortran preprocessor. I also tried using a

What is the # for when formatting using %s

我的未来我决定 提交于 2019-12-05 09:15:48
I came across this example of an assertion and was wondering what the # is for: #define ASSERT( x ) if ( !( x ) ) { \ int *p = NULL; \ DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \ *p=1; \ } It is the "stringize" preprocessing operator. It takes the tokens passed as the argument to the macro parameter x and turns them into a string literal. #define ASSERT(x) #x ASSERT(a b c d) // is replaced by "a b c d" #x is the stringification directive #define Stringify(x) #x means Stringify(abc) will be substituted with "abc" as in #define initVarWithItsOwnName(x) const char* p = #x int main() {