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 1569 of file futil.c.

1570 {
1571 if (string.s == nullptr)
1572 return string;
1573 free(string.s);
1574 string.l = 0;
1575 string.s = nullptr;
1576 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 1601 of file futil.c.

1602 {
1603 if (dest == nullptr || src == nullptr || src->s == nullptr)
1604 return 0;
1605 size_t new_len = strlen(dest->s) + strlen(src->s) + 1;
1606 if (dest->l < new_len) {
1607 dest->s = (char *)realloc(dest->s, new_len);
1608 dest->l = new_len;
1609 }
1610 strcat(dest->s, src->s);
1611 return new_len;
size_t l
Definition cm.h:411
char * s
Definition cm.h:410

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 1584 of file futil.c.

1585 {
1586 if (dest == nullptr || src == nullptr || src->s == nullptr)
1587 return 0;
1588 if (dest->l < src->l) {
1589 dest->s = (char *)realloc(dest->s, src->l);
1590 dest->l = src->l;
1591 }
1592 strcpy(dest->s, src->s);
1593 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 1620 of file futil.c.

1621 {
1622 if (dest == nullptr || src == nullptr || src->s == nullptr)
1623 return 0;
1624 size_t dest_len = strlen(dest->s);
1625 size_t src_len = strlen(src->s);
1626 size_t cat_len = (n < src_len) ? n : src_len;
1627 size_t new_len = dest_len + cat_len + 1;
1628 if (dest->l < new_len) {
1629 dest->s = (char *)realloc(dest->s, new_len);
1630 dest->l = new_len;
1631 }
1632 strncat(dest->s, src->s, cat_len);
1633 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 1641 of file futil.c.

1642 {
1643 if (dest == nullptr || src == nullptr || src->s == nullptr)
1644 return 0;
1645 size_t src_len = strlen(src->s);
1646 size_t cpy_len = (n < src_len) ? n : src_len;
1647 size_t new_len = cpy_len + 1;
1648 if (dest->l < new_len) {
1649 dest->s = (char *)realloc(dest->s, new_len);
1650 dest->l = new_len;
1651 }
1652 strncpy(dest->s, src->s, cpy_len);
1653 dest->s[cpy_len] = '\0';
1654 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
Definition cm.h:409

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 1530 of file futil.c.

1531 {
1532 if (s == nullptr) {
1533 String str;
1534 str.l = 0;
1535 str.s = nullptr;
1536 return str;
1537 }
1538 String str;
1539 str.l = strlen(s) + 1;
1540 str.s = (char *)malloc(str.l);
1541 strcpy(str.s, s);
1542 return str;

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