aboutsummaryrefslogblamecommitdiffstats
path: root/WireGuard/Shared/Model/String+ArrayConversion.swift
blob: 7d00be9764c25e42878e0263070d58e74aacc527 (plain) (tree)
1
2
3
4
5
6
7
                               
                                                             



                  
 









                                                                                                        
 


                                            
 







                                                                                                        
 
 
// SPDX-License-Identifier: MIT
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

import Foundation

extension String {

    func splitToArray(separator: Character = ",", trimmingCharacters: CharacterSet? = nil) -> [String] {
        return split(separator: separator)
            .map {
                if let charSet = trimmingCharacters {
                    return $0.trimmingCharacters(in: charSet)
                } else {
                    return String($0)
                }
        }
    }

}

extension Optional where Wrapped == String {

    func splitToArray(separator: Character = ",", trimmingCharacters: CharacterSet? = nil) -> [String] {
        switch self {
        case .none:
            return []
        case .some(let wrapped):
            return wrapped.splitToArray(separator: separator, trimmingCharacters: trimmingCharacters)
        }
    }

}