{"id":91,"date":"2009-08-26T03:45:15","date_gmt":"2009-08-26T03:45:15","guid":{"rendered":"http:\/\/yuguangzhang.com\/blog\/?p=91"},"modified":"2009-08-26T03:45:15","modified_gmt":"2009-08-26T03:45:15","slug":"shifting-unicode-codepoints","status":"publish","type":"post","link":"http:\/\/yuguangzhang.com\/blog\/shifting-unicode-codepoints\/","title":{"rendered":"Shifting Unicode Codepoints"},"content":{"rendered":"<p>While working on a word search generator that translated letter symbols (sort of like cryptograms, except this one&#8217;s impossible to solve without the key), php ran out of memory on a rather ordinary function.<br \/>\n[cc lang=&#8221;php&#8221;]<br \/>\nfunction uniord($c) {<br \/>\n    $h = ord($c{0});<br \/>\n    if ($h &lt;= 0x7F) {<br \/>\n        return $h;<br \/>\n    } else if ($h &lt; 0xC2) {<br \/>\n        return false;<br \/>\n    } else if ($h &lt;= 0xDF) {<br \/>\n        return ($h &amp; 0x1F) &lt;&lt; 6 | (ord($c{1}) &amp; 0x3F);<br \/>\n    } else if ($h &lt;= 0xEF) {<br \/>\n        return ($h &amp; 0x0F) &lt;&lt; 12 | (ord($c{1}) &amp; 0x3F) &lt;&lt; 6<br \/>\n                                 | (ord($c{2}) &amp; 0x3F);<br \/>\n    } else if ($h &lt;= 0xF4) {<br \/>\n        return ($h &amp; 0x0F) &lt;&lt; 18 | (ord($c{1}) &amp; 0x3F) &lt;&lt; 12<br \/>\n                                 | (ord($c{2}) &amp; 0x3F) &lt;&lt; 6<br \/>\n                                 | (ord($c{3}) &amp; 0x3F);<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n}<br \/>\nfunction unichr($c) {<br \/>\n    if ($c &lt;= 0x7F) {<br \/>\n        return chr($c);<br \/>\n    } else if ($c &lt;= 0x7FF) {<br \/>\n        return chr(0xC0 | $c &gt;&gt; 6) . chr(0x80 | $c &amp; 0x3F);<br \/>\n    } else if ($c &lt;= 0xFFFF) {<br \/>\n        return chr(0xE0 | $c &gt;&gt; 12) . chr(0x80 | $c &gt;&gt; 6 &amp; 0x3F)<br \/>\n                                    . chr(0x80 | $c &amp; 0x3F);<br \/>\n    } else if ($c &lt;= 0x10FFFF) {<br \/>\n        return chr(0xF0 | $c &gt;&gt; 18) . chr(0x80 | $c &gt;&gt; 12 &amp; 0x3F)<br \/>\n                                    . chr(0x80 | $c &gt;&gt; 6 &amp; 0x3F)<br \/>\n                                    . chr(0x80 | $c &amp; 0x3F);<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n}<br \/>\nfunction asc_shift($string, $amount) {<br \/>\n $key = substr($string, 0, 1);<br \/>\n if(strlen($string)==1) {<br \/>\n   return unichr(uniord($key) + $amount);<br \/>\n } else {<br \/>\n   return unichr(uniord($key) + $amount) . asc_shift(substr($string, 1, strlen($string)-1), $amount);<br \/>\n }<br \/>\n}<br \/>\n[\/cc]<br \/>\nNow I understand why there is a need for a lower level implementation of UTF-8 strings by default in php 6. Python support of UTF-8 support has already been included in Python 3.0 released <em>December 3rd<\/em>, 2008.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on a word search generator that translated letter symbols (sort of like cryptograms, except this one&#8217;s impossible to solve without the key), php ran out of memory on a rather ordinary function. [cc lang=&#8221;php&#8221;] function uniord($c) { $h = ord($c{0}); if ($h &lt;= 0x7F) { return $h; } else if ($h &lt; 0xC2) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[23],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":0,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}