| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 39b3875 commit 8e2e167
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -332,6 +332,7 @@ | |||
| 332 | 332 | 'src/node_contextify.cc', | |
| 333 | 333 | 'src/node_debug_options.cc', | |
| 334 | 334 | 'src/node_domain.cc', | |
| 335 | + 'src/node_encoding.cc', | ||
| 335 | 336 | 'src/node_errors.h', | |
| 336 | 337 | 'src/node_file.cc', | |
| 337 | 338 | 'src/node_http2.cc', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -765,129 +765,6 @@ Local<Value> MakeCallback(Isolate* isolate, | |||
| 765 | 765 | .FromMaybe(Local<Value>())); | |
| 766 | 766 | } | |
| 767 | 767 | ||
| 768 | - | ||
| 769 | - enum encoding ParseEncoding(const char* encoding, | ||
| 770 | - enum encoding default_encoding) { | ||
| 771 | - switch (encoding[0]) { | ||
| 772 | - case 'u': | ||
| 773 | - // utf8, utf16le | ||
| 774 | - if (encoding[1] == 't' && encoding[2] == 'f') { | ||
| 775 | - // Skip `-` | ||
| 776 | - encoding += encoding[3] == '-' ? 4 : 3; | ||
| 777 | - if (encoding[0] == '8' && encoding[1] == '\0') | ||
| 778 | - return UTF8; | ||
| 779 | - if (strncmp(encoding, "16le", 4) == 0) | ||
| 780 | - return UCS2; | ||
| 781 | - | ||
| 782 | - // ucs2 | ||
| 783 | - } else if (encoding[1] == 'c' && encoding[2] == 's') { | ||
| 784 | - encoding += encoding[3] == '-' ? 4 : 3; | ||
| 785 | - if (encoding[0] == '2' && encoding[1] == '\0') | ||
| 786 | - return UCS2; | ||
| 787 | - } | ||
| 788 | - break; | ||
| 789 | - case 'l': | ||
| 790 | - // latin1 | ||
| 791 | - if (encoding[1] == 'a') { | ||
| 792 | - if (strncmp(encoding + 2, "tin1", 4) == 0) | ||
| 793 | - return LATIN1; | ||
| 794 | - } | ||
| 795 | - break; | ||
| 796 | - case 'b': | ||
| 797 | - // binary | ||
| 798 | - if (encoding[1] == 'i') { | ||
| 799 | - if (strncmp(encoding + 2, "nary", 4) == 0) | ||
| 800 | - return LATIN1; | ||
| 801 | - | ||
| 802 | - // buffer | ||
| 803 | - } else if (encoding[1] == 'u') { | ||
| 804 | - if (strncmp(encoding + 2, "ffer", 4) == 0) | ||
| 805 | - return BUFFER; | ||
| 806 | - } | ||
| 807 | - break; | ||
| 808 | - case '\0': | ||
| 809 | - return default_encoding; | ||
| 810 | - default: | ||
| 811 | - break; | ||
| 812 | - } | ||
| 813 | - | ||
| 814 | - if (StringEqualNoCase(encoding, "utf8")) { | ||
| 815 | - return UTF8; | ||
| 816 | - } else if (StringEqualNoCase(encoding, "utf-8")) { | ||
| 817 | - return UTF8; | ||
| 818 | - } else if (StringEqualNoCase(encoding, "ascii")) { | ||
| 819 | - return ASCII; | ||
| 820 | - } else if (StringEqualNoCase(encoding, "base64")) { | ||
| 821 | - return BASE64; | ||
| 822 | - } else if (StringEqualNoCase(encoding, "ucs2")) { | ||
| 823 | - return UCS2; | ||
| 824 | - } else if (StringEqualNoCase(encoding, "ucs-2")) { | ||
| 825 | - return UCS2; | ||
| 826 | - } else if (StringEqualNoCase(encoding, "utf16le")) { | ||
| 827 | - return UCS2; | ||
| 828 | - } else if (StringEqualNoCase(encoding, "utf-16le")) { | ||
| 829 | - return UCS2; | ||
| 830 | - } else if (StringEqualNoCase(encoding, "latin1")) { | ||
| 831 | - return LATIN1; | ||
| 832 | - } else if (StringEqualNoCase(encoding, "binary")) { | ||
| 833 | - return LATIN1; // BINARY is a deprecated alias of LATIN1. | ||
| 834 | - } else if (StringEqualNoCase(encoding, "buffer")) { | ||
| 835 | - return BUFFER; | ||
| 836 | - } else if (StringEqualNoCase(encoding, "hex")) { | ||
| 837 | - return HEX; | ||
| 838 | - } else { | ||
| 839 | - return default_encoding; | ||
| 840 | - } | ||
| 841 | - } | ||
| 842 | - | ||
| 843 | - | ||
| 844 | - enum encoding ParseEncoding(Isolate* isolate, | ||
| 845 | - Local<Value> encoding_v, | ||
| 846 | - enum encoding default_encoding) { | ||
| 847 | - CHECK(!encoding_v.IsEmpty()); | ||
| 848 | - | ||
| 849 | - if (!encoding_v->IsString()) | ||
| 850 | - return default_encoding; | ||
| 851 | - | ||
| 852 | - node::Utf8Value encoding(isolate, encoding_v); | ||
| 853 | - | ||
| 854 | - return ParseEncoding(*encoding, default_encoding); | ||
| 855 | - } | ||
| 856 | - | ||
| 857 | - Local<Value> Encode(Isolate* isolate, | ||
| 858 | - const char* buf, | ||
| 859 | - size_t len, | ||
| 860 | - enum encoding encoding) { | ||
| 861 | - CHECK_NE(encoding, UCS2); | ||
| 862 | - Local<Value> error; | ||
| 863 | - return StringBytes::Encode(isolate, buf, len, encoding, &error) | ||
| 864 | - .ToLocalChecked(); | ||
| 865 | - } | ||
| 866 | - | ||
| 867 | - Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { | ||
| 868 | - Local<Value> error; | ||
| 869 | - return StringBytes::Encode(isolate, buf, len, &error) | ||
| 870 | - .ToLocalChecked(); | ||
| 871 | - } | ||
| 872 | - | ||
| 873 | - // Returns -1 if the handle was not valid for decoding | ||
| 874 | - ssize_t DecodeBytes(Isolate* isolate, | ||
| 875 | - Local<Value> val, | ||
| 876 | - enum encoding encoding) { | ||
| 877 | - HandleScope scope(isolate); | ||
| 878 | - | ||
| 879 | - return StringBytes::Size(isolate, val, encoding); | ||
| 880 | - } | ||
| 881 | - | ||
| 882 | - // Returns number of bytes written. | ||
| 883 | - ssize_t DecodeWrite(Isolate* isolate, | ||
| 884 | - char* buf, | ||
| 885 | - size_t buflen, | ||
| 886 | - Local<Value> val, | ||
| 887 | - enum encoding encoding) { | ||
| 888 | - return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr); | ||
| 889 | - } | ||
| 890 | - | ||
| 891 | 768 | bool IsExceptionDecorated(Environment* env, Local<Value> er) { | |
| 892 | 769 | if (!er.IsEmpty() && er->IsObject()) { | |
| 893 | 770 | Local<Object> err_obj = er.As<Object>(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,138 @@ | |||
| 1 | + #include "node.h" | ||
| 2 | + #include "env.h" | ||
| 3 | + #include "env-inl.h" | ||
| 4 | + #include "string_bytes.h" | ||
| 5 | + #include "util.h" | ||
| 6 | + #include "util-inl.h" | ||
| 7 | + #include "v8.h" | ||
| 8 | + | ||
| 9 | + namespace node { | ||
| 10 | + | ||
| 11 | + using v8::HandleScope; | ||
| 12 | + using v8::Isolate; | ||
| 13 | + using v8::Local; | ||
| 14 | + using v8::Value; | ||
| 15 | + | ||
| 16 | + enum encoding ParseEncoding(const char* encoding, | ||
| 17 | + enum encoding default_encoding) { | ||
| 18 | + switch (encoding[0]) { | ||
| 19 | + case 'u': | ||
| 20 | + // utf8, utf16le | ||
| 21 | + if (encoding[1] == 't' && encoding[2] == 'f') { | ||
| 22 | + // Skip `-` | ||
| 23 | + encoding += encoding[3] == '-' ? 4 : 3; | ||
| 24 | + if (encoding[0] == '8' && encoding[1] == '\0') | ||
| 25 | + return UTF8; | ||
| 26 | + if (strncmp(encoding, "16le", 4) == 0) | ||
| 27 | + return UCS2; | ||
| 28 | + | ||
| 29 | + // ucs2 | ||
| 30 | + } else if (encoding[1] == 'c' && encoding[2] == 's') { | ||
| 31 | + encoding += encoding[3] == '-' ? 4 : 3; | ||
| 32 | + if (encoding[0] == '2' && encoding[1] == '\0') | ||
| 33 | + return UCS2; | ||
| 34 | + } | ||
| 35 | + break; | ||
| 36 | + case 'l': | ||
| 37 | + // latin1 | ||
| 38 | + if (encoding[1] == 'a') { | ||
| 39 | + if (strncmp(encoding + 2, "tin1", 4) == 0) | ||
| 40 | + return LATIN1; | ||
| 41 | + } | ||
| 42 | + break; | ||
| 43 | + case 'b': | ||
| 44 | + // binary | ||
| 45 | + if (encoding[1] == 'i') { | ||
| 46 | + if (strncmp(encoding + 2, "nary", 4) == 0) | ||
| 47 | + return LATIN1; | ||
| 48 | + | ||
| 49 | + // buffer | ||
| 50 | + } else if (encoding[1] == 'u') { | ||
| 51 | + if (strncmp(encoding + 2, "ffer", 4) == 0) | ||
| 52 | + return BUFFER; | ||
| 53 | + } | ||
| 54 | + break; | ||
| 55 | + case '\0': | ||
| 56 | + return default_encoding; | ||
| 57 | + default: | ||
| 58 | + break; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + if (StringEqualNoCase(encoding, "utf8")) { | ||
| 62 | + return UTF8; | ||
| 63 | + } else if (StringEqualNoCase(encoding, "utf-8")) { | ||
| 64 | + return UTF8; | ||
| 65 | + } else if (StringEqualNoCase(encoding, "ascii")) { | ||
| 66 | + return ASCII; | ||
| 67 | + } else if (StringEqualNoCase(encoding, "base64")) { | ||
| 68 | + return BASE64; | ||
| 69 | + } else if (StringEqualNoCase(encoding, "ucs2")) { | ||
| 70 | + return UCS2; | ||
| 71 | + } else if (StringEqualNoCase(encoding, "ucs-2")) { | ||
| 72 | + return UCS2; | ||
| 73 | + } else if (StringEqualNoCase(encoding, "utf16le")) { | ||
| 74 | + return UCS2; | ||
| 75 | + } else if (StringEqualNoCase(encoding, "utf-16le")) { | ||
| 76 | + return UCS2; | ||
| 77 | + } else if (StringEqualNoCase(encoding, "latin1")) { | ||
| 78 | + return LATIN1; | ||
| 79 | + } else if (StringEqualNoCase(encoding, "binary")) { | ||
| 80 | + return LATIN1; // BINARY is a deprecated alias of LATIN1. | ||
| 81 | + } else if (StringEqualNoCase(encoding, "buffer")) { | ||
| 82 | + return BUFFER; | ||
| 83 | + } else if (StringEqualNoCase(encoding, "hex")) { | ||
| 84 | + return HEX; | ||
| 85 | + } else { | ||
| 86 | + return default_encoding; | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + enum encoding ParseEncoding(Isolate* isolate, | ||
| 92 | + Local<Value> encoding_v, | ||
| 93 | + enum encoding default_encoding) { | ||
| 94 | + CHECK(!encoding_v.IsEmpty()); | ||
| 95 | + | ||
| 96 | + if (!encoding_v->IsString()) | ||
| 97 | + return default_encoding; | ||
| 98 | + | ||
| 99 | + Utf8Value encoding(isolate, encoding_v); | ||
| 100 | + | ||
| 101 | + return ParseEncoding(*encoding, default_encoding); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + Local<Value> Encode(Isolate* isolate, | ||
| 105 | + const char* buf, | ||
| 106 | + size_t len, | ||
| 107 | + enum encoding encoding) { | ||
| 108 | + CHECK_NE(encoding, UCS2); | ||
| 109 | + Local<Value> error; | ||
| 110 | + return StringBytes::Encode(isolate, buf, len, encoding, &error) | ||
| 111 | + .ToLocalChecked(); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { | ||
| 115 | + Local<Value> error; | ||
| 116 | + return StringBytes::Encode(isolate, buf, len, &error) | ||
| 117 | + .ToLocalChecked(); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + // Returns -1 if the handle was not valid for decoding | ||
| 121 | + ssize_t DecodeBytes(Isolate* isolate, | ||
| 122 | + Local<Value> val, | ||
| 123 | + enum encoding encoding) { | ||
| 124 | + HandleScope scope(isolate); | ||
| 125 | + | ||
| 126 | + return StringBytes::Size(isolate, val, encoding); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + // Returns number of bytes written. | ||
| 130 | + ssize_t DecodeWrite(Isolate* isolate, | ||
| 131 | + char* buf, | ||
| 132 | + size_t buflen, | ||
| 133 | + Local<Value> val, | ||
| 134 | + enum encoding encoding) { | ||
| 135 | + return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + } // namespace node | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments