StrPushBack

Table of Contents

StrPushBack

Description

Push char into string.

Parameters

NameDirectionDescription
strinStr to push char into
chrinPointer to value to be pushed

Success

Returns str the string itself on success.

Failure

Returns NULL otherwise.

Usage example (Cross-references)

    bool ___is_first___ = true;                                                                                    \
    (void)___is_first___;                                                                                          \
    StrPushBack(&(j), '{');                                                                                        \
    {writer};                                                                                                      \
    StrPushBack(&(j), '}');                                                                                        \
    StrPushBack(&(j), '{');                                                                                        \
    {writer};                                                                                                      \
    StrPushBack(&(j), '}');                                                                                        \
    } while (0)
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    bool ___is_first___ = true;                                                                                    \
    (void)___is_first___;                                                                                          \
    StrPushBack(&(j), '[');                                                                                        \
    VecForeach(&(arr), item, {                                                                                     \
    if (___is_first___) {                                                                                      \
    ___is_first___ = false;                                                                                \
    } else {                                                                                                   \
    StrPushBack(&(j), ',');                                                                                \
    }                                                                                                          \
    { writer }                                                                                                 \
    { writer }                                                                                                 \
    });                                                                                                            \
    StrPushBack(&(j), ']');                                                                                        \
    } while (0)
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    ___is_first___ = false;                                                                                    \
    } else {                                                                                                       \
    StrPushBack(&(j), ',');                                                                                    \
    }                                                                                                              \
    StrAppendf(&(j), "\"%s\":", k);                                                                                \
    // escape sequence
    case '\\' :
    StrPushBack(str, '\\');
    StrIterNext(&si);
    break;
    
    case '"' :
    StrPushBack(str, '"');
    StrIterNext(&si);
    break;
    
    case '/' :
    StrPushBack(str, '/');
    StrIterNext(&si);
    break;
    
    case 'b' :
    StrPushBack(str, '\b');
    StrIterNext(&si);
    break;
    
    case 'f' :
    StrPushBack(str, '\f');
    StrIterNext(&si);
    break;
    
    case 'n' :
    StrPushBack(str, '\n');
    StrIterNext(&si);
    break;
    
    case 'r' :
    StrPushBack(str, '\r');
    StrIterNext(&si);
    break;
    
    case 't' :
    StrPushBack(str, '\t');
    StrIterNext(&si);
    break;
    // default allowed characters
    default :
    StrPushBack(str, StrIterPeek(&si));
    StrIterNext(&si);
    break;
    has_exp = true;
    is_flt  = true;
    StrPushBack(&ns, StrIterPeek(&si));
    StrIterNext(&si);
    break;
    }
    is_flt = true;
    StrPushBack(&ns, StrIterPeek(&si));
    StrIterNext(&si);
    break;
    case '8' :
    case '9' :
    StrPushBack(&ns, StrIterPeek(&si));
    StrIterNext(&si);
    break;
    }
    has_exp_plus_minus = true;
    StrPushBack(&ns, StrIterPeek(&si));
    StrIterNext(&si);
    break;
    // Pad on right
    for (size i = 0; i < pad_len; i++) {
    StrPushBack(o, ' ');
    }
    } else { // ALIGN_CENTER
    // Pad on right
    for (size i = 0; i < right_pad; i++) {
    StrPushBack(o, ' ');
    }
    }
    // Check for escaped brace
    if (i + 1 < fmt_len && fmt[i + 1] == '{') {
    StrPushBack(o, '{');
    i++; // Skip next brace
    continue;
    // Check for escaped brace
    if (i + 1 < fmt_len && fmt[i + 1] == '}') {
    StrPushBack(o, '}');
    i++; // Skip next brace
    continue;
    return false;
    } else {
    StrPushBack(o, fmt[i]);
    }
    }
    // For 'c' format specifier, preserve the original case
    if (force_case) {
    StrPushBack(o, is_caps ? TO_UPPER(byte) : TO_LOWER(byte));
    } else {
    StrPushBack(o, byte);
    StrPushBack(o, is_caps ? TO_UPPER(byte) : TO_LOWER(byte));
    } else {
    StrPushBack(o, byte);
    }
    } else {
    
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, c1);
    StrPushBack(o, c2);
    }
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, c1);
    StrPushBack(o, c2);
    }
    }
    // For 'c' format specifier, preserve the original case
    if (force_case) {
    StrPushBack(o, is_caps ? TO_UPPER(*vs) : TO_LOWER(*vs));
    } else {
    StrPushBack(o, *vs);
    StrPushBack(o, is_caps ? TO_UPPER(*vs) : TO_LOWER(*vs));
    } else {
    StrPushBack(o, *vs);
    }
    } else {
    StrForeachIdx(s, c, i, {
    if (i > 0) {
    StrPushBack(o, ' ');
    }
    // Create hex string for each character
    StrForeachInRange(s, c, 0, len, {
    if (IS_PRINTABLE(c)) {
    StrPushBack(o, c);
    } else {
    const char* digits = "0123456789abcdef";
    const char* digits = "0123456789abcdef";
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, digits[c >> 4]);
    StrPushBack(o, digits[c & 0xf]);
    }
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, digits[c >> 4]);
    StrPushBack(o, digits[c & 0xf]);
    }
    });
    while (xs[i]) {
    if (i > 0) {
    StrPushBack(o, ' ');
    }
    // Create hex string for each character
    for (size i = 0; i < len; i++) {
    if (IS_PRINTABLE(xs[i])) {
    StrPushBack(o, xs[i]);
    } else {
    const char* digits = "0123456789abcdef";
    const char* digits = "0123456789abcdef";
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, digits[xs[i] >> 4]);
    StrPushBack(o, digits[xs[i] & 0xf]);
    }
    StrPushBackZstr(o, "\\x");
    StrPushBack(o, digits[xs[i] >> 4]);
    StrPushBack(o, digits[xs[i] & 0xf]);
    }
    }
    // Direct string append for infinity
    if (*v < 0) {
    StrPushBack(o, '-');
    }
    }
    
    StrPushBack(s, c);
    } else if (*i == quote) {
    i++;      // Skip closing quote
    }
    
    StrPushBack(s, c);
    }
    } else {
    }
    
    StrPushBack(s, c);
    } else {
    char c = *i++;
    }
    
    StrPushBack(s, c);
    }
    }
    }
    
    StrPushBack(o, *v);
    }
    switch (fmt_info->endian) {
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 8));
    StrPushBack(o, (x & 0xff));
    break;
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 8));
    StrPushBack(o, (x & 0xff));
    break;
    }
    }
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8));
    break;
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8));
    break;
    }
    switch (fmt_info->endian) {
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 24));
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 24));
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 24));
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    break;
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    break;
    }
    }
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24));
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24));
    break;
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24));
    break;
    }
    switch (fmt_info->endian) {
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 56));
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    case ENDIAN_BIG : {
    StrPushBack(o, (x >> 56));
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 56));
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    break;
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x & 0xff));
    break;
    }
    }
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    case ENDIAN_LITTLE : {
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x & 0xff));
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 8) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 16) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 24) & 0xff);
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 56));
    StrPushBack(o, (x >> 32) & 0xff);
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 56));
    break;
    StrPushBack(o, (x >> 40) & 0xff);
    StrPushBack(o, (x >> 48) & 0xff);
    StrPushBack(o, (x >> 56));
    break;
    }
    for (u64 i = 0; i < bv->length; i++) {
    char bit_char = BitVecGet(bv, i) ? '1' : '0';
    StrPushBack(&result, bit_char);
    }
    // Add prefix if requested
    if (config->use_prefix) {
    StrPushBack(str, '0');
    if (config->base == 2) {
    StrPushBack(str, 'b');
    StrPushBack(str, '0');
    if (config->base == 2) {
    StrPushBack(str, 'b');
    } else if (config->base == 8) {
    StrPushBack(str, 'o');
    StrPushBack(str, 'b');
    } else if (config->base == 8) {
    StrPushBack(str, 'o');
    } else if (config->base == 16) {
    StrPushBack(str, 'x');
    StrPushBack(str, 'o');
    } else if (config->base == 16) {
    StrPushBack(str, 'x');
    }
    }
    // Convert number to string
    if (value == 0) {
    StrPushBack(str, '0');
    } else {
    char   buffer[65];
    // Add digits in correct order
    while (pos > 0) {
    StrPushBack(str, buffer[--pos]);
    }
    }
    const char* nan_str = config->uppercase ? "NAN" : "nan";
    for (size_t i = 0; i < 3; i++) {
    StrPushBack(str, nan_str[i]);
    }
    return str;
    if (isinf(value)) {
    if (value < 0) {
    StrPushBack(str, '-');
    } else if (config->always_sign) {
    StrPushBack(str, '+');
    StrPushBack(str, '-');
    } else if (config->always_sign) {
    StrPushBack(str, '+');
    }
    const char* inf_str = config->uppercase ? "INF" : "inf";
    const char* inf_str = config->uppercase ? "INF" : "inf";
    for (size_t i = 0; i < 3; i++) {
    StrPushBack(str, inf_str[i]);
    }
    return str;
    // Handle sign
    if (value < 0) {
    StrPushBack(str, '-');
    value = -value;
    } else if (config->always_sign) {
    value = -value;
    } else if (config->always_sign) {
    StrPushBack(str, '+');
    }
    // Format mantissa with proper rounding
    i64 int_part = (i64)mantissa;
    StrPushBack(str, '0' + int_part);
    
    if (config->precision > 0) {
    
    if (config->precision > 0) {
    StrPushBack(str, '.');
    f64 frac_part = mantissa - int_part;
    if (digit >= 10)
    digit = 9;                       // Clamp to prevent overflow
    StrPushBack(str, '0' + digit);
    frac_part -= (int)frac_part;         // Remove the integer part
    }
    
    // Add exponent
    StrPushBack(str, config->uppercase ? 'E' : 'e');
    if (exp >= 0) {
    StrPushBack(str, '+');
    StrPushBack(str, config->uppercase ? 'E' : 'e');
    if (exp >= 0) {
    StrPushBack(str, '+');
    } else {
    StrPushBack(str, '-');
    StrPushBack(str, '+');
    } else {
    StrPushBack(str, '-');
    exp = -exp;
    }
    // Format exponent digits (always at least 2 digits)
    if (exp == 0) {
    StrPushBack(str, '0');
    StrPushBack(str, '0');
    } else {
    if (exp == 0) {
    StrPushBack(str, '0');
    StrPushBack(str, '0');
    } else {
    char   exp_buf[8];
    }
    while (exp_pos > 0) {
    StrPushBack(str, exp_buf[--exp_pos]);
    }
    }
    // Format integer part
    if (int_part == 0) {
    StrPushBack(str, '0');
    } else {
    char   int_buf[32];
    }
    while (int_pos > 0) {
    StrPushBack(str, int_buf[--int_pos]);
    }
    }
    // Format fractional part
    if (config->precision > 0) {
    StrPushBack(str, '.');
    
    // Apply rounding to the entire fractional part first
    }
    
    StrPushBack(str, '0' + digit);
    frac_part -= digit;
    }
    Str path = StrInit();
    StrMerge(&path, &current_path);
    StrPushBack(&path, '/');
    StrMerge(&path, &dir_entry.name);
    Str path = StrInit();
    StrMerge(&path, &current_path);
    StrPushBack(&path, '/');
    StrMerge(&path, &dir_entry.name);
    char c = output->data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&output_clean, c);
    }
    }
    char c = expected_str.data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&expected_clean, c);
    }
    }
    char c = output->data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&output_clean, c);
    }
    }
    char c = expected_str.data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&expected_clean, c);
    }
    }
    char c = output->data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&output_clean, c);
    }
    }
    char c = expected_str.data[i];
    if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
    StrPushBack(&expected_clean, c);
    }
    }
    
    // Check that it behaves like a Vec of chars
    StrPushBack(&s, 'H');
    StrPushBack(&s, 'e');
    StrPushBack(&s, 'l');
    // Check that it behaves like a Vec of chars
    StrPushBack(&s, 'H');
    StrPushBack(&s, 'e');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'H');
    StrPushBack(&s, 'e');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'e');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'o');
    
    bool result = (s.length == 5 && ZstrCompare(s.data, "Hello") == 0);
    // Test StrPushBack function
    bool test_str_push_back(void) {
    printf("Testing StrPushBack\n");
    
    Str s = StrInitFromZstr("Hello");
    
    // Push characters at the back
    StrPushBack(&s, ' ');
    StrPushBack(&s, 'W');
    StrPushBack(&s, 'o');
    // Push characters at the back
    StrPushBack(&s, ' ');
    StrPushBack(&s, 'W');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'r');
    StrPushBack(&s, ' ');
    StrPushBack(&s, 'W');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'r');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'W');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'r');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'd');
    StrPushBack(&s, 'o');
    StrPushBack(&s, 'r');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'd');
    StrPushBack(&s, 'r');
    StrPushBack(&s, 'l');
    StrPushBack(&s, 'd');
    
    // Check that the characters were inserted correctly
    StrForeach(&s, chr, {
    // Append the character to the result string
    StrPushBack(&result, chr);
    });
    StrForeachReverse(&s, chr, {
    // Append the character to the result string
    StrPushBack(&result, chr);
    char_count++;
    });
    StrForeachPtr(&s, chrptr, {
    // Append the character (via pointer) to the result string
    StrPushBack(&result, *chrptr);
    
    // Modify the original string by converting to uppercase
    StrForeachPtrReverse(&s, chrptr, {
    // Append the character (via pointer) to the result string
    StrPushBack(&result, *chrptr);
    
    // Modify the original string by converting to uppercase
    StrForeachInRangeIdx(&s, chr, idx, 3, 3, {
    // This block should not execute
    StrPushBack(&empty_result, chr);
    });
    StrForeachInRange(&s, chr, 0, 5, {
    // Append the character to the result string
    StrPushBack(&result, chr);
    });
    StrForeachInRange(&s, chr, 6, 11, {
    // Append the character to the result string
    StrPushBack(&end_result, chr);
    });
    StrForeachPtrInRange(&s, chrptr, 0, 5, {
    // Append the character to the result string
    StrPushBack(&result, *chrptr);
    
    // Modify the original string by converting to uppercase

Share :