]>
git.proxmox.com Git - libgit2.git/blob - src/buf_text.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_buf_text_h__
8 #define INCLUDE_buf_text_h__
22 git_bom_t bom
; /* BOM found at head of text */
23 unsigned int nul
, cr
, lf
, crlf
; /* NUL, CR, LF and CRLF counts */
24 unsigned int printable
, nonprintable
; /* These are just approximations! */
28 * Append string to buffer, prefixing each character from `esc_chars` with
31 * @param buf Buffer to append data to
32 * @param string String to escape and append
33 * @param esc_chars Characters to be escaped
34 * @param esc_with String to insert in from of each found character
35 * @return 0 on success, <0 on failure (probably allocation problem)
37 extern int git_buf_text_puts_escaped(
40 const char *esc_chars
,
41 const char *esc_with
);
44 * Append string escaping characters that are regex special
46 GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf
*buf
, const char *string
)
48 return git_buf_text_puts_escaped(buf
, string
, "^.[]$()|*+?{}\\", "\\");
52 * Unescape all characters in a buffer in place
54 * I.e. remove backslashes
56 extern void git_buf_text_unescape(git_buf
*buf
);
59 * Replace all \r\n with \n (or do nothing if no \r\n are found)
61 * @return 0 on success, GIT_ENOTFOUND if no \r\n, -1 on memory error
63 extern int git_buf_text_crlf_to_lf(git_buf
*tgt
, const git_buf
*src
);
66 * Replace all \n with \r\n (or do nothing if no \n are found)
68 * @return 0 on success, GIT_ENOTFOUND if no \n, -1 on memory error
70 extern int git_buf_text_lf_to_crlf(git_buf
*tgt
, const git_buf
*src
);
73 * Fill buffer with the common prefix of a array of strings
75 * Buffer will be set to empty if there is no common prefix
77 extern int git_buf_text_common_prefix(git_buf
*buf
, const git_strarray
*strs
);
80 * Check quickly if buffer looks like it contains binary data
82 * @param buf Buffer to check
83 * @return true if buffer looks like non-text data
85 extern bool git_buf_text_is_binary(const git_buf
*buf
);
88 * Check quickly if buffer contains a NUL byte
90 * @param buf Buffer to check
91 * @return true if buffer contains a NUL byte
93 extern bool git_buf_text_contains_nul(const git_buf
*buf
);
96 * Check if a buffer begins with a UTF BOM
98 * @param bom Set to the type of BOM detected or GIT_BOM_NONE
99 * @param buf Buffer in which to check the first bytes for a BOM
100 * @param offset Offset into buffer to look for BOM
101 * @return Number of bytes of BOM data (or 0 if no BOM found)
103 extern int git_buf_text_detect_bom(
104 git_bom_t
*bom
, const git_buf
*buf
, size_t offset
);
107 * Gather stats for a piece of text
109 * Fill the `stats` structure with counts of unreadable characters, carriage
110 * returns, etc, so it can be used in heuristics. This automatically skips
111 * a trailing EOF (\032 character). Also it will look for a BOM at the
112 * start of the text and can be told to skip that as well.
114 * @param stats Structure to be filled in
115 * @param buf Text to process
116 * @param skip_bom Exclude leading BOM from stats if true
117 * @return Does the buffer heuristically look like binary data
119 extern bool git_buf_text_gather_stats(
120 git_buf_text_stats
*stats
, const git_buf
*buf
, bool skip_bom
);