Printing Strings
As well as using mpc
to unescape strings we also used mpc
to escape strings when we printed them out.
So if we’re going to replace all uses of mpc
we better do it here too. Given our functions we already defined for escaping and unescaping characters this wont be too difficult. We’ll just loop over each character in the string and if it is esapable then we’ll escape it, otherwise we’ll print it out as normal.
void lval_print_str(lval* v) {
putchar('"');
/* Loop over the characters in the string */
for (int i = 0; i < strlen(v->str); i++) {
if (strchr(lval_str_escapable, v->str[i])) {
/* If the character is escapable then escape it */
printf("%s", lval_str_escape(v->str[i]));
} else {
/* Otherwise print character as it is */
putchar(v->str[i]);
}
}
putchar('"');
}
当前内容版权归 orangeduck 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 orangeduck .