То, что вы ищете, это «Кодирование длин серий» . Обратите внимание, что это , а не шифрование!
Вот возможная реализация (пояснения включены):
func runLengthEncode(_ str: String) -> String {
var result = ""
var pos = str.startIndex // Start index of current run
while pos != str.endIndex {
let char = str[pos]
// Find index of next run (or `endIndex` if there is none):
let next = str[pos...].firstIndex(where: { [110] != char }) ?? str.endIndex
// Compute the length of the current run:
let length = str.distance(from: pos, to: next)
// Append compressed output to the result:
result.append(length <= 2 ? String(repeating: char, count: length) : "\(char)\(length)")
pos = next // ... and continue with next run
}
return result
}
Примеры:
print(runLengthEncode("atttbcdddd")) // at3bcd4
print(runLengthEncode("aahhhgggg")) // aah3g4
print(runLengthEncode("abbbaaa")) // ab3a3
Основываясь на том, что сказал fbonnet, для использования функций, показанных в статье kb на C # Сайт pinvoke.net - отличный ресурс. У них также есть статья о функции PostMessage
здесь .
Таким образом, основным способом заставить ваш код работать может быть поиск функций, перечисленных в kb статья на сайте pinvoke.net . Или вы можете прочитать эта статья сделала это за вас.