PostGIS  2.2.8dev-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 297 of file stringbuffer.c.

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

Referenced by ptarray_to_kml2_sb().

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