1<p align="center">
2  <img src="https://static.hoa-project.net/Image/Hoa.svg" alt="Hoa" width="250px" />
3</p>
4
5---
6
7<p align="center">
8  <a href="https://travis-ci.org/hoaproject/ustring"><img src="https://img.shields.io/travis/hoaproject/ustring/master.svg" alt="Build status" /></a>
9  <a href="https://coveralls.io/github/hoaproject/ustring?branch=master"><img src="https://img.shields.io/coveralls/hoaproject/ustring/master.svg" alt="Code coverage" /></a>
10  <a href="https://packagist.org/packages/hoa/ustring"><img src="https://img.shields.io/packagist/dt/hoa/ustring.svg" alt="Packagist" /></a>
11  <a href="https://hoa-project.net/LICENSE"><img src="https://img.shields.io/packagist/l/hoa/ustring.svg" alt="License" /></a>
12</p>
13<p align="center">
14  Hoa is a <strong>modular</strong>, <strong>extensible</strong> and
15  <strong>structured</strong> set of PHP libraries.<br />
16  Moreover, Hoa aims at being a bridge between industrial and research worlds.
17</p>
18
19# Hoa\Ustring
20
21[![Help on IRC](https://img.shields.io/badge/help-%23hoaproject-ff0066.svg)](https://webchat.freenode.net/?channels=#hoaproject)
22[![Help on Gitter](https://img.shields.io/badge/help-gitter-ff0066.svg)](https://gitter.im/hoaproject/central)
23[![Documentation](https://img.shields.io/badge/documentation-hack_book-ff0066.svg)](https://central.hoa-project.net/Documentation/Library/Ustring)
24[![Board](https://img.shields.io/badge/organisation-board-ff0066.svg)](https://waffle.io/hoaproject/ustring)
25
26This library allows to manipulate UTF-8 strings easily with some search
27algorithms.
28
29[Learn more](https://central.hoa-project.net/Documentation/Library/Ustring).
30
31## Installation
32
33With [Composer](https://getcomposer.org/), to include this library into
34your dependencies, you need to
35require [`hoa/ustring`](https://packagist.org/packages/hoa/ustring):
36
37```sh
38$ composer require hoa/ustring '~4.0'
39```
40
41For more installation procedures, please read [the Source
42page](https://hoa-project.net/Source.html).
43
44## Testing
45
46Before running the test suites, the development dependencies must be installed:
47
48```sh
49$ composer install
50```
51
52Then, to run all the test suites:
53
54```sh
55$ vendor/bin/hoa test:run
56```
57
58For more information, please read the [contributor
59guide](https://hoa-project.net/Literature/Contributor/Guide.html).
60
61## Quick usage
62
63We propose a quick overview of two usages: manipulate UTF-8 strings and one
64search algorithm.
65
66### Natural UTF-8 strings manipulation
67
68The `Hoa\Ustring\Ustring` class allows to manipulate easily UTF-8 strings in a
69very natural way. This class implements the `\ArrayAccess`, `\Countable` and
70`\IteratorAggregate` interfaces. We will use the following examples:
71
72```php
73$french   = new Hoa\Ustring\Ustring('Je t\'aime');
74$arabic   = new Hoa\Ustring\Ustring('أحبك');
75$japanese = new Hoa\Ustring\Ustring('私はあなたを愛して');
76```
77
78To get the first character, we will do:
79
80```php
81var_dump(
82    $french[0],  // string(1) "J"
83    $arabic[0],  // string(2) "أ"
84    $japanese[0] // string(3) "私"
85);
86```
87
88And to get the last character, we will do `[-1]`. It supports unbounded (and
89modulo) indexes.
90
91We note that it cares about text **direction**. Look at `$arabic[0]`, it returns
92`أ` and not `ك`. To get the direction, we can use the
93`Hoa\Ustring\Ustring::getDirection` method (which call the
94`Hoa\Ustring\Ustring::getCharDirection` static method), it returns either
95`Hoa\Ustring\Ustring::LTR` (`0`) or `Hoa\Ustring\Ustring::RTL` (`1`):
96
97```php
98var_dump(
99    $french->getDirection(),  // int(0)
100    $arabic->getDirection(),  // int(1)
101    $japanese->getDirection() // int(0)
102);
103```
104
105Text direction is also important for the `append`, `prepend`, `pad`… methods on
106`Hoa\Ustring\Ustring` for example.
107
108To get the length of a string, we can use the `count` function:
109
110```php
111var_dump(
112    count($french),  // int(9)
113    count($arabic),  // int(4)
114    count($japanese) // int(9)
115);
116```
117
118We are also able to iterate over the string:
119
120```php
121foreach ($arabic as $letter) {
122    var_dump($letter);
123}
124
125/**
126 * Will output:
127 *     string(2) "أ"
128 *     string(2) "ح"
129 *     string(2) "ب"
130 *     string(2) "ك"
131 */
132```
133
134Again, text direction is useful here. For `$arabic`, the iteration is done from
135right to left.
136
137Some static methods are helpful, such as `fromCode`, `toCode` or `isUtf8` on
138`Hoa\Ustring\Ustring`:
139
140```php
141var_dump(
142    Hoa\Ustring\Ustring::fromCode(0x1a9), // string(2) "Ʃ"
143    Hoa\Ustring\Ustring::toCode('Ʃ'),     // int(425) == 0x1a9
144    Hoa\Ustring\Ustring::isUtf8('Ʃ')      // bool(true)
145);
146```
147
148We can also transform any text into ASCII:
149
150```php
151$emoji = new Hoa\Ustring\Ustring('I ❤ Unicode');
152$maths = new Hoa\Ustring\Ustring('∀ i ∈ ℕ');
153
154echo
155    $emoji->toAscii(), "\n",
156    $maths->toAscii(), "\n";
157
158/**
159 * Will output:
160 *     I (heavy black heart) Unicode
161 *     (for all) i (element of) N
162 */
163```
164
165### Search algorithm
166
167The `Hoa\Ustring\Search` implements search algorithms on strings.
168
169For example, the `Hoa\Ustring\Search::approximated` method make a search by
170approximated patterns (with *k* differences based upon the principle diagonal
171monotony). If we search the word `GATAA` in `CAGATAAGAGAA` with 1 difference, we
172will do:
173
174```php
175$search = Hoa\Ustring\Search::approximated(
176    $haystack = 'CAGATAAGAGAA',
177    $needle   = 'GATAA',
178    $k        = 1
179);
180$solutions = array();
181
182foreach ($search as $pos) {
183    $solutions[] = substr($haystack, $pos['i'], $pos['l']);
184}
185```
186
187We will found `AGATA`, `GATAA`, `ATAAG` and `GAGAA`.
188
189The result is not very handy but the algorithm is much optimized and found many
190applications.
191
192## Documentation
193
194The
195[hack book of `Hoa\Ustring`](https://central.hoa-project.net/Documentation/Library/Ustring) contains
196detailed information about how to use this library and how it works.
197
198To generate the documentation locally, execute the following commands:
199
200```sh
201$ composer require --dev hoa/devtools
202$ vendor/bin/hoa devtools:documentation --open
203```
204
205More documentation can be found on the project's website:
206[hoa-project.net](https://hoa-project.net/).
207
208## Getting help
209
210There are mainly two ways to get help:
211
212  * On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)
213    IRC channel,
214  * On the forum at [users.hoa-project.net](https://users.hoa-project.net).
215
216## Contribution
217
218Do you want to contribute? Thanks! A detailed [contributor
219guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains
220everything you need to know.
221
222## License
223
224Hoa is under the New BSD License (BSD-3-Clause). Please, see
225[`LICENSE`](https://hoa-project.net/LICENSE) for details.
226