#!/usr/bin/perl
######################################################################
# eg/ko/mb_length.pl - mbë¡ ë¬¸ì ìì ë°ì´í¸ ì ë¹êµ
#
# ë¬´ìì ë³´ì¬ì£¼ë:
#   CORE length()ë ì¥í(ë°ì´í¸)ë¥¼ ì¸ê³ , mb::length()ë ì íë
#   ì¤í¬ë¦½í¸ ì¸ì½ë© ê¸°ì¤ì¼ë¡ ë©í°ë°ì´í¸ ë¬¸ì íëë¥¼ 1ë¡ ì¼ë¤.
#   mb::substr()ì mb::index()ë ë¬¸ì ë¨ìë¡ ëìíë¯ë¡ ë ë°ì´í¸
#   ë¬¸ìê° ì¤ê°ìì ìë¦¬ì§ ìëë¤.
#
# COREìì ì°¨ì´:
#   length("\x82\xA0")ë 2(ë°ì´í¸)ì§ë§ mb::length("\x82\xA0")ë 1
#   (Shift_JIS íë¼ê°ë íë).
#
# Ì¸ê³ : ìì¤ì \xHH ë°ì´í°ë US-ASCII ê·¸ëë¡ì´ê³ , ì´ íì¼ì
# UTF-8ìëë¤(ì£¼ìë§ íêµ­ì´ë¡ íì§í).
#
#     perl eg/ko/mb_length.pl
#
######################################################################
use strict;
use vars qw($aiu $byte_len $char_len $head $tail $pos);

use FindBin;
use lib "$FindBin::Bin/../../lib";
use mb;
mb::set_script_encoding('sjis');

# Shift_JIS íë¼ê°ë 3ê°, ëª¨ë 6ë°ì´í¸:
#     \x82\xA0  a   \x82\xA2  i   \x82\xA4  u
$aiu = "\x82\xA0\x82\xA2\x82\xA4";

# CORE length()ë ë°ì´í¸ë¥¼, mb::length()ë ë¬¸ìë¥¼ ì¼ë¤.
$byte_len = length($aiu);
$char_len = mb::length($aiu);
print "byte length      : $byte_len\n";   # 6
print "character length : $char_len\n";   # 3

# mb::substr()ë ë¬¸ì ë¨ìë¡ ìë¼ë´ë¯ë¡ ë ë°ì´í¸ ë¬¸ìê° ë°ì¼ë¡
# ìë¦¬ì§ ìëë¤. ì²ì ë ë¬¸ìë ì íí 4ë°ì´í¸ ë¬¸ìì´ì´ë¤.
$head = mb::substr($aiu, 0, 2);
$tail = mb::substr($aiu, 2);
print "first 2 chars    : ", length($head), " bytes\n";   # 4
print "remaining chars  : ", length($tail), " bytes\n";   # 2

# mb::index()ë ìì¹ë¥¼ ë°ì´í¸ê° ìë ë¬¸ìë¡ ë³´ê³ íë¤. ì¸ ë²ì§¸
# ë¬¸ìë ë°ì´í¸ 4ìì ììíì§ë§ ë¬¸ì ì¸ë±ì¤ë¡ë 2ì´ë¤.
$pos = mb::index($aiu, "\x82\xA4");
print "index of 3rd char: $pos\n";        # 2

exit 0;
