276
278
# ref_list := ref (CR ref)*
277
279
# ref := BYTES (NULL BYTES)*
279
if not reference_lists:
280
# Simple case, we only have the key and the value
281
# So we have the (key NULL NULL value LF)
282
key_len = PyString_Size(string_key)
283
value = PyString_AsString(node[2])
284
value_len = PyString_Size(node[2])
285
flat_len = (key_len + 1 + 1 + value_len + 1)
286
line = PyString_FromStringAndSize(NULL, flat_len)
287
# Get a pointer to the new buffer
288
s = PyString_AsString(line)
289
memcpy(s, PyString_AsString(string_key), key_len)
291
s[key_len + 1] = c'\0'
292
memcpy(s + key_len + 2, value, value_len)
293
s[key_len + 2 + value_len] = c'\n'
283
# Figure out how many bytes it will take to store the references
284
next_len = len(node[3]) # TODO: use a Py function
286
# If there are no nodes, we don't need to do any work
287
# Otherwise we will need (len - 1) '\t' characters to separate
288
# the reference lists
289
ref_len = ref_len + (next_len - 1)
290
for ref_list in node[3]:
291
next_len = len(ref_list)
293
# We will need (len - 1) '\r' characters to separate the
295
ref_len = ref_len + (next_len - 1)
296
for reference in ref_list:
297
next_len = len(reference)
299
# We will need (len - 1) '\x00' characters to
300
# separate the reference key
301
ref_len = ref_len + (next_len - 1)
302
for ref in reference:
303
ref_len = ref_len + len(ref)
295
304
flattened_references = []
296
305
for ref_list in node[3]:
298
307
for reference in ref_list:
299
308
ref_keys.append('\x00'.join(reference))
300
309
flattened_references.append('\r'.join(ref_keys))
301
line = ("%s\x00%s\x00%s\n" % (string_key,
302
'\t'.join(flattened_references), node[2]))
310
refs = '\t'.join(flattened_references)
312
# So we have the (key NULL refs NULL value LF)
313
key_len = PyString_Size(string_key)
314
value = PyString_AsString(node[2])
315
value_len = PyString_Size(node[2])
316
flat_len = (key_len + 1 + ref_len + 1 + value_len + 1)
317
line = PyString_FromStringAndSize(NULL, flat_len)
318
# Get a pointer to the new buffer
319
out = PyString_AsString(line)
320
memcpy(out, PyString_AsString(string_key), key_len)
325
memcpy(out, PyString_AsString(refs), ref_len)
329
memcpy(out, value, value_len)
330
out = out + value_len
303
332
return string_key, line