diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 3e69a10..0000000 --- a/.gitignore +++ /dev/null @@ -1,54 +0,0 @@ -# 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 diff --git a/dns/BlockedCount.swift b/dns/BlockedCount.swift index 69d7686..8a0faab 100644 --- a/dns/BlockedCount.swift +++ b/dns/BlockedCount.swift @@ -3,18 +3,8 @@ 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 { @@ -26,21 +16,26 @@ 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 > Self.startIndex else { - return nil + 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 + } } - // Find the first space character (ASCII 32). - guard let endIndex = data.suffix(from: Self.startIndex).firstIndex(of: 32) else { - return nil - } + guard let foundEndIndex = endIndex else { return nil } - guard let numberString = String(data: data[Self.startIndex ..< endIndex], encoding: .utf8) - else { - return nil - } + // Extract bytes between start and space + let numberData = subData.prefix(foundEndIndex) - return Self.formatter.string(for: Int(numberString)) + return String(decoding: numberData, as: UTF8.self) } func fetchTXTRecord() {