<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: comparison code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 22 Jul 2008 18:38:10 GMT</pubDate>
    <description>DZone Snippets: comparison code</description>
    <item>
      <title>String equality tester</title>
      <link>http://snippets.dzone.com/posts/show/4253</link>
      <description>If you want to test the equality of two strings and don't want the overhead of strcmp(), then this is the function for you.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;_Bool strequals(char* a, char* b) {&lt;br /&gt; if (!a || !b) return 0;&lt;br /&gt; do {if (*a != *b) return 0; } while (*a++ &amp;&amp; *b++);&lt;br /&gt; return 1;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:22:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4253</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>JavaScript String Comparison</title>
      <link>http://snippets.dzone.com/posts/show/3482</link>
      <description>// Example use&lt;br /&gt;// if (isSameString(tags, "_none_")) ...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;      function isSameString( s1, s2 )&lt;br /&gt;      {&lt;br /&gt;        alert( "s1: " + s1.toString() );&lt;br /&gt;        alert( "s2: " + s2.toString() );&lt;br /&gt;&lt;br /&gt;      	if ( s1.toString() == s2.toString() )&lt;br /&gt;        {&lt;br /&gt;      	  return true;&lt;br /&gt;        }&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;          return false;&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Feb 2007 09:13:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3482</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Is a value between two other values?</title>
      <link>http://snippets.dzone.com/posts/show/1607</link>
      <description>&lt;code&gt;&lt;br /&gt;between?: func [&lt;br /&gt;    value bound-1 bound-2&lt;br /&gt;    /exclusive&lt;br /&gt;    /local low-bound high-bound&lt;br /&gt;][&lt;br /&gt;    set [low-bound high-bound] sort reduce [bound-1 bound-2]&lt;br /&gt;    either exclusive [&lt;br /&gt;        all [(value &gt; low-bound) (value &lt; high-bound)]&lt;br /&gt;    ][&lt;br /&gt;        ;-- Inclusive comparison&lt;br /&gt;        all [(value &gt;= low-bound) (value &lt;= high-bound)]&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 01 Mar 2006 03:52:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1607</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>limit - limit a value between boundary values</title>
      <link>http://snippets.dzone.com/posts/show/1605</link>
      <description>&lt;code&gt;&lt;br /&gt;limit: func [&lt;br /&gt;    "Make sure val falls between lower and upper bounds, inclusive"&lt;br /&gt;    val lower upper&lt;br /&gt;][&lt;br /&gt;    max min val upper lower&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 01 Mar 2006 03:49:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1605</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>All Exceptions Created Equal</title>
      <link>http://snippets.dzone.com/posts/show/1509</link>
      <description>All exceptions are created equal. But what if you have a very good and pressing reason to serialize one to save it in a database? For instance, you had a collection of checks you wanted to run against some data, and save the results?&lt;br /&gt;&lt;br /&gt;Ah, that's easy! serialize() was made for that! So off you trot, you make a few changes to the code, add a blank line here, a blank line there, and suddenly your code can't find the exact matches of the serialized object in the database.&lt;br /&gt;&lt;br /&gt;HUH? What's going on? You're the exact same Exception I just threw three minutes ago, and you've sporadically broken?&lt;br /&gt;&lt;br /&gt;It took me a while to twig. When you create an exception ($e = new Exception("foo");), it's shiny and new and listens when you do equality comparisions (==).&lt;br /&gt;&lt;br /&gt;But things go awry: you throw a new Exception from your filter, catch it, and serialize it. You haven't remembered that...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$a = new Exception("foo");&lt;br /&gt;try {&lt;br /&gt;    throw $a; //Line 1&lt;br /&gt;} catch (Exception $e) {&lt;br /&gt;    throw $a; //Line 10;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;will result in two difference traces. One saying "I was thrown on on line 1", the other "I was thrown on on line 10"...&lt;br /&gt;&lt;br /&gt;Fuck oath, hello stupid coder. You've been wracking your brains wondering why every time you go off and edit a different bit of code it serializes differently; and there you have it.&lt;br /&gt;&lt;br /&gt;How the hell do I fix it? Going to __sleep() on the job actually helps.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;class DumbException extends Exception {&lt;br /&gt;    /**&lt;br /&gt;     * Cleanup anything we need before serialisation&lt;br /&gt;     *&lt;br /&gt;     * @return  string[]    An array of member varible names to serialize&lt;br /&gt;     * @see     http://php.planetmirror.com/manual/en/language.oop5.magic.php&lt;br /&gt;     */&lt;br /&gt;    public function __sleep() {&lt;br /&gt;        return array('string','code');&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Compare against another DumbException for equality.&lt;br /&gt;     *&lt;br /&gt;     * Since two exceptions can be !== because the trace / line / file&lt;br /&gt;     * information is different, we need to do this.&lt;br /&gt;     */&lt;br /&gt;    public function cmp(DumbException $e) {&lt;br /&gt;        return (serialize($e) == serialize($this));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print '&lt;pre&gt;';&lt;br /&gt;$a = new DumbException();&lt;br /&gt;$b = new DumbException();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    try {&lt;br /&gt;        throw $b;&lt;br /&gt;    } catch (Exception $e) {&lt;br /&gt;        throw $a;&lt;br /&gt;    }&lt;br /&gt;} catch (Exception $e) {&lt;br /&gt;&lt;br /&gt;    var_dump($a === $b);&lt;br /&gt;    var_dump($a == $b);&lt;br /&gt;    var_dump($b === $e);&lt;br /&gt;    var_dump($b == $e);&lt;br /&gt;    var_dump($a === $e);&lt;br /&gt;    var_dump($a == $e);&lt;br /&gt;&lt;br /&gt;    var_dump($b-&gt;cmp($e));&lt;br /&gt;    var_dump($a-&gt;cmp($e));&lt;br /&gt;}&lt;br /&gt;print '&lt;/pre&gt;';&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Feb 2006 10:01:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1509</guid>
      <author>CloCkWeRX (Daniel O'Connor)</author>
    </item>
    <item>
      <title>Comparing path.py with os.path</title>
      <link>http://snippets.dzone.com/posts/show/1463</link>
      <description>Taken from Simon Willson's &lt;a href=http://simon.incutio.com/archive/2003/01/22/pythonPathModule&gt;blog post&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# with os.path.walk&lt;br /&gt;def delete_backups(arg, dirname, names):&lt;br /&gt;    for name in names:&lt;br /&gt;        if name.endswith('~'):&lt;br /&gt;            os.remove(os.path.join(dirname, name))&lt;br /&gt;&lt;br /&gt;os.path.walk(os.environ['HOME'], delete_backups, None)&lt;br /&gt;&lt;br /&gt;# with os.path, if (like me) you can never remember how os.path.walk works&lt;br /&gt;def walk_tree_delete_backups(dir):&lt;br /&gt;    for name in os.listdir(dir):&lt;br /&gt;        path = os.path.join(dir, name)&lt;br /&gt;        if os.path.isdir(path):&lt;br /&gt;            walk_tree_delete_backups(path)&lt;br /&gt;        elif name.endswith('~'):&lt;br /&gt;            os.remove(path)&lt;br /&gt;&lt;br /&gt;walk_tree_delete_backups(os.environ['HOME'])&lt;br /&gt;&lt;br /&gt;# with path&lt;br /&gt;dir = path(os.environ['HOME'])&lt;br /&gt;for f in dir.walk():&lt;br /&gt;    if f.isfile() and f.endswith('~'):&lt;br /&gt;        os.remove(f)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 12 Feb 2006 20:08:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1463</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Comparing pygame and pys60</title>
      <link>http://snippets.dzone.com/posts/show/1454</link>
      <description>I take the pygame code from &lt;a href=http://rene.f0o.com/mywiki/LectureThree&gt;here&lt;/a&gt;. Then, write my own pys60 part.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame's typical beginning&lt;br /&gt;import pygame&lt;br /&gt;from pygame.locals import *&lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;import e32, graphics&lt;br /&gt;from appuifw import *&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Setting up the screen&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame&lt;br /&gt;window = pygame.display.set_mode((468, 60)) &lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;app.screen = 'full'   # or 'normal', 'large'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Get screen or canvas&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame&lt;br /&gt;screen = pygame.display.get_surface() &lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;app.body = canvas = Canvas(None, key.handle_event) &lt;br /&gt;# you need to create key (a Keyboard instance) before this&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Loading image&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame&lt;br /&gt;monkey_image = pygame.image.load(file_name)&lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;monkey_image = graphics.Image.open(file_name)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Drawing image onto the screen&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame&lt;br /&gt;screen.blit(monkey_image, (0,0))&lt;br /&gt;pygame.display.flip()  &lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;canvas.blit(monkey_image, target=(0,0))&lt;br /&gt;# we may add a buffer, which will need buffer.blit(image)&lt;br /&gt;# and canvas.blit(buffer) for equivalent steps&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Loading and playing sound&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# pygame&lt;br /&gt;sound = pygame.mixer.Sound(fullname)&lt;br /&gt;sound.play()&lt;br /&gt;&lt;br /&gt;# pys60&lt;br /&gt;import audio&lt;br /&gt;sound = audio.Sound.open(fullname)&lt;br /&gt;sound.play()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;For Rect and Sprite, there is no pys60 equivalence yet.&lt;br /&gt;I may write an equivalent class someday.</description>
      <pubDate>Fri, 10 Feb 2006 20:34:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1454</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
