File: strlib.c

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (395 lines) | stat: -rw-r--r-- 9,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include <ctype.h>
#ifdef _MSC_VER
typedef __int64 int64_t;
#else
#include <inttypes.h>
#include <stdint.h>
#endif

#if !defined(VMS) && !defined(_WIN32)
#include <unistd.h>
#endif

#include "strlib.h"

#ifndef DBL_EPSILON
#define DBL_EPSILON 2.2204460492503131e-16
#endif

/*
 * Maximum number of decimal digits: 15
 * Maximum length of the exponent: 4
 * Mathtex symbols: 11 \times10^{}
 * Negative signs: 2
 */
#define STR_MAX 32

/*
 * maximum number of digits to be used in standard notation
 */
#if !defined(LENGTH_DIGITS)
#define LENGTH_DIGITS 14
#endif

#if !defined(SCIENTIFIC_EXP_LOW)
#define SCIENTIFIC_EXP_LOW -14
#endif

#if !defined(SCIENTIFIC_EXP_HIGH)
#define SCIENTIFIC_EXP_HIGH 14
#endif

#ifndef isnan
#define isnan(x) ((x) != (x))
#endif
#ifndef isinf
#define isinf(x) (!isnan(x) && isnan((x) - (x)))
#endif
#ifndef round
#define round(x) (((x) < 0) ? ceil((x)-.5) : floor((x) + .5))
#endif

str_format_reference_t *str_get_format_reference(str_format_reference_t *result, double origin, double min, double max,
                                                 double tick_width, int major)

/*
 * str_get_reference - Checks for each value between min and max (step-size: tick_width) if there is one value which
 *                      needs scientific notation. Also counts the maximum number of decimal places.
 *                      Both values are stored in the result reference
 */

{
  int i, steps;
  int64_t exponent;

  result->decimal_digits = 0;
  result->scientific = 0;

  if (major != 0)
    {
      tick_width = major * tick_width;
    }

  steps = round((max - min) / tick_width);

  /* Check if there is one value between min and max which needs scientific notation */
  for (i = 0; i <= steps; i++)
    {
      double current = min + i * tick_width;
      if (current != origin || origin == min || origin == max)
        {
          if (fabs(current) > DBL_EPSILON)
            {
              exponent = (int64_t)floor(log10(fabs(current)));
              if (exponent <= SCIENTIFIC_EXP_LOW || exponent >= SCIENTIFIC_EXP_HIGH)
                {
                  result->scientific = 1;
                  break;
                }
            }
        }
    }
  /* Count number of decimal digits */
  if (!result->scientific)
    {
      double tick_width_multiplied = tick_width;
      while ((int64_t)tick_width_multiplied < tick_width_multiplied &&
             log10(tick_width_multiplied - (int64_t)tick_width_multiplied) >= result->decimal_digits - LENGTH_DIGITS &&
             result->decimal_digits < LENGTH_DIGITS)
        {
          result->decimal_digits++;
          tick_width_multiplied = (tick_width + 1e-15) * pow(10.0, result->decimal_digits);
        }
    }
  return result;
}

char *str_remove(char *str, char ch)

/*
 *  str_remove - remove trailing characters
 */

{
  int i;

  i = strlen(str) - 1;
  while (i >= 0 && str[i] == ch) str[i--] = '\0';

  return str;
}


char *str_pad(char *str, char fill, int size)

/*
 *  str_pad - pad string with fill character
 */

{
  int i, len;

  len = strlen(str);
  for (i = len; i < size; i++) str[i] = fill;

  if (size < 0) size = 0;
  str[size] = '\0';

  return str;
}


static char *str_write_decimal(char *result, int64_t mantissa, int decimal_point, int decimal_digits)

/*
 * str_write_decimal - Writes the decimal number (mantissa split by the decimal point) into result.
 */

{
  int i;
  char *end = NULL;

  /* skip non-significant digits */
  for (i = 0; i < decimal_point; i++)
    {
      if (mantissa % 10 != 0 || decimal_point - i <= decimal_digits)
        {
          end = &result[LENGTH_DIGITS - i + 1];
          break;
        }
      mantissa /= 10;
    }

  /* write or skip decimal point */
  if (end)
    {
      result[LENGTH_DIGITS - decimal_point] = '.';
    }
  else
    {
      end = &result[LENGTH_DIGITS - decimal_point];
    }

  /* write significant digits */
  for (; i < LENGTH_DIGITS + 1; i++)
    {
      if (i == decimal_point)
        {
          /* decimal point was handled already */
          continue;
        }
      result[LENGTH_DIGITS - i] = '0' + mantissa % 10;
      mantissa /= 10;
    }

  /* end string with null byte */
  end[0] = '\0';

  return end;
}


char *str_ftoa(char *result, double value, str_format_reference_t *reference, int format_option)

/*
 * str_ftoa - Convert a real value to string in scientific notation. The reference is used to specify the number of
 *            decimal places and the usage of scientific notation.
 */

