dynamiC  0.1
Macros | Functions
dynamic_string.c File Reference

Definition of C string manipulation functions. More...

#include "dynamic_string.h"

Go to the source code of this file.

Macros

#define FLOAT_DIGITS   100000.
 

Functions

dyn_ushort dyn_strlen (dyn_const_str str)
 Returns the length of an string. More...
 
void dyn_strcat (dyn_str destination, dyn_const_str source)
 Concatenate strings. More...
 
void dyn_strcat2 (dyn_str destination, dyn_const_str source)
 Concatenate strings, required memory is automatically allocated. More...
 
void dyn_strcpy (dyn_str destination, dyn_const_str source)
 Copy string. More...
 
dyn_ushort dyn_itoa_len (dyn_int i)
 Calculates the number of required characters for integer to string (decimal) conversion, minus increases the value by one. More...
 
void dyn_itoa (dyn_str str, dyn_int i)
 Integer to ASCII-string conversion. More...
 
dyn_ushort dyn_ftoa_len (const dyn_float f)
 Calculates the number of required characters for float to string (decimal) conversion, minus increases the value by one. More...
 
void dyn_ftoa (dyn_str str, const dyn_float f)
 Float to ASCII-string conversion (decimal). More...
 
dyn_char dyn_strcmp (dyn_const_str a, dyn_const_str b)
 Compares the string a to the string b. More...
 

Detailed Description

Definition of C string manipulation functions.

Author
André Dietrich
Date
14 December 2016

This project is released under the MIT-License.

Definition in file dynamic_string.c.

Function Documentation

◆ dyn_ftoa()

void dyn_ftoa ( dyn_str  str,
const dyn_float  f 
)

Float to ASCII-string conversion (decimal).

The length of the character-array has to be sufficient, it can be calculated previously with function dyn_ftoa_len.

See also
dyn_ftoa_len
dyn_itoa
Parameters
[out]strcharacter array with with new ASCII representation of f
[in]ffloat value to convert

Definition at line 185 of file dynamic_string.c.

Referenced by dyn_get_bool().

186 {
187  dyn_int a = (dyn_int) f;
188  dyn_int b = (dyn_int) ((f - a) * FLOAT_DIGITS);
189 
190  dyn_itoa(str, a);
191 
192  dyn_ushort len = dyn_strlen(str);
193 
194  str[len] = '.';
195  dyn_itoa(&str[len+1], b < 0 ? -b : b);
196 }
int32_t dyn_int
32bit signed integer, standard Integer type.
Definition: dynamic_types.h:49
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_str str
pointer to character-array
Definition: dynamic_types.h:61
dyn_float f
float value
Definition: dynamic_types.h:60
void dyn_itoa(dyn_str str, dyn_int i)
Integer to ASCII-string conversion.
dyn_char b
boolean value
Definition: dynamic_types.h:58

◆ dyn_ftoa_len()

dyn_ushort dyn_ftoa_len ( const dyn_float  f)

Calculates the number of required characters for float to string (decimal) conversion, minus increases the value by one.

Parameters
ffloat value to check
Returns
string length

Definition at line 162 of file dynamic_string.c.

Referenced by dyn_get_bool().

163 {
164  dyn_ushort len = 1;
165 
166  dyn_int a = (dyn_int) f;
167  dyn_int b = (dyn_int) ((f - a) * FLOAT_DIGITS);
168 
169  len += dyn_itoa_len(a);
170  len += dyn_itoa_len(b);
171 
172  return len;
173 }
dyn_ushort dyn_itoa_len(dyn_int i)
Calculates the number of required characters for integer to string (decimal) conversion, minus increases the value by one.
int32_t dyn_int
32bit signed integer, standard Integer type.
Definition: dynamic_types.h:49
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_float f
float value
Definition: dynamic_types.h:60
dyn_char b
boolean value
Definition: dynamic_types.h:58

◆ dyn_itoa()

void dyn_itoa ( dyn_str  str,
dyn_int  i 
)

Integer to ASCII-string conversion.

The length of the character array has to have a sufficient length, it can be calculated previously with function dyn_itoa_len "(" str ")"

See also
dyn_ftoa
Parameters
[out]strcharacter array with ASCII representation of i
[in]iinteger value to convert

Definition at line 140 of file dynamic_string.c.

Referenced by dyn_get_bool().

