Couchbase Lite C
Couchbase Lite C API
Loading...
Searching...
No Matches
FLSlice.h
Go to the documentation of this file.
1//
2// FLSlice.h
3// Fleece
4//
5// Created by Jens Alfke on 8/13/18.
6// Copyright 2018-Present Couchbase, Inc.
7//
8// Use of this software is governed by the Business Source License included
9// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
10// in that file, in accordance with the Business Source License, use of this
11// software will be governed by the Apache License, Version 2.0, included in
12// the file licenses/APL2.txt.
13//
14
15#pragma once
16#ifndef _FLSLICE_H
17#define _FLSLICE_H
18
19#include "CompilerSupport.h"
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24
25
26#ifdef __cplusplus
27 #include <string>
28 namespace fleece { struct alloc_slice; }
29#endif
30
31
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38
41
42
45typedef struct FLSlice {
46 const void* FL_NULLABLE buf;
47 size_t size;
48
49#ifdef __cplusplus
50 constexpr const void* FL_NULLABLE data() const noexcept FLPURE {return buf;}
51 constexpr size_t size_bytes() const noexcept FLPURE {return size;} // compatibility with std::span
52 constexpr bool empty() const noexcept FLPURE {return size == 0;}
53 constexpr explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
54 explicit operator std::string() const {return std::string((char*)buf, size);}
55#endif
56} FLSlice;
57
58
67 const void* FL_NULLABLE buf;
68 size_t size;
69
70#ifdef __cplusplus
71 constexpr const void* FL_NULLABLE data() const noexcept FLPURE {return buf;}
72 constexpr size_t size_bytes() const noexcept FLPURE {return size;} // compatibility with std::span
73 constexpr bool empty() const noexcept FLPURE {return size == 0;}
74 constexpr explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
75 constexpr explicit operator FLSlice () const {return {buf, size};}
76 inline explicit operator std::string() const;
77#endif
78};
79typedef struct FLSliceResult FLSliceResult;
80
81
85#ifdef __cplusplus
86 struct FLHeapSlice : public FLSlice {
87 constexpr FLHeapSlice() noexcept :FLSlice{nullptr, 0} { }
88 private:
89 constexpr FLHeapSlice(const void *FL_NULLABLE b, size_t s) noexcept :FLSlice{b, s} { }
90 friend struct fleece::alloc_slice;
91 };
92#else
94#endif
95
96
97// Aliases used to indicate that a slice is expected to contain UTF-8 data.
100
101
103#ifdef _MSC_VER
104 static const FLSlice kFLSliceNull = { NULL, 0 };
105#else
106 #define kFLSliceNull ((FLSlice){NULL, 0})
107#endif
108
109
112inline FLPURE int FLMemCmp(const void * FL_NULLABLE a,
113 const void * FL_NULLABLE b, size_t size) FLAPI
114{
115 if (_usuallyFalse(size == 0))
116 return 0;
117 return memcmp(a, b, size);
118}
119
122inline void FLMemCpy(void* FL_NULLABLE dst, const void* FL_NULLABLE src, size_t size) FLAPI {
123 if (_usuallyTrue(size > 0))
124 memcpy(dst, src, size);
125}
126
127
132inline FLSlice FLStr(const char* FL_NULLABLE str LIFETIMEBOUND) FLAPI {
133 FLSlice foo = { str, str ? strlen(str) : 0 };
134 return foo;
135}
136
139#ifdef __cplusplus
140 #define FLSTR(STR) (FLSlice {("" STR), sizeof(("" STR))-1})
141#else
142 #define FLSTR(STR) ((FLSlice){("" STR), sizeof(("" STR))-1})
143#endif
144
145
148
152
155
163FLEECE_PUBLIC bool FLSlice_ToCString(FLSlice s, char* buffer, size_t capacity) FLAPI;
164
167
170
171
173inline FLSliceResult FLSliceResult_CreateWith(const void* FL_NULLABLE bytes, size_t size) FLAPI {
174 FLSlice s = {bytes, size};
175 return FLSlice_Copy(s);
176}
177
178
179FLEECE_PUBLIC void _FLBuf_Retain(const void* FL_NULLABLE) FLAPI; // internal; do not call
180FLEECE_PUBLIC void _FLBuf_Release(const void* FL_NULLABLE) FLAPI; // internal; do not call
181
184 _FLBuf_Retain(s.buf);
185 return s;
186}
187
190 _FLBuf_Release(s.buf);
191}
192
195 FLSlice ret;
196 memcpy(&ret, &sr, sizeof(ret));
197 return ret;
198}
199
200
204FLEECE_PUBLIC void FL_WipeMemory(void *dst, size_t size) FLAPI;
205
206
208
209#ifdef __cplusplus
210}
211
212 FLPURE inline bool operator== (FLSlice s1, FLSlice s2) {return FLSlice_Equal(s1, s2);}
213 FLPURE inline bool operator!= (FLSlice s1, FLSlice s2) {return !(s1 == s2);}
214
215 FLPURE inline bool operator== (FLSliceResult sr, FLSlice s) {return (FLSlice)sr == s;}
216 FLPURE inline bool operator!= (FLSliceResult sr, FLSlice s) {return !(sr ==s);}
217
218
219 FLSliceResult::operator std::string () const {
220 auto str = std::string((char*)buf, size);
222 return str;
223 }
224#endif
225
227#endif // _FLSLICE_H
#define FL_NULLABLE
Definition CompilerSupport.h:94
#define LIFETIMEBOUND
Definition CompilerSupport.h:293
#define FLEECE_PUBLIC
Definition CompilerSupport.h:269
#define FL_ASSUME_NONNULL_BEGIN
Definition CompilerSupport.h:92
#define _usuallyTrue(VAL)
Definition CompilerSupport.h:75
#define _usuallyFalse(VAL)
Definition CompilerSupport.h:76
#define FLPURE
Definition CompilerSupport.h:130
#define NODISCARD
Definition CompilerSupport.h:63
#define FL_ASSUME_NONNULL_END
Definition CompilerSupport.h:93
#define FLAPI
Definition CompilerSupport.h:292
FLEECE_PUBLIC void _FLBuf_Release(const void *FL_NULLABLE)
FLEECE_PUBLIC void FL_WipeMemory(void *dst, size_t size)
Writes zeroes to size bytes of memory starting at dst.
FLSlice FLStr(const char *FL_NULLABLE str LIFETIMEBOUND)
Returns a slice pointing to the contents of a C string.
Definition FLSlice.h:132
FLEECE_PUBLIC void _FLBuf_Retain(const void *FL_NULLABLE)
FLEECE_PUBLIC FLSliceResult FLSliceResult_New(size_t)
Allocates an FLSliceResult of the given size, without initializing the buffer.
#define kFLSliceNull
A convenient constant denoting a null slice.
Definition FLSlice.h:106
FLSliceResult FLSliceResult_Retain(FLSliceResult s)
Increments the ref-count of a FLSliceResult.
Definition FLSlice.h:183
FLEECE_PUBLIC bool FLSlice_Equal(FLSlice a, FLSlice b) FLPURE
Equality test of two slices.
void FLSliceResult_Release(FLSliceResult s)
Decrements the ref-count of a FLSliceResult, freeing its memory if it reached zero.
Definition FLSlice.h:189
FLSlice FLSliceResult_AsSlice(FLSliceResult sr LIFETIMEBOUND)
Type-casts a FLSliceResult to FLSlice, since C doesn't know it's a subclass.
Definition FLSlice.h:194
FLSliceResult FLStringResult
Definition FLSlice.h:99
FLPURE int FLMemCmp(const void *FL_NULLABLE a, const void *FL_NULLABLE b, size_t size)
Exactly like memcmp, but safely handles the case where a or b is NULL and size is 0 (by returning 0),...
Definition FLSlice.h:112
FLSlice FLString
Definition FLSlice.h:98
FLSlice FLHeapSlice
A heap-allocated, reference-counted slice.
Definition FLSlice.h:93
FLEECE_PUBLIC bool FLSlice_ToCString(FLSlice s, char *buffer, size_t capacity)
Copies a slice to a buffer, adding a trailing zero byte to make it a valid C string.
FLSliceResult FLSliceResult_CreateWith(const void *FL_NULLABLE bytes, size_t size)
Allocates an FLSliceResult, copying size bytes starting at buf.
Definition FLSlice.h:173
FLEECE_PUBLIC int FLSlice_Compare(FLSlice, FLSlice) FLPURE
Lexicographic comparison of two slices; basically like memcmp(), but taking into account differences ...
FLEECE_PUBLIC uint32_t FLSlice_Hash(FLSlice s) FLPURE
Computes a 32-bit hash of a slice's data, suitable for use in hash tables.
void FLMemCpy(void *FL_NULLABLE dst, const void *FL_NULLABLE src, size_t size)
Exactly like memcmp, but safely handles the case where dst or src is NULL and size is 0 (as a no-op),...
Definition FLSlice.h:122
FLEECE_PUBLIC FLSliceResult FLSlice_Copy(FLSlice)
Allocates an FLSliceResult, copying the given slice.
A simple reference to a block of memory.
Definition FLSlice.h:45
size_t size
Definition FLSlice.h:47
const void *FL_NULLABLE buf
Definition FLSlice.h:46
A heap-allocated block of memory returned from an API call.
Definition FLSlice.h:66
const void *FL_NULLABLE buf
Definition FLSlice.h:67
size_t size
Definition FLSlice.h:68