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.

  1. void lval_print_str(lval* v) {
  2. putchar('"');
  3. /* Loop over the characters in the string */
  4. for (int i = 0; i < strlen(v->str); i++) {
  5. if (strchr(lval_str_escapable, v->str[i])) {
  6. /* If the character is escapable then escape it */
  7. printf("%s", lval_str_escape(v->str[i]));
  8. } else {
  9. /* Otherwise print character as it is */
  10. putchar(v->str[i]);
  11. }
  12. }
  13. putchar('"');
  14. }