This is a first run at a patch to support the ord and chr opcodes. It
mainly, I'm afraid, serves as an example to show that we need to be able to
transcode out of the native encoding; I have to special-case it several ways
otherwise.
Limitations
- Ord only works on native strings if they have 8 bit characters or INTVAL
sized characters. (So utf16 probably won't work).
- Both chr and ord assume that the byteorder of a UTF32 string matches the
byteorder of an INTVAL.
-=- James Mastros
Index: core.ops
===================================================================
RCS file: /home/perlcvs/parrot/core.ops,v
retrieving revision 1.13
diff -u -r1.13 core.ops
--- core.ops 2001/10/18 14:14:58 1.13
+++ core.ops 2001/10/19 01:30:15
@@ -1758,6 +1758,53 @@
=cut
+########################################
+
+=item B<chr>(s, i)
+=item B<chr>(s, ic)
+
+Create a single-character string containing the codepoint of the integer.
+(Unicode codepoint, so far only handles the Basic Multilingual Plane.)
+
+=cut
+
+/* ASSUMPTION: The byteorder within a char of a utf32 string is the same as
+ * the byteorder of an intval. */
+AUTO_OP chr(s, i) {
+ $1 = string_make((char *)&$2, sizeof($2), enc_utf32, 0, 0);
+}
+
+########################################
+
+=item B<ord>(i, s)
+=item B<ord>(i, sc)
+
+Gets the codepoint in the native encoding if that is the current encoding,
+else the unicode codepoint (only handles the Basic Multilingual Plane).
+
+=cut
+
+AUTO_OP ord(i, s|sc) {
+ STRING* tempstr=NULL;
+ INTVAL* tempint;
+ tempstr = string_substr($2, 0, 1, &tempstr);
+ if (tempstr->encoding->which == enc_native) {
+ if (tempstr->bufused == 1) {
+ $1 = *(char *)tempstr->bufstart;
+ $1 = $1 & 0xFF;
+ } else if (tempstr->bufused == sizeof(INTVAL)) {
+ $1 = *(INTVAL *)tempstr->bufstart;
+ } else {
+ printf("Character length of %d not supported in ord\n", tempstr->bufused);
+ }
+ } else {
+ if (tempstr->encoding->which != enc_utf32)
+ tempstr =
+Parrot_transcode_table[tempstr->encoding->which][enc_utf32](tempstr, NULL);
+ $1 = *(INTVAL *)tempstr->bufstart & 0xFFFFFFFF;
+ }
+}
+
+
###############################################################################
=head1 COPYRIGHT