Signal-iOS/SignalMessaging/categories/String+OWS.swift
Michael Kirk ffea3a020f WIP: FTS - wired up Search VC
-[] Backend
  -[] indexes e5.25
    -[x] wire up results: Contacts / Conversations / Messages actual: 3hr
    -[ ] group thread est: actual:
      -[x] group name actual: e.25
      -[ ] group member name: e.25
      -[ ] group member number: e.25
    -[ ] contact thread e.5
      -[ ] name
      -[ ] number
    -[ ] messages e1
      -[ ] content
-[] Frontend e10.75
  -[x] wire up VC's a.5
  -[x] show search results only when search box has content a.25
  -[] show search results: Contact / Conversation / Messages e2
  -[] tapping thread search result takes you to conversation e1
  -[] tapping message search result takes you to message e1
  -[] show snippet text for matched message e1
  -[] highlight matched text in thread e3
  -[] go to next search result in thread e2
2018-06-11 09:58:36 -04:00

54 lines
1.7 KiB
Swift

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
public extension String {
var stripped: String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
// Truncates string to be less than or equal to byteCount, while ensuring we never truncate partial characters for multibyte characters.
func truncated(toByteCount byteCount: UInt) -> String? {
var lowerBoundCharCount = 0
var upperBoundCharCount = self.count
while (lowerBoundCharCount < upperBoundCharCount) {
guard let upperBoundData = self.prefix(upperBoundCharCount).data(using: .utf8) else {
owsFail("in \(#function) upperBoundData was unexpectedly nil")
return nil
}
if upperBoundData.count <= byteCount {
break
}
// converge
if upperBoundCharCount - lowerBoundCharCount == 1 {
upperBoundCharCount = lowerBoundCharCount
break
}
let midpointCharCount = (lowerBoundCharCount + upperBoundCharCount) / 2
let midpointString = self.prefix(midpointCharCount)
guard let midpointData = midpointString.data(using: .utf8) else {
owsFail("in \(#function) midpointData was unexpectedly nil")
return nil
}
let midpointByteCount = midpointData.count
if midpointByteCount < byteCount {
lowerBoundCharCount = midpointCharCount
} else {
upperBoundCharCount = midpointCharCount
}
}
return String(self.prefix(upperBoundCharCount))
}
}