1//===- llvm/Support/Unicode.h - Unicode character properties  -*- C++ -*-=====//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines functions that allow querying certain properties of Unicode
11// characters.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/StringRef.h"
16
17namespace llvm {
18namespace sys {
19namespace unicode {
20
21enum ColumnWidthErrors {
22  ErrorInvalidUTF8 = -2,
23  ErrorNonPrintableCharacter = -1
24};
25
26/// Determines if a character is likely to be displayed correctly on the
27/// terminal. Exact implementation would have to depend on the specific
28/// terminal, so we define the semantic that should be suitable for generic case
29/// of a terminal capable to output Unicode characters.
30///
31/// All characters from the Unicode code point range are considered printable
32/// except for:
33///   * C0 and C1 control character ranges;
34///   * default ignorable code points as per 5.21 of
35///     http://www.unicode.org/versions/Unicode6.2.0/UnicodeStandard-6.2.pdf
36///     except for U+00AD SOFT HYPHEN, as it's actually displayed on most
37///     terminals;
38///   * format characters (category = Cf);
39///   * surrogates (category = Cs);
40///   * unassigned characters (category = Cn).
41/// \return true if the character is considered printable.
42bool isPrintable(int UCS);
43
44/// Gets the number of positions the UTF8-encoded \p Text is likely to occupy
45/// when output on a terminal ("character width"). This depends on the
46/// implementation of the terminal, and there's no standard definition of
47/// character width.
48///
49/// The implementation defines it in a way that is expected to be compatible
50/// with a generic Unicode-capable terminal.
51///
52/// \return Character width:
53///   * ErrorNonPrintableCharacter (-1) if \p Text contains non-printable
54///     characters (as identified by isPrintable);
55///   * 0 for each non-spacing and enclosing combining mark;
56///   * 2 for each CJK character excluding halfwidth forms;
57///   * 1 for each of the remaining characters.
58int columnWidthUTF8(StringRef Text);
59
60} // namespace unicode
61} // namespace sys
62} // namespace llvm
63