Blame src/buf_text.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
#ifndef INCLUDE_buf_text_h__
Packit ae9e2a
#define INCLUDE_buf_text_h__
Packit ae9e2a
Packit ae9e2a
#include "buffer.h"
Packit ae9e2a
Packit ae9e2a
typedef enum {
Packit ae9e2a
	GIT_BOM_NONE = 0,
Packit ae9e2a
	GIT_BOM_UTF8 = 1,
Packit ae9e2a
	GIT_BOM_UTF16_LE = 2,
Packit ae9e2a
	GIT_BOM_UTF16_BE = 3,
Packit ae9e2a
	GIT_BOM_UTF32_LE = 4,
Packit ae9e2a
	GIT_BOM_UTF32_BE = 5
Packit ae9e2a
} git_bom_t;
Packit ae9e2a
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_bom_t bom; /* BOM found at head of text */
Packit ae9e2a
	unsigned int nul, cr, lf, crlf; /* NUL, CR, LF and CRLF counts */
Packit ae9e2a
	unsigned int printable, nonprintable; /* These are just approximations! */
Packit ae9e2a
} git_buf_text_stats;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Append string to buffer, prefixing each character from `esc_chars` with
Packit ae9e2a
 * `esc_with` string.
Packit ae9e2a
 *
Packit ae9e2a
 * @param buf Buffer to append data to
Packit ae9e2a
 * @param string String to escape and append
Packit ae9e2a
 * @param esc_chars Characters to be escaped
Packit ae9e2a
 * @param esc_with String to insert in from of each found character
Packit ae9e2a
 * @return 0 on success, <0 on failure (probably allocation problem)
Packit ae9e2a
 */
Packit ae9e2a
extern int git_buf_text_puts_escaped(
Packit ae9e2a
	git_buf *buf,
Packit ae9e2a
	const char *string,
Packit ae9e2a
	const char *esc_chars,
Packit ae9e2a
	const char *esc_with);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Append string escaping characters that are regex special
Packit ae9e2a
 */
Packit ae9e2a
GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf *buf, const char *string)
Packit ae9e2a
{
Packit ae9e2a
	return git_buf_text_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\");
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Unescape all characters in a buffer in place
Packit ae9e2a
 *
Packit ae9e2a
 * I.e. remove backslashes
Packit ae9e2a
 */
Packit ae9e2a
extern void git_buf_text_unescape(git_buf *buf);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Replace all \r\n with \n.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, -1 on memory error
Packit ae9e2a
 */
Packit ae9e2a
extern int git_buf_text_crlf_to_lf(git_buf *tgt, const git_buf *src);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Replace all \n with \r\n. Does not modify existing \r\n.
Packit ae9e2a
 *
Packit ae9e2a
 * @return 0 on success, -1 on memory error
Packit ae9e2a
 */
Packit ae9e2a
extern int git_buf_text_lf_to_crlf(git_buf *tgt, const git_buf *src);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Fill buffer with the common prefix of a array of strings
Packit ae9e2a
 *
Packit ae9e2a
 * Buffer will be set to empty if there is no common prefix
Packit ae9e2a
 */
Packit ae9e2a
extern int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strs);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check quickly if buffer looks like it contains binary data
Packit ae9e2a
 *
Packit ae9e2a
 * @param buf Buffer to check
Packit ae9e2a
 * @return true if buffer looks like non-text data
Packit ae9e2a
 */
Packit ae9e2a
extern bool git_buf_text_is_binary(const git_buf *buf);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check quickly if buffer contains a NUL byte
Packit ae9e2a
 *
Packit ae9e2a
 * @param buf Buffer to check
Packit ae9e2a
 * @return true if buffer contains a NUL byte
Packit ae9e2a
 */
Packit ae9e2a
extern bool git_buf_text_contains_nul(const git_buf *buf);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Check if a buffer begins with a UTF BOM
Packit ae9e2a
 *
Packit ae9e2a
 * @param bom Set to the type of BOM detected or GIT_BOM_NONE
Packit ae9e2a
 * @param buf Buffer in which to check the first bytes for a BOM
Packit ae9e2a
 * @param offset Offset into buffer to look for BOM
Packit ae9e2a
 * @return Number of bytes of BOM data (or 0 if no BOM found)
Packit ae9e2a
 */
Packit ae9e2a
extern int git_buf_text_detect_bom(
Packit ae9e2a
	git_bom_t *bom, const git_buf *buf, size_t offset);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Gather stats for a piece of text
Packit ae9e2a
 *
Packit ae9e2a
 * Fill the `stats` structure with counts of unreadable characters, carriage
Packit ae9e2a
 * returns, etc, so it can be used in heuristics.  This automatically skips
Packit ae9e2a
 * a trailing EOF (\032 character).  Also it will look for a BOM at the
Packit ae9e2a
 * start of the text and can be told to skip that as well.
Packit ae9e2a
 *
Packit ae9e2a
 * @param stats Structure to be filled in
Packit ae9e2a
 * @param buf Text to process
Packit ae9e2a
 * @param skip_bom Exclude leading BOM from stats if true
Packit ae9e2a
 * @return Does the buffer heuristically look like binary data
Packit ae9e2a
 */
Packit ae9e2a
extern bool git_buf_text_gather_stats(
Packit ae9e2a
	git_buf_text_stats *stats, const git_buf *buf, bool skip_bom);
Packit ae9e2a
Packit ae9e2a
#endif