/*
Copyright (C) 2008, The Perl Foundation.
$Id: /mirror/trunk/languages/pipp/src/pmc/phpstring.pmc 30457 2008-08-21T17:01:26.101883Z cotto  $

=head1 NAME

src/pmc/phpstring.pmc - PHP String

=head1 DESCRIPTION

=head2 Methods

=over 4

=cut

*/

#include "php_private.h"

pmclass PhpString
    extends  String
    provides scalar
    provides string
    dynpmc
    group    php_group
    hll      PHP
    maps     String {

/*

=item C<STRING* name()>

Return the string 'string'.

=cut

*/
    STRING* name() {
        return const_string(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   = string_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;
            }
        }

        string_cstring_free(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  = string_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);
        string_cstring_free(s1);
        RETURN(PMC *retval);
    }

/*

=item C<PMC* to_number()>

=cut

*/
    METHOD PMC* to_number() {
        PMC          *retval;
        STRING       *rep = SELF.get_string();
        char         *s   = string_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);
            PMC_int_val(retval) = i;
        }
        else {
            FLOATVAL d = strtod(s1, &s2);

            if (s1 != s2 && (*s2 == '\0' || isspace((unsigned char)*s2))) {
                retval = pmc_new(INTERP, dynpmc_PhpFloat);
                PMC_num_val(retval) = d;
            }
            else {
                retval = pmc_new(INTERP, dynpmc_PhpInteger);
                PMC_int_val(retval) = 0;
            }
        }

        string_cstring_free(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 = string_printf(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:
 */
