Skip to content

Commit edffc8d

Browse files
authored
Rework code handling ReadLine() (#2154)
1 parent 635219e commit edffc8d

File tree

5 files changed

+87
-74
lines changed

5 files changed

+87
-74
lines changed

src/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort__.cpp

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,25 @@ bool Library_sys_io_ser_native_System_IO_Ports_SerialPort::GetLineFromRxBuffer(
9393
{
9494
const char *newLine;
9595
uint32_t newLineLength;
96+
uint32_t newLineIndex;
9697
int32_t compareIndex;
9798
uint8_t *buffer;
9899
uint8_t *comparison;
99100
uint32_t matchCount = 0;
100-
uint32_t index;
101+
uint32_t index = 0;
101102

102103
// clear line
103104
line = NULL;
104105

105106
// check for anything in the buffer
106107
if (ringBuffer->Length() > 0)
107108
{
108-
// get new line from field
109+
// get "new line" from field
109110
newLine = serialDevice[FIELD___newLine].RecoverString();
110111
newLineLength = hal_strlen_s(newLine);
112+
111113
// need to subtract one because we are 0 indexed
112-
newLineLength--;
114+
newLineIndex = newLineLength - 1;
113115

114116
// better optimize to speed up search
115117
ringBuffer->OptimizeSequence();
@@ -118,61 +120,64 @@ bool Library_sys_io_ser_native_System_IO_Ports_SerialPort::GetLineFromRxBuffer(
118120
buffer = ringBuffer->Reader();
119121

120122
// search for latest new line char in the buffer
121-
for (index = 0; index < ringBuffer->Length(); index++)
123+
do
122124
{
123-
if (*buffer == newLine[newLineLength])
125+
if (*buffer == newLine[newLineIndex])
124126
{
125127
matchCount = 1;
126128

127-
if (newLineLength == 1)
129+
if (newLineIndex == 0)
128130
{
129131
// found and nothing else to compare
130132
break;
131133
}
132134
else
133135
{
134-
if (index >= newLineLength)
135-
{
136-
// get pointer to the index before the last one
137-
comparison = buffer;
138-
comparison--;
136+
// get pointer to the index before the last one
137+
comparison = buffer;
138+
comparison--;
139139

140-
// subtract one position, we've already check the last one
141-
compareIndex = newLineLength - 1;
140+
// subtract one position, we've already check the last one
141+
compareIndex = newLineIndex - 1;
142142

143-
do
143+
do
144+
{
145+
if (*comparison == newLine[compareIndex])
144146
{
145-
if (*comparison == newLine[compareIndex])
146-
{
147-
// found another match
148-
matchCount++;
149-
150-
// move comparer to position before
151-
comparison--;
152-
}
153-
} while (--compareIndex <= 0);
154-
}
147+
// found another match
148+
matchCount++;
149+
150+
//
151+
}
152+
153+
// move comparer to position before
154+
comparison--;
155+
156+
} while (--compareIndex > 0);
155157
}
156158
}
157159

158-
buffer--;
159-
}
160+
// move to next position in the buffer
161+
buffer++;
162+
index++;
163+
164+
} while (index < ringBuffer->Length() || matchCount < newLineLength);
160165

161166
// sequence found?
162167
if (matchCount == newLineLength)
163168
{
164-
// allocate memory for the string
165-
// index has the position of the last char of the "new line" string
166-
// need to add an extra position for the terminator
167-
line = (uint8_t *)platform_malloc(index + 1);
169+
// allocate memory for the string, including the new line char(s)
170+
// the new line char allow enough room in the buffer for for the terminator
171+
line = (uint8_t *)platform_malloc(index + newLineLength);
168172

169173
if (line != NULL)
170174
{
171-
// clear memory
172-
memset(line, 0, index + 1);
175+
// pop string AND new line from buffer
176+
ringBuffer->Pop(line, index + newLineLength);
173177

174-
// the returned string DOES NOT include the new line string
175-
memcpy(line, ringBuffer->Reader(), index - newLineLength);
178+
// the returned string DOES NOT include the new line char(s)
179+
// put a terminator at index 0 where the new line char(s) are, so the real string ends there
180+
line[index] = 0;
176181
}
177182
}
178183
}

targets/ChibiOS/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -526,18 +526,20 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::ReadLine___STRING(
526526
// got one!
527527
eventResult = false;
528528
}
529+
else
530+
{
531+
// get new line from field
532+
newLine = pThis[FIELD___newLine].RecoverString();
533+
newLineLength = hal_strlen_s(newLine);
534+
// need to subtract one because we are 0 indexed
535+
newLineLength--;
529536

530-
// get new line from field
531-
newLine = pThis[FIELD___newLine].RecoverString();
532-
newLineLength = hal_strlen_s(newLine);
533-
// need to subtract one because we are 0 indexed
534-
newLineLength--;
535-
536-
// set new line char as the last one in the string
537-
// only if this one is found it will have a chance of the others being there
538-
palUart->NewLineChar = newLine[newLineLength];
537+
// set new line char as the last one in the string
538+
// only if this one is found it will have a chance of the others being there
539+
palUart->NewLineChar = newLine[newLineLength];
539540

540-
stack.m_customState = 2;
541+
stack.m_customState = 2;
542+
}
541543
}
542544

543545
while (eventResult)

targets/ESP32/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,18 +556,20 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::ReadLine___STRING(
556556
// got one!
557557
eventResult = false;
558558
}
559+
else
560+
{
561+
// get new line from field
562+
newLine = pThis[FIELD___newLine].RecoverString();
563+
newLineLength = hal_strlen_s(newLine);
564+
// need to subtract one because we are 0 indexed
565+
newLineLength--;
559566

560-
// get new line from field
561-
newLine = pThis[FIELD___newLine].RecoverString();
562-
newLineLength = hal_strlen_s(newLine);
563-
// need to subtract one because we are 0 indexed
564-
newLineLength--;
565-
566-
// set new line char as the last one in the string
567-
// only if this one is found it will have a chance of the others being there
568-
palUart->NewLineChar = newLine[newLineLength];
567+
// set new line char as the last one in the string
568+
// only if this one is found it will have a chance of the others being there
569+
palUart->NewLineChar = newLine[newLineLength];
569570

570-
stack.m_customState = 2;
571+
stack.m_customState = 2;
572+
}
571573
}
572574

