-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_view.c
More file actions
110 lines (94 loc) · 2.47 KB
/
Copy pathstring_view.c
File metadata and controls
110 lines (94 loc) · 2.47 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <limits.h>
#include "strings.h"
/**
* string_readc - returns current character and advances the string cursor.
* @s: the string.
*
* An error is returned if: the cursor position < 0 or cursor position > size
* or size <= 0.
* If the cursor is at the end, CHAR_MIN will be returned.
*
* Return: the current character, -1 on error.
*/
char string_readc(view_string * const s)
{
if (!s || s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
if (s->i == s->size)
return (CHAR_MIN);
return (s->s[s->i++]);
}
/**
* string_readp - returns previous character and retreats the string cursor.
* @s: the string.
*
* An error is returned if: the cursor position < 0 or cursor position >= size
* or size < 0.
* If there are no more characters before the cursor,
* CHAR_MIN will be returned.
*
* Return: the previous character, -1 on error.
*/
char string_readp(view_string * const s)
{
if (!s || s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
if (s->i == 0)
return (CHAR_MIN);
return (s->s[--s->i]);
}
/**
* string_peekc - returns character at the string cursor.
* @s: the string.
*
* An error is returned if: the cursor position < 0 or cursor position > size
* or size <= 0.
* If the cursor is at the end, CHAR_MIN will be returned.
*
* Return: the current character, -1 on error.
*/
char string_peekc(view_string const *const s)
{
if (!s || s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
if (s->i == s->size)
return (CHAR_MIN);
return (s->s[s->i]);
}
/**
* string_peekp - returns character before the string cursor.
* @s: the string.
*
* An error is returned if: the cursor position < 0 or cursor position >= size
* or size < 0.
* If there are no more characters before the cursor,
* CHAR_MIN will be returned.
*
* Return: the previous character, -1 on error.
*/
char string_peekp(view_string const *const s)
{
if (!s || s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
if (s->i == 0)
return (CHAR_MIN);
return (s->s[s->i - 1]);
}
/**
* string_peekn - returns character after the string cursor.
* @s: the string.
*
* An error is returned if: the cursor position < 0 or cursor position >= size
* or size < 0.
* If the cursor is at the end, CHAR_MIN will be returned.
*
* Return: the next character, -1 on error.
*/
char string_peekn(view_string const *const s)
{
if (!s || s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
if (s->i == s->size)
return (CHAR_MIN);
return (s->s[s->i + 1]);
}