Repository navigation
Expand file tree
/
Copy pathStringUtils.m
More file actions
119 lines (99 loc) · 2.98 KB
/
Copy pathStringUtils.m
File metadata and controls
119 lines (99 loc) · 2.98 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// StringUtils.m
// Intuit
//
// Created by Глеб Тарасов on 11.01.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "StringUtils.h"
@implementation StringUtils
+ (NSArray *)splitByCapitalLetters:(NSString *)string
{
if (!string)
return nil;
if (string.length == 0)
return [NSArray array];
if (string.length == 1)
return [NSArray arrayWithObject:[string lowercaseString]];
NSMutableArray *result = [NSMutableArray array];
NSInteger lastCapital = 0;
for (int i = 1; i < string.length + 1; ++i)
{
unichar c = i == string.length ? 0 : [string characterAtIndex:i];
if (isupper(c) || i == string.length)
{
NSRange range;
range.length = i - lastCapital;
range.location = lastCapital;
NSString *part = [string substringWithRange:range];
[result addObject:part.lowercaseString];
lastCapital = i;
}
}
return result;
}
+ (NSString *)getCase:(NSInteger)num
nominative:(NSString *)nominative
genitive:(NSString *)genitive
pluralGenitive:(NSString *)pluralGenitive
{
if ((num % 10) == 1 && num != 11)
return nominative;
if ((num % 10) >= 2 && (num % 10) <= 4 && num / 10 != 1)
{
return genitive;
}
return pluralGenitive;
}
+ (NSString *)sizeToString:(NSInteger)bytes
{
CGFloat floatBytes = bytes;
CGFloat result;
NSString *format;
if (bytes >= (1 << 30))
{
result = floatBytes / (1 << 30);
format = @"%d Гб";
}
else if (bytes >= (1 << 20))
{
result = floatBytes / (1 << 20);
format = @"%d Мб";
}
else if (bytes >= (1 << 10))
{
result = floatBytes / (1 << 10);
format = @"%d Кб";
}
else
{
result = bytes;
format = @"%d б";
}
return [NSString stringWithFormat:format, (int)roundf(result)];
}
+ (NSString *)timeIntervalToString:(NSTimeInterval)time
{
int seconds = time;
int minutes = seconds / 60;
seconds %= 60;
int hours = minutes / 60;
minutes %= 60;
NSString *hoursString = [self getCase:hours nominative:@"час" genitive:@"часа" pluralGenitive:@"часов"];
NSString *minutesString = [self getCase:minutes nominative:@"минута" genitive:@"минуты" pluralGenitive:@"минут"];
NSString *secondsString = [self getCase:seconds nominative:@"секунда" genitive:@"секунды" pluralGenitive:@"секунд"];
if (hours)
{
return [NSString stringWithFormat:@"%d %@ %d %@", hours, hoursString, minutes, minutesString];
}
else if (minutes)
{
return [NSString stringWithFormat:@"%d %@ %d %@", minutes, minutesString, seconds, secondsString];
}
else
{
return [NSString stringWithFormat:@"%d %@", seconds, secondsString];
}
}
@end