Doxygen
Loading...
Searching...
No Matches
dstring.cpp File Reference
#include "dstring.h"
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
Include dependency graph for dstring.cpp:

Go to the source code of this file.

Functions

char toLowerChar (char c)
static bool ok_in_base (char c, int base)
char * dstrdup (const char *str)
void dstrfree (const char *str)
 Frees the memory allocated using dstrdup().
char * dstrncpy (char *dst, const char *src, size_t len)
int dstricmp (const char *s1, const char *s2)
int dstrnicmp (const char *s1, const char *s2, size_t len)
DString substitute (const DString &s, const DString &src, const DString &dst)
 substitute all occurrences of src in s by dst
DString substitute (const DString &s, const DString &src, const DString &dst, int skip_seq)
 substitute all occurrences of src in s by dst, but skip each consecutive sequence of src where the number consecutive src matches skip_seq; if skip_seq is negative, skip any number of consecutive src

Function Documentation

◆ dstrdup()

char * dstrdup ( const char * s)

Returns a copy of a string s. Note that memory is passed to the caller, use dstrfree() to release.

Definition at line 415 of file dstring.cpp.

416{
417 if ( !str ) return nullptr;
418 char *dst = new char[dstrlen(str)+1];
419 return strcpy( dst, str );
420}
uint32_t dstrlen(const char *str)
Returns the length of string str, or 0 if a null pointer is passed.
Definition dstring.h:44

References dstrdup(), and dstrlen().

Referenced by dstrdup().

◆ dstrfree()

void dstrfree ( const char * str)

Frees the memory allocated using dstrdup().

Definition at line 422 of file dstring.cpp.

423{
424 delete [](str);
425}

References dstrfree().

Referenced by dstrfree().

◆ dstricmp()

int dstricmp ( const char * s1,
const char * s2 )

Definition at line 435 of file dstring.cpp.

436{
437 if ( !s1 || !s2 )
438 {
439 return s1 == s2 ? 0 : static_cast<int>(s2 - s1);
440 }
441 int res = 0;
442 char c = 0;
443 for ( ; !(res = ((c=toLowerChar(*s1)) - toLowerChar(*s2))); s1++, s2++ )
444 {
445 if ( !c ) // strings are equal
446 {
447 break;
448 }
449 }
450 return res;
451}
char toLowerChar(char c)
Definition dstring.cpp:24

References dstricmp(), and toLowerChar().

Referenced by compareString(), dstricmp(), dstricmp(), dstricmp(), dstricmp(), dstricmp_sort(), VhdlDocGen::findAllArchitectures(), DocGroup::findExistingGroup(), FlowChart::findLabel(), VhdlDocGen::findVhdlClass(), readConfiguration(), stripFromPath(), transcodeCharacterBuffer(), transcodeCharacterStringToUTF8(), FlowChart::writeFlowLinks(), ClassDefImpl::writeIncludeFilesForSlice(), and VhdlDocGen::writeProcedureProto().

◆ dstrncpy()

char * dstrncpy ( char * dst,
const char * src,
size_t len )

Definition at line 427 of file dstring.cpp.

428{
429 if ( !src ) return nullptr;
430 strncpy( dst, src, len );
431 if ( len > 0 ) dst[len-1] = '\0';
432 return dst;
433}

References dstrncpy().

Referenced by dstrncpy().

◆ dstrnicmp()

int dstrnicmp ( const char * s1,
const char * s2,
size_t len )

Definition at line 453 of file dstring.cpp.

454{
455 if ( !s1 || !s2 )
456 {
457 return static_cast<int>(s2 - s1);
458 }
459 for ( ; len--; s1++, s2++ )
460 {
461 char c = toLowerChar(*s1);
462 int res = c-toLowerChar(*s2);
463 if ( res!=0 ) // strings are not equal
464 {
465 return res;
466 }
467 if ( c==0 ) // strings are equal
468 {
469 break;
470 }
471 }
472 return 0;
473}

References dstrnicmp(), and toLowerChar().

Referenced by DString::contains(), dstrnicmp(), dstrnicmp(), dstrnicmp(), dstrnicmp(), and DString::rfind_insensitive().

◆ ok_in_base()

bool ok_in_base ( char c,
int base )
static

Definition at line 152 of file dstring.cpp.

153{
154 if ( base <= 10 )
155 return c>='0' && c<='9' && (c-'0') < base;
156 else
157 return (c>='0' && c<='9') ||
158 (c >= 'a' && c < char('a'+base-10)) ||
159 (c >= 'A' && c < char('A'+base-10));
160}

References ok_in_base().

Referenced by ok_in_base(), DString::toLong(), DString::toUInt64(), and DString::toULong().

◆ substitute() [1/2]

DString substitute ( const DString & s,
const DString & src,
const DString & dst )

substitute all occurrences of src in s by dst

Definition at line 476 of file dstring.cpp.

477{
478 if (s.empty() || src.empty()) return s;
479 const char *q = nullptr, *p = nullptr;
480 size_t srcLen = src.length();
481 size_t dstLen = dst.length();
482 size_t resLen = 0;
483 if (srcLen!=dstLen)
484 {
485 int count = 0;
486 for (p = s.data(); (q=strstr(p,src.data()))!=nullptr; p=q+srcLen) count++;
487 resLen = s.length()+count*(dstLen-srcLen);
488 }
489 else // result has same size as s
490 {
491 resLen = s.length();
492 }
493 DString result(resLen, DString::ExplicitSize);
494 char *r = result.rawData();
495 for (p = s.data(); (q=strstr(p,src.data()))!=nullptr; p=q+srcLen)
496 {
497 int l = static_cast<int>(q-p);
498 memcpy(r,p,l);
499 r+=l;
500
501 if (dstLen>0) memcpy(r,dst.data(),dstLen);
502 r+=dstLen;
503 }
504 if (r)
505 {
506 dstrcpy(r,p);
507 }
508 //printf("substitute(%s,%s,%s)->%s\n",s,src,dst,result.data());
509 return result;
510}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:89
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:153
@ ExplicitSize
Definition dstring.h:136
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:162
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:156
char * dstrcpy(char *dst, const char *src)
Definition dstring.h:47

