<commit_before>//
//  Cartfile.swift
//  Carthage
//
//  Created by <NAME> on 2014-10-10.
//  Copyright (c) 2014 Carthage. All rights reserved.
//

import Foundation
import Result
import ReactiveCocoa

/// The relative path to a project's checked out dependencies.
public let CarthageProjectCheckoutsPath = "Carthage/Checkouts"

/// Represents a Cartfile, which is a specification of a project's dependencies
/// and any other settings Carthage needs to build it.
public struct Cartfile {
	/// The dependencies listed in the Cartfile.
	public var dependencies: [Dependency<VersionSpecifier>]

	public init(dependencies: [Dependency<VersionSpecifier>] = []) {
		self.dependencies = dependencies
	}

	/// Returns the location where Cartfile should exist within the given
	/// directory.
	public static func URLInDirectory(directoryURL: NSURL) -> NSURL {
		return directoryURL.URLByAppendingPathComponent("Cartfile")
	}

	/// Attempts to parse Cartfile information from a string.
	public static func fromString(string: String) -> Result<Cartfile, CarthageError> {
		var cartfile = self()
		var result: Result<(), CarthageError> = .success(())

		let commentIndicator = "#"
		(string as NSString).enumerateLinesUsingBlock { (line, stop) in
			let scanner = NSScanner(string: line)
			if scanner.scanString(commentIndicator, intoString: nil) {
				// Skip the rest of the line.
				return
			}

			if scanner.atEnd {
				// The line was all whitespace.
				return
			}

			switch (Dependency<VersionSpecifier>.fromScanner(scanner)) {
			case let .Success(dep):
				cartfile.dependencies.append(dep.value)

			case let .Failure(error):
				result = .failure(error.value)
				stop.memory = true
			}

			if scanner.scanString(commentIndicator, intoString: nil) {
				// Skip the rest of the line.
				return
			}

			if !scanner.atEnd {
				result = .failure(CarthageError.ParseError(description: "unexpected trailing characters in line: \(line)"))
				stop.memory = true
			}
		}

		return result.map { _ in cartfile }
	}

	/// Attempts to parse a Cartfile from a file at a given URL.
	public static func fromFile(cartfileURL: NSURL) -> Result<Cartfile, CarthageError> {
		var error: NSError?
		if let cartfileContents = NSString(contentsOfURL: cartfileURL, encoding: NSUTF8StringEncoding, error: &error) {
			return Cartfile.fromString(cartfileContents as String)
		} else {
			return .failure(CarthageError.ReadFailed(cartfileURL, error))
		}
	}

	/// Appends the contents of another Cartfile to that of the receiver.
	public mutating func appendCartfile(cartfile: Cartfile) {
		dependencies += cartfile.dependencies
	}
}

extension Cartfile: Printable {
	public var description: String {
		return "\(dependencies)"
	}
}

// Duplicate dependencies
extension Cartfile {
	/// Returns an array containing projects that are listed as duplicate
	/// dependencies.
	public func duplicateProjects() -> [ProjectIdentifier] {
		return filter(self.dependencyCountedSet) { $0.1 > 1}
			.map { $0.0 }
	}

	/// Returns the dependencies in a cartfile as a counted set containing the
    /// corresponding projects, represented as a dictionary.
	private var dependencyCountedSet: [ProjectIdentifier: Int] {<commit_msg>Style: Convert errant spaces to tab.<commit_after>//
//  Cartfile.swift
//  Carthage
//
//  Created by <NAME> on 2014-10-10.
//  Copyright (c) 2014 Carthage. All rights reserved.
//

import Foundation
import Result
import ReactiveCocoa

/// The relative path to a project's checked out dependencies.
public let CarthageProjectCheckoutsPath = "Carthage/Checkouts"

/// Represents a Cartfile, which is a specification of a project's dependencies
/// and any other settings Carthage needs to build it.
public struct Cartfile {
	/// The dependencies listed in the Cartfile.
	public var dependencies: [Dependency<VersionSpecifier>]

	public init(dependencies: [Dependency<VersionSpecifier>] = []) {
		self.dependencies = dependencies
	}

	/// Returns the location where Cartfile should exist within the given
	/// directory.
	public static func URLInDirectory(directoryURL: NSURL) -> NSURL {
		return directoryURL.URLByAppendingPathComponent("Cartfile")
	}

	/// Attempts to parse Cartfile information from a string.
	public static func fromString(string: String) -> Result<Cartfile, CarthageError> {
		var cartfile = self()
		var result: Result<(), CarthageError> = .success(())

		let commentIndicator = "#"
		(string as NSString).enumerateLinesUsingBlock { (line, stop) in
			let scanner = NSScanner(string: line)
			if scanner.scanString(commentIndicator, intoString: nil) {
				// Skip the rest of the line.
				return
			}

			if scanner.atEnd {
				// The line was all whitespace.
				return
			}

			switch (Dependency<VersionSpecifier>.fromScanner(scanner)) {
			case let .Success(dep):
				cartfile.dependencies.append(dep.value)

			case let .Failure(error):
				result = .failure(error.value)
				stop.memory = true
			}

			if scanner.scanString(commentIndicator, intoString: nil) {
				// Skip the rest of the line.
				return
			}

			if !scanner.atEnd {
				result = .failure(CarthageError.ParseError(description: "unexpected trailing characters in line: \(line)"))
				stop.memory = true
			}
		}

		return result.map { _ in cartfile }
	}

	/// Attempts to parse a Cartfile from a file at a given URL.
	public static func fromFile(cartfileURL: NSURL) -> Result<Cartfile, CarthageError> {
		var error: NSError?
		if let cartfileContents = NSString(contentsOfURL: cartfileURL, encoding: NSUTF8StringEncoding, error: &error) {
			return Cartfile.fromString(cartfileContents as String)
		} else {
			return .failure(CarthageError.ReadFailed(cartfileURL, error))
		}
	}

	/// Appends the contents of another Cartfile to that of the receiver.
	public mutating func appendCartfile(cartfile: Cartfile) {
		dependencies += cartfile.dependencies
	}
}

extension Cartfile: Printable {
	public var description: String {
		return "\(dependencies)"
	}
}

// Duplicate dependencies
extension Cartfile {
	/// Returns an array containing projects that are listed as duplicate
	/// dependencies.
	public func duplicateProjects() -> [ProjectIdentifier] {
		return filter(self.dependencyCountedSet) { $0.1 > 1}
			.map { $0.0 }
	}

	/// Returns the dependencies in a cartfile as a counted set containing the
	/// corresponding projects, represented as a dictionary.
	private var dependencyCountedSet: [ProjectIdentifier: Int] {
{'fraction_non_alphanumeric': 0.09940791523839203, 'fraction_numerical': 0.005609224057338735, 'mean_word_length': 3.7689450222882614, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1058458', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1833, 'n_words': 603}