2014-05-14 22:08:42 -04:00
|
|
|
/*
|
|
|
|
* str.c
|
|
|
|
* Copyright (C) 2014 c9s <c9s@c9smba.local>
|
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-05-16 08:22:25 -04:00
|
|
|
#include "r3_str.h"
|
|
|
|
#include "str_array.h"
|
|
|
|
#include "r3_define.h"
|
2014-05-14 22:08:42 -04:00
|
|
|
|
2014-05-15 10:57:13 -04:00
|
|
|
int strndiff(char * d1, char * d2, unsigned int n) {
|
|
|
|
char * o = d1;
|
|
|
|
while ( *d1 == *d2 && n-- > 0 ) {
|
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
|
|
|
return d1 - o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int strdiff(char * d1, char * d2) {
|
|
|
|
char * o = d1;
|
|
|
|
while( *d1 == *d2 ) {
|
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
|
|
|
return d1 - o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-15 12:00:19 -04:00
|
|
|
/**
|
|
|
|
* provide a quick way to count slugs, simply search for '{'
|
|
|
|
*/
|
|
|
|
int count_slug(char * p, int len) {
|
|
|
|
int s = 0;
|
2014-05-18 06:58:31 -04:00
|
|
|
int lev = 0;
|
2014-05-15 12:00:19 -04:00
|
|
|
while( len-- ) {
|
2014-05-18 06:58:31 -04:00
|
|
|
if ( lev == 0 && *p == '{' )
|
2014-05-15 12:00:19 -04:00
|
|
|
s++;
|
2014-05-18 06:58:31 -04:00
|
|
|
if ( *p == '{' ) {
|
|
|
|
lev++;
|
|
|
|
}
|
|
|
|
if ( *p == '}' ) {
|
|
|
|
lev--;
|
|
|
|
}
|
2014-05-15 12:00:19 -04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2014-05-15 10:57:13 -04:00
|
|
|
|
2014-05-16 00:33:59 -04:00
|
|
|
bool contains_slug(char * str) {
|
|
|
|
return strchr(str, '{') != NULL ? TRUE : FALSE;
|
2014-05-15 11:46:49 -04:00
|
|
|
}
|
|
|
|
|
2014-05-18 06:58:31 -04:00
|
|
|
char * inside_slug(char * needle, int needle_len, char *offset) {
|
|
|
|
char * s1 = offset;
|
|
|
|
char * s2 = offset;
|
|
|
|
|
|
|
|
while( s1 >= needle ) {
|
|
|
|
if ( *s1 == '{' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s1--;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *end = needle+ needle_len;
|
|
|
|
while( s2 < end ) {
|
|
|
|
if ( *s2 == '}' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( *s1 == '{' && *s2 == '}' ) {
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * find_slug_placeholder(char *s1, int *len) {
|
|
|
|
char *c;
|
|
|
|
char *s2;
|
|
|
|
int cnt = 0;
|
|
|
|
if ( NULL != (c = strchr(s1, '{')) ) {
|
|
|
|
// find closing '}'
|
|
|
|
s2 = c;
|
|
|
|
while(*s2) {
|
|
|
|
if (*s2 == '{' )
|
|
|
|
cnt++;
|
|
|
|
else if (*s2 == '}' )
|
|
|
|
cnt--;
|
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cnt!=0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(len) {
|
|
|
|
*len = s2 - c + 1;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* given a slug string, duplicate the pattern string of the slug
|
|
|
|
*/
|
|
|
|
char * find_slug_pattern(char *s1) {
|
|
|
|
char *c;
|
|
|
|
char *s2;
|
|
|
|
int cnt = 1;
|
|
|
|
if ( NULL != (c = strchr(s1, ':')) ) {
|
|
|
|
c++;
|
|
|
|
// find closing '}'
|
|
|
|
s2 = c;
|
|
|
|
while(s2) {
|
|
|
|
if (*s2 == '{' )
|
|
|
|
cnt++;
|
|
|
|
else if (*s2 == '}' )
|
|
|
|
cnt--;
|
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
int len = s2 - c;
|
|
|
|
return strndup(c, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-15 08:38:07 -04:00
|
|
|
/**
|
|
|
|
* @param char * sep separator
|
|
|
|
*/
|
2014-05-15 11:46:49 -04:00
|
|
|
char * compile_slug(char * str, int len)
|
2014-05-15 08:38:07 -04:00
|
|
|
{
|
2014-05-17 23:03:01 -04:00
|
|
|
char *s1 = NULL, *s2 = NULL, *o = NULL;
|
2014-05-15 11:46:49 -04:00
|
|
|
char *pat = NULL;
|
|
|
|
char sep = '/';
|
|
|
|
|
|
|
|
// find '{'
|
|
|
|
s1 = strchr(str, '{');
|
|
|
|
|
|
|
|
if ( s1 == NULL ) {
|
2014-05-16 19:26:47 -04:00
|
|
|
return strdup(str);
|
2014-05-15 11:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( (s1 - str) > 0 ) {
|
|
|
|
sep = *(s1-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char * out = NULL;
|
|
|
|
if ((out = calloc(sizeof(char),128)) == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// append prefix
|
|
|
|
o = out;
|
|
|
|
strncat(o, str, s1 - str);
|
|
|
|
o += (s1 - str);
|
|
|
|
|
|
|
|
// start after ':'
|
|
|
|
if ( NULL != (pat = strchr(s1, ':')) ) {
|
|
|
|
pat++;
|
2014-05-15 08:38:07 -04:00
|
|
|
|
2014-05-18 02:30:00 -04:00
|
|
|
// find closing '}'
|
|
|
|
int cnt = 1;
|
|
|
|
s2 = pat;
|
|
|
|
while(s2) {
|
|
|
|
if (*s2 == '{' )
|
|
|
|
cnt++;
|
|
|
|
else if (*s2 == '}' )
|
|
|
|
cnt--;
|
|
|
|
|
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
2014-05-15 08:38:07 -04:00
|
|
|
// this slug contains a pattern
|
2014-05-18 02:30:00 -04:00
|
|
|
// s2 = strchr(pat, '}');
|
2014-05-15 11:46:49 -04:00
|
|
|
|
|
|
|
*o = '(';
|
|
|
|
o++;
|
|
|
|
|
|
|
|
strncat(o, pat, (s2 - pat) );
|
|
|
|
o += (s2 - pat);
|
|
|
|
|
|
|
|
*o = ')';
|
|
|
|
o++;
|
2014-05-15 08:38:07 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// should return a '[^/]+' pattern
|
2014-05-15 11:46:49 -04:00
|
|
|
// strncat(c, "([^%c]+)", strlen("([^%c]+)") );
|
|
|
|
// snprintf(pat, 128, "([^%c]+)", sep);
|
|
|
|
sprintf(o, "([^%c]+)", sep);
|
|
|
|
o+= sizeof("([^%c]+)");
|
|
|
|
}
|
|
|
|
|
|
|
|
s2++;
|
|
|
|
while( (s2 - str) > len ) {
|
|
|
|
*o = *s2;
|
|
|
|
s2++;
|
|
|
|
o++;
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
2014-05-15 11:46:49 -04:00
|
|
|
return out;
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
2014-05-14 22:08:42 -04:00
|
|
|
|
|
|
|
|
2014-05-15 00:53:48 -04:00
|
|
|
char * ltrim_slash(char* str)
|
|
|
|
{
|
|
|
|
char * p = str;
|
|
|
|
while (*p == '/') p++;
|
2014-05-16 19:26:47 -04:00
|
|
|
return strdup(p);
|
2014-05-15 00:53:48 -04:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:08:42 -04:00
|
|
|
char** str_split(char* a_str, const char a_delim)
|
|
|
|
{
|
|
|
|
char** result = 0;
|
|
|
|
size_t count = 0;
|
|
|
|
char* tmp = a_str;
|
|
|
|
char* last_comma = 0;
|
|
|
|
char delim[2];
|
|
|
|
delim[0] = a_delim;
|
|
|
|
delim[1] = 0;
|
|
|
|
|
|
|
|
/* Count how many elements will be extracted. */
|
|
|
|
while (*tmp)
|
|
|
|
{
|
|
|
|
if (a_delim == *tmp)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
last_comma = tmp;
|
|
|
|
}
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add space for trailing token. */
|
|
|
|
count += last_comma < (a_str + strlen(a_str) - 1);
|
|
|
|
|
|
|
|
/* Add space for terminating null string so caller
|
|
|
|
knows where the list of returned strings ends. */
|
|
|
|
count++;
|
|
|
|
|
|
|
|
result = malloc(sizeof(char*) * count);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
char* token = strtok(a_str, delim);
|
|
|
|
|
|
|
|
while (token)
|
|
|
|
{
|
|
|
|
assert(idx < count);
|
2014-05-16 19:26:47 -04:00
|
|
|
*(result + idx++) = strdup(token);
|
2014-05-14 22:08:42 -04:00
|
|
|
token = strtok(0, delim);
|
|
|
|
}
|
|
|
|
assert(idx == count - 1);
|
|
|
|
*(result + idx) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-05-15 06:02:10 -04:00
|
|
|
|
|
|
|
void str_repeat(char *s, char *c, int len) {
|
|
|
|
while(len--) {
|
|
|
|
s[len - 1] = *c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_indent(int level) {
|
|
|
|
int len = level * 2;
|
|
|
|
while(len--) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 12:35:56 -04:00
|
|
|
|
2014-05-16 19:26:47 -04:00
|
|
|
#ifndef HAVE_STRDUP
|
|
|
|
char *strdup(const char *s) {
|
2014-05-16 12:35:56 -04:00
|
|
|
char *out;
|
2014-05-16 15:17:51 -04:00
|
|
|
int count = 0;
|
2014-05-16 12:35:56 -04:00
|
|
|
while( s[count] )
|
|
|
|
++count;
|
|
|
|
++count;
|
2014-05-17 01:01:33 -04:00
|
|
|
out = malloc(sizeof(char) * count);
|
2014-05-16 12:35:56 -04:00
|
|
|
out[--count] = 0;
|
|
|
|
while( --count >= 0 )
|
|
|
|
out[count] = s[count];
|
|
|
|
return out;
|
|
|
|
}
|
2014-05-16 19:26:47 -04:00
|
|
|
#endif
|
2014-05-16 12:35:56 -04:00
|
|
|
|
2014-05-16 19:26:47 -04:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
char *strndup(const char *s, int n) {
|
2014-05-16 12:35:56 -04:00
|
|
|
char *out;
|
2014-05-16 15:17:51 -04:00
|
|
|
int count = 0;
|
2014-05-16 12:35:56 -04:00
|
|
|
while( count < n && s[count] )
|
|
|
|
++count;
|
|
|
|
++count;
|
2014-05-17 01:01:33 -04:00
|
|
|
out = malloc(sizeof(char) * count);
|
2014-05-16 12:35:56 -04:00
|
|
|
out[--count] = 0;
|
|
|
|
while( --count >= 0 )
|
|
|
|
out[count] = s[count];
|
|
|
|
return out;
|
|
|
|
}
|
2014-05-16 19:26:47 -04:00
|
|
|
#endif
|