<commit_before>//
//  main.swift
//  xcrmsimdup
//
//  Created by <NAME> on 09/08/15.
//  Copyright (c) 2015 fiftytwo. All rights reserved.
//

import Foundation


// MARK: Data types


struct Section
{
    var name: String
    var rangeTitle: Range<String.Index>
    var rangeData: Range<String.Index>
}


struct Device
{
    var name: String
    var id: String
    var status: String
}


// MARK: Functions


func printUsage()
{
    println(
        "Usage: xcrmsimdup [options]\n" +
        "\n" +
        "Find duplicated simulator records from the active developer directory listed\n" +
        "by command `xcrun simctl list` and remove duplicates using\n" +
        "`xcrun simctl delete`.\n" +
        "\n" +
        "The active developer directory can be set using `xcode-select`, or via the\n" +
        "DEVELOPER_DIR environment variable. See the xcrun and xcode-select manual\n" +
        "pages for more information.\n" +
        "\n" +
        "Options:\n" +
        "  -h, --help      show this help message and exit\n" +
        "  -d, --delete    delete duplicates\n" +
        "  -s, --show      just show duplicates, but don't touch them"
    );

}


func executeWithBash(command: String) -> (status: Int32, output: String)
{
    var task = NSTask()

    var outputPipe = NSPipe()
    var outputFileHandle = outputPipe.fileHandleForReading

    task.launchPath = "/bin/bash"
    task.arguments = ["-c", command]
    task.standardOutput = outputPipe
    task.standardError = outputPipe

    task.launch()

    var outputData = outputFileHandle.readDataToEndOfFile()

    task.waitUntilExit()

    var output = NSString(data: outputData, encoding: NSUTF8StringEncoding) as! String

    return (task.terminationStatus, output)
}


func sectionsFromString(string: String, prefix: String, suffix: String, range: Range<String.Index>? = nil) -> Array<Section>?
{
    var startIndex: String.Index
    var endIndex: String.Index

    if range == nil
    {
        startIndex = string.startIndex
        endIndex = string.endIndex
    }
    else
    {
        startIndex = range!.startIndex
        endIndex = range!.endIndex
    }

    var sections = [Section]()

    while let prefixFound = string.rangeOfString(prefix, range: startIndex..<endIndex)
    {
        if let suffixFound = string.rangeOfString(suffix, range: prefixFound.endIndex..<endIndex)
        {
            var section = Section(
                name: string.substringWithRange(prefixFound.endIndex..<suffixFound.startIndex),
                rangeTitle: prefixFound.startIndex..<suffixFound.endIndex,
                rangeData: suffixFound.endIndex..<endIndex
            )

            sections.append(section)

            startIndex = suffixFound.endIndex;
        }
        else
        {
            return nil;
        }
    }

    for var i = sections.count - 2; i >= 0; i--
    {
        sections[i].rangeData.endIndex = sections[i + 1].rangeTitle.startIndex
    }

    return sections
}


func intFromStringIndex(string: String, index: String.Index) -> Int
{
    let utf16 = string.utf16;

    let utf16Index = index.samePositionIn(utf16)

    return distance(utf16.startIndex, utf16Index)
}


public func NSRangeFromStringRange(string: String, range: Range<String.Index>) -> NSRange
{
    let utf16Start = intFromStringIndex(string, range.startIndex)

    let utf16End = intFromStringIndex(string, range.endIndex)

    let utf16length = distance(utf16Start, utf16End)

    return NSRange(location: utf16Start, length: utf16length)
}


func stringIndexFromInt(string: String, index: Int) -> String.Index?
{
    let utf16 = string.utf16

    let utf16Index = advance(utf16.startIndex, index, utf16.endIndex)

    return utf16Index.samePositionIn(string)
}


public func stringRangeFromNSRange(string: String, nsRange: NSRange) -> Range<String.Index>?
{
    if nsRange.location == NSNotFound
    {
        return nil
    }

    if let start = stringIndexFromInt(string, nsRange.location),
        end = stringIndexFromInt(string, nsRange.location + nsRange.length)
    {<commit_msg>Removed unneeded public keywords<commit_after>//
//  main.swift
//  xcrmsimdup
//
//  Created by <NAME> on 09/08/15.
//  Copyright (c) 2015 fiftytwo. All rights reserved.
//

import Foundation


// MARK: Data types


struct Section
{
    var name: String
    var rangeTitle: Range<String.Index>
    var rangeData: Range<String.Index>
}


struct Device
{
    var name: String
    var id: String
    var status: String
}


// MARK: Functions


func printUsage()
{
    println(
        "Usage: xcrmsimdup [options]\n" +
        "\n" +
        "Find duplicated simulator records from the active developer directory listed\n" +
        "by command `xcrun simctl list` and remove duplicates using\n" +
        "`xcrun simctl delete`.\n" +
        "\n" +
        "The active developer directory can be set using `xcode-select`, or via the\n" +
        "DEVELOPER_DIR environment variable. See the xcrun and xcode-select manual\n" +
        "pages for more information.\n" +
        "\n" +
        "Options:\n" +
        "  -h, --help      show this help message and exit\n" +
        "  -d, --delete    delete duplicates\n" +
        "  -s, --show      just show duplicates, but don't touch them"
    );

}


func executeWithBash(command: String) -> (status: Int32, output: String)
{
    var task = NSTask()

    var outputPipe = NSPipe()
    var outputFileHandle = outputPipe.fileHandleForReading

    task.launchPath = "/bin/bash"
    task.arguments = ["-c", command]
    task.standardOutput = outputPipe
    task.standardError = outputPipe

    task.launch()

    var outputData = outputFileHandle.readDataToEndOfFile()

    task.waitUntilExit()

    var output = NSString(data: outputData, encoding: NSUTF8StringEncoding) as! String

    return (task.terminationStatus, output)
}


func sectionsFromString(string: String, prefix: String, suffix: String, range: Range<String.Index>? = nil) -> Array<Section>?
{
    var startIndex: String.Index
    var endIndex: String.Index

    if range == nil
    {
        startIndex = string.startIndex
        endIndex = string.endIndex
    }
    else
    {
        startIndex = range!.startIndex
        endIndex = range!.endIndex
    }

    var sections = [Section]()

    while let prefixFound = string.rangeOfString(prefix, range: startIndex..<endIndex)
    {
        if let suffixFound = string.rangeOfString(suffix, range: prefixFound.endIndex..<endIndex)
        {
            var section = Section(
                name: string.substringWithRange(prefixFound.endIndex..<suffixFound.startIndex),
                rangeTitle: prefixFound.startIndex..<suffixFound.endIndex,
                rangeData: suffixFound.endIndex..<endIndex
            )

            sections.append(section)

            startIndex = suffixFound.endIndex;
        }
        else
        {
            return nil;
        }
    }

    for var i = sections.count - 2; i >= 0; i--
    {
        sections[i].rangeData.endIndex = sections[i + 1].rangeTitle.startIndex
    }

    return sections
}


func intFromStringIndex(string: String, index: String.Index) -> Int
{
    let utf16 = string.utf16;

    let utf16Index = index.samePositionIn(utf16)

    return distance(utf16.startIndex, utf16Index)
}


func NSRangeFromStringRange(string: String, range: Range<String.Index>) -> NSRange
{
    let utf16Start = intFromStringIndex(string, range.startIndex)

    let utf16End = intFromStringIndex(string, range.endIndex)

    let utf16length = distance(utf16Start, utf16End)

    return NSRange(location: utf16Start, length: utf16length)
}


func stringIndexFromInt(string: String, index: Int) -> String.Index?
{
    let utf16 = string.utf16

    let utf16Index = advance(utf16.startIndex, index, utf16.endIndex)

    return utf16Index.samePositionIn(string)
}


func stringRangeFromNSRange(string: String, nsRange: NSRange) -> Range<String.Index>?
{
    if nsRange.location == NSNotFound
    {
        return nil
    }

    if let start = stringIndexFromInt(string, nsRange.location),
        end = stringIndexFromInt(string, nsRange.location + nsRange.length)
    {
{'fraction_non_alphanumeric': 0.10534069981583793, 'fraction_numerical': 0.013259668508287293, 'mean_word_length': 2.6076173604960142, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '5178861', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2401, 'n_words': 623}