Skip to content

Commit 712cdf6

Browse files
Rework Text stamps. Now we have String stamps that produce Pulp-based string in desired encoding, and StampText<T> template that joins these strings with space separator
1 parent f5096cc commit 712cdf6

File tree

3 files changed

+72
-44
lines changed

3 files changed

+72
-44
lines changed

blobstamper/stamp_text.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,38 @@
1919
#include "stamp_text.h"
2020

2121
std::string
22-
StampTextPulp::ExtractStr(std::shared_ptr<Blob> blob)
22+
StampStringLatin1::ExtractStr(std::shared_ptr<Blob> blob)
2323
{
2424
std::vector<char> data = blob->Chop(minSize(), maxSize())->AsByteVector();
2525

26-
std::vector<char>::iterator the_iterator;
26+
std::vector<char>::iterator iterator;
2727

28-
the_iterator = data.begin();
28+
iterator = data.begin();
2929
std::string res;
30-
while (the_iterator != data.end())
30+
while (iterator != data.end())
3131
{
32-
if (*the_iterator == '\0') { *the_iterator = ' '; }
33-
res.push_back(*the_iterator++);
32+
if (*iterator == '\0') { *iterator = ' '; }
33+
res.push_back(*iterator++);
3434
}
3535

3636
return res;
3737
}
3838

39-
std::string StampTextPulpWords::ExtractStr(std::shared_ptr<Blob> blob)
39+
std::string
40+
StampStringASCII::ExtractStr(std::shared_ptr<Blob> blob)
4041
{
41-
std::vector<std::string> data = ExtractStrVector(blob);
42-
std::string res = "";
43-
44-
for(std::string s : data)
45-
{
46-
if (!res.empty())
47-
{
48-
res+=" ";
49-
}
50-
res+= s;
51-
}
52-
return res;
53-
}
42+
std::vector<char> data = blob->Chop(minSize(), maxSize())->AsByteVector();
5443

55-
std::string StampTextDictWords::ExtractStr(std::shared_ptr<Blob> blob)
56-
{
57-
std::vector<std::string> data = ExtractStrVector(blob);
58-
std::string res = "";
44+
std::vector<char>::iterator iterator;
5945

60-
for(std::string s : data)
61-
{
62-
if (!res.empty())
46+
iterator = data.begin();
47+
std::string res;
48+
while (iterator != data.end())
6349
{
64-
res+=" ";
50+
*iterator &= 0x7F; // Remove first bit. This will made it ASCII (or more precisely 7bit encoding including character 0x01..0x1F)
51+
if (*iterator == '\0') { *iterator = ' '; }
52+
res.push_back(*iterator++);
6553
}
66-
res+= s;
67-
}
68-
return res;
54+
return res;
6955
}
7056

blobstamper/stamp_text.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,48 @@
2424

2525
#include "stamp_dict.h"
2626

27-
class StampTextPulp: public StampBaseStr
27+
class StampStringLatin1: public StampBaseStr
2828
{
2929
public:
3030
virtual int minSize() override { return 1; }
3131
virtual int maxSize() override { return -1; }
3232
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
3333
};
3434

35-
class StampTextPulpWords: public GalleyVectorStrStampBase<StampTextPulp>
35+
class StampStringASCII: public StampBaseStr
3636
{
3737
public:
38-
virtual std::string ExtractStr(std::shared_ptr<Blob> blob) override;
38+
virtual int minSize() override { return 1; }
39+
virtual int maxSize() override { return -1; }
40+
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
3941
};
4042

41-
class StampTextDictWords: public GalleyVectorStrStampBase<StampDictLCAlphaSmall>
43+
template<class T> class StampText: public GalleyVectorStrStampBase<T>
4244
{
4345
public:
4446
virtual std::string ExtractStr(std::shared_ptr<Blob> blob) override;
4547
};
4648

