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

Tehreer / SheenBidi / 15618549131

12 Jun 2025 06:45PM UTC coverage: 97.027% (+0.4%) from 96.656%
15618549131

push

github

mta452
[lib] Mark SBLine as immutable

3 of 3 new or added lines in 1 file covered. (100.0%)

31 existing lines in 5 files now uncovered.

2121 of 2186 relevant lines covered (97.03%)

754359.57 hits per line

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

67.61
/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✔
UNCOV
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
    if (SBCodepointSequenceIsValid(codepointSequence)) {
861,949✔
103
        return CreateAlgorithm(codepointSequence);
861,949✔
104
    }
105

UNCOV
106
    return NULL;
×
107
}
108

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

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

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

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

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

135
    return separatorLength;
33,466✔
136
}
137

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

147
    if (separatorLength) {
×
UNCOV
148
        *separatorLength = 0;
×
149
    }
150

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

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

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

UNCOV
160
            startIndex += codeUnitCount;
×
161

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

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

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

180
    SBUIntegerNormalizeRange(stringLength, &paragraphOffset, &suggestedLength);
861,949✔
181

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

UNCOV
186
    return NULL;
×
187
}
188

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

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