repository_name
stringlengths 5
54
| func_path_in_repository
stringlengths 4
155
| func_name
stringlengths 1
118
| whole_func_string
stringlengths 52
3.87M
| language
stringclasses 1
value | func_code_string
stringlengths 52
3.87M
| func_code_tokens
sequencelengths 21
188k
| func_documentation_string
stringlengths 4
26.3k
| func_documentation_tokens
sequencelengths 1
3.92k
| split_name
stringclasses 1
value | func_code_url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|
go-gl/mathgl | mgl64/matrix.go | Mul | func (m1 Mat4x2) Mul(c float64) Mat4x2 {
return Mat4x2{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c}
} | go | func (m1 Mat4x2) Mul(c float64) Mat4x2 {
return Mat4x2{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c}
} | [
"func",
"(",
"m1",
"Mat4x2",
")",
"Mul",
"(",
"c",
"float64",
")",
"Mat4x2",
"{",
"return",
"Mat4x2",
"{",
"m1",
"[",
"0",
"]",
"*",
"c",
",",
"m1",
"[",
"1",
"]",
"*",
"c",
",",
"m1",
"[",
"2",
"]",
"*",
"c",
",",
"m1",
"[",
"3",
"]",
"*",
"c",
",",
"m1",
"[",
"4",
"]",
"*",
"c",
",",
"m1",
"[",
"5",
"]",
"*",
"c",
",",
"m1",
"[",
"6",
"]",
"*",
"c",
",",
"m1",
"[",
"7",
"]",
"*",
"c",
"}",
"\n",
"}"
] | // Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c. | [
"Mul",
"performs",
"a",
"scalar",
"multiplcation",
"of",
"the",
"matrix",
".",
"This",
"is",
"equivalent",
"to",
"iterating",
"over",
"every",
"element",
"of",
"the",
"matrix",
"and",
"multiply",
"it",
"by",
"c",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1591-L1593 |
go-gl/mathgl | mgl64/matrix.go | At | func (m Mat4x2) At(row, col int) float64 {
return m[col*4+row]
} | go | func (m Mat4x2) At(row, col int) float64 {
return m[col*4+row]
} | [
"func",
"(",
"m",
"Mat4x2",
")",
"At",
"(",
"row",
",",
"col",
"int",
")",
"float64",
"{",
"return",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"\n",
"}"
] | // At returns the matrix element at the given row and column.
// This is equivalent to mat[col * numRow + row] where numRow is constant
// (E.G. for a Mat3x2 it's equal to 3)
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds. | [
"At",
"returns",
"the",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"is",
"equivalent",
"to",
"mat",
"[",
"col",
"*",
"numRow",
"+",
"row",
"]",
"where",
"numRow",
"is",
"constant",
"(",
"E",
".",
"G",
".",
"for",
"a",
"Mat3x2",
"it",
"s",
"equal",
"to",
"3",
")",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"At",
"(",
"5",
"0",
")",
"will",
"work",
"just",
"like",
"At",
"(",
"1",
"1",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1722-L1724 |
go-gl/mathgl | mgl64/matrix.go | Set | func (m *Mat4x2) Set(row, col int, value float64) {
m[col*4+row] = value
} | go | func (m *Mat4x2) Set(row, col int, value float64) {
m[col*4+row] = value
} | [
"func",
"(",
"m",
"*",
"Mat4x2",
")",
"Set",
"(",
"row",
",",
"col",
"int",
",",
"value",
"float64",
")",
"{",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"=",
"value",
"\n",
"}"
] | // Set sets the corresponding matrix element at the given row and column.
// This has a pointer receiver because it mutates the matrix.
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds. | [
"Set",
"sets",
"the",
"corresponding",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"has",
"a",
"pointer",
"receiver",
"because",
"it",
"mutates",
"the",
"matrix",
".",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"Set",
"(",
"5",
"0",
"val",
")",
"will",
"work",
"just",
"like",
"Set",
"(",
"1",
"1",
"val",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1731-L1733 |
go-gl/mathgl | mgl64/matrix.go | Mul | func (m1 Mat4x3) Mul(c float64) Mat4x3 {
return Mat4x3{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c, m1[8] * c, m1[9] * c, m1[10] * c, m1[11] * c}
} | go | func (m1 Mat4x3) Mul(c float64) Mat4x3 {
return Mat4x3{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c, m1[8] * c, m1[9] * c, m1[10] * c, m1[11] * c}
} | [
"func",
"(",
"m1",
"Mat4x3",
")",
"Mul",
"(",
"c",
"float64",
")",
"Mat4x3",
"{",
"return",
"Mat4x3",
"{",
"m1",
"[",
"0",
"]",
"*",
"c",
",",
"m1",
"[",
"1",
"]",
"*",
"c",
",",
"m1",
"[",
"2",
"]",
"*",
"c",
",",
"m1",
"[",
"3",
"]",
"*",
"c",
",",
"m1",
"[",
"4",
"]",
"*",
"c",
",",
"m1",
"[",
"5",
"]",
"*",
"c",
",",
"m1",
"[",
"6",
"]",
"*",
"c",
",",
"m1",
"[",
"7",
"]",
"*",
"c",
",",
"m1",
"[",
"8",
"]",
"*",
"c",
",",
"m1",
"[",
"9",
"]",
"*",
"c",
",",
"m1",
"[",
"10",
"]",
"*",
"c",
",",
"m1",
"[",
"11",
"]",
"*",
"c",
"}",
"\n",
"}"
] | // Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c. | [
"Mul",
"performs",
"a",
"scalar",
"multiplcation",
"of",
"the",
"matrix",
".",
"This",
"is",
"equivalent",
"to",
"iterating",
"over",
"every",
"element",
"of",
"the",
"matrix",
"and",
"multiply",
"it",
"by",
"c",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1829-L1831 |
go-gl/mathgl | mgl64/matrix.go | At | func (m Mat4x3) At(row, col int) float64 {
return m[col*4+row]
} | go | func (m Mat4x3) At(row, col int) float64 {
return m[col*4+row]
} | [
"func",
"(",
"m",
"Mat4x3",
")",
"At",
"(",
"row",
",",
"col",
"int",
")",
"float64",
"{",
"return",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"\n",
"}"
] | // At returns the matrix element at the given row and column.
// This is equivalent to mat[col * numRow + row] where numRow is constant
// (E.G. for a Mat3x2 it's equal to 3)
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds. | [
"At",
"returns",
"the",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"is",
"equivalent",
"to",
"mat",
"[",
"col",
"*",
"numRow",
"+",
"row",
"]",
"where",
"numRow",
"is",
"constant",
"(",
"E",
".",
"G",
".",
"for",
"a",
"Mat3x2",
"it",
"s",
"equal",
"to",
"3",
")",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"At",
"(",
"5",
"0",
")",
"will",
"work",
"just",
"like",
"At",
"(",
"1",
"1",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1960-L1962 |
go-gl/mathgl | mgl64/matrix.go | Set | func (m *Mat4x3) Set(row, col int, value float64) {
m[col*4+row] = value
} | go | func (m *Mat4x3) Set(row, col int, value float64) {
m[col*4+row] = value
} | [
"func",
"(",
"m",
"*",
"Mat4x3",
")",
"Set",
"(",
"row",
",",
"col",
"int",
",",
"value",
"float64",
")",
"{",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"=",
"value",
"\n",
"}"
] | // Set sets the corresponding matrix element at the given row and column.
// This has a pointer receiver because it mutates the matrix.
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds. | [
"Set",
"sets",
"the",
"corresponding",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"has",
"a",
"pointer",
"receiver",
"because",
"it",
"mutates",
"the",
"matrix",
".",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"Set",
"(",
"5",
"0",
"val",
")",
"will",
"work",
"just",
"like",
"Set",
"(",
"1",
"1",
"val",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L1969-L1971 |
go-gl/mathgl | mgl64/matrix.go | Mul | func (m1 Mat4) Mul(c float64) Mat4 {
return Mat4{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c, m1[8] * c, m1[9] * c, m1[10] * c, m1[11] * c, m1[12] * c, m1[13] * c, m1[14] * c, m1[15] * c}
} | go | func (m1 Mat4) Mul(c float64) Mat4 {
return Mat4{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c, m1[8] * c, m1[9] * c, m1[10] * c, m1[11] * c, m1[12] * c, m1[13] * c, m1[14] * c, m1[15] * c}
} | [
"func",
"(",
"m1",
"Mat4",
")",
"Mul",
"(",
"c",
"float64",
")",
"Mat4",
"{",
"return",
"Mat4",
"{",
"m1",
"[",
"0",
"]",
"*",
"c",
",",
"m1",
"[",
"1",
"]",
"*",
"c",
",",
"m1",
"[",
"2",
"]",
"*",
"c",
",",
"m1",
"[",
"3",
"]",
"*",
"c",
",",
"m1",
"[",
"4",
"]",
"*",
"c",
",",
"m1",
"[",
"5",
"]",
"*",
"c",
",",
"m1",
"[",
"6",
"]",
"*",
"c",
",",
"m1",
"[",
"7",
"]",
"*",
"c",
",",
"m1",
"[",
"8",
"]",
"*",
"c",
",",
"m1",
"[",
"9",
"]",
"*",
"c",
",",
"m1",
"[",
"10",
"]",
"*",
"c",
",",
"m1",
"[",
"11",
"]",
"*",
"c",
",",
"m1",
"[",
"12",
"]",
"*",
"c",
",",
"m1",
"[",
"13",
"]",
"*",
"c",
",",
"m1",
"[",
"14",
"]",
"*",
"c",
",",
"m1",
"[",
"15",
"]",
"*",
"c",
"}",
"\n",
"}"
] | // Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c. | [
"Mul",
"performs",
"a",
"scalar",
"multiplcation",
"of",
"the",
"matrix",
".",
"This",
"is",
"equivalent",
"to",
"iterating",
"over",
"every",
"element",
"of",
"the",
"matrix",
"and",
"multiply",
"it",
"by",
"c",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L2091-L2093 |
go-gl/mathgl | mgl64/matrix.go | Det | func (m Mat4) Det() float64 {
return m[0]*m[5]*m[10]*m[15] - m[0]*m[5]*m[11]*m[14] - m[0]*m[6]*m[9]*m[15] + m[0]*m[6]*m[11]*m[13] + m[0]*m[7]*m[9]*m[14] - m[0]*m[7]*m[10]*m[13] - m[1]*m[4]*m[10]*m[15] + m[1]*m[4]*m[11]*m[14] + m[1]*m[6]*m[8]*m[15] - m[1]*m[6]*m[11]*m[12] - m[1]*m[7]*m[8]*m[14] + m[1]*m[7]*m[10]*m[12] + m[2]*m[4]*m[9]*m[15] - m[2]*m[4]*m[11]*m[13] - m[2]*m[5]*m[8]*m[15] + m[2]*m[5]*m[11]*m[12] + m[2]*m[7]*m[8]*m[13] - m[2]*m[7]*m[9]*m[12] - m[3]*m[4]*m[9]*m[14] + m[3]*m[4]*m[10]*m[13] + m[3]*m[5]*m[8]*m[14] - m[3]*m[5]*m[10]*m[12] - m[3]*m[6]*m[8]*m[13] + m[3]*m[6]*m[9]*m[12]
} | go | func (m Mat4) Det() float64 {
return m[0]*m[5]*m[10]*m[15] - m[0]*m[5]*m[11]*m[14] - m[0]*m[6]*m[9]*m[15] + m[0]*m[6]*m[11]*m[13] + m[0]*m[7]*m[9]*m[14] - m[0]*m[7]*m[10]*m[13] - m[1]*m[4]*m[10]*m[15] + m[1]*m[4]*m[11]*m[14] + m[1]*m[6]*m[8]*m[15] - m[1]*m[6]*m[11]*m[12] - m[1]*m[7]*m[8]*m[14] + m[1]*m[7]*m[10]*m[12] + m[2]*m[4]*m[9]*m[15] - m[2]*m[4]*m[11]*m[13] - m[2]*m[5]*m[8]*m[15] + m[2]*m[5]*m[11]*m[12] + m[2]*m[7]*m[8]*m[13] - m[2]*m[7]*m[9]*m[12] - m[3]*m[4]*m[9]*m[14] + m[3]*m[4]*m[10]*m[13] + m[3]*m[5]*m[8]*m[14] - m[3]*m[5]*m[10]*m[12] - m[3]*m[6]*m[8]*m[13] + m[3]*m[6]*m[9]*m[12]
} | [
"func",
"(",
"m",
"Mat4",
")",
"Det",
"(",
")",
"float64",
"{",
"return",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"\n",
"}"
] | // Det returns the determinant of a matrix. It is a measure of a square matrix's
// singularity and invertability, among other things. In this library, the
// determinant is hard coded based on pre-computed cofactor expansion, and uses
// no loops. Of course, the addition and multiplication must still be done. | [
"Det",
"returns",
"the",
"determinant",
"of",
"a",
"matrix",
".",
"It",
"is",
"a",
"measure",
"of",
"a",
"square",
"matrix",
"s",
"singularity",
"and",
"invertability",
"among",
"other",
"things",
".",
"In",
"this",
"library",
"the",
"determinant",
"is",
"hard",
"coded",
"based",
"on",
"pre",
"-",
"computed",
"cofactor",
"expansion",
"and",
"uses",
"no",
"loops",
".",
"Of",
"course",
"the",
"addition",
"and",
"multiplication",
"must",
"still",
"be",
"done",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L2186-L2188 |
go-gl/mathgl | mgl64/matrix.go | Inv | func (m Mat4) Inv() Mat4 {
det := m.Det()
if FloatEqual(det, float64(0.0)) {
return Mat4{}
}
retMat := Mat4{
-m[7]*m[10]*m[13] + m[6]*m[11]*m[13] + m[7]*m[9]*m[14] - m[5]*m[11]*m[14] - m[6]*m[9]*m[15] + m[5]*m[10]*m[15],
m[3]*m[10]*m[13] - m[2]*m[11]*m[13] - m[3]*m[9]*m[14] + m[1]*m[11]*m[14] + m[2]*m[9]*m[15] - m[1]*m[10]*m[15],
-m[3]*m[6]*m[13] + m[2]*m[7]*m[13] + m[3]*m[5]*m[14] - m[1]*m[7]*m[14] - m[2]*m[5]*m[15] + m[1]*m[6]*m[15],
m[3]*m[6]*m[9] - m[2]*m[7]*m[9] - m[3]*m[5]*m[10] + m[1]*m[7]*m[10] + m[2]*m[5]*m[11] - m[1]*m[6]*m[11],
m[7]*m[10]*m[12] - m[6]*m[11]*m[12] - m[7]*m[8]*m[14] + m[4]*m[11]*m[14] + m[6]*m[8]*m[15] - m[4]*m[10]*m[15],
-m[3]*m[10]*m[12] + m[2]*m[11]*m[12] + m[3]*m[8]*m[14] - m[0]*m[11]*m[14] - m[2]*m[8]*m[15] + m[0]*m[10]*m[15],
m[3]*m[6]*m[12] - m[2]*m[7]*m[12] - m[3]*m[4]*m[14] + m[0]*m[7]*m[14] + m[2]*m[4]*m[15] - m[0]*m[6]*m[15],
-m[3]*m[6]*m[8] + m[2]*m[7]*m[8] + m[3]*m[4]*m[10] - m[0]*m[7]*m[10] - m[2]*m[4]*m[11] + m[0]*m[6]*m[11],
-m[7]*m[9]*m[12] + m[5]*m[11]*m[12] + m[7]*m[8]*m[13] - m[4]*m[11]*m[13] - m[5]*m[8]*m[15] + m[4]*m[9]*m[15],
m[3]*m[9]*m[12] - m[1]*m[11]*m[12] - m[3]*m[8]*m[13] + m[0]*m[11]*m[13] + m[1]*m[8]*m[15] - m[0]*m[9]*m[15],
-m[3]*m[5]*m[12] + m[1]*m[7]*m[12] + m[3]*m[4]*m[13] - m[0]*m[7]*m[13] - m[1]*m[4]*m[15] + m[0]*m[5]*m[15],
m[3]*m[5]*m[8] - m[1]*m[7]*m[8] - m[3]*m[4]*m[9] + m[0]*m[7]*m[9] + m[1]*m[4]*m[11] - m[0]*m[5]*m[11],
m[6]*m[9]*m[12] - m[5]*m[10]*m[12] - m[6]*m[8]*m[13] + m[4]*m[10]*m[13] + m[5]*m[8]*m[14] - m[4]*m[9]*m[14],
-m[2]*m[9]*m[12] + m[1]*m[10]*m[12] + m[2]*m[8]*m[13] - m[0]*m[10]*m[13] - m[1]*m[8]*m[14] + m[0]*m[9]*m[14],
m[2]*m[5]*m[12] - m[1]*m[6]*m[12] - m[2]*m[4]*m[13] + m[0]*m[6]*m[13] + m[1]*m[4]*m[14] - m[0]*m[5]*m[14],
-m[2]*m[5]*m[8] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] + m[0]*m[5]*m[10],
}
return retMat.Mul(1 / det)
} | go | func (m Mat4) Inv() Mat4 {
det := m.Det()
if FloatEqual(det, float64(0.0)) {
return Mat4{}
}
retMat := Mat4{
-m[7]*m[10]*m[13] + m[6]*m[11]*m[13] + m[7]*m[9]*m[14] - m[5]*m[11]*m[14] - m[6]*m[9]*m[15] + m[5]*m[10]*m[15],
m[3]*m[10]*m[13] - m[2]*m[11]*m[13] - m[3]*m[9]*m[14] + m[1]*m[11]*m[14] + m[2]*m[9]*m[15] - m[1]*m[10]*m[15],
-m[3]*m[6]*m[13] + m[2]*m[7]*m[13] + m[3]*m[5]*m[14] - m[1]*m[7]*m[14] - m[2]*m[5]*m[15] + m[1]*m[6]*m[15],
m[3]*m[6]*m[9] - m[2]*m[7]*m[9] - m[3]*m[5]*m[10] + m[1]*m[7]*m[10] + m[2]*m[5]*m[11] - m[1]*m[6]*m[11],
m[7]*m[10]*m[12] - m[6]*m[11]*m[12] - m[7]*m[8]*m[14] + m[4]*m[11]*m[14] + m[6]*m[8]*m[15] - m[4]*m[10]*m[15],
-m[3]*m[10]*m[12] + m[2]*m[11]*m[12] + m[3]*m[8]*m[14] - m[0]*m[11]*m[14] - m[2]*m[8]*m[15] + m[0]*m[10]*m[15],
m[3]*m[6]*m[12] - m[2]*m[7]*m[12] - m[3]*m[4]*m[14] + m[0]*m[7]*m[14] + m[2]*m[4]*m[15] - m[0]*m[6]*m[15],
-m[3]*m[6]*m[8] + m[2]*m[7]*m[8] + m[3]*m[4]*m[10] - m[0]*m[7]*m[10] - m[2]*m[4]*m[11] + m[0]*m[6]*m[11],
-m[7]*m[9]*m[12] + m[5]*m[11]*m[12] + m[7]*m[8]*m[13] - m[4]*m[11]*m[13] - m[5]*m[8]*m[15] + m[4]*m[9]*m[15],
m[3]*m[9]*m[12] - m[1]*m[11]*m[12] - m[3]*m[8]*m[13] + m[0]*m[11]*m[13] + m[1]*m[8]*m[15] - m[0]*m[9]*m[15],
-m[3]*m[5]*m[12] + m[1]*m[7]*m[12] + m[3]*m[4]*m[13] - m[0]*m[7]*m[13] - m[1]*m[4]*m[15] + m[0]*m[5]*m[15],
m[3]*m[5]*m[8] - m[1]*m[7]*m[8] - m[3]*m[4]*m[9] + m[0]*m[7]*m[9] + m[1]*m[4]*m[11] - m[0]*m[5]*m[11],
m[6]*m[9]*m[12] - m[5]*m[10]*m[12] - m[6]*m[8]*m[13] + m[4]*m[10]*m[13] + m[5]*m[8]*m[14] - m[4]*m[9]*m[14],
-m[2]*m[9]*m[12] + m[1]*m[10]*m[12] + m[2]*m[8]*m[13] - m[0]*m[10]*m[13] - m[1]*m[8]*m[14] + m[0]*m[9]*m[14],
m[2]*m[5]*m[12] - m[1]*m[6]*m[12] - m[2]*m[4]*m[13] + m[0]*m[6]*m[13] + m[1]*m[4]*m[14] - m[0]*m[5]*m[14],
-m[2]*m[5]*m[8] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] + m[0]*m[5]*m[10],
}
return retMat.Mul(1 / det)
} | [
"func",
"(",
"m",
"Mat4",
")",
"Inv",
"(",
")",
"Mat4",
"{",
"det",
":=",
"m",
".",
"Det",
"(",
")",
"\n",
"if",
"FloatEqual",
"(",
"det",
",",
"float64",
"(",
"0.0",
")",
")",
"{",
"return",
"Mat4",
"{",
"}",
"\n",
"}",
"\n\n",
"retMat",
":=",
"Mat4",
"{",
"-",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
",",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"10",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"11",
"]",
",",
"-",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"11",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"15",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"15",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"15",
"]",
",",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"8",
"]",
"-",
"m",
"[",
"3",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"7",
"]",
"*",
"m",
"[",
"9",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"11",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"11",
"]",
",",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
",",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"12",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"10",
"]",
"*",
"m",
"[",
"13",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"8",
"]",
"*",
"m",
"[",
"14",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"9",
"]",
"*",
"m",
"[",
"14",
"]",
",",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"12",
"]",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"13",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"14",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"14",
"]",
",",
"-",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"8",
"]",
"+",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"8",
"]",
"+",
"m",
"[",
"2",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"9",
"]",
"-",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"6",
"]",
"*",
"m",
"[",
"9",
"]",
"-",
"m",
"[",
"1",
"]",
"*",
"m",
"[",
"4",
"]",
"*",
"m",
"[",
"10",
"]",
"+",
"m",
"[",
"0",
"]",
"*",
"m",
"[",
"5",
"]",
"*",
"m",
"[",
"10",
"]",
",",
"}",
"\n\n",
"return",
"retMat",
".",
"Mul",
"(",
"1",
"/",
"det",
")",
"\n",
"}"
] | // Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the
// original, yields the identity.
//
// M_inv * M = M * M_inv = I
//
// In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling
// are still done. This can still be (relatively) expensive for a 4x4.
//
// This function checks the determinant to see if the matrix is invertible.
// If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is
// entirely plausible to get a false positive or negative.
// In the future, an alternate function may be written which takes in a pre-computed determinant. | [
"Inv",
"computes",
"the",
"inverse",
"of",
"a",
"square",
"matrix",
".",
"An",
"inverse",
"is",
"a",
"square",
"matrix",
"such",
"that",
"when",
"multiplied",
"by",
"the",
"original",
"yields",
"the",
"identity",
".",
"M_inv",
"*",
"M",
"=",
"M",
"*",
"M_inv",
"=",
"I",
"In",
"this",
"library",
"the",
"math",
"is",
"precomputed",
"and",
"uses",
"no",
"loops",
"though",
"the",
"multiplications",
"additions",
"determinant",
"calculation",
"and",
"scaling",
"are",
"still",
"done",
".",
"This",
"can",
"still",
"be",
"(",
"relatively",
")",
"expensive",
"for",
"a",
"4x4",
".",
"This",
"function",
"checks",
"the",
"determinant",
"to",
"see",
"if",
"the",
"matrix",
"is",
"invertible",
".",
"If",
"the",
"determinant",
"is",
"0",
".",
"0",
"this",
"function",
"returns",
"the",
"zero",
"matrix",
".",
"However",
"due",
"to",
"floating",
"point",
"errors",
"it",
"is",
"entirely",
"plausible",
"to",
"get",
"a",
"false",
"positive",
"or",
"negative",
".",
"In",
"the",
"future",
"an",
"alternate",
"function",
"may",
"be",
"written",
"which",
"takes",
"in",
"a",
"pre",
"-",
"computed",
"determinant",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L2202-L2228 |
go-gl/mathgl | mgl64/matrix.go | At | func (m Mat4) At(row, col int) float64 {
return m[col*4+row]
} | go | func (m Mat4) At(row, col int) float64 {
return m[col*4+row]
} | [
"func",
"(",
"m",
"Mat4",
")",
"At",
"(",
"row",
",",
"col",
"int",
")",
"float64",
"{",
"return",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"\n",
"}"
] | // At returns the matrix element at the given row and column.
// This is equivalent to mat[col * numRow + row] where numRow is constant
// (E.G. for a Mat3x2 it's equal to 3)
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds. | [
"At",
"returns",
"the",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"is",
"equivalent",
"to",
"mat",
"[",
"col",
"*",
"numRow",
"+",
"row",
"]",
"where",
"numRow",
"is",
"constant",
"(",
"E",
".",
"G",
".",
"for",
"a",
"Mat3x2",
"it",
"s",
"equal",
"to",
"3",
")",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"At",
"(",
"5",
"0",
")",
"will",
"work",
"just",
"like",
"At",
"(",
"1",
"1",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L2270-L2272 |
go-gl/mathgl | mgl64/matrix.go | Set | func (m *Mat4) Set(row, col int, value float64) {
m[col*4+row] = value
} | go | func (m *Mat4) Set(row, col int, value float64) {
m[col*4+row] = value
} | [
"func",
"(",
"m",
"*",
"Mat4",
")",
"Set",
"(",
"row",
",",
"col",
"int",
",",
"value",
"float64",
")",
"{",
"m",
"[",
"col",
"*",
"4",
"+",
"row",
"]",
"=",
"value",
"\n",
"}"
] | // Set sets the corresponding matrix element at the given row and column.
// This has a pointer receiver because it mutates the matrix.
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds. | [
"Set",
"sets",
"the",
"corresponding",
"matrix",
"element",
"at",
"the",
"given",
"row",
"and",
"column",
".",
"This",
"has",
"a",
"pointer",
"receiver",
"because",
"it",
"mutates",
"the",
"matrix",
".",
"This",
"method",
"is",
"garbage",
"-",
"in",
"garbage",
"-",
"out",
".",
"For",
"instance",
"on",
"a",
"Mat4",
"asking",
"for",
"Set",
"(",
"5",
"0",
"val",
")",
"will",
"work",
"just",
"like",
"Set",
"(",
"1",
"1",
"val",
")",
".",
"Or",
"it",
"may",
"panic",
"if",
"it",
"s",
"out",
"of",
"bounds",
"."
] | train | https://github.com/go-gl/mathgl/blob/c4601bc793c7a480dab005cf969aab0d7a7fb07d/mgl64/matrix.go#L2279-L2281 |
jessevdk/go-assets | file.go | Close | func (f *File) Close() error {
f.buf = nil
f.dirIndex = 0
return nil
} | go | func (f *File) Close() error {
f.buf = nil
f.dirIndex = 0
return nil
} | [
"func",
"(",
"f",
"*",
"File",
")",
"Close",
"(",
")",
"error",
"{",
"f",
".",
"buf",
"=",
"nil",
"\n",
"f",
".",
"dirIndex",
"=",
"0",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Implementation of http.File | [
"Implementation",
"of",
"http",
".",
"File"
] | train | https://github.com/jessevdk/go-assets/blob/4f4301a06e153ff90e17793577ab6bf79f8dc5c5/file.go#L57-L62 |
jessevdk/go-assets | filesystem.go | Open | func (f *FileSystem) Open(p string) (http.File, error) {
p = path.Clean(p)
if len(f.LocalPath) != 0 {
return http.Dir(f.LocalPath).Open(p)
}
if fi, ok := f.Files[p]; ok {
if !fi.IsDir() {
// Make a copy for reading
ret := fi
ret.buf = bytes.NewReader(ret.Data)
return ret, nil
}
return fi, nil
}
return nil, os.ErrNotExist
} | go | func (f *FileSystem) Open(p string) (http.File, error) {
p = path.Clean(p)
if len(f.LocalPath) != 0 {
return http.Dir(f.LocalPath).Open(p)
}
if fi, ok := f.Files[p]; ok {
if !fi.IsDir() {
// Make a copy for reading
ret := fi
ret.buf = bytes.NewReader(ret.Data)
return ret, nil
}
return fi, nil
}
return nil, os.ErrNotExist
} | [
"func",
"(",
"f",
"*",
"FileSystem",
")",
"Open",
"(",
"p",
"string",
")",
"(",
"http",
".",
"File",
",",
"error",
")",
"{",
"p",
"=",
"path",
".",
"Clean",
"(",
"p",
")",
"\n\n",
"if",
"len",
"(",
"f",
".",
"LocalPath",
")",
"!=",
"0",
"{",
"return",
"http",
".",
"Dir",
"(",
"f",
".",
"LocalPath",
")",
".",
"Open",
"(",
"p",
")",
"\n",
"}",
"\n\n",
"if",
"fi",
",",
"ok",
":=",
"f",
".",
"Files",
"[",
"p",
"]",
";",
"ok",
"{",
"if",
"!",
"fi",
".",
"IsDir",
"(",
")",
"{",
"// Make a copy for reading",
"ret",
":=",
"fi",
"\n",
"ret",
".",
"buf",
"=",
"bytes",
".",
"NewReader",
"(",
"ret",
".",
"Data",
")",
"\n\n",
"return",
"ret",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"fi",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"os",
".",
"ErrNotExist",
"\n",
"}"
] | // Implementation of http.FileSystem | [
"Implementation",
"of",
"http",
".",
"FileSystem"
] | train | https://github.com/jessevdk/go-assets/blob/4f4301a06e153ff90e17793577ab6bf79f8dc5c5/filesystem.go#L50-L70 |
jessevdk/go-assets | generate.go | Add | func (x *Generator) Add(p string) error {
if x.fsFilesMap == nil {
x.fsFilesMap = make(map[string]file)
}
if x.fsDirsMap == nil {
x.fsDirsMap = make(map[string][]string)
}
p = path.Clean(p)
info, err := os.Stat(p)
if err != nil {
return err
}
prefix, p := x.splitRelPrefix(p)
if err := x.addParents(p, prefix); err != nil {
return err
}
return x.addPath(path.Dir(p), prefix, info)
} | go | func (x *Generator) Add(p string) error {
if x.fsFilesMap == nil {
x.fsFilesMap = make(map[string]file)
}
if x.fsDirsMap == nil {
x.fsDirsMap = make(map[string][]string)
}
p = path.Clean(p)
info, err := os.Stat(p)
if err != nil {
return err
}
prefix, p := x.splitRelPrefix(p)
if err := x.addParents(p, prefix); err != nil {
return err
}
return x.addPath(path.Dir(p), prefix, info)
} | [
"func",
"(",
"x",
"*",
"Generator",
")",
"Add",
"(",
"p",
"string",
")",
"error",
"{",
"if",
"x",
".",
"fsFilesMap",
"==",
"nil",
"{",
"x",
".",
"fsFilesMap",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"file",
")",
"\n",
"}",
"\n\n",
"if",
"x",
".",
"fsDirsMap",
"==",
"nil",
"{",
"x",
".",
"fsDirsMap",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"\n",
"}",
"\n\n",
"p",
"=",
"path",
".",
"Clean",
"(",
"p",
")",
"\n\n",
"info",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"p",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"prefix",
",",
"p",
":=",
"x",
".",
"splitRelPrefix",
"(",
"p",
")",
"\n\n",
"if",
"err",
":=",
"x",
".",
"addParents",
"(",
"p",
",",
"prefix",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"x",
".",
"addPath",
"(",
"path",
".",
"Dir",
"(",
"p",
")",
",",
"prefix",
",",
"info",
")",
"\n",
"}"
] | // Add a file or directory asset to the generator. Added directories will be
// recursed automatically. | [
"Add",
"a",
"file",
"or",
"directory",
"asset",
"to",
"the",
"generator",
".",
"Added",
"directories",
"will",
"be",
"recursed",
"automatically",
"."
] | train | https://github.com/jessevdk/go-assets/blob/4f4301a06e153ff90e17793577ab6bf79f8dc5c5/generate.go#L129-L153 |
jessevdk/go-assets | generate.go | Write | func (x *Generator) Write(wr io.Writer) error {
p := x.PackageName
if len(p) == 0 {
p = "main"
}
variableName := x.VariableName
if len(variableName) == 0 {
variableName = "Assets"
}
writer := &bytes.Buffer{}
// Write package and import
fmt.Fprintf(writer, "package %s\n\n", p)
fmt.Fprintln(writer, "import (")
fmt.Fprintln(writer, "\t\"time\"")
fmt.Fprintln(writer)
fmt.Fprintln(writer, "\t\"github.com/jessevdk/go-assets\"")
fmt.Fprintln(writer, ")")
fmt.Fprintln(writer)
vnames := make(map[string]string)
// Write file contents as const strings
if x.fsFilesMap != nil {
// Create mapping from full file path to asset variable name.
// This also reads the file and writes the contents as a const
// string
for k, v := range x.fsFilesMap {
if v.info.IsDir() {
continue
}
f, err := os.Open(v.path)
if err != nil {
return err
}
data, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return err
}
s := sha1.New()
io.WriteString(s, k)
vname := fmt.Sprintf("_%s%x", variableName, s.Sum(nil))
vnames[k] = vname
fmt.Fprintf(writer, "var %s = %#v\n", vname, string(data))
}
fmt.Fprintln(writer)
}
fmt.Fprintf(writer, "// %s returns go-assets FileSystem\n", variableName)
fmt.Fprintf(writer, "var %s = assets.NewFileSystem(", variableName)
if x.fsDirsMap == nil {
x.fsDirsMap = make(map[string][]string)
}
if x.fsFilesMap == nil {
x.fsFilesMap = make(map[string]file)
}
dirmap := make(map[string][]string)
for k, v := range x.fsDirsMap {
if kk, ok := x.stripPrefix(k); ok {
if len(kk) == 0 {
kk = "/"
}
dirmap[kk] = v
}
}
fmt.Fprintf(writer, "%#v, ", dirmap)
fmt.Fprintf(writer, "map[string]*assets.File{\n")
// Write files
for k, v := range x.fsFilesMap {
kk, ok := x.stripPrefix(k)
if !ok {
continue
}
if len(kk) == 0 {
kk = "/"
}
mt := v.info.ModTime()
var dt string
if !v.info.IsDir() {
dt = "[]byte(" + vnames[k] + ")"
} else {
dt = "nil"
}
fmt.Fprintf(writer, "\t\t%#v: &assets.File{\n", kk)
fmt.Fprintf(writer, "\t\t\tPath: %#v,\n", kk)
fmt.Fprintf(writer, "\t\t\tFileMode: %#v,\n", v.info.Mode())
fmt.Fprintf(writer, "\t\t\tMtime: time.Unix(%#v, %#v),\n", mt.Unix(), mt.UnixNano())
fmt.Fprintf(writer, "\t\t\tData: %s,\n", dt)
fmt.Fprintf(writer, "\t\t},")
}
fmt.Fprintln(writer, "\t}, \"\")")
ret, err := format.Source(writer.Bytes())
if err != nil {
return err
}
wr.Write(ret)
return nil
} | go | func (x *Generator) Write(wr io.Writer) error {
p := x.PackageName
if len(p) == 0 {
p = "main"
}
variableName := x.VariableName
if len(variableName) == 0 {
variableName = "Assets"
}
writer := &bytes.Buffer{}
// Write package and import
fmt.Fprintf(writer, "package %s\n\n", p)
fmt.Fprintln(writer, "import (")
fmt.Fprintln(writer, "\t\"time\"")
fmt.Fprintln(writer)
fmt.Fprintln(writer, "\t\"github.com/jessevdk/go-assets\"")
fmt.Fprintln(writer, ")")
fmt.Fprintln(writer)
vnames := make(map[string]string)
// Write file contents as const strings
if x.fsFilesMap != nil {
// Create mapping from full file path to asset variable name.
// This also reads the file and writes the contents as a const
// string
for k, v := range x.fsFilesMap {
if v.info.IsDir() {
continue
}
f, err := os.Open(v.path)
if err != nil {
return err
}
data, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return err
}
s := sha1.New()
io.WriteString(s, k)
vname := fmt.Sprintf("_%s%x", variableName, s.Sum(nil))
vnames[k] = vname
fmt.Fprintf(writer, "var %s = %#v\n", vname, string(data))
}
fmt.Fprintln(writer)
}
fmt.Fprintf(writer, "// %s returns go-assets FileSystem\n", variableName)
fmt.Fprintf(writer, "var %s = assets.NewFileSystem(", variableName)
if x.fsDirsMap == nil {
x.fsDirsMap = make(map[string][]string)
}
if x.fsFilesMap == nil {
x.fsFilesMap = make(map[string]file)
}
dirmap := make(map[string][]string)
for k, v := range x.fsDirsMap {
if kk, ok := x.stripPrefix(k); ok {
if len(kk) == 0 {
kk = "/"
}
dirmap[kk] = v
}
}
fmt.Fprintf(writer, "%#v, ", dirmap)
fmt.Fprintf(writer, "map[string]*assets.File{\n")
// Write files
for k, v := range x.fsFilesMap {
kk, ok := x.stripPrefix(k)
if !ok {
continue
}
if len(kk) == 0 {
kk = "/"
}
mt := v.info.ModTime()
var dt string
if !v.info.IsDir() {
dt = "[]byte(" + vnames[k] + ")"
} else {
dt = "nil"
}
fmt.Fprintf(writer, "\t\t%#v: &assets.File{\n", kk)
fmt.Fprintf(writer, "\t\t\tPath: %#v,\n", kk)
fmt.Fprintf(writer, "\t\t\tFileMode: %#v,\n", v.info.Mode())
fmt.Fprintf(writer, "\t\t\tMtime: time.Unix(%#v, %#v),\n", mt.Unix(), mt.UnixNano())
fmt.Fprintf(writer, "\t\t\tData: %s,\n", dt)
fmt.Fprintf(writer, "\t\t},")
}
fmt.Fprintln(writer, "\t}, \"\")")
ret, err := format.Source(writer.Bytes())
if err != nil {
return err
}
wr.Write(ret)
return nil
} | [
"func",
"(",
"x",
"*",
"Generator",
")",
"Write",
"(",
"wr",
"io",
".",
"Writer",
")",
"error",
"{",
"p",
":=",
"x",
".",
"PackageName",
"\n\n",
"if",
"len",
"(",
"p",
")",
"==",
"0",
"{",
"p",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"variableName",
":=",
"x",
".",
"VariableName",
"\n\n",
"if",
"len",
"(",
"variableName",
")",
"==",
"0",
"{",
"variableName",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"writer",
":=",
"&",
"bytes",
".",
"Buffer",
"{",
"}",
"\n\n",
"// Write package and import",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\n",
"\\n",
"\"",
",",
"p",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
",",
"\"",
"\"",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
",",
"\"",
"\\t",
"\\\"",
"\\\"",
"\"",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
",",
"\"",
"\\t",
"\\\"",
"\\\"",
"\"",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
",",
"\"",
"\"",
")",
"\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
")",
"\n\n",
"vnames",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n\n",
"// Write file contents as const strings",
"if",
"x",
".",
"fsFilesMap",
"!=",
"nil",
"{",
"// Create mapping from full file path to asset variable name.",
"// This also reads the file and writes the contents as a const",
"// string",
"for",
"k",
",",
"v",
":=",
"range",
"x",
".",
"fsFilesMap",
"{",
"if",
"v",
".",
"info",
".",
"IsDir",
"(",
")",
"{",
"continue",
"\n",
"}",
"\n\n",
"f",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"v",
".",
"path",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"data",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"f",
")",
"\n\n",
"f",
".",
"Close",
"(",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"s",
":=",
"sha1",
".",
"New",
"(",
")",
"\n",
"io",
".",
"WriteString",
"(",
"s",
",",
"k",
")",
"\n\n",
"vname",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"variableName",
",",
"s",
".",
"Sum",
"(",
"nil",
")",
")",
"\n",
"vnames",
"[",
"k",
"]",
"=",
"vname",
"\n\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\n",
"\"",
",",
"vname",
",",
"string",
"(",
"data",
")",
")",
"\n",
"}",
"\n\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
")",
"\n",
"}",
"\n\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\n",
"\"",
",",
"variableName",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\"",
",",
"variableName",
")",
"\n\n",
"if",
"x",
".",
"fsDirsMap",
"==",
"nil",
"{",
"x",
".",
"fsDirsMap",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"\n",
"}",
"\n\n",
"if",
"x",
".",
"fsFilesMap",
"==",
"nil",
"{",
"x",
".",
"fsFilesMap",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"file",
")",
"\n",
"}",
"\n\n",
"dirmap",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
")",
"\n\n",
"for",
"k",
",",
"v",
":=",
"range",
"x",
".",
"fsDirsMap",
"{",
"if",
"kk",
",",
"ok",
":=",
"x",
".",
"stripPrefix",
"(",
"k",
")",
";",
"ok",
"{",
"if",
"len",
"(",
"kk",
")",
"==",
"0",
"{",
"kk",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"dirmap",
"[",
"kk",
"]",
"=",
"v",
"\n",
"}",
"\n",
"}",
"\n\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\"",
",",
"dirmap",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\n",
"\"",
")",
"\n\n",
"// Write files",
"for",
"k",
",",
"v",
":=",
"range",
"x",
".",
"fsFilesMap",
"{",
"kk",
",",
"ok",
":=",
"x",
".",
"stripPrefix",
"(",
"k",
")",
"\n\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"kk",
")",
"==",
"0",
"{",
"kk",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"mt",
":=",
"v",
".",
"info",
".",
"ModTime",
"(",
")",
"\n\n",
"var",
"dt",
"string",
"\n\n",
"if",
"!",
"v",
".",
"info",
".",
"IsDir",
"(",
")",
"{",
"dt",
"=",
"\"",
"\"",
"+",
"vnames",
"[",
"k",
"]",
"+",
"\"",
"\"",
"\n",
"}",
"else",
"{",
"dt",
"=",
"\"",
"\"",
"\n",
"}",
"\n\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\\n",
"\"",
",",
"kk",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\\t",
"\\n",
"\"",
",",
"kk",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\\t",
"\\n",
"\"",
",",
"v",
".",
"info",
".",
"Mode",
"(",
")",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\\t",
"\\n",
"\"",
",",
"mt",
".",
"Unix",
"(",
")",
",",
"mt",
".",
"UnixNano",
"(",
")",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\\t",
"\\n",
"\"",
",",
"dt",
")",
"\n",
"fmt",
".",
"Fprintf",
"(",
"writer",
",",
"\"",
"\\t",
"\\t",
"\"",
")",
"\n",
"}",
"\n\n",
"fmt",
".",
"Fprintln",
"(",
"writer",
",",
"\"",
"\\t",
"\\\"",
"\\\"",
"\"",
")",
"\n\n",
"ret",
",",
"err",
":=",
"format",
".",
"Source",
"(",
"writer",
".",
"Bytes",
"(",
")",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"wr",
".",
"Write",
"(",
"ret",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // Write the asset tree specified in the generator to the given writer. The
// written asset tree is a valid, standalone go file with the assets
// embedded into it. | [
"Write",
"the",
"asset",
"tree",
"specified",
"in",
"the",
"generator",
"to",
"the",
"given",
"writer",
".",
"The",
"written",
"asset",
"tree",
"is",
"a",
"valid",
"standalone",
"go",
"file",
"with",
"the",
"assets",
"embedded",
"into",
"it",
"."
] | train | https://github.com/jessevdk/go-assets/blob/4f4301a06e153ff90e17793577ab6bf79f8dc5c5/generate.go#L170-L298 |
ttacon/libphonenumber | phonenumberutil.go | extractPossibleNumber | func extractPossibleNumber(number string) string {
if VALID_START_CHAR_PATTERN.MatchString(number) {
start := VALID_START_CHAR_PATTERN.FindIndex([]byte(number))[0]
number = number[start:]
// Remove trailing non-alpha non-numerical characters.
indices := UNWANTED_END_CHAR_PATTERN.FindIndex([]byte(number))
if len(indices) > 0 {
number = number[0:indices[0]]
}
// Check for extra numbers at the end.
indices = SECOND_NUMBER_START_PATTERN.FindIndex([]byte(number))
if len(indices) > 0 {
number = number[0:indices[0]]
}
return number
}
return ""
} | go | func extractPossibleNumber(number string) string {
if VALID_START_CHAR_PATTERN.MatchString(number) {
start := VALID_START_CHAR_PATTERN.FindIndex([]byte(number))[0]
number = number[start:]
// Remove trailing non-alpha non-numerical characters.
indices := UNWANTED_END_CHAR_PATTERN.FindIndex([]byte(number))
if len(indices) > 0 {
number = number[0:indices[0]]
}
// Check for extra numbers at the end.
indices = SECOND_NUMBER_START_PATTERN.FindIndex([]byte(number))
if len(indices) > 0 {
number = number[0:indices[0]]
}
return number
}
return ""
} | [
"func",
"extractPossibleNumber",
"(",
"number",
"string",
")",
"string",
"{",
"if",
"VALID_START_CHAR_PATTERN",
".",
"MatchString",
"(",
"number",
")",
"{",
"start",
":=",
"VALID_START_CHAR_PATTERN",
".",
"FindIndex",
"(",
"[",
"]",
"byte",
"(",
"number",
")",
")",
"[",
"0",
"]",
"\n",
"number",
"=",
"number",
"[",
"start",
":",
"]",
"\n",
"// Remove trailing non-alpha non-numerical characters.",
"indices",
":=",
"UNWANTED_END_CHAR_PATTERN",
".",
"FindIndex",
"(",
"[",
"]",
"byte",
"(",
"number",
")",
")",
"\n",
"if",
"len",
"(",
"indices",
")",
">",
"0",
"{",
"number",
"=",
"number",
"[",
"0",
":",
"indices",
"[",
"0",
"]",
"]",
"\n",
"}",
"\n",
"// Check for extra numbers at the end.",
"indices",
"=",
"SECOND_NUMBER_START_PATTERN",
".",
"FindIndex",
"(",
"[",
"]",
"byte",
"(",
"number",
")",
")",
"\n",
"if",
"len",
"(",
"indices",
")",
">",
"0",
"{",
"number",
"=",
"number",
"[",
"0",
":",
"indices",
"[",
"0",
"]",
"]",
"\n",
"}",
"\n",
"return",
"number",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
"\n",
"}"
] | // Attempts to extract a possible number from the string passed in.
// This currently strips all leading characters that cannot be used to
// start a phone number. Characters that can be used to start a phone
// number are defined in the VALID_START_CHAR_PATTERN. If none of these
// characters are found in the number passed in, an empty string is
// returned. This function also attempts to strip off any alternative
// extensions or endings if two or more are present, such as in the case
// of: (530) 583-6985 x302/x2303. The second extension here makes this
// actually two phone numbers, (530) 583-6985 x302 and (530) 583-6985 x2303.
// We remove the second extension so that the first number is parsed correctly. | [
"Attempts",
"to",
"extract",
"a",
"possible",
"number",
"from",
"the",
"string",
"passed",
"in",
".",
"This",
"currently",
"strips",
"all",
"leading",
"characters",
"that",
"cannot",
"be",
"used",
"to",
"start",
"a",
"phone",
"number",
".",
"Characters",
"that",
"can",
"be",
"used",
"to",
"start",
"a",
"phone",
"number",
"are",
"defined",
"in",
"the",
"VALID_START_CHAR_PATTERN",
".",
"If",
"none",
"of",
"these",
"characters",
"are",
"found",
"in",
"the",
"number",
"passed",
"in",
"an",
"empty",
"string",
"is",
"returned",
".",
"This",
"function",
"also",
"attempts",
"to",
"strip",
"off",
"any",
"alternative",
"extensions",
"or",
"endings",
"if",
"two",
"or",
"more",
"are",
"present",
"such",
"as",
"in",
"the",
"case",
"of",
":",
"(",
"530",
")",
"583",
"-",
"6985",
"x302",
"/",
"x2303",
".",
"The",
"second",
"extension",
"here",
"makes",
"this",
"actually",
"two",
"phone",
"numbers",
"(",
"530",
")",
"583",
"-",
"6985",
"x302",
"and",
"(",
"530",
")",
"583",
"-",
"6985",
"x2303",
".",
"We",
"remove",
"the",
"second",
"extension",
"so",
"that",
"the",
"first",
"number",
"is",
"parsed",
"correctly",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L699-L716 |
ttacon/libphonenumber | phonenumberutil.go | isViablePhoneNumber | func isViablePhoneNumber(number string) bool {
if len(number) < MIN_LENGTH_FOR_NSN {
return false
}
return VALID_PHONE_NUMBER_PATTERN.MatchString(number)
} | go | func isViablePhoneNumber(number string) bool {
if len(number) < MIN_LENGTH_FOR_NSN {
return false
}
return VALID_PHONE_NUMBER_PATTERN.MatchString(number)
} | [
"func",
"isViablePhoneNumber",
"(",
"number",
"string",
")",
"bool",
"{",
"if",
"len",
"(",
"number",
")",
"<",
"MIN_LENGTH_FOR_NSN",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"VALID_PHONE_NUMBER_PATTERN",
".",
"MatchString",
"(",
"number",
")",
"\n",
"}"
] | // Checks to see if the string of characters could possibly be a phone
// number at all. At the moment, checks to see that the string begins
// with at least 2 digits, ignoring any punctuation commonly found in
// phone numbers. This method does not require the number to be
// normalized in advance - but does assume that leading non-number symbols
// have been removed, such as by the method extractPossibleNumber.
// @VisibleForTesting | [
"Checks",
"to",
"see",
"if",
"the",
"string",
"of",
"characters",
"could",
"possibly",
"be",
"a",
"phone",
"number",
"at",
"all",
".",
"At",
"the",
"moment",
"checks",
"to",
"see",
"that",
"the",
"string",
"begins",
"with",
"at",
"least",
"2",
"digits",
"ignoring",
"any",
"punctuation",
"commonly",
"found",
"in",
"phone",
"numbers",
".",
"This",
"method",
"does",
"not",
"require",
"the",
"number",
"to",
"be",
"normalized",
"in",
"advance",
"-",
"but",
"does",
"assume",
"that",
"leading",
"non",
"-",
"number",
"symbols",
"have",
"been",
"removed",
"such",
"as",
"by",
"the",
"method",
"extractPossibleNumber",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L725-L730 |
ttacon/libphonenumber | phonenumberutil.go | normalize | func normalize(number string) string {
if VALID_ALPHA_PHONE_PATTERN.MatchString(number) {
return normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true)
}
return NormalizeDigitsOnly(number)
} | go | func normalize(number string) string {
if VALID_ALPHA_PHONE_PATTERN.MatchString(number) {
return normalizeHelper(number, ALPHA_PHONE_MAPPINGS, true)
}
return NormalizeDigitsOnly(number)
} | [
"func",
"normalize",
"(",
"number",
"string",
")",
"string",
"{",
"if",
"VALID_ALPHA_PHONE_PATTERN",
".",
"MatchString",
"(",
"number",
")",
"{",
"return",
"normalizeHelper",
"(",
"number",
",",
"ALPHA_PHONE_MAPPINGS",
",",
"true",
")",
"\n",
"}",
"\n",
"return",
"NormalizeDigitsOnly",
"(",
"number",
")",
"\n",
"}"
] | // 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.
//
// - 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. | [
"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",
".",
"-",
"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",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L745-L750 |
ttacon/libphonenumber | phonenumberutil.go | normalizeBytes | func normalizeBytes(number *builder.Builder) *builder.Builder {
normalizedNumber := normalize(number.String())
b := number.Bytes()
copy(b[0:len(normalizedNumber)], []byte(normalizedNumber))
return builder.NewBuilder(b)
} | go | func normalizeBytes(number *builder.Builder) *builder.Builder {
normalizedNumber := normalize(number.String())
b := number.Bytes()
copy(b[0:len(normalizedNumber)], []byte(normalizedNumber))
return builder.NewBuilder(b)
} | [
"func",
"normalizeBytes",
"(",
"number",
"*",
"builder",
".",
"Builder",
")",
"*",
"builder",
".",
"Builder",
"{",
"normalizedNumber",
":=",
"normalize",
"(",
"number",
".",
"String",
"(",
")",
")",
"\n",
"b",
":=",
"number",
".",
"Bytes",
"(",
")",
"\n",
"copy",
"(",
"b",
"[",
"0",
":",
"len",
"(",
"normalizedNumber",
")",
"]",
",",
"[",
"]",
"byte",
"(",
"normalizedNumber",
")",
")",
"\n",
"return",
"builder",
".",
"NewBuilder",
"(",
"b",
")",
"\n",
"}"
] | // Normalizes a string of characters representing a phone number. This is
// a wrapper for normalize(String number) but does in-place normalization
// of the StringBuilder provided. | [
"Normalizes",
"a",
"string",
"of",
"characters",
"representing",
"a",
"phone",
"number",
".",
"This",
"is",
"a",
"wrapper",
"for",
"normalize",
"(",
"String",
"number",
")",
"but",
"does",
"in",
"-",
"place",
"normalization",
"of",
"the",
"StringBuilder",
"provided",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L755-L760 |
ttacon/libphonenumber | phonenumberutil.go | GetLengthOfGeographicalAreaCode | func GetLengthOfGeographicalAreaCode(number *PhoneNumber) int {
metadata := getMetadataForRegion(GetRegionCodeForNumber(number))
if metadata == nil {
return 0
}
// If a country doesn't use a national prefix, and this number
// doesn't have an Italian leading zero, we assume it is a closed
// dialling plan with no area codes.
if len(metadata.GetNationalPrefix()) == 0 && !number.GetItalianLeadingZero() {
return 0
}
if !isNumberGeographical(number) {
return 0
}
return GetLengthOfNationalDestinationCode(number)
} | go | func GetLengthOfGeographicalAreaCode(number *PhoneNumber) int {
metadata := getMetadataForRegion(GetRegionCodeForNumber(number))
if metadata == nil {
return 0
}
// If a country doesn't use a national prefix, and this number
// doesn't have an Italian leading zero, we assume it is a closed
// dialling plan with no area codes.
if len(metadata.GetNationalPrefix()) == 0 && !number.GetItalianLeadingZero() {
return 0
}
if !isNumberGeographical(number) {
return 0
}
return GetLengthOfNationalDestinationCode(number)
} | [
"func",
"GetLengthOfGeographicalAreaCode",
"(",
"number",
"*",
"PhoneNumber",
")",
"int",
"{",
"metadata",
":=",
"getMetadataForRegion",
"(",
"GetRegionCodeForNumber",
"(",
"number",
")",
")",
"\n",
"if",
"metadata",
"==",
"nil",
"{",
"return",
"0",
"\n",
"}",
"\n\n",
"// If a country doesn't use a national prefix, and this number",
"// doesn't have an Italian leading zero, we assume it is a closed",
"// dialling plan with no area codes.",
"if",
"len",
"(",
"metadata",
".",
"GetNationalPrefix",
"(",
")",
")",
"==",
"0",
"&&",
"!",
"number",
".",
"GetItalianLeadingZero",
"(",
")",
"{",
"return",
"0",
"\n",
"}",
"\n\n",
"if",
"!",
"isNumberGeographical",
"(",
"number",
")",
"{",
"return",
"0",
"\n",
"}",
"\n\n",
"return",
"GetLengthOfNationalDestinationCode",
"(",
"number",
")",
"\n",
"}"
] | // Gets the length of the geographical area code from the PhoneNumber
// object passed in, so that clients could use it to split a national
// significant number into geographical area code and subscriber number. It
// 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;
//
// 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. | [
"Gets",
"the",
"length",
"of",
"the",
"geographical",
"area",
"code",
"from",
"the",
"PhoneNumber",
"object",
"passed",
"in",
"so",
"that",
"clients",
"could",
"use",
"it",
"to",
"split",
"a",
"national",
"significant",
"number",
"into",
"geographical",
"area",
"code",
"and",
"subscriber",
"number",
".",
"It",
"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",
";",
"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",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L868-L886 |
ttacon/libphonenumber | phonenumberutil.go | GetLengthOfNationalDestinationCode | func GetLengthOfNationalDestinationCode(number *PhoneNumber) int {
var copiedProto *PhoneNumber
if len(number.GetExtension()) > 0 {
// We don't want to alter the proto given to us, but we don't
// want to include the extension when we format it, so we copy
// it and clear the extension here.
copiedProto = &PhoneNumber{}
proto.Merge(copiedProto, number)
copiedProto.Extension = nil
} else {
copiedProto = number
}
nationalSignificantNumber := Format(copiedProto, INTERNATIONAL)
numberGroups := DIGITS_PATTERN.FindAllString(nationalSignificantNumber, -1)
// The pattern will start with "+COUNTRY_CODE " so the first group
// will always be the empty string (before the + symbol) and the
// second group will be the country calling code. The third group
// will be area code if it is not the last group.
if len(numberGroups) <= 3 {
return 0
}
if GetNumberType(number) == MOBILE {
// For example Argentinian mobile numbers, when formatted in
// the international format, are in the form of +54 9 NDC XXXX....
// As a result, we take the length of the third group (NDC) and
// add the length of the second group (which is the mobile token),
// which also forms part of the national significant number. This
// assumes that the mobile token is always formatted separately
// from the rest of the phone number.
mobileToken := GetCountryMobileToken(int(number.GetCountryCode()))
if mobileToken != "" {
return len(numberGroups[1]) + len(numberGroups[2])
}
}
return len(numberGroups[1])
} | go | func GetLengthOfNationalDestinationCode(number *PhoneNumber) int {
var copiedProto *PhoneNumber
if len(number.GetExtension()) > 0 {
// We don't want to alter the proto given to us, but we don't
// want to include the extension when we format it, so we copy
// it and clear the extension here.
copiedProto = &PhoneNumber{}
proto.Merge(copiedProto, number)
copiedProto.Extension = nil
} else {
copiedProto = number
}
nationalSignificantNumber := Format(copiedProto, INTERNATIONAL)
numberGroups := DIGITS_PATTERN.FindAllString(nationalSignificantNumber, -1)
// The pattern will start with "+COUNTRY_CODE " so the first group
// will always be the empty string (before the + symbol) and the
// second group will be the country calling code. The third group
// will be area code if it is not the last group.
if len(numberGroups) <= 3 {
return 0
}
if GetNumberType(number) == MOBILE {
// For example Argentinian mobile numbers, when formatted in
// the international format, are in the form of +54 9 NDC XXXX....
// As a result, we take the length of the third group (NDC) and
// add the length of the second group (which is the mobile token),
// which also forms part of the national significant number. This
// assumes that the mobile token is always formatted separately
// from the rest of the phone number.
mobileToken := GetCountryMobileToken(int(number.GetCountryCode()))
if mobileToken != "" {
return len(numberGroups[1]) + len(numberGroups[2])
}
}
return len(numberGroups[1])
} | [
"func",
"GetLengthOfNationalDestinationCode",
"(",
"number",
"*",
"PhoneNumber",
")",
"int",
"{",
"var",
"copiedProto",
"*",
"PhoneNumber",
"\n",
"if",
"len",
"(",
"number",
".",
"GetExtension",
"(",
")",
")",
">",
"0",
"{",
"// We don't want to alter the proto given to us, but we don't",
"// want to include the extension when we format it, so we copy",
"// it and clear the extension here.",
"copiedProto",
"=",
"&",
"PhoneNumber",
"{",
"}",
"\n",
"proto",
".",
"Merge",
"(",
"copiedProto",
",",
"number",
")",
"\n",
"copiedProto",
".",
"Extension",
"=",
"nil",
"\n",
"}",
"else",
"{",
"copiedProto",
"=",
"number",
"\n",
"}",
"\n\n",
"nationalSignificantNumber",
":=",
"Format",
"(",
"copiedProto",
",",
"INTERNATIONAL",
")",
"\n",
"numberGroups",
":=",
"DIGITS_PATTERN",
".",
"FindAllString",
"(",
"nationalSignificantNumber",
",",
"-",
"1",
")",
"\n",
"// The pattern will start with \"+COUNTRY_CODE \" so the first group",
"// will always be the empty string (before the + symbol) and the",
"// second group will be the country calling code. The third group",
"// will be area code if it is not the last group.",
"if",
"len",
"(",
"numberGroups",
")",
"<=",
"3",
"{",
"return",
"0",
"\n",
"}",
"\n",
"if",
"GetNumberType",
"(",
"number",
")",
"==",
"MOBILE",
"{",
"// For example Argentinian mobile numbers, when formatted in",
"// the international format, are in the form of +54 9 NDC XXXX....",
"// As a result, we take the length of the third group (NDC) and",
"// add the length of the second group (which is the mobile token),",
"// which also forms part of the national significant number. This",
"// assumes that the mobile token is always formatted separately",
"// from the rest of the phone number.",
"mobileToken",
":=",
"GetCountryMobileToken",
"(",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
")",
"\n",
"if",
"mobileToken",
"!=",
"\"",
"\"",
"{",
"return",
"len",
"(",
"numberGroups",
"[",
"1",
"]",
")",
"+",
"len",
"(",
"numberGroups",
"[",
"2",
"]",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"len",
"(",
"numberGroups",
"[",
"1",
"]",
")",
"\n",
"}"
] | // Gets the length of the national destination code (NDC) from the
// PhoneNumber object passed in, so that clients could use it to split a
// national significant number into NDC and subscriber number. The NDC of
// a phone number is normally the first group of digit(s) right after the
// country calling code when the number is formatted in the international
// 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;
//
// 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(). | [
"Gets",
"the",
"length",
"of",
"the",
"national",
"destination",
"code",
"(",
"NDC",
")",
"from",
"the",
"PhoneNumber",
"object",
"passed",
"in",
"so",
"that",
"clients",
"could",
"use",
"it",
"to",
"split",
"a",
"national",
"significant",
"number",
"into",
"NDC",
"and",
"subscriber",
"number",
".",
"The",
"NDC",
"of",
"a",
"phone",
"number",
"is",
"normally",
"the",
"first",
"group",
"of",
"digit",
"(",
"s",
")",
"right",
"after",
"the",
"country",
"calling",
"code",
"when",
"the",
"number",
"is",
"formatted",
"in",
"the",
"international",
"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",
";",
"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",
"()",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L916-L952 |
ttacon/libphonenumber | phonenumberutil.go | GetCountryMobileToken | func GetCountryMobileToken(countryCallingCode int) string {
if val, ok := MOBILE_TOKEN_MAPPINGS[countryCallingCode]; ok {
return val
}
return ""
} | go | func GetCountryMobileToken(countryCallingCode int) string {
if val, ok := MOBILE_TOKEN_MAPPINGS[countryCallingCode]; ok {
return val
}
return ""
} | [
"func",
"GetCountryMobileToken",
"(",
"countryCallingCode",
"int",
")",
"string",
"{",
"if",
"val",
",",
"ok",
":=",
"MOBILE_TOKEN_MAPPINGS",
"[",
"countryCallingCode",
"]",
";",
"ok",
"{",
"return",
"val",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
"\n",
"}"
] | // Returns the mobile token for the provided country calling code if it
// has one, otherwise returns an empty string. A mobile token is a number
// inserted before the area code when dialing a mobile number from that
// country from abroad. | [
"Returns",
"the",
"mobile",
"token",
"for",
"the",
"provided",
"country",
"calling",
"code",
"if",
"it",
"has",
"one",
"otherwise",
"returns",
"an",
"empty",
"string",
".",
"A",
"mobile",
"token",
"is",
"a",
"number",
"inserted",
"before",
"the",
"area",
"code",
"when",
"dialing",
"a",
"mobile",
"number",
"from",
"that",
"country",
"from",
"abroad",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L958-L963 |
ttacon/libphonenumber | phonenumberutil.go | normalizeHelper | func normalizeHelper(number string,
normalizationReplacements map[rune]rune,
removeNonMatches bool) string {
var normalizedNumber = builder.NewBuilder(nil)
for _, character := range number {
newDigit, ok := normalizationReplacements[unicode.ToUpper(character)]
if ok {
normalizedNumber.WriteRune(newDigit)
} else if !removeNonMatches {
normalizedNumber.WriteRune(character)
}
// If neither of the above are true, we remove this character.
}
return normalizedNumber.String()
} | go | func normalizeHelper(number string,
normalizationReplacements map[rune]rune,
removeNonMatches bool) string {
var normalizedNumber = builder.NewBuilder(nil)
for _, character := range number {
newDigit, ok := normalizationReplacements[unicode.ToUpper(character)]
if ok {
normalizedNumber.WriteRune(newDigit)
} else if !removeNonMatches {
normalizedNumber.WriteRune(character)
}
// If neither of the above are true, we remove this character.
}
return normalizedNumber.String()
} | [
"func",
"normalizeHelper",
"(",
"number",
"string",
",",
"normalizationReplacements",
"map",
"[",
"rune",
"]",
"rune",
",",
"removeNonMatches",
"bool",
")",
"string",
"{",
"var",
"normalizedNumber",
"=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n",
"for",
"_",
",",
"character",
":=",
"range",
"number",
"{",
"newDigit",
",",
"ok",
":=",
"normalizationReplacements",
"[",
"unicode",
".",
"ToUpper",
"(",
"character",
")",
"]",
"\n",
"if",
"ok",
"{",
"normalizedNumber",
".",
"WriteRune",
"(",
"newDigit",
")",
"\n",
"}",
"else",
"if",
"!",
"removeNonMatches",
"{",
"normalizedNumber",
".",
"WriteRune",
"(",
"character",
")",
"\n",
"}",
"\n",
"// If neither of the above are true, we remove this character.",
"}",
"\n",
"return",
"normalizedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Normalizes a string of characters representing a phone number by replacing
// all characters found in the accompanying map with the values therein,
// and stripping all other characters if removeNonMatches is true. | [
"Normalizes",
"a",
"string",
"of",
"characters",
"representing",
"a",
"phone",
"number",
"by",
"replacing",
"all",
"characters",
"found",
"in",
"the",
"accompanying",
"map",
"with",
"the",
"values",
"therein",
"and",
"stripping",
"all",
"other",
"characters",
"if",
"removeNonMatches",
"is",
"true",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L968-L983 |
ttacon/libphonenumber | phonenumberutil.go | isNumberGeographical | func isNumberGeographical(phoneNumber *PhoneNumber) bool {
numberType := GetNumberType(phoneNumber)
// TODO: Include mobile phone numbers from countries like Indonesia,
// which has some mobile numbers that are geographical.
return numberType == FIXED_LINE ||
numberType == FIXED_LINE_OR_MOBILE
} | go | func isNumberGeographical(phoneNumber *PhoneNumber) bool {
numberType := GetNumberType(phoneNumber)
// TODO: Include mobile phone numbers from countries like Indonesia,
// which has some mobile numbers that are geographical.
return numberType == FIXED_LINE ||
numberType == FIXED_LINE_OR_MOBILE
} | [
"func",
"isNumberGeographical",
"(",
"phoneNumber",
"*",
"PhoneNumber",
")",
"bool",
"{",
"numberType",
":=",
"GetNumberType",
"(",
"phoneNumber",
")",
"\n",
"// TODO: Include mobile phone numbers from countries like Indonesia,",
"// which has some mobile numbers that are geographical.",
"return",
"numberType",
"==",
"FIXED_LINE",
"||",
"numberType",
"==",
"FIXED_LINE_OR_MOBILE",
"\n",
"}"
] | // Tests whether a phone number has a geographical association. It checks
// if the number is associated to a certain region in the country where it
// belongs to. Note that this doesn't verify if the number is actually in use.
//
// A similar method is implemented as PhoneNumberOfflineGeocoder.canBeGeocoded,
// which performs a looser check, since it only prevents cases where prefixes
// overlap for geocodable and non-geocodable numbers. Also, if new phone
// number types were added, we should check if this other method should be
// updated too. | [
"Tests",
"whether",
"a",
"phone",
"number",
"has",
"a",
"geographical",
"association",
".",
"It",
"checks",
"if",
"the",
"number",
"is",
"associated",
"to",
"a",
"certain",
"region",
"in",
"the",
"country",
"where",
"it",
"belongs",
"to",
".",
"Note",
"that",
"this",
"doesn",
"t",
"verify",
"if",
"the",
"number",
"is",
"actually",
"in",
"use",
".",
"A",
"similar",
"method",
"is",
"implemented",
"as",
"PhoneNumberOfflineGeocoder",
".",
"canBeGeocoded",
"which",
"performs",
"a",
"looser",
"check",
"since",
"it",
"only",
"prevents",
"cases",
"where",
"prefixes",
"overlap",
"for",
"geocodable",
"and",
"non",
"-",
"geocodable",
"numbers",
".",
"Also",
"if",
"new",
"phone",
"number",
"types",
"were",
"added",
"we",
"should",
"check",
"if",
"this",
"other",
"method",
"should",
"be",
"updated",
"too",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1012-L1018 |
ttacon/libphonenumber | phonenumberutil.go | isValidRegionCode | func isValidRegionCode(regionCode string) bool {
_, contains := readFromSupportedRegions(regionCode)
return len(regionCode) != 0 && contains
} | go | func isValidRegionCode(regionCode string) bool {
_, contains := readFromSupportedRegions(regionCode)
return len(regionCode) != 0 && contains
} | [
"func",
"isValidRegionCode",
"(",
"regionCode",
"string",
")",
"bool",
"{",
"_",
",",
"contains",
":=",
"readFromSupportedRegions",
"(",
"regionCode",
")",
"\n",
"return",
"len",
"(",
"regionCode",
")",
"!=",
"0",
"&&",
"contains",
"\n",
"}"
] | // Helper function to check region code is not unknown or null. | [
"Helper",
"function",
"to",
"check",
"region",
"code",
"is",
"not",
"unknown",
"or",
"null",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1021-L1024 |
ttacon/libphonenumber | phonenumberutil.go | Format | func Format(number *PhoneNumber, numberFormat PhoneNumberFormat) string {
if number.GetNationalNumber() == 0 && len(number.GetRawInput()) > 0 {
// Unparseable numbers that kept their raw input just use that.
// This is the only case where a number can be formatted as E164
// without a leading '+' symbol (but the original number wasn't
// parseable anyway).
// TODO: Consider removing the 'if' above so that unparseable
// strings without raw input format to the empty string instead of "+00"
rawInput := number.GetRawInput()
if len(rawInput) > 0 {
return rawInput
}
}
var formattedNumber = builder.NewBuilder(nil)
FormatWithBuf(number, numberFormat, formattedNumber)
return formattedNumber.String()
} | go | func Format(number *PhoneNumber, numberFormat PhoneNumberFormat) string {
if number.GetNationalNumber() == 0 && len(number.GetRawInput()) > 0 {
// Unparseable numbers that kept their raw input just use that.
// This is the only case where a number can be formatted as E164
// without a leading '+' symbol (but the original number wasn't
// parseable anyway).
// TODO: Consider removing the 'if' above so that unparseable
// strings without raw input format to the empty string instead of "+00"
rawInput := number.GetRawInput()
if len(rawInput) > 0 {
return rawInput
}
}
var formattedNumber = builder.NewBuilder(nil)
FormatWithBuf(number, numberFormat, formattedNumber)
return formattedNumber.String()
} | [
"func",
"Format",
"(",
"number",
"*",
"PhoneNumber",
",",
"numberFormat",
"PhoneNumberFormat",
")",
"string",
"{",
"if",
"number",
".",
"GetNationalNumber",
"(",
")",
"==",
"0",
"&&",
"len",
"(",
"number",
".",
"GetRawInput",
"(",
")",
")",
">",
"0",
"{",
"// Unparseable numbers that kept their raw input just use that.",
"// This is the only case where a number can be formatted as E164",
"// without a leading '+' symbol (but the original number wasn't",
"// parseable anyway).",
"// TODO: Consider removing the 'if' above so that unparseable",
"// strings without raw input format to the empty string instead of \"+00\"",
"rawInput",
":=",
"number",
".",
"GetRawInput",
"(",
")",
"\n",
"if",
"len",
"(",
"rawInput",
")",
">",
"0",
"{",
"return",
"rawInput",
"\n",
"}",
"\n",
"}",
"\n",
"var",
"formattedNumber",
"=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n",
"FormatWithBuf",
"(",
"number",
",",
"numberFormat",
",",
"formattedNumber",
")",
"\n",
"return",
"formattedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Formats a phone number in the specified format using default rules. Note
// that this does not promise to produce a phone number that the user can
// dial from where they are - although we do format in either 'national' or
// 'international' format depending on what the client asks for, we do not
// currently support a more abbreviated format, such as for users in the
// same "area" who could potentially dial the number without area code.
// Note that if the phone number has a country calling code of 0 or an
// otherwise invalid country calling code, we cannot work out which
// formatting rules to apply so we return the national significant number
// with no formatting applied. | [
"Formats",
"a",
"phone",
"number",
"in",
"the",
"specified",
"format",
"using",
"default",
"rules",
".",
"Note",
"that",
"this",
"does",
"not",
"promise",
"to",
"produce",
"a",
"phone",
"number",
"that",
"the",
"user",
"can",
"dial",
"from",
"where",
"they",
"are",
"-",
"although",
"we",
"do",
"format",
"in",
"either",
"national",
"or",
"international",
"format",
"depending",
"on",
"what",
"the",
"client",
"asks",
"for",
"we",
"do",
"not",
"currently",
"support",
"a",
"more",
"abbreviated",
"format",
"such",
"as",
"for",
"users",
"in",
"the",
"same",
"area",
"who",
"could",
"potentially",
"dial",
"the",
"number",
"without",
"area",
"code",
".",
"Note",
"that",
"if",
"the",
"phone",
"number",
"has",
"a",
"country",
"calling",
"code",
"of",
"0",
"or",
"an",
"otherwise",
"invalid",
"country",
"calling",
"code",
"we",
"cannot",
"work",
"out",
"which",
"formatting",
"rules",
"to",
"apply",
"so",
"we",
"return",
"the",
"national",
"significant",
"number",
"with",
"no",
"formatting",
"applied",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1042-L1058 |
ttacon/libphonenumber | phonenumberutil.go | FormatWithBuf | func FormatWithBuf(
number *PhoneNumber,
numberFormat PhoneNumberFormat,
formattedNumber *builder.Builder) {
// Clear the StringBuilder first.
formattedNumber.Reset()
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if numberFormat == E164 {
// Early exit for E164 case (even if the country calling code
// is invalid) since no formatting of the national number needs
// to be applied. Extensions are not formatted.
formattedNumber.WriteString(nationalSignificantNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode,
E164,
formattedNumber)
return
} else if !hasValidCountryCallingCode(countryCallingCode) {
formattedNumber.WriteString(nationalSignificantNumber)
return
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is
// valid (which means that the region code cannot be ZZ and must
// be one of our supported region codes).
metadata := getMetadataForRegionOrCallingCode(
countryCallingCode, regionCode)
formattedNumber.WriteString(
formatNsn(nationalSignificantNumber, metadata, numberFormat))
maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode, numberFormat, formattedNumber)
} | go | func FormatWithBuf(
number *PhoneNumber,
numberFormat PhoneNumberFormat,
formattedNumber *builder.Builder) {
// Clear the StringBuilder first.
formattedNumber.Reset()
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if numberFormat == E164 {
// Early exit for E164 case (even if the country calling code
// is invalid) since no formatting of the national number needs
// to be applied. Extensions are not formatted.
formattedNumber.WriteString(nationalSignificantNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode,
E164,
formattedNumber)
return
} else if !hasValidCountryCallingCode(countryCallingCode) {
formattedNumber.WriteString(nationalSignificantNumber)
return
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is
// valid (which means that the region code cannot be ZZ and must
// be one of our supported region codes).
metadata := getMetadataForRegionOrCallingCode(
countryCallingCode, regionCode)
formattedNumber.WriteString(
formatNsn(nationalSignificantNumber, metadata, numberFormat))
maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode, numberFormat, formattedNumber)
} | [
"func",
"FormatWithBuf",
"(",
"number",
"*",
"PhoneNumber",
",",
"numberFormat",
"PhoneNumberFormat",
",",
"formattedNumber",
"*",
"builder",
".",
"Builder",
")",
"{",
"// Clear the StringBuilder first.",
"formattedNumber",
".",
"Reset",
"(",
")",
"\n",
"countryCallingCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"nationalSignificantNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n\n",
"if",
"numberFormat",
"==",
"E164",
"{",
"// Early exit for E164 case (even if the country calling code",
"// is invalid) since no formatting of the national number needs",
"// to be applied. Extensions are not formatted.",
"formattedNumber",
".",
"WriteString",
"(",
"nationalSignificantNumber",
")",
"\n",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
",",
"E164",
",",
"formattedNumber",
")",
"\n",
"return",
"\n",
"}",
"else",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCallingCode",
")",
"{",
"formattedNumber",
".",
"WriteString",
"(",
"nationalSignificantNumber",
")",
"\n",
"return",
"\n",
"}",
"\n",
"// Note GetRegionCodeForCountryCode() is used because formatting",
"// information for regions which share a country calling code is",
"// contained by only one region for performance reasons. For",
"// example, for NANPA regions it will be contained in the metadata for US.",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCallingCode",
")",
"\n",
"// Metadata cannot be null because the country calling code is",
"// valid (which means that the region code cannot be ZZ and must",
"// be one of our supported region codes).",
"metadata",
":=",
"getMetadataForRegionOrCallingCode",
"(",
"countryCallingCode",
",",
"regionCode",
")",
"\n",
"formattedNumber",
".",
"WriteString",
"(",
"formatNsn",
"(",
"nationalSignificantNumber",
",",
"metadata",
",",
"numberFormat",
")",
")",
"\n",
"maybeAppendFormattedExtension",
"(",
"number",
",",
"metadata",
",",
"numberFormat",
",",
"formattedNumber",
")",
"\n",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
",",
"numberFormat",
",",
"formattedNumber",
")",
"\n",
"}"
] | // Same as Format(PhoneNumber, PhoneNumberFormat), but accepts a mutable
// StringBuilder as a parameter to decrease object creation when invoked
// many times. | [
"Same",
"as",
"Format",
"(",
"PhoneNumber",
"PhoneNumberFormat",
")",
"but",
"accepts",
"a",
"mutable",
"StringBuilder",
"as",
"a",
"parameter",
"to",
"decrease",
"object",
"creation",
"when",
"invoked",
"many",
"times",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1063-L1101 |
ttacon/libphonenumber | phonenumberutil.go | FormatByPattern | func FormatByPattern(number *PhoneNumber,
numberFormat PhoneNumberFormat,
userDefinedFormats []*NumberFormat) string {
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For example,
// for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid
metadata := getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNumber := builder.NewBuilder(nil)
formattingPattern := chooseFormattingPatternForNumber(
userDefinedFormats, nationalSignificantNumber)
if formattingPattern == nil {
// If no pattern above is matched, we format the number as a whole.
formattedNumber.WriteString(nationalSignificantNumber)
} else {
var numFormatCopy *NumberFormat
// Before we do a replacement of the national prefix pattern
// $NP with the national prefix, we need to copy the rule so
// that subsequent replacements for different numbers have the
// appropriate national prefix.
proto.Merge(numFormatCopy, formattingPattern)
nationalPrefixFormattingRule := formattingPattern.GetNationalPrefixFormattingRule()
if len(nationalPrefixFormattingRule) > 0 {
nationalPrefix := metadata.GetNationalPrefix()
if len(nationalPrefix) > 0 {
// Replace $NP with national prefix and $FG with the
// first group ($1).
nationalPrefixFormattingRule =
NP_PATTERN.ReplaceAllString(
nationalPrefixFormattingRule, nationalPrefix)
nationalPrefixFormattingRule =
FG_PATTERN.ReplaceAllString(
nationalPrefixFormattingRule, "\\$1")
numFormatCopy.NationalPrefixFormattingRule =
&nationalPrefixFormattingRule
} else {
// We don't want to have a rule for how to format the
// national prefix if there isn't one.
numFormatCopy.NationalPrefixFormattingRule = nil
}
}
formattedNumber.WriteString(
formatNsnUsingPattern(
nationalSignificantNumber, numFormatCopy, numberFormat))
}
maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber)
prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber)
return formattedNumber.String()
} | go | func FormatByPattern(number *PhoneNumber,
numberFormat PhoneNumberFormat,
userDefinedFormats []*NumberFormat) string {
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For example,
// for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid
metadata := getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNumber := builder.NewBuilder(nil)
formattingPattern := chooseFormattingPatternForNumber(
userDefinedFormats, nationalSignificantNumber)
if formattingPattern == nil {
// If no pattern above is matched, we format the number as a whole.
formattedNumber.WriteString(nationalSignificantNumber)
} else {
var numFormatCopy *NumberFormat
// Before we do a replacement of the national prefix pattern
// $NP with the national prefix, we need to copy the rule so
// that subsequent replacements for different numbers have the
// appropriate national prefix.
proto.Merge(numFormatCopy, formattingPattern)
nationalPrefixFormattingRule := formattingPattern.GetNationalPrefixFormattingRule()
if len(nationalPrefixFormattingRule) > 0 {
nationalPrefix := metadata.GetNationalPrefix()
if len(nationalPrefix) > 0 {
// Replace $NP with national prefix and $FG with the
// first group ($1).
nationalPrefixFormattingRule =
NP_PATTERN.ReplaceAllString(
nationalPrefixFormattingRule, nationalPrefix)
nationalPrefixFormattingRule =
FG_PATTERN.ReplaceAllString(
nationalPrefixFormattingRule, "\\$1")
numFormatCopy.NationalPrefixFormattingRule =
&nationalPrefixFormattingRule
} else {
// We don't want to have a rule for how to format the
// national prefix if there isn't one.
numFormatCopy.NationalPrefixFormattingRule = nil
}
}
formattedNumber.WriteString(
formatNsnUsingPattern(
nationalSignificantNumber, numFormatCopy, numberFormat))
}
maybeAppendFormattedExtension(number, metadata, numberFormat, formattedNumber)
prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber)
return formattedNumber.String()
} | [
"func",
"FormatByPattern",
"(",
"number",
"*",
"PhoneNumber",
",",
"numberFormat",
"PhoneNumberFormat",
",",
"userDefinedFormats",
"[",
"]",
"*",
"NumberFormat",
")",
"string",
"{",
"countryCallingCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"nationalSignificantNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCallingCode",
")",
"{",
"return",
"nationalSignificantNumber",
"\n",
"}",
"\n",
"// Note GetRegionCodeForCountryCode() is used because formatting",
"// information for regions which share a country calling code is",
"// contained by only one region for performance reasons. For example,",
"// for NANPA regions it will be contained in the metadata for US.",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCallingCode",
")",
"\n",
"// Metadata cannot be null because the country calling code is valid",
"metadata",
":=",
"getMetadataForRegionOrCallingCode",
"(",
"countryCallingCode",
",",
"regionCode",
")",
"\n\n",
"formattedNumber",
":=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n\n",
"formattingPattern",
":=",
"chooseFormattingPatternForNumber",
"(",
"userDefinedFormats",
",",
"nationalSignificantNumber",
")",
"\n",
"if",
"formattingPattern",
"==",
"nil",
"{",
"// If no pattern above is matched, we format the number as a whole.",
"formattedNumber",
".",
"WriteString",
"(",
"nationalSignificantNumber",
")",
"\n",
"}",
"else",
"{",
"var",
"numFormatCopy",
"*",
"NumberFormat",
"\n",
"// Before we do a replacement of the national prefix pattern",
"// $NP with the national prefix, we need to copy the rule so",
"// that subsequent replacements for different numbers have the",
"// appropriate national prefix.",
"proto",
".",
"Merge",
"(",
"numFormatCopy",
",",
"formattingPattern",
")",
"\n",
"nationalPrefixFormattingRule",
":=",
"formattingPattern",
".",
"GetNationalPrefixFormattingRule",
"(",
")",
"\n",
"if",
"len",
"(",
"nationalPrefixFormattingRule",
")",
">",
"0",
"{",
"nationalPrefix",
":=",
"metadata",
".",
"GetNationalPrefix",
"(",
")",
"\n",
"if",
"len",
"(",
"nationalPrefix",
")",
">",
"0",
"{",
"// Replace $NP with national prefix and $FG with the",
"// first group ($1).",
"nationalPrefixFormattingRule",
"=",
"NP_PATTERN",
".",
"ReplaceAllString",
"(",
"nationalPrefixFormattingRule",
",",
"nationalPrefix",
")",
"\n",
"nationalPrefixFormattingRule",
"=",
"FG_PATTERN",
".",
"ReplaceAllString",
"(",
"nationalPrefixFormattingRule",
",",
"\"",
"\\\\",
"\"",
")",
"\n",
"numFormatCopy",
".",
"NationalPrefixFormattingRule",
"=",
"&",
"nationalPrefixFormattingRule",
"\n",
"}",
"else",
"{",
"// We don't want to have a rule for how to format the",
"// national prefix if there isn't one.",
"numFormatCopy",
".",
"NationalPrefixFormattingRule",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"formattedNumber",
".",
"WriteString",
"(",
"formatNsnUsingPattern",
"(",
"nationalSignificantNumber",
",",
"numFormatCopy",
",",
"numberFormat",
")",
")",
"\n",
"}",
"\n",
"maybeAppendFormattedExtension",
"(",
"number",
",",
"metadata",
",",
"numberFormat",
",",
"formattedNumber",
")",
"\n",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
",",
"numberFormat",
",",
"formattedNumber",
")",
"\n",
"return",
"formattedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Formats a phone number in the specified format using client-defined
// formatting rules. Note that if the phone number has a country calling
// code of zero or an otherwise invalid country calling code, we cannot
// work out things like whether there should be a national prefix applied,
// or how to format extensions, so we return the national significant
// number with no formatting applied. | [
"Formats",
"a",
"phone",
"number",
"in",
"the",
"specified",
"format",
"using",
"client",
"-",
"defined",
"formatting",
"rules",
".",
"Note",
"that",
"if",
"the",
"phone",
"number",
"has",
"a",
"country",
"calling",
"code",
"of",
"zero",
"or",
"an",
"otherwise",
"invalid",
"country",
"calling",
"code",
"we",
"cannot",
"work",
"out",
"things",
"like",
"whether",
"there",
"should",
"be",
"a",
"national",
"prefix",
"applied",
"or",
"how",
"to",
"format",
"extensions",
"so",
"we",
"return",
"the",
"national",
"significant",
"number",
"with",
"no",
"formatting",
"applied",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1109-L1167 |
ttacon/libphonenumber | phonenumberutil.go | FormatNationalNumberWithCarrierCode | func FormatNationalNumberWithCarrierCode(number *PhoneNumber, carrierCode string) string {
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid.
metadata := getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNumber := builder.NewBuilder(nil)
formattedNumber.WriteString(
formatNsnWithCarrier(
nationalSignificantNumber,
metadata,
NATIONAL,
carrierCode))
maybeAppendFormattedExtension(number, metadata, NATIONAL, formattedNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode,
NATIONAL,
formattedNumber)
return formattedNumber.String()
} | go | func FormatNationalNumberWithCarrierCode(number *PhoneNumber, carrierCode string) string {
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
// Note GetRegionCodeForCountryCode() is used because formatting
// information for regions which share a country calling code is
// contained by only one region for performance reasons. For
// example, for NANPA regions it will be contained in the metadata for US.
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid.
metadata := getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNumber := builder.NewBuilder(nil)
formattedNumber.WriteString(
formatNsnWithCarrier(
nationalSignificantNumber,
metadata,
NATIONAL,
carrierCode))
maybeAppendFormattedExtension(number, metadata, NATIONAL, formattedNumber)
prefixNumberWithCountryCallingCode(
countryCallingCode,
NATIONAL,
formattedNumber)
return formattedNumber.String()
} | [
"func",
"FormatNationalNumberWithCarrierCode",
"(",
"number",
"*",
"PhoneNumber",
",",
"carrierCode",
"string",
")",
"string",
"{",
"countryCallingCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"nationalSignificantNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCallingCode",
")",
"{",
"return",
"nationalSignificantNumber",
"\n",
"}",
"\n",
"// Note GetRegionCodeForCountryCode() is used because formatting",
"// information for regions which share a country calling code is",
"// contained by only one region for performance reasons. For",
"// example, for NANPA regions it will be contained in the metadata for US.",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCallingCode",
")",
"\n",
"// Metadata cannot be null because the country calling code is valid.",
"metadata",
":=",
"getMetadataForRegionOrCallingCode",
"(",
"countryCallingCode",
",",
"regionCode",
")",
"\n\n",
"formattedNumber",
":=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n",
"formattedNumber",
".",
"WriteString",
"(",
"formatNsnWithCarrier",
"(",
"nationalSignificantNumber",
",",
"metadata",
",",
"NATIONAL",
",",
"carrierCode",
")",
")",
"\n",
"maybeAppendFormattedExtension",
"(",
"number",
",",
"metadata",
",",
"NATIONAL",
",",
"formattedNumber",
")",
"\n",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
",",
"NATIONAL",
",",
"formattedNumber",
")",
"\n",
"return",
"formattedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Formats a phone number in national format for dialing using the carrier
// as specified in the carrierCode. The carrierCode will always be used
// regardless of whether the phone number already has a preferred domestic
// carrier code stored. If carrierCode contains an empty string, returns
// the number in national format without any carrier code. | [
"Formats",
"a",
"phone",
"number",
"in",
"national",
"format",
"for",
"dialing",
"using",
"the",
"carrier",
"as",
"specified",
"in",
"the",
"carrierCode",
".",
"The",
"carrierCode",
"will",
"always",
"be",
"used",
"regardless",
"of",
"whether",
"the",
"phone",
"number",
"already",
"has",
"a",
"preferred",
"domestic",
"carrier",
"code",
"stored",
".",
"If",
"carrierCode",
"contains",
"an",
"empty",
"string",
"returns",
"the",
"number",
"in",
"national",
"format",
"without",
"any",
"carrier",
"code",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1174-L1201 |
ttacon/libphonenumber | phonenumberutil.go | FormatNationalNumberWithPreferredCarrierCode | func FormatNationalNumberWithPreferredCarrierCode(
number *PhoneNumber,
fallbackCarrierCode string) string {
pref := number.GetPreferredDomesticCarrierCode()
if number.GetPreferredDomesticCarrierCode() == "" {
pref = fallbackCarrierCode
}
return FormatNationalNumberWithCarrierCode(number, pref)
} | go | func FormatNationalNumberWithPreferredCarrierCode(
number *PhoneNumber,
fallbackCarrierCode string) string {
pref := number.GetPreferredDomesticCarrierCode()
if number.GetPreferredDomesticCarrierCode() == "" {
pref = fallbackCarrierCode
}
return FormatNationalNumberWithCarrierCode(number, pref)
} | [
"func",
"FormatNationalNumberWithPreferredCarrierCode",
"(",
"number",
"*",
"PhoneNumber",
",",
"fallbackCarrierCode",
"string",
")",
"string",
"{",
"pref",
":=",
"number",
".",
"GetPreferredDomesticCarrierCode",
"(",
")",
"\n",
"if",
"number",
".",
"GetPreferredDomesticCarrierCode",
"(",
")",
"==",
"\"",
"\"",
"{",
"pref",
"=",
"fallbackCarrierCode",
"\n",
"}",
"\n",
"return",
"FormatNationalNumberWithCarrierCode",
"(",
"number",
",",
"pref",
")",
"\n",
"}"
] | // Formats a phone number in national format for dialing using the carrier
// as specified in the preferredDomesticCarrierCode field of the PhoneNumber
// object passed in. If that is missing, use the fallbackCarrierCode passed
// in instead. If there is no preferredDomesticCarrierCode, and the
// fallbackCarrierCode contains an empty string, return the number in
// national format without any carrier code.
//
// Use formatNationalNumberWithCarrierCode instead if the carrier code
// passed in should take precedence over the number's
// preferredDomesticCarrierCode when formatting. | [
"Formats",
"a",
"phone",
"number",
"in",
"national",
"format",
"for",
"dialing",
"using",
"the",
"carrier",
"as",
"specified",
"in",
"the",
"preferredDomesticCarrierCode",
"field",
"of",
"the",
"PhoneNumber",
"object",
"passed",
"in",
".",
"If",
"that",
"is",
"missing",
"use",
"the",
"fallbackCarrierCode",
"passed",
"in",
"instead",
".",
"If",
"there",
"is",
"no",
"preferredDomesticCarrierCode",
"and",
"the",
"fallbackCarrierCode",
"contains",
"an",
"empty",
"string",
"return",
"the",
"number",
"in",
"national",
"format",
"without",
"any",
"carrier",
"code",
".",
"Use",
"formatNationalNumberWithCarrierCode",
"instead",
"if",
"the",
"carrier",
"code",
"passed",
"in",
"should",
"take",
"precedence",
"over",
"the",
"number",
"s",
"preferredDomesticCarrierCode",
"when",
"formatting",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1221-L1230 |
ttacon/libphonenumber | phonenumberutil.go | FormatNumberForMobileDialing | func FormatNumberForMobileDialing(
number *PhoneNumber,
regionCallingFrom string,
withFormatting bool) string {
countryCallingCode := int(number.GetCountryCode())
if !hasValidCountryCallingCode(countryCallingCode) {
return number.GetRawInput() // go impl defaults to ""
}
formattedNumber := ""
// Clear the extension, as that part cannot normally be dialed
// together with the main number.
var numberNoExt = &PhoneNumber{}
proto.Merge(numberNoExt, number)
numberNoExt.Extension = nil // can we assume this is safe? (no nil-pointer?)
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
numberType := GetNumberType(numberNoExt)
isValidNumber := numberType != UNKNOWN
if regionCallingFrom == regionCode {
isFixedLineOrMobile :=
numberType == FIXED_LINE ||
numberType == MOBILE ||
numberType == FIXED_LINE_OR_MOBILE
// Carrier codes may be needed in some countries. We handle this here.
if regionCode == "CO" && numberType == FIXED_LINE {
formattedNumber =
FormatNationalNumberWithCarrierCode(
numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX)
} else if regionCode == "BR" && isFixedLineOrMobile {
if numberNoExt.GetPreferredDomesticCarrierCode() != "" {
formattedNumber =
FormatNationalNumberWithPreferredCarrierCode(numberNoExt, "")
} else {
// Brazilian fixed line and mobile numbers need to be dialed
// with a carrier code when called within Brazil. Without
// that, most of the carriers won't connect the call.
// Because of that, we return an empty string here.
formattedNumber = ""
}
} else if isValidNumber && regionCode == "HU" {
// The national format for HU numbers doesn't contain the
// national prefix, because that is how numbers are normally
// written down. However, the national prefix is obligatory when
// dialing from a mobile phone, except for short numbers. As a
// result, we add it back here
// if it is a valid regular length phone number.
formattedNumber =
GetNddPrefixForRegion(regionCode, true /* strip non-digits */) +
" " + Format(numberNoExt, NATIONAL)
} else if countryCallingCode == NANPA_COUNTRY_CODE {
// For NANPA countries, we output international format for
// numbers that can be dialed internationally, since that
// always works, except for numbers which might potentially be
// short numbers, which are always dialled in national format.
regionMetadata := getMetadataForRegion(regionCallingFrom)
if canBeInternationallyDialled(numberNoExt) &&
!isShorterThanPossibleNormalNumber(regionMetadata,
GetNationalSignificantNumber(numberNoExt)) {
formattedNumber = Format(numberNoExt, INTERNATIONAL)
} else {
formattedNumber = Format(numberNoExt, NATIONAL)
}
} else {
// For non-geographical countries, and Mexican and Chilean fixed
// line and mobile numbers, we output international format for
// numbers that can be dialed internationally as that always
// works.
// MX fixed line and mobile numbers should always be formatted
// in international format, even when dialed within MX. For
// national format to work, a carrier code needs to be used,
// and the correct carrier code depends on if the caller and
// callee are from the same local area. It is trickier to get
// that to work correctly than using international format, which
// is tested to work fine on all carriers. CL fixed line
// numbers need the national prefix when dialing in the national
// format, but don't have it when used for display. The reverse
// is true for mobile numbers. As a result, we output them in
// the international format to make it work.
if regionCode == REGION_CODE_FOR_NON_GEO_ENTITY ||
((regionCode == "MX" ||
regionCode == "CL") &&
isFixedLineOrMobile) &&
canBeInternationallyDialled(numberNoExt) {
formattedNumber = Format(numberNoExt, INTERNATIONAL)
} else {
formattedNumber = Format(numberNoExt, NATIONAL)
}
}
} else if isValidNumber && canBeInternationallyDialled(numberNoExt) {
// We assume that short numbers are not diallable from outside
// their region, so if a number is not a valid regular length
// phone number, we treat it as if it cannot be internationally
// dialled.
if withFormatting {
return Format(numberNoExt, INTERNATIONAL)
}
return Format(numberNoExt, E164)
}
if withFormatting {
return formattedNumber
}
return normalizeDiallableCharsOnly(formattedNumber)
} | go | func FormatNumberForMobileDialing(
number *PhoneNumber,
regionCallingFrom string,
withFormatting bool) string {
countryCallingCode := int(number.GetCountryCode())
if !hasValidCountryCallingCode(countryCallingCode) {
return number.GetRawInput() // go impl defaults to ""
}
formattedNumber := ""
// Clear the extension, as that part cannot normally be dialed
// together with the main number.
var numberNoExt = &PhoneNumber{}
proto.Merge(numberNoExt, number)
numberNoExt.Extension = nil // can we assume this is safe? (no nil-pointer?)
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
numberType := GetNumberType(numberNoExt)
isValidNumber := numberType != UNKNOWN
if regionCallingFrom == regionCode {
isFixedLineOrMobile :=
numberType == FIXED_LINE ||
numberType == MOBILE ||
numberType == FIXED_LINE_OR_MOBILE
// Carrier codes may be needed in some countries. We handle this here.
if regionCode == "CO" && numberType == FIXED_LINE {
formattedNumber =
FormatNationalNumberWithCarrierCode(
numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX)
} else if regionCode == "BR" && isFixedLineOrMobile {
if numberNoExt.GetPreferredDomesticCarrierCode() != "" {
formattedNumber =
FormatNationalNumberWithPreferredCarrierCode(numberNoExt, "")
} else {
// Brazilian fixed line and mobile numbers need to be dialed
// with a carrier code when called within Brazil. Without
// that, most of the carriers won't connect the call.
// Because of that, we return an empty string here.
formattedNumber = ""
}
} else if isValidNumber && regionCode == "HU" {
// The national format for HU numbers doesn't contain the
// national prefix, because that is how numbers are normally
// written down. However, the national prefix is obligatory when
// dialing from a mobile phone, except for short numbers. As a
// result, we add it back here
// if it is a valid regular length phone number.
formattedNumber =
GetNddPrefixForRegion(regionCode, true /* strip non-digits */) +
" " + Format(numberNoExt, NATIONAL)
} else if countryCallingCode == NANPA_COUNTRY_CODE {
// For NANPA countries, we output international format for
// numbers that can be dialed internationally, since that
// always works, except for numbers which might potentially be
// short numbers, which are always dialled in national format.
regionMetadata := getMetadataForRegion(regionCallingFrom)
if canBeInternationallyDialled(numberNoExt) &&
!isShorterThanPossibleNormalNumber(regionMetadata,
GetNationalSignificantNumber(numberNoExt)) {
formattedNumber = Format(numberNoExt, INTERNATIONAL)
} else {
formattedNumber = Format(numberNoExt, NATIONAL)
}
} else {
// For non-geographical countries, and Mexican and Chilean fixed
// line and mobile numbers, we output international format for
// numbers that can be dialed internationally as that always
// works.
// MX fixed line and mobile numbers should always be formatted
// in international format, even when dialed within MX. For
// national format to work, a carrier code needs to be used,
// and the correct carrier code depends on if the caller and
// callee are from the same local area. It is trickier to get
// that to work correctly than using international format, which
// is tested to work fine on all carriers. CL fixed line
// numbers need the national prefix when dialing in the national
// format, but don't have it when used for display. The reverse
// is true for mobile numbers. As a result, we output them in
// the international format to make it work.
if regionCode == REGION_CODE_FOR_NON_GEO_ENTITY ||
((regionCode == "MX" ||
regionCode == "CL") &&
isFixedLineOrMobile) &&
canBeInternationallyDialled(numberNoExt) {
formattedNumber = Format(numberNoExt, INTERNATIONAL)
} else {
formattedNumber = Format(numberNoExt, NATIONAL)
}
}
} else if isValidNumber && canBeInternationallyDialled(numberNoExt) {
// We assume that short numbers are not diallable from outside
// their region, so if a number is not a valid regular length
// phone number, we treat it as if it cannot be internationally
// dialled.
if withFormatting {
return Format(numberNoExt, INTERNATIONAL)
}
return Format(numberNoExt, E164)
}
if withFormatting {
return formattedNumber
}
return normalizeDiallableCharsOnly(formattedNumber)
} | [
"func",
"FormatNumberForMobileDialing",
"(",
"number",
"*",
"PhoneNumber",
",",
"regionCallingFrom",
"string",
",",
"withFormatting",
"bool",
")",
"string",
"{",
"countryCallingCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCallingCode",
")",
"{",
"return",
"number",
".",
"GetRawInput",
"(",
")",
"// go impl defaults to \"\"",
"\n",
"}",
"\n\n",
"formattedNumber",
":=",
"\"",
"\"",
"\n",
"// Clear the extension, as that part cannot normally be dialed",
"// together with the main number.",
"var",
"numberNoExt",
"=",
"&",
"PhoneNumber",
"{",
"}",
"\n",
"proto",
".",
"Merge",
"(",
"numberNoExt",
",",
"number",
")",
"\n",
"numberNoExt",
".",
"Extension",
"=",
"nil",
"// can we assume this is safe? (no nil-pointer?)",
"\n",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCallingCode",
")",
"\n",
"numberType",
":=",
"GetNumberType",
"(",
"numberNoExt",
")",
"\n",
"isValidNumber",
":=",
"numberType",
"!=",
"UNKNOWN",
"\n",
"if",
"regionCallingFrom",
"==",
"regionCode",
"{",
"isFixedLineOrMobile",
":=",
"numberType",
"==",
"FIXED_LINE",
"||",
"numberType",
"==",
"MOBILE",
"||",
"numberType",
"==",
"FIXED_LINE_OR_MOBILE",
"\n",
"// Carrier codes may be needed in some countries. We handle this here.",
"if",
"regionCode",
"==",
"\"",
"\"",
"&&",
"numberType",
"==",
"FIXED_LINE",
"{",
"formattedNumber",
"=",
"FormatNationalNumberWithCarrierCode",
"(",
"numberNoExt",
",",
"COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX",
")",
"\n",
"}",
"else",
"if",
"regionCode",
"==",
"\"",
"\"",
"&&",
"isFixedLineOrMobile",
"{",
"if",
"numberNoExt",
".",
"GetPreferredDomesticCarrierCode",
"(",
")",
"!=",
"\"",
"\"",
"{",
"formattedNumber",
"=",
"FormatNationalNumberWithPreferredCarrierCode",
"(",
"numberNoExt",
",",
"\"",
"\"",
")",
"\n",
"}",
"else",
"{",
"// Brazilian fixed line and mobile numbers need to be dialed",
"// with a carrier code when called within Brazil. Without",
"// that, most of the carriers won't connect the call.",
"// Because of that, we return an empty string here.",
"formattedNumber",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"}",
"else",
"if",
"isValidNumber",
"&&",
"regionCode",
"==",
"\"",
"\"",
"{",
"// The national format for HU numbers doesn't contain the",
"// national prefix, because that is how numbers are normally",
"// written down. However, the national prefix is obligatory when",
"// dialing from a mobile phone, except for short numbers. As a",
"// result, we add it back here",
"// if it is a valid regular length phone number.",
"formattedNumber",
"=",
"GetNddPrefixForRegion",
"(",
"regionCode",
",",
"true",
"/* strip non-digits */",
")",
"+",
"\"",
"\"",
"+",
"Format",
"(",
"numberNoExt",
",",
"NATIONAL",
")",
"\n",
"}",
"else",
"if",
"countryCallingCode",
"==",
"NANPA_COUNTRY_CODE",
"{",
"// For NANPA countries, we output international format for",
"// numbers that can be dialed internationally, since that",
"// always works, except for numbers which might potentially be",
"// short numbers, which are always dialled in national format.",
"regionMetadata",
":=",
"getMetadataForRegion",
"(",
"regionCallingFrom",
")",
"\n",
"if",
"canBeInternationallyDialled",
"(",
"numberNoExt",
")",
"&&",
"!",
"isShorterThanPossibleNormalNumber",
"(",
"regionMetadata",
",",
"GetNationalSignificantNumber",
"(",
"numberNoExt",
")",
")",
"{",
"formattedNumber",
"=",
"Format",
"(",
"numberNoExt",
",",
"INTERNATIONAL",
")",
"\n",
"}",
"else",
"{",
"formattedNumber",
"=",
"Format",
"(",
"numberNoExt",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"// For non-geographical countries, and Mexican and Chilean fixed",
"// line and mobile numbers, we output international format for",
"// numbers that can be dialed internationally as that always",
"// works.",
"// MX fixed line and mobile numbers should always be formatted",
"// in international format, even when dialed within MX. For",
"// national format to work, a carrier code needs to be used,",
"// and the correct carrier code depends on if the caller and",
"// callee are from the same local area. It is trickier to get",
"// that to work correctly than using international format, which",
"// is tested to work fine on all carriers. CL fixed line",
"// numbers need the national prefix when dialing in the national",
"// format, but don't have it when used for display. The reverse",
"// is true for mobile numbers. As a result, we output them in",
"// the international format to make it work.",
"if",
"regionCode",
"==",
"REGION_CODE_FOR_NON_GEO_ENTITY",
"||",
"(",
"(",
"regionCode",
"==",
"\"",
"\"",
"||",
"regionCode",
"==",
"\"",
"\"",
")",
"&&",
"isFixedLineOrMobile",
")",
"&&",
"canBeInternationallyDialled",
"(",
"numberNoExt",
")",
"{",
"formattedNumber",
"=",
"Format",
"(",
"numberNoExt",
",",
"INTERNATIONAL",
")",
"\n",
"}",
"else",
"{",
"formattedNumber",
"=",
"Format",
"(",
"numberNoExt",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"else",
"if",
"isValidNumber",
"&&",
"canBeInternationallyDialled",
"(",
"numberNoExt",
")",
"{",
"// We assume that short numbers are not diallable from outside",
"// their region, so if a number is not a valid regular length",
"// phone number, we treat it as if it cannot be internationally",
"// dialled.",
"if",
"withFormatting",
"{",
"return",
"Format",
"(",
"numberNoExt",
",",
"INTERNATIONAL",
")",
"\n",
"}",
"\n",
"return",
"Format",
"(",
"numberNoExt",
",",
"E164",
")",
"\n",
"}",
"\n",
"if",
"withFormatting",
"{",
"return",
"formattedNumber",
"\n",
"}",
"\n",
"return",
"normalizeDiallableCharsOnly",
"(",
"formattedNumber",
")",
"\n",
"}"
] | // Returns a number formatted in such a way that it can be dialed from a
// mobile phone in a specific region. If the number cannot be reached from
// the region (e.g. some countries block toll-free numbers from being
// called outside of the country), the method returns an empty string. | [
"Returns",
"a",
"number",
"formatted",
"in",
"such",
"a",
"way",
"that",
"it",
"can",
"be",
"dialed",
"from",
"a",
"mobile",
"phone",
"in",
"a",
"specific",
"region",
".",
"If",
"the",
"number",
"cannot",
"be",
"reached",
"from",
"the",
"region",
"(",
"e",
".",
"g",
".",
"some",
"countries",
"block",
"toll",
"-",
"free",
"numbers",
"from",
"being",
"called",
"outside",
"of",
"the",
"country",
")",
"the",
"method",
"returns",
"an",
"empty",
"string",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1236-L1340 |
ttacon/libphonenumber | phonenumberutil.go | FormatOutOfCountryCallingNumber | func FormatOutOfCountryCallingNumber(
number *PhoneNumber,
regionCallingFrom string) string {
if !isValidRegionCode(regionCallingFrom) {
return Format(number, INTERNATIONAL)
}
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
if countryCallingCode == NANPA_COUNTRY_CODE {
if IsNANPACountry(regionCallingFrom) {
// For NANPA regions, return the national format for these
// regions but prefix it with the country calling code.
return strconv.Itoa(countryCallingCode) + " " + Format(number, NATIONAL)
}
} else if countryCallingCode == getCountryCodeForValidRegion(regionCallingFrom) {
// If regions share a country calling code, the country calling
// code need not be dialled. This also applies when dialling
// within a region, so this if clause covers both these cases.
// Technically this is the case for dialling from La Reunion to
// other overseas departments of France (French Guiana, Martinique,
// Guadeloupe), but not vice versa - so we don't cover this edge
// case for now and for those cases return the version including
// country calling code.
// Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion
return Format(number, NATIONAL)
}
// Metadata cannot be null because we checked 'isValidRegionCode()' above.
metadataForRegionCallingFrom := getMetadataForRegion(regionCallingFrom)
internationalPrefix := metadataForRegionCallingFrom.GetInternationalPrefix()
// For regions that have multiple international prefixes, the
// international format of the number is returned, unless there is
// a preferred international prefix.
internationalPrefixForFormatting := ""
metPref := metadataForRegionCallingFrom.GetPreferredInternationalPrefix()
if UNIQUE_INTERNATIONAL_PREFIX.MatchString(internationalPrefix) {
internationalPrefixForFormatting = internationalPrefix
} else if metPref != "" {
internationalPrefixForFormatting = metPref
}
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid.
metadataForRegion :=
getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNationalNumber :=
formatNsn(
nationalSignificantNumber, metadataForRegion, INTERNATIONAL)
formattedNumber := builder.NewBuilder([]byte(formattedNationalNumber))
maybeAppendFormattedExtension(number, metadataForRegion, INTERNATIONAL,
formattedNumber)
if len(internationalPrefixForFormatting) > 0 {
formattedBytes := formattedNumber.Bytes()
formattedBytes = append([]byte(" "), formattedBytes...)
// we know countryCallingCode is really an int32
intBuf := []byte{
byte(countryCallingCode >> 24),
byte(countryCallingCode >> 16),
byte(countryCallingCode >> 8),
byte(countryCallingCode),
}
formattedBytes = append(intBuf, formattedBytes...)
formattedBytes = append([]byte(" "), formattedBytes...)
formattedBytes = append(
[]byte(internationalPrefixForFormatting), formattedBytes...)
return string(formattedBytes)
} else {
prefixNumberWithCountryCallingCode(
countryCallingCode, INTERNATIONAL, formattedNumber)
}
return formattedNumber.String()
} | go | func FormatOutOfCountryCallingNumber(
number *PhoneNumber,
regionCallingFrom string) string {
if !isValidRegionCode(regionCallingFrom) {
return Format(number, INTERNATIONAL)
}
countryCallingCode := int(number.GetCountryCode())
nationalSignificantNumber := GetNationalSignificantNumber(number)
if !hasValidCountryCallingCode(countryCallingCode) {
return nationalSignificantNumber
}
if countryCallingCode == NANPA_COUNTRY_CODE {
if IsNANPACountry(regionCallingFrom) {
// For NANPA regions, return the national format for these
// regions but prefix it with the country calling code.
return strconv.Itoa(countryCallingCode) + " " + Format(number, NATIONAL)
}
} else if countryCallingCode == getCountryCodeForValidRegion(regionCallingFrom) {
// If regions share a country calling code, the country calling
// code need not be dialled. This also applies when dialling
// within a region, so this if clause covers both these cases.
// Technically this is the case for dialling from La Reunion to
// other overseas departments of France (French Guiana, Martinique,
// Guadeloupe), but not vice versa - so we don't cover this edge
// case for now and for those cases return the version including
// country calling code.
// Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion
return Format(number, NATIONAL)
}
// Metadata cannot be null because we checked 'isValidRegionCode()' above.
metadataForRegionCallingFrom := getMetadataForRegion(regionCallingFrom)
internationalPrefix := metadataForRegionCallingFrom.GetInternationalPrefix()
// For regions that have multiple international prefixes, the
// international format of the number is returned, unless there is
// a preferred international prefix.
internationalPrefixForFormatting := ""
metPref := metadataForRegionCallingFrom.GetPreferredInternationalPrefix()
if UNIQUE_INTERNATIONAL_PREFIX.MatchString(internationalPrefix) {
internationalPrefixForFormatting = internationalPrefix
} else if metPref != "" {
internationalPrefixForFormatting = metPref
}
regionCode := GetRegionCodeForCountryCode(countryCallingCode)
// Metadata cannot be null because the country calling code is valid.
metadataForRegion :=
getMetadataForRegionOrCallingCode(countryCallingCode, regionCode)
formattedNationalNumber :=
formatNsn(
nationalSignificantNumber, metadataForRegion, INTERNATIONAL)
formattedNumber := builder.NewBuilder([]byte(formattedNationalNumber))
maybeAppendFormattedExtension(number, metadataForRegion, INTERNATIONAL,
formattedNumber)
if len(internationalPrefixForFormatting) > 0 {
formattedBytes := formattedNumber.Bytes()
formattedBytes = append([]byte(" "), formattedBytes...)
// we know countryCallingCode is really an int32
intBuf := []byte{
byte(countryCallingCode >> 24),
byte(countryCallingCode >> 16),
byte(countryCallingCode >> 8),
byte(countryCallingCode),
}
formattedBytes = append(intBuf, formattedBytes...)
formattedBytes = append([]byte(" "), formattedBytes...)
formattedBytes = append(
[]byte(internationalPrefixForFormatting), formattedBytes...)
return string(formattedBytes)
} else {
prefixNumberWithCountryCallingCode(
countryCallingCode, INTERNATIONAL, formattedNumber)
}
return formattedNumber.String()
} | [
"func",
"FormatOutOfCountryCallingNumber",
"(",
"number",
"*",
"PhoneNumber",
",",
"regionCallingFrom",
"string",
")",
"string",
"{",
"if",
"!",
"isValidRegionCode",
"(",
"regionCallingFrom",
")",
"{",
"return",
"Format",
"(",
"number",
",",
"INTERNATIONAL",
")",
"\n",
"}",
"\n",
"countryCallingCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"nationalSignificantNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCallingCode",
")",
"{",
"return",
"nationalSignificantNumber",
"\n",
"}",
"\n",
"if",
"countryCallingCode",
"==",
"NANPA_COUNTRY_CODE",
"{",
"if",
"IsNANPACountry",
"(",
"regionCallingFrom",
")",
"{",
"// For NANPA regions, return the national format for these",
"// regions but prefix it with the country calling code.",
"return",
"strconv",
".",
"Itoa",
"(",
"countryCallingCode",
")",
"+",
"\"",
"\"",
"+",
"Format",
"(",
"number",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"}",
"else",
"if",
"countryCallingCode",
"==",
"getCountryCodeForValidRegion",
"(",
"regionCallingFrom",
")",
"{",
"// If regions share a country calling code, the country calling",
"// code need not be dialled. This also applies when dialling",
"// within a region, so this if clause covers both these cases.",
"// Technically this is the case for dialling from La Reunion to",
"// other overseas departments of France (French Guiana, Martinique,",
"// Guadeloupe), but not vice versa - so we don't cover this edge",
"// case for now and for those cases return the version including",
"// country calling code.",
"// Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion",
"return",
"Format",
"(",
"number",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"// Metadata cannot be null because we checked 'isValidRegionCode()' above.",
"metadataForRegionCallingFrom",
":=",
"getMetadataForRegion",
"(",
"regionCallingFrom",
")",
"\n",
"internationalPrefix",
":=",
"metadataForRegionCallingFrom",
".",
"GetInternationalPrefix",
"(",
")",
"\n\n",
"// For regions that have multiple international prefixes, the",
"// international format of the number is returned, unless there is",
"// a preferred international prefix.",
"internationalPrefixForFormatting",
":=",
"\"",
"\"",
"\n",
"metPref",
":=",
"metadataForRegionCallingFrom",
".",
"GetPreferredInternationalPrefix",
"(",
")",
"\n",
"if",
"UNIQUE_INTERNATIONAL_PREFIX",
".",
"MatchString",
"(",
"internationalPrefix",
")",
"{",
"internationalPrefixForFormatting",
"=",
"internationalPrefix",
"\n",
"}",
"else",
"if",
"metPref",
"!=",
"\"",
"\"",
"{",
"internationalPrefixForFormatting",
"=",
"metPref",
"\n",
"}",
"\n\n",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCallingCode",
")",
"\n",
"// Metadata cannot be null because the country calling code is valid.",
"metadataForRegion",
":=",
"getMetadataForRegionOrCallingCode",
"(",
"countryCallingCode",
",",
"regionCode",
")",
"\n",
"formattedNationalNumber",
":=",
"formatNsn",
"(",
"nationalSignificantNumber",
",",
"metadataForRegion",
",",
"INTERNATIONAL",
")",
"\n",
"formattedNumber",
":=",
"builder",
".",
"NewBuilder",
"(",
"[",
"]",
"byte",
"(",
"formattedNationalNumber",
")",
")",
"\n",
"maybeAppendFormattedExtension",
"(",
"number",
",",
"metadataForRegion",
",",
"INTERNATIONAL",
",",
"formattedNumber",
")",
"\n",
"if",
"len",
"(",
"internationalPrefixForFormatting",
")",
">",
"0",
"{",
"formattedBytes",
":=",
"formattedNumber",
".",
"Bytes",
"(",
")",
"\n",
"formattedBytes",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"formattedBytes",
"...",
")",
"\n",
"// we know countryCallingCode is really an int32",
"intBuf",
":=",
"[",
"]",
"byte",
"{",
"byte",
"(",
"countryCallingCode",
">>",
"24",
")",
",",
"byte",
"(",
"countryCallingCode",
">>",
"16",
")",
",",
"byte",
"(",
"countryCallingCode",
">>",
"8",
")",
",",
"byte",
"(",
"countryCallingCode",
")",
",",
"}",
"\n",
"formattedBytes",
"=",
"append",
"(",
"intBuf",
",",
"formattedBytes",
"...",
")",
"\n",
"formattedBytes",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"formattedBytes",
"...",
")",
"\n",
"formattedBytes",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"internationalPrefixForFormatting",
")",
",",
"formattedBytes",
"...",
")",
"\n",
"return",
"string",
"(",
"formattedBytes",
")",
"\n",
"}",
"else",
"{",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
",",
"INTERNATIONAL",
",",
"formattedNumber",
")",
"\n",
"}",
"\n",
"return",
"formattedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Formats a phone number for out-of-country dialing purposes. If no
// regionCallingFrom is supplied, we format the number in its
// INTERNATIONAL format. If the country calling code is the same as that
// of the region where the number is from, then NATIONAL formatting will
// be applied.
//
// If the number itself has a country calling code of zero or an otherwise
// invalid country calling code, then we return the number with no
// formatting applied.
//
// Note this function takes care of the case for calling inside of NANPA and
// between Russia and Kazakhstan (who share the same country calling code).
// In those cases, no international prefix is used. For regions which have
// multiple international prefixes, the number in its INTERNATIONAL format
// will be returned instead. | [
"Formats",
"a",
"phone",
"number",
"for",
"out",
"-",
"of",
"-",
"country",
"dialing",
"purposes",
".",
"If",
"no",
"regionCallingFrom",
"is",
"supplied",
"we",
"format",
"the",
"number",
"in",
"its",
"INTERNATIONAL",
"format",
".",
"If",
"the",
"country",
"calling",
"code",
"is",
"the",
"same",
"as",
"that",
"of",
"the",
"region",
"where",
"the",
"number",
"is",
"from",
"then",
"NATIONAL",
"formatting",
"will",
"be",
"applied",
".",
"If",
"the",
"number",
"itself",
"has",
"a",
"country",
"calling",
"code",
"of",
"zero",
"or",
"an",
"otherwise",
"invalid",
"country",
"calling",
"code",
"then",
"we",
"return",
"the",
"number",
"with",
"no",
"formatting",
"applied",
".",
"Note",
"this",
"function",
"takes",
"care",
"of",
"the",
"case",
"for",
"calling",
"inside",
"of",
"NANPA",
"and",
"between",
"Russia",
"and",
"Kazakhstan",
"(",
"who",
"share",
"the",
"same",
"country",
"calling",
"code",
")",
".",
"In",
"those",
"cases",
"no",
"international",
"prefix",
"is",
"used",
".",
"For",
"regions",
"which",
"have",
"multiple",
"international",
"prefixes",
"the",
"number",
"in",
"its",
"INTERNATIONAL",
"format",
"will",
"be",
"returned",
"instead",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1357-L1432 |
ttacon/libphonenumber | phonenumberutil.go | FormatInOriginalFormat | func FormatInOriginalFormat(number *PhoneNumber, regionCallingFrom string) string {
rawInput := number.GetRawInput()
if len(rawInput) == 0 &&
(hasUnexpectedItalianLeadingZero(number) ||
!hasFormattingPatternForNumber(number)) {
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix.
return rawInput
}
if number.GetCountryCodeSource() == 0 {
return Format(number, NATIONAL)
}
var formattedNumber string
switch number.GetCountryCodeSource() {
case PhoneNumber_FROM_NUMBER_WITH_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)
case PhoneNumber_FROM_NUMBER_WITH_IDD:
formattedNumber = FormatOutOfCountryCallingNumber(number, regionCallingFrom)
case PhoneNumber_FROM_NUMBER_WITHOUT_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)[1:]
case PhoneNumber_FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
fallthrough
default:
regionCode := GetRegionCodeForCountryCode(int(number.GetCountryCode()))
// We strip non-digits from the NDD here, and from the raw
// input later, so that we can compare them easily.
nationalPrefix := GetNddPrefixForRegion(
regionCode, true /* strip non-digits */)
nationalFormat := Format(number, NATIONAL)
if len(nationalPrefix) == 0 {
// If the region doesn't have a national prefix at all,
// we can safely return the national format without worrying
// about a national prefix being added.
formattedNumber = nationalFormat
break
}
// Otherwise, we check if the original number was entered with
// a national prefix.
if rawInputContainsNationalPrefix(rawInput, nationalPrefix, regionCode) {
// If so, we can safely return the national format.
formattedNumber = nationalFormat
}
// Metadata cannot be null here because GetNddPrefixForRegion()
// (above) returns null if there is no metadata for the region.
metadata := getMetadataForRegion(regionCode)
nationalNumber := GetNationalSignificantNumber(number)
formatRule :=
chooseFormattingPatternForNumber(metadata.GetNumberFormat(), nationalNumber)
// The format rule could still be null here if the national
// number was 0 and there was no raw input (this should not
// be possible for numbers generated by the phonenumber library
// as they would also not have a country calling code and we
// would have exited earlier).
if formatRule == nil {
formattedNumber = nationalFormat
break
}
// When the format we apply to this number doesn't contain
// national prefix, we can just return the national format.
// TODO: Refactor the code below with the code in
// isNationalPrefixPresentIfRequired.
candidateNationalPrefixRule := formatRule.GetNationalPrefixFormattingRule()
// We assume that the first-group symbol will never be _before_
// the national prefix.
indexOfFirstGroup := strings.Index(candidateNationalPrefixRule, "$1")
if indexOfFirstGroup <= 0 {
formattedNumber = nationalFormat
break
}
candidateNationalPrefixRule =
candidateNationalPrefixRule[0:indexOfFirstGroup]
candidateNationalPrefixRule = NormalizeDigitsOnly(candidateNationalPrefixRule)
if len(candidateNationalPrefixRule) == 0 {
// National prefix not used when formatting this number.
formattedNumber = nationalFormat
break
}
// Otherwise, we need to remove the national prefix from our output.
var numFormatCopy *NumberFormat
proto.Merge(numFormatCopy, formatRule)
numFormatCopy.NationalPrefixFormattingRule = nil
var numberFormats = []*NumberFormat{numFormatCopy}
formattedNumber = FormatByPattern(number, NATIONAL, numberFormats)
break
}
rawInput = number.GetRawInput()
// If no digit is inserted/removed/modified as a result of our
// formatting, we return the formatted phone number; otherwise we
// return the raw input the user entered.
if len(formattedNumber) != 0 && len(rawInput) > 0 {
normalizedFormattedNumber := normalizeDiallableCharsOnly(formattedNumber)
normalizedRawInput := normalizeDiallableCharsOnly(rawInput)
if normalizedFormattedNumber != normalizedRawInput {
formattedNumber = rawInput
}
}
return formattedNumber
} | go | func FormatInOriginalFormat(number *PhoneNumber, regionCallingFrom string) string {
rawInput := number.GetRawInput()
if len(rawInput) == 0 &&
(hasUnexpectedItalianLeadingZero(number) ||
!hasFormattingPatternForNumber(number)) {
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix.
return rawInput
}
if number.GetCountryCodeSource() == 0 {
return Format(number, NATIONAL)
}
var formattedNumber string
switch number.GetCountryCodeSource() {
case PhoneNumber_FROM_NUMBER_WITH_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)
case PhoneNumber_FROM_NUMBER_WITH_IDD:
formattedNumber = FormatOutOfCountryCallingNumber(number, regionCallingFrom)
case PhoneNumber_FROM_NUMBER_WITHOUT_PLUS_SIGN:
formattedNumber = Format(number, INTERNATIONAL)[1:]
case PhoneNumber_FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
fallthrough
default:
regionCode := GetRegionCodeForCountryCode(int(number.GetCountryCode()))
// We strip non-digits from the NDD here, and from the raw
// input later, so that we can compare them easily.
nationalPrefix := GetNddPrefixForRegion(
regionCode, true /* strip non-digits */)
nationalFormat := Format(number, NATIONAL)
if len(nationalPrefix) == 0 {
// If the region doesn't have a national prefix at all,
// we can safely return the national format without worrying
// about a national prefix being added.
formattedNumber = nationalFormat
break
}
// Otherwise, we check if the original number was entered with
// a national prefix.
if rawInputContainsNationalPrefix(rawInput, nationalPrefix, regionCode) {
// If so, we can safely return the national format.
formattedNumber = nationalFormat
}
// Metadata cannot be null here because GetNddPrefixForRegion()
// (above) returns null if there is no metadata for the region.
metadata := getMetadataForRegion(regionCode)
nationalNumber := GetNationalSignificantNumber(number)
formatRule :=
chooseFormattingPatternForNumber(metadata.GetNumberFormat(), nationalNumber)
// The format rule could still be null here if the national
// number was 0 and there was no raw input (this should not
// be possible for numbers generated by the phonenumber library
// as they would also not have a country calling code and we
// would have exited earlier).
if formatRule == nil {
formattedNumber = nationalFormat
break
}
// When the format we apply to this number doesn't contain
// national prefix, we can just return the national format.
// TODO: Refactor the code below with the code in
// isNationalPrefixPresentIfRequired.
candidateNationalPrefixRule := formatRule.GetNationalPrefixFormattingRule()
// We assume that the first-group symbol will never be _before_
// the national prefix.
indexOfFirstGroup := strings.Index(candidateNationalPrefixRule, "$1")
if indexOfFirstGroup <= 0 {
formattedNumber = nationalFormat
break
}
candidateNationalPrefixRule =
candidateNationalPrefixRule[0:indexOfFirstGroup]
candidateNationalPrefixRule = NormalizeDigitsOnly(candidateNationalPrefixRule)
if len(candidateNationalPrefixRule) == 0 {
// National prefix not used when formatting this number.
formattedNumber = nationalFormat
break
}
// Otherwise, we need to remove the national prefix from our output.
var numFormatCopy *NumberFormat
proto.Merge(numFormatCopy, formatRule)
numFormatCopy.NationalPrefixFormattingRule = nil
var numberFormats = []*NumberFormat{numFormatCopy}
formattedNumber = FormatByPattern(number, NATIONAL, numberFormats)
break
}
rawInput = number.GetRawInput()
// If no digit is inserted/removed/modified as a result of our
// formatting, we return the formatted phone number; otherwise we
// return the raw input the user entered.
if len(formattedNumber) != 0 && len(rawInput) > 0 {
normalizedFormattedNumber := normalizeDiallableCharsOnly(formattedNumber)
normalizedRawInput := normalizeDiallableCharsOnly(rawInput)
if normalizedFormattedNumber != normalizedRawInput {
formattedNumber = rawInput
}
}
return formattedNumber
} | [
"func",
"FormatInOriginalFormat",
"(",
"number",
"*",
"PhoneNumber",
",",
"regionCallingFrom",
"string",
")",
"string",
"{",
"rawInput",
":=",
"number",
".",
"GetRawInput",
"(",
")",
"\n",
"if",
"len",
"(",
"rawInput",
")",
"==",
"0",
"&&",
"(",
"hasUnexpectedItalianLeadingZero",
"(",
"number",
")",
"||",
"!",
"hasFormattingPatternForNumber",
"(",
"number",
")",
")",
"{",
"// We check if we have the formatting pattern because without that, we might format the number",
"// as a group without national prefix.",
"return",
"rawInput",
"\n",
"}",
"\n",
"if",
"number",
".",
"GetCountryCodeSource",
"(",
")",
"==",
"0",
"{",
"return",
"Format",
"(",
"number",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"var",
"formattedNumber",
"string",
"\n",
"switch",
"number",
".",
"GetCountryCodeSource",
"(",
")",
"{",
"case",
"PhoneNumber_FROM_NUMBER_WITH_PLUS_SIGN",
":",
"formattedNumber",
"=",
"Format",
"(",
"number",
",",
"INTERNATIONAL",
")",
"\n",
"case",
"PhoneNumber_FROM_NUMBER_WITH_IDD",
":",
"formattedNumber",
"=",
"FormatOutOfCountryCallingNumber",
"(",
"number",
",",
"regionCallingFrom",
")",
"\n",
"case",
"PhoneNumber_FROM_NUMBER_WITHOUT_PLUS_SIGN",
":",
"formattedNumber",
"=",
"Format",
"(",
"number",
",",
"INTERNATIONAL",
")",
"[",
"1",
":",
"]",
"\n",
"case",
"PhoneNumber_FROM_DEFAULT_COUNTRY",
":",
"// Fall-through to default case.",
"fallthrough",
"\n",
"default",
":",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
")",
"\n",
"// We strip non-digits from the NDD here, and from the raw",
"// input later, so that we can compare them easily.",
"nationalPrefix",
":=",
"GetNddPrefixForRegion",
"(",
"regionCode",
",",
"true",
"/* strip non-digits */",
")",
"\n",
"nationalFormat",
":=",
"Format",
"(",
"number",
",",
"NATIONAL",
")",
"\n",
"if",
"len",
"(",
"nationalPrefix",
")",
"==",
"0",
"{",
"// If the region doesn't have a national prefix at all,",
"// we can safely return the national format without worrying",
"// about a national prefix being added.",
"formattedNumber",
"=",
"nationalFormat",
"\n",
"break",
"\n",
"}",
"\n",
"// Otherwise, we check if the original number was entered with",
"// a national prefix.",
"if",
"rawInputContainsNationalPrefix",
"(",
"rawInput",
",",
"nationalPrefix",
",",
"regionCode",
")",
"{",
"// If so, we can safely return the national format.",
"formattedNumber",
"=",
"nationalFormat",
"\n",
"}",
"\n",
"// Metadata cannot be null here because GetNddPrefixForRegion()",
"// (above) returns null if there is no metadata for the region.",
"metadata",
":=",
"getMetadataForRegion",
"(",
"regionCode",
")",
"\n",
"nationalNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n",
"formatRule",
":=",
"chooseFormattingPatternForNumber",
"(",
"metadata",
".",
"GetNumberFormat",
"(",
")",
",",
"nationalNumber",
")",
"\n",
"// The format rule could still be null here if the national",
"// number was 0 and there was no raw input (this should not",
"// be possible for numbers generated by the phonenumber library",
"// as they would also not have a country calling code and we",
"// would have exited earlier).",
"if",
"formatRule",
"==",
"nil",
"{",
"formattedNumber",
"=",
"nationalFormat",
"\n",
"break",
"\n",
"}",
"\n",
"// When the format we apply to this number doesn't contain",
"// national prefix, we can just return the national format.",
"// TODO: Refactor the code below with the code in",
"// isNationalPrefixPresentIfRequired.",
"candidateNationalPrefixRule",
":=",
"formatRule",
".",
"GetNationalPrefixFormattingRule",
"(",
")",
"\n",
"// We assume that the first-group symbol will never be _before_",
"// the national prefix.",
"indexOfFirstGroup",
":=",
"strings",
".",
"Index",
"(",
"candidateNationalPrefixRule",
",",
"\"",
"\"",
")",
"\n",
"if",
"indexOfFirstGroup",
"<=",
"0",
"{",
"formattedNumber",
"=",
"nationalFormat",
"\n",
"break",
"\n",
"}",
"\n",
"candidateNationalPrefixRule",
"=",
"candidateNationalPrefixRule",
"[",
"0",
":",
"indexOfFirstGroup",
"]",
"\n",
"candidateNationalPrefixRule",
"=",
"NormalizeDigitsOnly",
"(",
"candidateNationalPrefixRule",
")",
"\n",
"if",
"len",
"(",
"candidateNationalPrefixRule",
")",
"==",
"0",
"{",
"// National prefix not used when formatting this number.",
"formattedNumber",
"=",
"nationalFormat",
"\n",
"break",
"\n",
"}",
"\n",
"// Otherwise, we need to remove the national prefix from our output.",
"var",
"numFormatCopy",
"*",
"NumberFormat",
"\n",
"proto",
".",
"Merge",
"(",
"numFormatCopy",
",",
"formatRule",
")",
"\n",
"numFormatCopy",
".",
"NationalPrefixFormattingRule",
"=",
"nil",
"\n",
"var",
"numberFormats",
"=",
"[",
"]",
"*",
"NumberFormat",
"{",
"numFormatCopy",
"}",
"\n",
"formattedNumber",
"=",
"FormatByPattern",
"(",
"number",
",",
"NATIONAL",
",",
"numberFormats",
")",
"\n",
"break",
"\n",
"}",
"\n",
"rawInput",
"=",
"number",
".",
"GetRawInput",
"(",
")",
"\n",
"// If no digit is inserted/removed/modified as a result of our",
"// formatting, we return the formatted phone number; otherwise we",
"// return the raw input the user entered.",
"if",
"len",
"(",
"formattedNumber",
")",
"!=",
"0",
"&&",
"len",
"(",
"rawInput",
")",
">",
"0",
"{",
"normalizedFormattedNumber",
":=",
"normalizeDiallableCharsOnly",
"(",
"formattedNumber",
")",
"\n",
"normalizedRawInput",
":=",
"normalizeDiallableCharsOnly",
"(",
"rawInput",
")",
"\n",
"if",
"normalizedFormattedNumber",
"!=",
"normalizedRawInput",
"{",
"formattedNumber",
"=",
"rawInput",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"formattedNumber",
"\n",
"}"
] | // Formats a phone number using the original phone number format that the
// number is parsed from. The original format is embedded in the
// country_code_source field of the PhoneNumber object passed in. If such
// information is missing, the number will be formatted into the NATIONAL
// format by default. When the number contains a leading zero and this is
// unexpected for this country, or we don't have a formatting pattern for
// the number, the method returns the raw input when it is available.
//
// Note this method guarantees no digit will be inserted, removed or
// modified as a result of formatting. | [
"Formats",
"a",
"phone",
"number",
"using",
"the",
"original",
"phone",
"number",
"format",
"that",
"the",
"number",
"is",
"parsed",
"from",
".",
"The",
"original",
"format",
"is",
"embedded",
"in",
"the",
"country_code_source",
"field",
"of",
"the",
"PhoneNumber",
"object",
"passed",
"in",
".",
"If",
"such",
"information",
"is",
"missing",
"the",
"number",
"will",
"be",
"formatted",
"into",
"the",
"NATIONAL",
"format",
"by",
"default",
".",
"When",
"the",
"number",
"contains",
"a",
"leading",
"zero",
"and",
"this",
"is",
"unexpected",
"for",
"this",
"country",
"or",
"we",
"don",
"t",
"have",
"a",
"formatting",
"pattern",
"for",
"the",
"number",
"the",
"method",
"returns",
"the",
"raw",
"input",
"when",
"it",
"is",
"available",
".",
"Note",
"this",
"method",
"guarantees",
"no",
"digit",
"will",
"be",
"inserted",
"removed",
"or",
"modified",
"as",
"a",
"result",
"of",
"formatting",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1444-L1542 |
ttacon/libphonenumber | phonenumberutil.go | rawInputContainsNationalPrefix | func rawInputContainsNationalPrefix(rawInput, nationalPrefix, regionCode string) bool {
normalizedNationalNumber := NormalizeDigitsOnly(rawInput)
if strings.HasPrefix(normalizedNationalNumber, nationalPrefix) {
// Some Japanese numbers (e.g. 00777123) might be mistaken to
// contain the national prefix when written without it
// (e.g. 0777123) if we just do prefix matching. To tackle that,
// we check the validity of the number if the assumed national
// prefix is removed (777123 won't be valid in Japan).
num, err := Parse(normalizedNationalNumber[len(nationalPrefix):], regionCode)
if err != nil {
return false
}
return IsValidNumber(num)
}
return false
} | go | func rawInputContainsNationalPrefix(rawInput, nationalPrefix, regionCode string) bool {
normalizedNationalNumber := NormalizeDigitsOnly(rawInput)
if strings.HasPrefix(normalizedNationalNumber, nationalPrefix) {
// Some Japanese numbers (e.g. 00777123) might be mistaken to
// contain the national prefix when written without it
// (e.g. 0777123) if we just do prefix matching. To tackle that,
// we check the validity of the number if the assumed national
// prefix is removed (777123 won't be valid in Japan).
num, err := Parse(normalizedNationalNumber[len(nationalPrefix):], regionCode)
if err != nil {
return false
}
return IsValidNumber(num)
}
return false
} | [
"func",
"rawInputContainsNationalPrefix",
"(",
"rawInput",
",",
"nationalPrefix",
",",
"regionCode",
"string",
")",
"bool",
"{",
"normalizedNationalNumber",
":=",
"NormalizeDigitsOnly",
"(",
"rawInput",
")",
"\n",
"if",
"strings",
".",
"HasPrefix",
"(",
"normalizedNationalNumber",
",",
"nationalPrefix",
")",
"{",
"// Some Japanese numbers (e.g. 00777123) might be mistaken to",
"// contain the national prefix when written without it",
"// (e.g. 0777123) if we just do prefix matching. To tackle that,",
"// we check the validity of the number if the assumed national",
"// prefix is removed (777123 won't be valid in Japan).",
"num",
",",
"err",
":=",
"Parse",
"(",
"normalizedNationalNumber",
"[",
"len",
"(",
"nationalPrefix",
")",
":",
"]",
",",
"regionCode",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"IsValidNumber",
"(",
"num",
")",
"\n\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // Check if rawInput, which is assumed to be in the national format, has
// a national prefix. The national prefix is assumed to be in digits-only
// form. | [
"Check",
"if",
"rawInput",
"which",
"is",
"assumed",
"to",
"be",
"in",
"the",
"national",
"format",
"has",
"a",
"national",
"prefix",
".",
"The",
"national",
"prefix",
"is",
"assumed",
"to",
"be",
"in",
"digits",
"-",
"only",
"form",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1547-L1563 |
ttacon/libphonenumber | phonenumberutil.go | hasUnexpectedItalianLeadingZero | func hasUnexpectedItalianLeadingZero(number *PhoneNumber) bool {
return number.GetItalianLeadingZero() &&
!isLeadingZeroPossible(int(number.GetCountryCode()))
} | go | func hasUnexpectedItalianLeadingZero(number *PhoneNumber) bool {
return number.GetItalianLeadingZero() &&
!isLeadingZeroPossible(int(number.GetCountryCode()))
} | [
"func",
"hasUnexpectedItalianLeadingZero",
"(",
"number",
"*",
"PhoneNumber",
")",
"bool",
"{",
"return",
"number",
".",
"GetItalianLeadingZero",
"(",
")",
"&&",
"!",
"isLeadingZeroPossible",
"(",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
")",
"\n",
"}"
] | // Returns true if a number is from a region whose national significant
// number couldn't contain a leading zero, but has the italian_leading_zero
// field set to true. | [
"Returns",
"true",
"if",
"a",
"number",
"is",
"from",
"a",
"region",
"whose",
"national",
"significant",
"number",
"couldn",
"t",
"contain",
"a",
"leading",
"zero",
"but",
"has",
"the",
"italian_leading_zero",
"field",
"set",
"to",
"true",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1568-L1571 |
ttacon/libphonenumber | phonenumberutil.go | FormatOutOfCountryKeepingAlphaChars | func FormatOutOfCountryKeepingAlphaChars(
number *PhoneNumber,
regionCallingFrom string) string {
rawInput := number.GetRawInput()
// If there is no raw input, then we can't keep alpha characters
// because there aren't any. In this case, we return
// formatOutOfCountryCallingNumber.
if len(rawInput) == 0 {
return FormatOutOfCountryCallingNumber(number, regionCallingFrom)
}
countryCode := int(number.GetCountryCode())
if !hasValidCountryCallingCode(countryCode) {
return rawInput
}
// Strip any prefix such as country calling code, IDD, that was
// present. We do this by comparing the number in raw_input with
// the parsed number. To do this, first we normalize punctuation.
// We retain number grouping symbols such as " " only.
rawInput = normalizeHelper(rawInput, ALL_PLUS_NUMBER_GROUPING_SYMBOLS, true)
// Now we trim everything before the first three digits in the
// parsed number. We choose three because all valid alpha numbers
// have 3 digits at the start - if it does not, then we don't trim
// anything at all. Similarly, if the national number was less than
// three digits, we don't trim anything at all.
nationalNumber := GetNationalSignificantNumber(number)
if len(nationalNumber) > 3 {
firstNationalNumberDigit := strings.Index(rawInput, nationalNumber[0:3])
if firstNationalNumberDigit > -1 {
rawInput = rawInput[firstNationalNumberDigit:]
}
}
metadataForRegionCallingFrom := getMetadataForRegion(regionCallingFrom)
if countryCode == NANPA_COUNTRY_CODE {
if IsNANPACountry(regionCallingFrom) {
return strconv.Itoa(countryCode) + " " + rawInput
}
} else if metadataForRegionCallingFrom != nil &&
countryCode == getCountryCodeForValidRegion(regionCallingFrom) {
formattingPattern :=
chooseFormattingPatternForNumber(
metadataForRegionCallingFrom.GetNumberFormat(),
nationalNumber)
if formattingPattern == nil {
// If no pattern above is matched, we format the original input.
return rawInput
}
var newFormat *NumberFormat
proto.Merge(newFormat, formattingPattern)
// The first group is the first group of digits that the user
// wrote together.
newFormat.Pattern = proto.String("(\\d+)(.*)")
// Here we just concatenate them back together after the national
// prefix has been fixed.
newFormat.Format = proto.String("$1$2")
// Now we format using this pattern instead of the default pattern,
// but with the national prefix prefixed if necessary. This will not
// work in the cases where the pattern (and not the leading digits)
// decide whether a national prefix needs to be used, since we
// have overridden the pattern to match anything, but that is not
// the case in the metadata to date.
return formatNsnUsingPattern(rawInput, newFormat, NATIONAL)
}
var internationalPrefixForFormatting = ""
// If an unsupported region-calling-from is entered, or a country
// with multiple international prefixes, the international format
// of the number is returned, unless there is a preferred international
// prefix.
if metadataForRegionCallingFrom != nil {
internationalPrefix := metadataForRegionCallingFrom.GetInternationalPrefix()
internationalPrefixForFormatting = internationalPrefix
if !UNIQUE_INTERNATIONAL_PREFIX.MatchString(internationalPrefix) {
internationalPrefixForFormatting =
metadataForRegionCallingFrom.GetPreferredInternationalPrefix()
}
}
var formattedNumber = builder.NewBuilder([]byte(rawInput))
regionCode := GetRegionCodeForCountryCode(countryCode)
// Metadata cannot be null because the country calling code is valid.
var metadataForRegion *PhoneMetadata = getMetadataForRegionOrCallingCode(countryCode, regionCode)
maybeAppendFormattedExtension(number, metadataForRegion,
INTERNATIONAL, formattedNumber)
if len(internationalPrefixForFormatting) > 0 {
formattedBytes := append([]byte(" "), formattedNumber.Bytes()...)
// we know countryCode is really an int32
intBuf := []byte{
byte(countryCode >> 24),
byte(countryCode >> 16),
byte(countryCode >> 8),
byte(countryCode),
}
formattedBytes = append(intBuf, formattedBytes...)
formattedBytes = append([]byte(" "), formattedBytes...)
formattedBytes = append(
[]byte(internationalPrefixForFormatting), formattedBytes...)
formattedNumber = builder.NewBuilder(formattedBytes)
} else {
// Invalid region entered as country-calling-from (so no metadata
// was found for it) or the region chosen has multiple international
// dialling prefixes.
prefixNumberWithCountryCallingCode(countryCode,
INTERNATIONAL,
formattedNumber)
}
return formattedNumber.String()
} | go | func FormatOutOfCountryKeepingAlphaChars(
number *PhoneNumber,
regionCallingFrom string) string {
rawInput := number.GetRawInput()
// If there is no raw input, then we can't keep alpha characters
// because there aren't any. In this case, we return
// formatOutOfCountryCallingNumber.
if len(rawInput) == 0 {
return FormatOutOfCountryCallingNumber(number, regionCallingFrom)
}
countryCode := int(number.GetCountryCode())
if !hasValidCountryCallingCode(countryCode) {
return rawInput
}
// Strip any prefix such as country calling code, IDD, that was
// present. We do this by comparing the number in raw_input with
// the parsed number. To do this, first we normalize punctuation.
// We retain number grouping symbols such as " " only.
rawInput = normalizeHelper(rawInput, ALL_PLUS_NUMBER_GROUPING_SYMBOLS, true)
// Now we trim everything before the first three digits in the
// parsed number. We choose three because all valid alpha numbers
// have 3 digits at the start - if it does not, then we don't trim
// anything at all. Similarly, if the national number was less than
// three digits, we don't trim anything at all.
nationalNumber := GetNationalSignificantNumber(number)
if len(nationalNumber) > 3 {
firstNationalNumberDigit := strings.Index(rawInput, nationalNumber[0:3])
if firstNationalNumberDigit > -1 {
rawInput = rawInput[firstNationalNumberDigit:]
}
}
metadataForRegionCallingFrom := getMetadataForRegion(regionCallingFrom)
if countryCode == NANPA_COUNTRY_CODE {
if IsNANPACountry(regionCallingFrom) {
return strconv.Itoa(countryCode) + " " + rawInput
}
} else if metadataForRegionCallingFrom != nil &&
countryCode == getCountryCodeForValidRegion(regionCallingFrom) {
formattingPattern :=
chooseFormattingPatternForNumber(
metadataForRegionCallingFrom.GetNumberFormat(),
nationalNumber)
if formattingPattern == nil {
// If no pattern above is matched, we format the original input.
return rawInput
}
var newFormat *NumberFormat
proto.Merge(newFormat, formattingPattern)
// The first group is the first group of digits that the user
// wrote together.
newFormat.Pattern = proto.String("(\\d+)(.*)")
// Here we just concatenate them back together after the national
// prefix has been fixed.
newFormat.Format = proto.String("$1$2")
// Now we format using this pattern instead of the default pattern,
// but with the national prefix prefixed if necessary. This will not
// work in the cases where the pattern (and not the leading digits)
// decide whether a national prefix needs to be used, since we
// have overridden the pattern to match anything, but that is not
// the case in the metadata to date.
return formatNsnUsingPattern(rawInput, newFormat, NATIONAL)
}
var internationalPrefixForFormatting = ""
// If an unsupported region-calling-from is entered, or a country
// with multiple international prefixes, the international format
// of the number is returned, unless there is a preferred international
// prefix.
if metadataForRegionCallingFrom != nil {
internationalPrefix := metadataForRegionCallingFrom.GetInternationalPrefix()
internationalPrefixForFormatting = internationalPrefix
if !UNIQUE_INTERNATIONAL_PREFIX.MatchString(internationalPrefix) {
internationalPrefixForFormatting =
metadataForRegionCallingFrom.GetPreferredInternationalPrefix()
}
}
var formattedNumber = builder.NewBuilder([]byte(rawInput))
regionCode := GetRegionCodeForCountryCode(countryCode)
// Metadata cannot be null because the country calling code is valid.
var metadataForRegion *PhoneMetadata = getMetadataForRegionOrCallingCode(countryCode, regionCode)
maybeAppendFormattedExtension(number, metadataForRegion,
INTERNATIONAL, formattedNumber)
if len(internationalPrefixForFormatting) > 0 {
formattedBytes := append([]byte(" "), formattedNumber.Bytes()...)
// we know countryCode is really an int32
intBuf := []byte{
byte(countryCode >> 24),
byte(countryCode >> 16),
byte(countryCode >> 8),
byte(countryCode),
}
formattedBytes = append(intBuf, formattedBytes...)
formattedBytes = append([]byte(" "), formattedBytes...)
formattedBytes = append(
[]byte(internationalPrefixForFormatting), formattedBytes...)
formattedNumber = builder.NewBuilder(formattedBytes)
} else {
// Invalid region entered as country-calling-from (so no metadata
// was found for it) or the region chosen has multiple international
// dialling prefixes.
prefixNumberWithCountryCallingCode(countryCode,
INTERNATIONAL,
formattedNumber)
}
return formattedNumber.String()
} | [
"func",
"FormatOutOfCountryKeepingAlphaChars",
"(",
"number",
"*",
"PhoneNumber",
",",
"regionCallingFrom",
"string",
")",
"string",
"{",
"rawInput",
":=",
"number",
".",
"GetRawInput",
"(",
")",
"\n",
"// If there is no raw input, then we can't keep alpha characters",
"// because there aren't any. In this case, we return",
"// formatOutOfCountryCallingNumber.",
"if",
"len",
"(",
"rawInput",
")",
"==",
"0",
"{",
"return",
"FormatOutOfCountryCallingNumber",
"(",
"number",
",",
"regionCallingFrom",
")",
"\n",
"}",
"\n",
"countryCode",
":=",
"int",
"(",
"number",
".",
"GetCountryCode",
"(",
")",
")",
"\n",
"if",
"!",
"hasValidCountryCallingCode",
"(",
"countryCode",
")",
"{",
"return",
"rawInput",
"\n",
"}",
"\n",
"// Strip any prefix such as country calling code, IDD, that was",
"// present. We do this by comparing the number in raw_input with",
"// the parsed number. To do this, first we normalize punctuation.",
"// We retain number grouping symbols such as \" \" only.",
"rawInput",
"=",
"normalizeHelper",
"(",
"rawInput",
",",
"ALL_PLUS_NUMBER_GROUPING_SYMBOLS",
",",
"true",
")",
"\n",
"// Now we trim everything before the first three digits in the",
"// parsed number. We choose three because all valid alpha numbers",
"// have 3 digits at the start - if it does not, then we don't trim",
"// anything at all. Similarly, if the national number was less than",
"// three digits, we don't trim anything at all.",
"nationalNumber",
":=",
"GetNationalSignificantNumber",
"(",
"number",
")",
"\n",
"if",
"len",
"(",
"nationalNumber",
")",
">",
"3",
"{",
"firstNationalNumberDigit",
":=",
"strings",
".",
"Index",
"(",
"rawInput",
",",
"nationalNumber",
"[",
"0",
":",
"3",
"]",
")",
"\n",
"if",
"firstNationalNumberDigit",
">",
"-",
"1",
"{",
"rawInput",
"=",
"rawInput",
"[",
"firstNationalNumberDigit",
":",
"]",
"\n",
"}",
"\n",
"}",
"\n",
"metadataForRegionCallingFrom",
":=",
"getMetadataForRegion",
"(",
"regionCallingFrom",
")",
"\n",
"if",
"countryCode",
"==",
"NANPA_COUNTRY_CODE",
"{",
"if",
"IsNANPACountry",
"(",
"regionCallingFrom",
")",
"{",
"return",
"strconv",
".",
"Itoa",
"(",
"countryCode",
")",
"+",
"\"",
"\"",
"+",
"rawInput",
"\n",
"}",
"\n",
"}",
"else",
"if",
"metadataForRegionCallingFrom",
"!=",
"nil",
"&&",
"countryCode",
"==",
"getCountryCodeForValidRegion",
"(",
"regionCallingFrom",
")",
"{",
"formattingPattern",
":=",
"chooseFormattingPatternForNumber",
"(",
"metadataForRegionCallingFrom",
".",
"GetNumberFormat",
"(",
")",
",",
"nationalNumber",
")",
"\n",
"if",
"formattingPattern",
"==",
"nil",
"{",
"// If no pattern above is matched, we format the original input.",
"return",
"rawInput",
"\n",
"}",
"\n",
"var",
"newFormat",
"*",
"NumberFormat",
"\n",
"proto",
".",
"Merge",
"(",
"newFormat",
",",
"formattingPattern",
")",
"\n",
"// The first group is the first group of digits that the user",
"// wrote together.",
"newFormat",
".",
"Pattern",
"=",
"proto",
".",
"String",
"(",
"\"",
"\\\\",
"\"",
")",
"\n",
"// Here we just concatenate them back together after the national",
"// prefix has been fixed.",
"newFormat",
".",
"Format",
"=",
"proto",
".",
"String",
"(",
"\"",
"\"",
")",
"\n",
"// Now we format using this pattern instead of the default pattern,",
"// but with the national prefix prefixed if necessary. This will not",
"// work in the cases where the pattern (and not the leading digits)",
"// decide whether a national prefix needs to be used, since we",
"// have overridden the pattern to match anything, but that is not",
"// the case in the metadata to date.",
"return",
"formatNsnUsingPattern",
"(",
"rawInput",
",",
"newFormat",
",",
"NATIONAL",
")",
"\n",
"}",
"\n",
"var",
"internationalPrefixForFormatting",
"=",
"\"",
"\"",
"\n",
"// If an unsupported region-calling-from is entered, or a country",
"// with multiple international prefixes, the international format",
"// of the number is returned, unless there is a preferred international",
"// prefix.",
"if",
"metadataForRegionCallingFrom",
"!=",
"nil",
"{",
"internationalPrefix",
":=",
"metadataForRegionCallingFrom",
".",
"GetInternationalPrefix",
"(",
")",
"\n",
"internationalPrefixForFormatting",
"=",
"internationalPrefix",
"\n",
"if",
"!",
"UNIQUE_INTERNATIONAL_PREFIX",
".",
"MatchString",
"(",
"internationalPrefix",
")",
"{",
"internationalPrefixForFormatting",
"=",
"metadataForRegionCallingFrom",
".",
"GetPreferredInternationalPrefix",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"var",
"formattedNumber",
"=",
"builder",
".",
"NewBuilder",
"(",
"[",
"]",
"byte",
"(",
"rawInput",
")",
")",
"\n",
"regionCode",
":=",
"GetRegionCodeForCountryCode",
"(",
"countryCode",
")",
"\n",
"// Metadata cannot be null because the country calling code is valid.",
"var",
"metadataForRegion",
"*",
"PhoneMetadata",
"=",
"getMetadataForRegionOrCallingCode",
"(",
"countryCode",
",",
"regionCode",
")",
"\n",
"maybeAppendFormattedExtension",
"(",
"number",
",",
"metadataForRegion",
",",
"INTERNATIONAL",
",",
"formattedNumber",
")",
"\n",
"if",
"len",
"(",
"internationalPrefixForFormatting",
")",
">",
"0",
"{",
"formattedBytes",
":=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"formattedNumber",
".",
"Bytes",
"(",
")",
"...",
")",
"\n",
"// we know countryCode is really an int32",
"intBuf",
":=",
"[",
"]",
"byte",
"{",
"byte",
"(",
"countryCode",
">>",
"24",
")",
",",
"byte",
"(",
"countryCode",
">>",
"16",
")",
",",
"byte",
"(",
"countryCode",
">>",
"8",
")",
",",
"byte",
"(",
"countryCode",
")",
",",
"}",
"\n",
"formattedBytes",
"=",
"append",
"(",
"intBuf",
",",
"formattedBytes",
"...",
")",
"\n",
"formattedBytes",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"formattedBytes",
"...",
")",
"\n",
"formattedBytes",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"internationalPrefixForFormatting",
")",
",",
"formattedBytes",
"...",
")",
"\n\n",
"formattedNumber",
"=",
"builder",
".",
"NewBuilder",
"(",
"formattedBytes",
")",
"\n",
"}",
"else",
"{",
"// Invalid region entered as country-calling-from (so no metadata",
"// was found for it) or the region chosen has multiple international",
"// dialling prefixes.",
"prefixNumberWithCountryCallingCode",
"(",
"countryCode",
",",
"INTERNATIONAL",
",",
"formattedNumber",
")",
"\n",
"}",
"\n",
"return",
"formattedNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Formats a phone number for out-of-country dialing purposes.
//
// Note that in this version, if the number was entered originally using
// alpha characters and this version of the number is stored in raw_input,
// this representation of the number will be used rather than the digit
// representation. Grouping information, as specified by characters
// such as "-" and " ", will be retained.
//
// 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. | [
"Formats",
"a",
"phone",
"number",
"for",
"out",
"-",
"of",
"-",
"country",
"dialing",
"purposes",
".",
"Note",
"that",
"in",
"this",
"version",
"if",
"the",
"number",
"was",
"entered",
"originally",
"using",
"alpha",
"characters",
"and",
"this",
"version",
"of",
"the",
"number",
"is",
"stored",
"in",
"raw_input",
"this",
"representation",
"of",
"the",
"number",
"will",
"be",
"used",
"rather",
"than",
"the",
"digit",
"representation",
".",
"Grouping",
"information",
"as",
"specified",
"by",
"characters",
"such",
"as",
"-",
"and",
"will",
"be",
"retained",
".",
"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",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1607-L1713 |
ttacon/libphonenumber | phonenumberutil.go | GetNationalSignificantNumber | func GetNationalSignificantNumber(number *PhoneNumber) string {
// If leading zero(s) have been set, we prefix this now. Note this
// is not a national prefix.
nationalNumber := builder.NewBuilder(nil)
if number.GetItalianLeadingZero() {
zeros := make([]byte, number.GetNumberOfLeadingZeros())
for i := range zeros {
zeros[i] = '0'
}
nationalNumber.Write(zeros)
}
asStr := strconv.FormatUint(number.GetNationalNumber(), 10)
nationalNumber.WriteString(asStr)
return nationalNumber.String()
} | go | func GetNationalSignificantNumber(number *PhoneNumber) string {
// If leading zero(s) have been set, we prefix this now. Note this
// is not a national prefix.
nationalNumber := builder.NewBuilder(nil)
if number.GetItalianLeadingZero() {
zeros := make([]byte, number.GetNumberOfLeadingZeros())
for i := range zeros {
zeros[i] = '0'
}
nationalNumber.Write(zeros)
}
asStr := strconv.FormatUint(number.GetNationalNumber(), 10)
nationalNumber.WriteString(asStr)
return nationalNumber.String()
} | [
"func",
"GetNationalSignificantNumber",
"(",
"number",
"*",
"PhoneNumber",
")",
"string",
"{",
"// If leading zero(s) have been set, we prefix this now. Note this",
"// is not a national prefix.",
"nationalNumber",
":=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n",
"if",
"number",
".",
"GetItalianLeadingZero",
"(",
")",
"{",
"zeros",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"number",
".",
"GetNumberOfLeadingZeros",
"(",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"zeros",
"{",
"zeros",
"[",
"i",
"]",
"=",
"'0'",
"\n",
"}",
"\n",
"nationalNumber",
".",
"Write",
"(",
"zeros",
")",
"\n",
"}",
"\n",
"asStr",
":=",
"strconv",
".",
"FormatUint",
"(",
"number",
".",
"GetNationalNumber",
"(",
")",
",",
"10",
")",
"\n",
"nationalNumber",
".",
"WriteString",
"(",
"asStr",
")",
"\n\n",
"return",
"nationalNumber",
".",
"String",
"(",
")",
"\n",
"}"
] | // Gets the national significant number of the a phone number. Note a
// national significant number doesn't contain a national prefix or
// any formatting. | [
"Gets",
"the",
"national",
"significant",
"number",
"of",
"the",
"a",
"phone",
"number",
".",
"Note",
"a",
"national",
"significant",
"number",
"doesn",
"t",
"contain",
"a",
"national",
"prefix",
"or",
"any",
"formatting",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1718-L1733 |
ttacon/libphonenumber | phonenumberutil.go | prefixNumberWithCountryCallingCode | func prefixNumberWithCountryCallingCode(
countryCallingCode int,
numberFormat PhoneNumberFormat,
formattedNumber *builder.Builder) {
// TODO(ttacon): add some sort of BulkWrite builder to builder.Builder
// also that name isn't too awesome...:)
newBuf := builder.NewBuilder(nil)
switch numberFormat {
case E164:
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.Write(formattedNumber.Bytes())
case INTERNATIONAL:
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.WriteString(" ")
newBuf.Write(formattedNumber.Bytes())
case RFC3966:
newBuf.WriteString(RFC3966_PREFIX)
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.WriteString("-")
newBuf.Write(formattedNumber.Bytes())
case NATIONAL:
fallthrough
default:
newBuf.Write(formattedNumber.Bytes())
}
formattedNumber.ResetWith(newBuf.Bytes())
} | go | func prefixNumberWithCountryCallingCode(
countryCallingCode int,
numberFormat PhoneNumberFormat,
formattedNumber *builder.Builder) {
// TODO(ttacon): add some sort of BulkWrite builder to builder.Builder
// also that name isn't too awesome...:)
newBuf := builder.NewBuilder(nil)
switch numberFormat {
case E164:
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.Write(formattedNumber.Bytes())
case INTERNATIONAL:
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.WriteString(" ")
newBuf.Write(formattedNumber.Bytes())
case RFC3966:
newBuf.WriteString(RFC3966_PREFIX)
newBuf.WriteString(string(PLUS_SIGN))
newBuf.Write(strconv.AppendInt([]byte{}, int64(countryCallingCode), 10))
newBuf.WriteString("-")
newBuf.Write(formattedNumber.Bytes())
case NATIONAL:
fallthrough
default:
newBuf.Write(formattedNumber.Bytes())
}
formattedNumber.ResetWith(newBuf.Bytes())
} | [
"func",
"prefixNumberWithCountryCallingCode",
"(",
"countryCallingCode",
"int",
",",
"numberFormat",
"PhoneNumberFormat",
",",
"formattedNumber",
"*",
"builder",
".",
"Builder",
")",
"{",
"// TODO(ttacon): add some sort of BulkWrite builder to builder.Builder",
"// also that name isn't too awesome...:)",
"newBuf",
":=",
"builder",
".",
"NewBuilder",
"(",
"nil",
")",
"\n",
"switch",
"numberFormat",
"{",
"case",
"E164",
":",
"newBuf",
".",
"WriteString",
"(",
"string",
"(",
"PLUS_SIGN",
")",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"strconv",
".",
"AppendInt",
"(",
"[",
"]",
"byte",
"{",
"}",
",",
"int64",
"(",
"countryCallingCode",
")",
",",
"10",
")",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"formattedNumber",
".",
"Bytes",
"(",
")",
")",
"\n",
"case",
"INTERNATIONAL",
":",
"newBuf",
".",
"WriteString",
"(",
"string",
"(",
"PLUS_SIGN",
")",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"strconv",
".",
"AppendInt",
"(",
"[",
"]",
"byte",
"{",
"}",
",",
"int64",
"(",
"countryCallingCode",
")",
",",
"10",
")",
")",
"\n",
"newBuf",
".",
"WriteString",
"(",
"\"",
"\"",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"formattedNumber",
".",
"Bytes",
"(",
")",
")",
"\n",
"case",
"RFC3966",
":",
"newBuf",
".",
"WriteString",
"(",
"RFC3966_PREFIX",
")",
"\n",
"newBuf",
".",
"WriteString",
"(",
"string",
"(",
"PLUS_SIGN",
")",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"strconv",
".",
"AppendInt",
"(",
"[",
"]",
"byte",
"{",
"}",
",",
"int64",
"(",
"countryCallingCode",
")",
",",
"10",
")",
")",
"\n",
"newBuf",
".",
"WriteString",
"(",
"\"",
"\"",
")",
"\n",
"newBuf",
".",
"Write",
"(",
"formattedNumber",
".",
"Bytes",
"(",
")",
")",
"\n",
"case",
"NATIONAL",
":",
"fallthrough",
"\n",
"default",
":",
"newBuf",
".",
"Write",
"(",
"formattedNumber",
".",
"Bytes",
"(",
")",
")",
"\n",
"}",
"\n",
"formattedNumber",
".",
"ResetWith",
"(",
"newBuf",
".",
"Bytes",
"(",
")",
")",
"\n",
"}"
] | // A helper function that is used by format and formatByPattern. | [
"A",
"helper",
"function",
"that",
"is",
"used",
"by",
"format",
"and",
"formatByPattern",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1736-L1766 |
ttacon/libphonenumber | phonenumberutil.go | formatNsn | func formatNsn(
number string, metadata *PhoneMetadata, numberFormat PhoneNumberFormat) string {
return formatNsnWithCarrier(number, metadata, numberFormat, "")
} | go | func formatNsn(
number string, metadata *PhoneMetadata, numberFormat PhoneNumberFormat) string {
return formatNsnWithCarrier(number, metadata, numberFormat, "")
} | [
"func",
"formatNsn",
"(",
"number",
"string",
",",
"metadata",
"*",
"PhoneMetadata",
",",
"numberFormat",
"PhoneNumberFormat",
")",
"string",
"{",
"return",
"formatNsnWithCarrier",
"(",
"number",
",",
"metadata",
",",
"numberFormat",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // Simple wrapper of formatNsn for the common case of no carrier code. | [
"Simple",
"wrapper",
"of",
"formatNsn",
"for",
"the",
"common",
"case",
"of",
"no",
"carrier",
"code",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1769-L1772 |
ttacon/libphonenumber | phonenumberutil.go | formatNsnWithCarrier | func formatNsnWithCarrier(
number string,
metadata *PhoneMetadata,
numberFormat PhoneNumberFormat,
carrierCode string) string {
var intlNumberFormats []*NumberFormat = metadata.GetIntlNumberFormat()
// When the intlNumberFormats exists, we use that to format national
// number for the INTERNATIONAL format instead of using the
// numberDesc.numberFormats.
var availableFormats []*NumberFormat = metadata.GetIntlNumberFormat()
if len(intlNumberFormats) == 0 || numberFormat == NATIONAL {
availableFormats = metadata.GetNumberFormat()
}
var formattingPattern *NumberFormat = chooseFormattingPatternForNumber(
availableFormats, number)
if formattingPattern == nil {
return number
}
return formatNsnUsingPatternWithCarrier(
number, formattingPattern, numberFormat, carrierCode)
} | go | func formatNsnWithCarrier(
number string,
metadata *PhoneMetadata,
numberFormat PhoneNumberFormat,
carrierCode string) string {
var intlNumberFormats []*NumberFormat = metadata.GetIntlNumberFormat()
// When the intlNumberFormats exists, we use that to format national
// number for the INTERNATIONAL format instead of using the
// numberDesc.numberFormats.
var availableFormats []*NumberFormat = metadata.GetIntlNumberFormat()
if len(intlNumberFormats) == 0 || numberFormat == NATIONAL {
availableFormats = metadata.GetNumberFormat()
}
var formattingPattern *NumberFormat = chooseFormattingPatternForNumber(
availableFormats, number)
if formattingPattern == nil {
return number
}
return formatNsnUsingPatternWithCarrier(
number, formattingPattern, numberFormat, carrierCode)
} | [
"func",
"formatNsnWithCarrier",
"(",
"number",
"string",
",",
"metadata",
"*",
"PhoneMetadata",
",",
"numberFormat",
"PhoneNumberFormat",
",",
"carrierCode",
"string",
")",
"string",
"{",
"var",
"intlNumberFormats",
"[",
"]",
"*",
"NumberFormat",
"=",
"metadata",
".",
"GetIntlNumberFormat",
"(",
")",
"\n",
"// When the intlNumberFormats exists, we use that to format national",
"// number for the INTERNATIONAL format instead of using the",
"// numberDesc.numberFormats.",
"var",
"availableFormats",
"[",
"]",
"*",
"NumberFormat",
"=",
"metadata",
".",
"GetIntlNumberFormat",
"(",
")",
"\n",
"if",
"len",
"(",
"intlNumberFormats",
")",
"==",
"0",
"||",
"numberFormat",
"==",
"NATIONAL",
"{",
"availableFormats",
"=",
"metadata",
".",
"GetNumberFormat",
"(",
")",
"\n",
"}",
"\n",
"var",
"formattingPattern",
"*",
"NumberFormat",
"=",
"chooseFormattingPatternForNumber",
"(",
"availableFormats",
",",
"number",
")",
"\n",
"if",
"formattingPattern",
"==",
"nil",
"{",
"return",
"number",
"\n",
"}",
"\n",
"return",
"formatNsnUsingPatternWithCarrier",
"(",
"number",
",",
"formattingPattern",
",",
"numberFormat",
",",
"carrierCode",
")",
"\n",
"}"
] | // Note in some regions, the national number can be written in two
// completely different ways depending on whether it forms part of the
// NATIONAL format or INTERNATIONAL format. The numberFormat parameter
// here is used to specify which format to use for those cases. If a
// carrierCode is specified, this will be inserted into the formatted
// string to replace $CC. | [
"Note",
"in",
"some",
"regions",
"the",
"national",
"number",
"can",
"be",
"written",
"in",
"two",
"completely",
"different",
"ways",
"depending",
"on",
"whether",
"it",
"forms",
"part",
"of",
"the",
"NATIONAL",
"format",
"or",
"INTERNATIONAL",
"format",
".",
"The",
"numberFormat",
"parameter",
"here",
"is",
"used",
"to",
"specify",
"which",
"format",
"to",
"use",
"for",
"those",
"cases",
".",
"If",
"a",
"carrierCode",
"is",
"specified",
"this",
"will",
"be",
"inserted",
"into",
"the",
"formatted",
"string",
"to",
"replace",
"$CC",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1780-L1800 |
ttacon/libphonenumber | phonenumberutil.go | formatNsnUsingPattern | func formatNsnUsingPattern(
nationalNumber string,
formattingPattern *NumberFormat,
numberFormat PhoneNumberFormat) string {
return formatNsnUsingPatternWithCarrier(
nationalNumber, formattingPattern, numberFormat, "")
} | go | func formatNsnUsingPattern(
nationalNumber string,
formattingPattern *NumberFormat,
numberFormat PhoneNumberFormat) string {
return formatNsnUsingPatternWithCarrier(
nationalNumber, formattingPattern, numberFormat, "")
} | [
"func",
"formatNsnUsingPattern",
"(",
"nationalNumber",
"string",
",",
"formattingPattern",
"*",
"NumberFormat",
",",
"numberFormat",
"PhoneNumberFormat",
")",
"string",
"{",
"return",
"formatNsnUsingPatternWithCarrier",
"(",
"nationalNumber",
",",
"formattingPattern",
",",
"numberFormat",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // Simple wrapper of formatNsnUsingPattern for the common case of no carrier code. | [
"Simple",
"wrapper",
"of",
"formatNsnUsingPattern",
"for",
"the",
"common",
"case",
"of",
"no",
"carrier",
"code",
"."
] | train | https://github.com/ttacon/libphonenumber/blob/23ddf903e8f8800d2857645eb155ffbe15cd02ee/phonenumberutil.go#L1844-L1850 |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
- Downloads last month
- 13