2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_BSTR_H
20 #define MPLAYER_BSTR_H
29 /* NOTE: 'len' is size_t, but most string-handling functions below assume
30 * that input size has been sanity checked and len fits in an int.
37 // demux_rtp.cpp (live555) C++ compilation workaround
39 // If str.start is NULL, return NULL.
40 static inline char *bstrdup0(void *talloc_ctx
, struct bstr str
)
42 return talloc_strndup(talloc_ctx
, (char *)str
.start
, str
.len
);
45 // Return start = NULL iff that is true for the original.
46 static inline struct bstr
bstrdup(void *talloc_ctx
, struct bstr str
)
48 struct bstr r
= { NULL
, str
.len
};
50 r
.start
= talloc_memdup(talloc_ctx
, str
.start
, str
.len
);
54 static inline struct bstr
bstr(const unsigned char *s
)
56 return (struct bstr
){(unsigned char *)s
, s
? strlen(s
) : 0};
59 int bstrcmp(struct bstr str1
, struct bstr str2
);
60 int bstrcasecmp(struct bstr str1
, struct bstr str2
);
61 int bstrchr(struct bstr str
, int c
);
62 int bstrrchr(struct bstr str
, int c
);
63 int bstrcspn(struct bstr str
, const char *reject
);
65 int bstr_find(struct bstr haystack
, struct bstr needle
);
66 struct bstr
*bstr_splitlines(void *talloc_ctx
, struct bstr str
);
67 struct bstr
bstr_getline(struct bstr str
, struct bstr
*rest
);
68 struct bstr
bstr_lstrip(struct bstr str
);
69 struct bstr
bstr_strip(struct bstr str
);
70 struct bstr
bstr_split(struct bstr str
, const char *sep
, struct bstr
*rest
);
71 struct bstr
bstr_splice(struct bstr str
, int start
, int end
);
72 long long bstrtoll(struct bstr str
, struct bstr
*rest
, int base
);
73 double bstrtod(struct bstr str
, struct bstr
*rest
);
74 void bstr_lower(struct bstr str
);
75 int bstr_sscanf(struct bstr str
, const char *format
, ...);
77 // Decode the UTF-8 code point at the start of the string,, and return the
79 // After calling this function, *out_next will point to the next character.
80 // out_next can be NULL.
81 // On error, -1 is returned, and *out_next is not modified.
82 int bstr_decode_utf8(struct bstr str
, struct bstr
*out_next
);
84 // Return the length of the UTF-8 sequence that starts with the given byte.
85 // Given a string char *s, the next UTF-8 code point is to be expected at
86 // s + bstr_parse_utf8_code_length(s[0])
87 // On error, -1 is returned. On success, it returns a value in the range [1, 4].
88 int bstr_parse_utf8_code_length(unsigned char b
);
90 // If s starts with prefix, return true and return the rest of the string in s.
91 bool bstr_eatstart(struct bstr
*s
, struct bstr prefix
);
93 static inline struct bstr
bstr_cut(struct bstr str
, int n
)
97 return (struct bstr
){str
.start
+ n
, str
.len
- n
};
100 static inline bool bstr_startswith(struct bstr str
, struct bstr prefix
)
102 if (str
.len
< prefix
.len
)
104 return !memcmp(str
.start
, prefix
.start
, prefix
.len
);
107 static inline bool bstr_startswith0(struct bstr str
, const char *prefix
)
109 return bstr_startswith(str
, bstr(prefix
));
112 static inline bool bstr_endswith(struct bstr str
, struct bstr suffix
)
114 if (str
.len
< suffix
.len
)
116 return !memcmp(str
.start
+ str
.len
- suffix
.len
, suffix
.start
, suffix
.len
);
119 static inline bool bstr_endswith0(struct bstr str
, const char *suffix
)
121 return bstr_endswith(str
, bstr(suffix
));
124 static inline int bstrcmp0(struct bstr str1
, const char *str2
)
126 return bstrcmp(str1
, bstr(str2
));
129 static inline int bstrcasecmp0(struct bstr str1
, const char *str2
)
131 return bstrcasecmp(str1
, bstr(str2
));
134 static inline int bstr_find0(struct bstr haystack
, const char *needle
)
136 return bstr_find(haystack
, bstr(needle
));
139 static inline int bstr_eatstart0(struct bstr
*s
, char *prefix
)
141 return bstr_eatstart(s
, bstr(prefix
));
146 // create a pair (not single value!) for "%.*s" printf syntax
147 #define BSTR_P(bstr) (int)((bstr).len), (bstr).start
149 #define WHITESPACE " \f\n\r\t\v"
151 #endif /* MPLAYER_BSTR_H */