-
Notifications
You must be signed in to change notification settings - Fork 1
/
mstring.h
161 lines (129 loc) · 4.84 KB
/
mstring.h
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
/**
* Copyright (C) 2023 Amrit Bhogal
*
* This file is part of ManagedC.
*
* ManagedC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ManagedC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ManagedC. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(MANAGEDC_MSTRING)
#define MANAGEDC_MSTRING
#include <string.h>
#include "managed.h"
#if !defined(MSTRING_CHAR_T) && !defined(mc_char_t)
#define MSTRING_CHAR_T char
typedef MSTRING_CHAR_T mc_char_t;
#endif
typedef mc_char_t mstring_t;
#define _mcinternal_ptrinfo(ptr) ((struct managed_PointerInfo *)managed_info_of(ptr))
#if defined(MC_MUTEX)
# define _mcinternal_lock(ptr) (MC_MUTEX_LOCK(&_mcinternal_ptrinfo(ptr)->lock))
# define _mcinternal_unlock(ptr) (MC_MUTEX_UNLOCK(&_mcinternal_ptrinfo(ptr)->lock))
#else
# define _mcinternal_lock(ptr)
# define _mcinternal_unlock(ptr)
#endif
#define mstrlen(str) mc_countof(str)
/**
* Creates metadata for the str pointer, does not copy any bytes
*/
static mstring_t *mc_nullable managed_string(const mc_char_t *mc_nonnull str, size_t len)
{
mstring_t *s = managed_allocate(len + 1, sizeof(mc_char_t), NULL, NULL);
if (s == NULL) return NULL;
_mcinternal_lock(s);
MC_MEMCPY(s, str, len);
s[len] = '\0';
_mcinternal_ptrinfo(s)->count--; /* Don't count the null terminator */
_mcinternal_unlock(s);
return s;
}
static mstring_t *mc_nullable mstr(const mc_char_t *mc_nonnull str)
{ return managed_string(str, strlen(str)); }
/**
* Duplicates the managed string str
*/
static mstring_t *mc_nullable managed_string_duplicate(const mstring_t *mc_nonnull str)
{
long int length = mstrlen(str);
if (length == -1) return NULL;
return managed_string(str, (size_t)length);
}
static mstring_t *mc_nullable mstrdup(const mstring_t *mc_nonnull str)
{ return managed_string_duplicate(str); }
#define mstrcat_unsafe(str1, str2) managed_string_concatenate(str1, str2, mstrlen(str2) < 0 ? strlen(str2) : mstrlen(str2))
#define mstrcat(str1, str2) managed_string_concatenate(str1, str2, strlen(str2))
#define mstrncat(str1, str2, len) managed_string_concatenate(str1, str2, len)
#define mstrmcat(str1, str2) managed_string_concatenate(str1, mstrlen(s2))
/**
* Allocates a new string of needed size, and concatenates the 2 strings into it
*/
static mstring_t *mc_nullable managed_string_concatenate(mstring_t *mc_nonnull s1, const mc_char_t *mc_nonnull s2, size_t s2len)
{
mstring_t *s = NULL;
long int s1len = mstrlen(s1), total;
if (s1len < 1) return NULL;
total = (long int)s1len + (long int)s2len;
s = managed_allocate((size_t)total + 1, sizeof(mc_char_t), NULL, NULL);
if (s == NULL) return NULL;
_mcinternal_lock(s);
_mcinternal_lock(s1);
MC_MEMCPY(s, s1, (size_t)s1len); /* It cannot be a param for data because that assumes that sizeof(data) == count * typesize */
MC_MEMCPY((mstring_t *)(s + s1len), s2, s2len);
((mstring_t *)s)[total] = '\0';
_mcinternal_ptrinfo(s)->count--; /* Don't count the null terminator */
_mcinternal_unlock(s1);
_mcinternal_unlock(s);
return s;
}
/**
* Compares each character of both strings, returning 0 if not equal, and 1 if equal
*/
static int managed_string_equals(mstring_t *mc_nonnull s1, const mc_char_t *mc_nonnull s2, size_t s2len)
{
long int s1len = mstrlen(s1), i = 0, managed = 1;
if (s1len == -1) {
s1len = (long int)strlen(s1);
managed = 0;
} else _mcinternal_lock(s1);
if (s1len != (long int)s2len) return 0;
for (i = 0; i < s1len; i++)
if (((mc_char_t *)s1)[i] != ((const mc_char_t *)s2)[i]) return 0;
if (managed)
_mcinternal_unlock(s1);
return 1;
}
static int mstreq_unsafe(mstring_t *mc_nonnull s1, const mc_char_t *mc_nonnull s2)
{
long int s1len = mstrlen(s1), s2len = mstrlen(s2), i = 0, s1_managed = 1, s2_managed = 1;
if (s1len == -1) {
s1len = (long int)strlen(s1);
s1_managed = 0;
} else _mcinternal_lock(s1);
if (s2len == -1) {
s2len = (long int)strlen(s2);
s2_managed = 0;
} else _mcinternal_lock(s2);
if (s1len != s2len) return 0;
for (i = 0; i < s1len; i++)
if (((mc_char_t *)s1)[i] != ((const mc_char_t *)s2)[i]) return 0;
/*INTENTIONAL INDENTATION*/
if (s1_managed) _mcinternal_unlock(s1);
if (s2_managed) _mcinternal_unlock(s2);
return 1;
}
static int mstreq(mstring_t *mc_nonnull s1, const mc_char_t *mc_nonnull s2)
{ return managed_string_equals(s1, s2, strlen(s2)); }
static int mstrneq(mstring_t *mc_nonnull s1, const mc_char_t *mc_nonnull s2, size_t len)
{ return managed_string_equals(s1, s2, len); }
#endif