Unicode.h revision 261991
1215226Sadrian//===- llvm/Support/Unicode.h - Unicode character properties -*- C++ -*-=====// 2215226Sadrian// 3215226Sadrian// The LLVM Compiler Infrastructure 4215226Sadrian// 5215226Sadrian// This file is distributed under the University of Illinois Open Source 6215226Sadrian// License. See LICENSE.TXT for details. 7215226Sadrian// 8215226Sadrian//===----------------------------------------------------------------------===// 9215226Sadrian// 10215226Sadrian// This file defines functions that allow querying certain properties of Unicode 11215226Sadrian// characters. 12215226Sadrian// 13215226Sadrian//===----------------------------------------------------------------------===// 14215226Sadrian 15215226Sadrian#include "llvm/ADT/StringRef.h" 16215226Sadrian 17215226Sadriannamespace llvm { 18215226Sadriannamespace sys { 19215226Sadriannamespace unicode { 20215226Sadrian 21215226Sadrianenum ColumnWidthErrors { 22215226Sadrian ErrorInvalidUTF8 = -2, 23215226Sadrian ErrorNonPrintableCharacter = -1 24215226Sadrian}; 25229658Sadrian 26229658Sadrian/// Determines if a character is likely to be displayed correctly on the 27215226Sadrian/// terminal. Exact implementation would have to depend on the specific 28215226Sadrian/// terminal, so we define the semantic that should be suitable for generic case 29215226Sadrian/// of a terminal capable to output Unicode characters. 30215226Sadrian/// 31215226Sadrian/// All characters from the Unicode code point range are considered printable 32215226Sadrian/// except for: 33215226Sadrian/// * C0 and C1 control character ranges; 34215226Sadrian/// * default ignorable code points as per 5.21 of 35215226Sadrian/// http://www.unicode.org/versions/Unicode6.2.0/UnicodeStandard-6.2.pdf 36215226Sadrian/// except for U+00AD SOFT HYPHEN, as it's actually displayed on most 37215226Sadrian/// terminals; 38215226Sadrian/// * format characters (category = Cf); 39215226Sadrian/// * surrogates (category = Cs); 40215226Sadrian/// * unassigned characters (category = Cn). 41241298Smarcel/// \return true if the character is considered printable. 42241298Smarcelbool isPrintable(int UCS); 43215226Sadrian 44215226Sadrian/// Gets the number of positions the UTF8-encoded \p Text is likely to occupy 45215226Sadrian/// when output on a terminal ("character width"). This depends on the 46229658Sadrian/// implementation of the terminal, and there's no standard definition of 47215226Sadrian/// character width. 48215226Sadrian/// 49215226Sadrian/// The implementation defines it in a way that is expected to be compatible 50251512Semaste/// with a generic Unicode-capable terminal. 51251512Semaste/// 52251512Semaste/// \return Character width: 53215226Sadrian/// * ErrorNonPrintableCharacter (-1) if \p Text contains non-printable 54215226Sadrian/// characters (as identified by isPrintable); 55215226Sadrian/// * 0 for each non-spacing and enclosing combining mark; 56215226Sadrian/// * 2 for each CJK character excluding halfwidth forms; 57215226Sadrian/// * 1 for each of the remaining characters. 58215226Sadrianint columnWidthUTF8(StringRef Text); 59215226Sadrian 60215226Sadrian} // namespace unicode 61215226Sadrian} // namespace sys 62229658Sadrian} // namespace llvm 63215413Sadrian