Skip to content

Commit 7417466

Browse files
authored
Merge pull request #5054 from JackStouffer/uri-cleanup
std.uri Cleanup
2 parents 88a964a + ada176f commit 7417466

File tree

2 files changed

+24
-40
lines changed

2 files changed

+24
-40
lines changed

std/uri.d

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,14 @@ module std.uri;
2525

2626
//debug=uri; // uncomment to turn on debugging writefln's
2727
debug(uri) private import std.stdio;
28-
29-
/* ====================== URI Functions ================ */
30-
31-
private import std.ascii;
32-
private import core.stdc.stdlib;
33-
private import std.utf;
3428
private import std.traits : isSomeChar;
35-
import core.exception : OutOfMemoryError;
36-
import std.exception;
3729

3830
/** This Exception is thrown if something goes wrong when encoding or
3931
decoding a URI.
4032
*/
4133
class URIException : Exception
4234
{
35+
import std.exception : basicExceptionCtors;
4336
mixin basicExceptionCtors;
4437
}
4538

@@ -52,9 +45,9 @@ private enum
5245
URI_Hash = 0x10, // '#'
5346
}
5447

55-
immutable char[16] hex2ascii = "0123456789ABCDEF";
48+
private immutable char[16] hex2ascii = "0123456789ABCDEF";
5649

