<commit_before>/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb
package http

import java.io.{InputStream, ByteArrayInputStream, File, FileInputStream,
                       FileOutputStream}
import scala.xml._

import common._
import json._
import util._
import Helpers._
import http.provider._
import sitemap._


object UserAgentCalculator extends Factory {
  /**
   * The default regular expression for IE
   */
  val iePattern = """MSIE ([0-9]+)""".r

  /**
   * You can change the mechanism by which the user agent for IE
   * is calculated by doing the Factory thing with this object
   */
  object ieCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultIeCalcFunction _)

  /**
   * The built-in mechanism for calculating IE
   */
  def defaultIeCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = iePattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Safari
   */
  val safariPattern = """Version.([0-9]+)[.0-9]+ ([^S])*Safari\/""".r

  /**
   * You can change the mechanism by which the user agent for Safari
   * is calculated by doing the Factory thing with this object
   */
  object safariCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultSafariCalcFunction _)

  /**
   * The built-in mechanism for calculating Safari
   */
  def defaultSafariCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = safariPattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Firefox
   */
  val firefoxPattern = """Firefox.([1-9][0-9]*\.[0-9])""".r

  /**
   * You can change the mechanism by which the user agent for Firefox
   * is calculated by doing the Factory thing with this object
   */
  object firefoxCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultFirefoxCalcFunction _)

  /**
   * The built-in mechanism for calculating Firefox
   */
  def defaultFirefoxCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = firefoxPattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Chrome
   */
  val chromePattern = """Chrome.([1-9][0-9]*\.[0-9])""".r

  /**
   * You can change the mechanism by which the user agent for Chrome
   * is calculated by doing the Factory thing with this object
   */
  object chromeCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultChromeCalcFunction _)

  /**
   * You can change the mechanism by which Lift calculates
   * if the User-Agent represents an iPhone.  Put your
   * special calculation function in here
   */
  object iPhoneCalcFunction extends FactoryMaker[Box[Box[String] => 
    Boolean]](Empty)

  /**
   * You can change the mechanism by which Lift calculates
   * if the User-Agent represents an iPad.  Put your
   * special calculation function in here
   */
  object iPadCalcFunction extends FactoryMaker[Box[Box[String] => 
    Boolean]](Empty)


  /**
   * The built-in mechanism for calculating Chrome
   */
  def defaultChromeCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = chromePattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver
}

trait UserAgentCalculator {
  lazy val ieVersion: Box[Int] = UserAgentCalculator.ieCalcFunction.vend.apply(userAgent).map(_.toInt)

  lazy val isIE6: Boolean = ieVersion.map(_ == 6) openOr false
  lazy val isIE7: Boolean = ieVersion.map(_ == 7) openOr false
  lazy val isIE8: Boolean = ieVersion.map(_ == 8) openOr false
  lazy val isIE9: Boolean = ieVersion.map(_ == 9) openOr false
  lazy val ieIE10: Boolean = ieVersion.map(_ == 10) openOr false
  lazy val isIE11: Boolean = ieVersion.map(_ == 11) openOr false
  lazy val isIE = ieVersion.map(_ >= 6) openOr false

  lazy val safariVersion: Box[Int] = 
    UserAgentCalculator.safariCalcFunction.vend.apply(userAgent).map(_.toInt)
<commit_msg>IE < 6 still counts as IE.<commit_after>/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb
package http

import java.io.{InputStream, ByteArrayInputStream, File, FileInputStream,
                       FileOutputStream}
import scala.xml._

import common._
import json._
import util._
import Helpers._
import http.provider._
import sitemap._


object UserAgentCalculator extends Factory {
  /**
   * The default regular expression for IE
   */
  val iePattern = """MSIE ([0-9]+)""".r

  /**
   * You can change the mechanism by which the user agent for IE
   * is calculated by doing the Factory thing with this object
   */
  object ieCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultIeCalcFunction _)

  /**
   * The built-in mechanism for calculating IE
   */
  def defaultIeCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = iePattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Safari
   */
  val safariPattern = """Version.([0-9]+)[.0-9]+ ([^S])*Safari\/""".r

  /**
   * You can change the mechanism by which the user agent for Safari
   * is calculated by doing the Factory thing with this object
   */
  object safariCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultSafariCalcFunction _)

  /**
   * The built-in mechanism for calculating Safari
   */
  def defaultSafariCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = safariPattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Firefox
   */
  val firefoxPattern = """Firefox.([1-9][0-9]*\.[0-9])""".r

  /**
   * You can change the mechanism by which the user agent for Firefox
   * is calculated by doing the Factory thing with this object
   */
  object firefoxCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultFirefoxCalcFunction _)

  /**
   * The built-in mechanism for calculating Firefox
   */
  def defaultFirefoxCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = firefoxPattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver

  /**
   * The default regular expression for Chrome
   */
  val chromePattern = """Chrome.([1-9][0-9]*\.[0-9])""".r

  /**
   * You can change the mechanism by which the user agent for Chrome
   * is calculated by doing the Factory thing with this object
   */
  object chromeCalcFunction extends FactoryMaker[Box[String] => 
    Box[Double]](defaultChromeCalcFunction _)

  /**
   * You can change the mechanism by which Lift calculates
   * if the User-Agent represents an iPhone.  Put your
   * special calculation function in here
   */
  object iPhoneCalcFunction extends FactoryMaker[Box[Box[String] => 
    Boolean]](Empty)

  /**
   * You can change the mechanism by which Lift calculates
   * if the User-Agent represents an iPad.  Put your
   * special calculation function in here
   */
  object iPadCalcFunction extends FactoryMaker[Box[Box[String] => 
    Boolean]](Empty)


  /**
   * The built-in mechanism for calculating Chrome
   */
  def defaultChromeCalcFunction(userAgent: Box[String]): Box[Double] = 
    for {
      ua <- userAgent
      m = chromePattern.pattern.matcher(ua)
      ver <- if (m.find) Helpers.asDouble(m.group(1)) else Empty
    } yield ver
}

trait UserAgentCalculator {
  lazy val ieVersion: Box[Int] = UserAgentCalculator.ieCalcFunction.vend.apply(userAgent).map(_.toInt)

  lazy val isIE6: Boolean = ieVersion.map(_ == 6) openOr false
  lazy val isIE7: Boolean = ieVersion.map(_ == 7) openOr false
  lazy val isIE8: Boolean = ieVersion.map(_ == 8) openOr false
  lazy val isIE9: Boolean = ieVersion.map(_ == 9) openOr false
  lazy val ieIE10: Boolean = ieVersion.map(_ == 10) openOr false
  lazy val isIE11: Boolean = ieVersion.map(_ == 11) openOr false
  lazy val isIE = ieVersion.isDefined

  lazy val safariVersion: Box[Int] = 
    UserAgentCalculator.safariCalcFunction.vend.apply(userAgent).map(_.toInt)

{'fraction_non_alphanumeric': 0.10491257285595337, 'fraction_numerical': 0.010616153205661949, 'mean_word_length': 3.3089686098654707, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5446383', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2835, 'n_words': 944}