573575
while (eventResult)

targets/FreeRTOS/NXP/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,20 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::ReadLine___STRING(
437437
// got one!
438438
eventResult = false;
439439
}
440+
else
441+
{
442+
// get new line from field
443+
newLine = pThis[FIELD___newLine].RecoverString();
444+
newLineLength = hal_strlen_s(newLine);
445+
// need to subtract one because we are 0 indexed
446+
newLineLength--;
440447

441-
// get new line from field
442-
newLine = pThis[FIELD___newLine].RecoverString();
443-
newLineLength = hal_strlen_s(newLine);
444-
// need to subtract one because we are 0 indexed
445-
newLineLength--;
446-
447-
// set new line char as the last one in the string
448-
// only if this one is found it will have a chance of the others being there
449-
palUart->NewLineChar = newLine[newLineLength];
448+
// set new line char as the last one in the string
449+
// only if this one is found it will have a chance of the others being there
450+
palUart->NewLineChar = newLine[newLineLength];
450451

451-
stack.m_customState = 2;
452+
stack.m_customState = 2;
453+
}
452454
}
453455

454456
while (eventResult)

targets/TI_SimpleLink/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,18 +461,20 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::ReadLine___STRING(
461461
// got one!
462462
eventResult = false;
463463
}
464+
else
465+
{
466+
// get new line from field
467+
newLine = pThis[FIELD___newLine].RecoverString();
468+
newLineLength = hal_strlen_s(newLine);
469+
// need to subtract one because we are 0 indexed
470+
newLineLength--;
464471

465-
// get new line from field
466-
newLine = pThis[FIELD___newLine].RecoverString();
467-
newLineLength = hal_strlen_s(newLine);
468-
// need to subtract one because we are 0 indexed
469-
newLineLength--;
470-
471-
// set new line char as the last one in the string
472-
// only if this one is found it will have a chance of the others being there
473-
palUart->NewLineChar = newLine[newLineLength];
472+
// set new line char as the last one in the string
473+
// only if this one is found it will have a chance of the others being there
474+
palUart->NewLineChar = newLine[newLineLength];
474475

475-
stack.m_customState = 2;
476+
stack.m_customState = 2;
477+
}
476478
}
477479

478480
while (eventResult)

0 commit comments

Comments
 (0)