blob: bfed1ac614ddd42cd4aaf4eaf9bfa74977ad8d39 [file] [log] [blame]
Andreas Traczyk252a94a2018-04-20 16:36:20 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2018-2019 Savoir-faire Linux Inc.
Andreas Traczyk252a94a2018-04-20 16:36:20 -04003 * 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
77@end