@@ -467,9 +467,8 @@ OFCondition DcmByteString::putString(const char *stringVal,
467467 return errorFlag;
468468}
469469
470-
471- OFCondition DcmByteString::putOFStringAtPos (const OFString& stringVal,
472- const unsigned long pos)
470+ OFCondition DcmByteString::putOFStringAtPosWithCharset (const OFString& stringVal, const unsigned long pos,
471+ const OFString& charSet)
473472{
474473 OFCondition result;
475474 // Get old value
@@ -505,46 +504,42 @@ OFCondition DcmByteString::putOFStringAtPos(const OFString& stringVal,
505504 // First value is set: Replace old value with new value
506505 else
507506 {
508- rightPos = str.find_first_of ( ' \\ ' , 0 );
509- str = str.replace (0 , rightPos, stringVal);
507+ rightPos = findNextValuePosition ( str.c_str (), str. length (), 0 , charSet );
508+ str = str.replace (0 , rightPos - 1 , stringVal);
510509 }
511510 return putOFStringArray (str);
512511 }
513512
514513 // 3rd case: New value should be inserted somewhere in the middle
515514 size_t leftPos = 0 ;
516515 size_t vmPos = 0 ;
516+ size_t strLen = str.length ();
517517 // First, find the correct position, and then insert / replace new value
518518 do
519519 {
520520 // Step from value to value by looking for delimiters.
521- // Special handling first search (start looking at position 0 instead of 1)
522- if (vmPos == 0 ) leftPos = str.find (' \\ ' , 0 );
523- else leftPos = str.find (' \\ ' , leftPos + 1 );
524- // leftPos = str.find('\\', leftPos == 0 ? 0 : leftPos +1);
521+ leftPos = findNextValuePosition (str.c_str (), strLen, leftPos, charSet);
525522 if (leftPos != OFString_npos)
526- {
527523 vmPos++;
528- }
529524 }
530525 while ( (leftPos != OFString_npos) && (vmPos != pos) );
531- rightPos = str.find_first_of ( ' \\ ' , leftPos+ 1 );
532- if (rightPos == OFString_npos) rightPos = str. length () ;
526+ rightPos = findNextValuePosition ( str.c_str (), strLen, leftPos, charSet );
527+ if (rightPos == OFString_npos) rightPos = strLen + 1 ;
533528
534529 // If we do not have an old value of size 1 or we have an empty value
535530 if (rightPos - leftPos == 1 )
536531 {
537532 // Empty value
538533 if (str.at (leftPos) == ' \\ ' )
539- str = str.insert (rightPos , stringVal);
534+ str = str.insert (leftPos , stringVal);
540535 // Old value (length 1)
541536 else
542537 str = str.replace (leftPos, 1 , stringVal);
543538 }
544539 // Otherwise replace existing old value (length > 1)
545540 else
546541 {
547- str = str.replace (leftPos+ 1 , rightPos - leftPos - 1 , stringVal);
542+ str = str.replace (leftPos, rightPos - leftPos - 1 , stringVal);
548543 }
549544 // Finally re-insert all values include new value
550545 result = putOFStringArray ( str );
@@ -553,6 +548,28 @@ OFCondition DcmByteString::putOFStringAtPos(const OFString& stringVal,
553548}
554549
555550
551+
552+ OFCondition DcmByteString::putOFStringAtPos (const OFString& stringVal,
553+ const unsigned long pos)
554+ {
555+ return putOFStringAtPosWithCharset (stringVal, pos, " " );
556+ }
557+
558+
559+ // ********************************
560+
561+
562+ size_t DcmByteString::findNextValuePosition (const char * str, size_t len, size_t start, const OFString& /* charSet*/ ) const
563+ {
564+ const char *p = str + start;
565+ for (size_t i = start; i < len; ++i)
566+ {
567+ if (*p++ == ' \\ ' )
568+ return i + 1 ;
569+ }
570+ return OFString_npos;
571+ }
572+
556573// ********************************
557574
558575
@@ -770,7 +787,7 @@ OFBool DcmByteString::containsExtendedCharacters(const OFBool checkAllStrings)
770787 OFBool result = OFFalse;
771788 /* only check if parameter is true since derived VRs are not affected
772789 by the attribute SpecificCharacterSet (0008,0005) */
773- if (checkAllStrings)
790+ if (checkAllStrings || isAffectedBySpecificCharacterSet () )
774791 {
775792 char *str = NULL ;
776793 Uint32 len = 0 ;
@@ -876,10 +893,10 @@ OFBool DcmByteString::containsExtendedCharacters(const char *stringVal,
876893{
877894 if (stringVal != NULL )
878895 {
879- for (size_t i = stringLen; i != 0 ; --i)
896+ for (size_t i = stringLen; i != 0 ; --i, ++stringVal )
880897 {
881- /* check for 8 bit characters */
882- if (OFstatic_cast ( unsigned char , *stringVal++) > 127 )
898+ /* check for 8 bit and Escape characters */
899+ if ((*stringVal & 0x80 ) != 0 || ( *stringVal == 0x1b ) )
883900 return OFTrue;
884901 }
885902 }
0 commit comments