PostGIS  3.4.0dev-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 298 of file stringbuffer.c.

299 {
300  char *ptr = s->str_end;
301  char *decimal_ptr = NULL;
302  int dist;
303 
304  if ( s->str_end - s->str_start < 2)
305  return 0;
306 
307  /* Roll backwards to find the decimal for this number */
308  while( ptr > s->str_start )
309  {
310  ptr--;
311  if ( *ptr == '.' )
312  {
313  decimal_ptr = ptr;
314  break;
315  }
316  if ( (*ptr >= '0') && (*ptr <= '9' ) )
317  continue;
318  else
319  break;
320  }
321 
322  /* No decimal? Nothing to trim! */
323  if ( ! decimal_ptr )
324  return 0;
325 
326  ptr = s->str_end;
327 
328  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
329  while( ptr >= decimal_ptr )
330  {
331  ptr--;
332  if ( *ptr == '0' )
333  continue;
334  else
335  break;
336  }
337 
338  /* Huh, we get anywhere. Must not have trimmed anything. */
339  if ( ptr == s->str_end )
340  return 0;
341 
342  /* If we stopped at the decimal, we want to null that out.
343  It we stopped on a numeral, we want to preserve that, so push the
344  pointer forward one space. */
345  if ( *ptr != '.' )
346  ptr++;
347 
348  /* Add null terminator re-set the end of the stringbuffer. */
349  *ptr = '\0';
350  dist = s->str_end - ptr;
351  s->str_end = ptr;
352  return dist;
353 }
char * s
Definition: cu_in_wkt.c:23

References s.