@@ -617,48 +617,51 @@ std::string RemoveRelativePathComponents(std::string path)
617
617
{
618
618
size_t pos = path.size ();
619
619
size_t dotdot_count = 0 ;
620
- while (pos != 0 ){
620
+ while (pos != 0 ) {
621
621
size_t component_with_delim_end = pos;
622
622
// skip a dir delimiter
623
- while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
623
+ while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
624
624
pos--;
625
625
// strip a path component
626
626
size_t component_end = pos;
627
- while (pos != 0 && !IsDirDelimiter (path[pos-1 ]))
627
+ while (pos != 0 && !IsDirDelimiter (path[pos-1 ]))
628
628
pos--;
629
629
size_t component_start = pos;
630
630
631
631
std::string component = path.substr (component_start,
632
632
component_end - component_start);
633
633
bool remove_this_component = false ;
634
- if (component == " ." && component_start != 0 ) {
634
+ if (component == " ." ) {
635
635
remove_this_component = true ;
636
- }
637
- else if (component == " .." ){
636
+ } else if (component == " .." ) {
638
637
remove_this_component = true ;
639
638
dotdot_count += 1 ;
640
- }
641
- else if (dotdot_count != 0 ){
639
+ } else if (dotdot_count != 0 ) {
642
640
remove_this_component = true ;
643
641
dotdot_count -= 1 ;
644
642
}
645
643
646
- if (remove_this_component){
647
- while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
644
+ if (remove_this_component) {
645
+ while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
648
646
pos--;
649
- path = path.substr (0 , pos) + DIR_DELIM +
650
- path.substr (component_with_delim_end,
651
- std::string::npos);
652
- pos++;
647
+ if (component_start == 0 ) {
648
+ // We need to remove the delemiter too
649
+ path = path.substr (component_with_delim_end, std::string::npos);
650
+ } else {
651
+ path = path.substr (0 , pos) + DIR_DELIM +
652
+ path.substr (component_with_delim_end, std::string::npos);
653
+ }
654
+ if (pos > 0 )
655
+ pos++;
653
656
}
654
657
}
655
658
656
- if (dotdot_count > 0 )
659
+ if (dotdot_count > 0 )
657
660
return " " ;
658
661
659
662
// remove trailing dir delimiters
660
663
pos = path.size ();
661
- while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
664
+ while (pos != 0 && IsDirDelimiter (path[pos-1 ]))
662
665
pos--;
663
666
return path.substr (0 , pos);
664
667
}
0 commit comments