141 {
142  char const digit[] = "0123456789";
143 
144  if (i<0) {
145  *str++ = '-';
146  i *= -1;
147  }
148 
149  str += dyn_itoa_len(i);
150 
151  *str = '\0';
152  do {
153  *--str = digit[i%10];
154  i /= 10;
155  } while(i);
156 }
dyn_ushort dyn_itoa_len(dyn_int i)
Calculates the number of required characters for integer to string (decimal) conversion, minus increases the value by one.
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_str str
pointer to character-array
Definition: dynamic_types.h:61

◆ dyn_itoa_len()

dyn_ushort dyn_itoa_len ( dyn_int  i)

Calculates the number of required characters for integer to string (decimal) conversion, minus increases the value by one.

Examples:

dyn_itoa_len(0) == 1
dyn_itoa_len(1) == 1
dyn_itoa_len(999) == 3
dyn_itoa_len(-999) == 4
Parameters
iinteger value to convert
Returns
length of decimal string

Definition at line 114 of file dynamic_string.c.

Referenced by dyn_get_bool(), and dyn_itoa().

115 {
116  if (!i) return 1;
117 
118  dyn_ushort len = 0;
119 
120  if (i < 0) {
121  i *= -1;
122  ++len;
123  }
124 
125  while (i)
126  i/=10, ++len;
127 
128  return len;
129 }
dyn_int i
basic integer
Definition: dynamic_types.h:59
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43

◆ dyn_strcat()

void dyn_strcat ( dyn_str  destination,
dyn_const_str  source 
)

Concatenate strings.

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

destination and source shall not overlap and the length of destination must be sufficient for concatenation, otherwise use dyn_strcat2.

See also
dyn_strcat2
Parameters
destinationPointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
sourceC string to be appended. This should not overlap destination.

Definition at line 54 of file dynamic_string.c.

Referenced by dyn_dict_string_add(), dyn_get_bool(), dyn_list_string_add(), and dyn_strcat2().

55 {
56  dyn_strcpy(&destination[dyn_strlen(destination)], source);
57 }
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
void dyn_strcpy(dyn_str destination, dyn_const_str source)
Copy string.

◆ dyn_strcat2()

void dyn_strcat2 ( dyn_str  destination,
dyn_const_str  source 
)

Concatenate strings, required memory is automatically allocated.

Appends a copy of the source string to the destination string and adds automatically required memory. The terminating null character in destination

  • is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

    destination and source shall not overlap.

    See also
    dyn_strcat
    Parameters
    destinationPointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
    sourceC string to be appended. This should not overlap destination.

Definition at line 76 of file dynamic_string.c.

77 {
78  destination = (dyn_str) realloc(destination, dyn_strlen(destination)+dyn_strlen(source)+1);
79  dyn_strcat(destination, source);
80 }
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
void dyn_strcat(dyn_str destination, dyn_const_str source)
Concatenate strings.
char * dyn_str
Standard dynamic C string.
Definition: dynamic_types.h:36

◆ dyn_strcmp()

dyn_char dyn_strcmp ( dyn_const_str  a,
dyn_const_str  b 
)

Compares the string a to the string b.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating '\0' is reached.

Parameters
achar array to be compared
bchar array to be compared
Return values
<0the first character that does not match has a lower value in a than in b
0the contents of both strings are equal
>0the first character that does not match has a greater value in a than in b

Definition at line 212 of file dynamic_string.c.

Referenced by dyn_dict_has_key().

213 {
214  while (*a == *b++) {
215  if (*a++ == 0)
216  return 0;
217  }
218  return (*a - *(b - 1));
219 }
dyn_char b
boolean value
Definition: dynamic_types.h:58

◆ dyn_strcpy()

void dyn_strcpy ( dyn_str  destination,
dyn_const_str  source 
)

Copy string.

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

Parameters
[out]destinationPointer to the destination array where the content is to be copied.
[in]sourceC string to be copied.

Definition at line 94 of file dynamic_string.c.

Referenced by dyn_set_string(), and dyn_strcat().

95 {
96  while(*source)
97  *destination++=*source++;
98 
99  *destination = '\0';
100 }

◆ dyn_strlen()

dyn_ushort dyn_strlen ( dyn_const_str  str)

Returns the length of an string.

Iteratates through an character array to sum up its length, the end is defined by the character '\0'.

Parameters
strPointer to an character array
Returns
string length

Definition at line 27 of file dynamic_string.c.

Referenced by dyn_dict_string_add(), dyn_dict_string_len(), dyn_get_bool(), dyn_list_string_add(), dyn_set_string(), dyn_size(), dyn_strcat(), and dyn_strcat2().

28 {
29  dyn_ushort len = 0;
30 
31  while (*str++)
32  ++len;
33 
34  return len;
35 }
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_str str
pointer to character-array
Definition: dynamic_types.h:61