Skip to content

Commit

Permalink
Merge pull request #161 from wordpress-mobile/issue/10343-relocate-ch…
Browse files Browse the repository at this point in the history
…ar-count

Moves character count to String extension
  • Loading branch information
stevebaranski authored Nov 6, 2018
2 parents c093172 + 60744b2 commit 0f0300d
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 0f0300d

Please sign in to comment.