Skip to content

Commit b448164

Browse files
committed
fix: add endian.h
1 parent c3ce8d6 commit b448164

File tree

3 files changed

+243
-1
lines changed

3 files changed

+243
-1
lines changed

_automation/treesitter_updater/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func modifyIncludePaths(path string) error {
157157
// Modify the content and write back
158158
modifiedContent := strings.ReplaceAll(string(content), `"tree_sitter/`, `"`)
159159
modifiedContent = strings.ReplaceAll(modifiedContent, `"unicode/`, `"`)
160+
modifiedContent = strings.ReplaceAll(modifiedContent, `"portable/`, `"`)
160161
return os.WriteFile(filePath, []byte(modifiedContent), info.Mode())
161162
})
162163
}

endian.h

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// "License": Public Domain
2+
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
3+
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
4+
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
5+
// an example on how to get the endian conversion functions on different platforms.
6+
7+
// updates from https://github.com/mikepb/endian.h/issues/4
8+
9+
#ifndef ENDIAN_H
10+
#define ENDIAN_H
11+
12+
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
13+
14+
# define __WINDOWS__
15+
16+
#endif
17+
18+
#if defined(HAVE_ENDIAN_H) || \
19+
defined(__linux__) || \
20+
defined(__GNU__) || \
21+
defined(__HAIKU__) || \
22+
defined(__illumos__) || \
23+
defined(__NetBSD__) || \
24+
defined(__OpenBSD__) || \
25+
defined(__CYGWIN__) || \
26+
defined(__MSYS__) || \
27+
defined(__EMSCRIPTEN__) || \
28+
defined(__wasi__) || \
29+
defined(__wasm__)
30+
31+
#if defined(__NetBSD__)
32+
#define _NETBSD_SOURCE 1
33+
#endif
34+
35+
# include <endian.h>
36+
37+
#elif defined(HAVE_SYS_ENDIAN_H) || \
38+
defined(__FreeBSD__) || \
39+
defined(__DragonFly__)
40+
41+
# include <sys/endian.h>
42+
43+
#elif defined(__APPLE__)
44+
# define __BYTE_ORDER BYTE_ORDER
45+
# define __BIG_ENDIAN BIG_ENDIAN
46+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
47+
# define __PDP_ENDIAN PDP_ENDIAN
48+
49+
# if !defined(_POSIX_C_SOURCE)
50+
# include <libkern/OSByteOrder.h>
51+
52+
# define htobe16(x) OSSwapHostToBigInt16(x)
53+
# define htole16(x) OSSwapHostToLittleInt16(x)
54+
# define be16toh(x) OSSwapBigToHostInt16(x)
55+
# define le16toh(x) OSSwapLittleToHostInt16(x)
56+
57+
# define htobe32(x) OSSwapHostToBigInt32(x)
58+
# define htole32(x) OSSwapHostToLittleInt32(x)
59+
# define be32toh(x) OSSwapBigToHostInt32(x)
60+
# define le32toh(x) OSSwapLittleToHostInt32(x)
61+
62+
# define htobe64(x) OSSwapHostToBigInt64(x)
63+
# define htole64(x) OSSwapHostToLittleInt64(x)
64+
# define be64toh(x) OSSwapBigToHostInt64(x)
65+
# define le64toh(x) OSSwapLittleToHostInt64(x)
66+
# else
67+
# if BYTE_ORDER == LITTLE_ENDIAN
68+
# define htobe16(x) __builtin_bswap16(x)
69+
# define htole16(x) (x)
70+
# define be16toh(x) __builtin_bswap16(x)
71+
# define le16toh(x) (x)
72+
73+
# define htobe32(x) __builtin_bswap32(x)
74+
# define htole32(x) (x)
75+
# define be32toh(x) __builtin_bswap32(x)
76+
# define le32toh(x) (x)
77+
78+
# define htobe64(x) __builtin_bswap64(x)
79+
# define htole64(x) (x)
80+
# define be64toh(x) __builtin_bswap64(x)
81+
# define le64toh(x) (x)
82+
# elif BYTE_ORDER == BIG_ENDIAN
83+
# define htobe16(x) (x)
84+
# define htole16(x) __builtin_bswap16(x)
85+
# define be16toh(x) (x)
86+
# define le16toh(x) __builtin_bswap16(x)
87+
88+
# define htobe32(x) (x)
89+
# define htole32(x) __builtin_bswap32(x)
90+
# define be32toh(x) (x)
91+
# define le32toh(x) __builtin_bswap32(x)
92+
93+
# define htobe64(x) (x)
94+
# define htole64(x) __builtin_bswap64(x)
95+
# define be64toh(x) (x)
96+
# define le64toh(x) __builtin_bswap64(x)
97+
# else
98+
# error byte order not supported
99+
# endif
100+
# endif
101+
102+
#elif defined(__WINDOWS__)
103+
104+
# if defined(_MSC_VER) && !defined(__clang__)
105+
# include <stdlib.h>
106+
# define B_SWAP_16(x) _byteswap_ushort(x)
107+
# define B_SWAP_32(x) _byteswap_ulong(x)
108+
# define B_SWAP_64(x) _byteswap_uint64(x)
109+
# else
110+
# define B_SWAP_16(x) __builtin_bswap16(x)
111+
# define B_SWAP_32(x) __builtin_bswap32(x)
112+
# define B_SWAP_64(x) __builtin_bswap64(x)
113+
# endif
114+
115+
# if defined(__MINGW32__) || defined(HAVE_SYS_PARAM_H)
116+
# include <sys/param.h>
117+
# endif
118+
119+
# ifndef BIG_ENDIAN
120+
# ifdef __BIG_ENDIAN
121+
# define BIG_ENDIAN __BIG_ENDIAN
122+
# elif defined(__ORDER_BIG_ENDIAN__)
123+
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
124+
# else
125+
# define BIG_ENDIAN 4321
126+
# endif
127+
# endif
128+
129+
# ifndef LITTLE_ENDIAN
130+
# ifdef __LITTLE_ENDIAN
131+
# define LITTLE_ENDIAN __LITTLE_ENDIAN
132+
# elif defined(__ORDER_LITTLE_ENDIAN__)
133+
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
134+
# else
135+
# define LITTLE_ENDIAN 1234
136+
# endif
137+
# endif
138+
139+
# ifndef BYTE_ORDER
140+
# ifdef __BYTE_ORDER
141+
# define BYTE_ORDER __BYTE_ORDER
142+
# elif defined(__BYTE_ORDER__)
143+
# define BYTE_ORDER __BYTE_ORDER__
144+
# else
145+
/* assume LE on Windows if nothing was defined */
146+
# define BYTE_ORDER LITTLE_ENDIAN
147+
# endif
148+
# endif
149+
150+
# if BYTE_ORDER == LITTLE_ENDIAN
151+
152+
# define htobe16(x) B_SWAP_16(x)
153+
# define htole16(x) (x)
154+
# define be16toh(x) B_SWAP_16(x)
155+
# define le16toh(x) (x)
156+
157+
# define htobe32(x) B_SWAP_32(x)
158+
# define htole32(x) (x)
159+
# define be32toh(x) B_SWAP_32(x)
160+
# define le32toh(x) (x)
161+
162+
# define htobe64(x) B_SWAP_64(x)
163+
# define htole64(x) (x)
164+
# define be64toh(x) B_SWAP_64(x)
165+
# define le64toh(x) (x)
166+
167+
# elif BYTE_ORDER == BIG_ENDIAN
168+
169+
# define htobe16(x) (x)
170+
# define htole16(x) B_SWAP_16(x)
171+
# define be16toh(x) (x)
172+
# define le16toh(x) B_SWAP_16(x)
173+
174+
# define htobe32(x) (x)
175+
# define htole32(x) B_SWAP_32(x)
176+
# define be32toh(x) (x)
177+
# define le32toh(x) B_SWAP_32(x)
178+
179+
# define htobe64(x) (x)
180+
# define htole64(x) B_SWAP_64(x)
181+
# define be64toh(x) (x)
182+
# define le64toh(x) B_SWAP_64(x)
183+
184+
# else
185+
186+
# error byte order not supported
187+
188+
# endif
189+
190+
#elif defined(__QNXNTO__)
191+
192+
# include <gulliver.h>
193+
194+
# define __LITTLE_ENDIAN 1234
195+
# define __BIG_ENDIAN 4321
196+
# define __PDP_ENDIAN 3412
197+
198+
# if defined(__BIGENDIAN__)
199+
200+
# define __BYTE_ORDER __BIG_ENDIAN
201+
202+
# define htobe16(x) (x)
203+
# define htobe32(x) (x)
204+
# define htobe64(x) (x)
205+
206+
# define htole16(x) ENDIAN_SWAP16(x)
207+
# define htole32(x) ENDIAN_SWAP32(x)
208+
# define htole64(x) ENDIAN_SWAP64(x)
209+
210+
# elif defined(__LITTLEENDIAN__)
211+
212+
# define __BYTE_ORDER __LITTLE_ENDIAN
213+
214+
# define htole16(x) (x)
215+
# define htole32(x) (x)
216+
# define htole64(x) (x)
217+
218+
# define htobe16(x) ENDIAN_SWAP16(x)
219+
# define htobe32(x) ENDIAN_SWAP32(x)
220+
# define htobe64(x) ENDIAN_SWAP64(x)
221+
222+
# else
223+
224+
# error byte order not supported
225+
226+
# endif
227+
228+
# define be16toh(x) ENDIAN_BE16(x)
229+
# define be32toh(x) ENDIAN_BE32(x)
230+
# define be64toh(x) ENDIAN_BE64(x)
231+
# define le16toh(x) ENDIAN_LE16(x)
232+
# define le32toh(x) ENDIAN_LE32(x)
233+
# define le64toh(x) ENDIAN_LE64(x)
234+
235+
#else
236+
237+
# error platform not supported
238+
239+
#endif
240+
241+
#endif

unicode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern "C" {
1212
#define U_EXPORT2
1313
#include "utf8.h"
1414
#include "utf16.h"
15-
#include "portable/endian.h"
15+
#include "endian.h"
1616

1717
#define U16_NEXT_LE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
1818
(c)=le16toh((s)[(i)++]); \

0 commit comments

Comments
 (0)