49+
50+
/* StampText create a string that consist of word that are separated by spaces.
51+
* Each word is created by T stamp, (T is template parameter, you can set any
52+
* stamp type there */
53+
54+
template<class T>
55+
std::string StampText<T>::ExtractStr(std::shared_ptr<Blob> blob)
56+
{
57+
std::vector<std::string> data = this->ExtractStrVector(blob);
58+
std::string res = "";
59+
60+
for(std::string s : data)
61+
{
62+
if (!res.empty())
63+
{
64+
res+=" ";
65+
}
66+
res+= s;
67+
}
68+
return res;
69+
}
70+
4771
#endif /* STAMP_TEXT_H */

t/150-stamp_text.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,48 @@ using namespace TAP;
3131
int
3232
main()
3333
{
34-
TEST_START(3);
34+
TEST_START(5);
3535
{ /* 1..1 */
3636
char data[] = "папа\0мама\0бабушка\0дедушка\0братик\0сестричка";
3737
std::shared_ptr<Blob> blob = std::make_shared<Blob>(data, (sizeof data)-1);
38-
StampTextPulp stamp;
39-
std::string s = stamp.ExtractStr(blob);
40-
is(s, "папа мама бабушка дедушка братик сестричка", "StampTextSimple");
38+
StampStringLatin1 stamp_str_l1;
39+
std::string s = stamp_str_l1.ExtractStr(blob);
40+
is(s, "папа мама бабушка дедушка братик сестричка", "StampStringLatin1");
4141
}
4242

4343
{ /* 2..2 */
44+
char data[] = "папа\0мама\0бабушка\0дедушка\0братик\0сестричка";
45+
std::shared_ptr<Blob> blob = std::make_shared<Blob>(data, (sizeof data)-1);
46+
StampStringASCII stamp_str_ascii;
47+
std::string s = stamp_str_ascii.ExtractStr(blob);
48+
is(s,
49+
"P?P0P?P0 P<P0P<P0 P1P0P1Q\x3Q\x8P:P0 P4P5P4Q\x3Q\x8P:P0 P1Q P0Q\x2P8P: Q\x1P5Q\x1Q\x2Q P8Q\x7P:P0",
50+
"StampTextASCII");
51+
}
52+
53+
{ /* 3..3 */
4454
char data[] = "dad\0mam\0granddad\0grandmam\0brother\0sister";
4555
std::shared_ptr<Blob> blob = std::make_shared<Blob>(data, (sizeof data)-1);
46-
StampTextPulpWords stamp;
56+
StampText<StampStringLatin1> stamp;
4757
std::string s = stamp.ExtractStr(blob);
48-
is(s, "d dad gra n dmam broth er siste", "GalleyTextSimple");
58+
is(s, "d dad gra n dmam broth er siste", "StampText<StampStringLatin1>");
4959
}
5060

51-
{ /* 3..3 */
61+
{ /* 4..4 */
5262
char data[] = "abcdef" "abcdef" "ABCDEF" "012345";
5363
std::shared_ptr<Blob> blob = std::make_shared<Blob>(data, (sizeof data)-1);
54-
StampTextDictWords stamp;
64+
StampText<StampDictLCAlphaSmall> stamp;
5565
std::string s = stamp.ExtractStr(blob);
56-
is(s, "gleam godfather graffiti greened grouping gunshots gleam godfather graffiti greened grouping gunshots dismally dissented divested doorstep dread drunks convertors corpulent counterparts cranking crippled crusades", "GalleyLCAlphaSmall");
66+
is(s, "gleam godfather graffiti greened grouping gunshots gleam godfather graffiti greened grouping gunshots dismally dissented divested doorstep dread drunks convertors corpulent counterparts cranking crippled crusades", "StampText<StampDictLCAlphaSmall>");
67+
}
5768

69+
{ /* 5..5 */
70+
char data[] = "Некоторый текст написанный русскими буквами в кодировке utf-8";
71+
std::shared_ptr<Blob> blob = std::make_shared<Blob>(data, (sizeof data)-1);
72+
StampText<StampStringASCII> stamp;
73+
std::string s = stamp.ExtractStr(blob);
74+
is(s, "P9 Q Q\x3Q \x1Q \x1P: P8 P<P 8 P 1Q\x3 P:P 2P0 P<P 8 P 2 P: P>P 4P8 Q P>P 2P :P5 ut f-8", "StampText<StampStringASCII>");
5875
}
76+
5977
TEST_END;
6078
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy