Blame src/buf_text.h

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