Skip to content

Commit 501e0d8

Browse files
committed
CDRIVER-3489 add bson_isspace
1 parent 635db83 commit 501e0d8

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/common/common-b64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap)
287287
for (i = 1; i < 256; ++i) {
288288
ch = (unsigned char) i;
289289
/* Whitespaces */
290-
if (ch >= -1 && ch <= 255 && isspace (ch))
290+
if (bson_isspace (ch))
291291
mongoc_b64rmap[i] = mongoc_b64rmap_space;
292292
/* Padding: stop parsing */
293293
else if (ch == Pad64)

src/libbson/doc/bson_isspace.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
:man_page: bson_isspace
2+
3+
bson_isspace()
4+
==============
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
bson_isspace (int c);
13+
14+
Parameters
15+
----------
16+
17+
* ``c``: A character.
18+
19+
Description
20+
-----------
21+
22+
A safer alternative to ``isspace`` with additional bounds checking.
23+
24+
It is equivalent to ``isspace``, excepts always returns false if ``c`` is out of the inclusive bounds [-1, 255].
25+
26+
Returns
27+
-------
28+
29+
A boolean indicating if the ``c`` is considered white-space (as determined by the ``isspace`` function).

src/libbson/doc/character_and_string_routines.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ We provide a small number of character and string routines to substitute for tho
1515
:maxdepth: 1
1616

1717
bson_ascii_strtoll
18+
bson_isspace
1819
bson_snprintf
1920
bson_strcasecmp
2021
bson_strdup

src/libbson/src/bson/bson-string.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ bson_ascii_strtoll (const char *s, char **e, int base)
712712

713713
c = *tok;
714714

715-
while (c >= -1 && c <= 255 && isspace (c)) {
715+
while (bson_isspace (c)) {
716716
c = *++tok;
717717
}
718718

@@ -811,3 +811,10 @@ bson_strcasecmp (const char *s1, const char *s2)
811811
return strcasecmp (s1, s2);
812812
#endif
813813
}
814+
815+
816+
bool
817+
bson_isspace (int c)
818+
{
819+
return c >= -1 && c <= 255 && isspace (c);
820+
}

src/libbson/src/bson/bson-string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ BSON_EXPORT (int64_t)
7676
bson_ascii_strtoll (const char *str, char **endptr, int base);
7777
BSON_EXPORT (int)
7878
bson_strcasecmp (const char *s1, const char *s2);
79+
BSON_EXPORT (bool)
80+
bson_isspace (int c);
7981

8082

8183
BSON_END_DECLS

src/libmongoc/tests/TestSuite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,13 @@ test_error (const char *format, ...) BSON_GNUC_PRINTF (1, 2);
348348
char *__bb = bson_malloc0 (strlen (__b) + 1); \
349349
char *f = __a; \
350350
do { \
351-
while (*__a >= -1 &&*__a <= 255 && isspace (*__a)) \
351+
while (bson_isspace (*__a)) \
352352
__a++; \
353353
__aa[i++] = *__a++; \
354354
} while (*__a); \
355355
i = 0; \
356356
do { \
357-
while (*__b >= -1 &&*__b <= 255 && isspace (*__b)) \
357+
while (bson_isspace (*__b)) \
358358
__b++; \
359359
__bb[i++] = *__b++; \
360360
} while (*__b); \

0 commit comments

Comments
 (0)