Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@samcv
Created January 11, 2017 09:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samcv/f385f19111df9ad282472a6d3610fc6f to your computer and use it in GitHub Desktop.
Save samcv/f385f19111df9ad282472a6d3610fc6f to your computer and use it in GitHub Desktop.
# Gets a string from a Unicode codepoint or other name
sub getstrfromname ( str $name ) {
my int $cp := nqp::codepointfromname($name);
return $cp < 0 ?? "" !! nqp::chr($cp);
}
@arnsholt
Copy link

The Java op version here should be (I'd add it to Ops.java below codepointfromname, line 4200 or so):

public static String (String name) {
    long cp = codepointfromname(name);
    /* I've inlined nqp::chr here, since it takes an additional ThreadContext argument
     * (so that it can throw an exception on cp < 0) which we don't need. */
    return cp < 0? "" : (new StringBuffer()).append(Character.toChars((int)ord)).toString();
}

You make it available to the compiler by adding a mapping to src/vm/jvm/QAST/Compiler.nqp (I'd add it after the mapping for codepointfromname, again):

QAST::OperationsJAST.map_classlib_core_op('getstrfromname', $TYPE_OPS, 'getstrfromname', [$RT_STR], $RT_STR);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment