Esempio n. 1
0
 private static void SecondPass(VersionHandler handler, int[] maxCharacters, List<DeviceResult> initialResults, Results results)
 {
     int lowestScore = int.MaxValue;
     foreach (DeviceResult current in initialResults)
     {
         // Calculate the score for this device.
         int deviceScore = 0;
         for (int segment = 0; segment < handler.VersionRegexes.Length; segment++)
         {
             deviceScore += (maxCharacters[segment] - current.Scores[segment].CharactersMatched + 1)*
                            (current.Scores[segment].Difference + maxCharacters[segment] -
                             current.Scores[segment].CharactersMatched);
         }
         // If the score is lower than the lowest so far then reset the list
         // of best matching devices.
         if (deviceScore < lowestScore)
         {
             results.Clear();
             lowestScore = deviceScore;
         }
         // If the device score is the same as the lowest score so far then add this
         // device to the list.
         if (deviceScore == lowestScore)
         {
             results.Add(current.Device);
         }
     }
 }
Esempio n. 2
0
        internal static Results Match(string userAgent, VersionHandler handler)
        {
            int[] maxCharacters = new int[handler.VersionRegexes.Length];
            List<DeviceResult> initialResults = new List<DeviceResult>(handler.UserAgents.Count);
            Results results = new Results();

            // The 1st pass calculates the scores for every segment of every device
            // available against the target useragent.
            FirstPass(handler, userAgent, maxCharacters, initialResults);

            // The 2nd pass returns the devices with the lowest difference across all the
            // versions available.
            SecondPass(handler, maxCharacters, initialResults, results);

            // Return the best device matches.
            return results;
        }
Esempio n. 3
0
 private static void FirstPass(VersionHandler handler, string userAgent, int[] maxCharacters, List<DeviceResult> initialResults)
 {
     string compare, target;
     foreach (var devices in handler.UserAgents)
     {
         foreach (DeviceInfo device in devices)
         {
             SegmentScore[] scores = new SegmentScore[handler.VersionRegexes.Length];
             for (int segment = 0; segment < handler.VersionRegexes.Length; segment++)
             {
                 target = handler.VersionRegexes[segment].Match(userAgent).Value;
                 compare = handler.VersionRegexes[segment].Match(device.UserAgent).Value;
                 for (int i = 0; i < target.Length && i < compare.Length; i++)
                 {
                     scores[segment].Difference += Math.Abs((target[i] - compare[i]));
                     scores[segment].CharactersMatched++;
                 }
                 if (scores[segment].CharactersMatched > maxCharacters[segment])
                     maxCharacters[segment] = scores[segment].CharactersMatched;
             }
             initialResults.Add(new DeviceResult(scores, device));
         }
     }
 }