blob: 76a70ab7aa2a67e844c7bcde5287988bc83bddf6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the NumericLiteralParser, CharLiteralParser, and
11// StringLiteralParser interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/LiteralSupport.h"
16#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
Chris Lattner136f93a2007-07-16 06:55:01 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/StringExtras.h"
22using namespace clang;
23
24/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
25/// not valid.
26static int HexDigitValue(char C) {
27 if (C >= '0' && C <= '9') return C-'0';
28 if (C >= 'a' && C <= 'f') return C-'a'+10;
29 if (C >= 'A' && C <= 'F') return C-'A'+10;
30 return -1;
31}
32
33/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
34/// either a character or a string literal.
35static unsigned ProcessCharEscape(const char *&ThisTokBuf,
36 const char *ThisTokEnd, bool &HadError,
37 SourceLocation Loc, bool IsWide,
38 Preprocessor &PP) {
39 // Skip the '\' char.
40 ++ThisTokBuf;
41
42 // We know that this character can't be off the end of the buffer, because
43 // that would have been \", which would not have been the end of string.
44 unsigned ResultChar = *ThisTokBuf++;
45 switch (ResultChar) {
46 // These map to themselves.
47 case '\\': case '\'': case '"': case '?': break;
48
49 // These have fixed mappings.
50 case 'a':
51 // TODO: K&R: the meaning of '\\a' is different in traditional C
52 ResultChar = 7;
53 break;
54 case 'b':
55 ResultChar = 8;
56 break;
57 case 'e':
58 PP.Diag(Loc, diag::ext_nonstandard_escape, "e");
59 ResultChar = 27;
60 break;
61 case 'f':
62 ResultChar = 12;
63 break;
64 case 'n':
65 ResultChar = 10;
66 break;
67 case 'r':
68 ResultChar = 13;
69 break;
70 case 't':
71 ResultChar = 9;
72 break;
73 case 'v':
74 ResultChar = 11;
75 break;
76
77 //case 'u': case 'U': // FIXME: UCNs.
78 case 'x': { // Hex escape.
79 ResultChar = 0;
80 if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
81 PP.Diag(Loc, diag::err_hex_escape_no_digits);
82 HadError = 1;
83 break;
84 }
85
86 // Hex escapes are a maximal series of hex digits.
87 bool Overflow = false;
88 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
89 int CharVal = HexDigitValue(ThisTokBuf[0]);
90 if (CharVal == -1) break;
91 Overflow |= ResultChar & 0xF0000000; // About to shift out a digit?
92 ResultChar <<= 4;
93 ResultChar |= CharVal;
94 }
95
96 // See if any bits will be truncated when evaluated as a character.
97 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
98 : PP.getTargetInfo().getCharWidth(Loc);
99 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
100 Overflow = true;
101 ResultChar &= ~0U >> (32-CharWidth);
102 }
103
104 // Check for overflow.
105 if (Overflow) // Too many digits to fit in
106 PP.Diag(Loc, diag::warn_hex_escape_too_large);
107 break;
108 }
109 case '0': case '1': case '2': case '3':
110 case '4': case '5': case '6': case '7': {
111 // Octal escapes.
112 --ThisTokBuf;
113 ResultChar = 0;
114
115 // Octal escapes are a series of octal digits with maximum length 3.
116 // "\0123" is a two digit sequence equal to "\012" "3".
117 unsigned NumDigits = 0;
118 do {
119 ResultChar <<= 3;
120 ResultChar |= *ThisTokBuf++ - '0';
121 ++NumDigits;
122 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
123 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
124
125 // Check for overflow. Reject '\777', but not L'\777'.
126 unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc)
127 : PP.getTargetInfo().getCharWidth(Loc);
128 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
129 PP.Diag(Loc, diag::warn_octal_escape_too_large);
130 ResultChar &= ~0U >> (32-CharWidth);
131 }
132 break;
133 }
134
135 // Otherwise, these are not valid escapes.
136 case '(': case '{': case '[': case '%':
137 // GCC accepts these as extensions. We warn about them as such though.
138 if (!PP.getLangOptions().NoExtensions) {
139 PP.Diag(Loc, diag::ext_nonstandard_escape,
140 std::string()+(char)ResultChar);
141 break;
142 }
143 // FALL THROUGH.
144 default:
145 if (isgraph(ThisTokBuf[0])) {
146 PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar);
147 } else {
148 PP.Diag(Loc, diag::ext_unknown_escape, "x"+llvm::utohexstr(ResultChar));
149 }
150 break;
151 }
152
153 return ResultChar;
154}
155
156
157
158
159/// integer-constant: [C99 6.4.4.1]
160/// decimal-constant integer-suffix
161/// octal-constant integer-suffix
162/// hexadecimal-constant integer-suffix
163/// decimal-constant:
164/// nonzero-digit
165/// decimal-constant digit
166/// octal-constant:
167/// 0
168/// octal-constant octal-digit
169/// hexadecimal-constant:
170/// hexadecimal-prefix hexadecimal-digit
171/// hexadecimal-constant hexadecimal-digit
172/// hexadecimal-prefix: one of
173/// 0x 0X
174/// integer-suffix:
175/// unsigned-suffix [long-suffix]
176/// unsigned-suffix [long-long-suffix]
177/// long-suffix [unsigned-suffix]
178/// long-long-suffix [unsigned-sufix]
179/// nonzero-digit:
180/// 1 2 3 4 5 6 7 8 9
181/// octal-digit:
182/// 0 1 2 3 4 5 6 7
183/// hexadecimal-digit:
184/// 0 1 2 3 4 5 6 7 8 9
185/// a b c d e f
186/// A B C D E F
187/// unsigned-suffix: one of
188/// u U
189/// long-suffix: one of
190/// l L
191/// long-long-suffix: one of
192/// ll LL
193///
194/// floating-constant: [C99 6.4.4.2]
195/// TODO: add rules...
196///
197
198NumericLiteralParser::
199NumericLiteralParser(const char *begin, const char *end,
200 SourceLocation TokLoc, Preprocessor &pp)
201 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
202 s = DigitsBegin = begin;
203 saw_exponent = false;
204 saw_period = false;
205 saw_float_suffix = false;
206 isLong = false;
207 isUnsigned = false;
208 isLongLong = false;
209 hadError = false;
210
211 if (*s == '0') { // parse radix
212 s++;
213 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
214 s++;
215 radix = 16;
216 DigitsBegin = s;
217 s = SkipHexDigits(s);
218 if (s == ThisTokEnd) {
219 // Done.
220 } else if (*s == '.') {
221 s++;
222 saw_period = true;
223 s = SkipHexDigits(s);
224 }
225 // A binary exponent can appear with or with a '.'. If dotted, the
226 // binary exponent is required.
227 if (*s == 'p' || *s == 'P') {
228 s++;
229 saw_exponent = true;
230 if (*s == '+' || *s == '-') s++; // sign
231 const char *first_non_digit = SkipDigits(s);
232 if (first_non_digit == s) {
233 Diag(TokLoc, diag::err_exponent_has_no_digits);
234 return;
235 } else {
236 s = first_non_digit;
237 }
238 } else if (saw_period) {
239 Diag(TokLoc, diag::err_hexconstant_requires_exponent);
240 return;
241 }
242 } else if (*s == 'b' || *s == 'B') {
243 // 0b101010 is a GCC extension.
244 ++s;
245 radix = 2;
246 DigitsBegin = s;
247 s = SkipBinaryDigits(s);
248 if (s == ThisTokEnd) {
249 // Done.
250 } else if (isxdigit(*s)) {
251 Diag(TokLoc, diag::err_invalid_binary_digit, std::string(s, s+1));
252 return;
253 }
254 PP.Diag(TokLoc, diag::ext_binary_literal);
255 } else {
256 // For now, the radix is set to 8. If we discover that we have a
257 // floating point constant, the radix will change to 10. Octal floating
258 // point constants are not permitted (only decimal and hexadecimal).
259 radix = 8;
260 DigitsBegin = s;
261 s = SkipOctalDigits(s);
262 if (s == ThisTokEnd) {
263 // Done.
264 } else if (isxdigit(*s)) {
Chris Lattner136f93a2007-07-16 06:55:01 +0000265 TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1));
267 return;
268 } else if (*s == '.') {
269 s++;
270 radix = 10;
271 saw_period = true;
272 s = SkipDigits(s);
273 }
274 if (*s == 'e' || *s == 'E') { // exponent
275 s++;
276 radix = 10;
277 saw_exponent = true;
278 if (*s == '+' || *s == '-') s++; // sign
279 const char *first_non_digit = SkipDigits(s);
280 if (first_non_digit == s) {
281 Diag(TokLoc, diag::err_exponent_has_no_digits);
282 return;
283 } else {
284 s = first_non_digit;
285 }
286 }
287 }
288 } else { // the first digit is non-zero
289 radix = 10;
290 s = SkipDigits(s);
291 if (s == ThisTokEnd) {
292 // Done.
293 } else if (isxdigit(*s)) {
294 Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1));
295 return;
296 } else if (*s == '.') {
297 s++;
298 saw_period = true;
299 s = SkipDigits(s);
300 }
301 if (*s == 'e' || *s == 'E') { // exponent
302 s++;
303 saw_exponent = true;
304 if (*s == '+' || *s == '-') s++; // sign
305 const char *first_non_digit = SkipDigits(s);
306 if (first_non_digit == s) {
307 Diag(TokLoc, diag::err_exponent_has_no_digits);
308 return;
309 } else {
310 s = first_non_digit;
311 }
312 }
313 }
314
315 SuffixBegin = s;
316
317 if (saw_period || saw_exponent) {
318 if (s < ThisTokEnd) { // parse size suffix (float, long double)
319 if (*s == 'f' || *s == 'F') {
320 saw_float_suffix = true;
321 s++;
322 } else if (*s == 'l' || *s == 'L') {
323 isLong = true;
324 s++;
325 }
326 if (s != ThisTokEnd) {
327 Diag(TokLoc, diag::err_invalid_suffix_float_constant,
328 std::string(SuffixBegin, ThisTokEnd));
329 return;
330 }
331 }
332 } else {
333 if (s < ThisTokEnd) {
334 // parse int suffix - they can appear in any order ("ul", "lu", "llu").
335 if (*s == 'u' || *s == 'U') {
336 s++;
337 isUnsigned = true; // unsigned
338
339 if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) {
340 s++;
341 // handle "long long" type - l's need to be adjacent and same case.
342 if ((s < ThisTokEnd) && (*s == *(s-1))) {
343 isLongLong = true; // unsigned long long
344 s++;
345 } else {
346 isLong = true; // unsigned long
347 }
348 }
349 } else if (*s == 'l' || *s == 'L') {
350 s++;
351 // handle "long long" types - l's need to be adjacent and same case.
352 if ((s < ThisTokEnd) && (*s == *(s-1))) {
353 s++;
354 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
355 isUnsigned = true; // unsigned long long
356 s++;
357 } else {
358 isLongLong = true; // long long
359 }
360 } else { // handle "long" types
361 if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) {
362 isUnsigned = true; // unsigned long
363 s++;
364 } else {
365 isLong = true; // long
366 }
367 }
368 }
369 if (s != ThisTokEnd) {
370 Diag(TokLoc, diag::err_invalid_suffix_integer_constant,
371 std::string(SuffixBegin, ThisTokEnd));
372 return;
373 }
374 }
375 }
376}
377
378/// GetIntegerValue - Convert this numeric literal value to an APInt that
379/// matches Val's input width. If there is an overflow, set Val to the low bits
380/// of the result and return true. Otherwise, return false.
381bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
382 Val = 0;
383 s = DigitsBegin;
384
385 llvm::APInt RadixVal(Val.getBitWidth(), radix);
386 llvm::APInt CharVal(Val.getBitWidth(), 0);
387 llvm::APInt OldVal = Val;
388
389 bool OverflowOccurred = false;
390 while (s < SuffixBegin) {
391 unsigned C = HexDigitValue(*s++);
392
393 // If this letter is out of bound for this radix, reject it.
394 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
395
396 CharVal = C;
397
398 // Add the digit to the value in the appropriate radix. If adding in digits
399 // made the value smaller, then this overflowed.
400 OldVal = Val;
401
402 // Multiply by radix, did overflow occur on the multiply?
403 Val *= RadixVal;
404 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
405
406 OldVal = Val;
407 // Add value, did overflow occur on the value?
408 Val += CharVal;
409 OverflowOccurred |= Val.ult(OldVal);
410 OverflowOccurred |= Val.ult(CharVal);
411 }
412 return OverflowOccurred;
413}
414
415// GetFloatValue - Poor man's floatvalue (FIXME).
416float NumericLiteralParser::GetFloatValue() {
417 char floatChars[256];
418 strncpy(floatChars, ThisTokBegin, ThisTokEnd-ThisTokBegin);
419 floatChars[ThisTokEnd-ThisTokBegin] = '\0';
Chris Lattner67798cb2007-07-17 15:27:33 +0000420 return (float)strtod(floatChars, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421}
422
423void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID,
424 const std::string &M) {
425 PP.Diag(Loc, DiagID, M);
426 hadError = true;
427}
428
429
430CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
431 SourceLocation Loc, Preprocessor &PP) {
432 // At this point we know that the character matches the regex "L?'.*'".
433 HadError = false;
434 Value = 0;
435
436 // Determine if this is a wide character.
437 IsWide = begin[0] == 'L';
438 if (IsWide) ++begin;
439
440 // Skip over the entry quote.
441 assert(begin[0] == '\'' && "Invalid token lexed");
442 ++begin;
443
444 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the
445 // size of "value".
446 assert(PP.getTargetInfo().getIntWidth(Loc) == 32 &&
447 "Assumes sizeof(int) == 4 for now");
448 // FIXME: This assumes that wchar_t is 32-bits for now.
449 assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 &&
450 "Assumes sizeof(wchar_t) == 4 for now");
451 // FIXME: This extensively assumes that 'char' is 8-bits.
452 assert(PP.getTargetInfo().getCharWidth(Loc) == 8 &&
453 "Assumes char is 8 bits");
454
455 bool isFirstChar = true;
456 bool isMultiChar = false;
457 while (begin[0] != '\'') {
458 unsigned ResultChar;
459 if (begin[0] != '\\') // If this is a normal character, consume it.
460 ResultChar = *begin++;
461 else // Otherwise, this is an escape character.
462 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
463
464 // If this is a multi-character constant (e.g. 'abc'), handle it. These are
465 // implementation defined (C99 6.4.4.4p10).
466 if (!isFirstChar) {
467 // If this is the second character being processed, do special handling.
468 if (!isMultiChar) {
469 isMultiChar = true;
470
471 // Warn about discarding the top bits for multi-char wide-character
472 // constants (L'abcd').
473 if (IsWide)
474 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
475 }
476
477 if (IsWide) {
478 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
479 Value = 0;
480 } else {
481 // Narrow character literals act as though their value is concatenated
482 // in this implementation.
483 if (((Value << 8) >> 8) != Value)
484 PP.Diag(Loc, diag::warn_char_constant_too_large);
485 Value <<= 8;
486 }
487 }
488
489 Value += ResultChar;
490 isFirstChar = false;
491 }
492
493 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
494 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
495 // character constants are not sign extended in the this implementation:
496 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
497 if (!IsWide && !isMultiChar && (Value & 128) &&
498 PP.getTargetInfo().isCharSigned(Loc))
499 Value = (signed char)Value;
500}
501
502
503/// string-literal: [C99 6.4.5]
504/// " [s-char-sequence] "
505/// L" [s-char-sequence] "
506/// s-char-sequence:
507/// s-char
508/// s-char-sequence s-char
509/// s-char:
510/// any source character except the double quote ",
511/// backslash \, or newline character
512/// escape-character
513/// universal-character-name
514/// escape-character: [C99 6.4.4.4]
515/// \ escape-code
516/// universal-character-name
517/// escape-code:
518/// character-escape-code
519/// octal-escape-code
520/// hex-escape-code
521/// character-escape-code: one of
522/// n t b r f v a
523/// \ ' " ?
524/// octal-escape-code:
525/// octal-digit
526/// octal-digit octal-digit
527/// octal-digit octal-digit octal-digit
528/// hex-escape-code:
529/// x hex-digit
530/// hex-escape-code hex-digit
531/// universal-character-name:
532/// \u hex-quad
533/// \U hex-quad hex-quad
534/// hex-quad:
535/// hex-digit hex-digit hex-digit hex-digit
536///
537StringLiteralParser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000538StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 Preprocessor &pp, TargetInfo &t)
540 : PP(pp), Target(t) {
541 // Scan all of the string portions, remember the max individual token length,
542 // computing a bound on the concatenated string length, and see whether any
543 // piece is a wide-string. If any of the string portions is a wide-string
544 // literal, the result is a wide-string literal [C99 6.4.5p4].
545 MaxTokenLength = StringToks[0].getLength();
546 SizeBound = StringToks[0].getLength()-2; // -2 for "".
547 AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
548
549 hadError = false;
550
551 // Implement Translation Phase #6: concatenation of string literals
552 /// (C99 5.1.1.2p1). The common case is only one string fragment.
553 for (unsigned i = 1; i != NumStringToks; ++i) {
554 // The string could be shorter than this if it needs cleaning, but this is a
555 // reasonable bound, which is all we need.
556 SizeBound += StringToks[i].getLength()-2; // -2 for "".
557
558 // Remember maximum string piece length.
559 if (StringToks[i].getLength() > MaxTokenLength)
560 MaxTokenLength = StringToks[i].getLength();
561
562 // Remember if we see any wide strings.
563 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
564 }
565
566
567 // Include space for the null terminator.
568 ++SizeBound;
569
570 // TODO: K&R warning: "traditional C rejects string constant concatenation"
571
572 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
573 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
574 wchar_tByteWidth = ~0U;
575 if (AnyWide) {
576 wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation());
577 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
578 wchar_tByteWidth /= 8;
579 }
580
581 // The output buffer size needs to be large enough to hold wide characters.
582 // This is a worst-case assumption which basically corresponds to L"" "long".
583 if (AnyWide)
584 SizeBound *= wchar_tByteWidth;
585
586 // Size the temporary buffer to hold the result string data.
587 ResultBuf.resize(SizeBound);
588
589 // Likewise, but for each string piece.
590 llvm::SmallString<512> TokenBuf;
591 TokenBuf.resize(MaxTokenLength);
592
593 // Loop over all the strings, getting their spelling, and expanding them to
594 // wide strings as appropriate.
595 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
596
597 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
598 const char *ThisTokBuf = &TokenBuf[0];
599 // Get the spelling of the token, which eliminates trigraphs, etc. We know
600 // that ThisTokBuf points to a buffer that is big enough for the whole token
601 // and 'spelled' tokens can only shrink.
602 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
603 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
604
605 // TODO: Input character set mapping support.
606
607 // Skip L marker for wide strings.
608 bool ThisIsWide = false;
609 if (ThisTokBuf[0] == 'L') {
610 ++ThisTokBuf;
611 ThisIsWide = true;
612 }
613
614 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
615 ++ThisTokBuf;
616
617 while (ThisTokBuf != ThisTokEnd) {
618 // Is this a span of non-escape characters?
619 if (ThisTokBuf[0] != '\\') {
620 const char *InStart = ThisTokBuf;
621 do {
622 ++ThisTokBuf;
623 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
624
625 // Copy the character span over.
626 unsigned Len = ThisTokBuf-InStart;
627 if (!AnyWide) {
628 memcpy(ResultPtr, InStart, Len);
629 ResultPtr += Len;
630 } else {
631 // Note: our internal rep of wide char tokens is always little-endian.
632 for (; Len; --Len, ++InStart) {
633 *ResultPtr++ = InStart[0];
634 // Add zeros at the end.
635 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
636 *ResultPtr++ = 0;
637 }
638 }
639 continue;
640 }
641
642 // Otherwise, this is an escape character. Process it.
643 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
644 StringToks[i].getLocation(),
645 ThisIsWide, PP);
646
647 // Note: our internal rep of wide char tokens is always little-endian.
648 *ResultPtr++ = ResultChar & 0xFF;
649
650 if (AnyWide) {
651 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
652 *ResultPtr++ = ResultChar >> i*8;
653 }
654 }
655 }
656
657 // Add zero terminator.
658 *ResultPtr = 0;
659 if (AnyWide) {
660 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
661 *ResultPtr++ = 0;
662 }
663}