wrote = PR_snprintf(p2, available, "%s%s;q=0.%02u", comma, token, (unsigned) (q * 100.0));
wrote = PR_snprintf(p2, available, "%s%s;q=0.%01u", comma, token, QVAL_TO_UINT(q));
PrepareAcceptCharsets(const char *i_AcceptCharset, nsACString &o_AcceptCharset){    PRUint32 n, size, wrote;    PRInt32 available;    double q, dec;    char *p, *p2, *token, *q_Accept, *o_Accept;    const char *acceptable, *comma;    PRBool add_utf = PR_FALSE;    PRBool add_asterisk = PR_FALSE;    if (!i_AcceptCharset)        acceptable = "";    else        acceptable = i_AcceptCharset;    o_Accept = nsCRT::strdup(acceptable);    if (nsnull == o_Accept)        return NS_ERROR_OUT_OF_MEMORY;    for (p = o_Accept, n = size = 0; '\0' != *p; p++) {        if (*p == ',') n++;            size++;    }    // only add "utf-8" and "*" to the list if they aren't    // already specified.    if (PL_strcasestr(acceptable, "utf-8") == NULL) {        n++;        add_utf = PR_TRUE;    }    if (PL_strstr(acceptable, "*") == NULL) {        n++;        add_asterisk = PR_TRUE;    }    available = size + ++n * 11 + 1;    q_Accept = new char[available];    if ((char *) 0 == q_Accept)        return NS_ERROR_OUT_OF_MEMORY;    *q_Accept = '\0';    q = 1.0;    dec = q / (double) n;    n = 0;    p2 = q_Accept;    for (token = nsCRT::strtok(o_Accept, ",", &p);         token != (char *) 0;         token = nsCRT::strtok(p, ",", &p)) {        while (*token == ' ' || *token == '\x9') token++;        char* trim;        trim = PL_strpbrk(token, "; \x9");        if (trim != (char*)0)  // remove "; q=..." if present            *trim = '\0';        if (*token != '\0') {            comma = n++ != 0 ? ", " : ""; // delimiter if not first item            if (q < 0.9995)                wrote = PR_snprintf(p2, available, "%s%s;q=0.%02u", comma, token, (unsigned) (q * 100.0));            else                wrote = PR_snprintf(p2, available, "%s%s", comma, token);            q -= dec;            p2 += wrote;            available -= wrote;            NS_ASSERTION(available > 0, "allocated string not long enough");        }    }    if (add_utf) {        comma = n++ != 0 ? ", " : ""; // delimiter if not first item        if (q < 0.9995)            wrote = PR_snprintf(p2, available, "%sutf-8;q=0.%02u", comma, (unsigned) (q * 100.0));        else            wrote = PR_snprintf(p2, available, "%sutf-8", comma);        q -= dec;        p2 += wrote;        available -= wrote;        NS_ASSERTION(available > 0, "allocated string not long enough");    }    if (add_asterisk) {        comma = n++ != 0 ? ", " : ""; // delimiter if not first item        // keep q of "*" equal to the lowest q value        // in the event of a tie between the q of "*" and a non-wildcard        // the non-wildcard always receives preference.        q += dec;        if (q < 0.9995) {            wrote = PR_snprintf(p2, available, "%s*;q=0.%02u", comma, (unsigned) (q * 100.0));        }        else            wrote = PR_snprintf(p2, available, "%s*", comma);        available -= wrote;        p2 += wrote;        NS_ASSERTION(available > 0, "allocated string not long enough");    }    nsCRT::free(o_Accept);    // change alloc from C++ new/delete to nsCRT::strdup's way    o_AcceptCharset.Assign(q_Accept);#if defined DEBUG_havill    printf("Accept-Charset: %s\n", q_Accept);#endif    delete [] q_Accept;    return NS_OK;}