PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ stringbuffer_trim_trailing_zeroes()

int stringbuffer_trim_trailing_zeroes ( stringbuffer_t s)

Trims zeroes off the end of the last number in the stringbuffer.

The number has to be the very last thing in the buffer. Only the last number will be trimmed. Returns the number of characters trimmed.

eg: 1.22000 -> 1.22 1.0 -> 1 0.0 -> 0

Definition at line 281 of file stringbuffer.c.

282 {
283  char *ptr = s->str_end;
284  char *decimal_ptr = NULL;
285  int dist;
286 
287  if ( s->str_end - s->str_start < 2)
288  return 0;
289 
290  /* Roll backwards to find the decimal for this number */
291  while( ptr > s->str_start )
292  {
293  ptr--;
294  if ( *ptr == '.' )
295  {
296  decimal_ptr = ptr;
297  break;
298  }
299  if ( (*ptr >= '0') && (*ptr <= '9' ) )
300  continue;
301  else
302  break;
303  }
304 
305  /* No decimal? Nothing to trim! */
306  if ( ! decimal_ptr )
307  return 0;
308 
309  ptr = s->str_end;
310 
311  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
312  while( ptr >= decimal_ptr )
313  {
314  ptr--;
315  if ( *ptr == '0' )
316  continue;
317  else
318  break;
319  }
320 
321  /* Huh, we get anywhere. Must not have trimmed anything. */
322  if ( ptr == s->str_end )
323  return 0;
324 
325  /* If we stopped at the decimal, we want to null that out.
326  It we stopped on a numeral, we want to preserve that, so push the
327  pointer forward one space. */
328  if ( *ptr != '.' )
329  ptr++;
330 
331  /* Add null terminator re-set the end of the stringbuffer. */
332  *ptr = '\0';
333  dist = s->str_end - ptr;
334  s->str_end = ptr;
335  return dist;
336 }
char * s
Definition: cu_in_wkt.c:23

References s.