Advertisement
WeltEnSTurm

Untitled

Aug 19th, 2016
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.76 KB | None | 0 0
  1.  
  2.  
  3. long cmpFuzzy(string haystack, string needle){
  4.     long scoreMul = 100;
  5.     long score = scoreMul*10;
  6.     size_t curIdx;
  7.     long largestScore;
  8.     foreach(i, c; haystack){
  9.         if(curIdx<needle.length && c.toLower == needle[curIdx].toLower){
  10.             score += scoreMul;
  11.             scoreMul *= scoreMul;//(c == needle[curIdx] ? 4 : 3);
  12.             curIdx++;
  13.         }else{
  14.             scoreMul = 100;
  15.         }
  16.         if(curIdx==needle.length){
  17.             scoreMul = 100;
  18.             curIdx = 0;
  19.             score = scoreMul*10;
  20.             if(largestScore < score-i+needle.length)
  21.                 largestScore = score-i+needle.length;
  22.         }
  23.     }
  24.     if(!largestScore)
  25.         return 0;
  26.     if(!needle.startsWith(".") && (haystack.canFind("/.") || haystack.startsWith(".")))
  27.         largestScore -= 5;
  28.     if(needle == haystack)
  29.         largestScore += 10000000;
  30.     return largestScore;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement