MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / sdsnewlen

Function sdsnewlen

src/util/sds/sds.c:91–147  ·  view source on GitHub ↗

Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. * If SDS_NOINIT is used, the buffer is left uninitialized; * * The string is always null-termined (all the sds strings are, always) so * even if you create an sds string with: * * mystring = sdsnewlen("abc",3); * * You can print

Source from the content-addressed store, hash-verified

89 * end of the string. However the string is binary safe and can contain
90 * \0 characters in the middle, as the length is stored in the sds header. */
91sds sdsnewlen(const void *init, size_t initlen) {
92 void *sh;
93 sds s;
94 char type = sdsReqType(initlen);
95 /* Empty strings are usually created in order to append. Use type 8
96 * since type 5 is not good at this. */
97 if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
98 int hdrlen = sdsHdrSize(type);
99 unsigned char *fp; /* flags pointer. */
100
101 sh = s_malloc(hdrlen+initlen+1);
102 if (init==SDS_NOINIT)
103 init = NULL;
104 else if (!init)
105 memset(sh, 0, hdrlen+initlen+1);
106 if (sh == NULL) return NULL;
107 s = (char*)sh+hdrlen;
108 fp = ((unsigned char*)s)-1;
109 switch(type) {
110 case SDS_TYPE_5: {
111 *fp = type | (initlen << SDS_TYPE_BITS);
112 break;
113 }
114 case SDS_TYPE_8: {
115 SDS_HDR_VAR(8,s);
116 sh->len = initlen;
117 sh->alloc = initlen;
118 *fp = type;
119 break;
120 }
121 case SDS_TYPE_16: {
122 SDS_HDR_VAR(16,s);
123 sh->len = initlen;
124 sh->alloc = initlen;
125 *fp = type;
126 break;
127 }
128 case SDS_TYPE_32: {
129 SDS_HDR_VAR(32,s);
130 sh->len = initlen;
131 sh->alloc = initlen;
132 *fp = type;
133 break;
134 }
135 case SDS_TYPE_64: {
136 SDS_HDR_VAR(64,s);
137 sh->len = initlen;
138 sh->alloc = initlen;
139 *fp = type;
140 break;
141 }
142 }
143 if (initlen && init)
144 memcpy(s, init, initlen);
145 s[initlen] = '\0';
146 return s;
147}
148

Callers 6

sdsemptyFunction · 0.85
sdsnewFunction · 0.85
sdsdupFunction · 0.85
sdsfromlonglongFunction · 0.85
sdssplitlenFunction · 0.85
sdsTestFunction · 0.85

Calls 2

sdsReqTypeFunction · 0.85
sdsHdrSizeFunction · 0.85

Tested by

no test coverage detected