pwnt.be

More JavaScript

Another addition I would like to see in MooTools—or, better yet, ECMAScript in general—is the equivalent of Java’s String.equalsIgnoreCase(String) function. Sure, a first implementation is trivial:

String.extend({
  'equalsIgnoreCase': function(str) {
    return (this.toLowerCase() == str.toLowerCase()
      && this.toUpperCase() == str.toUpperCase());
  }
});

… but if you want it to be a little more efficient, you’ll avoid inspecting both strings in their entirety first and use String.charAt(int) instead. If your motto is ‘Code is Poetry’, you’ll end up with something like …

String.extend({
  'equalsIgnoreCase': function(str) {
    var i = -1;
    if (this.length == str.length) {
      i = 0;
      while (i < this.length
      && this.charAt(i).toLowerCase() == str.charAt(i).toLowerCase()
      && this.charAt(i).toUpperCase() == str.charAt(i).toUpperCase()) {
        i++;
      }
    }
    return (i == this.length);
  }
});

Since ECMAScript is weakly typed and doesn’t have a Character class like Java does, the actual comparison still uses string functions. I wonder how much this influences the algorithm’s performance.

Update: I added the String.toUpperCase() calls, because Java’s implementation has them too. I’m no expert when it comes to international characters.

Post a Comment

This contraption supports Gravatar, as well as Markdown with SmartyPants. If none of that made sense to you, feel free to ignore it and start typing.

  • :)
  • :D
  • ;)
  • :-O
  • :P
  • :@
  • :$
  • :S
  • :(
  • :'(
  • :|
  • :-#
  • 8-|
  • ^o)
  • :-*
  • +o(
  • :^)
  • *-)
  • 8-)
  • |-)
Disorientation
Continuity
Tangentiality
Retributions
Koop eens een Nokia Lumia 800
Samuel Debruyn
Bizar Hairdressing & Beyond
Hanne, Hanne, Ruxi, Wim, Tim, Sarina, Lies, Lynn, erwin, Ano, Frederick, Jacqueline, Wazaaa, Tim, Rebecca, Charlie
Lplayer for the Rest of Us
fieryy-AA, jesus2099, Tim, jesus2099, Tim, jesus2099, Tim, PixelPirate
Automating OpenVPN Connection on Windows XP
blanky, sky, Tim, Geb, 12vpn, Tim, neecom
Simple Linear Regression with JFreeChart
Nicolas Machado, Sascha, Tim, Sascha, Tim, Sascha
Colophonics