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

1424 {
1425 if (string.s == nullptr)
1426 return string;
1427 free(string.s);
1428 string.l = 0;
1429 string.s = nullptr;
1430 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 1455 of file futil.c.

1456 {
1457 if (dest == nullptr || src == nullptr || src->s == nullptr)
1458 return 0;
1459 size_t new_len = strlen(dest->s) + strlen(src->s) + 1;
1460 if (dest->l < new_len) {
1461 dest->s = (char *)realloc(dest->s, new_len);
1462 dest->l = new_len;
1463 }
1464 strcat(dest->s, src->s);
1465 return new_len;
size_t l
Definition cm.h:655
char * s
Definition cm.h:654

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

1439 {
1440 if (dest == nullptr || src == nullptr || src->s == nullptr)
1441 return 0;
1442 if (dest->l < src->l) {
1443 dest->s = (char *)realloc(dest->s, src->l);
1444 dest->l = src->l;
1445 }
1446 strcpy(dest->s, src->s);
1447 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 1474 of file futil.c.

1475 {
1476 if (dest == nullptr || src == nullptr || src->s == nullptr)
1477 return 0;
1478 size_t dest_len = strlen(dest->s);
1479 size_t src_len = strlen(src->s);
1480 size_t cat_len = (n < src_len) ? n : src_len;
1481 size_t new_len = dest_len + cat_len + 1;
1482 if (dest->l < new_len) {
1483 dest->s = (char *)realloc(dest->s, new_len);
1484 dest->l = new_len;
1485 }
1486 strncat(dest->s, src->s, cat_len);
1487 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 1495 of file futil.c.

1496 {
1497 if (dest == nullptr || src == nullptr || src->s == nullptr)
1498 return 0;
1499 size_t src_len = strlen(src->s);
1500 size_t cpy_len = (n < src_len) ? n : src_len;
1501 size_t new_len = cpy_len + 1;
1502 if (dest->l < new_len) {
1503 dest->s = (char *)realloc(dest->s, new_len);
1504 dest->l = new_len;
1505 }
1506 strncpy(dest->s, src->s, cpy_len);
1507 dest->s[cpy_len] = '\0';
1508 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:653

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

1385 {
1386 if (s == nullptr) {
1387 String str;
1388 str.l = 0;
1389 str.s = nullptr;
1390 return str;
1391 }
1392 String str;
1393 str.l = strlen(s) + 1;
1394 str.s = (char *)malloc(str.l);
1395 strcpy(str.s, s);
1396 return str;

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