Skip to content

Commit

Permalink
Create try_convert extension
Browse files Browse the repository at this point in the history
  • Loading branch information
robozmey committed Dec 11, 2024
1 parent 1d41230 commit 6727e1c
Show file tree
Hide file tree
Showing 52 changed files with 12,879 additions and 53 deletions.
2 changes: 2 additions & 0 deletions GNUmakefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ifeq ($(with_openssl), yes)
$(MAKE) -C contrib/sslinfo all
endif
$(MAKE) -C contrib/tablefunc all
$(MAKE) -C contrib/try_convert all
ifneq ($(with_uuid),no)
$(MAKE) -C contrib/uuid-ossp all
endif
Expand Down Expand Up @@ -96,6 +97,7 @@ ifeq ($(with_openssl), yes)
$(MAKE) -C contrib/sslinfo $@
endif
$(MAKE) -C contrib/tablefunc $@
$(MAKE) -C contrib/try_convert $@
ifneq ($(with_uuid),no)
$(MAKE) -C contrib/uuid-ossp $@
endif
Expand Down
1 change: 1 addition & 0 deletions contrib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SUBDIRS = \
test_decoding \
test_parser \
test_shm_mq \
try_convert \
tsearch2 \
tablefunc \
unaccent
Expand Down
4 changes: 4 additions & 0 deletions contrib/try_convert/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated subdirectories
/results/
/sql/
/expected/
20 changes: 20 additions & 0 deletions contrib/try_convert/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# contrib/try_convert/Makefile

MODULE_big = try_convert
OBJS = try_convert.o $(WIN32RES)

EXTENSION = try_convert
DATA = try_convert--1.0.sql
PGFILEDESC = "try_convert - function for type cast"
REGRESS = try_convert

ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
else
subdir = contrib/try_convert
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif
12 changes: 12 additions & 0 deletions contrib/try_convert/data/tt_abstime.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
2015-10-28 16:35:45
1974-02-15 15:52:07
2019-07-22 23:19:33
2024-06-24 23:02:00
1986-12-03 12:16:50
1982-12-11 19:01:59
1979-07-17 08:59:54
1982-06-27 18:52:43
2001-06-25 20:23:38
1975-02-17 19:50:17
2007-06-14 13:50:09
1973-06-11 12:06:52
11 changes: 11 additions & 0 deletions contrib/try_convert/data/tt_bit.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1
0
1010101
11001010
1011011101111
01001
1111
00000000000000000000000
1111111111111111111
0101001010101
11101001000100
19 changes: 19 additions & 0 deletions contrib/try_convert/data/tt_bool.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
f
true
t
false
t
t
f
t
False
t
f
True
TRUE
f
f
1
0
f
FALSE
70 changes: 70 additions & 0 deletions contrib/try_convert/data/tt_bpchar.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
All the world's a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages. At first, the infant,
Mewling and puking in the nurse's arms.
Then the whining schoolboy, with his satchel
And shining morning face, creeping like snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress' eyebrow. Then a soldier,
Full of strange oaths and bearded like the pard,
Jealous in honor, sudden and quick in quarrel,
Seeking the bubble reputation
Even in the cannon's mouth. And then the justice,
In fair round belly with good capon lined,
With eyes severe and beard of formal cut,
Full of wise saws and modern instances;
And so he plays his part. The sixth age shifts
Into the lean and slippered pantaloon,
With spectacles on nose and pouch on side;
His youthful hose, well saved, a world too wide
For his shrunk shank, and his big manly voice,
Turning again toward childish treble, pipes
And whistles in his sound. Last scene of all,
That ends this strange eventful history,
Is second childishness and mere oblivion,
Sans teeth, sans eyes, sans taste, sans everything.

std::to_string
C++ Strings library std::basic_string
Defined in header <string>
std::string to_string( int value );
std::string to_string( long value );
std::string to_string( long long value );
std::string to_string( unsigned value );
std::string to_string( unsigned long value );
std::string to_string( unsigned long long value );
std::string to_string( float value );
std::string to_string( double value );
std::string to_string( long double value );
Converts a numeric value to std::string.

