old-graph: Move graph API into the common graph struct
[tig.git] / include / tig / string.h
blob3997c2eb9353b4aecb7c8b31e96d5f821b85281b
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #ifndef TIG_STRING_H
15 #define TIG_STRING_H
17 #include "tig/tig.h"
18 #include "tig/string.h"
21 * Strings.
24 #define prefixcmp(str1, str2) \
25 strncmp(str1, str2, STRING_SIZE(str2))
27 bool string_isnumber(const char *str);
28 bool iscommit(const char *str);
29 #define get_graph_indent(str) strspn(str, "*|\\/_ ")
31 static inline int
32 ascii_toupper(int c)
34 if (c >= 'a' && c <= 'z')
35 c &= ~0x20;
36 return c;
39 static inline int
40 ascii_tolower(int c)
42 if (c >= 'A' && c <= 'Z')
43 c |= 0x20;
44 return c;
47 int suffixcmp(const char *str, int slen, const char *suffix);
49 void string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen);
51 /* Shorthands for safely copying into a fixed buffer. */
53 #define FORMAT_BUFFER(buf, bufsize, fmt, retval, allow_truncate) \
54 do { \
55 va_list args; \
56 va_start(args, fmt); \
57 retval = vsnprintf(buf, bufsize, fmt, args); \
58 va_end(args); \
59 if (retval >= (bufsize) && allow_truncate) { \
60 (buf)[(bufsize) - 1] = 0; \
61 (buf)[(bufsize) - 2] = '.'; \
62 (buf)[(bufsize) - 3] = '.'; \
63 (buf)[(bufsize) - 4] = '.'; \
64 retval = (bufsize) - 1; \
65 } else if (retval < 0 || retval >= (bufsize)) { \
66 retval = -1; \
67 } \
68 } while (0)
70 #define string_copy(dst, src) \
71 string_ncopy_do(dst, sizeof(dst), src, sizeof(src))
73 #define string_ncopy(dst, src, srclen) \
74 string_ncopy_do(dst, sizeof(dst), src, srclen)
76 void string_copy_rev(char *dst, const char *src);
77 void string_copy_rev_from_commit_line(char *dst, const char *src);
79 #define string_rev_is_null(rev) !strncmp(rev, NULL_ID, STRING_SIZE(NULL_ID))
81 #define string_add(dst, from, src) \
82 string_ncopy_do(dst + (from), sizeof(dst) - (from), src, sizeof(src))
84 size_t string_expanded_length(const char *src, size_t srclen, size_t tabsize, size_t max_size);
85 size_t string_expand(char *dst, size_t dstlen, const char *src, int tabsize);
87 char *chomp_string(char *name);
89 bool PRINTF_LIKE(4, 5) string_nformat(char *buf, size_t bufsize, size_t *bufpos, const char *fmt, ...);
91 #define string_format(buf, fmt, args...) \
92 string_nformat(buf, sizeof(buf), NULL, fmt, args)
94 #define string_format_from(buf, from, fmt, args...) \
95 string_nformat(buf, sizeof(buf), from, fmt, args)
97 int strcmp_null(const char *s1, const char *s2);
98 int strcmp_numeric(const char *s1, const char *s2);
101 * Unicode / UTF-8 handling
104 int unicode_width(unsigned long c, int tab_size);
106 unsigned char utf8_char_length(const char *string);
108 /* Decode UTF-8 multi-byte representation into a Unicode character. */
109 unsigned long utf8_to_unicode(const char *string, size_t length);
111 /* Calculates how much of string can be shown within the given maximum width
112 * and sets trimmed parameter to non-zero value if all of string could not be
113 * shown. If the reserve flag is TRUE, it will reserve at least one
114 * trailing character, which can be useful when drawing a delimiter.
116 * Returns the number of bytes to output from string to satisfy max_width. */
117 size_t utf8_length(const char **start, size_t skip, int *width, size_t max_width, int *trimmed, bool reserve, int tab_size);
119 int utf8_width_max(const char *text, int max);
120 #define utf8_width(text) utf8_width_max(text, -1)
122 #endif
123 /* vim: set ts=8 sw=8 noexpandtab: */