1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Daniel Bratell <bratell@lysator.liu.se>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either of the GNU General Public License Version 2 or later (the "GPL"),
27
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
42
static const char gIDFormat[] =
43
"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
45
static const char gIDFormat2[] =
46
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
50
* Multiplies the_int_var with 16 (0x10) and adds the value of the
51
* hexadecimal digit the_char. If it fails it returns PR_FALSE from
52
* the function it's used in.
55
#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
56
the_int_var = (the_int_var << 4) + the_char; \
57
if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
58
else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
59
else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
64
* Parses number_of_chars characters from the char_pointer pointer and
65
* puts the number in the dest_variable. The pointer is moved to point
66
* at the first character after the parsed ones. If it fails it returns
67
* PR_FALSE from the function the macro is used in.
70
#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
71
do { PRInt32 _i=number_of_chars; \
74
ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
81
* Parses a hyphen from the char_pointer string. If there is no hyphen there
82
* the function returns PR_FALSE from the function it's used in. The
83
* char_pointer is advanced one step.
86
#define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return PR_FALSE
89
* Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
90
* an nsID. It can also handle the old format without the { and }.
93
NS_COM PRBool nsID::Parse(const char *aIDStr)
95
/* Optimized for speed */
100
PRBool expectFormat1 = (aIDStr[0] == '{');
101
if(expectFormat1) aIDStr++;
103
PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
104
PARSE_HYPHEN(aIDStr);
105
PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
106
PARSE_HYPHEN(aIDStr);
107
PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
108
PARSE_HYPHEN(aIDStr);
111
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
112
PARSE_HYPHEN(aIDStr);
114
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
118
return expectFormat1 ? *aIDStr == '}' : PR_TRUE;
122
* Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
123
* format. The string is allocated with PR_Malloc and should be freed by
127
NS_COM char *nsID::ToString() const
129
char *res = (char*)PR_Malloc(39); // use PR_Malloc if this is to be freed with nsCRT::free
132
PR_snprintf(res, 39, gIDFormat,
133
m0, (PRUint32) m1, (PRUint32) m2,
134
(PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
135
(PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
136
(PRUint32) m3[6], (PRUint32) m3[7]);