References DString::data(), dstrcpy(), DString::empty(), DString::ExplicitSize, DString::length(), DString::rawData(), and substitute().

Referenced by abbreviate(), addEnumValuesToEnums(), FlowChart::addFlowChart(), addFrom(), addGlobalFunction(), HtmlHelpIndex::addItem(), MemberDefImpl::addListReference(), addMemberFunction(), addMethodToClass(), addModule(), addPageToContext(), Markdown::Private::addStrEscapeUtf8Nbsp(), addVariable(), addVariableToClass(), buildListOfUsingDecls(), buildNamespaceList(), convertFileId2Var(), Portable::correctPath(), MemberDefImpl::displayDefinition(), findDirDocumentation(), DocParser::findDocsForMemberOrCompound(), findGroupScope(), findMember(), findUsingDeclarations(), findUsingDirectives(), fixSpaces(), format_warn(), generateClassOrGlobalLink(), generateClassOrGlobalLink(), generateJSLink(), generateJSNavTree(), RefList::generatePage(), VhdlDocGen::getClassName(), DocPara::handleStartCode(), HtmlGenerator::init(), linkifyText(), linkToText(), makeDisplayName(), makeDisplayName(), matchExcludedSymbols(), Markdown::process(), Markdown::Private::processLink(), resolveRef(), DotFilePatcher::run(), SearchIndex::setCurrentDoc(), stripIndentation(), substitute(), substitute(), substitute(), substitute(), substituteHtmlKeywords(), substituteLatexKeywords(), Portable::system(), ConfigImpl::takeStartComment(), ConfigImpl::takeStoreRepl(), ConfigImpl::takeUserComment(), unescapeCRef(), validatingParseDoc(), warn_line(), MemberDefImpl::warnIfUndocumented(), writeAlphabeticalClassList(), MemberDefImpl::writeDeclaration(), MemberList::writeDeclarations(), writeDefArgumentList(), writeDefaultLayoutFile(), MemberDefImpl::writeDocumentation(), writeJavaScriptSearchIndex(), HtmlGenerator::writeNavigationPath(), PlantumlManager::writePlantUMLSource(), HtmlGenerator::writeSearchData(), HtmlGenerator::writeSearchPage(), and VhdlDocGen::writeVHDLTypeDocumentation().

◆ substitute() [2/2]

DString substitute ( const DString & s,
const DString & src,
const DString & dst,
int skip_seq )

substitute all occurrences of src in s by dst, but skip each consecutive sequence of src where the number consecutive src matches skip_seq; if skip_seq is negative, skip any number of consecutive src

Definition at line 517 of file dstring.cpp.

518{
519 if (s.empty() || src.empty()) return s;
520 const char *p = nullptr, *q = nullptr;
521 size_t srcLen = src.length();
522 size_t dstLen = dst.length();
523 size_t resLen = 0;
524 if (srcLen!=dstLen)
525 {
526 int count = 0;
527 for (p=s.data(); (q=strstr(p,src.data()))!=nullptr; p=q+srcLen) count++;
528 resLen = s.length()+count*(dstLen-srcLen);
529 }
530 else // result has same size as s
531 {
532 resLen = s.length();
533 }
534 DString result(resLen, DString::ExplicitSize);
535 char *r = result.rawData();
536 for (p = s.data(); (q=strstr(p,src.data()))!=nullptr; p=q+srcLen)
537 {
538 // search a consecutive sequence of src
539 int seq = 0, skip = 0;
540 if (skip_seq)
541 {
542 for (const char *n=q+srcLen; dstrncmp(n,src.data(),srcLen)==0; seq=1+skip, n+=srcLen)
543 ++skip; // number of consecutive src after the current one
544
545 // verify the allowed number of consecutive src to skip
546 if (skip_seq > 0 && skip_seq != seq)
547 seq = skip = 0;
548 }
549
550 // skip a consecutive sequence of src when necessary
551 int l = static_cast<int>((q + seq * srcLen)-p);
552 memcpy(r,p,l);
553 r+=l;
554
555 if (skip)
556 {
557 // skip only the consecutive src found after the current one
558 q += skip * srcLen;
559 // the next loop will skip the current src, aka (p=q+srcLen)
560 continue;
561 }
562
563 if (dstLen>0) memcpy(r,dst.data(),dstLen);
564 r+=dstLen;
565 }
566 dstrcpy(r,p);
567 result.resize(strlen(result.data()));
568 //printf("substitute(%s,%s,%s)->%s\n",s,src,dst,result.data());
569 return result;
570}
int dstrncmp(const char *str1, const char *str2, size_t len)
Definition dstring.h:61

References DString::data(), dstrcpy(), dstrncmp(), DString::empty(), DString::ExplicitSize, DString::length(), DString::rawData(), DString::resize(), and substitute().

◆ toLowerChar()

char toLowerChar ( char c)
inline

Definition at line 24 of file dstring.cpp.

25{
26 return c>='A' && c<='Z' ? c|0x20 : c;
27}

References toLowerChar().

Referenced by DString::contains(), dstricmp(), dstrnicmp(), DString::rfind_insensitive(), and toLowerChar().