Skip to content

Commit 5579004

Browse files
committed
add choice for ugly safenames
1 parent 38b6330 commit 5579004

File tree

6 files changed

+175
-57
lines changed

6 files changed

+175
-57
lines changed

source/lib/api/Support/Radix.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
#include "api/Support/Radix.hpp"
13+
#include "api/Support/Debug.hpp"
1314
#include <algorithm>
1415
#include <vector>
1516

@@ -242,5 +243,84 @@ toBaseFN(
242243
return llvm::StringRef(dest.data(), n);
243244
}
244245

246+
llvm::StringRef
247+
toBase32(
248+
std::string& dest,
249+
llvm::StringRef src)
250+
{
251+
#if 0
252+
std::vector<std::uint8_t> v;
253+
v.reserve(2 * ((binaryString.size() + 14) / 15));
254+
while(binaryString.size() >= 15)
255+
{
256+
std::uint16_t u =
257+
(binaryString[ 0]-'0') * 0x0001 +
258+
(binaryString[ 1]-'0') * 0x0002 +
259+
(binaryString[ 2]-'0') * 0x0004 +
260+
(binaryString[ 3]-'0') * 0x0008 +
261+
(binaryString[ 4]-'0') * 0x0010 +
262+
(binaryString[ 5]-'0') * 0x0020 +
263+
(binaryString[ 6]-'0') * 0x0040 +
264+
(binaryString[ 7]-'0') * 0x0080 +
265+
(binaryString[ 8]-'0') * 0x0100 +
266+
(binaryString[ 9]-'0') * 0x0200 +
267+
(binaryString[10]-'0') * 0x0400 +
268+
(binaryString[11]-'0') * 0x0800 +
269+
(binaryString[12]-'0') * 0x1000 +
270+
(binaryString[13]-'0') * 0x2000 +
271+
(binaryString[14]-'0') * 0x4000;
272+
v.push_back( u & 0x00ff);
273+
v.push_back((u & 0xff00) >> 8);
274+
binaryString = binaryString.substr(15);
275+
}
276+
if(! binaryString.empty())
277+
{
278+
char temp[15] = {
279+
'0', '0', '0', '0', '0', '0', '0',
280+
'0', '0', '0', '0', '0', '0', '0', '0' };
281+
std::memcpy(temp, binaryString.data(), binaryString.size());
282+
std::uint16_t u =
283+
(temp[ 0]-'0') * 0x0001 +
284+
(temp[ 1]-'0') * 0x0002 +
285+
(temp[ 2]-'0') * 0x0004 +
286+
(temp[ 3]-'0') * 0x0008 +
287+
(temp[ 4]-'0') * 0x0010 +
288+
(temp[ 5]-'0') * 0x0020 +
289+
(temp[ 6]-'0') * 0x0040 +
290+
(temp[ 7]-'0') * 0x0080 +
291+
(temp[ 8]-'0') * 0x0100 +
292+
(temp[ 9]-'0') * 0x0200 +
293+
(temp[10]-'0') * 0x0400 +
294+
(temp[11]-'0') * 0x0800 +
295+
(temp[12]-'0') * 0x1000 +
296+
(temp[13]-'0') * 0x2000 +
297+
(temp[14]-'0') * 0x4000;
298+
v.push_back( u & 0x00ff);
299+
v.push_back((u & 0xff00) >> 8);
300+
}
301+
dest.clear();
302+
Assert((v.size() & 1) == 0);
303+
dest.reserve(3 * (v.size() / 2));
304+
auto it = v.data();
305+
auto const end = it + v.size();
306+
while(it != end)
307+
{
308+
static constexpr char alphabet[33] =
309+
"012345abcdefghijklmnopqrstuvwxyz";
310+
std::uint16_t t = 256 * it[1] + it[0];
311+
dest.push_back(alphabet[(t & 0x001f)]);
312+
dest.push_back(alphabet[(t & 0x03e0) >> 5]);
313+
dest.push_back(alphabet[(t & 0x7c00) >> 10]);
314+
it += 2;
315+
}
316+
while(! dest.empty())
317+
if(dest.back() == '0')
318+
dest.pop_back();
319+
else
320+
break;
321+
#endif
322+
return dest;
323+
}
324+
245325
} // mrdox
246326
} // clang

