Compare commits

...

2 commits

2 changed files with 75 additions and 16 deletions

54
.gitignore vendored Normal file
View file

@ -0,0 +1,54 @@
# Created by https://www.toptal.com/developers/gitignore/api/xcode,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=xcode,macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
### Xcode ###
## User settings
xcuserdata/
## Xcode 8 and earlier
*.xcscmblueprint
*.xccheckout
### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcodeproj/project.xcworkspace/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
**/xcshareddata/WorkspaceSettings.xcsettings
# End of https://www.toptal.com/developers/gitignore/api/xcode,macos

View file

@ -3,8 +3,18 @@ import SwiftUI
import Foundation
struct BlockedCount: View {
@State private var txtRecord: String = "..."
private static let startIndex = 44
private static let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter
}()
var body: some View {
Text(txtRecord)
.onAppear {
@ -16,26 +26,21 @@ struct BlockedCount: View {
// This is a DNS wire format response and we make a lot of assumptions
// It is not critical functionality so just let it fail if it fails
guard data.count > 44 else { return nil }
let startIndex = 44
let subData = data.suffix(from: startIndex)
// Find the first space character (ASCII 32)
var endIndex: Int?
for (offset, byte) in subData.enumerated() {
if byte == 32 { // Space character
endIndex = offset
break
}
guard data.count > Self.startIndex else {
return nil
}
guard let foundEndIndex = endIndex else { return nil }
// Find the first space character (ASCII 32).
guard let endIndex = data.suffix(from: Self.startIndex).firstIndex(of: 32) else {
return nil
}
// Extract bytes between start and space
let numberData = subData.prefix(foundEndIndex)
guard let numberString = String(data: data[Self.startIndex ..< endIndex], encoding: .utf8)
else {
return nil
}
return String(decoding: numberData, as: UTF8.self)
return Self.formatter.string(for: Int(numberString))
}
func fetchTXTRecord() {