-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_methods.c
More file actions
106 lines (91 loc) · 2.4 KB
/
string_methods.c
File metadata and controls
106 lines (91 loc) · 2.4 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
#include <limits.h>
#include "string_object.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.
* Once the cursor reaches the end of the string,
* CHAR_MIN will be returned on the next call to this function.
*
* Return: the current character, -1 on error.
*/
char string_readc(string *s)
{
if (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.
* Once the cursor reaches the start of the string,
* CHAR_MIN will be returned on the next call to this function.
*
* Return: the previous character, -1 on error.
*/
char string_readp(string *s)
{
if (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.
*
* Return: the current character, -1 on error.
*/
char string_peekc(string const *const s)
{
if (s->i < 0 || s->size <= 0 || s->i > s->size)
return (-1);
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 characters before the cursor, CHAR_MIN will be returned.
*
* Return: the previous character, -1 on error.
*/
char string_peekp(string const *const s)
{
if (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 there are no characters after the cursor, CHAR_MIN will be returned.
*
* Return: the next character, -1 on error.
*/
char string_peekn(string const *const s)
{
if (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]);
}