summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/String+Split.swift
blob: 82352280fce71943f9698e70875622ce8c27b260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
//  String+Split.swift
//  MullvadVPN
//
//  Created by pronebird on 27/03/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation

extension String {

    /// Returns the array of the longest possible subsequences of the given length.
    func split(every length: Int) -> [Substring] {
        guard length > 0 else { return [prefix(upTo: endIndex)] }

        let resultCount = Int((Float(count) / Float(length)).rounded(.up))

        return (0 ..< resultCount)
            .map { dropFirst($0 * length).prefix(length) }
    }
}