-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathfmt_convert.mm
More file actions
40 lines (34 loc) · 1.37 KB
/
fmt_convert.mm
File metadata and controls
40 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// fmt_convert.m
// Kodi Remote
//
// Created by Buschmann on 14.11.25.
// Copyright © 2025 Team Kodi. All rights reserved.
//
#import "fmt_convert.h"
#include <fmt/core.h>
#include <string>
@implementation NSString (fmt)
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat intValue:(int)value {
try {
const std::string formatted = fmt::format(format.UTF8String, value);
// not sure if `length` param should be +1 to account for the terminating 0-character
return [[NSString alloc] initWithBytes:formatted.data() length:formatted.size() encoding:NSUTF8StringEncoding];
}
catch (const std::exception &exc) {
NSLog(@"generic format error: %s", exc.what());
}
return [NSString stringWithFormat:defaultFormat, value];
}
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat floatValue:(float)value {
try {
const std::string formatted = fmt::format(format.UTF8String, value);
// not sure if `length` param should be +1 to account for the terminating 0-character
return [[NSString alloc] initWithBytes:formatted.data() length:formatted.size() encoding:NSUTF8StringEncoding];
}
catch (const std::exception &exc) {
NSLog(@"generic format error: %s", exc.what());
}
return [NSString stringWithFormat:defaultFormat, value];
}
@end