String.swift 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // String.swift
  3. // Mastodon
  4. //
  5. // Created by sxiaojian on 2021/3/2.
  6. //
  7. import Foundation
  8. extension String {
  9. func capitalizingFirstLetter() -> String {
  10. return prefix(1).capitalized + dropFirst()
  11. }
  12. mutating func capitalizeFirstLetter() {
  13. self = self.capitalizingFirstLetter()
  14. }
  15. static let empty = ""
  16. }
  17. extension String {
  18. static func normalize(base64String: String) -> String {
  19. let base64 = base64String
  20. .replacingOccurrences(of: "-", with: "+")
  21. .replacingOccurrences(of: "_", with: "/")
  22. .padding()
  23. return base64
  24. }
  25. private func padding() -> String {
  26. let remainder = self.count % 4
  27. if remainder > 0 {
  28. return self.padding(
  29. toLength: self.count + 4 - remainder,
  30. withPad: "=",
  31. startingAt: 0
  32. )
  33. }
  34. return self
  35. }
  36. }