Let buf be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.

1) Converts a signed integer to a string as if by std::sprintf(buf, "%d", value).
2) Converts a signed integer to a string as if by std::sprintf(buf, "%ld", value).
3) Converts a signed integer to a string as if by std::sprintf(buf, "%lld", value).
4) Converts an unsigned integer to a string as if by std::sprintf(buf, "%u", value).
5) Converts an unsigned integer to a string as if by std::sprintf(buf, "%lu", value).
6) Converts an unsigned integer to a string as if by std::sprintf(buf, "%llu", value).
7,8) Converts a floating point value to a string as if by std::sprintf(buf, "%f", value).
9) Converts a floating point value to a string as if by std::sprintf(buf, "%Lf", value).
(until C++26)
1-9) Converts a numeric value to a string as if by std::format("{}", value).
(since C++26)
Parameters
Return value
A string holding the converted value.

Exceptions
May throw std::bad_alloc from the std::string constructor.

Notes
With floating point types std::to_string may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
The return value may differ significantly from what std::cout prints by default, see the example.
std::to_string relies on the current C locale for formatting purposes, and therefore concurrent calls to std::to_string from multiple threads may result in partial serialization of calls.
The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
(until C++26)
C++17 provides std::to_chars as a higher-performance locale-independent alternative.
70 changes: 70 additions & 0 deletions contrib/try_convert/data/tt_char.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
All the world's a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages. At first, the infant,
Mewling and puking in the nurse's arms.
Then the whining schoolboy, with his satchel
And shining morning face, creeping like snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress' eyebrow. Then a soldier,
Full of strange oaths and bearded like the pard,
Jealous in honor, sudden and quick in quarrel,
Seeking the bubble reputation
Even in the cannon's mouth. And then the justice,
In fair round belly with good capon lined,
With eyes severe and beard of formal cut,
Full of wise saws and modern instances;
And so he plays his part. The sixth age shifts
Into the lean and slippered pantaloon,
With spectacles on nose and pouch on side;
His youthful hose, well saved, a world too wide
For his shrunk shank, and his big manly voice,
Turning again toward childish treble, pipes
And whistles in his sound. Last scene of all,
That ends this strange eventful history,
Is second childishness and mere oblivion,
Sans teeth, sans eyes, sans taste, sans everything.

std::to_string
C++ Strings library std::basic_string
Defined in header <string>
std::string to_string( int value );
std::string to_string( long value );
std::string to_string( long long value );
std::string to_string( unsigned value );
std::string to_string( unsigned long value );
std::string to_string( unsigned long long value );
std::string to_string( float value );
std::string to_string( double value );
std::string to_string( long double value );
Converts a numeric value to std::string.

Let buf be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.

1) Converts a signed integer to a string as if by std::sprintf(buf, "%d", value).
2) Converts a signed integer to a string as if by std::sprintf(buf, "%ld", value).
3) Converts a signed integer to a string as if by std::sprintf(buf, "%lld", value).
4) Converts an unsigned integer to a string as if by std::sprintf(buf, "%u", value).
5) Converts an unsigned integer to a string as if by std::sprintf(buf, "%lu", value).
6) Converts an unsigned integer to a string as if by std::sprintf(buf, "%llu", value).
7,8) Converts a floating point value to a string as if by std::sprintf(buf, "%f", value).
9) Converts a floating point value to a string as if by std::sprintf(buf, "%Lf", value).
(until C++26)
1-9) Converts a numeric value to a string as if by std::format("{}", value).
(since C++26)
Parameters
Return value
A string holding the converted value.

Exceptions
May throw std::bad_alloc from the std::string constructor.

