Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2018-2019 Savoir-faire Linux Inc. |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "NSString+Extensions.h" |
| 21 | |
| 22 | @implementation NSString (Extensions) |
| 23 | |
| 24 | - (NSString *) removeAllNewLinesAtTheEnd { |
| 25 | NSString *result = self; |
| 26 | while ([result endedByEmptyLine]) { |
| 27 | result = [result removeLastWhiteSpaceAndNewLineCharacter]; |
| 28 | } |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | - (NSString *) removeAllNewLinesAtBegining { |
| 33 | NSString *result = self; |
| 34 | while ([result startByEmptyLine]) { |
| 35 | result = [result removeFirstWhiteSpaceAndNewLineCharacter]; |
| 36 | } |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | - (NSString *) removeEmptyLinesAtBorders { |
| 41 | NSString *result = self; |
| 42 | result = [result removeAllNewLinesAtBegining]; |
| 43 | result = [result removeAllNewLinesAtTheEnd]; |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | -(bool)endedByEmptyLine { |
| 48 | if ([self length] < 1) { |
| 49 | return false; |
| 50 | } |
| 51 | unichar last = [self characterAtIndex:[self length] - 1]; |
| 52 | return [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:last]; |
| 53 | } |
| 54 | |
| 55 | - (bool)startByEmptyLine { |
| 56 | if ([self length] < 1) { |
| 57 | return false; |
| 58 | } |
| 59 | unichar first = [self characterAtIndex:0]; |
| 60 | return [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:first]; |
| 61 | } |
| 62 | |
| 63 | - (NSString *) removeLastWhiteSpaceAndNewLineCharacter { |
| 64 | if ([self endedByEmptyLine]) { |
| 65 | return [self substringToIndex:[self length]-1]; |
| 66 | } |
| 67 | return self; |
| 68 | } |
| 69 | |
| 70 | - (NSString *) removeFirstWhiteSpaceAndNewLineCharacter { |
| 71 | if ([self startByEmptyLine]) { |
| 72 | return [self substringFromIndex:1]; |
| 73 | } |
| 74 | return self; |
| 75 | } |
| 76 | |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 77 | + (NSString *) formattedStringTimeFromSeconds:(int) totalSeconds { |
| 78 | int seconds = totalSeconds % 60; |
| 79 | int minutes = (totalSeconds / 60) % 60; |
| 80 | return [NSString stringWithFormat:@"%02d:%02d",minutes, seconds]; |
| 81 | } |
| 82 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 83 | @end |