]> git.proxmox.com Git - libgit2.git/blame - src/buf_text.h
Merge pull request #1208 from ethomson/ppc_sha1_asm_deadness
[libgit2.git] / src / buf_text.h
CommitLineData
7bf87ab6
RB
1/*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
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.
6 */
7#ifndef INCLUDE_buf_text_h__
8#define INCLUDE_buf_text_h__
9
10#include "buffer.h"
11
12typedef enum {
13 GIT_BOM_NONE = 0,
14 GIT_BOM_UTF8 = 1,
15 GIT_BOM_UTF16_LE = 2,
16 GIT_BOM_UTF16_BE = 3,
17 GIT_BOM_UTF32_LE = 4,
18 GIT_BOM_UTF32_BE = 5
19} git_bom_t;
20
21typedef struct {
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! */
25} git_buf_text_stats;
26
27/**
28 * Append string to buffer, prefixing each character from `esc_chars` with
29 * `esc_with` string.
30 *
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)
36 */
37extern int git_buf_text_puts_escaped(
38 git_buf *buf,
39 const char *string,
40 const char *esc_chars,
41 const char *esc_with);
42
43/**
44 * Append string escaping characters that are regex special
45 */
46GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf *buf, const char *string)
47{
48 return git_buf_text_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\");
49}
50
51/**
52 * Unescape all characters in a buffer in place
53 *
54 * I.e. remove backslashes
55 */
56extern void git_buf_text_unescape(git_buf *buf);
57
58/**
59 * Fill buffer with the common prefix of a array of strings
60 *
61 * Buffer will be set to empty if there is no common prefix
62 */
63extern int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strs);
64
65/**
66 * Check quickly if buffer looks like it contains binary data
67 *
68 * @param buf Buffer to check
69 * @return true if buffer looks like non-text data
70 */
71extern bool git_buf_text_is_binary(const git_buf *buf);
72
73/**
74 * Check if a buffer begins with a UTF BOM
75 *
76 * @param bom Set to the type of BOM detected or GIT_BOM_NONE
77 * @param buf Buffer in which to check the first bytes for a BOM
78 * @param offset Offset into buffer to look for BOM
79 * @return Number of bytes of BOM data (or 0 if no BOM found)
80 */
81extern int git_buf_text_detect_bom(
82 git_bom_t *bom, const git_buf *buf, size_t offset);
83
84/**
85 * Gather stats for a piece of text
86 *
87 * Fill the `stats` structure with counts of unreadable characters, carriage
88 * returns, etc, so it can be used in heuristics. This automatically skips
89 * a trailing EOF (\032 character). Also it will look for a BOM at the
90 * start of the text and can be told to skip that as well.
91 *
92 * @param stats Structure to be filled in
93 * @param buf Text to process
94 * @param skip_bom Exclude leading BOM from stats if true
95 * @return Does the buffer heuristically look like binary data
96 */
97extern bool git_buf_text_gather_stats(
98 git_buf_text_stats *stats, const git_buf *buf, bool skip_bom);
99
100#endif