Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsID.h"
8 : #include "nsMemory.h"
9 : #include "mozilla/Sprintf.h"
10 :
11 0 : void nsID::Clear()
12 : {
13 0 : m0 = 0;
14 0 : m1 = 0;
15 0 : m2 = 0;
16 0 : memset(m3, 0, sizeof(m3));
17 0 : }
18 :
19 : /**
20 : * Multiplies the_int_var with 16 (0x10) and adds the value of the
21 : * hexadecimal digit the_char. If it fails it returns false from
22 : * the function it's used in.
23 : */
24 :
25 : #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
26 : the_int_var = (the_int_var << 4) + the_char; \
27 : if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
28 : else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
29 : else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
30 : else return false
31 :
32 :
33 : /**
34 : * Parses number_of_chars characters from the char_pointer pointer and
35 : * puts the number in the dest_variable. The pointer is moved to point
36 : * at the first character after the parsed ones. If it fails it returns
37 : * false from the function the macro is used in.
38 : */
39 :
40 : #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
41 : do { int32_t _i=number_of_chars; \
42 : dest_variable = 0; \
43 : while(_i) { \
44 : ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
45 : char_pointer++; \
46 : _i--; \
47 : } } while(0)
48 :
49 :
50 : /**
51 : * Parses a hyphen from the char_pointer string. If there is no hyphen there
52 : * the function returns false from the function it's used in. The
53 : * char_pointer is advanced one step.
54 : */
55 :
56 : #define PARSE_HYPHEN(char_pointer) if (*(char_pointer++) != '-') return false
57 :
58 : /*
59 : * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
60 : * an nsID. It can also handle the old format without the { and }.
61 : */
62 :
63 : bool
64 939 : nsID::Parse(const char* aIDStr)
65 : {
66 : /* Optimized for speed */
67 939 : if (!aIDStr) {
68 0 : return false;
69 : }
70 :
71 939 : bool expectFormat1 = (aIDStr[0] == '{');
72 939 : if (expectFormat1) {
73 931 : ++aIDStr;
74 : }
75 :
76 939 : PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
77 939 : PARSE_HYPHEN(aIDStr);
78 939 : PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
79 939 : PARSE_HYPHEN(aIDStr);
80 939 : PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
81 939 : PARSE_HYPHEN(aIDStr);
82 : int i;
83 2817 : for (i = 0; i < 2; ++i) {
84 1878 : PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
85 : }
86 939 : PARSE_HYPHEN(aIDStr);
87 12207 : while (i < 8) {
88 5634 : PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
89 5634 : i++;
90 : }
91 :
92 939 : return expectFormat1 ? *aIDStr == '}' : true;
93 : }
94 :
95 : #ifndef XPCOM_GLUE_AVOID_NSPR
96 :
97 : static const char gIDFormat[] =
98 : "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
99 :
100 : /*
101 : * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
102 : * format. The string is allocated with NS_Alloc and should be freed by
103 : * the caller.
104 : */
105 :
106 : char*
107 114 : nsID::ToString() const
108 : {
109 114 : char* res = (char*)NS_Alloc(NSID_LENGTH);
110 :
111 114 : if (res) {
112 1140 : snprintf(res, NSID_LENGTH, gIDFormat,
113 342 : m0, (uint32_t)m1, (uint32_t)m2,
114 342 : (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
115 342 : (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
116 342 : (uint32_t)m3[6], (uint32_t)m3[7]);
117 : }
118 114 : return res;
119 : }
120 :
121 : void
122 146 : nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const
123 : {
124 1460 : SprintfLiteral(aDest, gIDFormat,
125 438 : m0, (uint32_t)m1, (uint32_t)m2,
126 438 : (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
127 438 : (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
128 438 : (uint32_t)m3[6], (uint32_t)m3[7]);
129 146 : }
130 :
131 : #endif // XPCOM_GLUE_AVOID_NSPR
|