/usr/share/doc/libghc-utf8-string-doc/html/src/Codec-Binary-UTF8-String.html is in libghc-utf8-string-doc 1.0.1.1-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Codec/Binary/UTF8/String.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE CPP #-}</span>
<a name="line-2"></a><span class='hs-cpp'>#if __GLASGOW_HASKELL__ >= 701</span>
<a name="line-3"></a><span class='hs-comment'>{-# LANGUAGE Trustworthy #-}</span>
<a name="line-4"></a><span class='hs-cpp'>#endif</span>
<a name="line-5"></a><span class='hs-comment'>--</span>
<a name="line-6"></a><span class='hs-comment'>-- |</span>
<a name="line-7"></a><span class='hs-comment'>-- Module : Codec.Binary.UTF8.String</span>
<a name="line-8"></a><span class='hs-comment'>-- Copyright : (c) Eric Mertens 2007</span>
<a name="line-9"></a><span class='hs-comment'>-- License : BSD3-style (see LICENSE)</span>
<a name="line-10"></a><span class='hs-comment'>--</span>
<a name="line-11"></a><span class='hs-comment'>-- Maintainer: emertens@galois.com</span>
<a name="line-12"></a><span class='hs-comment'>-- Stability : experimental</span>
<a name="line-13"></a><span class='hs-comment'>-- Portability : portable</span>
<a name="line-14"></a><span class='hs-comment'>--</span>
<a name="line-15"></a><span class='hs-comment'>-- Support for encoding UTF8 Strings to and from @[Word8]@</span>
<a name="line-16"></a><span class='hs-comment'>--</span>
<a name="line-17"></a>
<a name="line-18"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Codec</span><span class='hs-varop'>.</span><span class='hs-conid'>Binary</span><span class='hs-varop'>.</span><span class='hs-conid'>UTF8</span><span class='hs-varop'>.</span><span class='hs-conid'>String</span> <span class='hs-layout'>(</span>
<a name="line-19"></a> <span class='hs-varid'>encode</span>
<a name="line-20"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>decode</span>
<a name="line-21"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>encodeString</span>
<a name="line-22"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>decodeString</span>
<a name="line-23"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>encodeChar</span>
<a name="line-24"></a>
<a name="line-25"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>isUTF8Encoded</span>
<a name="line-26"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>utf8Encode</span>
<a name="line-27"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-28"></a>
<a name="line-29"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Word</span> <span class='hs-layout'>(</span><span class='hs-conid'>Word8</span><span class='hs-layout'>,</span><span class='hs-conid'>Word32</span><span class='hs-layout'>)</span>
<a name="line-30"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Bits</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varop'>.|.</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span><span class='hs-layout'>(</span><span class='hs-varop'>.&.</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span><span class='hs-varid'>shiftL</span><span class='hs-layout'>,</span><span class='hs-varid'>shiftR</span><span class='hs-layout'>)</span>
<a name="line-31"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Char</span> <span class='hs-layout'>(</span><span class='hs-varid'>chr</span><span class='hs-layout'>,</span><span class='hs-varid'>ord</span><span class='hs-layout'>)</span>
<a name="line-32"></a>
<a name="line-33"></a><span class='hs-keyword'>default</span><span class='hs-layout'>(</span><span class='hs-conid'>Int</span><span class='hs-layout'>)</span>
<a name="line-34"></a>
<a name="line-35"></a><a name="encodeString"></a><span class='hs-comment'>-- | Encode a string using 'encode' and store the result in a 'String'.</span>
<a name="line-36"></a><span class='hs-definition'>encodeString</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-37"></a><span class='hs-definition'>encodeString</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>toEnum</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fromEnum</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>encode</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-38"></a>
<a name="line-39"></a><a name="decodeString"></a><span class='hs-comment'>-- | Decode a string using 'decode' using a 'String' as input.</span>
<a name="line-40"></a><span class='hs-comment'>-- | This is not safe but it is necessary if UTF-8 encoded text</span>
<a name="line-41"></a><span class='hs-comment'>-- | has been loaded into a 'String' prior to being decoded.</span>
<a name="line-42"></a><span class='hs-definition'>decodeString</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-43"></a><span class='hs-definition'>decodeString</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>decode</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>toEnum</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fromEnum</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-44"></a>
<a name="line-45"></a><a name="replacement_character"></a><span class='hs-definition'>replacement_character</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Char</span>
<a name="line-46"></a><span class='hs-definition'>replacement_character</span> <span class='hs-keyglyph'>=</span> <span class='hs-chr'>'\xfffd'</span>
<a name="line-47"></a>
<a name="line-48"></a><a name="encodeChar"></a><span class='hs-comment'>-- | Encode a single Haskell Char to a list of Word8 values, in UTF8 format.</span>
<a name="line-49"></a><span class='hs-definition'>encodeChar</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Char</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Word8</span><span class='hs-keyglyph'>]</span>
<a name="line-50"></a><span class='hs-definition'>encodeChar</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>fromIntegral</span> <span class='hs-varop'>.</span> <span class='hs-varid'>go</span> <span class='hs-varop'>.</span> <span class='hs-varid'>ord</span>
<a name="line-51"></a> <span class='hs-keyword'>where</span>
<a name="line-52"></a> <span class='hs-varid'>go</span> <span class='hs-varid'>oc</span>
<a name="line-53"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>oc</span> <span class='hs-varop'><=</span> <span class='hs-num'>0x7f</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>oc</span><span class='hs-keyglyph'>]</span>
<a name="line-54"></a>
<a name="line-55"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>oc</span> <span class='hs-varop'><=</span> <span class='hs-num'>0x7ff</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-num'>0xc0</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>6</span><span class='hs-layout'>)</span>
<a name="line-56"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-varid'>oc</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span>
<a name="line-57"></a> <span class='hs-keyglyph'>]</span>
<a name="line-58"></a>
<a name="line-59"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>oc</span> <span class='hs-varop'><=</span> <span class='hs-num'>0xffff</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-num'>0xe0</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>12</span><span class='hs-layout'>)</span>
<a name="line-60"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>6</span><span class='hs-layout'>)</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-61"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-varid'>oc</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span>
<a name="line-62"></a> <span class='hs-keyglyph'>]</span>
<a name="line-63"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-num'>0xf0</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>18</span><span class='hs-layout'>)</span>
<a name="line-64"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>12</span><span class='hs-layout'>)</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-65"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>`shiftR`</span> <span class='hs-num'>6</span><span class='hs-layout'>)</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-66"></a> <span class='hs-layout'>,</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>+</span> <span class='hs-varid'>oc</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span>
<a name="line-67"></a> <span class='hs-keyglyph'>]</span>
<a name="line-68"></a>
<a name="line-69"></a>
<a name="line-70"></a><a name="encode"></a><span class='hs-comment'>-- | Encode a Haskell String to a list of Word8 values, in UTF8 format.</span>
<a name="line-71"></a><span class='hs-definition'>encode</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Word8</span><span class='hs-keyglyph'>]</span>
<a name="line-72"></a><span class='hs-definition'>encode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>encodeChar</span>
<a name="line-73"></a>
<a name="line-74"></a><a name="decode"></a><span class='hs-comment'>--</span>
<a name="line-75"></a><span class='hs-comment'>-- | Decode a UTF8 string packed into a list of Word8 values, directly to String</span>
<a name="line-76"></a><span class='hs-comment'>--</span>
<a name="line-77"></a><span class='hs-definition'>decode</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Word8</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-78"></a><span class='hs-definition'>decode</span> <span class='hs-keyglyph'>[</span> <span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>""</span>
<a name="line-79"></a><span class='hs-definition'>decode</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-conop'>:</span><span class='hs-varid'>cs</span><span class='hs-layout'>)</span>
<a name="line-80"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0x80</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>chr</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromEnum</span> <span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>cs</span>
<a name="line-81"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xc0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>cs</span>
<a name="line-82"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xe0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>multi1</span>
<a name="line-83"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xf0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>multi_byte</span> <span class='hs-num'>2</span> <span class='hs-num'>0xf</span> <span class='hs-num'>0x800</span>
<a name="line-84"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xf8</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>multi_byte</span> <span class='hs-num'>3</span> <span class='hs-num'>0x7</span> <span class='hs-num'>0x10000</span>
<a name="line-85"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfc</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>multi_byte</span> <span class='hs-num'>4</span> <span class='hs-num'>0x3</span> <span class='hs-num'>0x200000</span>
<a name="line-86"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfe</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>multi_byte</span> <span class='hs-num'>5</span> <span class='hs-num'>0x1</span> <span class='hs-num'>0x4000000</span>
<a name="line-87"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>cs</span>
<a name="line-88"></a> <span class='hs-keyword'>where</span>
<a name="line-89"></a> <span class='hs-varid'>multi1</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>cs</span> <span class='hs-keyword'>of</span>
<a name="line-90"></a> <span class='hs-varid'>c1</span> <span class='hs-conop'>:</span> <span class='hs-varid'>ds</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c1</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0xc0</span> <span class='hs-varop'>==</span> <span class='hs-num'>0x80</span> <span class='hs-keyglyph'>-></span>
<a name="line-91"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>d</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>fromEnum</span> <span class='hs-varid'>c</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x1f</span><span class='hs-layout'>)</span> <span class='hs-varop'>`shiftL`</span> <span class='hs-num'>6</span><span class='hs-layout'>)</span> <span class='hs-varop'>.|.</span> <span class='hs-varid'>fromEnum</span> <span class='hs-layout'>(</span><span class='hs-varid'>c1</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-92"></a> <span class='hs-keyword'>in</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>d</span> <span class='hs-varop'>>=</span> <span class='hs-num'>0x000080</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>toEnum</span> <span class='hs-varid'>d</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>ds</span>
<a name="line-93"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>ds</span>
<a name="line-94"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>cs</span>
<a name="line-95"></a>
<a name="line-96"></a> <span class='hs-varid'>multi_byte</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Word8</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span>
<a name="line-97"></a> <span class='hs-varid'>multi_byte</span> <span class='hs-varid'>i</span> <span class='hs-varid'>mask</span> <span class='hs-varid'>overlong</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>aux</span> <span class='hs-varid'>i</span> <span class='hs-varid'>cs</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromEnum</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span> <span class='hs-varop'>.&.</span> <span class='hs-varid'>mask</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-98"></a> <span class='hs-keyword'>where</span>
<a name="line-99"></a> <span class='hs-varid'>aux</span> <span class='hs-num'>0</span> <span class='hs-varid'>rs</span> <span class='hs-varid'>acc</span>
<a name="line-100"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>overlong</span> <span class='hs-varop'><=</span> <span class='hs-varid'>acc</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>acc</span> <span class='hs-varop'><=</span> <span class='hs-num'>0x10ffff</span> <span class='hs-varop'>&&</span>
<a name="line-101"></a> <span class='hs-layout'>(</span><span class='hs-varid'>acc</span> <span class='hs-varop'><</span> <span class='hs-num'>0xd800</span> <span class='hs-varop'>||</span> <span class='hs-num'>0xdfff</span> <span class='hs-varop'><</span> <span class='hs-varid'>acc</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span>
<a name="line-102"></a> <span class='hs-layout'>(</span><span class='hs-varid'>acc</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfffe</span> <span class='hs-varop'>||</span> <span class='hs-num'>0xffff</span> <span class='hs-varop'><</span> <span class='hs-varid'>acc</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>chr</span> <span class='hs-varid'>acc</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>rs</span>
<a name="line-103"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>rs</span>
<a name="line-104"></a>
<a name="line-105"></a> <span class='hs-varid'>aux</span> <span class='hs-varid'>n</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-varid'>acc</span>
<a name="line-106"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>r</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0xc0</span> <span class='hs-varop'>==</span> <span class='hs-num'>0x80</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>aux</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>rs</span>
<a name="line-107"></a> <span class='hs-varop'>$</span> <span class='hs-varid'>shiftL</span> <span class='hs-varid'>acc</span> <span class='hs-num'>6</span> <span class='hs-varop'>.|.</span> <span class='hs-varid'>fromEnum</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-108"></a>
<a name="line-109"></a> <span class='hs-varid'>aux</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>rs</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>replacement_character</span> <span class='hs-conop'>:</span> <span class='hs-varid'>decode</span> <span class='hs-varid'>rs</span>
<a name="line-110"></a>
<a name="line-111"></a>
<a name="line-112"></a><a name="utf8Encode"></a><span class='hs-comment'>-- | @utf8Encode str@ is a convenience function; checks to see if</span>
<a name="line-113"></a><span class='hs-comment'>-- @str@ isn't UTF-8 encoded before doing so. Sometimes useful, but</span>
<a name="line-114"></a><span class='hs-comment'>-- you are better off keeping track of the encoding so as to avoid</span>
<a name="line-115"></a><span class='hs-comment'>-- the cost of checking.</span>
<a name="line-116"></a><span class='hs-definition'>utf8Encode</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-117"></a><span class='hs-definition'>utf8Encode</span> <span class='hs-varid'>str</span>
<a name="line-118"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>isUTF8Encoded</span> <span class='hs-varid'>str</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>str</span>
<a name="line-119"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>encodeString</span> <span class='hs-varid'>str</span>
<a name="line-120"></a>
<a name="line-121"></a>
<a name="line-122"></a><a name="isUTF8Encoded"></a><span class='hs-comment'>-- | @isUTF8Encoded str@ tries to recognize input string as being in UTF-8 form.</span>
<a name="line-123"></a><span class='hs-definition'>isUTF8Encoded</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-124"></a><span class='hs-definition'>isUTF8Encoded</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-125"></a><span class='hs-definition'>isUTF8Encoded</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-126"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>ox</span> <span class='hs-keyword'>of</span>
<a name="line-127"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0x80</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>isUTF8Encoded</span> <span class='hs-varid'>xs</span>
<a name="line-128"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'>></span> <span class='hs-num'>0xff</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-129"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xc0</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-130"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xe0</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>check1</span>
<a name="line-131"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xf0</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>check_byte</span> <span class='hs-num'>2</span> <span class='hs-num'>0xf</span> <span class='hs-num'>0</span>
<a name="line-132"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xf8</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>check_byte</span> <span class='hs-num'>3</span> <span class='hs-num'>0x7</span> <span class='hs-num'>0x10000</span>
<a name="line-133"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfc</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>check_byte</span> <span class='hs-num'>4</span> <span class='hs-num'>0x3</span> <span class='hs-num'>0x200000</span>
<a name="line-134"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ox</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfe</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>check_byte</span> <span class='hs-num'>5</span> <span class='hs-num'>0x1</span> <span class='hs-num'>0x4000000</span>
<a name="line-135"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-136"></a> <span class='hs-keyword'>where</span>
<a name="line-137"></a> <span class='hs-varid'>ox</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>toW32</span> <span class='hs-varid'>x</span>
<a name="line-138"></a>
<a name="line-139"></a> <span class='hs-varid'>toW32</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Char</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Word32</span>
<a name="line-140"></a> <span class='hs-varid'>toW32</span> <span class='hs-varid'>ch</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fromIntegral</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromEnum</span> <span class='hs-varid'>ch</span><span class='hs-layout'>)</span>
<a name="line-141"></a>
<a name="line-142"></a> <span class='hs-varid'>check1</span> <span class='hs-keyglyph'>=</span>
<a name="line-143"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>of</span>
<a name="line-144"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-145"></a> <span class='hs-varid'>c1</span> <span class='hs-conop'>:</span> <span class='hs-varid'>ds</span>
<a name="line-146"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>oc</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0xc0</span> <span class='hs-varop'>/=</span> <span class='hs-num'>0x80</span> <span class='hs-varop'>||</span> <span class='hs-varid'>d</span> <span class='hs-varop'><</span> <span class='hs-num'>0x000080</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-147"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>isUTF8Encoded</span> <span class='hs-varid'>ds</span>
<a name="line-148"></a> <span class='hs-keyword'>where</span>
<a name="line-149"></a> <span class='hs-varid'>oc</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>toW32</span> <span class='hs-varid'>c1</span>
<a name="line-150"></a> <span class='hs-varid'>d</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>ox</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x1f</span><span class='hs-layout'>)</span> <span class='hs-varop'>`shiftL`</span> <span class='hs-num'>6</span><span class='hs-layout'>)</span> <span class='hs-varop'>.|.</span> <span class='hs-layout'>(</span><span class='hs-varid'>oc</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span>
<a name="line-151"></a>
<a name="line-152"></a> <span class='hs-varid'>check_byte</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Word32</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Word32</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-153"></a> <span class='hs-varid'>check_byte</span> <span class='hs-varid'>i</span> <span class='hs-varid'>mask</span> <span class='hs-varid'>overlong</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>aux</span> <span class='hs-varid'>i</span> <span class='hs-varid'>xs</span> <span class='hs-layout'>(</span><span class='hs-varid'>ox</span> <span class='hs-varop'>.&.</span> <span class='hs-varid'>mask</span><span class='hs-layout'>)</span>
<a name="line-154"></a> <span class='hs-keyword'>where</span>
<a name="line-155"></a> <span class='hs-varid'>aux</span> <span class='hs-num'>0</span> <span class='hs-varid'>rs</span> <span class='hs-varid'>acc</span>
<a name="line-156"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>overlong</span> <span class='hs-varop'><=</span> <span class='hs-varid'>acc</span> <span class='hs-varop'>&&</span>
<a name="line-157"></a> <span class='hs-varid'>acc</span> <span class='hs-varop'><=</span> <span class='hs-num'>0x10ffff</span> <span class='hs-varop'>&&</span>
<a name="line-158"></a> <span class='hs-layout'>(</span><span class='hs-varid'>acc</span> <span class='hs-varop'><</span> <span class='hs-num'>0xd800</span> <span class='hs-varop'>||</span> <span class='hs-num'>0xdfff</span> <span class='hs-varop'><</span> <span class='hs-varid'>acc</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span>
<a name="line-159"></a> <span class='hs-layout'>(</span><span class='hs-varid'>acc</span> <span class='hs-varop'><</span> <span class='hs-num'>0xfffe</span> <span class='hs-varop'>||</span> <span class='hs-num'>0xffff</span> <span class='hs-varop'><</span> <span class='hs-varid'>acc</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>isUTF8Encoded</span> <span class='hs-varid'>rs</span>
<a name="line-160"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-161"></a>
<a name="line-162"></a> <span class='hs-varid'>aux</span> <span class='hs-varid'>n</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-varid'>acc</span>
<a name="line-163"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>toW32</span> <span class='hs-varid'>r</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0xc0</span> <span class='hs-varop'>==</span> <span class='hs-num'>0x80</span> <span class='hs-keyglyph'>=</span>
<a name="line-164"></a> <span class='hs-varid'>aux</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>rs</span> <span class='hs-layout'>(</span><span class='hs-varid'>acc</span> <span class='hs-varop'>`shiftL`</span> <span class='hs-num'>6</span> <span class='hs-varop'>.|.</span> <span class='hs-layout'>(</span><span class='hs-varid'>toW32</span> <span class='hs-varid'>r</span> <span class='hs-varop'>.&.</span> <span class='hs-num'>0x3f</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-165"></a>
<a name="line-166"></a> <span class='hs-varid'>aux</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-167"></a>
</pre></body>
</html>
|