{
  char *mantissa_str = result;
  double abs_value = fabs(value);

  int64_t exponent, decimal_places, mantissa;

  str_format_reference_t local_reference;

  if (value == 0)
    {
      result[0] = '0';
      result[1] = '\0';
    }
  else if (isnan(value))
    {
      result[0] = 'n';
      result[1] = 'a';
      result[2] = 'n';
      result[3] = '\0';
    }
  else if (isinf(abs_value))
    {
      char *inf_str = result;
      if (value < 0)
        {
          result[0] = '-';
          inf_str = &result[1];
        }
      inf_str[0] = 'i';
      inf_str[1] = 'n';
      inf_str[2] = 'f';
      inf_str[3] = '\0';
    }
  else
    {

      exponent = (int64_t)(floor(log10(abs_value)));
      decimal_places = (LENGTH_DIGITS - 1) - exponent;

      /* When reference is null check wether scientific notation is needed or not */
      if (!reference)
        {
          local_reference.scientific = (exponent <= SCIENTIFIC_EXP_LOW || exponent >= SCIENTIFIC_EXP_HIGH);
          local_reference.decimal_digits = 0;
          reference = &local_reference;
        }

      if (exponent < 0 && !reference->scientific)
        {
          mantissa = (int64_t)(round(abs_value * pow(10.0, LENGTH_DIGITS - 1)));
        }
      else
        {
          mantissa = (int64_t)(round(abs_value * pow(10.0, decimal_places)));
        }

      if (value < 0)
        {
          result[0] = '-';
          mantissa_str = &result[1];
        }

      if (reference->scientific)
        {
          char *exponent_str = NULL;
          int64_t exponent_abs = exponent;
          int exponent_length;
          int i = 0;

          exponent_str = str_write_decimal(mantissa_str, mantissa, LENGTH_DIGITS - 1, reference->decimal_digits);

          if (format_option == SCIENTIFIC_FORMAT_OPTION_E)
            {
              exponent_str[0] = 'E';
            }
          else
            {
              if (format_option == SCIENTIFIC_FORMAT_OPTION_TEXTEX)
                {
                  exponent_str[0] = 0xC3;
                  exponent_str[1] = 0x97;
                  exponent_str = &exponent_str[2];
                }
              else if (format_option == SCIENTIFIC_FORMAT_OPTION_MATHTEX)
                {
                  exponent_str[0] = '\\';
                  exponent_str[1] = 't';
                  exponent_str[2] = 'i';
                  exponent_str[3] = 'm';
                  exponent_str[4] = 'e';
                  exponent_str[5] = 's';
                  exponent_str = &exponent_str[6];
                }
              exponent_str[0] = '1';
              exponent_str[1] = '0';
              exponent_str[2] = '^';
              exponent_str[3] = '{';
              exponent_str = &exponent_str[3];
            }

          if (exponent < 0)
            {
              exponent_abs *= -1;
              exponent_str[1] = '-';
              exponent_str = &exponent_str[1];
            }

          if (exponent_abs == 0)
            {
              exponent_length = 1;
            }
          else
            {
              exponent_length = log10(exponent_abs) + 1;
            }
          for (i = exponent_length; i > 0; i--)
            {
              exponent_str[i] = ('0' + exponent_abs % 10);
              exponent_abs /= 10;
            }
          if (format_option == SCIENTIFIC_FORMAT_OPTION_TEXTEX || format_option == SCIENTIFIC_FORMAT_OPTION_MATHTEX)
            {
              exponent_str[exponent_length + 1] = '}';
              exponent_str[exponent_length + 2] = '\0';
            }
          else
            {
              exponent_str[exponent_length + 1] = '\0';
            }
        }
      else
        {
          if (exponent >= 0)
            {
              str_write_decimal(mantissa_str, mantissa, decimal_places, reference->decimal_digits);
            }
          else
            {
              str_write_decimal(mantissa_str, mantissa, LENGTH_DIGITS - 1, reference->decimal_digits);
            }
        }
    }

  return result;
}


int str_casecmp(char *s1, char *s2)
{
  while ((*s1 != '\0') && (tolower(*(unsigned char *)s1) == tolower(*(unsigned char *)s2)))
    {
      s1++;
      s2++;
    }

  return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
}


int str_utf8_to_unicode(const unsigned char *utf8_str, int *length)
{
  int codepoint;

  if ((utf8_str[0] & 0x80U) == 0x00U)
    {
      *length = 1;
      codepoint = utf8_str[0U];
    }
  else if ((utf8_str[0] & 0xe0U) == 0xc0U && (utf8_str[1] & 0xc0U) == 0x80U)
    {
      *length = 2;
      codepoint = ((utf8_str[0] & 0x1fU) << 6U) + (utf8_str[1] & 0x3fU);
    }
  else if ((utf8_str[0] & 0xf0U) == 0xe0U && (utf8_str[1] & 0xc0U) == 0x80U && (utf8_str[2] & 0xc0U) == 0x80U)
    {
      *length = 3;
      codepoint = ((utf8_str[0] & 0xfU) << 12U) + ((utf8_str[1] & 0x3fU) << 6U) + (utf8_str[2] & 0x3fU);
    }
  else if ((utf8_str[0] & 0xf8U) == 0xf0U && (utf8_str[1] & 0xc0U) == 0x80U && (utf8_str[2] & 0xc0U) == 0x80U &&
           (utf8_str[3] & 0xc0U) == 0x80U)
    {
      *length = 4;
      codepoint = ((utf8_str[0] & 0x7U) << 18U) + ((utf8_str[1] & 0x3fU) << 12U) + ((utf8_str[2] & 0x3fU) << 6U) +
                  (utf8_str[3] & 0x3fU);
    }
  else
    {
      /* invalid byte combination */
      *length = 1;
      codepoint = (unsigned int)'?';
    }
  return codepoint;
}