Skip to content
This repository
tag: v1517
Fetching contributors…

Cannot retrieve contributors at this time

file 171 lines (136 sloc) 3.967 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(**
Server side implementation of Cactutf.
Encoding of unicode characters
@author Corentin Gallet
@author Rudy Sicard
@author Mathieu Barbin (documentation)
*)

(**
CactUTF is a `light version' of Camomile, the popular Ocaml library often
used for its full support of Unicode. Sadly, there are two problems with
it :
- Camomile is big. Really big.
- Camomile has to be installed.

So, here comes Cactutf, with only the functions needed.
It's a plain translation of EucalyptUTF into Ocaml.

You may note that this implementation only used up to 4 bytes for one
Unicode character, where Camomile goes as far as 6. That's because
they go further than the RFC 3629.
*)

(**
{6 Types alias}
*)

(**
There is at least 3 different int manipulated, so these types
alias try to reduce confusion
*)

(**
The representation of unicode char
*)
type unicode = int

(**
The indexation in unicode char, independant from the implementation
*)
type unicode_index = int

(**
The indexation in bytes
*)
type bytes_index = int

(**
{6 Indexation and length}
*)

(**
For one Unicode code, return the number of bytes needed for
a representation in UTF-8.
@raises Lenbytes if the code is invalid
*)
val lenbytes : unicode -> bytes_index

(**
[Cactutf.length_until string pos]
Returns the number of unicode characters encoded in the string
until the position [pos] given in [bytes]
*)
val length_until : string -> bytes_index -> unicode_index

(**
Returns the number of unicode characters encoded in the string.
This returns the same result as [Cactutf.length_until s (String.length s)]
*)
val length : string -> unicode_index

(**
Return the index in bytes of the n-th Unicode character.
*)
val nth : string -> unicode_index -> bytes_index

(**
[Cactutf.next string pos]
Return the index of the next Unicode character.
<!> Silently returns [pos+1] in case of error.
*)
val next : string -> bytes_index -> bytes_index

(**
{6 Unicode}
*)

(**
Unicode char can be encoded with 1 up to 4 bytes.
Theses function encode unicode from there bytes.
*)

val one_byte : int -> unicode
val two_bytes : int -> int -> unicode
val three_bytes : int -> int -> int -> unicode
val four_bytes : int -> int -> int -> int -> unicode

(**
{6 Access}
*)

(**
Return the Unicode code of the nth Unicode character.
*)
val get : string -> unicode_index -> unicode

(**
Return the Unicode code using the index (and not the nth).
A lot faster, but only when using index instead of position.
*)
val look : string -> bytes_index -> unicode

(**
{6 Allocation}
*)

(**
Build a new string from a character.
*)
val cons : unicode -> string

(**
{6 Extraction, Transformation}
*)

(**
<!> This is weird, the length given for [sub] is the length in bytes,
not in number of unicode characters.
*)

val sub : string -> bytes_index -> int -> string

val sub_opt : string -> bytes_index -> int -> string option

(**
uppercase the string
*)
val uppercase : string -> string

(**
lowercase the string
*)
val lowercase : string -> string


(**
{6 Deprecated}
*)

(**
FIXME: undocumented, incorrect, dirty, not following guidelines.
This exception should not be exported, and goes not out this module.
*)
exception Lenbytes of int
Something went wrong with that request. Please try again.