Skip to content

Instantly share code, notes, and snippets.

@ThomasLocke
Created February 6, 2012 14:02
Show Gist options
  • Save ThomasLocke/1752268 to your computer and use it in GitHub Desktop.
Save ThomasLocke/1752268 to your computer and use it in GitHub Desktop.
Patch for AWS.Utils.IS_Number - adding support for negative integers.
From f566fc2ed1a8073d3384b197322d6ed49372e584 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20L=C3=B8cke?= <thomas@12boo.net>
Date: Mon, 6 Feb 2012 14:45:08 +0100
Subject: [PATCH] Added support for negative numbers to AWS.Utils.Is_Number
---
src/core/aws-utils.adb | 11 ++++++++++-
src/core/aws-utils.ads | 7 +++++--
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/src/core/aws-utils.adb b/src/core/aws-utils.adb
index 4f297a2..534600c 100644
--- a/src/core/aws-utils.adb
+++ b/src/core/aws-utils.adb
@@ -495,9 +495,18 @@ package body AWS.Utils is
-- Is_Number --
---------------
- function Is_Number (S : String) return Boolean is
+ function Is_Number (S : String;
+ Allow_Negative : Boolean := False) return Boolean is
use Strings.Maps;
begin
+ if Allow_Negative
+ and then S'Length > 1
+ and then S (S'First) = '-'
+ then
+ return Is_Subset (To_Set (S (S'First + 1 .. S'Last)),
+ Constants.Decimal_Digit_Set);
+ end if;
+
return S'Length > 0
and then Is_Subset (To_Set (S), Constants.Decimal_Digit_Set);
end Is_Number;
diff --git a/src/core/aws-utils.ads b/src/core/aws-utils.ads
index d04985a..56e0fe0 100644
--- a/src/core/aws-utils.ads
+++ b/src/core/aws-utils.ads
@@ -98,8 +98,11 @@ package AWS.Utils is
-- Returns the value for the hexadecimal number Hex. Raises
-- Constraint_Error is Hex is not an hexadecimal number.
- function Is_Number (S : String) return Boolean;
- -- Returns True is S contains only decimal digits and is not empty
+ function Is_Number (S : String;
+ Allow_Negative : Boolean := False) return Boolean;
+ -- Returns True if S contains only decimal digits and is not empty. If
+ -- Allow_Negative is True, then also allow the '-' character in the first
+ -- position of S.
function Quote (Str : String; Replace : String := """") return String;
-- Returns Str with character '"' added at the start and the end
--
1.7.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment