• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Tehreer / SheenBidi / 15192346582

22 May 2025 04:13PM UTC coverage: 79.545% (+1.0%) from 78.546%
15192346582

push

github

mta452
[make] Set C++ standard version to 14

1400 of 1760 relevant lines covered (79.55%)

868008.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.74
/Source/SBAlgorithm.c
1
/*
2
 * Copyright (C) 2016-2025 Muhammad Tayyab Akram
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
#include <SBConfig.h>
18
#include <stddef.h>
19

20
#include "BidiTypeLookup.h"
21
#include "Object.h"
22
#include "SBBase.h"
23
#include "SBCodepoint.h"
24
#include "SBCodepointSequence.h"
25
#include "SBLog.h"
26
#include "SBParagraph.h"
27
#include "SBAlgorithm.h"
28

29
#define ALGORITHM  0
30
#define BIDI_TYPES 1
31
#define COUNT      2
32

33
static SBAlgorithmRef AllocateAlgorithm(SBUInteger stringLength)
861,949✔
34
{
35
    void *pointers[COUNT] = { NULL };
861,949✔
36
    SBUInteger sizes[COUNT];
861,949✔
37

38
    sizes[ALGORITHM]  = sizeof(SBAlgorithm);
861,949✔
39
    sizes[BIDI_TYPES] = sizeof(SBBidiType) * stringLength;
861,949✔
40

41
    if (ObjectCreate(sizes, COUNT, pointers)) {
861,949✔
42
        SBAlgorithmRef algorithm = pointers[ALGORITHM];
861,949✔
43
        SBBidiType *fixedTypes = pointers[BIDI_TYPES];
861,949✔
44

45
        algorithm->fixedTypes = fixedTypes;
861,949✔
46
    }
47

48
    return pointers[ALGORITHM];
861,949✔
49
}
50

51
#undef ALGORITHM
52
#undef BIDI_TYPES
53
#undef COUNT
54

55
static void DetermineBidiTypes(const SBCodepointSequence *sequence, SBBidiType *types)
861,949✔
56
{
57
    SBUInteger stringIndex = 0;
861,949✔
58
    SBUInteger firstIndex = 0;
861,949✔
59
    SBCodepoint codepoint;
60

61
    while ((codepoint = SBCodepointSequenceGetCodepointAt(sequence, &stringIndex)) != SBCodepointInvalid) {
4,629,076✔
62
        types[firstIndex] = LookupBidiType(codepoint);
3,767,127✔
63

64
        /* Subsequent code units get 'BN' type. */
65
        while (++firstIndex < stringIndex) {
3,767,127✔
66
            types[firstIndex] = SBBidiTypeBN;
×
67
        }
68
    }
69
}
861,949✔
70

71
static SBAlgorithmRef CreateAlgorithm(const SBCodepointSequence *codepointSequence)
861,949✔
72
{
73
    SBUInteger stringLength = codepointSequence->stringLength;
861,949✔
74
    SBAlgorithmRef algorithm;
75

76
    SB_LOG_BLOCK_OPENER("Algorithm Input");
77
    SB_LOG_STATEMENT("Codepoints", 1, SB_LOG_CODEPOINT_SEQUENCE(codepointSequence));
78
    SB_LOG_BLOCK_CLOSER();
79

80
    algorithm = AllocateAlgorithm(stringLength);
861,949✔
81

82
    if (algorithm) {
861,949✔
83
        algorithm->codepointSequence = *codepointSequence;
861,949✔
84
        algorithm->retainCount = 1;
861,949✔
85

86
        DetermineBidiTypes(codepointSequence, algorithm->fixedTypes);
861,949✔
87

88
        SB_LOG_BLOCK_OPENER("Determined Types");
89
        SB_LOG_STATEMENT("Types",  1, SB_LOG_BIDI_TYPES_ARRAY(algorithm->fixedTypes, stringLength));
90
        SB_LOG_BLOCK_CLOSER();
91

92
        SB_LOG_BREAKER();
93
    }
94

95
    return algorithm;
861,949✔
96
}
97

98
SBAlgorithmRef SBAlgorithmCreate(const SBCodepointSequence *codepointSequence)
861,949✔
99
{
100
    if (SBCodepointSequenceIsValid(codepointSequence)) {
861,949✔
101
        return CreateAlgorithm(codepointSequence);
861,949✔
102
    }
103

104
    return NULL;
×
105
}
106

107
const SBBidiType *SBAlgorithmGetBidiTypesPtr(SBAlgorithmRef algorithm)
×
108
{
109
    return algorithm->fixedTypes;
×
110
}
111

112
SB_INTERNAL SBUInteger SBAlgorithmGetSeparatorLength(SBAlgorithmRef algorithm, SBUInteger separatorIndex)
33,466✔
113
{
114
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
33,466✔
115
    SBUInteger stringIndex = separatorIndex;
33,466✔
116
    SBCodepoint codepoint;
117
    SBUInteger separatorLength;
118

119
    codepoint = SBCodepointSequenceGetCodepointAt(codepointSequence, &stringIndex);
33,466✔
120
    separatorLength = stringIndex - separatorIndex;
33,466✔
121

122
    if (codepoint == '\r') {
33,466✔
123
        /* Don't break in between 'CR' and 'LF'. */
124
        if (stringIndex < codepointSequence->stringLength) {
1✔
125
            codepoint = SBCodepointSequenceGetCodepointAt(codepointSequence, &stringIndex);
1✔
126

127
            if (codepoint == '\n') {
1✔
128
                separatorLength = stringIndex - separatorIndex;
1✔
129
            }
130
        }
131
    }
132

133
    return separatorLength;
33,466✔
134
}
135

136
void SBAlgorithmGetParagraphBoundary(SBAlgorithmRef algorithm,
×
137
    SBUInteger paragraphOffset, SBUInteger suggestedLength,
138
    SBUInteger *acutalLength, SBUInteger *separatorLength)
139
{
140
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
×
141
    SBBidiType *bidiTypes = algorithm->fixedTypes;
×
142
    SBUInteger limitIndex;
143
    SBUInteger startIndex;
144

145
    if (separatorLength) {
×
146
        *separatorLength = 0;
×
147
    }
148

149
    SBUIntegerNormalizeRange(codepointSequence->stringLength, &paragraphOffset, &suggestedLength);
×
150
    limitIndex = paragraphOffset + suggestedLength;
×
151

152
    for (startIndex = paragraphOffset; startIndex < limitIndex; startIndex++) {
×
153
        SBBidiType currentType = bidiTypes[startIndex];
×
154

155
        if (currentType == SBBidiTypeB) {
×
156
            SBUInteger codeUnitCount = SBAlgorithmGetSeparatorLength(algorithm, startIndex);
×
157

158
            startIndex += codeUnitCount;
×
159

160
            if (separatorLength) {
×
161
                *separatorLength = codeUnitCount;
×
162
            }
163
            break;
×
164
        }
165
    }
166

167
    if (acutalLength) {
×
168
        *acutalLength = startIndex - paragraphOffset;
×
169
    }
170
}
×
171

172
SBParagraphRef SBAlgorithmCreateParagraph(SBAlgorithmRef algorithm,
861,949✔
173
    SBUInteger paragraphOffset, SBUInteger suggestedLength, SBLevel baseLevel)
174
{
175
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
861,949✔
176
    SBUInteger stringLength = codepointSequence->stringLength;
861,949✔
177

178
    SBUIntegerNormalizeRange(stringLength, &paragraphOffset, &suggestedLength);
861,949✔
179

180
    if (suggestedLength > 0) {
861,949✔
181
        return SBParagraphCreate(algorithm, paragraphOffset, suggestedLength, baseLevel);
861,949✔
182
    }
183

184
    return NULL;
×
185
}
186

187
SBAlgorithmRef SBAlgorithmRetain(SBAlgorithmRef algorithm)
861,949✔
188
{
189
    if (algorithm) {
861,949✔
190
        algorithm->retainCount += 1;
861,949✔
191
    }
192

193
    return algorithm;
861,949✔
194
}
195

196
void SBAlgorithmRelease(SBAlgorithmRef algorithm)
1,723,898✔
197
{
198
    if (algorithm && --algorithm->retainCount == 0) {
1,723,898✔
199
        ObjectDispose(&algorithm->_object);
861,949✔
200
    }
201
}
1,723,898✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc