Compare commits
	
		
			1 Commits
		
	
	
		
			master
			...
			translit-i
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					b5a0918160 | 
							
								
								
									
										20
									
								
								converter.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								converter.go
									
									
									
									
									
								
							@ -4,21 +4,23 @@ package iconv
 | 
			
		||||
#cgo darwin LDFLAGS: -liconv
 | 
			
		||||
#cgo freebsd LDFLAGS: -liconv
 | 
			
		||||
#cgo windows LDFLAGS: -liconv
 | 
			
		||||
#cgo LDFLAGS: -liconv
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <iconv.h>
 | 
			
		||||
#include <locale.h>
 | 
			
		||||
 | 
			
		||||
// As of GO 1.6 passing a pointer to Go pointer, will lead to panic
 | 
			
		||||
// Therofore we use this wrapper function, to avoid passing **char directly from go
 | 
			
		||||
size_t call_iconv(iconv_t ctx, char *in, size_t *size_in, char *out, size_t *size_out){
 | 
			
		||||
	return iconv(ctx, &in, size_in, &out, size_out);
 | 
			
		||||
// called by init, seems to be necessary for TRANSLIT to work
 | 
			
		||||
void initLocale() {
 | 
			
		||||
	setlocale(LC_ALL, "");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
import "C"
 | 
			
		||||
import "syscall"
 | 
			
		||||
import "unsafe"
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	C.initLocale()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Converter struct {
 | 
			
		||||
	context C.iconv_t
 | 
			
		||||
	open    bool
 | 
			
		||||
@ -88,7 +90,7 @@ func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, byte
 | 
			
		||||
			inputPointer := (*C.char)(unsafe.Pointer(&input[0]))
 | 
			
		||||
			outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
 | 
			
		||||
 | 
			
		||||
			_, err = C.call_iconv(this.context, inputPointer, &inputLeft, outputPointer, &outputLeft)
 | 
			
		||||
			_, err = C.iconv(this.context, &inputPointer, &inputLeft, &outputPointer, &outputLeft)
 | 
			
		||||
 | 
			
		||||
			// update byte counters
 | 
			
		||||
			bytesRead = len(input) - int(inputLeft)
 | 
			
		||||
@ -97,13 +99,13 @@ func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, byte
 | 
			
		||||
			// inputPointer will be nil, outputPointer is generated as above
 | 
			
		||||
			outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
 | 
			
		||||
 | 
			
		||||
			_, err = C.call_iconv(this.context, nil, &inputLeft, outputPointer, &outputLeft)
 | 
			
		||||
			_, err = C.iconv(this.context, nil, &inputLeft, &outputPointer, &outputLeft)
 | 
			
		||||
 | 
			
		||||
			// update write byte counter
 | 
			
		||||
			bytesWritten = len(output) - int(outputLeft)
 | 
			
		||||
		} else {
 | 
			
		||||
			// both input and output are zero length, do a shift state reset
 | 
			
		||||
			_, err = C.call_iconv(this.context, nil, &inputLeft, nil, &outputLeft)
 | 
			
		||||
			_, err = C.iconv(this.context, nil, &inputLeft, nil, &outputLeft)
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		err = syscall.EBADF
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user