mini_phone 1.0.4 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec6635c4a695331ec847d69f21e79578f5f9ebdddf2752a7f1074fabd33ccfac
4
- data.tar.gz: 51aae23c0a2330a9599ee0336ad591e8fa4548c041b0a6aa1710f5834791b5f7
3
+ metadata.gz: 45a73dd6dde16c5baff08c0d9cf225e4bb53ae15b8b040559e4555180070e8d1
4
+ data.tar.gz: cbb196644cb95f7cf3a46648c176e22845708f5e3bab14256a8d80832e187175
5
5
  SHA512:
6
- metadata.gz: 19540acad836a3102e52aab746c530c8f78613e4d3ac98ba3844c24e156d1a994aa92c5dcfaa3fd831ffe7d0a8ba26aa036a8e23f982545a81a08fac2e0cab6c
7
- data.tar.gz: 747484c1f84802be42523c762f213eaa5044257d78546ac8a30c554c101053d6764e264c892fe29df74f265b44afa7829172a697f23996d1443835203266705b
6
+ metadata.gz: b95b0e2e18d15b7cf7c17e901a85d0b42f2e5fd6d9947f6d0e63a97197c86b39ce8f6341669e67ab69a07a6356b336f3cf4659a328a9c603a6d03856ac7c325b
7
+ data.tar.gz: 5db0ebb434cd3d427ae511e2a3e8bad13bcc7520c1147e1d7a8fafbf06c3213ae8521b30234b7691d0f06f634e0a503912bf638820f7dd37750ea12838944cf3
@@ -0,0 +1,9 @@
1
+ FROM ruby:2.7-alpine
2
+
3
+ WORKDiR /app
4
+
5
+ # RUN apt-get update -y && apt-get install -y valgrind libphonenumber
6
+ RUN apk add --no-cache libphonenumber-dev valgrind git make libffi-dev build-base
7
+ COPY Gemfile Gemfile.lock mini_phone.gemspec ./
8
+ COPY lib/mini_phone/version.rb ./lib/mini_phone/version.rb
9
+ RUN bundle install -j4
data/Gemfile CHANGED
@@ -7,6 +7,8 @@ gemspec
7
7
 
8
8
  gem 'rake', require: false
9
9
  # https://github.com/rake-compiler/rake-compiler/pull/166
10
+ gem 'get_process_mem', require: false
11
+ gem 'pry', require: false
10
12
  gem 'rake-compiler', github: 'larskanis/rake-compiler', branch: 'fix-native-version'
11
13
  gem 'rspec', '~> 3.0', require: false
12
14
  gem 'rspec-github', require: false
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # MiniPhone
2
2
 
3
- A Ruby gem which plugs directly in the the Google's native C++
4
- [libphonenumber](https://github.com/google/libphonenumber) for extemely
3
+ A Ruby gem which plugs directly into Google's native C++
4
+ [libphonenumber](https://github.com/google/libphonenumber) for extremely
5
5
  _fast_ and _robust_ phone number parsing, validation, and formatting. On
6
- average, most methods are 40x t0 50x faster than other Ruby phone number
6
+ average, most methods are 40x to 50x faster than other Ruby phone number
7
7
  libraries.
8
8
 
9
9
  ## Usage
@@ -23,10 +23,12 @@ MiniPhone.default_country = 'US'
23
23
 
24
24
  phone_number = MiniPhone.parse('404-384-1399')
25
25
 
26
- phone_number.e164 # +14043841399
27
- phone_number.national # (404) 384-1399
28
- phone_number.international # +1 404-384-1399
29
- phone_number.rfc3966 # tel:+1-404-384-1384
26
+ phone_number.e164 # +14043841399
27
+ phone_number.national # (404) 384-1399
28
+ phone_number.raw_national # 4043841399
29
+ phone_number.dasherized_national # 404-384-1399
30
+ phone_number.international # +1 404-384-1399
31
+ phone_number.rfc3966 # tel:+1-404-384-1384
30
32
  ```
31
33
 
32
34
  ### Checking if a phone number is possible
@@ -41,7 +43,7 @@ phone_number.possible? # false
41
43
 
42
44
  ```ruby
43
45
  MiniPhone.parse('+12423570000').type # :mobile
44
- MiniPhone.parse['+12423651234').type # :fixed_line
46
+ MiniPhone.parse('+12423651234').type # :fixed_line
45
47
  ```
46
48
 
47
49
  The possible types are directly mapped from [this
@@ -65,14 +67,14 @@ enum](https://github.com/google/libphonenumber/blob/4e9954edea7cf263532c5dd3861a
65
67
  ## Compatibility with PhoneLib
66
68
 
67
69
  MiniPhone aims to be compatible with
68
- [Phonelib](https://github.com/daddyz/phonelib) so in many cases it can be a
69
- drop in replacement. It has a smaller feature set, so if you need it it not a
70
- drop in replacement for every use case. If there is a feature you need, open
70
+ [Phonelib](https://github.com/daddyz/phonelib). In many cases it can be a
71
+ drop in replacement. It has a smaller feature set, so it is not a
72
+ replacement for every use case. If there is a feature you need, open
71
73
  an issue and we will try to support it.
72
74
 
73
75
  ## Benchmarks
74
76
 
75
- On average, most methods are 40x t0 50x faster than other libraries. To run
77
+ On average, most methods are 40x to 50x faster than other libraries. To run
76
78
  the benchmarks locally, execute: `bundle exec rake bench`
77
79
 
78
80
  ```
@@ -132,10 +134,10 @@ push git commits and tags, and push the `.gem` file to
132
134
  ## Contributing
133
135
 
134
136
  Bug reports and pull requests are welcome on GitHub at
135
- https://github.com/[USERNAME]/mini_phone. This project is intended to be a
137
+ https://github.com/ianks/mini_phone. This project is intended to be a
136
138
  safe, welcoming space for collaboration, and contributors are expected to
137
139
  adhere to the [code of
138
- conduct](https://github.com/[USERNAME]/mini_phone/blob/master/CODE_OF_CONDUCT.md).
140
+ conduct](https://github.com/ianks/mini_phone/blob/master/CODE_OF_CONDUCT.md).
139
141
 
140
142
  ## License
141
143
 
@@ -146,4 +148,4 @@ License](https://opensource.org/licenses/MIT).
146
148
 
147
149
  Everyone interacting in the MiniPhone project's codebases, issue trackers,
148
150
  chat rooms and mailing lists is expected to follow the [code of
149
- conduct](https://github.com/[USERNAME]/mini_phone/blob/master/CODE_OF_CONDUCT.md).
151
+ conduct](https://github.com/ianks/mini_phone/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -76,3 +76,21 @@ namespace :publish do
76
76
  push_to_github_registry(g)
77
77
  end
78
78
  end
79
+
80
+ desc 'Run valgrind test'
81
+
82
+ namespace :debug do
83
+ desc 'Plot memory'
84
+ task :memory do
85
+ sh 'debug/memory_plot/plot.sh'
86
+ end
87
+
88
+ task :valgrind do
89
+ sh 'docker build --tag mini_phone_dev -f Dockerfile.dev .'
90
+ args = '--tool=memcheck --num-callers=15 --partial-loads-ok=yes --undef-value-errors=no'
91
+ script = 'ruby debug/memory_plot/memory.rb --once'
92
+ cmd = "docker run -it --rm -v #{Dir.pwd}:/app -w /app mini_phone_dev valgrind #{args} #{script}"
93
+ puts cmd
94
+ system cmd
95
+ end
96
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'mini_phone'
5
+ require 'get_process_mem'
6
+
7
+ 10.times do
8
+ GC.start
9
+ GC.compact
10
+ end
11
+
12
+ $stdout.sync = true
13
+
14
+ loop do
15
+ 10_000.times do
16
+ pn = MiniPhone::PhoneNumber.new('+1 404 388 1299')
17
+ pn.e164
18
+ pn.valid?
19
+ pn.possible?
20
+ pn.raw_national
21
+ pn.international
22
+ pn.country_code
23
+ pn.area_code
24
+ end
25
+
26
+ 4.times { GC.start }
27
+
28
+ memory_mb = GetProcessMem.new.mb
29
+
30
+ $stdout.puts memory_mb
31
+
32
+ break if ARGV.first == '--once'
33
+ end
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+
5
+ bundle exec rake compile
6
+ exec ruby "$(dirname "$0")/memory.rb" | ttyplot
@@ -12,11 +12,31 @@ static VALUE rb_mMiniPhone;
12
12
  static VALUE rb_cPhoneNumber;
13
13
 
14
14
  static RepeatedPtrField<NumberFormat> raw_national_format;
15
+ static RepeatedPtrField<NumberFormat> dasherized_national_format;
15
16
 
16
- extern "C" struct PhoneNumberInfo {
17
- PhoneNumber phone_number;
18
- std::string raw_phone_number;
19
- std::string raw_country_code;
17
+ extern "C" struct PhoneNumberInfo { PhoneNumber *phone_number; };
18
+
19
+ extern "C" size_t phone_number_info_size(const void *data) { return sizeof(PhoneNumberInfo) + sizeof(PhoneNumber); }
20
+
21
+ extern "C" void phone_number_info_free(void *data) {
22
+ PhoneNumberInfo *phone_number_info = static_cast<PhoneNumberInfo *>(data);
23
+ phone_number_info->phone_number->~PhoneNumber();
24
+ xfree(phone_number_info->phone_number);
25
+ phone_number_info->~PhoneNumberInfo();
26
+ xfree(data);
27
+ }
28
+
29
+ extern "C" const rb_data_type_t phone_number_info_type = {
30
+ .wrap_struct_name = "MiniPhone/PhoneNumberInfo",
31
+ .function =
32
+ {
33
+ .dmark = NULL,
34
+ .dfree = phone_number_info_free,
35
+ .dsize = phone_number_info_size,
36
+ },
37
+ .parent = NULL,
38
+ .data = NULL,
39
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
20
40
  };
21
41
 
22
42
  static inline VALUE is_phone_number_valid(VALUE self, VALUE str, VALUE cc) {
@@ -106,23 +126,27 @@ extern "C" VALUE rb_set_default_country(VALUE self, VALUE str_code) {
106
126
 
107
127
  extern "C" VALUE rb_get_default_country(VALUE self) { return rb_iv_get(self, "@default_country"); }
108
128
 
109
- extern "C" void rb_phone_number_dealloc(PhoneNumberInfo *phone_number_info) { delete phone_number_info; }
110
-
111
129
  extern "C" VALUE rb_phone_number_parse(int argc, VALUE *argv, VALUE self) {
112
130
  return rb_class_new_instance(argc, argv, rb_cPhoneNumber);
113
131
  }
114
132
 
115
133
  extern "C" VALUE rb_phone_number_alloc(VALUE self) {
116
- PhoneNumberInfo *phone_number_info = new PhoneNumberInfo();
134
+ void *phone_number_data = ALLOC(PhoneNumber);
135
+ void *data = ALLOC(PhoneNumberInfo);
136
+ PhoneNumberInfo *phone_number_info = new (data) PhoneNumberInfo();
137
+ PhoneNumber *phone_number = new (phone_number_data) PhoneNumber();
138
+ phone_number_info->phone_number = phone_number;
117
139
 
118
- /* wrap */
119
- return Data_Wrap_Struct(self, NULL, &rb_phone_number_dealloc, phone_number_info);
140
+ return TypedData_Wrap_Struct(self, &phone_number_info_type, phone_number_info);
120
141
  }
121
142
 
122
143
  static inline VALUE rb_phone_number_nullify_ivars(VALUE self) {
123
144
  rb_iv_set(self, "@national", Qnil);
124
145
  rb_iv_set(self, "@raw_national", Qnil);
146
+ rb_iv_set(self, "@dasherized_national", Qnil);
125
147
  rb_iv_set(self, "@international", Qnil);
148
+ rb_iv_set(self, "@raw_international", Qnil);
149
+ rb_iv_set(self, "@dasherized_international", Qnil);
126
150
  rb_iv_set(self, "@e164", Qnil);
127
151
  rb_iv_set(self, "@country_code", Qnil);
128
152
  rb_iv_set(self, "@region_code", Qnil);
@@ -156,7 +180,7 @@ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
156
180
  PhoneNumberInfo *phone_number_info;
157
181
  PhoneNumber parsed_number;
158
182
 
159
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
183
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
160
184
 
161
185
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
162
186
 
@@ -168,7 +192,7 @@ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
168
192
  if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
169
193
  rb_phone_number_nullify_ivars(self);
170
194
  } else {
171
- phone_number_info->phone_number = parsed_number;
195
+ phone_number_info->phone_number->Swap(&parsed_number);
172
196
  }
173
197
 
174
198
  return self;
@@ -177,11 +201,11 @@ extern "C" VALUE rb_phone_number_initialize(int argc, VALUE *argv, VALUE self) {
177
201
  static inline VALUE rb_phone_number_format(VALUE self, PhoneNumberUtil::PhoneNumberFormat fmt) {
178
202
  std::string formatted_number;
179
203
  PhoneNumberInfo *phone_number_info;
180
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
204
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
181
205
 
182
206
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
183
- PhoneNumber parsed_number = phone_number_info->phone_number;
184
- phone_util.Format(parsed_number, fmt, &formatted_number);
207
+ PhoneNumber *parsed_number = phone_number_info->phone_number;
208
+ phone_util.Format(*parsed_number, fmt, &formatted_number);
185
209
 
186
210
  return rb_str_new(formatted_number.c_str(), formatted_number.size());
187
211
  }
@@ -219,22 +243,76 @@ extern "C" VALUE rb_phone_number_rfc3966(VALUE self) {
219
243
  return rb_iv_set(self, "@rfc3966", rb_phone_number_format(self, PhoneNumberUtil::PhoneNumberFormat::RFC3966));
220
244
  }
221
245
 
246
+ VALUE format_by_pattern_national(VALUE self, RepeatedPtrField<NumberFormat> format) {
247
+ std::string formatted_number;
248
+ PhoneNumberInfo *phone_number_info;
249
+ const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
250
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
251
+
252
+ phone_util.FormatByPattern(*phone_number_info->phone_number, PhoneNumberUtil::NATIONAL, format, &formatted_number);
253
+
254
+ return rb_str_new(formatted_number.c_str(), formatted_number.size());
255
+ }
256
+
222
257
  extern "C" VALUE rb_phone_number_raw_national(VALUE self) {
223
258
  if (rb_ivar_defined(self, rb_intern("@raw_national"))) {
224
259
  return rb_iv_get(self, "@raw_national");
225
260
  }
226
261
 
227
- std::string formatted_number;
262
+ VALUE result = format_by_pattern_national(self, raw_national_format);
263
+
264
+ return rb_iv_set(self, "@raw_national", result);
265
+ }
266
+
267
+ extern "C" VALUE rb_phone_number_dasherized_national(VALUE self) {
268
+ if (rb_ivar_defined(self, rb_intern("@dasherized_national"))) {
269
+ return rb_iv_get(self, "@dasherized_national");
270
+ }
271
+
272
+ VALUE result = format_by_pattern_national(self, dasherized_national_format);
273
+
274
+ return rb_iv_set(self, "@dasherized_national", result);
275
+ }
276
+
277
+ extern "C" VALUE rb_phone_number_country_code(VALUE self) {
278
+ if (rb_ivar_defined(self, rb_intern("@country_code"))) {
279
+ return rb_iv_get(self, "@country_code");
280
+ }
281
+
228
282
  PhoneNumberInfo *phone_number_info;
229
- const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
230
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
283
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
231
284
 
232
- phone_util.FormatByPattern(phone_number_info->phone_number, PhoneNumberUtil::NATIONAL, raw_national_format,
233
- &formatted_number);
285
+ int code = phone_number_info->phone_number->country_code();
234
286
 
235
- VALUE result = rb_str_new(formatted_number.c_str(), formatted_number.size());
287
+ VALUE result = INT2NUM(code);
236
288
 
237
- return rb_iv_set(self, "@raw_national", result);
289
+ return rb_iv_set(self, "@country_code", result);
290
+ }
291
+
292
+ extern "C" VALUE rb_phone_number_dasherized_international(VALUE self) {
293
+ if (rb_ivar_defined(self, rb_intern("@dasherized_international"))) {
294
+ return rb_iv_get(self, "@dasherized_international");
295
+ }
296
+
297
+ VALUE national = rb_phone_number_dasherized_national(self);
298
+ VALUE cc = rb_fix2str(rb_phone_number_country_code(self), 10);
299
+ VALUE dash = rb_str_new("-", 1);
300
+ VALUE prefix = rb_str_concat(cc, dash);
301
+ VALUE result = rb_str_concat(prefix, national);
302
+
303
+ return rb_iv_set(self, "@dasherized_international", result);
304
+ }
305
+
306
+ extern "C" VALUE rb_phone_number_raw_international(VALUE self) {
307
+ if (rb_ivar_defined(self, rb_intern("@raw_international"))) {
308
+ return rb_iv_get(self, "@raw_international");
309
+ }
310
+
311
+ VALUE national = rb_phone_number_raw_national(self);
312
+ VALUE cc = rb_fix2str(rb_phone_number_country_code(self), 10);
313
+ VALUE result = rb_str_concat(cc, national);
314
+
315
+ return rb_iv_set(self, "@raw_international", result);
238
316
  }
239
317
 
240
318
  extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
@@ -244,11 +322,11 @@ extern "C" VALUE rb_phone_number_valid_eh(VALUE self) {
244
322
 
245
323
  std::string formatted_number;
246
324
  PhoneNumberInfo *phone_number_info;
247
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
325
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
248
326
 
249
327
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
250
328
 
251
- if (phone_util.IsValidNumber(phone_number_info->phone_number)) {
329
+ if (phone_util.IsValidNumber(*phone_number_info->phone_number)) {
252
330
  return rb_iv_set(self, "@valid", Qtrue);
253
331
  } else {
254
332
  return rb_iv_set(self, "@valid", Qfalse);
@@ -266,11 +344,11 @@ extern "C" VALUE rb_phone_number_possible_eh(VALUE self) {
266
344
 
267
345
  std::string formatted_number;
268
346
  PhoneNumberInfo *phone_number_info;
269
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
347
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
270
348
 
271
349
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
272
350
 
273
- if (phone_util.IsPossibleNumber(phone_number_info->phone_number)) {
351
+ if (phone_util.IsPossibleNumber(*phone_number_info->phone_number)) {
274
352
  return rb_iv_set(self, "@possible", Qtrue);
275
353
  } else {
276
354
  return rb_iv_set(self, "@possible", Qfalse);
@@ -288,44 +366,38 @@ extern "C" VALUE rb_phone_number_region_code(VALUE self) {
288
366
 
289
367
  PhoneNumberInfo *phone_number_info;
290
368
  std::string code;
291
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
369
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
292
370
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
293
371
 
294
- phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number.country_code(), &code);
372
+ phone_util.GetRegionCodeForCountryCode(phone_number_info->phone_number->country_code(), &code);
295
373
 
296
374
  VALUE result = rb_str_new(code.c_str(), code.size());
297
375
 
298
376
  return rb_iv_set(self, "@region_code", result);
299
377
  }
300
378
 
301
- extern "C" VALUE rb_phone_number_country_code(VALUE self) {
302
- if (rb_ivar_defined(self, rb_intern("@country_code"))) {
303
- return rb_iv_get(self, "@country_code");
379
+ extern "C" VALUE rb_phone_number_match_eh(VALUE self, VALUE other) {
380
+ if (!rb_obj_is_kind_of(other, rb_cPhoneNumber)) {
381
+ return Qfalse;
304
382
  }
305
383
 
306
- PhoneNumberInfo *phone_number_info;
307
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
384
+ VALUE self_input = rb_iv_get(self, "@input");
385
+ VALUE other_input = rb_iv_get(other, "@input");
308
386
 
309
- int code = phone_number_info->phone_number.country_code();
310
-
311
- VALUE result = INT2NUM(code);
312
-
313
- return rb_iv_set(self, "@country_code", result);
314
- }
315
-
316
- extern "C" VALUE rb_phone_number_eql_eh(VALUE self, VALUE other) {
317
- if (!rb_obj_is_instance_of(other, rb_cPhoneNumber)) {
318
- return Qfalse;
387
+ // If inputs are the exact same, the result is as well
388
+ if (rb_eql(self_input, other_input)) {
389
+ return Qtrue;
319
390
  }
320
391
 
321
392
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
322
393
 
323
- PhoneNumberInfo *self_phone_number_info;
324
- Data_Get_Struct(self, PhoneNumberInfo, self_phone_number_info);
394
+ PhoneNumberInfo *self_info;
395
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, self_info);
325
396
 
326
- PhoneNumberInfo *other_phone_number_info;
327
- Data_Get_Struct(other, PhoneNumberInfo, other_phone_number_info);
328
- if (phone_util.IsNumberMatch(other_phone_number_info->phone_number, self_phone_number_info->phone_number)) {
397
+ PhoneNumberInfo *other_info;
398
+ TypedData_Get_Struct(other, PhoneNumberInfo, &phone_number_info_type, other_info);
399
+
400
+ if (phone_util.IsNumberMatch(*other_info->phone_number, *self_info->phone_number) == PhoneNumberUtil::EXACT_MATCH) {
329
401
  return Qtrue;
330
402
  } else {
331
403
  return Qfalse;
@@ -338,14 +410,14 @@ extern "C" VALUE rb_phone_number_type(VALUE self) {
338
410
  }
339
411
 
340
412
  PhoneNumberInfo *phone_number_info;
341
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
413
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
342
414
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
343
415
 
344
416
  VALUE result;
345
417
 
346
418
  // @see
347
419
  // https://github.com/google/libphonenumber/blob/4e9954edea7cf263532c5dd3861a801104c3f012/cpp/src/phonenumbers/phonenumberutil.h#L91
348
- switch (phone_util.GetNumberType(phone_number_info->phone_number)) {
420
+ switch (phone_util.GetNumberType(*phone_number_info->phone_number)) {
349
421
  case PhoneNumberUtil::PREMIUM_RATE:
350
422
  result = rb_intern("premium_rate");
351
423
  break;
@@ -394,15 +466,15 @@ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
394
466
 
395
467
  const PhoneNumberUtil &phone_util(*PhoneNumberUtil::GetInstance());
396
468
  PhoneNumberInfo *phone_number_info;
397
- Data_Get_Struct(self, PhoneNumberInfo, phone_number_info);
469
+ TypedData_Get_Struct(self, PhoneNumberInfo, &phone_number_info_type, phone_number_info);
398
470
 
399
- PhoneNumber number = phone_number_info->phone_number;
471
+ PhoneNumber *number = phone_number_info->phone_number;
400
472
  string national_significant_number;
401
- phone_util.GetNationalSignificantNumber(number, &national_significant_number);
473
+ phone_util.GetNationalSignificantNumber(*number, &national_significant_number);
402
474
  string area_code;
403
475
  string subscriber_number;
404
476
 
405
- int area_code_length = phone_util.GetLengthOfGeographicalAreaCode(number);
477
+ int area_code_length = phone_util.GetLengthOfGeographicalAreaCode(*number);
406
478
  if (area_code_length > 0) {
407
479
  area_code = national_significant_number.substr(0, area_code_length);
408
480
  subscriber_number = national_significant_number.substr(area_code_length, string::npos);
@@ -416,7 +488,23 @@ extern "C" VALUE rb_phone_number_area_code(VALUE self) {
416
488
  return rb_iv_set(self, "@area_code", result);
417
489
  }
418
490
 
491
+ extern "C" VALUE rb_phone_number_to_s(VALUE self) { return rb_iv_get(self, "@input"); }
492
+
493
+ static inline void setup_formats() {
494
+ // Raw
495
+ NumberFormat *raw_fmt = raw_national_format.Add();
496
+ raw_fmt->set_pattern("(\\d{3})(\\d{3})(\\d{4})");
497
+ raw_fmt->set_format("$1$2$3");
498
+
499
+ // Dasherized
500
+ NumberFormat *dsh_fmt = dasherized_national_format.Add();
501
+ dsh_fmt->set_pattern("(\\d{3})(\\d{3})(\\d{4})");
502
+ dsh_fmt->set_format("$1-$2-$3");
503
+ }
504
+
419
505
  extern "C" void Init_mini_phone(void) {
506
+ setup_formats();
507
+
420
508
  rb_mMiniPhone = rb_define_module("MiniPhone");
421
509
 
422
510
  // Unknown
@@ -451,10 +539,13 @@ extern "C" void Init_mini_phone(void) {
451
539
  rb_define_method(rb_cPhoneNumber, "e164", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_e164), 0);
452
540
  rb_define_method(rb_cPhoneNumber, "national", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_national), 0);
453
541
 
454
- // Raw National
455
- raw_national_format.Add()->set_format("$1$2$3");
456
-
457
542
  rb_define_method(rb_cPhoneNumber, "raw_national", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_raw_national), 0);
543
+ rb_define_method(rb_cPhoneNumber, "raw_international",
544
+ reinterpret_cast<VALUE (*)(...)>(rb_phone_number_raw_international), 0);
545
+ rb_define_method(rb_cPhoneNumber, "dasherized_international",
546
+ reinterpret_cast<VALUE (*)(...)>(rb_phone_number_dasherized_international), 0);
547
+ rb_define_method(rb_cPhoneNumber, "dasherized_national",
548
+ reinterpret_cast<VALUE (*)(...)>(rb_phone_number_dasherized_national), 0);
458
549
  rb_define_method(rb_cPhoneNumber, "international", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_international),
459
550
  0);
460
551
  rb_define_method(rb_cPhoneNumber, "rfc3966", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_rfc3966), 0);
@@ -463,5 +554,6 @@ extern "C" void Init_mini_phone(void) {
463
554
  rb_define_method(rb_cPhoneNumber, "country_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_country_code), 0);
464
555
  rb_define_method(rb_cPhoneNumber, "type", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_type), 0);
465
556
  rb_define_method(rb_cPhoneNumber, "area_code", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_area_code), 0);
466
- rb_define_method(rb_cPhoneNumber, "eql?", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_eql_eh), 1);
557
+ rb_define_method(rb_cPhoneNumber, "to_s", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_to_s), 0);
558
+ rb_define_method(rb_cPhoneNumber, "==", reinterpret_cast<VALUE (*)(...)>(rb_phone_number_match_eh), 1);
467
559
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniPhone
4
- VERSION = '1.0.4'
4
+ VERSION = '1.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_phone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-08 00:00:00.000000000 Z
11
+ date: 2021-01-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Plugs directly in the the Google's native C++ [libphonenumber](https://github.com/google/libphonenumber)
14
14
  for extemely _fast_ and _robust_ phone number parsing, validation, and formatting.
@@ -22,10 +22,13 @@ extra_rdoc_files: []
22
22
  files:
23
23
  - CHANGELOG.md
24
24
  - CODE_OF_CONDUCT.md
25
+ - Dockerfile.dev
25
26
  - Gemfile
26
27
  - LICENSE.txt
27
28
  - README.md
28
29
  - Rakefile
30
+ - debug/memory_plot/memory.rb
31
+ - debug/memory_plot/plot.sh
29
32
  - ext/mini_phone/extconf.rb
30
33
  - ext/mini_phone/mini_phone.cc
31
34
  - ext/mini_phone/mini_phone.h