 |
Changeset 3261
- Timestamp:
- 02/23/08 03:32:39
(10 months ago)
- Author:
- kris
- Message:
added an unescape() function, which closes #894
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3210 |
r3261 |
|
| 81 | 81 | matching (s1*, s2*, length) // low-level compare |
|---|
| 82 | 82 | isSpace (match) // is whitespace? |
|---|
| | 83 | unescape(source, output) // convert '\' prefixes |
|---|
| 83 | 84 | layout (destination, format ...) // featherweight printf |
|---|
| 84 | 85 | lines (str) // foreach lines |
|---|
| … | … | |
| 965 | 966 | /****************************************************************************** |
|---|
| 966 | 967 | |
|---|
| | 968 | Convert 'escaped' chars to normal ones: \t => ^t for example. |
|---|
| | 969 | Supports \" \' \\ \a \b \f \n \r \t \v |
|---|
| | 970 | |
|---|
| | 971 | ******************************************************************************/ |
|---|
| | 972 | |
|---|
| | 973 | T[] unescape(T) (T[] src, T[] dst = null) |
|---|
| | 974 | { |
|---|
| | 975 | int delta; |
|---|
| | 976 | auto s = src.ptr; |
|---|
| | 977 | auto len = src.length; |
|---|
| | 978 | |
|---|
| | 979 | // take a peek first to see if there's anything |
|---|
| | 980 | if ((delta = indexOf (s, '\\', len)) < len) |
|---|
| | 981 | { |
|---|
| | 982 | // make some room if not enough provided |
|---|
| | 983 | if (dst.length < src.length) |
|---|
| | 984 | dst.length = src.length; |
|---|
| | 985 | auto d = dst.ptr; |
|---|
| | 986 | |
|---|
| | 987 | // copy segments over, a chunk at a time |
|---|
| | 988 | do { |
|---|
| | 989 | d [0 .. delta] = s [0 .. delta]; |
|---|
| | 990 | len -= delta; |
|---|
| | 991 | s += delta; |
|---|
| | 992 | d += delta; |
|---|
| | 993 | |
|---|
| | 994 | // bogus trailing '\' |
|---|
| | 995 | if (len < 2) |
|---|
| | 996 | { |
|---|
| | 997 | *d++ = '\\'; |
|---|
| | 998 | len = 0; |
|---|
| | 999 | break; |
|---|
| | 1000 | } |
|---|
| | 1001 | |
|---|
| | 1002 | // translate \char |
|---|
| | 1003 | auto c = s[1]; |
|---|
| | 1004 | switch (c) |
|---|
| | 1005 | { |
|---|
| | 1006 | case '\\': |
|---|
| | 1007 | break; |
|---|
| | 1008 | case '\'': |
|---|
| | 1009 | c = '\''; |
|---|
| | 1010 | break; |
|---|
| | 1011 | case '"': |
|---|
| | 1012 | c = '"'; |
|---|
| | 1013 | break; |
|---|
| | 1014 | case 'a': |
|---|
| | 1015 | c = '\a'; |
|---|
| | 1016 | break; |
|---|
| | 1017 | case 'b': |
|---|
| | 1018 | c = '\b'; |
|---|
| | 1019 | break; |
|---|
| | 1020 | case 'f': |
|---|
| | 1021 | c = '\f'; |
|---|
| | 1022 | break; |
|---|
| | 1023 | case 'n': |
|---|
| | 1024 | c = '\n'; |
|---|
| | 1025 | break; |
|---|
| | 1026 | case 'r': |
|---|
| | 1027 | c = '\r'; |
|---|
| | 1028 | break; |
|---|
| | 1029 | case 't': |
|---|
| | 1030 | c = '\t'; |
|---|
| | 1031 | break; |
|---|
| | 1032 | case 'v': |
|---|
| | 1033 | c = '\v'; |
|---|
| | 1034 | break; |
|---|
| | 1035 | default: |
|---|
| | 1036 | *d++ = '\\'; |
|---|
| | 1037 | } |
|---|
| | 1038 | *d++ = c; |
|---|
| | 1039 | len -= 2; |
|---|
| | 1040 | s += 2; |
|---|
| | 1041 | } while ((delta = indexOf (s, '\\', len)) < len); |
|---|
| | 1042 | |
|---|
| | 1043 | // copy tail too |
|---|
| | 1044 | d [0 .. len] = s [0 .. len]; |
|---|
| | 1045 | return dst [0 .. (d + len) - dst.ptr]; |
|---|
| | 1046 | } |
|---|
| | 1047 | return src; |
|---|
| | 1048 | } |
|---|
| | 1049 | |
|---|
| | 1050 | |
|---|
| | 1051 | /****************************************************************************** |
|---|
| | 1052 | |
|---|
| 967 | 1053 | jhash() -- hash a variable-length key into a 32-bit value |
|---|
| 968 | 1054 | |
|---|
| … | … | |
| 1246 | 1332 | debug (UnitTest) |
|---|
| 1247 | 1333 | { |
|---|
| 1248 | | //void main() {} |
|---|
| | 1334 | void main() {} |
|---|
| 1249 | 1335 | |
|---|
| 1250 | 1336 | unittest |
|---|
| … | … | |
| 1417 | 1503 | assert (repeat ("abc", 2, rep) == "abcabc"); |
|---|
| 1418 | 1504 | assert (repeat ("", 4, rep) == ""); |
|---|
| | 1505 | |
|---|
| | 1506 | assert (unescape ("abc") == "abc"); |
|---|
| | 1507 | assert (unescape ("abc\\") == "abc\\"); |
|---|
| | 1508 | assert (unescape ("abc\\t") == "abc\t"); |
|---|
| | 1509 | assert (unescape ("abc\\tc") == "abc\tc"); |
|---|
| | 1510 | assert (unescape ("\\t") == "\t"); |
|---|
| | 1511 | assert (unescape ("\\tx") == "\tx"); |
|---|
| | 1512 | assert (unescape ("\\v\\vx") == "\v\vx"); |
|---|
| | 1513 | assert (unescape ("abc\\t\\a\\bc") == "abc\t\a\bc"); |
|---|
| 1419 | 1514 | } |
|---|
| 1420 | 1515 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic