text:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! A property declaration block.

#![deny(missing_docs)]

use context::QuirksMode;
use cssparser::{DeclarationListParser, parse_important, ParserInput, CowRcStr};
use cssparser::{Parser, AtRuleParser, DeclarationParser, Delimiter, ParseErrorKind};
use custom_properties::CustomPropertiesBuilder;
use error_reporting::{ParseErrorReporter, ContextualParseError};
use parser::{ParserContext, ParserErrorContext};
use properties::animated_properties::AnimationValue;
use shared_lock::Locked;
use smallbitvec::{self, SmallBitVec};
use smallvec::SmallVec;
use std::fmt;
use std::iter::{DoubleEndedIterator, Zip};
use std::slice::Iter;
use style_traits::{ToCss, ParseError, ParsingMode, StyleParseErrorKind};
use stylesheets::{CssRuleType, Origin, UrlExtraData};
use super::*;
use values::computed::Context;
#[cfg(feature = "gecko")] use properties::animated_properties::AnimationValueMap;

/// The animation rules.
///
/// The first one is for Animation cascade level, and the second one is for
/// Transition cascade level.
pub struct AnimationRules(pub Option<Arc<Locked<PropertyDeclarationBlock>>>,
                          pub Option<Arc<Locked<PropertyDeclarationBlock>>>);

impl AnimationRules {
    /// Returns whether these animation rules represents an actual rule or not.
    pub fn is_empty(&self) -> bool {
        self.0.is_none() && self.1.is_none()
    }
}

/// Whether a given declaration comes from CSS parsing, or from CSSOM.
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq)]
pub enum DeclarationSource {
    /// The declaration was obtained from CSS parsing of sheets and such.
    Parsing,
    /// The declaration was obtained from CSSOM.
    CssOm,
}

/// A declaration [importance][importance].
///
/// [importance]: https://drafts.csswg.org/css-cascade/#importance
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq)]
pub enum Importance {
    /// Indicates a declaration without `!important`.
    Normal,

    /// Indicates a declaration with `!important`.
    Important,
}

impl Importance {
    /// Return whether this is an important declaration.
    pub fn important(self) -> bool {
        match self {
            Importance::Normal => false,
            Importance::Important => true,
        }
    }
}

/// Overridden declarations are skipped.
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone)]
pub struct PropertyDeclarationBlock {
    /// The group of declarations, along with their importance.
    ///
    /// Only deduplicated declarations appear here.
    declarations: Vec<PropertyDeclaration>,

    /// The "important" flag for each declaration in `declarations`.
    declarations_importance: SmallBitVec,

    longhands: LonghandIdSet,
}

/// Iterator over `(PropertyDeclaration, Importance)` pairs.
pub struct DeclarationImportanceIterator<'a> {
    iter: Zip<Iter<'a, PropertyDeclaration>, smallbitvec::Iter<'a>>,
}

impl<'a> DeclarationImportanceIterator<'a> {
    /// Constructor
    pub fn new(declarations: &'a [PropertyDeclaration], important: &'a SmallBitVec) -> Self {
        DeclarationImportanceIterator {
            iter: declarations.iter().zip(important.iter()),
        }
    }
}

impl<'a> Iterator for DeclarationImportanceIterator<'a> {
    type Item = (&'a PropertyDeclaration, Importance);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(decl, important)|
            (decl, if important { Importance::Important } else { Importance::Normal }))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a> DoubleEndedIterator for DeclarationImportanceIterator<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.iter.next_back().map(|(decl, important)|
            (decl, if important { Importance::Important } else { Importance::Normal }))
    }
}

/// Iterator over `PropertyDeclaration` for Importance::Normal.
pub struct NormalDeclarationIterator<'a>(DeclarationImportanceIterator<'a>);

impl<'a> NormalDeclarationIterator<'a> {
    /// Constructor
    pub fn new(declarations: &'a [PropertyDeclaration], important: &'a SmallBitVec) -> Self {
        NormalDeclarationIterator(
            DeclarationImportanceIterator::new(declarations, important)
        )
    }
}

impl<'a> Iterator for NormalDeclarationIterator<'a> {
    type Item = &'a PropertyDeclaration;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = self.0.iter.next();
            match next {
                Some((decl, importance)) => {
                    if !importance {
                        return Some(decl);
                    }
                },
                None => return None,
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.iter.size_hint()
    }
}

/// Iterator for AnimationValue to be generated from PropertyDeclarationBlock.
pub struct AnimationValueIterator<'a, 'cx, 'cx_a:'cx> {
    iter: NormalDeclarationIterator<'a>,
    context: &'cx mut Context<'cx_a>,
    default_values: &'a ComputedValues,
    /// Custom properties in a keyframe if exists.
    extra_custom_properties: Option<&'a Arc<::custom_properties::CustomPropertiesMap>>,
}

impl<'a, 'cx, 'cx_a:'cx> AnimationValueIterator<'a, 'cx, 'cx_a> {
    fn new(
        declarations: &'a PropertyDeclarationBlock,
        context: &'cx mut Context<'cx_a>,
        default_values: &'a ComputedValues,
       extra_custom_properties: Option<&'a Arc<::custom_properties::CustomPropertiesMap>>,
    ) -> AnimationValueIterator<'a, 'cx, 'cx_a> {
        AnimationValueIterator {
            iter: declarations.normal_declaration_iter(),
            context,
            default_values,
            extra_custom_properties,
        }
    }
}

impl<'a, 'cx, 'cx_a:'cx> Iterator for AnimationValueIterator<'a, 'cx, 'cx_a> {
    type Item = AnimationValue;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = self.iter.next();
            let decl = match next {
                Some(decl) => decl,
                None => return None,
            };

            let animation = AnimationValue::from_declaration(
                decl,
                &mut self.context,
                self.extra_custom_properties,
                self.default_values,
            );

            if let Some(anim) = animation {
                return Some(anim);
            }
        }
    }
}

impl fmt::Debug for PropertyDeclarationBlock {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.declarations.fmt(f)
    }
}

impl PropertyDeclarationBlock {
    /// Returns the number of declarations in the block.
    pub fn len(&self) -> usize {
        self.declarations.len()
    }

    /// Create an empty block
    pub fn new() -> Self {
        PropertyDeclarationBlock {
            declarations: Vec::new(),
            declarations_importance: SmallBitVec::new(),
            longhands: LonghandIdSet::new(),
        }
    }

    /// Create a block with a single declaration
    pub fn with_one(declaration: PropertyDeclaration, importance: Importance) -> Self {
        let mut longhands = LonghandIdSet::new();
        if let PropertyDeclarationId::Longhand(id) = declaration.id() {
            longhands.insert(id);
        }
        PropertyDeclarationBlock {
            declarations: vec![declaration],
            declarations_importance: SmallBitVec::from_elem(1, importance.important()),
            longhands: longhands,
        }
    }

    /// The declarations in this block
    pub fn declarations(&self) -> &[PropertyDeclaration] {
        &self.declarations
    }

    /// The `important` flags for declarations in this block
    pub fn declarations_importance(&self) -> &SmallBitVec {
        &self.declarations_importance
    }

    /// Iterate over `(PropertyDeclaration, Importance)` pairs
    pub fn declaration_importance_iter(&self) -> DeclarationImportanceIterator {
        DeclarationImportanceIterator::new(&self.declarations, &self.declarations_importance)
    }

    /// Iterate over `PropertyDeclaration` for Importance::Normal
    pub fn normal_declaration_iter(&self) -> NormalDeclarationIterator {
        NormalDeclarationIterator::new(&self.declarations, &self.declarations_importance)
    }

    /// Return an iterator of (AnimatableLonghand, AnimationValue).
    pub fn to_animation_value_iter<'a, 'cx, 'cx_a:'cx>(
        &'a self,
        context: &'cx mut Context<'cx_a>,
        default_values: &'a ComputedValues,
        extra_custom_properties: Option<&'a Arc<::custom_properties::CustomPropertiesMap>>,
    ) -> AnimationValueIterator<'a, 'cx, 'cx_a> {
        AnimationValueIterator::new(self, context, default_values, extra_custom_properties)
    }

    /// Returns whether this block contains any declaration with `!important`.
    ///
    /// This is based on the `declarations_importance` bit-vector,
    /// which should be maintained whenever `declarations` is changed.
    pub fn any_important(&self) -> bool {
        !self.declarations_importance.all_false()
    }

    /// Returns whether this block contains any declaration without `!important`.
    ///
    /// This is based on the `declarations_importance` bit-vector,
    /// which should be maintained whenever `declarations` is changed.
    pub fn any_normal(&self) -> bool {
        !self.declarations_importance.all_true()
    }

    /// Returns whether this block contains a declaration of a given longhand.
    pub fn contains(&self, id: LonghandId) -> bool {
        self.longhands.contains(id)
    }

    /// Get a declaration for a given property.
    ///
    /// NOTE: This is linear time.
    pub fn get(&self, property: PropertyDeclarationId) -> Option<(&PropertyDeclaration, Importance)> {
        self.declarations.iter().enumerate().find(|&(_, decl)| decl.id() == property).map(|(i, decl)| {
            let importance = if self.declarations_importance.get(i as u32) {
                Importance::Important
            } else {
                Importance::Normal
            };
            (decl, importance)
        })
    }

    /// Find the value of the given property in this block and serialize it
    ///
    /// <https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertyvalue>
    pub fn property_value_to_css<W>(&self, property: &PropertyId, dest: &mut W) -> fmt::Result
        where W: fmt::Write,
    {
        // Step 1.1: done when parsing a string to PropertyId

        // Step 1.2
        match property.as_shorthand() {
            Ok(shorthand) => {
                // Step 1.2.1
                let mut list = Vec::new();
                let mut important_count = 0;

                // Step 1.2.2
                for &longhand in shorthand.longhands() {
                    // Step 1.2.2.1
                    let declaration = self.get(PropertyDeclarationId::Longhand(longhand));

                    // Step 1.2.2.2 & 1.2.2.3
                    match declaration {
                        Some((declaration, importance)) => {
                            list.push(declaration);
                            if importance.important() {
                                important_count += 1;
                            }
                        },
                        None => return Ok(()),
                    }
                }

                // If there is one or more longhand with important, and one or more
                // without important, we don't serialize it as a shorthand.
                if important_count > 0 && important_count != list.len() {
                    return Ok(());
                }

                // Step 1.2.3
                // We don't print !important when serializing individual properties,
                // so we treat this as a normal-importance property
                match shorthand.get_shorthand_appendable_value(list) {
                    Some(appendable_value) =>
                        append_declaration_value(dest, appendable_value),
                    None => return Ok(()),
                }
            }
            Err(longhand_or_custom) => {
                if let Some((value, _importance)) = self.get(longhand_or_custom) {
                    // Step 2
                    value.to_css(dest)
                } else {
                    // Step 3
                    Ok(())
                }
            }
        }
    }

    /// <https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority>
    pub fn property_priority(&self, property: &PropertyId) -> Importance {
        // Step 1: done when parsing a string to PropertyId

        // Step 2
        match property.as_shorthand() {
            Ok(shorthand) => {
                // Step 2.1 & 2.2 & 2.3
                if shorthand.longhands().iter().all(|&l| {
                    self.get(PropertyDeclarationId::Longhand(l))
                        .map_or(false, |(_, importance)| importance.important())
                }) {
                    Importance::Important
                } else {
                    Importance::Normal
                }
            }
            Err(longhand_or_custom) => {
                // Step 3
                self.get(longhand_or_custom).map_or(Importance::Normal, |(_, importance)| importance)
            }
        }
    }

    /// Adds or overrides the declaration for a given property in this block.
    ///
    /// See the documentation of `push` to see what impact `source` has when the
    /// property is already there.
    pub fn extend(
        &mut self,
        mut drain: SourcePropertyDeclarationDrain,
        importance: Importance,
        source: DeclarationSource,
    ) -> bool {
        let all_shorthand_len = match drain.all_shorthand {
            AllShorthand::NotSet => 0,
            AllShorthand::CSSWideKeyword(_) |
            AllShorthand::WithVariables(_) => ShorthandId::All.longhands().len()
        };
        let push_calls_count = drain.declarations.len() + all_shorthand_len;

        // With deduplication the actual length increase may be less than this.
        self.declarations.reserve(push_calls_count);

        let mut changed = false;
        for decl in &mut drain.declarations {
            changed |= self.push(
                decl,
                importance,
                source,
            );
        }
        match drain.all_shorthand {
            AllShorthand::NotSet => {}
            AllShorthand::CSSWideKeyword(keyword) => {
                for &id in ShorthandId::All.longhands() {
                    let decl = PropertyDeclaration::CSSWideKeyword(id, keyword);
                    changed |= self.push(
                        decl,
                        importance,
                        source,
                    );
                }
            }
            AllShorthand::WithVariables(unparsed) => {
                for &id in ShorthandId::All.longhands() {
                    let decl = PropertyDeclaration::WithVariables(id, unparsed.clone());
                    changed |= self.push(
                        decl,
                        importance,
                        source,
                    );
                }
            }
        }
        changed
    }

    /// Adds or overrides the declaration for a given property in this block.
    ///
    /// Depending on the value of `source`, this has a different behavior in the
    /// presence of another declaration with the same ID in the declaration
    /// block.
    ///
    ///   * For `DeclarationSource::Parsing`, this will not override a
    ///     declaration with more importance, and will ensure that, if inserted,
    ///     it's inserted at the end of the declaration block.
    ///
    ///   * For `DeclarationSource::CssOm`, this will override importance and
    ///     will preserve the original position on the block.
    ///
    pub fn push(
        &mut self,
        declaration: PropertyDeclaration,
        importance: Importance,
        source: DeclarationSource,
    ) -> bool {
        let longhand_id = match declaration.id() {
            PropertyDeclarationId::Longhand(id) => Some(id),
            PropertyDeclarationId::Custom(..) => None,
        };

        let definitely_new = longhand_id.map_or(false, |id| {
            !self.longhands.contains(id)
        });

        if !definitely_new {
            let mut index_to_remove = None;
            for (i, slot) in self.declarations.iter_mut().enumerate() {
                if slot.id() == declaration.id() {
                    let important = self.declarations_importance.get(i as u32);
                    match (important, importance.important()) {
                        (false, true) => {}

                        (true, false) => {
                            // For declarations set from the OM, less-important
                            // declarations are overridden.
                            if !matches!(source, DeclarationSource::CssOm) {
                                return false
                            }
                        }
                        _ => if *slot == declaration {
                            return false;
                        }
                    }

                    match source {
                        // CSSOM preserves the declaration position, and
                        // overrides importance.
                        DeclarationSource::CssOm => {
                            *slot = declaration;
                            self.declarations_importance.set(i as u32, importance.important());
                            return true;
                        }
                        DeclarationSource::Parsing => {
                            // As a compatibility hack, specially on Android,
                            // don't allow to override a prefixed webkit display
                            // value with an unprefixed version from parsing
                            // code.
                            //
                            // TODO(emilio): Unship.
                            if let PropertyDeclaration::Display(old_display) = *slot {
                                use properties::longhands::display::computed_value::T as display;

                                if let PropertyDeclaration::Display(new_display) = declaration {
                                    if display::should_ignore_parsed_value(old_display, new_display) {
                                        return false;
                                    }
                                }
                            }

                            // NOTE(emilio): We could avoid this and just override for
                            // properties not affected by logical props, but it's not
                            // clear it's worth it given the `definitely_new` check.
                            index_to_remove = Some(i);
                            break;
                        }
                    }
                }
            }

            if let Some(index) = index_to_remove {
                self.declarations.remove(index);
                self.declarations_importance.remove(index as u32);
                self.declarations.push(declaration);
                self.declarations_importance.push(importance.important());
                return true;
            }
        }

        if let Some(id) = longhand_id {
            self.longhands.insert(id);
        }
        self.declarations.push(declaration);
        self.declarations_importance.push(importance.important());
        true
    }

    /// Set the declaration importance for a given property, if found.
    ///
    /// Returns whether any declaration was updated.
    pub fn set_importance(&mut self, property: &PropertyId, new_importance: Importance) -> bool {
        let mut updated_at_least_one = false;
        for (i, declaration) in self.declarations.iter().enumerate() {
            if declaration.id().is_or_is_longhand_of(property) {
                let is_important = new_importance.important();
                if self.declarations_importance.get(i as u32) != is_important {
                    self.declarations_importance.set(i as u32, is_important);
                    updated_at_least_one = true;
                }
            }
        }
        updated_at_least_one
    }

    /// <https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-removeproperty>
    ///
    /// Returns whether any declaration was actually removed.
    pub fn remove_property(&mut self, property: &PropertyId) -> bool {
        if let PropertyId::Longhand(id) = *property {
            if !self.longhands.contains(id) {
                return false
            }
        }
        let mut removed_at_least_one = false;
        let longhands = &mut self.longhands;
        let declarations_importance = &mut self.declarations_importance;
        let mut i = 0;
        self.declarations.retain(|declaration| {
            let id = declaration.id();
            let remove = id.is_or_is_longhand_of(property);
            if remove {
                removed_at_least_one = true;
                if let PropertyDeclarationId::Longhand(id) = id {
                    longhands.remove(id);
                }
                declarations_importance.remove(i);
            } else {
                i += 1;
            }
            !remove
        });

        if let PropertyId::Longhand(_) = *property {
            debug_assert!(removed_at_least_one);
        }
        removed_at_least_one
    }

    /// Take a declaration block known to contain a single property and serialize it.
    pub fn single_value_to_css<W>(
        &self,
        property: &PropertyId,
        dest: &mut W,
        computed_values: Option<&ComputedValues>,
        custom_properties_block: Option<&PropertyDeclarationBlock>,
    ) -> fmt::Result
    where
        W: fmt::Write,
    {
        match property.as_shorthand() {
            Err(_longhand_or_custom) => {
                if self.declarations.len() == 1 {
                    let declaration = &self.declarations[0];
                    let custom_properties = if let Some(cv) = computed_values {
                        // If there are extra custom properties for this
                        // declaration block, factor them in too.
                        if let Some(block) = custom_properties_block {
                            // FIXME(emilio): This is not super-efficient
                            // here...
                            block.cascade_custom_properties(cv.custom_properties())
                        } else {
                            cv.custom_properties().cloned()
                        }
                    } else {
                        None
                    };

                    match (declaration, computed_values) {
                        // If we have a longhand declaration with variables, those variables will be
                        // stored as unparsed values. As a temporary measure to produce sensible results
                        // in Gecko's getKeyframes() implementation for CSS animations, if
                        // |computed_values| is supplied, we use it to expand such variable
                        // declarations. This will be fixed properly in Gecko bug 1391537.
                        (&PropertyDeclaration::WithVariables(id, ref unparsed),
                         Some(ref _computed_values)) => {
                            unparsed.substitute_variables(
                                id,
                                custom_properties.as_ref(),
                                QuirksMode::NoQuirks,
                            )
                            .to_css(dest)
                        },
                        (ref d, _) => d.to_css(dest),
                    }
                } else {
                    Err(fmt::Error)
                }
            }
            Ok(shorthand) => {
                if !self.declarations.iter().all(|decl| decl.shorthands().contains(&shorthand)) {
                    return Err(fmt::Error)
                }
                let iter = self.declarations.iter();
                match shorthand.get_shorthand_appendable_value(iter) {
                    Some(AppendableValue::Css { css, .. }) => {
                        dest.write_str(css)
                    },
                    Some(AppendableValue::DeclarationsForShorthand(_, decls)) => {
                        shorthand.longhands_to_css(decls, dest)
                    }
                    _ => Ok(())
                }
            }
        }
    }

    /// Convert AnimationValueMap to PropertyDeclarationBlock.
    #[cfg(feature = "gecko")]
    pub fn from_animation_value_map(animation_value_map: &AnimationValueMap) -> Self {
        let len = animation_value_map.len();
        let mut declarations = Vec::with_capacity(len);
        let mut longhands = LonghandIdSet::new();

        for (property, animation_value) in animation_value_map.iter() {
          longhands.insert(*property);
          declarations.push(animation_value.uncompute());
        }

        PropertyDeclarationBlock {
            declarations,
            longhands,
            declarations_importance: SmallBitVec::from_elem(len as u32, false),
        }
    }

    /// Returns true if the declaration block has a CSSWideKeyword for the given
    /// property.
    #[cfg(feature = "gecko")]
    pub fn has_css_wide_keyword(&self, property: &PropertyId) -> bool {
        if let PropertyId::Longhand(id) = *property {
            if !self.longhands.contains(id) {
                return false
            }
        }
        self.declarations.iter().any(|decl|
            decl.id().is_or_is_longhand_of(property) &&
            decl.get_css_wide_keyword().is_some()
        )
    }

    /// Returns a custom properties map which is the result of cascading custom
    /// properties in this declaration block along with context's custom
    /// properties.
    pub fn cascade_custom_properties_with_context(
        &self,
        context: &Context,
    ) -> Option<Arc<::custom_properties::CustomPropertiesMap>> {
        self.cascade_custom_properties(context.style().custom_properties())
    }

    /// Returns a custom properties map which is the result of cascading custom
    /// properties in this declaration block along with the given custom
    /// properties.
    pub fn cascade_custom_properties(
        &self,
        inherited_custom_properties: Option<&Arc<::custom_properties::CustomPropertiesMap>>,
    ) -> Option<Arc<::custom_properties::CustomPropertiesMap>> {
        let mut builder = CustomPropertiesBuilder::new(inherited_custom_properties);

        for declaration in self.normal_declaration_iter() {
            if let PropertyDeclaration::Custom(ref name, ref value) = *declaration {
                builder.cascade(name, value.borrow());
            }
        }

        builder.build()
    }
}

impl ToCss for PropertyDeclarationBlock {
    // https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
        where W: fmt::Write,
    {
        let mut is_first_serialization = true; // trailing serializations should have a prepended space

        // Step 1 -> dest = result list

        // Step 2
        let mut already_serialized = PropertyDeclarationIdSet::new();

        // Step 3
        for (declaration, importance) in self.declaration_importance_iter() {
            // Step 3.1
            let property = declaration.id();

            // Step 3.2
            if already_serialized.contains(property) {
                continue;
            }

            // Step 3.3
            let shorthands = declaration.shorthands();
            if !shorthands.is_empty() {
                // Step 3.3.1 is done by checking already_serialized while
                // iterating below.

                // Step 3.3.2
                for &shorthand in shorthands {
                    let properties = shorthand.longhands();

                    // Substep 2 & 3
                    let mut current_longhands = SmallVec::<[_; 10]>::new();
                    let mut important_count = 0;
                    let mut found_system = None;

                    let is_system_font =
                        shorthand == ShorthandId::Font &&
                        self.declarations.iter().any(|l| {
                            !already_serialized.contains(l.id()) &&
                            l.get_system().is_some()
                        });

                    if is_system_font {
                        for (longhand, importance) in self.declaration_importance_iter() {
                            if longhand.get_system().is_some() || longhand.is_default_line_height() {
                                current_longhands.push(longhand);
                                if found_system.is_none() {
                                   found_system = longhand.get_system();
                                }
                                if importance.important() {
                                    important_count += 1;
                                }
                            }
                        }
                    } else {
                        for (longhand, importance) in self.declaration_importance_iter() {
                            if longhand.id().is_longhand_of(shorthand) {
                                current_longhands.push(longhand);
                                if importance.important() {
                                    important_count += 1;
                                }
                            }
                        }
                        // Substep 1:
                        //
                        // Assuming that the PropertyDeclarationBlock contains no
                        // duplicate entries, if the current_longhands length is
                        // equal to the properties length, it means that the
                        // properties that map to shorthand are present in longhands
                        if current_longhands.len() != properties.len() {
                            continue;
                        }
                    }

                    // Substep 4
                    let is_important = important_count > 0;
                    if is_important && important_count != current_longhands.len() {
                        continue;
                    }
                    let importance = if is_important {
                        Importance::Important
                    } else {
                        Importance::Normal
                    };

                    // Substep 5 - Let value be the result of invoking serialize
                    // a CSS value of current longhands.
                    let appendable_value =
                        match shorthand.get_shorthand_appendable_value(current_longhands.iter().cloned()) {
                            None => continue,
                            Some(appendable_value) => appendable_value,
                        };

                    // We avoid re-serializing if we're already an
                    // AppendableValue::Css.
                    let mut v = String::new();
                    let value = match (appendable_value, found_system) {
                        (AppendableValue::Css { css, with_variables }, _) => {
                            debug_assert!(!css.is_empty());
                            AppendableValue::Css {
                                css: css,
                                with_variables: with_variables,
                            }
                        }
                        #[cfg(feature = "gecko")]
                        (_, Some(sys)) => {
                            sys.to_css(&mut v)?;
                            AppendableValue::Css {
                                css: &v,
                                with_variables: false,
                            }
                        }
                        (other, _) => {
                            append_declaration_value(&mut v, other)?;

                            // Substep 6
                            if v.is_empty() {
                                continue;
                            }

                            AppendableValue::Css {
                                css: &v,
                                with_variables: false,
                            }
                        }
                    };

                    // Substeps 7 and 8
                    // We need to check the shorthand whether it's an alias property or not.
                    // If it's an alias property, it should be serialized like its longhand.
                    if shorthand.flags().contains(PropertyFlags::SHORTHAND_ALIAS_PROPERTY) {
                        append_serialization::<_, Cloned<slice::Iter< _>>, _>(
                             dest,
                             &property,
                             value,
                             importance,
                             &mut is_first_serialization)?;
                    } else {
                        append_serialization::<_, Cloned<slice::Iter< _>>, _>(
                             dest,
                             &shorthand,
                             value,
                             importance,
                             &mut is_first_serialization)?;
                    }

                    for current_longhand in &current_longhands {
                        // Substep 9
                        already_serialized.insert(current_longhand.id());
                    }

                    // FIXME(https://github.com/w3c/csswg-drafts/issues/1774)
                    // The specification does not include an instruction to abort
                    // the shorthand loop at this point, but doing so both matches
                    // Gecko and makes sense since shorthands are checked in
                    // preferred order.
                    break;
                }
            }

            // Step 3.3.4
            if already_serialized.contains(property) {
                continue;
            }

            use std::iter::Cloned;
            use std::slice;

            // Steps 3.3.5, 3.3.6 & 3.3.7
            // Need to specify an iterator type here even though it's unused to work around
            // "error: unable to infer enough type information about `_`;
            //  type annotations or generic parameter binding required [E0282]"
            // Use the same type as earlier call to reuse generated code.
            append_serialization::<_, Cloned<slice::Iter<_>>, _>(
                dest,
                &property,
                AppendableValue::Declaration(declaration),
                importance,
                &mut is_first_serialization)?;

            // Step 3.3.8
            already_serialized.insert(property);
        }

        // Step 4
        Ok(())
    }
}

/// A convenient enum to represent different kinds of stuff that can represent a
/// _value_ in the serialization of a property declaration.
pub enum AppendableValue<'a, I>
    where I: Iterator<Item=&'a PropertyDeclaration>,
{
    /// A given declaration, of which we'll serialize just the value.
    Declaration(&'a PropertyDeclaration),
    /// A set of declarations for a given shorthand.
    ///
    /// FIXME: This needs more docs, where are the shorthands expanded? We print
    /// the property name before-hand, don't we?
    DeclarationsForShorthand(ShorthandId, I),
    /// A raw CSS string, coming for example from a property with CSS variables,
    /// or when storing a serialized shorthand value before appending directly.
    Css {
        /// The raw CSS string.
        css: &'a str,
        /// Whether the original serialization contained variables or not.
        with_variables: bool,
    }
}

/// Potentially appends whitespace after the first (property: value;) pair.
fn handle_first_serialization<W>(dest: &mut W,
                                 is_first_serialization: &mut bool)
                                 -> fmt::Result
    where W: fmt::Write,
{
    if !*is_first_serialization {
        dest.write_str(" ")
    } else {
        *is_first_serialization = false;
        Ok(())
    }
}

/// Append a given kind of appendable value to a serialization.
pub fn append_declaration_value<'a, W, I>(dest: &mut W,
                                          appendable_value: AppendableValue<'a, I>)
                                          -> fmt::Result
    where W: fmt::Write,
          I: Iterator<Item=&'a PropertyDeclaration>,
{
    match appendable_value {
        AppendableValue::Css { css, .. } => {
            dest.write_str(css)
        },
        AppendableValue::Declaration(decl) => {
            decl.to_css(dest)
        },
        AppendableValue::DeclarationsForShorthand(shorthand, decls) => {
            shorthand.longhands_to_css(decls, dest)
        }
    }
}

/// Append a given property and value pair to a serialization.
pub fn append_serialization<'a, W, I, N>(dest: &mut W,
                                         property_name: &N,
                                         appendable_value: AppendableValue<'a, I>,
                                         importance: Importance,
                                         is_first_serialization: &mut bool)
                                         -> fmt::Result
    where W: fmt::Write,
          I: Iterator<Item=&'a PropertyDeclaration>,
          N: ToCss,
{
    handle_first_serialization(dest, is_first_serialization)?;

    property_name.to_css(dest)?;
    dest.write_char(':')?;

    // for normal parsed values, add a space between key: and value
    match appendable_value {
        AppendableValue::Declaration(decl) => {
            if !decl.value_is_unparsed() {
                // For normal parsed values, add a space between key: and value.
                dest.write_str(" ")?
            }
        },
        AppendableValue::Css { with_variables, .. } => {
            if !with_variables {
                dest.write_str(" ")?
            }
        }
        // Currently append_serialization is only called with a Css or
        // a Declaration AppendableValue.
        AppendableValue::DeclarationsForShorthand(..) => unreachable!(),
    }

    append_declaration_value(dest, appendable_value)?;

    if importance.important() {
        dest.write_str(" !important")?;
    }

    dest.write_char(';')
}

/// A helper to parse the style attribute of an element, in order for this to be
/// shared between Servo and Gecko.
pub fn parse_style_attribute<R>(input: &str,
                                url_data: &UrlExtraData,
                                error_reporter: &R,
                                quirks_mode: QuirksMode)
                                -> PropertyDeclarationBlock
    where R: ParseErrorReporter
{
    let context = ParserContext::new(Origin::Author,
                                     url_data,
                                     Some(CssRuleType::Style),
                                     ParsingMode::DEFAULT,
                                     quirks_mode);
    let error_context = ParserErrorContext { error_reporter: error_reporter };
    let mut input = ParserInput::new(input);
    parse_property_declaration_list(&context, &error_context, &mut Parser::new(&mut input))
}

/// Parse a given property declaration. Can result in multiple
/// `PropertyDeclaration`s when expanding a shorthand, for example.
///
/// This does not attempt to parse !important at all.
pub fn parse_one_declaration_into<R>(
    declarations: &mut SourcePropertyDeclaration,
    id: PropertyId,
    input: &str,
    url_data: &UrlExtraData,
    error_reporter: &R,
    parsing_mode: ParsingMode,
    quirks_mode: QuirksMode
) -> Result<(), ()>
where
    R: ParseErrorReporter
{
    let context = ParserContext::new(Origin::Author,
                                     url_data,
                                     Some(CssRuleType::Style),
                                     parsing_mode,
                                     quirks_mode);
    let mut input = ParserInput::new(input);
    let mut parser = Parser::new(&mut input);
    let start_position = parser.position();
    parser.parse_entirely(|parser| {
        let name = id.name().into();
        PropertyDeclaration::parse_into(declarations, id, name, &context, parser)
            .map_err(|e| e.into())
    }).map_err(|err| {
        let location = err.location;
        let error = ContextualParseError::UnsupportedPropertyDeclaration(
            parser.slice_from(start_position), err);
        let error_context = ParserErrorContext { error_reporter: error_reporter };
        context.log_css_error(&error_context, location, error);
    })
}

/// A struct to parse property declarations.
struct PropertyDeclarationParser<'a, 'b: 'a> {
    context: &'a ParserContext<'b>,
    declarations: &'a mut SourcePropertyDeclaration,
}

/// Default methods reject all at rules.
impl<'a, 'b, 'i> AtRuleParser<'i> for PropertyDeclarationParser<'a, 'b> {
    type PreludeNoBlock = ();
    type PreludeBlock = ();
    type AtRule = Importance;
    type Error = StyleParseErrorKind<'i>;
}

/// Based on NonMozillaVendorIdentifier from Gecko's CSS parser.
fn is_non_mozilla_vendor_identifier(name: &str) -> bool {
    (name.starts_with("-") && !name.starts_with("-moz-")) ||
        name.starts_with("_")
}

impl<'a, 'b, 'i> DeclarationParser<'i> for PropertyDeclarationParser<'a, 'b> {
    type Declaration = Importance;
    type Error = StyleParseErrorKind<'i>;

    fn parse_value<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
                       -> Result<Importance, ParseError<'i>> {
        let prop_context = PropertyParserContext::new(self.context);
        let id = match PropertyId::parse(&name, Some(&prop_context)) {
            Ok(id) => id,
            Err(()) => {
                return Err(input.new_custom_error(if is_non_mozilla_vendor_identifier(&name) {
                    StyleParseErrorKind::UnknownVendorProperty
                } else {
                    StyleParseErrorKind::UnknownProperty(name)
                }));
            }
        };
        input.parse_until_before(Delimiter::Bang, |input| {
            PropertyDeclaration::parse_into(self.declarations, id, name, self.context, input)
                .map_err(|e| e.into())
        })?;
        let importance = match input.try(parse_important) {
            Ok(()) => Importance::Important,
            Err(_) => Importance::Normal,
        };
        // In case there is still unparsed text in the declaration, we should roll back.
        input.expect_exhausted()?;
        Ok(importance)
    }
}

/// Parse a list of property declarations and return a property declaration
/// block.
pub fn parse_property_declaration_list<R>(
    context: &ParserContext,
    error_context: &ParserErrorContext<R>,
    input: &mut Parser,
) -> PropertyDeclarationBlock
where
    R: ParseErrorReporter
{
    let mut declarations = SourcePropertyDeclaration::new();
    let mut block = PropertyDeclarationBlock::new();
    let parser = PropertyDeclarationParser {
        context: context,
        declarations: &mut declarations,
    };
    let mut iter = DeclarationListParser::new(input, parser);
    while let Some(declaration) = iter.next() {
        match declaration {
            Ok(importance) => {
                block.extend(
                    iter.parser.declarations.drain(),
                    importance,
                    DeclarationSource::Parsing,
                );
            }
            Err((error, slice)) => {
                iter.parser.declarations.clear();

                // If the unrecognized property looks like a vendor-specific property,
                // silently ignore it instead of polluting the error output.
                if let ParseErrorKind::Custom(StyleParseErrorKind::UnknownVendorProperty) = error.kind {
                    continue;
                }

                let location = error.location;
                let error = ContextualParseError::UnsupportedPropertyDeclaration(slice, error);
                context.log_css_error(error_context, location, error);
            }
        }
    }
    block
}


text:
module Admin::CompanyUsersHelper
end

redpajama_set_name: RedPajamaGithub


text:
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala

import scala.scalajs.js

/** This class provides a simple way to get unique objects for equal strings.
 *  Since symbols are interned, they can be compared using reference equality.
 *  Instances of `Symbol` can be created easily with Scala's built-in quote
 *  mechanism.
 *
 *  For instance, the [[http://scala-lang.org/#_top Scala]] term `'mysym` will
 *  invoke the constructor of the `Symbol` class in the following way:
 *  `Symbol("mysym")`.
 *
 *  @author  Martin Odersky, Iulian Dragos
 *  @version 1.8
 */
final class Symbol private (val name: String) extends Serializable {
  /** Converts this symbol to a string.
   */
  override def toString(): String = "'" + name

  @throws(classOf[java.io.ObjectStreamException])
  private def readResolve(): Any = Symbol.apply(name)
  override def hashCode = name.hashCode()
  override def equals(other: Any) = this eq other.asInstanceOf[AnyRef]
}

// Modified to use Scala.js specific cache
object Symbol extends JSUniquenessCache[Symbol] {
  override def apply(name: String): Symbol = super.apply(name)
  protected def valueFromKey(name: String): Symbol = new Symbol(name)
  protected def keyFromValue(sym: Symbol): Option[String] = Some(sym.name)
}

private[scala] abstract class JSUniquenessCache[V]
{
  private val map = js.Dictionary.empty[js.Any] // V | js.Undefined

  protected def valueFromKey(k: String): V
  protected def keyFromValue(v: V): Option[String]

  def apply(name: String): V = {
    val cachedSym = map(name)
    if (js.isUndefined(cachedSym)) {
      val sym = valueFromKey(name)
      map(name) = sym.asInstanceOf[js.Any]
      sym.asInstanceOf[V]
    } else {
      cachedSym.asInstanceOf[V]
    }
  }

  def unapply(other: V): Option[String] = keyFromValue(other)
}

/** This is private so it won't appear in the library API, but
  * abstracted to offer some hope of reusability.  */
/* DELETED for Scala.js
private[scala] abstract class UniquenessCache[K >: js.String, V >: Null]
{

  import java.lang.ref.WeakReference
  import java.util.WeakHashMap
  import java.util.concurrent.locks.ReentrantReadWriteLock

  private val rwl = new ReentrantReadWriteLock()
  private val rlock = rwl.readLock
  private val wlock = rwl.writeLock
  private val map = new WeakHashMap[K, WeakReference[V]]

  protected def valueFromKey(k: K): V
  protected def keyFromValue(v: V): Option[K]

  def apply(name: K): V = {
    def cached(): V = {
      rlock.lock
      try {
        val reference = map get name
        if (reference == null) null
        else reference.get  // will be null if we were gc-ed
      }
      finally rlock.unlock
    }
    def updateCache(): V = {
      wlock.lock
      try {
        val res = cached()
        if (res != null) res
        else {
          // If we don't remove the old String key from the map, we can
          // wind up with one String as the key and a different String as
          // as the name field in the Symbol, which can lead to surprising
          // GC behavior and duplicate Symbols. See SI-6706.
          map remove name
          val sym = valueFromKey(name)
          map.put(name, new WeakReference(sym))
          sym
        }
      }
      finally wlock.unlock
    }

    val res = cached()
    if (res == null) updateCache()
    else res
  }
  def unapply(other: V): Option[K] = keyFromValue(other)
}
*/


text:
<!--
  ~ Copyright 2015 XXX
  ~
  ~ 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.
  -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>xxx.web.comments</groupId>
		<artifactId>xxx.web.comments</artifactId>
		<version>0.1-SNAPSHOT</version>
	</parent>

	<artifactId>xxx.web.comments.types</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.apache.uima</groupId>
			<artifactId>uimaj-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.uima</groupId>
			<artifactId>uimafit-core</artifactId>
		</dependency>

		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<!--generate types dynamically-->
				<groupId>org.apache.uima</groupId>
				<artifactId>jcasgen-maven-plugin</artifactId>
				<version>2.4.2</version>
				<configuration>
					<typeSystemIncludes>
						<include>src/main/resources/desc/types/WebArgumentTypeSystem.xml</include>
						<include>src/main/resources/desc/types/EmbeddingsAnnotation.xml</include>
					</typeSystemIncludes>
				</configuration>
				<executions>
					<execution>
						<!--call it in the generate-source phase-->
						<phase>generate-sources</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>1.8</version>
				<executions>
					<execution>
						<id>addToSourceFolder</id>
						<goals>
							<!--add the generated sources-->
							<goal>add-source</goal>
						</goals>
						<phase>process-sources</phase>
						<configuration>
							<sources>
								<!--default path to generated sources-->
								<source>${project.build.directory}/generated-sources/jcasgen
								</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>


text:
def extractTheWorldOnTheOtherSide(item):
	"""
	'The World On The Other Side…'
	"""
	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol or frag) or 'preview' in item['title'].lower():
		return None

	tagmap = [
		('Drop!! ~A Tale of the Fragrance Princess~',       'Drop!! ~A Tale of the Fragrance Princess~',                      'translated'),
		('I\'ll Live My Second Life!',                      'I\'ll Live My Second Life!',                                     'translated'),
	]

	for tagname, name, tl_type in tagmap:
		if tagname in item['tags']:
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	titlemap = [
		('Second Life! Chapter',                      'I\'ll Live My Second Life!',                                     'translated'),
	]

	for titlecomponent, name, tl_type in titlemap:
		if titlecomponent.lower() in item['title'].lower():
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


text:
package io.wearturilo.dependencyinjection;

import android.content.Context;
import android.location.LocationManager;
import dagger.Module;
import dagger.Provides;

@Module
public class SystemModule {

    private final  Context context;

    public SystemModule(Context context){
        this.context = context;
    }

    @Provides
    LocationManager provideLocationManager(){
           return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

}


text:
require 'limbo'
require 'rails/all'
require 'vcr'

module Dummy
  class Application < Rails::Application
  end
end

Dummy::Application.configure do
  config.active_support.deprecation = :log
end

Dummy::Application.initialize!

RSpec.configure do |config|
  config.fail_fast = true
  config.extend VCR::RSpec::Macros
end

VCR.configure do |config|
  config.cassette_library_dir = 'spec/cassettes'
  config.hook_into :fakeweb
end

def request(options = {})
  port = options.fetch(:port) { 80 }
  env = {
    'HTTP_HOST' => "example.com:#{port}",
    'rack.url_scheme' => 'http',
    'SCRIPT_NAME' => '',
    'PATH_INFO' => '/blog'
  }
  ActionDispatch::Request.new(env)
end


text:
namespace YAF.SampleWebApplication
{
    using System;
    using System.Web.Mvc;
    using System.Web.Routing;

    using YAF.Core.Context;

    /// <summary>
    /// The global.
    /// </summary>
    public class Global : YafHttpApplication
    {
        /// <summary>
        /// The application start.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles();

            base.Application_Start(sender, e);
        }
    }
}


text:
<?php

namespace Framework\Providers;

use Hubzero\Language\Translator;
use Hubzero\Base\ServiceProvider;

/**
 * Language translation service provider
 *
 * @codeCoverageIgnore
 */
class TranslationServiceProvider extends ServiceProvider
{
	/**
	 * Register the service provider.
	 *
	 * @return  void
	 */
	public function register()
	{
		$this->app['language'] = function($app)
		{
			$locale = $app['config']->get('locale', 'en-GB');
			$debug  = $app['config']->get('debug_lang', false);

			return new Translator($locale, $debug, $app['client']->name);
		};

		$this->app['language.filter'] = false;
	}

	/**
	 * Add the plugin loader to the event dispatcher.
	 *
	 * @return  void
	 */
	public function boot()
	{
		$translator = $this->app['language'];

		$language = null;

		// If a language was specified it has priority
		if (!$language && $this->app->has('request') && $this->app->isSite())
		{
			$lang = $this->app['request']->getString('language', null);

			if ($lang && $translator->exists($lang))
			{
				$language = $lang;
			}
		}

		// Detect user specified language
		if (!$language && $this->app->has('user'))
		{
			$lang = \User::getParam($this->app['client']->alias . '_language');

			if ($lang && $translator->exists($lang))
			{
				$language = $lang;
			}
		}

		// Detect browser language
		if (!$language && $this->app->has('browser') && $this->app->isSite())
		{
			$lang = $translator->detectLanguage();

			if ($lang && $translator->exists($lang))
			{
				$language = $lang;
			}
		}

		// Detect default language
		if (!$language && $this->app->has('component'))
		{
			$params = $this->app['component']->params('com_languages');

			$language = $params->get(
				$this->app['client']->name,
				$this->app['config']->get('language', 'en-GB')
			);
		}

		// One last check to make sure we have something
		if (!$language || !$translator->exists($language))
		{
			$lang = $this->app['config']->get('language', 'en-GB');

			if ($translator->exists($lang))
			{
				$language = $lang;
			}
		}

		if ($language)
		{
			$translator->setLanguage($language);
		}

		$boot = DS . 'bootstrap' . DS . ucfirst($this->app['client']->name);

		$translator->load('lib_hubzero', PATH_APP . $boot, null, false, true) ||
		$translator->load('lib_hubzero', PATH_CORE . $boot, null, false, true);
	}
}


text:
module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    class BbsGateway < Gateway
      TEST_URL = 'https://epayment-test.bbs.no/Terminal/'
      LIVE_URL = 'https://epayment.bbs.no/Terminal/'

      # The countries the gateway supports merchants from as 2 digit ISO country codes
      self.supported_countries = ['NO']

      # The card types supported by the payment gateway
      self.supported_cardtypes = [:visa, :master]

      # The homepage URL of the gateway
      self.homepage_url = 'http://www.betalingsterminal.no/Netthandel-forside/'

      # The name of the gateway
      self.display_name = 'Bankenes Betalingssentral (BBS)'

      self.default_currency = 'NOK'
      self.money_format = :cents

      SERVICE_TYPES = {
        :netaxept_hosted => "B",
        :merchant_hosted => "M",
        :call_center => "C"
      }

      def initialize(options = {})
        requires!(options, :login, :password)
        @options = options
        super
      end

      def setup(money, options = {})
        requires!(options, :currency_code, :order_id, :redirect_url)
        post = {}
        add_invoice(post, options)
        add_money(post, money)
        add_currency_code(post, money, options)
        add_redirect_url(post, options)
        add_service_type(post, options)
        add_description(post, options)
        add_force_3dsecure(post, options)

        commit('Register', post)
      end

      def authorize(transaction_id)
        post = {}
        add_transaction_id(transaction_id)
        commit('Auth', post)
      end

      def purchase(money, creditcard, options = {})
        post = {}
        add_invoice(post, options)
        add_creditcard(post, creditcard)
        add_address(post, creditcard, options)
        add_customer_data(post, options)

        commit('Sale', money, post)
      end

      def capture(money, authorization, options = {})
        commit('Capture', money, post)
      end

      private

      def add_customer_data(post, options)
      end

      def add_invoice(post, options)
        post[:orderNumber] = options[:order_id] unless options[:order_id].blank?
      end

      def add_money(post, money)
        post[:amount] = amount(money)
      end

      def add_currency_code(post, money, options)
        post[:currencyCode] = options[:currency] || currency(money)
      end

      def add_redirect_url(post, options)
        post[:redirectUrl] = options[:redirect_url] unless options[:redirect_url].blank?
      end

      def add_service_type(post, options)
        post[:serviceType] = service_type(options[:service_type]) unless options[:service_type].blank?
      end

      def add_description(post, options)
        post[:description] = options[:description] unless options[:description].blank?
      end

      def add_force_3dsecure(post, options)
        unless options[:force_3d_secure].blank?
          post[:force3DSecure] = (!!options[:force_3d_secure])? 1 : 0
        end
      end

      def parse(body)
      end

      def commit(action, parameters)
        url = "#{base_url}Process.aspx"

        case action
        when "Register"
          url = "#{base_url}Register.aspx"
        when "Auth"
          parameters[:operation] = "AUTH"
        when "Capture"
          parameters[:operation] = "CAPTURE"
        when "Query"
          url = "#{base_url}Query.aspx"
        end

        post_data(url, parameters)
      end

      def message_from(response)
      end

      def post_data(action, parameters = {})
      end

      def service_type(option)
        SERVICE_TYPES[option] || "B"
      end

      def base_url
        (test?)? TEST_URL : LIVE_URL
      end
    end
  end
end


text:
<?php

