@@ -16,6 +16,7 @@ NAN_METHOD(DetectCharacterEncoding) {
16
16
17
17
if (U_FAILURE (errorCode)) {
18
18
Nan::ThrowError (" Failed to open ICU charset detector." );
19
+ return ;
19
20
}
20
21
21
22
ucsdet_setText (
@@ -27,31 +28,40 @@ NAN_METHOD(DetectCharacterEncoding) {
27
28
28
29
if (U_FAILURE (errorCode)) {
29
30
Nan::ThrowError (" Failed to set ICU charset detector’s text." );
31
+ ucsdet_close (charsetDetector);
32
+ return ;
30
33
}
31
34
32
35
charsetMatch = ucsdet_detect (charsetDetector, &errorCode);
33
36
34
37
if (U_FAILURE (errorCode)) {
35
38
Nan::ThrowError (" Failed to detect charset." );
39
+ ucsdet_close (charsetDetector);
40
+ return ;
36
41
}
37
42
38
43
const char *charsetName = ucsdet_getName (charsetMatch, &errorCode);
39
44
40
45
if (U_FAILURE (errorCode)) {
41
46
Nan::ThrowError (" Failed to get name from charset match." );
47
+ ucsdet_close (charsetDetector);
48
+ return ;
42
49
}
43
50
44
51
int32_t confidence = ucsdet_getConfidence (charsetMatch, &errorCode);
45
52
46
53
if (U_FAILURE (errorCode)) {
47
54
Nan::ThrowError (" Failed to get confidence from charset match." );
55
+ ucsdet_close (charsetDetector);
56
+ return ;
48
57
}
49
58
50
59
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
51
60
obj->Set (Nan::New<v8::String>(" encoding" ).ToLocalChecked (), Nan::New<v8::String>(charsetName).ToLocalChecked ());
52
61
obj->Set (Nan::New<v8::String>(" confidence" ).ToLocalChecked (), Nan::New<v8::Number>(confidence));
53
62
54
63
info.GetReturnValue ().Set (obj);
64
+ ucsdet_close (charsetDetector);
55
65
}
56
66
57
67
void Init (v8::Local<v8::Object> exports) {
0 commit comments