------------------------------------------------------------------------------------------------ Encodes a string into a valid XML ID using the xsd:ID schema qualifications.
| 119 | // ------------------------------------------------------------------------------------------------ |
| 120 | // Encodes a string into a valid XML ID using the xsd:ID schema qualifications. |
| 121 | static const std::string XMLIDEncode(const std::string &name) { |
| 122 | const char XML_ID_CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-."; |
| 123 | const unsigned int XML_ID_CHARS_COUNT = sizeof(XML_ID_CHARS) / sizeof(char) - 1; |
| 124 | |
| 125 | if (name.length() == 0) { |
| 126 | return name; |
| 127 | } |
| 128 | |
| 129 | std::stringstream idEncoded; |
| 130 | |
| 131 | // xsd:ID must start with letter or underscore |
| 132 | if (!((name[0] >= 'A' && name[0] <= 'z') || name[0] == '_')) { |
| 133 | idEncoded << '_'; |
| 134 | } |
| 135 | |
| 136 | for (std::string::const_iterator it = name.begin(); it != name.end(); ++it) { |
| 137 | // xsd:ID can only contain letters, digits, underscores, hyphens and periods |
| 138 | if (strchr(XML_ID_CHARS, *it) != nullptr) { |
| 139 | idEncoded << *it; |
| 140 | } else { |
| 141 | // Select placeholder character based on invalid character to reduce ID collisions |
| 142 | idEncoded << XML_ID_CHARS[(*it) % XML_ID_CHARS_COUNT]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return idEncoded.str(); |
| 147 | } |
| 148 | |
| 149 | // ------------------------------------------------------------------------------------------------ |
| 150 | // Helper functions to create unique ids |
no test coverage detected