-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMiscMergeFunctions.m
executable file
·140 lines (115 loc) · 3.76 KB
/
MiscMergeFunctions.m
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// MiscMergeFunctions.m
//
// Written by Doug McClure and Carl Lindberg
//
// Copyright 2002-2004 by Don Yacktman, Doug McClure, and Carl Lindberg.
// All rights reserved.
//
// This notice may not be removed from this source code.
//
// This header is included in the MiscKit by permission from the author
// and its use is governed by the MiscKit license, found in the file
// "License.rtf" in the MiscKit distribution. Please refer to that file
// for a list of all applicable permissions and restrictions.
//
#import "MiscMergeFunctions.h"
#import <Foundation/NSValue.h>
#import "NSString+MiscAdditions.h"
BOOL MMBoolValueOfObject(id anObject)
{
if ( MMIsObjectANumber(anObject) )
return MMDoubleValueForObject(anObject) != 0;
else if ( MMIsObjectAString(anObject) )
return ([anObject length] > 0);
else
return (anObject != nil);
}
int MMIntegerValueOfObject(id anObject)
{
if ([anObject isKindOfClass:[NSNumber class]])
return [anObject intValue];
if ([anObject isKindOfClass:[NSString class]]) {
NSScanner *scanner = [NSScanner scannerWithString:anObject];
int intValue;
[scanner scanInt:&intValue];
return intValue;
}
return 0;
}
double MMDoubleValueForObject(id anObject)
{
if ( MMIsObjectANumber(anObject) )
return [anObject doubleValue];
else if ( MMIsObjectAString(anObject) )
return (double)([anObject length] > 0);
else
return (double)(anObject != nil);
}
BOOL MMIsObjectANumber(id anObject)
{
if ([anObject isKindOfClass:[NSNumber class]])
return YES;
if ([anObject isKindOfClass:[NSString class]]) {
NSScanner *scanner = [NSScanner scannerWithString:anObject];
float floatValue;
return [scanner scanFloat:&floatValue];
}
return NO;
}
BOOL MMIsObjectAString(id anObject)
{
return [anObject isKindOfClass:[NSString class]];
}
BOOL MMIsBooleanTrueString(NSString *anObject)
{
return ([anObject compare:@"yes" options:NSCaseInsensitiveSearch] == 0 ||
[anObject compare:@"true" options:NSCaseInsensitiveSearch] == 0);
}
NSComparisonResult MMCompareFloats(float num1, float num2)
{
if (num1 < num2) return NSOrderedAscending;
if (num1 > num2) return NSOrderedDescending;
return NSOrderedSame;
}
Class MMCommonAnscestorClass(id obj1, id obj2)
{
Class currSuperclass = [obj1 class];
for (; currSuperclass != nil; currSuperclass = [currSuperclass superclass]) {
Class currTestClass = [obj2 class];
for (; currTestClass != nil; currTestClass = [currTestClass superclass]) {
if (currTestClass == currSuperclass)
return currSuperclass;
}
}
return nil;
}
NSString *MMStringByTrimmingCommandSpace(NSString *string)
{
if ( [string isBlank] ) {
return @"";
}
else {
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
NSCharacterSet *newlineSet = [NSCharacterSet newlineCharacterSet];
NSInteger start = 0, end = [string length]-1;
NSInteger i;
for ( i = 0; i <= end; i++ ) {
unichar character = [string characterAtIndex:i];
if ( ![whitespaceSet characterIsMember:character] ) {
if ( [newlineSet characterIsMember:character] )
start = i+1;
break;
}
}
for ( i = end; i >= start; i-- ) {
unichar character = [string characterAtIndex:i];
if ( ![whitespaceSet characterIsMember:character] ) {
if ( [newlineSet characterIsMember:character] )
end = i;
break;
}
}
return [string substringWithRange:NSMakeRange(start, end - start + 1)];
}
}