Ejemplo n.º 1
0
static int
test_round_trip(void)
{
    char **strp;
    gboolean success = TRUE;

    for (strp = quotable_strings; *strp; strp++) {
	char *quoted, *unquoted;

	quoted = quote_string(*strp);
	unquoted = unquote_string(quoted);

	/* if they're not the same, complain */
	if (0 != strcmp(*strp, unquoted)) {
	    char *safe_orig = safestr(*strp);
	    char *safe_quoted = safestr(quoted);
	    char *safe_unquoted = safestr(unquoted);

	    printf("  bad round-trip: %s -quote_string-> %s -unquote_string-> %s\n",
		safe_orig, safe_quoted, safe_unquoted);

	    amfree(safe_orig);
	    amfree(safe_quoted);
	    amfree(safe_unquoted);

	    success = FALSE;
	}

	amfree(quoted);
	amfree(unquoted);
    }

    return success;
}
Ejemplo n.º 2
0
/****
 * Test the strquotedstr function
 */
static int
test_strquotedstr_skipping(void)
{
    char **iter1, **iter2;
    gboolean success = TRUE;

    /* the idea here is to loop over all pairs of strings, forming a
     * string by quoting them with quote_string and inserting a space, then
     * re-splitting with strquotedstr.  This should get us back to our
     * starting point. Note that we have to begin with a non-quoted identifier,
     * becuse strquotedstr requires that strtok_r has already been called. */

    for (iter1 = quotable_strings; *iter1; iter1++) {
	for (iter2 = quotable_strings; *iter2; iter2++) {
	    char *q1 = quote_string(*iter1);
	    char *q2 = quote_string(*iter2);
	    char *combined = vstralloc("START ", q1, " ", q2, NULL);
	    char *copy = g_strdup(combined);
	    char *saveptr = NULL;
	    char *tok;
	    int i;

	    tok = strtok_r(copy, " ", &saveptr);

	    for (i = 1; i <= 2; i++) {
		char *expected = (i == 1)? q1:q2;
		tok = strquotedstr(&saveptr);
		if (!tok) {
		    g_fprintf(stderr, "while parsing '%s', call %d to strquotedstr returned NULL\n",
			      combined, i);
		    success = FALSE;
		    goto next;
		}
		if (0 != strcmp(tok, expected)) {
		    char *safe = safestr(tok);

		    g_fprintf(stderr, "while parsing '%s', call %d to strquotedstr returned '%s' "
			      "but '%s' was expected.\n",
			      combined, i, safe, expected);
		    success = FALSE;
		    goto next;
		}
	    }

	    if (strquotedstr(&saveptr) != NULL) {
		g_fprintf(stderr, "while parsing '%s', call 3 to strquotedstr did not return NULL\n",
			  combined);
		success = FALSE;
		goto next;
	    }
next:
	    amfree(q1);
	    amfree(q2);
	    amfree(copy);
	    amfree(combined);
	}
    }

    return success;
}
Ejemplo n.º 3
0
int32
BString::IFindLast(const char* string) const
{
	if (string == NULL)
		return B_BAD_VALUE;

	return _IFindBefore(string, Length(), strlen(safestr(string)));
}
Ejemplo n.º 4
0
int32
BString::IFindFirst(const char* string) const
{
	if (string == NULL)
		return B_BAD_VALUE;

	return _IFindAfter(string, 0, strlen(safestr(string)));
}
Ejemplo n.º 5
0
int32
BString::IFindLast(const char* string, int32 beforeOffset) const
{
	if (string == NULL)
		return B_BAD_VALUE;

	if (beforeOffset < 0)
		return B_ERROR;

	return _IFindBefore(string, min_clamp0(beforeOffset, Length()),
		strlen(safestr(string)));
}
Ejemplo n.º 6
0
int32
BString::IFindFirst(const char* string, int32 fromOffset) const
{
	if (string == NULL)
		return B_BAD_VALUE;

	if (fromOffset < 0)
		return B_ERROR;

	return _IFindAfter(string, min_clamp0(fromOffset,Length()),
		strlen(safestr(string)));
}
Ejemplo n.º 7
0
static gboolean
compare_strv(
    const char **exp,
    char **got,
    const char *source,
    const char *original)
{
    const char **a = exp;
    char **b = got;
    while (*a && *b) {
	if (0 != strcmp(*a, *b))
	    break;
	a++; b++;
    }

    /* did we exit the loop early, or were they different lengths? */
    if (*a || *b) {
	char *safe;

	safe = safestr(original);
	g_printf("  %s: expected [", safe);
	amfree(safe);
	for (a = exp; *a; a++) {
	    safe = safestr(*a);
	    g_printf("%s%s", safe, *(a+1)? ", " : "");
	    amfree(safe);
	}
	g_printf("] but got [");
	for (b = got; *b; b++) {
	    safe = safestr(*b);
	    g_printf("%s%s", safe, *(b+1)? ", " : "");
	    amfree(safe);
	}
	g_printf("] using %s.\n", source);

	return FALSE;
    }

    return TRUE;
}
Ejemplo n.º 8
0
BString&
BString::SetTo(const char* string, int32 maxLength)
{
	if (maxLength < 0)
		maxLength = INT32_MAX;

	maxLength = strlen_clamp(safestr(string), maxLength);

	if (_MakeWritable(maxLength, false) == B_OK)
		memcpy(fPrivateData, string, maxLength);

	return *this;
}
Ejemplo n.º 9
0
bool
BString::operator>=(const char* string) const
{
	return strcmp(String(), safestr(string)) >= 0;
}
Ejemplo n.º 10
0
BString::BString(const char* string, int32 maxLength)
	: fPrivateData(NULL)
{
	_Init(string, strlen_clamp(safestr(string), maxLength));
}
Ejemplo n.º 11
0
BString::BString(const char* string)
	:
	fPrivateData(NULL)
{
	_Init(string, strlen(safestr(string)));
}
Ejemplo n.º 12
0
int
BString::ICompare(const char* string, int32 length) const
{
	return strncasecmp(String(), safestr(string), length);
}
Ejemplo n.º 13
0
int
BString::ICompare(const char* string) const
{
	return strcasecmp(String(), safestr(string));
}
Ejemplo n.º 14
0
/****
 * Test unquoting of some pathological strings
 */
static int
test_unquote_string(void)
{
    gboolean success = TRUE;
    char *tests[] = {
	"simple",              "simple",
	"\"quoted\"",          "quoted",
	"s p a c e",           "s p a c e",

	/* special escape characters */
	"esc \\\" quote",      "esc \" quote",
	"esc \\t tab",         "esc \t tab",
	"esc \\\\ esc",        "esc \\ esc",
	"esc \\02 oct",         "esc \02 oct",
	"esc \\7 oct",         "esc \7 oct",
	"esc \\17 oct",        "esc \17 oct",
	"esc \\117 oct",       "esc \117 oct",
	"esc \\1117 oct",      "esc \1117 oct", /* '7' is distinct char */

	/* same, but pre-quoted */
	"\"esc \\\" quote\"",  "esc \" quote",
	"\"esc \\t tab\"",     "esc \t tab",
	"\"esc \\\\ esc\"",    "esc \\ esc",
	"\"esc \\02 oct\"",     "esc \02 oct",
	"\"esc \\7 oct\"",     "esc \7 oct",
	"\"esc \\17 oct\"",    "esc \17 oct",
	"\"esc \\117 oct\"",   "esc \117 oct",
	"\"esc \\1117 oct\"",  "esc \1117 oct", /* '7' is distinct char */

	/* strips balanced quotes, even inside the string */
	">>\"x\"<<",           ">>x<<",
	">>\"x\"-\"y\"<<",     ">>x-y<<",

	/* pathological, but valid */
	"\\\\",                "\\",
	"\"\\\"\"",            "\"",
	"\"\\\\\"",            "\\",
	"--\\\"",              "--\"",
	"\\\"--",              "\"--",

	/* invalid strings (handling here is arbitrary, but these tests
	 * will alert us if the handling changes) */
	"\\",                  "", /* trailing backslash is ignored */
	"xx\\",                "xx", /* ditto */
	"\\\\\\\\\\\\\\",      "\\\\\\",   /* ditto */
	"\\777",               "\377", /* 0777 & 0xff = 0xff */
	"\"--",                "--", /* leading quote is dropped */
	"--\"",                "--", /* trailing quote is dropped */

	NULL, NULL,
    };
    char **strp;

    for (strp = tests; *strp;) {
	char *quoted = *(strp++);
	char *expected = *(strp++);
	char *unquoted = unquote_string(quoted);

	/* if they're not the same, complain */
	if (0 != strcmp(expected, unquoted)) {
	    char *safe_quoted = safestr(quoted);
	    char *safe_unquoted = safestr(unquoted);
	    char *safe_expected = safestr(expected);

	    printf("  %s unquoted to %s; expected %s.\n",
		safe_quoted, safe_unquoted, safe_expected);

	    amfree(safe_quoted);
	    amfree(safe_unquoted);
	    amfree(safe_expected);

	    success = FALSE;
	}

	amfree(unquoted);
    }

    return success;
}