/**
 * ImportItems
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    siwapp
 * @subpackage model
 * @author     Your name here
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class ImportItems extends PluginImportItems
{
}


text:
#if FEATURE_REFEMIT

#if FEATURE_CORE_DLR
using System.Linq.Expressions;
#else
using Microsoft.Scripting.Ast;
#endif

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting {
    /// <summary>
    /// ScriptCode is an instance of compiled code that is bound to a specific LanguageContext
    /// but not a specific ScriptScope. The code can be re-executed multiple times in different
    /// scopes. Hosting API counterpart for this class is <c>CompiledCode</c>.
    /// </summary>
    public abstract class SavableScriptCode : ScriptCode {

        protected SavableScriptCode(SourceUnit sourceUnit)
            : base(sourceUnit) {
        }

        class CodeInfo {
            public readonly MethodBuilder Builder;
            public readonly ScriptCode Code;
            public readonly Type DelegateType;

            public CodeInfo(MethodBuilder builder, ScriptCode code, Type delegateType) {
                Builder = builder;
                Code = code;
                DelegateType = delegateType;
            }
        }

        /// <summary>
        /// This takes an assembly name including extension and saves the provided ScriptCode objects into the assembly.
        ///
        /// The provided script codes can constitute code from multiple languages.  The assemblyName can be either a fully qualified
        /// or a relative path.  The DLR will simply save the assembly to the desired location.  The assembly is created by the DLR and
        /// if a file already exists than an exception is raised.
        ///
        /// The DLR determines the internal format of the ScriptCode and the DLR can feel free to rev this as appropriate.
        /// </summary>
        public static void SaveToAssembly(string assemblyName, params SavableScriptCode[] codes) {
            ContractUtils.RequiresNotNull(assemblyName, "assemblyName");
            ContractUtils.RequiresNotNullItems(codes, "codes");

#if FEATURE_FILESYSTEM
            // break the assemblyName into it's dir/name/extension
            string dir = Path.GetDirectoryName(assemblyName);
            if (String.IsNullOrEmpty(dir)) {
                dir = Environment.CurrentDirectory;
            }
#else
            string dir = null;
#endif

            string name = Path.GetFileNameWithoutExtension(assemblyName);
            string ext = Path.GetExtension(assemblyName);

            // build the assembly & type gen that all the script codes will live in...
            AssemblyGen ag = new AssemblyGen(new AssemblyName(name), dir, ext, /*emitSymbols*/false);
            TypeBuilder tb = ag.DefinePublicType("DLRCachedCode", typeof(object), true);
            TypeGen tg = new TypeGen(ag, tb);
            // then compile all of the code

            Dictionary<Type, List<CodeInfo>> langCtxBuilders = new Dictionary<Type, List<CodeInfo>>();
            foreach (SavableScriptCode sc in codes) {
                List<CodeInfo> builders;
                if (!langCtxBuilders.TryGetValue(sc.LanguageContext.GetType(), out builders)) {
                    langCtxBuilders[sc.LanguageContext.GetType()] = builders = new List<CodeInfo>();
                }

                KeyValuePair<MethodBuilder, Type> compInfo = sc.CompileForSave(tg);

                builders.Add(new CodeInfo(compInfo.Key, sc, compInfo.Value));
            }

            MethodBuilder mb = tb.DefineMethod(
                "GetScriptCodeInfo",
                MethodAttributes.SpecialName | MethodAttributes.Public | MethodAttributes.Static,
                typeof(MutableTuple<Type[], Delegate[][], string[][], string[][]>),
                ReflectionUtils.EmptyTypes);

            ILGen ilgen = new ILGen(mb.GetILGenerator());

            var langsWithBuilders = langCtxBuilders.ToArray();

            // lang ctx array
            ilgen.EmitArray(typeof(Type), langsWithBuilders.Length, (index) => {
                ilgen.Emit(OpCodes.Ldtoken, langsWithBuilders[index].Key);
                ilgen.EmitCall(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }));
            });

            // builders array of array
            ilgen.EmitArray(typeof(Delegate[]), langsWithBuilders.Length, (index) => {
                List<CodeInfo> builders = langsWithBuilders[index].Value;

                ilgen.EmitArray(typeof(Delegate), builders.Count, (innerIndex) => {
                    ilgen.EmitNull();
                    ilgen.Emit(OpCodes.Ldftn, builders[innerIndex].Builder);
                    ilgen.EmitNew(
                        builders[innerIndex].DelegateType,
                        new[] { typeof(object), typeof(IntPtr) }
                    );
                });
            });

            // paths array of array
            ilgen.EmitArray(typeof(string[]), langsWithBuilders.Length, (index) => {
                List<CodeInfo> builders = langsWithBuilders[index].Value;

                ilgen.EmitArray(typeof(string), builders.Count, (innerIndex) => {
                    ilgen.EmitString(builders[innerIndex].Code.SourceUnit.Path);
                });
            });

            // 4th element in tuple - custom per-language data
            ilgen.EmitArray(typeof(string[]), langsWithBuilders.Length, (index) => {
                List<CodeInfo> builders = langsWithBuilders[index].Value;

                ilgen.EmitArray(typeof(string), builders.Count, (innerIndex) => {
                    ICustomScriptCodeData data = builders[innerIndex].Code as ICustomScriptCodeData;
                    if (data != null) {
                        ilgen.EmitString(data.GetCustomScriptCodeData());
                    } else {
                        ilgen.Emit(OpCodes.Ldnull);
                    }
                });
            });

            ilgen.EmitNew(
                typeof(MutableTuple<Type[], Delegate[][], string[][], string[][]>),
                new[] { typeof(Type[]), typeof(Delegate[][]), typeof(string[][]), typeof(string[][]) }
            );
            ilgen.Emit(OpCodes.Ret);

            mb.SetCustomAttribute(new CustomAttributeBuilder(
                typeof(DlrCachedCodeAttribute).GetConstructor(ReflectionUtils.EmptyTypes),
                ArrayUtils.EmptyObjects
            ));

            tg.FinishType();
            ag.SaveAssembly();
        }

        /// <summary>
        /// This will take an assembly object which the user has loaded and return a new set of ScriptCode's which have
        /// been loaded into the provided ScriptDomainManager.
        ///
        /// If the language associated with the ScriptCode's has not already been loaded the DLR will load the
        /// LanguageContext into the ScriptDomainManager based upon the saved LanguageContext type.
        ///
        /// If the LanguageContext or the version of the DLR the language was compiled against is unavailable a
        /// TypeLoadException will be raised unless policy has been applied by the administrator to redirect bindings.
        /// </summary>
        public static ScriptCode[] LoadFromAssembly(ScriptDomainManager runtime, Assembly assembly) {
            ContractUtils.RequiresNotNull(runtime, "runtime");
            ContractUtils.RequiresNotNull(assembly, "assembly");

            // get the type which has our cached code...
            Type t = assembly.GetType("DLRCachedCode");
            if (t == null) {
                return new ScriptCode[0];
            }

            List<ScriptCode> codes = new List<ScriptCode>();

            MethodInfo mi = t.GetMethod("GetScriptCodeInfo");
            if (mi.IsSpecialName && mi.IsDefined(typeof(DlrCachedCodeAttribute), false)) {
                var infos = (MutableTuple<Type[], Delegate[][], string[][], string[][]>)mi.Invoke(null, ArrayUtils.EmptyObjects);

                for (int i = 0; i < infos.Item000.Length; i++) {
                    Type curType = infos.Item000[i];
                    LanguageContext lc = runtime.GetLanguage(curType);

                    Debug.Assert(infos.Item001[i].Length == infos.Item002[i].Length);

                    Delegate[] methods = infos.Item001[i];
                    string[] names = infos.Item002[i];
                    string[] customData = infos.Item003[i];

                    for (int j = 0; j < methods.Length; j++) {
                        codes.Add(lc.LoadCompiledCode(methods[j], names[j], customData[j]));
                    }
                }
            }

            return codes.ToArray();
        }

        protected LambdaExpression RewriteForSave(TypeGen typeGen, LambdaExpression code) {
            var diskRewriter = new ToDiskRewriter(typeGen);
            return diskRewriter.RewriteLambda(code);
        }

        protected virtual KeyValuePair<MethodBuilder, Type> CompileForSave(TypeGen typeGen) {
            throw new NotSupportedException();
        }

        public override string ToString() {
            return String.Format("ScriptCode '{0}' from {1}", SourceUnit.Path, LanguageContext.GetType().Name);
        }
    }
}
#endif


text:
<?php

namespace HAProxy\Config\Tests;

use HAProxy\Config\Comment;
use HAProxy\Config\Resolvers;
use PHPUnit\Framework\TestCase;

class ResolversTest extends TestCase
{
    /**
     * @var Resolvers
     */
    private $resolvers;

    protected function setUp(): void
    {
        $this->resolvers = new Resolvers('test');
    }

    public function testConstruct()
    {
        $resolvers = new Resolvers('test');
        $this->assertInstanceOf('HAProxy\Config\Resolvers', $resolvers);
    }

    public function testFactory()
    {
        $resolvers = Resolvers::create('test');
        $this->assertInstanceOf('HAProxy\Config\Resolvers', $resolvers);
    }

    public function testName()
    {
        $resolvers = new Resolvers('test');

        $this->assertEquals(
            'test',
            $resolvers->getName()
        );
    }

    public function testAddNameserver()
    {
        $this->resolvers
            ->addNameserver('google', '8.8.8.8', 53)
        ;

        $this->assertTrue(
            $this->resolvers->nameserverExists('google')
        );

        $this->assertEquals(
            ['ip' => '8.8.8.8', 'port' => 53],
            $this->resolvers->getNameserver('google')
        );
    }

    public function testRemoveNameserver()
    {
        $this->resolvers
            ->addNameserver('google', '8.8.8.8', 53)
            ->addNameserver('cloudflare', '1.1.1.1', 53)
        ;

        $this->resolvers
            ->removeNameserver('google')
        ;

        $this->assertFalse(
            $this->resolvers->nameserverExists('google')
        );
        $this->assertTrue(
            $this->resolvers->nameserverExists('cloudflare')
        );
    }

    public function testAddNameserverThroughAddParameter()
    {
        $this->expectException('HAProxy\Config\Exception\InvalidParameterException');
        $this->expectExceptionMessage('Please use the addNameserver() method to add nameservers!');

        $this->resolvers->addParameter('nameserver', 'google 8.8.8.8:53');
    }

    public function testAddParameter()
    {
        $this->resolvers->addParameter('resolve_retries', 3);
        $this->resolvers->addParameter('timeout', 'retry 1s');
        $this->resolvers->addParameter('hold', ['valid', '60s']);

        $this->assertTrue($this->resolvers->parameterExists('resolve_retries'));
        $this->assertEquals([3], $this->resolvers->getParameter('resolve_retries'));

        $this->assertTrue($this->resolvers->parameterExists('timeout retry'));
        $this->assertEquals(['1s'], $this->resolvers->getParameter('timeout retry'));

        $this->assertTrue($this->resolvers->parameterExists('hold'));
        $this->assertEquals(['valid', '60s'], $this->resolvers->getParameter('hold'));
    }

    public function testSetComment()
    {
        $this->assertFalse(
            $this->resolvers->hasComment()
        );

        $this->resolvers->setComment(new Comment("Hello world, I'm a comment!"));

        $this->assertTrue(
            $this->resolvers->hasComment()
        );

        $commend = $this->resolvers->getComment();

        $this->assertEquals(new Comment("Hello world, I'm a comment!"), $commend);
    }

    public function testRemoveComment()
    {
        $this->resolvers->setComment(new Comment("Hello world, I'm a comment!"))
        ;

        $this->assertTrue(
            $this->resolvers->hasComment()
        );

        $this->resolvers->removeComment();

        $this->assertFalse(
            $this->resolvers->hasComment()
        );
    }
}