source/lib/api/Support/Radix.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ toBaseFN(
3232
llvm::SmallVectorImpl<char>& dest,
3333
llvm::ArrayRef<uint8_t> src);
3434

35+
llvm::StringRef
36+
toBase32(
37+
std::string& dest,
38+
llvm::StringRef src);
39+
3540
} // mrdox
3641
} // clang
3742

source/lib/api/Support/SafeNames.cpp

+51-37
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "api/Support/Debug.hpp"
1212
#include "api/Support/Operator.hpp"
13+
#include "api/Support/Radix.hpp"
1314
#include "api/Support/SafeNames.hpp"
1415
#include "api/Support/Validate.hpp"
1516
#include <mrdox/Corpus.hpp>
@@ -21,6 +22,8 @@
2122
namespace clang {
2223
namespace mrdox {
2324

25+
namespace {
26+
2427
/*
2528
Unsafe names:
2629
@@ -29,15 +32,15 @@ namespace mrdox {
2932
function templates
3033
class templates
3134
*/
32-
class SafeNames::
33-
Builder : public Corpus::Visitor
35+
class PrettyBuilder : public Corpus::Visitor
3436
{
3537
llvm::raw_ostream* os_ = nullptr;
3638
std::string prefix_;
3739
std::string temp_;
3840

3941
public:
40-
Builder(
42+
explicit
43+
PrettyBuilder(
4144
Corpus const& corpus)
4245
: corpus_(corpus)
4346
{
@@ -51,7 +54,7 @@ class SafeNames::
5154
#endif
5255
}
5356

54-
Builder(
57+
PrettyBuilder(
5558
llvm::raw_ostream& os,
5659
Corpus const& corpus)
5760
:
@@ -268,61 +271,72 @@ class SafeNames::
268271

269272
//------------------------------------------------
270273

274+
// Always works but isn't the prettiest...
275+
class UglyBuilder : public Corpus::Visitor
276+
{
277+
Corpus const& corpus_;
278+
279+
public:
280+
llvm::StringMap<std::string> map;
281+
282+
explicit
283+
UglyBuilder(
284+
Corpus const& corpus)
285+
: corpus_(corpus)
286+
{
287+
llvm::SmallString<64> temp;
288+
for(Info const* I : corpus_.index())
289+
map.try_emplace(
290+
llvm::toStringRef(I->id),
291+
llvm::toHex(llvm::toStringRef(I->id), true));
292+
}
293+
};
294+
295+
} // (anon)
296+
297+
//------------------------------------------------
298+
271299
SafeNames::
272300
SafeNames(
301+
llvm::raw_ostream& os,
273302
Corpus const& corpus)
303+
: corpus_(corpus)
304+
//, map_(PrettyBuilder(corpus).map)
305+
, map_(UglyBuilder(corpus).map)
274306
{
275-
Builder b(corpus);
276-
map_ = std::move(b.map);
277307
}
278308

279309
SafeNames::
280310
SafeNames(
281-
llvm::raw_ostream& os,
282311
Corpus const& corpus)
283-
{
284-
Builder b(os, corpus);
285-
map_ = std::move(b.map);
312+
: corpus_(corpus)
313+
//, map_(PrettyBuilder(corpus).map)
314+
, map_(UglyBuilder(corpus).map)
315+
{
286316
}
287317

288318
llvm::StringRef
289319
SafeNames::
290320
get(
291-
SymbolID const &id) const
321+
SymbolID const &id) const noexcept
292322
{
293323
auto const it = map_.find(llvm::toStringRef(id));
294324
Assert(it != map_.end());
295325
return it->getValue();
296326
}
297327

298-
llvm::StringRef
299-
SafeNames::
300-
get(
301-
SymbolID const& id,
302-
char sep,
303-
std::string& dest) const
304-
{
305-
auto it = map_.find(llvm::toStringRef(id));
306-
Assert(it != map_.end());
307-
if(sep == '.')
308-
return it->getValue();
309-
dest = it->getValue();
310-
std::replace(dest.begin(), dest.end(), '.', sep);
311-
return dest;
312-
}
313-
314-
llvm::StringRef
328+
std::vector<llvm::StringRef>&
315329
SafeNames::
316-
getOverload(
317-
Info const& P,
318-
llvm::StringRef name,
319-
char sep,
320-
std::string& dest) const
330+
getPath(
331+
std::vector<llvm::StringRef>& dest,
332+
SymbolID id) const
321333
{
322-
dest = get(P.id);
323-
dest.push_back(sep);
324-
dest.push_back('0');
325-
dest.append(name);
334+
auto const& Parents = corpus_.get<Info>(id).Namespace;
335+
dest.clear();
336+
dest.reserve(1 + Parents.size());
337+
dest.push_back(get(id));
338+
for(auto const& ref : llvm::reverse(Parents))
339+
dest.push_back(get(ref.id));
326340
return dest;
327341
}
328342

source/lib/api/Support/SafeNames.hpp

+31-18
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,49 @@
2121
namespace clang {
2222
namespace mrdox {
2323

24+
/** A table mapping symbolIDs to safe names.
25+
26+
A safe name for a symbol is case-insensitive
27+
unique and only contains characters which are
28+
valid for both filenames and URL paths. For
29+
filenames this includes only the subset of
30+
characters valid for Windows, OSX, and Linux
31+
type filesystems.
32+
*/
2433
class SafeNames
2534
{
35+
Corpus const& corpus_;
2636
llvm::StringMap<std::string> map_;
2737

28-
class Builder;
29-
30-
public:
31-
explicit
3238
SafeNames(
39+
llvm::raw_ostream& os,
3340
Corpus const&);
3441

42+
public:
43+
/** Constructor.
44+
45+
Upon construction, the entire table of
46+
safe names is built from the corpus.
47+
*/
48+
explicit
3549
SafeNames(
36-
llvm::raw_ostream& os,
37-
Corpus const&);
50+
Corpus const& corpus);
3851

3952
llvm::StringRef
40-
get(SymbolID const& id) const;
53+
get(SymbolID const& id) const noexcept;
4154

42-
llvm::StringRef
43-
get(
44-
SymbolID const& id,
45-
char sep,
46-
std::string& dest) const;
55+
std::vector<llvm::StringRef>&
56+
getPath(
57+
std::vector<llvm::StringRef>& dest,
58+
SymbolID id) const;
4759

48-
llvm::StringRef
49-
getOverload(
50-
Info const &P,
51-
llvm::StringRef name,
52-
char sep,
53-
std::string& dest) const;
60+
std::vector<llvm::StringRef>
61+
getPath(SymbolID const& id) const
62+
{
63+
std::vector<llvm::StringRef> v;
64+
getPath(v, id);
65+
return v;
66+
}
5467
};
5568

5669
} // mrdox

source/lib/api/_adoc/AdocSinglePageWriter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ AdocSinglePageWriter(
2222
Corpus const& corpus,
2323
Reporter& R) noexcept
2424
: AdocWriter(os, names_, corpus, R)
25-
, names_(os, corpus)
25+
, names_(corpus)
2626
{
2727
}
2828

source/lib/api/_adoc/AdocWriter.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ linkFor(
449449
llvm::raw_string_ostream os(temp);
450450
std::string s;
451451
os << "xref:#" <<
452+
#if 0
452453
names_.getOverload(P, I.Name, '-', s) <<
454+
#endif
453455
"[" << I.Name << "]";
454456
return temp;
455457
}
@@ -811,7 +813,11 @@ beginSection(
811813
std::string temp;
812814
os_ <<
813815
"\n" <<
814-
"[\"#" << names_.getOverload(P, I.Name, '-', temp) << "\"]\n" <<
816+
"[\"#" <<
817+
#if 0
818+
names_.getOverload(P, I.Name, '-', temp) <<
819+
#endif
820+
"\"]\n" <<
815821
sect_.markup << ' ' << I.Name << "\n";
816822
}
817823
void

0 commit comments

Comments
 (0)