nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

pstrutil.h (1202B)


      1 #ifndef PSTRUTIL_H
      2 #define PSTRUTIL_H
      3 
      4 #include <ctype.h> /* toupper */
      5 
      6 
      7 /*
      8  * NOTE: unlike strncpy, we return the first character, and we do not
      9  * pad up to n. Same applies to related functions.
     10  */
     11 
     12 /* `strnlen` not widely supported. */
     13 static inline size_t pstrnlen(const char *s, size_t max_len)
     14 {
     15     const char *end = memchr (s, 0, max_len);
     16     return end ? (size_t)(end - s) : max_len;
     17 }
     18 
     19 static inline char *pstrcpyupper(char *dst, const char *src) {
     20     char *p = dst;
     21     while (*src) {
     22         *p++ = (char)toupper(*src++);
     23     }
     24     *p = '\0';
     25     return dst;
     26 }
     27 
     28 static inline char *pstrncpyupper(char *dst, const char *src, size_t n) {
     29     size_t i;
     30     for (i = 0; i < n && src[i]; ++i) {
     31         dst[i] = (char)toupper(src[i]);
     32     }
     33     if (i < n) {
     34         dst[i] = '\0';
     35     }
     36     return dst;
     37 }
     38 
     39 static inline char *pstrtoupper(char *dst) {
     40     char *p;
     41     for (p = dst; *p; ++p) {
     42         *p = (char)toupper(*p);
     43     }
     44     return dst;
     45 }
     46 
     47 static inline char *pstrntoupper(char *dst, size_t n) {
     48     size_t i;
     49     for (i = 0; i < n && dst[i]; ++i) {
     50         dst[i] = (char)toupper(dst[i]);
     51     }
     52     return dst;
     53 }
     54 
     55 #undef strnlen
     56 #define strnlen pstrnlen
     57 
     58 #endif /*  PSTRUTIL_H */