Encode::JavaScript::UCS - JavaScript unicode character encoding


Encode-JavaScript-UCS documentation Contained in the Encode-JavaScript-UCS distribution.

Index


Code Index:

NAME

Top

Encode::JavaScript::UCS - JavaScript unicode character encoding

SYNOPSIS

Top

  use Encode::JavaScript::UCS;

  my $name = "\x{5BAE}\x{5DDD}\x{9054}\x{5F66}";
  my $escaped = encode("JavaScript-UCS", $name); # \u5bar\u5ddd\u9054\u5f66

DESCRIPTION

Top

Encode::JavaScript::UCS is an Encoding module to represent JavaScript unicode characters like "\u5bae".

AUTHOR

Top

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

Encode


Encode-JavaScript-UCS documentation Contained in the Encode-JavaScript-UCS distribution.

package Encode::JavaScript::UCS;
use strict;
use 5.8.1;
our $VERSION = '0.01';

use base qw(Encode::Encoding);
use Encode 2.12 (); # for callbacks

__PACKAGE__->Define('JavaScript-UCS');

sub decode($$;$){
    my ($obj, $buf, $chk) = @_;
    $buf =~ s/\\u([0-9a-f]{4})/chr(hex($1))/eig;
    $_[1] = '' if $chk; # this is what in-place edit means
    return $buf;
}

sub encode($$;$){
    my ($obj, $str, $chk) = @_;
    $str = Encode::encode("ascii", $str, sub { sprintf("\\u%04x", $_[0]) });
    $_[1] = '' if $chk; # this is what in-place edit means
    return $str;
}

1;
__END__