source: docs/Working/icGrep/unicode-re.tex @ 4556

Last change on this file since 4556 was 4556, checked in by cameron, 4 years ago

Refine UTF-8 equations

File size: 7.8 KB
1\section{Bitwise Methods for UTF-8}\label{sec:Unicode}
3As described in the following section, \icGrep{} is a reimplementation
4of the bitwise data parallel method implemented on top of LLVM
5infrastructure and adapted for Unicode regular expression search
6through data streams represented in UTF-8.  In this section,
7we present the techniques we have used to extend the bitwise
8matching techniques to the variable-length encodings of UTF-8.
10The first requirement in implementing a regular expression processor
11over UTF-8 data streams is to translate Unicode regular expressions
12over codepoints to corresponding regular expressions over
13sequences of UTF-8 bytes or \emph{code units}.   The {\tt toUTF8} transformation
14performs this as a regular expression transformation, transforming
15input expressions such as `\verb:\u{244}[\u{2030}-\u{2137}]:`
16to the corresponding UTF-8 regular expression consisting of the series of sequences and alternations shown below:
24\paragraph{UTF-8 Byte Classification and Validation.}
25In UTF-8, bytes are classified as individual ASCII bytes, or as
26prefixes of two-, three-, or four-byte sequences, or as suffix bytes.
27In addition, we say that the {\em scope} bytes of a prefix are the
28immediately following byte positions at which a suffix byte is
30Mismatches between scope expectations and occurrences of suffix
31bytes indicate errors.
32Two helper streams are also useful.
33The Initial stream marks ASCII bytes and prefixes of multibyte sequences,
34while the NonFinal stream marks any position that is not the final
35position of a Unicode character.
37\mbox{\rm ASCII} & = & \mbox{\rm CharClass}(\verb:[\x00-\x7F]:) \\
38\mbox{\rm Prefix} & = & \mbox{\rm CharClass}(\verb:[\xC2-\F4]:) \\
39\mbox{\rm Prefix3or4} & = & \mbox{\rm CharClass}(\verb:[\xE0-\xF4]:) \\
40\mbox{\rm Prefix4} & = & \mbox{\rm CharClass}(\verb:[\xF0-\xF4]:) \\
41\mbox{\rm Suffix} & = & \mbox{\rm CharClass}(\verb:[\x80-\xBF]:) \\
42\mbox{\rm Scope} & = & \mbox{\rm Advance}(\mbox{Prefix}) \vee \mbox{\rm
43  Advance}(\mbox{Prefix3or4},2) \vee \mbox{\rm
44  Advance}(\mbox{Prefix4}, 3) \\
45\mbox{\rm Mismatch} & = & \mbox{\rm Scope} \oplus \mbox{Suffix} \\
46\mbox{\rm Initial} & = & \mbox{\rm ASCII} \vee \mbox{Prefix} \\
47\mbox{\rm NonFinal} & = & \mbox{\rm Prefix} \vee \mbox{\rm
48  Advance}(\mbox{Prefix3or4}) \vee \mbox{\rm Advance}(\mbox{Prefix4}, 2)
51\paragraph{Unicode Character Classes.}  Whereas ASCII character classes
52can be determined by simple bitwise logic at a single code unit position,
53the UnicodeClass stream for a given class involves logic for up to four positions.
54By convention, we define UnicodeClass($U$) for a given Unicode character class
55$U$ to be the stream marking the {\em final} position of
56Unicode character classes.
58Using these definitions, it is then possible to extend the matching
59equations to operate with Unicode character classes as follows.
62\mbox{\rm Match}(m, U) & = &  \mbox{\rm Advance}(\mbox{\rm ScanThru}(m, \mbox{\rm NonFinal}) \wedge \mbox{\rm UnicodeClass}(U)) \\
63\mbox{\rm Match}(m, U*) & = &  \mbox{\rm MatchStar}(m, \mbox{\rm UnicodeClass}(U) \vee \mbox{\rm NonFinal}) \wedge \mbox{\rm Initial}\\
66Here, we use
67the ScanThru~\cite{cameron2011parallel} operation to move a set of markers
68each through the nonfinal bytes of UTF-8 sequences to the final
69byte position.
70\[{\mbox ScanThru}(m, c) = (m + c)  \wedge \neg c\]
71Figure~\ref{fig:multibytesequence} shows this
72technique in operation in the case of advancing through byte
73sequences (each 3 bytes in length) corresponding to Chinese characters.
74To better demonstrate the process, we use \texttt{ni3}, \texttt{hao}
75and \texttt{men} to represent these characters.
76$\text{CC}_{\texttt{ni3}}$ is the bitstream that marks character
77\texttt{ni3} and $\text{CC}_{\texttt{hao}}$ is the bitstream that
78marks character \texttt{hao}.
79To match a two UTF-8 character sequence \texttt{ni3hao}, we first
80create an Initial stream that marks the first byte of all the valid characters.
81We also produce a NonFinal stream that marks every byte of all
82multibyte characters \emph{except for} the last byte.
83Using Initial to ScanThru NonFinal, we construct bitstream $M_1$, which
84marks the positions of the last byte of every character.
85An overlap between $M_1$ and $\text{CC}_{\texttt{ni3}}$ gives the start
86position for matching the next character.
87As illustrated by $M_2$, we find two matches for \texttt{ni3},
88and from these two positions we can start the matching process for
89the next character \texttt{hao}.
90The final result stream $M_4$ shows one match for the multibyte sequence
98input data                                                         & \verb`ni3hao(Hello),ni3men(You),`\\
99$\text{CC}_{\text{ni3}}$                                           & \verb`..1.............1.........`\\
100$\text{CC}_{\text{hao}}$                                           & \verb`.....1....................`\\
101Initial                                                            & \verb`1..1..111111111..1..111111`\\
102NonFinal                                                           & \verb`11.11.........11.11.......`\\
103$M_1 = \text{ScanThru}(\text{Initial}, \text{NonFinal})$           & \verb`..1..111111111..1..1111111`\\
104$M_2 = \text{Advance}(M_1 \land \text{CC}_{\text{ni3}})$           & \verb`...1.............1........`\\
105$M_3 = \text{ScanThru}(\text{Initial} \land M_2, \text{NonFinal})$ & \verb`.....1.............1......`\\
106$M_4 = M_3 \land CC_{\text{hao}}$                                  & \verb`.....1....................`
110\caption{Processing of a Multibyte Sequence ni3hao}
115\paragraph{Unicode MatchStar.}
116The $\mathit{MatchStar}(M, C)$ operation directly implements
117the operation of finding all positions reachable from a
118marker bit in $M$ through a character class repetition of
119an ASCII byte class $C$.   In UTF-8 matching, however,
120the character class byte streams are marked at their
121{\em final} positions.   Thus the one bits of a Unicode character
122class stream are not necessarily contiguous.  This in turn
123means that carry propagation within the MatchStar
124operation may terminate prematurely.
126In order to remedy this problem, \icGrep{} again uses the two helper bitstreams
127\emph{Initial} and \emph{NonFinal}.   Any full match to a multibyte sequence must
128reach the initial position of the next character. 
129The {\em NonFinal} bitstream consists of all positions except those
130that are final positions of UTF-8 sequences.
131It is used to ``fill in the gaps'' in the CC bitstream so that the
132 MatchStar addition can move through a contiguous sequence of one
133 bits.  In this way, matching of an arbitrary Unicode character class
134 $C$ (with a 1 bit set at final positions of any members of the class),
135can be implemented using ${\mathit{MatchStar}(M, C |\mathit{NonFinal})}$.
137\paragraph{Predefined Unicode Classes.}
138\icGrep{} employs a set of bitstreams that are precompiled
139into the executable.  These include all bitstreams corresponding
140to Unicode property expressions such as \verb:\p{Greek}:.
141Each property potentially contains many code points, so we further
142embed the calculations within an if hierarchy.   Each if-statement
143within the hierarchy determines whether the current block contains
144any codepoints at all in a given Unicode range.   At the outer
145level, the ranges are quite coarse, becoming successively refined
146at deeper levels.  This technique works well when input documents
147contain long runs of text confined to one or a few ranges.
149%\subsection{Character Class Intersection and Difference}
150%\subsection{Unicode Case-Insensitive Matching}
Note: See TracBrowser for help on using the repository browser.