Burak Gürsoy avatar Burak Gürsoy committed 3cf1147

Optimize

Comments (0)

Files changed (2)

 
 Time zone is GMT+2.
 
+0.11 Fri Jan 21 02:17:27 2011
+    => Clean up.
+
 0.10 Wed Jan 19 03:45:27 2011
     => First release. Nearly direct port from Python+Ruby code.

lib/Lingua/TR/ASCII.pm

 use base qw( Exporter );
 use Lingua::TR::ASCII::Data;
 
-our $VERSION = '0.10';
+our $VERSION = '0.11';
 our @EXPORT  = qw( ascii_to_turkish turkish_to_ascii );
 
 sub ascii_to_turkish {
     return $self;
 }
 
+# Convert a string with ASCII-only letters into one with Turkish letters.
 sub _deasciify {
     my($self) = @_;
-    # Convert a string with ASCII-only letters into one with
-    # Turkish letters.
     my $s     = \$self->{turkish};
     my @chars = split m{}xms, ${$s};
 
     return ${$s};
 }
 
+# Determine if char at cursor needs correction.
 sub _needs_correction {
     my($self, $ch, $point) = @_;
-    # Determine if char at cursor needs correction.
     my $tr = $ASCIIFY->{ $ch } || $ch;
     my $pl = $PATTERN->{ lc $tr };
     my $m  = $pl ? $self->_matches( $pl, $point || 0 ) : 0;
                       : ( $ch eq $tr ?   $m : ! $m );
 }
 
+# Check if the pattern is in the pattern table.
 sub _matches {
     my($self, $dlist, $point) = @_;
-    # Check if the pattern is in the pattern table.
-    my $rank  = 2 * keys %{ $dlist };
-    my $str   = $self->_get_context( $point || 0 );
-    my $start = 0;
-    my $end   = 0;
+    my $str  = $self->_get_context( $point || 0 );
+    my $rank = 2 * keys %{ $dlist };
+    my $len  = length $str;
+    my($start, $end);
 
-    my $_len = length $str;
-
-    while ( $start <= CONTEXT_SIZE ) {
-        $end = 1 + CONTEXT_SIZE;
-        while ( $end <= $_len ) {
+    while ( $start++ <= CONTEXT_SIZE ) {
+        $end = CONTEXT_SIZE;
+        while ( ++$end <= $len ) {
             my $s = substr $str, $start, $end - $start;
-            my $r = $dlist->{ $s };
-            $rank = $r if $r && abs $r < abs $rank;
-            $end++;
+            my $r = $dlist->{ $s } || next;
+            $rank = $r if abs $r < abs $rank;
         }
-        $start++;
     }
 
     return $rank > 0;
 sub _get_context {
     my($self, $point, $size) = @_;
     $size ||= CONTEXT_SIZE;
-    my $s = q{ } x ( 1 + ( 2 * $size ) );
+    my($s, $i, $space, $index);
+
+    my $morph = sub {
+        my($next, $lookup) = @_;
+        $space = 0;
+        while ( $next->() ) {
+            my $char = substr $self->{turkish}, $index, 1;
+            my $x    = $lookup->{ $char };
+            if ( $x ) {
+                substr $s, abs $i, 1, $x;
+                $space = 0;
+                $i++;
+                next;
+            }
+            next if $space;
+            $space = 1;
+            $i++;
+        }
+    };
+
+    $s     = q{ } x ( 1 + ( 2 * $size ) );
+    $i     = 1 + $size;
+    $index = $point;
     substr $s, $size, 1, 'X';
 
-    my $turkish = \$self->{turkish};
-    my $i       = 1 + $size;
-    my $index   = 1 + $point;
-    my $len     = $self->{length};
-    my $space;
-
-    while ( $i < length $s && ! $space && $index < $len ) {
-        my $current_char = substr $self->{turkish}, $index++, 1;
-        my $x = $DOWNCASE_ASCIIFY->{ $current_char };
-        if ( ! $x ) {
-            if ( ! $space ) {
-                $space = 1;
-                $i++;
-            }
-        }
-        else {
-            substr $s, $i, 1, $x;
-            $space = 0;
-            $i++;
-        }
-    }
+    $morph->(
+        sub { $i < length $s && ! $space && ++$index < $self->{length} },
+        $DOWNCASE_ASCIIFY
+    );
 
     $s     = substr $s, 0, $i;
-    $index = $point - 1;
-    $i     = $size  - 1;
-    $space = 0;
+    $i     = 0 - --$size;
+    $index = $point;
 
-    while ( $i >= 0 && $index >= 0 ) {
-        my $current_char = substr $self->{turkish}, $index--, 1;
-        my $x = $UPCASE_ACCENTS->{ $current_char };
-        if ( ! $x ) {
-            if ( ! $space ) {
-                $space = 1;
-                $i--;
-            }
-        }
-        else {
-            substr $s, $i, 1, $x;
-            $space = 0;
-            $i--;
-        }
-    }
+    $morph->(
+        sub { $i <= 0 && --$index >= 0 },
+        $UPCASE_ACCENTS
+    );
 
     return $s;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.