Skip to content

Instantly share code, notes, and snippets.

@masak
Created May 27, 2009 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/118700 to your computer and use it in GitHub Desktop.
Save masak/118700 to your computer and use it in GitHub Desktop.
$ git diff
diff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index 2aa70e9..578d050 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -1,4 +1,43 @@
class Any is also {
+ our Int multi method bytes() is export {
+ Q:PIR {
+ .local string str
+ .local pmc chars
+ .local pmc bytes
+ .local int count
+ .local string nextchar
+ .local int charcode
+ .local string hexstring
+ .local int hexstringlength
+ .local string firsttwo
+ str = self
+ chars = split '', str
+ bytes = new 'FixedIntegerArray'
+ set bytes, 1
+ count = 0
+ loop:
+ unless bytes goto done
+ nextchar = shift chars
+ charcode = ord nextchar
+ bytes[0] = charcode
+ hexstring = sprintf '%08x', bytes
+ nibble:
+ hexstringlength = length hexstring
+ if hexstringlength == 2 goto inc_length
+ firsttwo = substr hexstring, 0, 2
+ unless firsttwo == '00' goto inc_length
+ hexstring = substr hexstring, 2
+ goto nibble
+ inc_length:
+ hexstringlength = length hexstring
+ count = count + hexstringlength
+ goto loop
+ done:
+ count = count / 2
+ %r = box count
+ }
+ }
+
our Str multi method capitalize() is export {
self.lc.subst(/\w+/, { .ucfirst }, :global)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment