Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6de23d7

Browse files
committed
#23 fixing string comparing for unequal lengths
1 parent39b5a5d commit6de23d7

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

‎.clang-format‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
---
22
BasedOnStyle:LLVM
33
AlignAfterOpenBracket:DontAlign
4-
BinPackArguments:'true'
5-
BinPackParameters:'true'
4+
BinPackArguments:true
5+
BinPackParameters:true
66
BreakBeforeBraces:Mozilla
7-
IndentWidth:'2'
8-
ContinuationIndentWidth:'2'
9-
AllowAllParametersOfDeclarationOnNextLine:'false'
10-
AllowShortBlocksOnASingleLine:'false'
11-
AllowShortCaseLabelsOnASingleLine:'false'
12-
AllowShortFunctionsOnASingleLine:'false'
7+
IndentWidth:2
8+
ContinuationIndentWidth:2
9+
AllowAllParametersOfDeclarationOnNextLine:false
10+
AllowAllArgumentsOnNextLine:false
11+
AllowShortBlocksOnASingleLine:'Never'
12+
AllowShortCaseLabelsOnASingleLine:false
13+
AllowShortFunctionsOnASingleLine:'None'
1314
AllowShortIfStatementsOnASingleLine:'false'
14-
AllowShortLoopsOnASingleLine:'false'
15+
AllowShortLoopsOnASingleLine:false
1516
PenaltyBreakAssignment:10000
16-
PenaltyBreakBeforeFirstCallParameter:100000
17-
18-
BreakBeforeBraces:Custom
19-
BraceWrapping:
20-
AfterControlStatement:false
21-
AfterEnum:true
22-
AfterStruct:true
23-
AfterUnion:true
24-
AfterExternBlock:true
25-
AfterFunction:true
26-
IndentBraces:false
27-
BeforeElse:false
28-
SplitEmptyFunction:true
29-
SplitEmptyRecord:true
17+
PenaltyBreakBeforeFirstCallParameter:100000

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ if(ENABLE_TESTS)
178178
create_test(DEFAULT relative long_base)
179179
create_test(DEFAULT relative long_target)
180180
create_test(DEFAULT relativeequal)
181+
create_test(DEFAULT relative same_base)
181182
create_test(DEFAULT relative base_skipped_end)
182183
create_test(DEFAULT relative target_skipped_end)
183184
create_test(DEFAULT relative base_div_skipped_end)

‎src/cwalk.c‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,35 @@ static void cwk_path_terminate_output(char *buffer, size_t buffer_size,
122122
}
123123

124124
staticboolcwk_path_is_string_equal(constchar*first,constchar*second,
125-
size_tn)
125+
size_tfirst_size,size_tsecond_size)
126126
{
127+
// THe two strings are not equal if the sizes are not equal.
128+
if (first_size!=second_size) {
129+
return false;
130+
}
131+
127132
// If the path style is UNIX, we will compare case sensitively. This can be
128133
// done easily using strncmp.
129134
if (path_style==CWK_STYLE_UNIX) {
130-
returnstrncmp(first,second,n)==0;
135+
returnstrncmp(first,second,first_size)==0;
131136
}
132137

133138
// However, if this is windows we will have to compare case insensitively.
134139
// Since there is no standard method to do that we will have to do it on our
135140
// own.
136-
while (*first&&*second&&n>0) {
141+
while (*first&&*second&&first_size>0) {
137142
// We can consider the string to be not equal if the two lowercase
138143
// characters are not equal.
139144
if (tolower(*first++)!=tolower(*second++)) {
140145
return false;
141146
}
142147

143-
--n;
148+
--first_size;
144149
}
145150

146-
//We can consider thestringto be equalif we either reached n == 0 or both
147-
//cursors point to a null character.
148-
returnn==0|| (*first=='\0'&&*second=='\0');
151+
//Thestringmust be equalsince they both have the same length and all the
152+
//characters are the same.
153+
returntrue;
149154
}
150155

151156
staticconstchar*cwk_path_find_next_stop(constchar*c)
@@ -437,9 +442,7 @@ cwk_path_segment_will_be_removed(const struct cwk_segment_joined *sj,
437442
// First we check whether this is a CWK_CURRENT or CWK_BACK segment, since
438443
// those will always be dropped.
439444
type=cwk_path_get_segment_type(&sj->segment);
440-
if (type==CWK_CURRENT) {
441-
return true;
442-
}elseif (type==CWK_BACK&&absolute) {
445+
if (type==CWK_CURRENT|| (type==CWK_BACK&&absolute)) {
443446
return true;
444447
}elseif (type==CWK_BACK) {
445448
returncwk_path_segment_back_will_be_removed(&sjc);
@@ -701,7 +704,7 @@ static void cwk_path_skip_segments_until_diverge(struct cwk_segment_joined *bsj,
701704
// Compare the content of both segments. We are done if they are not equal,
702705
// since they diverge.
703706
if (!cwk_path_is_string_equal(bsj->segment.begin,osj->segment.begin,
704-
bsj->segment.size)) {
707+
bsj->segment.size,osj->segment.size)) {
705708
break;
706709
}
707710

@@ -729,7 +732,8 @@ size_t cwk_path_get_relative(const char *base_directory, const char *path,
729732
cwk_path_get_root(base_directory,&base_root_length);
730733
cwk_path_get_root(path,&path_root_length);
731734
if (base_root_length!=path_root_length||
732-
!cwk_path_is_string_equal(base_directory,path,base_root_length)) {
735+
!cwk_path_is_string_equal(base_directory,path,base_root_length,
736+
path_root_length)) {
733737
cwk_path_terminate_output(buffer,buffer_size,pos);
734738
returnpos;
735739
}
@@ -1126,7 +1130,8 @@ size_t cwk_path_get_intersection(const char *path_base, const char *path_other)
11261130
// absolute.
11271131
cwk_path_get_root(path_base,&base_root_length);
11281132
cwk_path_get_root(path_other,&other_root_length);
1129-
if (!cwk_path_is_string_equal(path_base,path_other,base_root_length)) {
1133+
if (!cwk_path_is_string_equal(path_base,path_other,base_root_length,
1134+
other_root_length)) {
11301135
return0;
11311136
}
11321137

@@ -1165,7 +1170,7 @@ size_t cwk_path_get_intersection(const char *path_base, const char *path_other)
11651170
}
11661171

11671172
if (!cwk_path_is_string_equal(base.segment.begin,other.segment.begin,
1168-
base.segment.size)) {
1173+
base.segment.size,other.segment.size)) {
11691174
// So the content of those two segments are not equal. We will return the
11701175
// size up to the beginning.
11711176
return (size_t)(end-path_base);

‎test/relative_test.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,26 @@ int relative_equal()
235235
returnEXIT_SUCCESS;
236236
}
237237

238+
intrelative_same_base()
239+
{
240+
charresult[FILENAME_MAX];
241+
size_tlength;
242+
243+
cwk_path_set_style(CWK_STYLE_UNIX);
244+
245+
length=cwk_path_get_relative("/dir/dir","/dir/dir3/file",result,
246+
sizeof(result));
247+
if (length!=12) {
248+
returnEXIT_FAILURE;
249+
}
250+
251+
if (strcmp(result,"../dir3/file")!=0) {
252+
returnEXIT_FAILURE;
253+
}
254+
255+
returnEXIT_SUCCESS;
256+
}
257+
238258
intrelative_long_target()
239259
{
240260
charresult[FILENAME_MAX];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp