PostGIS  2.4.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 304 of file stringbuffer.c.

References stringbuffer_t::str_end, and stringbuffer_t::str_start.

Referenced by ptarray_to_kml2_sb().

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