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

1546 {
1547 if (string.s == nullptr)
1548 return string;
1549 free(string.s);
1550 string.l = 0;
1551 string.s = nullptr;
1552 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 1577 of file futil.c.

1578 {
1579 if (dest == nullptr || src == nullptr || src->s == nullptr)
1580 return 0;
1581 size_t new_len = strlen(dest->s) + strlen(src->s) + 1;
1582 if (dest->l < new_len) {
1583 dest->s = (char *)realloc(dest->s, new_len);
1584 dest->l = new_len;
1585 }
1586 strcat(dest->s, src->s);
1587 return new_len;
size_t l
Definition cm.h:664
char * s
Definition cm.h:663

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

1561 {
1562 if (dest == nullptr || src == nullptr || src->s == nullptr)
1563 return 0;
1564 if (dest->l < src->l) {
1565 dest->s = (char *)realloc(dest->s, src->l);
1566 dest->l = src->l;
1567 }
1568 strcpy(dest->s, src->s);
1569 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 1596 of file futil.c.

1597 {
1598 if (dest == nullptr || src == nullptr || src->s == nullptr)
1599 return 0;
1600 size_t dest_len = strlen(dest->s);
1601 size_t src_len = strlen(src->s);
1602 size_t cat_len = (n < src_len) ? n : src_len;
1603 size_t new_len = dest_len + cat_len + 1;
1604 if (dest->l < new_len) {
1605 dest->s = (char *)realloc(dest->s, new_len);
1606 dest->l = new_len;
1607 }
1608 strncat(dest->s, src->s, cat_len);
1609 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 1617 of file futil.c.

1618 {
1619 if (dest == nullptr || src == nullptr || src->s == nullptr)
1620 return 0;
1621 size_t src_len = strlen(src->s);
1622 size_t cpy_len = (n < src_len) ? n : src_len;
1623 size_t new_len = cpy_len + 1;
1624 if (dest->l < new_len) {
1625 dest->s = (char *)realloc(dest->s, new_len);
1626 dest->l = new_len;
1627 }
1628 strncpy(dest->s, src->s, cpy_len);
1629 dest->s[cpy_len] = '\0';
1630 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:662

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

1507 {
1508 if (s == nullptr) {
1509 String str;
1510 str.l = 0;
1511 str.s = nullptr;
1512 return str;
1513 }
1514 String str;
1515 str.l = strlen(s) + 1;
1516 str.s = (char *)malloc(str.l);
1517 strcpy(str.s, s);
1518 return str;

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