Notes
With floating point types std::to_string may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
The return value may differ significantly from what std::cout prints by default, see the example.
std::to_string relies on the current C locale for formatting purposes, and therefore concurrent calls to std::to_string from multiple threads may result in partial serialization of calls.
The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
(until C++26)
C++17 provides std::to_chars as a higher-performance locale-independent alternative.
16 changes: 16 additions & 0 deletions contrib/try_convert/data/tt_cidr.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
192.168.100.128/25
192.168/24
192.168/25
192.168.1
192.168
128.1
128
128.1.2
10.1.2
10.1
10
10.1.2.3/32
2001:4f8:3:ba::/64
2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128
::ffff:1.2.3.0/120
::ffff:1.2.3.0/128
70 changes: 70 additions & 0 deletions contrib/try_convert/data/tt_citext.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
All the world's a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages. At first, the infant,
Mewling and puking in the nurse's arms.
Then the whining schoolboy, with his satchel
And shining morning face, creeping like snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress' eyebrow. Then a soldier,
Full of strange oaths and bearded like the pard,
Jealous in honor, sudden and quick in quarrel,
Seeking the bubble reputation
Even in the cannon's mouth. And then the justice,
In fair round belly with good capon lined,
With eyes severe and beard of formal cut,
Full of wise saws and modern instances;
And so he plays his part. The sixth age shifts
Into the lean and slippered pantaloon,
With spectacles on nose and pouch on side;
His youthful hose, well saved, a world too wide
For his shrunk shank, and his big manly voice,
Turning again toward childish treble, pipes
And whistles in his sound. Last scene of all,
That ends this strange eventful history,
Is second childishness and mere oblivion,
Sans teeth, sans eyes, sans taste, sans everything.

std::to_string
C++ Strings library std::basic_string
Defined in header <string>
std::string to_string( int value );
std::string to_string( long value );
std::string to_string( long long value );
std::string to_string( unsigned value );
std::string to_string( unsigned long value );
std::string to_string( unsigned long long value );
std::string to_string( float value );
std::string to_string( double value );
std::string to_string( long double value );
Converts a numeric value to std::string.

Let buf be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.

1) Converts a signed integer to a string as if by std::sprintf(buf, "%d", value).
2) Converts a signed integer to a string as if by std::sprintf(buf, "%ld", value).
3) Converts a signed integer to a string as if by std::sprintf(buf, "%lld", value).
4) Converts an unsigned integer to a string as if by std::sprintf(buf, "%u", value).
5) Converts an unsigned integer to a string as if by std::sprintf(buf, "%lu", value).
6) Converts an unsigned integer to a string as if by std::sprintf(buf, "%llu", value).
7,8) Converts a floating point value to a string as if by std::sprintf(buf, "%f", value).
9) Converts a floating point value to a string as if by std::sprintf(buf, "%Lf", value).
(until C++26)
1-9) Converts a numeric value to a string as if by std::format("{}", value).
(since C++26)
Parameters
Return value
A string holding the converted value.

Exceptions
May throw std::bad_alloc from the std::string constructor.

Notes
With floating point types std::to_string may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
The return value may differ significantly from what std::cout prints by default, see the example.
std::to_string relies on the current C locale for formatting purposes, and therefore concurrent calls to std::to_string from multiple threads may result in partial serialization of calls.
The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
(until C++26)
C++17 provides std::to_chars as a higher-performance locale-independent alternative.
12 changes: 12 additions & 0 deletions contrib/try_convert/data/tt_complex.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1.1 + 1.0i
10.0
-1213 - 13.342i
1546i
0.00004 + 9i
111111 - 0.00i
0i
0
-111.234
567.1
123331
-2
12 changes: 12 additions & 0 deletions contrib/try_convert/data/tt_date.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
2015-10-28
1974-02-15
2019-07-22
2024-06-24
1986-12-03
1982-12-12
1979-07-17
1982-06-27
2001-06-25
1975-02-18
2007-06-14
1973-06-11
24 changes: 24 additions & 0 deletions contrib/try_convert/data/tt_float4.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
5.09526
0.90909
0.47116
1.09649
62.7446
79.2079
42.2159
6.35277
381.619
996.121
529.114
971.078
8607.79
114.810
7207.21
6817.10
53697.0
26682.5
64096.1
11155.2
434765
453723
953815
875852
Loading

0 comments on commit 6727e1c

Please sign in to comment.