text:
from random import randint

import logging
import requests

XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/info.0.json'
LATEST_XKCD_URL = 'https://xkcd.com/info.0.json'

class XkcdHandler(object):
    '''
    This plugin provides several commands that can be used for fetch a comic
    strip from https://xkcd.com. The bot looks for messages starting with
    "@xkcd" and responds with a message with the comic based on provided
    commands.
    '''

    def usage(self):
        return '''
            This plugin allows users to fetch a comic strip provided by
            https://xkcd.com. Users should preface the command with "@xkcd".

            There are several commands to use this bot:
            - @xkcd help -> To show all commands the bot supports.
            - @xkcd latest -> To fetch the latest comic strip from xkcd.
            - @xkcd random -> To fetch a random comic strip from xkcd.
            - @xkcd <comic_id> -> To fetch a comic strip based on
            `<comic_id>`, e.g `@xkcd 1234`.
            '''

    def triage_message(self, message, client):
        original_content = message['content']
        is_xkcd_called = original_content.startswith('@xkcd ')
        is_xkcd_called_without_command = original_content == '@xkcd'

        return is_xkcd_called or is_xkcd_called_without_command

    def handle_message(self, message, client, state_handler):
        xkcd_bot_response = get_xkcd_bot_response(message)

        client.send_message(dict(
            type='stream',
            to=message['display_recipient'],
            subject=message['subject'],
            content=xkcd_bot_response,
        ))

class XkcdBotCommand(object):
    LATEST = 0
    RANDOM = 1
    COMIC_ID = 2

class XkcdNotFoundError(Exception):
    pass

class XkcdServerError(Exception):
    pass

def get_xkcd_bot_response(message):
    original_content = message['content'].strip()
    cropped = original_content[len('@xkcd '):]
    command = cropped.strip()

    xkcd_called_without_command = original_content == '@xkcd'

    commands_help = ("%s"
                     "\n* `@xkcd help` to show this help message."
                     "\n* `@xkcd latest` to fetch the latest comic strip from xkcd."
                     "\n* `@xkcd random` to fetch a random comic strip from xkcd."
                     "\n* `@xkcd <comic id>` to fetch a comic strip based on `<comic id>` "
                     "e.g `@xkcd 1234`.")

    try:
        if command == 'help' or xkcd_called_without_command:
            return commands_help % ('xkcd bot supports these commands:')
        elif command == 'latest':
            fetched = fetch_xkcd_query(XkcdBotCommand.LATEST)
        elif command == 'random':
            fetched = fetch_xkcd_query(XkcdBotCommand.RANDOM)
        elif command.isdigit():
            fetched = fetch_xkcd_query(XkcdBotCommand.COMIC_ID, cropped.strip())
        else:
            return commands_help % ('xkcd bot only supports these commands:')
    except (requests.exceptions.ConnectionError, XkcdServerError):
        logging.exception('Connection error occurred when trying to connect to xkcd server')
        return 'Sorry, I cannot process your request right now, please try again later!'
    except XkcdNotFoundError:
        logging.exception('XKCD server responded 404 when trying to fetch comic with id %s'
                          % (command))
        return 'Sorry, there is likely no xkcd comic strip with id: #%s' % (command,)
    else:
        return ("#%s: **%s**\n[%s](%s)" % (fetched['num'],
                                           fetched['title'],
                                           fetched['alt'],
                                           fetched['img']))

def fetch_xkcd_query(mode, comic_id=None):
    try:
        if mode == XkcdBotCommand.LATEST:  # Fetch the latest comic strip.
            url = LATEST_XKCD_URL

        elif mode == XkcdBotCommand.RANDOM:  # Fetch a random comic strip.
            latest = requests.get(LATEST_XKCD_URL)

            if latest.status_code != 200:
                raise XkcdServerError()

            latest_id = latest.json()['num']
            random_id = randint(1, latest_id)
            url = XKCD_TEMPLATE_URL % (str(random_id))

        elif mode == XkcdBotCommand.COMIC_ID:  # Fetch specific comic strip by id number.
            if comic_id is None:
                raise Exception('Missing comic_id argument')
            url = XKCD_TEMPLATE_URL % (comic_id)

        fetched = requests.get(url)

        if fetched.status_code == 404:
            raise XkcdNotFoundError()
        elif fetched.status_code != 200:
            raise XkcdServerError()

        xkcd_json = fetched.json()
    except requests.exceptions.ConnectionError as e:
        logging.warning(e)
        raise

    return xkcd_json

handler_class = XkcdHandler


text:
UINavigationController-Blocks
=============================

UINavigationController category with push and pop blocks

##DEPRECATED##

Please use https://github.com/martinrybak/MRNavigationController instead


text:
SYNONYM

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Annls mycol. 13(3/4): 399 (1915)

#### Original name
Dothidea aloëtica Berk. & M.A. Curtis, 1868

### Remarks
null


text:
<?php
  require_once('includes/load.php');
  if (!$session->isUserLoggedIn(true)) { redirect('index.php', false);}
?>
<?php
  $d_user = find_by_user_id((int)$_GET['id']);
  if(!$d_user){
    $session->msg("d","Missing user id.");
    redirect('users.php');
  }
?>
<?php
  $delete_id = delete_user_by_id((int)$d_user['id']);
  if($delete_id){
      $session->msg("s","User deleted.");
      redirect('users.php');
  } else {
      $session->msg("d","user deletion failed.");
      redirect('sales.php');
  }
?>


text:
# HOW TO USE - GUIDE:

Define below described properties to highlight inside the file
and set their values to true/false as per your need

[![Latest Version](https://img.shields.io/packagist/v/nexuslinkservices/link-email-highlighter.svg?style=flat-square)](https://packagist.org/packages/nexuslinkservices/link-email-highlighter)
[![Software License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nexuslinkservices/email-link-highlighter/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nexuslinkservices/email-link-highlighter/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/nexuslinkservices/email-link-highlighter/badges/build.png?b=master)](https://scrutinizer-ci.com/g/nexuslinkservices/email-link-highlighter/build-status/master)

i.e.:  I want to highlight only links in my content, so I need to
define following variables in my configuration yaml file.

```
highlighting_enabled:
    link: true
    mail: false
```

```
Note(s):
1. If configuration yaml is not there in your project then the default configuration will be taken into consideration.
2. The bundle uses symfony yaml component to parse the yaml configuration.
```

Following is the reference code to make it working,

```
/**
 * CODE
 * create a new object of HighLighter class
 */

$highLighter = new HighLighter($pathToYaml);  //$pathToYaml is optional as mentioned earlier
$highLightedContent = $highLighter->HighLight($contentToBeHighlighted);
```

That's it with the bundle.

## CONTRIBUTING:

Pull requests are always welcome.


text:
package org.apache.beam.sdk.schemas.utils;

import com.google.auto.value.AutoValue;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.beam.sdk.annotations.Experimental;
import org.apache.beam.sdk.annotations.Experimental.Kind;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.apache.beam.sdk.schemas.Schema.TypeName;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;

/**
 * Visitor that zips schemas, and accepts pairs of fields and their types.
 *
 * <p>Values returned by `accept` are accumulated.
 */
@Experimental(Kind.SCHEMAS)
@SuppressWarnings({
  "nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public abstract class SchemaZipFold<T> implements Serializable {

  public final T apply(Schema left, Schema right) {
    return visit(this, Context.EMPTY, FieldType.row(left), FieldType.row(right));
  }

  /** Accumulate two results together. */
  public abstract T accumulate(T left, T right);

  /** Accepts two components, context.parent() is always ROW, MAP, ARRAY or absent. */
  public abstract T accept(Context context, FieldType left, FieldType right);

  /** Accepts two fields, context.parent() is always ROW. */
  public abstract T accept(Context context, Optional<Field> left, Optional<Field> right);

  /** Context referring to a current position in a schema. */
  @AutoValue
  public abstract static class Context {
    /** Field path from a root of a schema. */
    public abstract List<String> path();

    /** Type of parent node in a tree. */
    public abstract Optional<TypeName> parent();

    public static final Context EMPTY = Context.create(Collections.emptyList(), Optional.empty());

    public Context withPathPart(String part) {
      return create(ImmutableList.<String>builder().addAll(path()).add(part).build(), parent());
    }

    public Context withParent(TypeName parent) {
      return create(path(), Optional.of(parent));
    }

    public static Context create(List<String> path, Optional<TypeName> parent) {
      return new AutoValue_SchemaZipFold_Context(path, parent);
    }
  }

  static <T> T visit(SchemaZipFold<T> zipFold, Context context, FieldType left, FieldType right) {
    if (left.getTypeName() != right.getTypeName()) {
      return zipFold.accept(context, left, right);
    }

    Context newContext = context.withParent(left.getTypeName());

    switch (left.getTypeName()) {
      case ARRAY:
      case ITERABLE:
        return zipFold.accumulate(
            zipFold.accept(context, left, right),
            visit(
                zipFold,
                newContext,
                left.getCollectionElementType(),
                right.getCollectionElementType()));

      case ROW:
        return visitRow(zipFold, newContext, left.getRowSchema(), right.getRowSchema());

      case MAP:
        return zipFold.accumulate(
            zipFold.accept(context, left, right),
            visit(
                zipFold,
                newContext,
                left.getCollectionElementType(),
                right.getCollectionElementType()));

      default:
        return zipFold.accept(context, left, right);
    }
  }

  static <T> T visitRow(SchemaZipFold<T> zipFold, Context context, Schema left, Schema right) {
    T node = zipFold.accept(context, FieldType.row(left), FieldType.row(right));

    Stream<String> union =
        Stream.concat(
                left.getFields().stream().map(Schema.Field::getName),
                right.getFields().stream().map(Schema.Field::getName))
            .distinct();

    Stream<String> intersection =
        left.getFields().stream().map(Schema.Field::getName).filter(right::hasField);

    T inner0 =
        intersection
            .map(
                name ->
                    visit(
                        zipFold,
                        context.withPathPart(name).withParent(TypeName.ROW),
                        left.getField(name).getType(),
                        right.getField(name).getType()))
            .reduce(node, zipFold::accumulate);

    T inner1 =
        union
            .map(
                name -> {
                  Optional<Field> field0 = Optional.empty();
                  Optional<Field> field1 = Optional.empty();

                  if (left.hasField(name)) {
                    field0 = Optional.of(left.getField(name));
                  }

                  if (right.hasField(name)) {
                    field1 = Optional.of(right.getField(name));
                  }

                  Context newContext = context.withPathPart(name).withParent(TypeName.ROW);
                  return zipFold.accept(newContext, field0, field1);
                })
            .reduce(node, zipFold::accumulate);

    return zipFold.accumulate(zipFold.accumulate(node, inner0), inner1);
  }
}


text:
package org.apache.streams.local.counters;

import org.apache.streams.local.builders.LocalStreamBuilder;
import org.apache.streams.util.ComponentUtils;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicLong;

/**
 *
 */
@ThreadSafe
public class StreamsTaskCounter implements StreamsTaskCounterMXBean {

  public static final String NAME_TEMPLATE = "org.apache.streams.local:type=StreamsTaskCounter,name=%s,identifier=%s,startedAt=%s";
  private static final Logger LOGGER = LoggerFactory.getLogger(StreamsTaskCounter.class);

  private AtomicLong emitted;
  private AtomicLong received;
  private AtomicLong errors;
  private AtomicLong totalTime;
  @GuardedBy("this")
  private volatile long maxTime;

  /**
   *
   * @param id
   */
  public StreamsTaskCounter(String id) {
    this(id, LocalStreamBuilder.DEFAULT_STREAM_IDENTIFIER, -1);
  }

  /**
   *
   * @param id
   */
  public StreamsTaskCounter(String id, String streamId, long startedAt) {
    this.emitted = new AtomicLong(0);
    this.received = new AtomicLong(0);
    this.errors = new AtomicLong(0);
    this.totalTime = new AtomicLong(0);
    this.maxTime = -1;
    ComponentUtils.registerLocalMBean(String.format(NAME_TEMPLATE, id, streamId, startedAt), this);
  }

  /**
   * Increment emitted count
   */
  public void incrementEmittedCount() {
    this.incrementEmittedCount(1);
  }

  /**
   * Increment emitted count
   * @param delta
   */
  public void incrementEmittedCount(long delta) {
    this.emitted.addAndGet(delta);
  }

  /**
   * Increment error count
   */
  public void incrementErrorCount() {
    this.incrementErrorCount(1);
  }

  /**
   * Increment error count
   * @param delta
   */
  public void incrementErrorCount(long delta) {
    this.errors.addAndGet(delta);
  }

  /**
   * Increment received count
   */
  public void incrementReceivedCount() {
    this.incrementReceivedCount(1);
  }

  /**
   * Increment received count
   * @param delta
   */
  public void incrementReceivedCount(long delta) {
    this.received.addAndGet(delta);
  }

  /**
   * Add the time it takes to process a single datum in milliseconds
   * @param processTime
   */
  public void addTime(long processTime) {
    synchronized (this) {
      if(processTime > this.maxTime) {
        this.maxTime = processTime;
      }
    }
    this.totalTime.addAndGet(processTime);
  }

  @Override
  public double getErrorRate() {
    if(this.received.get() == 0) {
      return 0.0;
    }
    return (double) this.errors.get() / (double) this.received.get();
  }

  @Override
  public long getNumEmitted() {
    return this.emitted.get();
  }

  @Override
  public long getNumReceived() {
    return this.received.get();
  }

  @Override
  public long getNumUnhandledErrors() {
    return this.errors.get();
  }

  @Override
  public double getAvgTime() {
    long rec = this.received.get();
    long emit = this.emitted.get();
    if(rec == 0 && emit == 0 ) {
      return 0.0;
    } else if( rec == 0) { //provider instance
      return this.totalTime.get() / (double) emit;
    } else {
      return this.totalTime.get() / ((double) this.received.get() - this.errors.get());
    }
  }

  @Override
  public long getMaxTime() {
    return this.maxTime;
  }
}


text:
<title>Windows XP & the Windows Driver Model - Driver Structure
& the I/O Request Packet</title>

The I/O Request Packet

What's in an IRP?

An IRP contains every possible relevant piece of information regarding
an I/O request. Keep in mind that I/O request, in the sense of an IRP, may
not be actually I/O, but may be a device command or query that some lower
level driver (before the function driver itself) can answer. Most
importantly, IRPs contain the following information: addresses of
memory or buffers that contain information or must be filled with the
results of an I/O operation, the device flags (from the Device Object
before) which can specify things like buffered I/O, the main function
of the IRP (major function codes), a minor function code (if
applicable), a pointer to a routine that can cancel the IRP in
mid-processing, a status code for the IRP, an indication if the irp is
pending and cannot be handled by a higher level driver, and many other
important fields that contain valuable information.

What the IRP Does

Kernel mode code creates IRPs as a result of internal kernel operations
or on behalf of a user-mode component that requests I/O operations. Along
with an IRP, an array of <tt>IO_STACK_LOCATIONS</tt> is also created, one
entry per driver that will process the IRP, and each location contains
some more information about the IRP as well as a completion routine.
The array of IO stack locations is as vital to keep in mind as the
IRP itself. The I/O stack locations each contain the following
information: the IRP major function code, the minor function code,
flags, a union of parameter substructures, a pointer to the device
object, a pointer to a Kernel file object to which the I/O request is
directed, a completion routine pointer, and an arbitrary context value to
be passed to the completion routine.

While all the data structures inside an IRP and an array of IO stack
locations may sound like a mouthful, pay attention to the functionality
that this kind of system affords. Storing major function codes in each
array location allows an IRP to morph from one type of I/O request to
another as it travels down the stack. Pointers to buffers in an IRP allow
the kernel to automatically handle the messy process of allocating and
freeing non-paged system buffers for I/O for driver writers who are
uncomfortable with this process. The Status and Cancel fields allow IRPs
to be halted and expunged or suspended midway through the I/O system.
These structures and fields have evolved over time to include all sorts
of functionality as Microsoft's I/O subsystem requirements have grown and
changed.

Types of IRPs

At this point, we must more fully limit our discussion to WDM filter
drivers. IRPs for DMA, interrupt driven I/O, and specific classes of
device vary in their behavior and processing so greatly that any driver
writer attempting to write a driver for such devices will have to learn
the very precise specifics of how XP handles the IRPs for her or his
device. For our WDM filter drivers, the IRP major function codes that we
will handle include IRP_MJ_CREATE and IRP_MJ_CLOSE, and
IRP_MJ_INTERNAL_DEVICE_CONTROL.

The Life of an IRP relevant to WDM drivers filtering Pointing
Devices

For pointing devices that fit the USB specification, Microsoft provides
a miniport and port driver combination for Human Input Devices (HID) that
work as a function driver for HID devices. For mice, mouhid.sys and
mouclass.sys also work together to become a function Mouhid and Mouclass
work together to present a uniform HID mouse interface to the system, as
defined by Microsoft. This enabled applications to transparently handle
all kinds of mice that fit the specifications. Now, because a mouse an
input device, a special direct-call interface (a callback function) is
used to transfer information from whatever port driver to Mouclass.sys.
This facilitates using the Microsoft driver to handle all IRPs, regardless
of the underlying port driver. Because of stringent USB specifications,
all USB mice behave in a predicatable way, the HID port/miniport
combination know how to drive our device, and mouhid/mouclass present that
uniform interface to the rest of the system (if we were using a PS/2-port
mouse, we would see the i8042prt driver in place of the hidusb ones).

 Here is a diagram of how our mouse examples is placed in the driver
stack:

The callback mechanism is set up by an
<tt>IRP_MJ_INTERNAL_DEVICE_CONTROL</tt> sent from the MouClass
layer down to the Mouhid layer. The IRP is completed by filling
it with a function pointer to mouclass/mouhid that hidusb/hidclass will
call in the case of input data to report. This input is delivered in the
context of a raw input thread (RIT) that the kernel assigns specifically
to reduce latency on input. While we could just drop IRPs that contain
pointers to mouse input data structures from our filter driver, we
instead intercept the <TT>IRP_MJ_INTERNAL_DEVICE_CONTROL</tt>, fill
that IRP with our own callback function, and then have our callback
function call the original one in mouclass/mouhid.

The reason we can intercept this function pointer installation and make
our own is because the miniport/port driver combination uses the callback
mechanism to communicate. For most other IRPs, we will just simply pass
them down the stack to whatever is below our driver, and forget about
them. The queryattrs example driver is a special example in that it
creates a second IRP after installing a function pointer, and sends that
down for completion, and catches the IRP on the way back up the stack.
 If the stacking order is still confusing, or you'd like to know more
about port/miniport combinations, the DDK includes mouclass.sys among
other related drivers (with full source), as well as more sample
device drivers or bus drivers that perform similar functions.

Also, see figures 2-3, 2-4, and 2-6, in Programming the
Microsoft Windows Driver Model for a better
understanding of the WDM stacking struture.


text:
layout: page
title: Promotional Material
description: PDF poster and flyer.
invisible: true
---
{% comment %}
Here you go!
{% endcomment %}


text:
<?php

namespace Incenteev\TranslationCheckerBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ConfigureTranslatorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('incenteev_translation_checker.exposing_translator')) {
            return;
        }

        if (!$container->has('translator.default')) {
            return;
        }

        $translatorDef = $container->findDefinition('translator.default');

        $optionsArgumentIndex = 4;

        $options = $translatorDef->getArgument($optionsArgumentIndex);

        if (!is_array($options)) {
            // Weird setup. Reset all options
            $options = array();
        }

        // use a separate cache as we have no fallback locales
        $options['cache_dir'] = '%kernel.cache_dir%/incenteev_translations';

        $container->findDefinition('incenteev_translation_checker.exposing_translator')
            ->replaceArgument($optionsArgumentIndex, $options);
    }
}