-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnv_string.c
112 lines (98 loc) · 2.31 KB
/
nv_string.c
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
#include "nv.h"
//
// Internal
//
long NV_Node_String_Internal_strtol(const NV_Node *ns, int *endptrindex, int base)
{
long v;
char *ep;
const char *str = NV_Node_getDataAsType(ns, kString);
if(!str){
if(endptrindex) *endptrindex = 0;
return 0;
}
v = strtol(str, &ep, base);
if(endptrindex) *endptrindex = ep - str;
return v;
}
//
// String
//
int NV_NodeID_isString(const NV_ID *id)
{
return (NV_Node_getType(id) == kString);
}
NV_ID NV_Node_createWithString(const char *s)
{
// string will be copied
NV_ID id = NV_ID_generateRandom();
return NV_Node_createWith_ID_String(&id, s);
}
NV_ID NV_Node_createWith_ID_String(const NV_ID *id, const char *s)
{
// string will be copied
size_t len = strlen(s);
size_t size = len + 1;
char *buf = NV_malloc(size);
NV_strncpy(buf, s, size, len);
return NV_Node_createWith_ID_Type_Data_Size(id, kString, buf, size);
}
NV_ID NV_Node_createWithStringFormat(const char *fmt, ...)
{
char *s;
int sLen;
va_list ap;
NV_ID newID;
//
sLen = strlen(fmt) * 2;
s = NV_malloc(sLen);
//
va_start(ap, fmt);
vsnprintf(s, sLen, fmt, ap);
va_end(ap);
//
newID = NV_Node_createWithString(s);
//
NV_free(s);
//
return newID;
}
const char *NV_NodeID_getCStr(const NV_ID *id)
{
return NV_NodeID_getDataAsType(id, kString);
}
int NV_Node_String_compare(const NV_ID *ida, const NV_ID *idb)
{
// compatible with strcmp
// but if node->data is null, returns -1.
// "" == "" -> true
const char *strA = NV_NodeID_getDataAsType(ida, kString);
const char *strB = NV_NodeID_getDataAsType(idb, kString);
if(!strA || !strB) return -1;
return strcmp(strA, strB);
}
int NV_Node_String_compareWithCStr(const NV_ID *ida, const char *s)
{
// compatible with strcmp
// but if node->data is null, returns -1.
// "" == "" -> true
const char *str = NV_NodeID_getDataAsType(ida, kString);
if(!str) return -1;
return strcmp(str, s);
}
char *NV_Node_String_strchr(const NV_ID *id, char c)
{
const char *str = NV_NodeID_getDataAsType(id, kString);
if(!str) return NULL;
return strchr(str, c);
}
long NV_Node_String_strtol(const NV_ID *ns, int *endptrindex, int base)
{
return NV_Node_String_Internal_strtol(NV_NodeID_getNode(ns), endptrindex, base);
}
size_t NV_Node_String_strlen(const NV_ID *id)
{
const char *str = NV_NodeID_getDataAsType(id, kString);
if(!str) return 0;
return strlen(str);
}