summaryrefslogtreecommitdiff
path: root/src/str.c
blob: fdff99a329a1b84a5d9e33dfd67f24a471557a14 (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
#include "str.h"
#include "str_partial.h"
#include <string.h>

H_ATTR
str_info
_info_getter(const str * const restrict s, UNUM n)
{
  return (str_info) { (NUM) *(s->data+n), 1 };
}

/* H_ATTR
 * NUM
 * _lenner(str *s)
 * {
 *   return s->size;
 * } */

str *
new_str(char *s, NUM size)
{
  str *str_pointer = MYALLOC(str, 1);

  str_pointer->size = size;
  str_pointer->data = s;
  str_pointer->getter = _info_getter;
  /* str_pointer->lenner = _lenner; */

  return str_pointer;
}

void
destroy_str(str *s, int flag)
{
  if (flag) free(s->data);

  s->size = 0;

  free(s);
}

H_ATTR
str_info
get_info(const str * const restrict s, UNUM n)
{
  return s->getter(s, n);
}

H_ATTR
NUM
str_length(const str * const restrict s)
{
  return s->size;
}

void
change_str_content(str *s, char *str, NUM size)
{
  s->data = str;
  s->size = size;
}

BOOL
copy_str(str *dest, str *source)
{
  if (str_length(dest) < str_length(source))
    return 1;

  dest->size = source->size;

  memcpy(dest->data, source->data, source->size);

  return 0;
}

char *
get_data(const str * const restrict s)
{
  return s->data;
}

void
str_set_length(str *s, UNUM size)
{
  s->size = size;
}