-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.c
77 lines (63 loc) · 1.62 KB
/
palindrome.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright (c) Omar Boukli-Hacene. All rights reserved.
Distributed under an MIT-style license that can be
found in the LICENSE file.
*/
/* SPDX-License-Identifier: MIT */
#include "forfun_c/palindrome.h"
#include <ctype.h>
#include <stddef.h>
/* Adhere to C89/C90, for the fun of it. */
#if __clang__
#pragma clang attribute push( \
__attribute__(( \
no_sanitize("pointer-overflow", "unsigned-integer-overflow") \
)), \
apply_to = function \
)
#endif /* __clang__ */
int forfun_is_palindrome(char const* str, size_t const length)
{
char const* const mid = str + (length / 2U);
/* Two insulated and harmless overflows when length is zero. */
char const* str_rhs = str + (length - 1U);
for (; str != mid; ++str)
{
if (*str != *str_rhs)
{
return 0;
}
--str_rhs;
}
return 1;
}
#if __clang__
#pragma clang attribute pop
#endif /* __clang__ */
#if __clang__
#pragma clang attribute push( \
__attribute__(( \
no_sanitize("pointer-overflow", "unsigned-integer-overflow") \
)), \
apply_to = function \
)
#endif /* __clang__ */
int forfun_is_palindrome_ci(char const* str, size_t const length)
{
char const* const mid = str + (length / 2U);
/* Two insulated and harmless overflows when length is zero. */
char const* str_rhs = str + (length - 1U);
for (; str != mid; ++str)
{
if (tolower((unsigned char)(*str))
!= tolower((unsigned char)(*str_rhs)))
{
return 0;
}
--str_rhs;
}
return 1;
}
#if __clang__
#pragma clang attribute pop
#endif /* __clang__ */