Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

about tutorial07 function—lept_stringify_string. why not add ' \" ' because it's the begin or end mark for a string #238

Open
wz991007 opened this issue Feb 26, 2023 · 3 comments

Comments

@wz991007
Copy link

wz991007 commented Feb 26, 2023

为什么在string解析的时候会 判断是否有 ' \" ' 来判断是否是一个string ,而在tutorial07没有将 ' \" ' 添加回来

@gxuu
Copy link

gxuu commented Feb 27, 2023

tutorial07_answer.md中的生成字符串一节里有

static void lept_stringify_string(lept_context* c, const char* s, size_t len) {
    size_t i;
    assert(s != NULL);
    PUTC(c, '"');  // add string begin mark
    for (i = 0; i < len; i++) {
        unsigned char ch = (unsigned char)s[i];
        switch (ch) {
            /* ... */
        }
    }
    PUTC(c, '"'); // add string end mark
}

有两个PUTC,加回来了。

@wz991007
Copy link
Author

wz991007 commented Feb 28, 2023

tutorial07_answer.md中的生成字符串一节里有

static void lept_stringify_string(lept_context* c, const char* s, size_t len) {
    size_t i;
    assert(s != NULL);
    PUTC(c, '"');  // add string begin mark
    for (i = 0; i < len; i++) {
        unsigned char ch = (unsigned char)s[i];
        switch (ch) {
            /* ... */
        }
    }
    PUTC(c, '"'); // add string end mark
}

有两个PUTC,加回来了。

为什么加的不是' \" ' ,

@gxuu
Copy link

gxuu commented Mar 2, 2023

因为这里的\是转义符。而双引号的转义符是可选的。也就是说:

PUTC(c, '\"'); 
PUTC(c, '"');

是等价的。两者都会添加一个双引号到字符串里。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants