From b121c4d2ff97520f96f4ca166cb1b6268fae7655 Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 13:57:55 +0100 Subject: [PATCH 1/6] feat/removed travis.yml and used github actions. Also added go.mod and go.sum --- .github/workflows/go.yml | 44 +++++++++++ .travis.yml | 16 ---- Gopkg.lock | 28 ------- Gopkg.toml | 38 --------- go.mod | 10 +++ go.sum | 10 +++ phonenumberutil.go | 164 ++++++++++++++++++++------------------- 7 files changed, 149 insertions(+), 161 deletions(-) create mode 100644 .github/workflows/go.yml delete mode 100644 .travis.yml delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..4f7e075 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,44 @@ +name: Go CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + go-version: + - 1.9 + - 1.10 + - 1.11 + - 1.12 + - 1.13 + - 1.14 + - 1.15 + - 1.16 + - master + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + + - name: Install dep + run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + + - name: Install dependencies + run: dep ensure + + - name: Run tests + run: go test ./... diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 3f76b2d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: go -go: - - 1.9 - - "1.10.x" - - "1.11.x" - - "1.12.x" - - "1.13.x" - - "1.14.x" - - "1.15.x" - - "1.16.x" - - master - -script: - - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - - dep ensure - - go test ./... diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index ff2ff43..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,28 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:ffc060c551980d37ee9e428ef528ee2813137249ccebb0bfc412ef83071cac91" - name = "github.com/golang/protobuf" - packages = ["proto"] - pruneopts = "UT" - revision = "925541529c1fa6821df4e44ce2723319eb2be768" - version = "v1.0.0" - -[[projects]] - branch = "master" - digest = "1:5a73b4e1f20bd3480321a24aace506204e87ef73f7dc2e23d21f24478e94cd34" - name = "github.com/ttacon/builder" - packages = ["."] - pruneopts = "UT" - revision = "c099f663e1c235176c175644792c5eb282017ad7" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/golang/protobuf/proto", - "github.com/ttacon/builder", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 6f41abe..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,38 +0,0 @@ -# Gopkg.toml example -# -# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" -# -# [prune] -# non-go = false -# go-tests = true -# unused-packages = true - - -[[constraint]] - name = "github.com/golang/protobuf" - version = "1.0.0" - -[[constraint]] - branch = "master" - name = "github.com/ttacon/builder" - -[prune] - go-tests = true - unused-packages = true diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2cfb07c --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/ttacon/libphonenumber + +go 1.22.2 + +require ( + github.com/golang/protobuf v1.5.4 + github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 +) + +require google.golang.org/protobuf v1.33.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4b30438 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0= +github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/phonenumberutil.go b/phonenumberutil.go index 622aa7e..9261931 100644 --- a/phonenumberutil.go +++ b/phonenumberutil.go @@ -731,17 +731,23 @@ func isViablePhoneNumber(number string) bool { // Normalizes a string of characters representing a phone number. This // performs the following conversions: +// // - Punctuation is stripped. +// // - For ALPHA/VANITY numbers: -// - Letters are converted to their numeric representation on a telephone -// keypad. The keypad used here is the one defined in ITU Recommendation -// E.161. This is only done if there are 3 or more letters in the -// number, to lessen the risk that such letters are typos. +// +// - Letters are converted to their numeric representation on a telephone +// keypad. The keypad used here is the one defined in ITU Recommendation +// E.161. This is only done if there are 3 or more letters in the +// number, to lessen the risk that such letters are typos. // // - For other numbers: -// - Wide-ascii digits are converted to normal ASCII (European) digits. -// - Arabic-Indic numerals are converted to European numerals. -// - Spurious alpha characters are stripped. +// +// - Wide-ascii digits are converted to normal ASCII (European) digits. +// +// - Arabic-Indic numerals are converted to European numerals. +// +// - Spurious alpha characters are stripped. func normalize(number string) string { if VALID_ALPHA_PHONE_PATTERN.MatchString(number) { return normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true) @@ -838,33 +844,33 @@ func ConvertAlphaCharactersInNumber(number string) string { // works in such a way that the resultant subscriber number should be // diallable, at least on some devices. An example of how this could be used: // -// number, err := Parse("16502530000", "US"); -// // ... deal with err appropriately ... -// nationalSignificantNumber := GetNationalSignificantNumber(number); -// var areaCode, subscriberNumber; +// number, err := Parse("16502530000", "US"); +// // ... deal with err appropriately ... +// nationalSignificantNumber := GetNationalSignificantNumber(number); +// var areaCode, subscriberNumber; // -// int areaCodeLength = GetLengthOfGeographicalAreaCode(number); -// if (areaCodeLength > 0) { -// areaCode = nationalSignificantNumber[0:areaCodeLength]; -// subscriberNumber = nationalSignificantNumber[areaCodeLength:]; -// } else { -// areaCode = ""; -// subscriberNumber = nationalSignificantNumber; -// } +// int areaCodeLength = GetLengthOfGeographicalAreaCode(number); +// if (areaCodeLength > 0) { +// areaCode = nationalSignificantNumber[0:areaCodeLength]; +// subscriberNumber = nationalSignificantNumber[areaCodeLength:]; +// } else { +// areaCode = ""; +// subscriberNumber = nationalSignificantNumber; +// } // // N.B.: area code is a very ambiguous concept, so the I18N team generally // recommends against using it for most purposes, but recommends using the // more general national_number instead. Read the following carefully before // deciding to use this method: // -// - geographical area codes change over time, and this method honors those changes; -// therefore, it doesn't guarantee the stability of the result it produces. -// - subscriber numbers may not be diallable from all devices (notably mobile -// devices, which typically requires the full national_number to be dialled -// in most regions). -// - most non-geographical numbers have no area codes, including numbers from -// non-geographical entities -// - some geographical numbers have no area codes. +// - geographical area codes change over time, and this method honors those changes; +// therefore, it doesn't guarantee the stability of the result it produces. +// - subscriber numbers may not be diallable from all devices (notably mobile +// devices, which typically requires the full national_number to be dialled +// in most regions). +// - most non-geographical numbers have no area codes, including numbers from +// non-geographical entities +// - some geographical numbers have no area codes. func GetLengthOfGeographicalAreaCode(number *PhoneNumber) int { metadata := getMetadataForRegion(GetRegionCodeForNumber(number)) if metadata == nil { @@ -893,23 +899,23 @@ func GetLengthOfGeographicalAreaCode(number *PhoneNumber) int { // format, if there is a subscriber number part that follows. An example // of how this could be used: // -// PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); -// PhoneNumber number = phoneUtil.parse("18002530000", "US"); -// String nationalSignificantNumber = phoneUtil.GetNationalSignificantNumber(number); -// String nationalDestinationCode; -// String subscriberNumber; +// PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); +// PhoneNumber number = phoneUtil.parse("18002530000", "US"); +// String nationalSignificantNumber = phoneUtil.GetNationalSignificantNumber(number); +// String nationalDestinationCode; +// String subscriberNumber; // -// int nationalDestinationCodeLength = -// phoneUtil.GetLengthOfNationalDestinationCode(number); -// if nationalDestinationCodeLength > 0 { -// nationalDestinationCode = nationalSignificantNumber.substring(0, -// nationalDestinationCodeLength); -// subscriberNumber = nationalSignificantNumber.substring( -// nationalDestinationCodeLength); -// } else { -// nationalDestinationCode = ""; -// subscriberNumber = nationalSignificantNumber; -// } +// int nationalDestinationCodeLength = +// phoneUtil.GetLengthOfNationalDestinationCode(number); +// if nationalDestinationCodeLength > 0 { +// nationalDestinationCode = nationalSignificantNumber.substring(0, +// nationalDestinationCodeLength); +// subscriberNumber = nationalSignificantNumber.substring( +// nationalDestinationCodeLength); +// } else { +// nationalDestinationCode = ""; +// subscriberNumber = nationalSignificantNumber; +// } // // Refer to the unittests to see the difference between this function and // GetLengthOfGeographicalAreaCode(). @@ -1594,16 +1600,16 @@ func hasFormattingPatternForNumber(number *PhoneNumber) bool { // // Caveats: // -// - This will not produce good results if the country calling code is -// both present in the raw input _and_ is the start of the national -// number. This is not a problem in the regions which typically use -// alpha numbers. -// - This will also not produce good results if the raw input has any -// grouping information within the first three digits of the national -// number, and if the function needs to strip preceding digits/words -// in the raw input before these digits. Normally people group the -// first three digits together so this is not a huge problem - and will -// be fixed if it proves to be so. +// - This will not produce good results if the country calling code is +// both present in the raw input _and_ is the start of the national +// number. This is not a problem in the regions which typically use +// alpha numbers. +// - This will also not produce good results if the raw input has any +// grouping information within the first three digits of the national +// number, and if the function needs to strip preceding digits/words +// in the raw input before these digits. Normally people group the +// first three digits together so this is not a huge problem - and will +// be fixed if it proves to be so. func FormatOutOfCountryKeepingAlphaChars( number *PhoneNumber, regionCallingFrom string) string { @@ -2383,21 +2389,21 @@ func isShorterThanPossibleNormalNumber( // Check whether a phone number is a possible number. It provides a more // lenient check than IsValidNumber() in the following sense: // -// - It only checks the length of phone numbers. In particular, it -// doesn't check starting digits of the number. -// - It doesn't attempt to figure out the type of the number, but uses -// general rules which applies to all types of phone numbers in a -// region. Therefore, it is much faster than isValidNumber. -// - For fixed line numbers, many regions have the concept of area code, -// which together with subscriber number constitute the national -// significant number. It is sometimes okay to dial the subscriber number -// only when dialing in the same area. This function will return true -// if the subscriber-number-only version is passed in. On the other hand, -// because isValidNumber validates using information on both starting -// digits (for fixed line numbers, that would most likely be area codes) -// and length (obviously includes the length of area codes for fixed -// line numbers), it will return false for the subscriber-number-only -// version. +// - It only checks the length of phone numbers. In particular, it +// doesn't check starting digits of the number. +// - It doesn't attempt to figure out the type of the number, but uses +// general rules which applies to all types of phone numbers in a +// region. Therefore, it is much faster than isValidNumber. +// - For fixed line numbers, many regions have the concept of area code, +// which together with subscriber number constitute the national +// significant number. It is sometimes okay to dial the subscriber number +// only when dialing in the same area. This function will return true +// if the subscriber-number-only version is passed in. On the other hand, +// because isValidNumber validates using information on both starting +// digits (for fixed line numbers, that would most likely be area codes) +// and length (obviously includes the length of area codes for fixed +// line numbers), it will return false for the subscriber-number-only +// version. func IsPossibleNumberWithReason(number *PhoneNumber) ValidationResult { nationalNumber := GetNationalSignificantNumber(number) countryCode := int(number.GetCountryCode()) @@ -2517,17 +2523,17 @@ var ErrTooShortAfterIDD = errors.New("Phone number had an IDD, but " + // return zero if no country calling code is considered to be present. // Country calling codes are extracted in the following ways: // -// - by stripping the international dialing prefix of the region the -// person is dialing from, if this is present in the number, and looking -// at the next digits -// - by stripping the '+' sign if present and then looking at the next digits -// - by comparing the start of the number and the country calling code of -// the default region. If the number is not considered possible for the -// numbering plan of the default region initially, but starts with the -// country calling code of this region, validation will be reattempted -// after stripping this country calling code. If this number is considered a -// possible number, then the first digits will be considered the country -// calling code and removed as such. +// - by stripping the international dialing prefix of the region the +// person is dialing from, if this is present in the number, and looking +// at the next digits +// - by stripping the '+' sign if present and then looking at the next digits +// - by comparing the start of the number and the country calling code of +// the default region. If the number is not considered possible for the +// numbering plan of the default region initially, but starts with the +// country calling code of this region, validation will be reattempted +// after stripping this country calling code. If this number is considered a +// possible number, then the first digits will be considered the country +// calling code and removed as such. // // It will throw a NumberParseException if the number starts with a '+' but // the country calling code supplied after this does not match that of any From 24b6d9997325607da72824d48784a29daaeea05e Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 14:13:35 +0100 Subject: [PATCH 2/6] updated go versions --- .github/workflows/go.yml | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4f7e075..91bc20c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -3,10 +3,10 @@ name: Go CI on: push: branches: - - master + - main pull_request: branches: - - master + - main jobs: test: @@ -15,15 +15,11 @@ jobs: strategy: matrix: go-version: - - 1.9 - - 1.10 - - 1.11 - - 1.12 - - 1.13 - - 1.14 - - 1.15 - 1.16 - - master + - 1.17 + - 1.18 + - 1.19 + - 1.20 steps: - name: Checkout code @@ -34,11 +30,11 @@ jobs: with: go-version: ${{ matrix.go-version }} - - name: Install dep - run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + - name: Display Go version + run: go version - name: Install dependencies - run: dep ensure + run: go mod tidy - name: Run tests run: go test ./... From eb57fc71502920f178866199b7830fb6513c4244 Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 14:14:55 +0100 Subject: [PATCH 3/6] changed branch from main to master --- .github/workflows/go.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 91bc20c..0633b30 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -3,10 +3,10 @@ name: Go CI on: push: branches: - - main + - master pull_request: branches: - - main + - master jobs: test: From 5ba2a7a621db7309fec86c2bb2ff92332a3d5010 Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 14:20:23 +0100 Subject: [PATCH 4/6] go mod version updated --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2cfb07c..1afe4a6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ttacon/libphonenumber -go 1.22.2 +go 1.22 require ( github.com/golang/protobuf v1.5.4 From c3b7269d8f0227d8b8b616ddad4c656a96fba678 Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 14:39:01 +0100 Subject: [PATCH 5/6] Update GitHub Actions to support Go 1.22 --- .github/workflows/go.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0633b30..348ac12 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,11 +15,7 @@ jobs: strategy: matrix: go-version: - - 1.16 - - 1.17 - - 1.18 - - 1.19 - - 1.20 + - 1.22 steps: - name: Checkout code From 9bf7079a1fec83c33b97b47154e9970a1a0a4882 Mon Sep 17 00:00:00 2001 From: Mathias Jiya Date: Wed, 20 Nov 2024 15:01:28 +0100 Subject: [PATCH 6/6] updated README.MD file --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2184af4..c1f032e 100644 --- a/README.md +++ b/README.md @@ -49,4 +49,9 @@ if geoCodeLength > 0 { areaCode = natSigNumber[0:geoCodeLength] } fmt.Println(areaCode) -``` \ No newline at end of file +``` + +## Contributions? +Hey, you might have some new implementation, bug fixes and whatnot. Feel free to create an issue if necessary and get on it. + +Ensure to run `go mod tidy` on you favorite terminal to download the project dependencies after cloning the project. \ No newline at end of file