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

Simple String Object Library. More...

Functions

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.
String free_string (String string)
 Free the dynamically allocated String.
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_cat (String *dest, const String *src)
 Concatenates 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

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
Note
Frees the dynamically allocated string and sets length to 0.

Definition at line 1415 of file futil.c.

1416 {
1417 if (string.s == nullptr)
1418 return string;
1419 free(string.s);
1420 string.l = 0;
1421 string.s = nullptr;
1422 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 1447 of file futil.c.

1448 {
1449 if (dest == nullptr || src == nullptr || src->s == nullptr)
1450 return 0;
1451 size_t new_len = strlen(dest->s) + strlen(src->s) + 1;
1452 if (dest->l < new_len) {
1453 dest->s = (char *)realloc(dest->s, new_len);
1454 dest->l = new_len;
1455 }
1456 strcat(dest->s, src->s);
1457 return new_len;
size_t l
Definition cm.h:586
char * s
Definition cm.h:585

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

1431 {
1432 if (dest == nullptr || src == nullptr || src->s == nullptr)
1433 return 0;
1434 if (dest->l < src->l) {
1435 dest->s = (char *)realloc(dest->s, src->l);
1436 dest->l = src->l;
1437 }
1438 strcpy(dest->s, src->s);
1439 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 1466 of file futil.c.

1467 {
1468 if (dest == nullptr || src == nullptr || src->s == nullptr)
1469 return 0;
1470 size_t dest_len = strlen(dest->s);
1471 size_t src_len = strlen(src->s);
1472 size_t cat_len = (n < src_len) ? n : src_len;
1473 size_t new_len = dest_len + cat_len + 1;
1474 if (dest->l < new_len) {
1475 dest->s = (char *)realloc(dest->s, new_len);
1476 dest->l = new_len;
1477 }
1478 strncat(dest->s, src->s, cat_len);
1479 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 1487 of file futil.c.

1488 {
1489 if (dest == nullptr || src == nullptr || src->s == nullptr)
1490 return 0;
1491 size_t src_len = strlen(src->s);
1492 size_t cpy_len = (n < src_len) ? n : src_len;
1493 size_t new_len = cpy_len + 1;
1494 if (dest->l < new_len) {
1495 dest->s = (char *)realloc(dest->s, new_len);
1496 dest->l = new_len;
1497 }
1498 strncpy(dest->s, src->s, cpy_len);
1499 dest->s[cpy_len] = '\0';
1500 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.

Note
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:584
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.
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.
See also
snippets/strings_test1.c

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

1376 {
1377 if (s == nullptr) {
1378 String str;
1379 str.l = 0;
1380 str.s = nullptr;
1381 return str;
1382 }
1383 String str;
1384 str.l = strlen(s) + 1;
1385 str.s = (char *)malloc(str.l);
1386 strcpy(str.s, s);
1387 return str;

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