ObjFW  Diff

Differences From Artifact [f223cdd779]:

To Artifact [03c484c2ac]:


15
16
17
18
19
20
21


22
23
24
25
26
27
28
 */

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>



#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFArray.h"
#import "OFDictionary.h"







>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 */

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFArray.h"
#import "OFDictionary.h"
52
53
54
55
56
57
58









































































































59
60
61
62
63
64
65
/*
 * It seems strtod is buggy on Win32.
 * However, the MinGW version __strtod seems to be ok.
 */
#ifdef _WIN32
# define strtod __strtod
#endif










































































































static OFString*
standardize_path(OFArray *components, OFString *currentDirectory,
    OFString *parentDirectory, OFString *joinString)
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableArray *array;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * It seems strtod is buggy on Win32.
 * However, the MinGW version __strtod seems to be ok.
 */
#ifdef _WIN32
# define strtod __strtod
#endif

/* References for static linking */
void _references_to_categories_of_OFString(void)
{
	_OFString_Hashing_reference = 1;
	_OFString_JSONValue_reference = 1;
	_OFString_Serialization_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
}

void _reference_to_OFConstantString(void)
{
	[OFConstantString class];
}

size_t
of_string_utf8_encode(of_unichar_t character, char *buffer)
{
	size_t i = 0;

	if (character < 0x80) {
		buffer[i] = character;
		return 1;
	} else if (character < 0x800) {
		buffer[i++] = 0xC0 | (character >> 6);
		buffer[i] = 0x80 | (character & 0x3F);
		return 2;
	} else if (character < 0x10000) {
		buffer[i++] = 0xE0 | (character >> 12);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 3;
	} else if (character < 0x110000) {
		buffer[i++] = 0xF0 | (character >> 18);
		buffer[i++] = 0x80 | (character >> 12 & 0x3F);
		buffer[i++] = 0x80 | (character >> 6 & 0x3F);
		buffer[i] = 0x80 | (character & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_utf8_decode(const char *buffer_, size_t length, of_unichar_t *ret)
{
	const uint8_t *buffer = (const uint8_t*)buffer_;

	if (!(*buffer & 0x80)) {
		*ret = buffer[0];
		return 1;
	}

	if ((*buffer & 0xE0) == 0xC0) {
		if OF_UNLIKELY (length < 2)
			return 0;

		*ret = ((buffer[0] & 0x1F) << 6) | (buffer[1] & 0x3F);
		return 2;
	}

	if ((*buffer & 0xF0) == 0xE0) {
		if OF_UNLIKELY (length < 3)
			return 0;

		*ret = ((buffer[0] & 0x0F) << 12) | ((buffer[1] & 0x3F) << 6) |
		    (buffer[2] & 0x3F);
		return 3;
	}

	if ((*buffer & 0xF8) == 0xF0) {
		if OF_UNLIKELY (length < 4)
			return 0;

		*ret = ((buffer[0] & 0x07) << 18) | ((buffer[1] & 0x3F) << 12) |
		    ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F);
		return 4;
	}

	return 0;
}

size_t
of_string_unicode_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

size_t
of_string_utf16_length(const uint16_t *string)
{
	const uint16_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

static OFString*
standardize_path(OFArray *components, OFString *currentDirectory,
    OFString *parentDirectory, OFString *joinString)
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableArray *array;
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
141
142
143
144
145
146
147
148
149
150
151
	ret = [[array componentsJoinedByString: joinString] retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}


/* References for static linking */
void _references_to_categories_of_OFString(void)
{
	_OFString_Hashing_reference = 1;
	_OFString_JSONValue_reference = 1;
	_OFString_Serialization_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
}

void _reference_to_OFConstantString(void)
{
	[OFConstantString class];
}

size_t
of_unicode_string_length(const of_unichar_t *string)
{
	const of_unichar_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

size_t
of_utf16_string_length(const uint16_t *string)
{
	const uint16_t *string_ = string;

	while (*string_ != 0)
		string_++;

	return (size_t)(string_ - string);
}

static struct {
	Class isa;
} placeholder;

@interface OFString_placeholder: OFString
@end








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







206
207
208
209
210
211
212







































213
214
215
216
217
218
219
	ret = [[array componentsJoinedByString: joinString] retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}








































static struct {
	Class isa;
} placeholder;

@interface OFString_placeholder: OFString
@end

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
	}
}

- initWithUnicodeString: (const of_unichar_t*)string
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE
				    length: of_unicode_string_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUnicodeString: string
				 byteOrder: byteOrder
				    length: of_unicode_string_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE







|







|







692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
	}
}

- initWithUnicodeString: (const of_unichar_t*)string
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE
				    length: of_string_unicode_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
	      byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUnicodeString: string
				 byteOrder: byteOrder
				    length: of_string_unicode_length(string)];
}

- initWithUnicodeString: (const of_unichar_t*)string
		 length: (size_t)length
{
	return [self initWithUnicodeString: string
				 byteOrder: OF_BYTE_ORDER_NATIVE
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
	}
}

- initWithUTF16String: (const uint16_t*)string
{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN
				  length: of_utf16_string_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUTF16String: string
			       byteOrder: byteOrder
				  length: of_utf16_string_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN







|







|







728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
	}
}

- initWithUTF16String: (const uint16_t*)string
{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN
				  length: of_string_utf16_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	    byteOrder: (of_byte_order_t)byteOrder
{
	return [self initWithUTF16String: string
			       byteOrder: byteOrder
				  length: of_string_utf16_length(string)];
}

- initWithUTF16String: (const uint16_t*)string
	       length: (size_t)length
{
	return [self initWithUTF16String: string
			       byteOrder: OF_BYTE_ORDER_BIG_ENDIAN
942
943
944
945
946
947
948


949
950
951
952
953
954
955
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		cString[j] = '\0';



		@try {
			cString = [object resizeMemory: cString
						  size: cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}







>
>







1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
			default:
				@throw [OFInvalidEncodingException
				    exceptionWithClass: [self class]];
			}
		}

		cString[j] = '\0';

		assert(j == cStringLength);

		@try {
			cString = [object resizeMemory: cString
						  size: cStringLength + 1];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only tried to make it smaller */
		}