C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
String Objects

Simple String Object Library. More...

Functions

String free_string (String string)
 Free the dynamically allocated String.
size_t string_cat (String *dest, const String *src)
 Concatenates src String to dest String, allocating additional memory for dest String if necessary.
size_t string_cpy (String *dest, const String *src)
 Copy src String to dest String, allocating additional memory for dest String if necessary.
size_t string_ncat (String *dest, const String *src, size_t n)
 Concatenates up to n characters from src String to dest String, allocating additional memory for dest String if necessary.
size_t string_ncpy (String *dest, const String *src, size_t n)
 copies up to n characters from src String to dest String, allocating additional memory for dest String if necessary
String to_string (const char *s)
 String functions provide a simple string library to facilitate string manipulation in C, allowing developers to easily create, copy, concatenate, and free strings without having to manage memory manually.

Detailed Description

Simple String Object Library.

Function Documentation

◆ free_string()

String free_string ( String string)

Free the dynamically allocated String.

Parameters
stringto free
Returns
string with nullptr pointer and length 0

Frees the dynamically allocated string and sets length to 0.

Definition at line 1562 of file futil.c.

1563 {
1564 if (string.s == nullptr)
1565 return string;
1566 free(string.s);
1567 string.l = 0;
1568 string.s = nullptr;
1569 return string;

References String::l, and String::s.

◆ string_cat()

size_t string_cat ( String * dest,
const String * src )

Concatenates src String to dest String, allocating additional memory for dest String if necessary.

Parameters
dest- destination String struct
src- source String struct
Returns
new length of dest String after concatenation
Note
the caller is responsible for freeing the allocated memory.

Definition at line 1594 of file futil.c.

1595 {
1596 if (dest == nullptr || src == nullptr || src->s == nullptr)
1597 return 0;
1598 size_t new_len = strlen(dest->s) + strlen(src->s) + 1;
1599 if (dest->l < new_len) {
1600 dest->s = (char *)realloc(dest->s, new_len);
1601 dest->l = new_len;
1602 }
1603 strcat(dest->s, src->s);
1604 return new_len;
size_t l
Definition cm.h:764
char * s
Definition cm.h:763

References String::l, and String::s.

◆ string_cpy()

size_t string_cpy ( String * dest,
const String * src )

Copy src String to dest String, allocating additional memory for dest String if necessary.

Parameters
dest- destination String struct
src- source String struct
Returns
length of dest String
Note
the caller is responsible for freeing the allocated memory.

Definition at line 1577 of file futil.c.

1578 {
1579 if (dest == nullptr || src == nullptr || src->s == nullptr)
1580 return 0;
1581 if (dest->l < src->l) {
1582 dest->s = (char *)realloc(dest->s, src->l);
1583 dest->l = src->l;
1584 }
1585 strcpy(dest->s, src->s);
1586 return src->l;

References String::l, and String::s.

◆ string_ncat()

size_t string_ncat ( String * dest,
const String * src,
size_t n )

Concatenates up to n characters from src String to dest String, allocating additional memory for dest String if necessary.

Parameters
dest- destination String struct
src- source String struct
n- maximum number of characters to concatenate
Returns
new length of dest String after concatenation
Note
the caller is responsible for freeing the allocated memory.

Definition at line 1613 of file futil.c.

1614 {
1615 if (dest == nullptr || src == nullptr || src->s == nullptr)
1616 return 0;
1617 size_t dest_len = strlen(dest->s);
1618 size_t src_len = strlen(src->s);
1619 size_t cat_len = (n < src_len) ? n : src_len;
1620 size_t new_len = dest_len + cat_len + 1;
1621 if (dest->l < new_len) {
1622 dest->s = (char *)realloc(dest->s, new_len);
1623 dest->l = new_len;
1624 }
1625 strncat(dest->s, src->s, cat_len);
1626 return new_len;

References String::l, and String::s.

◆ string_ncpy()

size_t string_ncpy ( String * dest,
const String * src,
size_t n )

copies up to n characters from src String to dest String, allocating additional memory for dest String if necessary

Parameters
dest- destination String struct
src- source String struct
n- maximum number of characters to copy
Note
the caller is responsible for freeing the allocated memory.

Definition at line 1634 of file futil.c.

1635 {
1636 if (dest == nullptr || src == nullptr || src->s == nullptr)
1637 return 0;
1638 size_t src_len = strlen(src->s);
1639 size_t cpy_len = (n < src_len) ? n : src_len;
1640 size_t new_len = cpy_len + 1;
1641 if (dest->l < new_len) {
1642 dest->s = (char *)realloc(dest->s, new_len);
1643 dest->l = new_len;
1644 }
1645 strncpy(dest->s, src->s, cpy_len);
1646 dest->s[cpy_len] = '\0';
1647 return new_len;

References String::l, and String::s.

◆ to_string()

String to_string ( const char * s)

String functions provide a simple string library to facilitate string manipulation in C, allowing developers to easily create, copy, concatenate, and free strings without having to manage memory manually.

The library includes functions to convert C strings to String structs, create new String structs with specified lengths, copy and concatenate String structs, and free the memory used by String structs. By using this library, developers can avoid common pitfalls of C string handling, such as buffer overflows and memory leaks, while still benefiting from the performance advantages of C. Designed to be simple and easy to use, making it a great choice for developers who want to work with strings in C without having to worry about the complexities of manual memory management. The String struct is defined as follows:

typedef struct {
size_t l; // length of the string (including null terminator)
char *s; // pointer to the dynamically allocated string
The String structure represents a string object with a pointer to the string and its allocated length...
Definition cm.h:762

All functions in this library that return a String struct allocate memory for the string using malloc or realloc. It is the caller's responsibility to free this memory using the free_string function when it is no longer needed to avoid memory leaks.

Note
The String functions in this library do not perform bounds checking on the input strings or the resulting strings. It is the caller's responsibility to ensure that all input strings are valid and that the resulting strings do not exceed available memory.
The String functions in this library assume that all input strings are null-terminated. If any input string is not null-terminated, the behavior is undefined.

Convert C string to String struct

Parameters
sC string
Returns
String struct containing dynamically allocated copy of input string
Note
the caller is responsible for freeing the allocated memory.

Definition at line 1523 of file futil.c.

1524 {
1525 if (s == nullptr) {
1526 String str;
1527 str.l = 0;
1528 str.s = nullptr;
1529 return str;
1530 }
1531 String str;
1532 str.l = strlen(s) + 1;
1533 str.s = (char *)malloc(str.l);
1534 strcpy(str.s, s);
1535 return str;

References String::l, and String::s.