57-
immutable ubyte[128] uri_flags = // indexed by character
50+
private immutable ubyte[128] uri_flags = // indexed by character
5851
({
5952
ubyte[128] uflags;
6053

@@ -74,6 +67,9 @@ immutable ubyte[128] uri_flags = // indexed by character
7467

7568
private string URI_Encode(dstring string, uint unescapedSet)
7669
{
70+
import core.exception : OutOfMemoryError;
71+
import core.stdc.stdlib : alloca;
72+
7773
uint j;
7874
uint k;
7975
dchar V;
@@ -152,27 +148,6 @@ private string URI_Encode(dstring string, uint unescapedSet)
152148
Octet[3] = cast(char)(0x80 | (V & 0x3F));
153149
L = 4;
154150
}
155-
/+
156-
else if (V <= 0x3FFFFFF)
157-
{
158-
Octet[0] = cast(char)(0xF8 | (V >> 24));
159-
Octet[1] = cast(char)(0x80 | ((V >> 18) & 0x3F));
160-
Octet[2] = cast(char)(0x80 | ((V >> 12) & 0x3F));
161-
Octet[3] = cast(char)(0x80 | ((V >> 6) & 0x3F));
162-
Octet[4] = cast(char)(0x80 | (V & 0x3F));
163-
L = 5;
164-
}
165-
else if (V <= 0x7FFFFFFF)
166-
{
167-
Octet[0] = cast(char)(0xFC | (V >> 30));
168-
Octet[1] = cast(char)(0x80 | ((V >> 24) & 0x3F));
169-
Octet[2] = cast(char)(0x80 | ((V >> 18) & 0x3F));
170-
Octet[3] = cast(char)(0x80 | ((V >> 12) & 0x3F));
171-
Octet[4] = cast(char)(0x80 | ((V >> 6) & 0x3F));
172-
Octet[5] = cast(char)(0x80 | (V & 0x3F));
173-
L = 6;
174-
}
175-
+/
176151
else
177152
{
178153
throw new URIException("Undefined UTF-32 code point");
@@ -211,7 +186,7 @@ private string URI_Encode(dstring string, uint unescapedSet)
211186
return R[0..Rlen].idup;
212187
}
213188

214-
uint ascii2hex(dchar c)
189+
private uint ascii2hex(dchar c) @nogc @safe pure nothrow
215190
{
216191
return (c <= '9') ? c - '0' :
217192
(c <= 'F') ? c - 'A' + 10 :
@@ -220,6 +195,10 @@ uint ascii2hex(dchar c)
220195

221196
private dstring URI_Decode(Char)(in Char[] uri, uint reservedSet) if (isSomeChar!Char)
222197
{
198+
import core.exception : OutOfMemoryError;
199+
import core.stdc.stdlib : alloca;
200+
import std.ascii : isHexDigit;
201+
223202
uint j;
224203
uint k;
225204
uint V;
@@ -335,8 +314,9 @@ private dstring URI_Decode(Char)(in Char[] uri, uint reservedSet) if (isSomeChar
335314

336315
string decode(Char)(in Char[] encodedURI) if (isSomeChar!Char)
337316
{
317+
import std.utf : toUTF8;
338318
auto s = URI_Decode(encodedURI, URI_Reserved | URI_Hash);
339-
return std.utf.toUTF8(s);
319+
return toUTF8(s);
340320
}
341321

342322
/*******************************
@@ -346,8 +326,9 @@ string decode(Char)(in Char[] encodedURI) if (isSomeChar!Char)
346326

347327
string decodeComponent(Char)(in Char[] encodedURIComponent) if (isSomeChar!Char)
348328
{
329+
import std.utf : toUTF8;
349330
auto s = URI_Decode(encodedURIComponent, 0);
350-
return std.utf.toUTF8(s);
331+
return toUTF8(s);
351332
}
352333

353334
/*****************************
@@ -357,7 +338,8 @@ string decodeComponent(Char)(in Char[] encodedURIComponent) if (isSomeChar!Char)
357338

358339
string encode(Char)(in Char[] uri) if (isSomeChar!Char)
359340
{
360-
auto s = std.utf.toUTF32(uri);
341+
import std.utf : toUTF32;
342+
auto s = toUTF32(uri);
361343
return URI_Encode(s, URI_Reserved | URI_Hash | URI_Alpha | URI_Digit | URI_Mark);
362344
}
363345

@@ -368,7 +350,8 @@ string encode(Char)(in Char[] uri) if (isSomeChar!Char)
368350

369351
string encodeComponent(Char)(in Char[] uriComponent) if (isSomeChar!Char)
370352
{
371-
auto s = std.utf.toUTF32(uriComponent);
353+
import std.utf : toUTF32;
354+
auto s = toUTF32(uriComponent);
372355
return URI_Encode(s, URI_Alpha | URI_Digit | URI_Mark);
373356
}
374357

@@ -426,6 +409,7 @@ ptrdiff_t uriLength(Char)(in Char[] s) if (isSomeChar!Char)
426409
* https://
427410
* www.
428411
*/
412+
import std.ascii : isAlphaNum;
429413
import std.uni : icmp;
430414

431415
ptrdiff_t i;
@@ -444,8 +428,6 @@ ptrdiff_t uriLength(Char)(in Char[] s) if (isSomeChar!Char)
444428
else
445429
return -1;
446430
}
447-
// if (icmp(s[0 .. 4], "www.") == 0)
448-
// i = 4;
449431

450432
ptrdiff_t lastdot;
451433
for (; i < s.length; i++)
@@ -465,7 +447,6 @@ ptrdiff_t uriLength(Char)(in Char[] s) if (isSomeChar!Char)
465447
}
466448
break;
467449
}
468-
//if (!lastdot || (i - lastdot != 3 && i - lastdot != 4))
469450
if (!lastdot)
470451
return -1;
471452

@@ -493,6 +474,8 @@ ptrdiff_t uriLength(Char)(in Char[] s) if (isSomeChar!Char)
493474
*/
494475
ptrdiff_t emailLength(Char)(in Char[] s) if (isSomeChar!Char)
495476
{
477+
import std.ascii : isAlpha, isAlphaNum;
478+
496479
ptrdiff_t i;
497480

498481
if (!isAlpha(s[0]))

unittest.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ int main(string[] args)
7676
sort(a); // qsort
7777
Clock.currTime(); // datetime
7878
cast(void)isValidDchar(cast(dchar)0); // utf
79-
std.uri.ascii2hex(0); // uri
79+
string s1 = "http://www.digitalmars.com/~fred/fredsRX.html#foo end!";
80+
assert(uriLength(s1) == 49);
8081
std.zlib.adler32(0,null); // D.zlib
8182
auto t = task!cmp("foo", "bar"); // parallelism
8283

0 commit comments

Comments
 (0)