<commit_before>//
//  PString.swift
//  Bartleby
//
//  Created by <NAME> on 19/12/2015.
//  Copyright © 2015 https://pereira-da-silva.com for Chaosmos SAS
//  All rights reserved you can ask for a license.

import Foundation

#if os(OSX)
    import AppKit
#else
    import UIKit
#endif



// The goal of PString is to facilitate PHP to SWIFT port
// It focuses on translating litteraly the PHP style string processing.
// For example Flexions' Pluralization.php has been ported to swift in a few minutes
// You can compare Pluralization.swift & Pluralization.php

// MARK: - global PHP Style String functions

public struct PString {

    public static func strtoupper(_ string: String) -> String {
        return string.uppercased()
    }

    public static func strtolower(_ string: String) -> String {
        return string.lowercased()
    }

    public static func substr(_ string: String, _ start: Int) -> String? {
        if(start>0) {
            return substr(string, start, string.characters.count)
        } else {
            return substr(string, string.characters.count+start, string.characters.count)
        }
    }

    /**
     Left trims the characters specified in the characterSet

     E.g:
     + PString.ltrim("   *   Hello    *    ",characterSet: NSCharacterSet(charactersInString:" *")) // returns "Hello    *    "
     + PString.ltrim(",A,B,C",characterSet: NSCharacterSet(charactersInString:",")) // Returns "A,B,C"

     - parameter string:       the string
     - parameter characterSet: the character set (White spaces and new line by default)

     - returns: the string
     */
    public static func ltrim(_ string: String, characterSet: CharacterSet=CharacterSet.whitespacesAndNewlines) -> String {
        if let range = string.rangeOfCharacter(from: characterSet.inverted) {
            return string[range.lowerBound..<string.endIndex]
        }
        return string
    }

    /**
     Left trims the characters specified in the characters

     E.g:
     + PString.ltrim("   *   Hello    *    ",characters:" *") // returns "Hello    *    "
     + PString.ltrim(",A,B,C",characters:",")) // Returns "A,B,C"

     - parameter string:       the string
     - parameter characterSet: the character set (White spaces and new line by default)

     - returns: the string
     */
    public static func ltrim(_ string: String, characters: String) -> String {
        return ltrim(string, characterSet: CharacterSet(charactersIn:characters) )
    }




    /**
     Right trim the characters specified in the characterSet

     - parameter string:       the string
     - parameter characters: the character set (White spaces and new line by default)

     - returns: the string
     */

    public static func rtrim(_ string: String, characterSet: CharacterSet=CharacterSet.whitespacesAndNewlines) -> String {
        if let range = string.rangeOfCharacter(from: characterSet.inverted, options: NSString.CompareOptions.backwards) {
            return string[string.startIndex...range.lowerBound]
        }
        return string
    }
    /**
     Right trim the characters specified in the characters

     - parameter string:     the string
     - parameter characters: the characters

     - returns: the string
     */
    public static func rtrim(_ string: String, characters: String) -> String {
        return rtrim(string, characterSet: CharacterSet(charactersIn:characters) )
    }


    /**
     Returns a sub string

     - parameter string: the string
     - parameter start:  If start is negative, the returned string will start at the start'th character from the end of string.
     - parameter length: length

     - returns: the sub string
     */
    public static func substr(_ string: String, _ start: Int, _ length: Int) -> String {
        let startIndex: String.Index?
        if start<0 {
            let l=strlen(string)
            let from=l-start
            if from>=l {
                startIndex=string.endIndex
            } else {
                startIndex=string.characters.index(string.startIndex, offsetBy: from)
            }
        } else if start==0 {
            startIndex=string.startIndex
        } else {
            startIndex=string.characters.index(string.startIndex, offsetBy: start)
        }
        let endIndex = string.characters.index(string.startIndex, offsetBy: length)
        return string.substring(with: startIndex! ..< endIndex)
    }

    public static func strlen(_ string: String) -> Int {
        return string.characters.count
    }

    public static func lcfirst(_ string: String) -> String {
        var tstring=string
        let first=tstring.firstCharacterRange()
        tstring.replaceSubrange(first, with:tstring.substring(with: first).lowercased())
        return tstring
    }

    public static func ucfirst(_ string: String) -> String {
        var tstring=string
        let first=tstring.firstCharacterRange()
        tstring.replaceSubrange(first, with:tstring.substring(with:first).uppercased())
        return tstring
    }

    //preg_match PREG_OFFSET_CAPTURE flag is currently not implemented

    static let PREG_OFFSET_CAPTURE=1

    public static func preg_match(_ pattern: String, _ subject: String, _ matches: inout [String], _ flags: Int = 0, _ offset: Int = 0) -> Int {
        let subjectWithOffset=substr(subject, offset, strlen(subject))
        if subjectWithOffset.isMatching(pattern) {<commit_msg>Fixes a bug on PString substring<commit_after>//
//  PString.swift
//  Bartleby
//
//  Created by <NAME> on 19/12/2015.
//  Copyright © 2015 https://pereira-da-silva.com for Chaosmos SAS
//  All rights reserved you can ask for a license.

import Foundation

#if os(OSX)
    import AppKit
#else
    import UIKit
#endif



// The goal of PString is to facilitate PHP to SWIFT port
// It focuses on translating litteraly the PHP style string processing.
// For example Flexions' Pluralization.php has been ported to swift in a few minutes
// You can compare Pluralization.swift & Pluralization.php

// MARK: - global PHP Style String functions

public struct PString {

    public static func strtoupper(_ string: String) -> String {
        return string.uppercased()
    }

    public static func strtolower(_ string: String) -> String {
        return string.lowercased()
    }

    public static func substr(_ string: String, _ start: Int) -> String? {
        if(start>0) {
            return substr(string, start, string.characters.count)
        } else {
            return substr(string, string.characters.count+start, string.characters.count)
        }
    }

    /**
     Left trims the characters specified in the characterSet

     E.g:
     + PString.ltrim("   *   Hello    *    ",characterSet: NSCharacterSet(charactersInString:" *")) // returns "Hello    *    "
     + PString.ltrim(",A,B,C",characterSet: NSCharacterSet(charactersInString:",")) // Returns "A,B,C"

     - parameter string:       the string
     - parameter characterSet: the character set (White spaces and new line by default)

     - returns: the string
     */
    public static func ltrim(_ string: String, characterSet: CharacterSet=CharacterSet.whitespacesAndNewlines) -> String {
        if let range = string.rangeOfCharacter(from: characterSet.inverted) {
            return string[range.lowerBound..<string.endIndex]
        }
        return string
    }

    /**
     Left trims the characters specified in the characters

     E.g:
     + PString.ltrim("   *   Hello    *    ",characters:" *") // returns "Hello    *    "
     + PString.ltrim(",A,B,C",characters:",")) // Returns "A,B,C"

     - parameter string:       the string
     - parameter characterSet: the character set (White spaces and new line by default)

     - returns: the string
     */
    public static func ltrim(_ string: String, characters: String) -> String {
        return ltrim(string, characterSet: CharacterSet(charactersIn:characters) )
    }




    /**
     Right trim the characters specified in the characterSet

     - parameter string:       the string
     - parameter characters: the character set (White spaces and new line by default)

     - returns: the string
     */

    public static func rtrim(_ string: String, characterSet: CharacterSet=CharacterSet.whitespacesAndNewlines) -> String {
        if let range = string.rangeOfCharacter(from: characterSet.inverted, options: NSString.CompareOptions.backwards) {
            return string[string.startIndex...range.lowerBound]
        }
        return string
    }
    /**
     Right trim the characters specified in the characters

     - parameter string:     the string
     - parameter characters: the characters

     - returns: the string
     */
    public static func rtrim(_ string: String, characters: String) -> String {
        return rtrim(string, characterSet: CharacterSet(charactersIn:characters) )
    }


    /**
     Returns a sub string

     - parameter string: the string
     - parameter start:  If start is negative, the returned string will start at the start'th character from the end of string.
     - parameter length: length

     - returns: the sub string
     */
    public static func substr(_ string: String, _ start: Int, _ length: Int) -> String {
        var startIndex: String.Index = string.startIndex
        if start<0 {
            let l=strlen(string)
            let from=l-start
            if from>=l {
                startIndex=string.endIndex
            } else {
                startIndex=string.characters.index(string.startIndex, offsetBy: from)
            }
        } else if start>0{
            startIndex=string.characters.index(string.startIndex, offsetBy: start)
        }
        let endIndex = string.characters.index(startIndex, offsetBy:length)
        return string.substring(with: startIndex..<endIndex)
    }

    public static func strlen(_ string: String) -> Int {
        return string.characters.count
    }

    public static func lcfirst(_ string: String) -> String {
        var tstring=string
        let first=tstring.firstCharacterRange()
        tstring.replaceSubrange(first, with:tstring.substring(with: first).lowercased())
        return tstring
    }

    public static func ucfirst(_ string: String) -> String {
        var tstring=string
        let first=tstring.firstCharacterRange()
        tstring.replaceSubrange(first, with:tstring.substring(with:first).uppercased())
        return tstring
    }

    //preg_match PREG_OFFSET_CAPTURE flag is currently not implemented

    static let PREG_OFFSET_CAPTURE=1

    public static func preg_match(_ pattern: String, _ subject: String, _ matches: inout [String], _ flags: Int = 0, _ offset: Int = 0) -> Int {
        let subjectWithOffset=substr(subject, offset, strlen(subject))
        if subjectWithOffset.isMatching(pattern) {
{'fraction_non_alphanumeric': 0.09304901689282748, 'fraction_numerical': 0.003323179174743838, 'mean_word_length': 2.576758005942555, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 2, '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': '6865157', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2905, 'n_words': 914}