Coverage Report

Created: 2026-07-30 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/Common/RemoveComments.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
  copyright notice, this list of conditions and the
15
  following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
  copyright notice, this list of conditions and the
19
  following disclaimer in the documentation and/or other
20
  materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
  contributors may be used to endorse or promote products
24
  derived from this software without specific prior
25
  written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/**
43
 *  @file  RemoveComments.cpp
44
 *  @brief Defines the CommentRemover utility class
45
 */
46
47
#include <assimp/RemoveComments.h>
48
#include <assimp/ParsingUtils.h>
49
50
namespace Assimp {
51
52
// ------------------------------------------------------------------------------------------------
53
// Remove line comments from a file
54
1.90k
void CommentRemover::RemoveLineComments(const char* szComment, char* szBuffer, char chReplacement /* = ' ' */) {
55
    // validate parameters
56
1.90k
    ai_assert(nullptr != szComment);
57
1.90k
    ai_assert(nullptr != szBuffer);
58
1.90k
    ai_assert(*szComment);
59
60
1.90k
    size_t len = strlen(szComment);
61
1.90k
    const size_t lenBuffer = strlen(szBuffer);
62
1.90k
    if (len > lenBuffer) {
63
3
        len = lenBuffer;
64
3
    }
65
66
892k
    for(size_t i = 0; i < lenBuffer; i++) {
67
        // skip over quotes
68
892k
        if (szBuffer[i] == '\"' || szBuffer[i] == '\'')
69
1.92M
            while (++i < lenBuffer && szBuffer[i] != '\"' && szBuffer[i] != '\'');
70
71
892k
        if(lenBuffer - i < len) {
72
1.88k
            break;
73
1.88k
        }
74
75
890k
        if (!strncmp(szBuffer + i,szComment,len)) {
76
19.2k
            while (i < lenBuffer && !IsLineEnd(szBuffer[i])) {
77
17.9k
                szBuffer[i++] = chReplacement;
78
17.9k
            }
79
1.32k
        }
80
890k
    }
81
1.90k
}
82
83
// ------------------------------------------------------------------------------------------------
84
// Remove multi-line comments from a file
85
void CommentRemover::RemoveMultiLineComments(const char* szCommentStart,
86
        const char* szCommentEnd,char* szBuffer,
87
0
        char chReplacement) {
88
    // validate parameters
89
0
    ai_assert(nullptr != szCommentStart);
90
0
    ai_assert(nullptr != szCommentEnd);
91
0
    ai_assert(nullptr != szBuffer);
92
0
    ai_assert(*szCommentStart);
93
0
    ai_assert(*szCommentEnd);
94
95
0
    const size_t len  = strlen(szCommentEnd);
96
0
    const size_t len2 = strlen(szCommentStart);
97
98
0
    while (*szBuffer)   {
99
        // skip over quotes
100
0
        if (*szBuffer == '\"' || *szBuffer == '\'') {
101
0
            while (*szBuffer++ && *szBuffer != '\"' && *szBuffer != '\'');
102
0
        }
103
104
0
        if (!strncmp(szBuffer,szCommentStart,len2))  {
105
0
            while (*szBuffer) {
106
0
                if (!::strncmp(szBuffer,szCommentEnd,len)) {
107
0
                    for (unsigned int i = 0; i < len;++i) {
108
0
                        *szBuffer++ = chReplacement;
109
0
                    }
110
111
0
                    break;
112
0
                }
113
0
                *szBuffer++ = chReplacement;
114
0
            }
115
0
            continue;
116
0
        }
117
0
        ++szBuffer;
118
0
    }
119
0
}
120
121
} // !! Assimp