Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This week saw the addition of ByteView, a stopgap PMC that allows us access to the bytes in a Parrot string, pending the Parrot people's long-term solution. Thanks to it, we now do string encoding of UTF-8 strings right.
- Loading branch information
Carl Masak
committed
Jun 6, 2010
1 parent
841262f
commit 359dbdf
Showing
5 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
$Id$ | ||
Copyright (C) 2010, The Perl Foundation. | ||
|
||
=head1 NAME | ||
|
||
src/pmc/byteview.pmc - PMC providing a byte view of a string | ||
|
||
=head1 DESCRIPTION | ||
|
||
The ByteView PMC provides a byte view of a string. It lazily just looks at the | ||
bytes of the string if it is only used in a readonly way; if anybody tries to | ||
set a value, however, it will then make a mutable copy that it can work with, | ||
thus not affecting the original Parrot STRING* (since those are immutable, plus | ||
avoiding action at a distance is a Good Thing). | ||
|
||
=head2 Methods | ||
|
||
=cut | ||
|
||
*/ | ||
|
||
#include "parrot/parrot.h" | ||
|
||
pmclass ByteView dynpmc group perl6_group auto_attrs { | ||
ATTR STRING *the_string; | ||
ATTR unsigned char *bytes; | ||
ATTR INTVAL length; | ||
|
||
VTABLE void init() { | ||
/* Need custom mark. */ | ||
PObj_custom_mark_SET(SELF); | ||
} | ||
|
||
VTABLE void mark() { | ||
STRING *the_string; | ||
GET_ATTR_the_string(INTERP, SELF, the_string); | ||
if (the_string) | ||
Parrot_gc_mark_STRING_alive(INTERP, the_string); | ||
} | ||
|
||
VTABLE INTVAL elements() { | ||
STRING *the_string; | ||
GET_ATTR_the_string(INTERP, SELF, the_string); | ||
if (the_string) { | ||
return the_string->bufused; | ||
} | ||
else { | ||
INTVAL length; | ||
GET_ATTR_length(INTERP, SELF, length); | ||
return length; | ||
} | ||
} | ||
|
||
VTABLE void set_string_native(STRING *new_string) { | ||
/* Put the string in place and invalidate any byte array we may have. */ | ||
SET_ATTR_the_string(INTERP, SELF, new_string); | ||
SET_ATTR_bytes(INTERP, SELF, NULL); | ||
SET_ATTR_length(INTERP, SELF, 0); | ||
} | ||
|
||
VTABLE INTVAL get_integer_keyed_int(INTVAL key) { | ||
STRING *the_string; | ||
GET_ATTR_the_string(INTERP, SELF, the_string); | ||
if (the_string) { | ||
/* We're looking into the string. */ | ||
if (key < the_string->bufused) | ||
return (INTVAL)(unsigned char)the_string->strstart[key]; | ||
} | ||
else { | ||
/* Need to look at our underlying byte array instead. */ | ||
unsigned char *bytes; | ||
INTVAL length; | ||
GET_ATTR_bytes(INTERP, SELF, bytes); | ||
GET_ATTR_length(INTERP, SELF, length); | ||
if (bytes && key < length) | ||
return (INTVAL)bytes[key]; | ||
} | ||
return 0; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Local variables: | ||
* c-file-style: "parrot" | ||
* End: | ||
* vim: expandtab shiftwidth=4: | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters