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

Tehreer / SheenBidi / 15942379337

28 Jun 2025 08:39AM UTC coverage: 95.533% (-1.5%) from 97.027%
15942379337

push

github

mta452
[lib] Use default allocator for memory management

26 of 35 new or added lines in 7 files covered. (74.29%)

14 existing lines in 1 file now uncovered.

2203 of 2306 relevant lines covered (95.53%)

778648.62 hits per line

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

69.44
/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 <stddef.h>
18

19
#include <SheenBidi/SBConfig.h>
20

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

30
typedef SBAlgorithm *SBMutableAlgorithmRef;
31

32
#define ALGORITHM  0
33
#define BIDI_TYPES 1
34
#define COUNT      2
35

36
static SBMutableAlgorithmRef AllocateAlgorithm(SBUInteger stringLength)
861,949✔
37
{
38
    void *pointers[COUNT] = { NULL };
861,949✔
39
    SBUInteger sizes[COUNT] = { 0 };
861,949✔
40
    SBMutableAlgorithmRef algorithm;
41

42
    sizes[ALGORITHM]  = sizeof(SBAlgorithm);
861,949✔
43
    sizes[BIDI_TYPES] = sizeof(SBBidiType) * stringLength;
861,949✔
44

45
    algorithm = ObjectCreate(sizes, COUNT, pointers, NULL);
861,949✔
46

47
    if (algorithm) {
861,949✔
48
        algorithm->fixedTypes = pointers[BIDI_TYPES];
861,949✔
49
    }
50

51
    return algorithm;
861,949✔
52
}
53

54
#undef ALGORITHM
55
#undef BIDI_TYPES
56
#undef COUNT
57

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

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

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

74
static SBAlgorithmRef CreateAlgorithm(const SBCodepointSequence *codepointSequence)
861,949✔
75
{
76
    SBUInteger stringLength = codepointSequence->stringLength;
861,949✔
77
    SBMutableAlgorithmRef algorithm;
78

79
    SB_LOG_BLOCK_OPENER("Algorithm Input");
80
    SB_LOG_STATEMENT("Codepoints", 1, SB_LOG_CODEPOINT_SEQUENCE(codepointSequence));
81
    SB_LOG_BLOCK_CLOSER();
82

83
    algorithm = AllocateAlgorithm(stringLength);
861,949✔
84

85
    if (algorithm) {
861,949✔
86
        algorithm->codepointSequence = *codepointSequence;
861,949✔
87

88
        DetermineBidiTypes(codepointSequence, algorithm->fixedTypes);
861,949✔
89

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

94
        SB_LOG_BREAKER();
95
    }
96

97
    return algorithm;
861,949✔
98
}
99

100
SBAlgorithmRef SBAlgorithmCreate(const SBCodepointSequence *codepointSequence)
861,949✔
101
{
102
    SBAlgorithmRef algorithm = NULL;
861,949✔
103

104
    if (SBCodepointSequenceIsValid(codepointSequence)) {
861,949✔
105
        algorithm = CreateAlgorithm(codepointSequence);
861,949✔
106
    }
107

108
    return algorithm;
861,949✔
109
}
110

111
const SBBidiType *SBAlgorithmGetBidiTypesPtr(SBAlgorithmRef algorithm)
×
112
{
UNCOV
113
    return algorithm->fixedTypes;
×
114
}
115

116
SB_INTERNAL SBUInteger SBAlgorithmGetSeparatorLength(SBAlgorithmRef algorithm, SBUInteger separatorIndex)
33,466✔
117
{
118
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
33,466✔
119
    SBUInteger stringIndex = separatorIndex;
33,466✔
120
    SBCodepoint codepoint;
121
    SBUInteger separatorLength;
122

123
    codepoint = SBCodepointSequenceGetCodepointAt(codepointSequence, &stringIndex);
33,466✔
124
    separatorLength = stringIndex - separatorIndex;
33,466✔
125

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

131
            if (codepoint == '\n') {
1✔
132
                separatorLength = stringIndex - separatorIndex;
1✔
133
            }
134
        }
135
    }
136

137
    return separatorLength;
33,466✔
138
}
139

UNCOV
140
void SBAlgorithmGetParagraphBoundary(SBAlgorithmRef algorithm,
×
141
    SBUInteger paragraphOffset, SBUInteger suggestedLength,
142
    SBUInteger *acutalLength, SBUInteger *separatorLength)
143
{
UNCOV
144
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
×
UNCOV
145
    SBBidiType *bidiTypes = algorithm->fixedTypes;
×
146
    SBUInteger limitIndex;
147
    SBUInteger startIndex;
148

UNCOV
149
    if (separatorLength) {
×
UNCOV
150
        *separatorLength = 0;
×
151
    }
152

UNCOV
153
    SBUIntegerNormalizeRange(codepointSequence->stringLength, &paragraphOffset, &suggestedLength);
×
154
    limitIndex = paragraphOffset + suggestedLength;
×
155

UNCOV
156
    for (startIndex = paragraphOffset; startIndex < limitIndex; startIndex++) {
×
157
        SBBidiType currentType = bidiTypes[startIndex];
×
158

UNCOV
159
        if (currentType == SBBidiTypeB) {
×
160
            SBUInteger codeUnitCount = SBAlgorithmGetSeparatorLength(algorithm, startIndex);
×
161

162
            startIndex += codeUnitCount;
×
163

UNCOV
164
            if (separatorLength) {
×
165
                *separatorLength = codeUnitCount;
×
166
            }
UNCOV
167
            break;
×
168
        }
169
    }
170

UNCOV
171
    if (acutalLength) {
×
172
        *acutalLength = startIndex - paragraphOffset;
×
173
    }
UNCOV
174
}
×
175

176
SBParagraphRef SBAlgorithmCreateParagraph(SBAlgorithmRef algorithm,
861,949✔
177
    SBUInteger paragraphOffset, SBUInteger suggestedLength, SBLevel baseLevel)
178
{
179
    const SBCodepointSequence *codepointSequence = &algorithm->codepointSequence;
861,949✔
180
    SBUInteger stringLength = codepointSequence->stringLength;
861,949✔
181

182
    SBUIntegerNormalizeRange(stringLength, &paragraphOffset, &suggestedLength);
861,949✔
183

184
    if (suggestedLength > 0) {
861,949✔
185
        return SBParagraphCreate(algorithm, paragraphOffset, suggestedLength, baseLevel);
861,949✔
186
    }
187

UNCOV
188
    return NULL;
×
189
}
190

191
SBAlgorithmRef SBAlgorithmRetain(SBAlgorithmRef algorithm)
861,949✔
192
{
193
    return ObjectRetain((ObjectRef)algorithm);
861,949✔
194
}
195

196
void SBAlgorithmRelease(SBAlgorithmRef algorithm)
1,723,898✔
197
{
198
    ObjectRelease((ObjectRef)algorithm);
1,723,898✔
199
}
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