Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created September 28, 2019 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiya000/dd57209f3abf43ce0a46fafb242fe79c to your computer and use it in GitHub Desktop.
Save aiya000/dd57209f3abf43ce0a46fafb242fe79c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_closing_quote(char *template, int is_literal_string, int has_previous)
{
if (!is_literal_string)
{
// is not \"
return
(!has_previous || *(template - 1) != '\\') &&
(*template == '"');
}
// is not ''
return
(has_previous && *(template - 1) != '\'' && *template == '\'') ||
(*template == '\'' && *(template + 1) != '\'') ;
}
int main(void) {
// char* str = "ho-ya! \\\"\"";
// printf("%d\n", is_closing_quote(str, 0, 0)); // 0
// printf("%d\n", is_closing_quote(str + 8, 0, 1)); // 0
// printf("%d\n", is_closing_quote(str + 9, 0, 1)); // 1
char* lit = "ho-ya! '''";
printf("%d\n", is_closing_quote(lit, 1, 0)); // 0
printf("%d\n", is_closing_quote(lit + 7, 1, 0)); // 0
printf("%d\n", is_closing_quote(lit + 8, 1, 0)); // 0
printf("%d\n", is_closing_quote(lit + 9, 1, 0)); // 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment