Skip to content

Commit 9370560

Browse files
committed
reorganize + general improvements
1 parent 3f5f088 commit 9370560

20 files changed

Lines changed: 132 additions & 38 deletions

File tree

count-characters/perl5.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Count characters of a string in Perl 5
2+
3+
## Code points
4+
5+
The core `length` function returns the number of code points when called on a
6+
character string (instead of a byte string).
7+
8+
$count = length $str;
9+
10+
## Grapheme clusters
11+
12+
There is no core function to count the number of grapheme clusters; however,
13+
either of the following examples will perform the task.
14+
15+
$count = () = $str =~ /\X/g;
16+
17+
$count++ while $str =~ /\X/g;
18+
19+
The CPAN module
20+
[Unicode::GCString](https://metacpan.org/module/Unicode::GCString) can also be
21+
used on character strings.
22+
23+
use Unicode::GCString;
24+
25+
$count = Unicode::GCString->new($str)->length;
26+
27+
As well as the CPAN module
28+
[Unicode::Util](Unicode::GCStrin://metacpan.org/module/Unicode::Util).
29+
30+
use Unicode::Util qw( grapheme_length );
31+
32+
$count = grapheme_length($str);

count-characters/perl6.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Count characters of a string in Perl 6
2+
3+
## Code points
4+
5+
The `codes` method is available for strings (`Str` objects).
6+
7+
$count = $str.codes;
8+
9+
## Grapheme clusters
10+
11+
The `graphemes` method is available for strings (`Str` objects).
12+
13+
$count = $str.graphemes;

count-characters/php.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Count characters of a string in PHP
2+
3+
## Code points
4+
5+
The `mb_strlen` function is available for strings in PHP 4.0.6 and requires the
6+
encoding of the string to either passed as the second argument or set in
7+
`mbstring.internal_encoding`.
8+
9+
$count = mb_strlen($str, 'UTF-8');
10+
11+
## Grapheme clusters
12+
13+
The `grapheme_strlen` function is available for UTF-8 strings in PHP 5.3.0 or
14+
the PECL extension [intl](http://pecl.php.net/package/intl).
15+
16+
$count = grapheme_strlen($str);

count-codepoints/perl5.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

count-codepoints/perl6.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

encode-decode/perl6.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Encode and decode in Perl 6
22

3+
UTF-8 is the default.
4+
35
$utf8 = $str.encode;
46
$str = $utf8.decode;
57

6-
or
8+
Other encodings can be explicitly specified.
79

8-
$utf8 = $str.encode('UTF-8');
9-
$str = $utf8.decode('UTF-8');
10+
$utf16 = $str.encode('UTF-16');
11+
$str = $utf16.decode('UTF-16');

io/perl5.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
# I/O
1+
# I/O encoding in Perl 5
22

3-
## everything
3+
## All I/O
4+
5+
All standard streams and filehandles.
46

57
use open qw( :encoding(UTF-8) :std );
68

7-
## input
9+
## Standard streams
810

9-
open my $fh, '<:encoding(UTF-8)', $file;
11+
binmode STDIN, ':encoding(UTF-8)';
12+
binmode STDOUT, ':encoding(UTF-8)';
13+
binmode STDERR, ':encoding(UTF-8)';
1014

11-
## output
15+
## Filehandle input
1216

13-
open my $fh, '>:encoding(UTF-8)', $file;
17+
open my $fh, '<:encoding(UTF-8)', $file;
1418

15-
## standard streams
19+
## Filehandle output
1620

17-
binmode STDIN, ':encoding(UTF-8)';
18-
binmode STDOUT, ':encoding(UTF-8)';
19-
binmode STDERR, ':encoding(UTF-8)';
21+
open my $fh, '>:encoding(UTF-8)', $file;

io/perl6.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
# I/O
1+
# I/O encoding in Perl 6
22

3-
## input
3+
All I/O is UTF-8 by default.
44

5-
my $fh = open $file, :r, :enc<UTF-8>;
5+
## Standard streams
66

7-
## output
7+
$*IN.encoding = 'UTF-16';
8+
$*OUT.encoding = 'UTF-16';
9+
$*ERR.encoding = 'UTF-16';
810

9-
my $fh = open $file, :w, :enc<UTF-8>;
11+
## Filehandle input
1012

11-
## standard streams
13+
my $fh = open $file, :r, :enc<UTF-16>;
1214

13-
$*IN.encoding = 'UTF-8';
14-
$*OUT.encoding = 'UTF-8';
15-
$*ERR.encoding = 'UTF-8';
15+
## Filehandle output
16+
17+
my $fh = open $file, :w, :enc<UTF-16>;

normalization/javascript.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Unicode normalization in JavaScript
2+
13
var unorm = require('unorm');
24

35
nfd = unorm.nfd(str);

normalization/perl5.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Unicode normalization in Perl 5
2+
13
use Unicode::Normalize;
24

35
$nfd = NFD($str);

0 commit comments

Comments
 (0)