aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
blob: ef303a9e980eb68e01af7e0a1075affaf4083a84 (plain)
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
#ifndef INCLUDED_UTILS_H
#define INCLUDED_UTILS_H

#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#define ASSERT assert
#define REALLOC realloc
#define FREE free

#define ERROR(FMT, ...) fprintf (stderr, "ERROR:   "FMT"\n", ##__VA_ARGS__)
#define INFO(FMT, ...) fprintf (stdout, "INFO:    "FMT"\n", ##__VA_ARGS__)
#ifdef VERBOSE
#define LOG(FMT, ...) fprintf (stderr, "INFO:    "FMT"\n", ##__VA_ARGS__)
#else
#define LOG(...)
#endif

#define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP)

#define MIN(A,B)                                \
  ({                                            \
    typeof (A) _a = (A);                        \
    typeof (B) _b = (B);                        \
    _a < _b ? _a : _b;                          \
  })

#define ARRAY_LEN(array) (sizeof (array)/sizeof (*array))
#define ARRAY_GET(array, index) \
    (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index])

#ifndef TEMP_CAPACITY
#define TEMP_CAPACITY (16*1024*1024)
#endif

static size_t temp_size = 0;
static char temp[TEMP_CAPACITY] = { 0 };

static inline void *
talloc (size_t size)
{
  ASSERT(temp_size + size <= TEMP_CAPACITY);
  void *result = &temp[temp_size];
  temp_size += size;
  return result;
}

static inline char *
tstrdup (const char *cstr)
{
  size_t n = strlen (cstr);
  char *result = talloc (n + 1);
  memcpy (result, cstr, n);
  result[n] = '\0';
  return result;
}

static inline char *
tprintf(const char *format, ...)
{
  va_list args;

  va_start (args, format);
  int n = vsnprintf (NULL, 0, format, args);
  va_end (args);
  ASSERT(n >= 0);

  char *result = talloc (n + 1);
  va_start (args, format);
  vsnprintf (result, n + 1, format, args);
  va_end (args);

  return result;
}

static inline void
temp_reset(void) { temp_size = 0; }

static inline size_t
temp_save(void) { return temp_size; }

static inline void
temp_rewind(size_t checkpoint) { temp_size = checkpoint; }

static inline int
rand_from_to (int min, int max) { return min + rand () % (max + 1 - min); }

static inline size_t
display_width (const char *mbs)
{
  size_t width = 0;
  while (*mbs) {
    width += 1;
    mbs += 1 + ((*mbs & 1 << 7) >> 7);
  } return width;
}

static inline size_t
count_mb (const char *mbs)
{
  size_t count = 0;
  while (*mbs) {
    char mb = (*mbs & 1 << 7) >> 7;
    count += mb;
    mbs += 1 + mb;
  } return count;
}

static inline void pause (void) { printf ("Нажмите <RET> чтобы продолжить..."); while (getchar () != '\n'); }

// NOTE(cmmm): not portable, size_t is expected to be 8 bytes long
static inline int
log10sz (size_t sz)
{
  return ((sz >= 10000000000000000000lu) ? 19 :
          (sz >=  1000000000000000000lu) ? 18 :
          (sz >=   100000000000000000lu) ? 17 :
          (sz >=    10000000000000000lu) ? 16 :
          (sz >=     1000000000000000lu) ? 15 :
          (sz >=      100000000000000lu) ? 14 :
          (sz >=       10000000000000lu) ? 13 :
          (sz >=        1000000000000lu) ? 12 :
          (sz >=         100000000000lu) ? 11 :
          (sz >=           1000000000lu) ?  9 :
          (sz >=            100000000lu) ?  8 :
          (sz >=             10000000lu) ?  7 :
          (sz >=              1000000lu) ?  6 :
          (sz >=               100000lu) ?  5 :
          (sz >=                10000lu) ?  4 :
          (sz >=                 1000lu) ?  3 :
          (sz >=                  100lu) ?  2 :
          (sz >=                   10lu) ?  1 : 0u);
}

extern int levenshtein_distance (const char *a, const char *b);

#endif