NAME

src/pmc/codestring.pmc - CodeString PMC Class

DESCRIPTION

CodeString is a class intended to simplify the process of emitting code strings. Ideally this will eventually become a form of "CodeBuffer" that is more efficient than string concatenation, but for now it works well enough for me.

The primary method for CodeString objects is emit, which appends a line (or lines) of code to the string according to a format parameter. The line can contain substitution markers (ala printf) that indicate where other parameters to the call should be placed.

Note that CodeString is just a subclass of Parrot's native String class, so it's easy to combine CodeString objects with other strings outside of the emit method.

Methods

emit(string fmt [, pmc args ] [, pmc hash ])
Add a line to a CodeString object according to fmt. The fmt string can contain any number of "%-replacements" which are replaced by the corresponding values from args or hash prior to being appended to the string. (Here args is a slurpy array, and hash is a slurpy hash.)The currently defined replacements include:
    %0 %1 ... %9     the value from the args array at index 0..9
    %,               the values of the args array separated by commas
    %%               a percent sign
A percent-sign followed by any other character that is a hash key receives the value of the hash element.A newline is automatically added to the end of the fmt.
lineof(INTVAL pos)
Return the line number of the line at offset pos. This code assumes that the first line is line number zero.*/
  METHOD lineof(INTVAL pos) {
    STRING *str  = SELF.get_string();
    INTVAL line  = 0;
    INTVAL ipos  = 0;
    INTVAL jpos;

    jpos = Parrot_str_find_cclass(INTERP, enum_cclass_newline, str, ipos, pos);
    while (jpos < pos) {
        line++;
        ipos = jpos + 1;
        /* treat \r\n as a single line separator */
        ipos += (string_ord(INTERP, str, jpos) == 13
                 && string_ord(INTERP, str, jpos+1) == 10);
        jpos = Parrot_str_find_cclass(INTERP, enum_cclass_newline, str, ipos, pos);
    }
    RETURN(INTVAL line);
}
/*
unique([string fmt])
Each call to unique returns a unique number, or if a fmt parameter is given it returns a unique string beginning with fmt. (This may eventually be generalized to allow uniqueness anywhere in the string.) The function starts counting at 10 (so that the values 0..9 can be considered "safe").
escape(string str)
Returns an escaped value of str suitable for including in PIR. If the string contains any non-ASCII characters, then it's prefixed with 'unicode:'.
charname_to_ord(string name)
Converts the Unicode character name given by name to its codepoint value. Returns -1 if an error occurs in conversion.*/
  METHOD charname_to_ord(STRING *name) {
#if PARROT_HAS_ICU
    UChar32    codepoint;
    UErrorCode err       = U_ZERO_ERROR;
    char       *cstr     = Parrot_str_to_cstring(INTERP, name);
    codepoint = u_charFromName(U_EXTENDED_CHAR_NAME, cstr, &err);
    Parrot_str_free_cstring(cstr);
    if (U_SUCCESS(err)) {
        RETURN(INTVAL codepoint);
    }
    RETURN(INTVAL -1);
#else
    Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LIBRARY_ERROR,
        "no ICU lib loaded");
#endif
  }
/*
key( string name1 [, string name2, ...] )
Construct a PIR key using the strings passed as arguments. For example, key('Foo', 'Bar') returns ["Foo";"Bar"].