Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created September 28, 2019 14:51
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/a8f0d76ac49710ff8db23033c4b225dd to your computer and use it in GitHub Desktop.
Save aiya000/a8f0d76ac49710ff8db23033c4b225dd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* get_repeated_single_quotes(char* x, int has_previous) {
char* p;
char* result;
size_t result_length;
size_t i;
for (p = x; *p == '\''; ++p)
{
}
result_length = (p - x) + (has_previous && *(x - 1) == '\'');
result = (char*) malloc(result_length + 1);
for (i = 0; i < result_length; ++i)
{
result[i] = '\'';
}
result[result_length] = '\0';
return result;
}
int is_closing_quote(char *template, int is_literal_string, int has_previous)
{
char* repeated_single_quotes;
int result_on_single;
if (!is_literal_string)
{
// is not \"
return
(!has_previous || *(template - 1) != '\\') &&
(*template == '"');
}
// is not ''
if (*template != '\'')
{
return 0;
}
if (!has_previous && *(template + 1) == '\'')
{
return 0;
}
else if (has_previous && *(template - 1) == '\'' && *(template + 1) != '\'')
{
return 0;
}
return 1;
}
int main(void) {
char* x = get_repeated_single_quotes("'''", 0);
printf("%s\n", x);
char* y = get_repeated_single_quotes(x + 1, 1);
printf("%s\n", y);
printf("double\n");
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);
printf("single\n");
char* lit = "ho-ya! '''";
printf("%d\n", is_closing_quote(lit, 1, 0) == 0);
printf("%d\n", is_closing_quote(lit + 7, 1, 1) == 0);
printf("%d\n", is_closing_quote(lit + 8, 1, 1) == 0);
printf("%d\n", is_closing_quote(lit + 9, 1, 1) == 1);
printf("single2\n");
char* whole = "'I''ll come with you.'";
char* to = whole + 2;
printf("%d\n", is_closing_quote(to, 1, 1) == 0);
printf("%d\n", is_closing_quote(to + 19, 1, 1) == 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment