Skip to content

Commit

Permalink
Moves a character count to String extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaranski committed Nov 6, 2018
1 parent c093172 commit 60744b2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions WordPressShared/Core/Utility/String+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,26 @@ extension String {
copy.remove(at: index)
return copy
}

/// Returns a count of valid text characters.
/// - Note : This implementation is influenced by `-wordCount` in `NSString+Helpers`.
public var characterCount: Int {
var charCount = 0

if isEmpty == false {
let textRange = startIndex..<endIndex
enumerateSubstrings(in: textRange, options: [.byWords, .localized]) { word, _, _, _ in
let wordLength = word?.count ?? 0
charCount += wordLength
}
}

return charCount
}
}

// MARK: - Prefix removal

public extension String {
/// Removes the given prefix from the string, if exists.
///
Expand Down Expand Up @@ -89,6 +106,7 @@ public extension String {
}

// MARK: - Suffix removal

public extension String {
/// Removes the given suffix from the string, if exists.
///
Expand Down

0 comments on commit 60744b2

Please sign in to comment.