1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Drop leading and trailing newlines in longstrings.

Long, heredoc style strings can now have
a non semantic leading newline character. This makes it
easier to define large columns of text.
This commit is contained in:
Calvin Rose 2019-04-21 13:34:41 -04:00
parent 4ddf90e301
commit 9100794cea
3 changed files with 18 additions and 6 deletions

View File

@ -4,7 +4,7 @@
:name "numarray" :name "numarray"
:source @["numarray.c"]) :source @["numarray.c"])
(import build/numarray :prefix "") (import build/numarray :as numarray)
(def a (numarray/new 30)) (def a (numarray/new 30))
(print (get a 20)) (print (get a 20))

View File

@ -100,12 +100,12 @@ Janet num_array_get(void *p, Janet key) {
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{ {
"numarray/new", num_array_new, "new", num_array_new,
"(numarray/new size)\n\n" "(numarray/new size)\n\n"
"Create new numarray" "Create new numarray"
}, },
{ {
"numarray/scale", num_array_scale, "scale", num_array_scale,
"(numarray/scale numarray factor)\n\n" "(numarray/scale numarray factor)\n\n"
"scale numarray by factor" "scale numarray by factor"
}, },

View File

@ -257,12 +257,24 @@ static int escape1(JanetParser *p, JanetParseState *state, uint8_t c) {
static int stringend(JanetParser *p, JanetParseState *state) { static int stringend(JanetParser *p, JanetParseState *state) {
Janet ret; Janet ret;
uint8_t *bufstart = p->buf;
int32_t buflen = (int32_t) p->bufcount;
if (state->flags & PFLAG_LONGSTRING) {
/* Check for leading newline character so we can remove it */
if (bufstart[0] == '\n') {
bufstart++;
buflen--;
}
if (buflen > 0 && bufstart[buflen - 1] == '\n') {
buflen--;
}
}
if (state->flags & PFLAG_BUFFER) { if (state->flags & PFLAG_BUFFER) {
JanetBuffer *b = janet_buffer((int32_t)p->bufcount); JanetBuffer *b = janet_buffer(buflen);
janet_buffer_push_bytes(b, p->buf, (int32_t)p->bufcount); janet_buffer_push_bytes(b, bufstart, buflen);
ret = janet_wrap_buffer(b); ret = janet_wrap_buffer(b);
} else { } else {
ret = janet_wrap_string(janet_string(p->buf, (int32_t)p->bufcount)); ret = janet_wrap_string(janet_string(bufstart, buflen));
} }
p->bufcount = 0; p->bufcount = 0;
popstate(p, ret); popstate(p, ret);