source: branches/stable/CORE/arb_string.h

Last change on this file was 18113, checked in by westram, 5 years ago
File size: 3.1 KB
Line 
1// ========================================================= //
2//                                                           //
3//   File      : arb_string.h                                //
4//   Purpose   : (inlined) string functions using char*      //
5//                                                           //
6//   Coded by Ralf Westram (coder@reallysoft.de) in Nov 10   //
7//   http://www.arb-home.de/                                 //
8//                                                           //
9// ========================================================= //
10
11// Note: code using std::string should go to arb_stdstring.h
12//                           or ../TEMPLATES/arb_stdstr.h
13
14#ifndef ARB_STRING_H
15#define ARB_STRING_H
16
17#ifndef ARB_MEM_H
18#include "arb_mem.h"
19#endif
20#ifndef ARB_ASSERT_H
21#include "arb_assert.h"
22#endif
23#ifndef _GLIBCXX_CSTRING
24#include <cstring>
25#endif
26
27inline char *ARB_strdup(const char *str) {
28    char *dup = strdup(str);
29    if (!dup) arb_mem::failed_to_allocate(strlen(str));
30    return dup;
31}
32
33inline char *ARB_strduplen(const char *p, unsigned len) {
34    // fast replacement for strdup, if len is known
35    //
36    // Note: to copy a part of a string -> use ARB_strpartdup or ARB_strndup.
37
38    if (p) {
39        char *neu;
40
41        arb_assert(strlen(p) == len);
42        // Note: Common reason for failure: a zero-char was manually printed by a GBS_global_string...-function
43
44        ARB_alloc(neu, len+1);
45        memcpy(neu, p, len+1);
46        return neu;
47    }
48    return NULp;
49}
50
51inline char *ARB_strpartdup(const char *start, const char *end) {
52    /* strdup of a part of a string (including 'start' and 'end')
53     * 'end' may point behind end of string -> copy only till zero byte
54     * if 'start' is NULp   -> return NULp
55     * if 'end'   is NULp   -> copy whole string
56     * if 'end'=('start'-1) -> return ""
57     * if 'end'<('start'-1) -> return NULp
58     */
59
60    char *result;
61    if (start && end) {
62        int len = end-start+1;
63
64        if (len >= 0) {
65            const char *eos = (const char *)memchr(start, 0, len);
66
67            if (eos) len = eos-start;
68            ARB_alloc(result, len+1);
69            memcpy(result, start, len);
70            result[len] = 0;
71        }
72        else {
73            result = NULp;
74        }
75    }
76    else { // end = 0 -> return copy of complete string
77        result = nulldup(start);
78    }
79
80    return result;
81}
82
83inline char *ARB_strndup(const char *start, int len) {
84    // strdup part of string.
85    // copies either 'len' characters or until zero-byte, whichever is shorter.
86    //
87    // Note: to copy the whole string when length is known -> use ARB_strduplen.
88    return start ? ARB_strpartdup(start, start+len-1) : NULp;
89}
90
91
92const char *ARB_date_string(void);
93const char *ARB_dateTime_suffix(void);
94const char *ARB_keep_string(char *str);
95
96inline const char *ARB_strchrnul(const char *str, int chr) {
97#if defined(DARWIN)
98    // replacement code for strchrnul:
99    char buf[2] = "x";
100    buf[0]      = chr;
101    return str+strcspn(str, buf);
102#else
103    return strchrnul(str, chr); // strchrnul is gnu specific
104#endif
105}
106
107#else
108#error arb_string.h included twice
109#endif /* ARB_STRING_H */
Note: See TracBrowser for help on using the repository browser.