From 9f30f9f68690a2e5bfa4c9eb24db2c307a96831a Mon Sep 17 00:00:00 2001 From: Benjamin Erhart Date: Tue, 14 Apr 2026 17:53:22 +0200 Subject: [PATCH] Issue #1: format the `BlockedCount` number output to locale. --- dns/BlockedCount.swift | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/dns/BlockedCount.swift b/dns/BlockedCount.swift index 8a0faab..69d7686 100644 --- a/dns/BlockedCount.swift +++ b/dns/BlockedCount.swift @@ -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() {