Rewrite SecondsFormatter for better readability.
This commit is contained in:
parent
5e7fbc64e9
commit
8dd0315aba
1 changed files with 53 additions and 85 deletions
|
@ -77,6 +77,11 @@
|
||||||
forString:(NSString *)string
|
forString:(NSString *)string
|
||||||
errorDescription:(out NSString * _Nullable __autoreleasing *)error
|
errorDescription:(out NSString * _Nullable __autoreleasing *)error
|
||||||
{
|
{
|
||||||
|
// In the previous implementation,
|
||||||
|
// all types were incorrectly treated indentically.
|
||||||
|
// This made the code much simpler,
|
||||||
|
// but the added complexity is needed to support both negative and large values.
|
||||||
|
|
||||||
NSScanner *scanner = [NSScanner scannerWithString:string];
|
NSScanner *scanner = [NSScanner scannerWithString:string];
|
||||||
|
|
||||||
BOOL malformed = NO;
|
BOOL malformed = NO;
|
||||||
|
@ -123,100 +128,63 @@
|
||||||
int seconds = 0;
|
int seconds = 0;
|
||||||
|
|
||||||
if (malformed == NO) {
|
if (malformed == NO) {
|
||||||
int secondsIndex;
|
// `segments` entries need to be mapped to the correct unit type.
|
||||||
int minutesIndex;
|
// The position of each type depends on the number of scanned segments.
|
||||||
int hoursIndex;
|
|
||||||
int daysIndex;
|
|
||||||
|
|
||||||
switch (lastScannedSegment) {
|
const int typeCount = segmentCount;
|
||||||
case 0: {
|
|
||||||
secondsIndex = 0;
|
typedef enum : int {
|
||||||
minutesIndex = -1;
|
DAYS = 0, HOURS = 1, MINUTES = 2, SECONDS = 3,
|
||||||
hoursIndex = -1;
|
} SegmentType;
|
||||||
daysIndex = -1;
|
|
||||||
|
const int segmentIndexes[segmentCount][typeCount] = {
|
||||||
|
{ -1, -1, -1, 0 },
|
||||||
|
{ -1, -1, 0, 1 },
|
||||||
|
{ -1, 0, 1, 2 },
|
||||||
|
{ 0, 1, 2, 3 },
|
||||||
|
};
|
||||||
|
|
||||||
|
#define HAS_SEGMENT(segmentType) \
|
||||||
|
(segmentIndexes[lastScannedSegment][(segmentType)] >= 0)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int max;
|
||||||
|
int scaleFactor;
|
||||||
|
} SegmentMetadata;
|
||||||
|
|
||||||
|
const SegmentMetadata segmentMetadata[segmentCount] = {
|
||||||
|
{.max = INT32_MAX, .scaleFactor = 24},
|
||||||
|
{.max = 24, .scaleFactor = 60},
|
||||||
|
{.max = 60, .scaleFactor = 60},
|
||||||
|
{.max = 60, .scaleFactor = 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (SegmentType segmentType = DAYS; segmentType < segmentCount; segmentType += 1) {
|
||||||
|
if (!HAS_SEGMENT(segmentType)) {
|
||||||
|
if (segmentType == SECONDS) {
|
||||||
|
// Must have SECONDS.
|
||||||
|
malformed = YES;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
case 1: {
|
continue;
|
||||||
secondsIndex = 1;
|
|
||||||
minutesIndex = 0;
|
|
||||||
hoursIndex = -1;
|
|
||||||
daysIndex = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 2: {
|
|
||||||
secondsIndex = 2;
|
|
||||||
minutesIndex = 1;
|
|
||||||
hoursIndex = 0;
|
|
||||||
daysIndex = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 3: {
|
|
||||||
secondsIndex = 3;
|
|
||||||
minutesIndex = 2;
|
|
||||||
hoursIndex = 1;
|
|
||||||
daysIndex = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default: {
|
|
||||||
secondsIndex = -1;
|
|
||||||
minutesIndex = -1;
|
|
||||||
hoursIndex = -1;
|
|
||||||
daysIndex = -1;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const BOOL hasDaysSegment = daysIndex >= 0;
|
const int index = segmentIndexes[lastScannedSegment][segmentType];
|
||||||
const BOOL hasHoursSegment = hoursIndex >= 0;
|
|
||||||
const BOOL hasMinutesSegment = minutesIndex >= 0;
|
|
||||||
const BOOL hasSecondsSegment = secondsIndex >= 0;
|
|
||||||
|
|
||||||
if (hasDaysSegment) {
|
const SegmentMetadata metadata = segmentMetadata[segmentType];
|
||||||
if ((segments[daysIndex] >= 0) && (segments[daysIndex] < INT32_MAX)) {
|
|
||||||
seconds += segments[daysIndex];
|
if ((segments[index] >= 0) && (segments[index] < metadata.max)) {
|
||||||
seconds *= 24;
|
seconds += segments[index];
|
||||||
|
seconds *= metadata.scaleFactor;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
malformed = YES;
|
malformed = YES;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasHoursSegment) {
|
|
||||||
if ((segments[hoursIndex] >= 0) && (segments[hoursIndex] < 24)) {
|
|
||||||
seconds += segments[hoursIndex];
|
|
||||||
seconds *= 60;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
malformed = YES;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasMinutesSegment) {
|
|
||||||
if ((segments[minutesIndex] >= 0) && (segments[minutesIndex] < 60)) {
|
|
||||||
seconds += segments[minutesIndex];
|
|
||||||
seconds *= 60;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
malformed = YES;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasSecondsSegment) {
|
|
||||||
if ((segments[secondsIndex] >= 0) && (segments[secondsIndex] < 60)) {
|
|
||||||
seconds += segments[secondsIndex];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
malformed = YES;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
malformed = YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
seconds *= (isNegative ? -1 : 1);
|
seconds *= (isNegative ? -1 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue