Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6a4daaa9fb
Fetching contributors…

Cannot retrieve contributors at this time

203 lines (140 sloc) 3.796 kB
/*
Copyright (C) 2008, The Perl Foundation.
=head1 NAME
src/pmc/phpstring.pmc - PHP String
=head1 DESCRIPTION
=head2 Methods
=over 4
=cut
*/
#include "pipp_private.h"
pmclass PhpString
extends String
provides scalar
provides string
dynpmc
group pipp_group
hll PHP
maps String {
/*
=item C<STRING* name()>
Return the string 'string'.
=cut
*/
VTABLE STRING* name() {
return Parrot_str_new_constant(INTERP, "string");
}
/*
=back
=head2 non-Vtable Methods
=over 4
=cut
*/
/*
=back
=head2 Specific Methods
=over 4
=item C<INTVAL is_numeric(INTVAL allow_errors)>
=cut
*/
METHOD INTVAL is_numeric(INTVAL allow_errors) {
INTVAL retval = 0;
STRING *rep = SELF.get_string();
char *s = Parrot_str_to_cstring(INTERP, rep);
char *s1, *s2;
INTVAL i;
s1 = s;
while (isspace((unsigned char)*s1)) s1++;
if (*s1 == '0' && (s1[1] == 'X' || s1[1] == 'x')) {
s1 += 2;
i = strtol(s1, &s2, 16);
}
else
i = strtol(s1, &s2, 10);
if (s1 != s2 && (*s2 == '\0' || (allow_errors && isspace((unsigned char)*s2)))) {
retval = 1;
}
else {
FLOATVAL d = strtod(s1, &s2);
if (s1 != s2 && (*s2 == '\0' || (allow_errors && isspace((unsigned char)*s2)))) {
retval = 1;
}
}
Parrot_str_free_cstring(s);
RETURN(INTVAL retval);
}
/*
=item C<PMC* to_base(INTVAL base)>
=cut
*/
METHOD PMC* to_base(INTVAL base) {
PMC *retval;
STRING *rep = SELF.get_string();
char *s1 = Parrot_str_to_cstring(INTERP, rep);
char *s2;
unsigned long n = strtoul(s1, &s2, base);
retval = pmc_new(INTERP, dynpmc_PhpInteger);
VTABLE_set_integer_native(INTERP, retval, n);
Parrot_str_free_cstring(s1);
RETURN(PMC *retval);
}
/*
=item C<PMC* to_number()>
=cut
*/
METHOD PMC* to_number() {
PMC *retval;
STRING *rep = SELF.get_string();
char *s = Parrot_str_to_cstring(INTERP, rep);
char *s1, *s2;
INTVAL i;
s1 = s;
while (isspace((unsigned char)*s1)) s1++;
if (*s1 == '0' && (s1[1] == 'X' || s1[1] == 'x')) {
s1 += 2;
i = strtol(s1, &s2, 16);
}
else
i = strtol(s1, &s2, 10);
if (s1 != s2 && (*s2 == '\0' || isspace((unsigned char)*s2))) {
retval = pmc_new(INTERP, dynpmc_PhpInteger);
VTABLE_set_integer_native(INTERP, retval, i);
}
else {
FLOATVAL d = strtod(s1, &s2);
if (s1 != s2 && (*s2 == '\0' || isspace((unsigned char)*s2))) {
retval = pmc_new(INTERP, dynpmc_PhpFloat);
VTABLE_set_number_native(INTERP, retval, d);
}
else {
retval = pmc_new(INTERP, dynpmc_PhpInteger);
VTABLE_set_integer_native(INTERP, retval, i);
}
}
Parrot_str_free_cstring(s);
RETURN(PMC *retval);
}
/*
=item C<STRING* serialize()>
Return a representation of this string in the same format as PHP's serialize function.
=cut
*/
METHOD STRING* serialize() {
STRING *serialized;
serialized = Parrot_str_format_data(INTERP, "s:%d:\"%Ss\";", VTABLE_elements(INTERP, SELF),
VTABLE_get_string(INTERP, SELF));
RETURN(STRING *serialized);
}
}
/*
=back
=head1 AUTHORS
Francois Perrad.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/
Jump to Line
Something went wrong with that request. Please try again.