O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
to_string.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
14/* ************************************************************************** */
15
16#include "o2s/to_string.h"
17
18#include "o2s/preprocessing.h"
19
20#include <ctype.h> // isprint
21#include <inttypes.h> // PRIi32
22#include <iso646.h> // not
23#include <limits.h> // INT_MAX
24#include <stdio.h> // snprintf
25#include <string.h> // strlen
26
28string_t float_to_string(const float* value)
29{
30 string_t result = string_new();
31
32 if (not string_reserve(&result, 20))
33 return result;
34 const int size = snprintf(result.start, result.capacity, "%g", *value);
35 if (size > 0)
36 result.count = (unsigned)size;
37 return result;
38}
39
41string_t double_to_string(const double* value)
42{
43 string_t result = string_new();
44
45 if (not string_reserve(&result, 20))
46 return result;
47 const int size = snprintf(result.start, result.capacity, "%lg", *value);
48 if (size > 0)
49 result.count = (unsigned)size;
50 return result;
51}
52
54string_t int_to_string(const int* value)
55{
56 string_t result = string_new();
57 const size_t maxsize = strlen(LIBO2S_PREPRO_STRING(INT_MIN)) + 1;
58
59 if (not string_reserve(&result, maxsize))
60 return result;
61 const int size = snprintf(result.start, maxsize, "%i", *value);
62 if (size > 0)
63 result.count = (unsigned)size;
64 return result;
65}
66
68string_t short_to_string(const short* value)
69{
70 string_t result = string_new();
71 const size_t maxsize = strlen(LIBO2S_PREPRO_STRING(SHORT_MIN)) + 1;
72
73 if (not string_reserve(&result, maxsize))
74 return result;
75 const int size = snprintf(result.start, maxsize, "%hi", *value);
76 if (size > 0)
77 result.count = (unsigned)size;
78 return result;
79}
80
82string_t long_to_string(const long* value)
83{
84 string_t result = string_new();
85 const size_t maxsize = strlen(LIBO2S_PREPRO_STRING(LONG_MIN)) + 1;
86
87 if (not string_reserve(&result, maxsize))
88 return result;
89 const int size = snprintf(result.start, maxsize, "%li", *value);
90 if (size > 0)
91 result.count = (unsigned)size;
92 return result;
93}
94
96string_t unsigned_to_string(const unsigned* value)
97{
98 string_t result = string_new();
99 const size_t maxsize = strlen(LIBO2S_PREPRO_STRING(UNSIGNED_MAX)) + 1;
100
101 if (not string_reserve(&result, maxsize))
102 return result;
103 const int size = snprintf(result.start, maxsize, "%u", *value);
104 if (size > 0)
105 result.count = (unsigned)size;
106 return result;
107}
108
114string_t cstring_to_string(const char* const* value)
115{
116 if (*value == NULL)
117 return string_from_literal("(null)");
118 return string_from(*value, strlen(*value));
119}
120
127{
128 string_t result = string_from_literal("'");
129 uint8_t value = *c;
130
131 if (isprint(value))
132 string_append_char(&result, value);
133 else
134 {
135 string_append_char(&result, '\\');
136 switch (value)
137 {
138 case 0:
139 string_append_char(&result, '0');
140 break;
141 case '\t':
142 string_append_char(&result, 't');
143 break;
144 case '\n':
145 string_append_char(&result, 'n');
146 break;
147 case '\r':
148 string_append_char(&result, 'r');
149 break;
150 default:
151 string_append_char(&result, 'x');
152 if (value >= 16)
153 string_append_char(&result, "0123456789abcdef"[value >> 4]);
154 string_append_char(&result, "0123456789abcdef"[value & 0xf]);
155 }
156 }
157 string_append_char(&result, '\'');
158 return result;
159}
160
167const char* boolean_to_cstring(bool value)
168{
169 return value ? "true" : "false";
170}
Compile-time utilities.
#define LIBO2S_PREPRO_STRING(EXPRESSION)
Create a string literal from the evaluation of its argument.
Dynamic string implementation.
string_t string_new(void)
Constructs an empty string.
#define string_from_literal(StringLiteral)
Allocates a string, copying the content from a C string.
Definition string.h:45
array_t string_t
A string is an array of char.
Definition string.h:30
void * start
Underlying storage.
Definition array.h:25
size_t count
Number of elements currently stored.
Definition array.h:27
size_t capacity
Number of elements that can fit in the storage.
Definition array.h:28
string_t int_to_string(const int *value)
Create a string from an integer.
Definition to_string.c:54
string_t float_to_string(const float *value)
Create a sring from a float.
Definition to_string.c:28
const char * boolean_to_cstring(bool value)
Null-temrminated string from a boolean.
Definition to_string.c:167
string_t double_to_string(const double *value)
Create a string from a double.
Definition to_string.c:41
string_t char_to_string(const char *c)
Creates a string to represent the char pointed to by c.
Definition to_string.c:126
string_t cstring_to_string(const char *const *value)
Create a string from a null-terminated character string.
Definition to_string.c:114
string_t short_to_string(const short *value)
Create a string from a short integer.
Definition to_string.c:68
string_t unsigned_to_string(const unsigned *value)
Create a string from an unsigned integer.
Definition to_string.c:96
string_t long_to_string(const long *value)
Create a string from a long integer.
Definition to_string.c:82
Create strings from data structures.