<commit_before>// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package guid

import (
	"errors"
	"github.com/gogf/gf/container/gtype"
	"net"
	"time"
)

const (
	BitLenTime      = 39                               // Bit length of time.
	BitLenSequence  = 8                                // Bit length of sequence number.
	BitLenMachineID = 63 - BitLenTime - BitLenSequence // Bit length of machine id.
)

var (
	// MachineId is 16 bits number composed with the last two parts of the first local private ip.
	// You can change it as your custom machine id in boot time, but do not change it in runtime.
	// Note that if there's no net card on the machine, it panics when the process boots.
	MachineId uint16

	// sequenceNumber is used for internal concurrent-safe sequence number counting.
	sequenceNumber = gtype.NewInt()
)

func init() {
	if MachineId != 0 {
		return
	}
	ip, err := getPrivateIPv4()
	if err != nil {
		panic(err)
	}
	MachineId = uint16(ip[2])<<8 + uint16(ip[3])
}

// I creates and returns an uint64 id which using improved SnowFlake algorithm.
// An Improved SnowFlake ID is composed of:
//     39 bits for time in units of 10 msec
//     16 bits for a machine id
//      8 bits for a sequence number.
func I() uint64 {
	return uint64(time.Now().UnixNano())<<(BitLenMachineID+BitLenSequence) | uint64(MachineId<<BitLenSequence) | uint64(sequenceNumber.Add(1)%0xFF)
}

func getPrivateIPv4() (net.IP, error) {
	as, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	for _, a := range as {
		ipNet, ok := a.(*net.IPNet)
		if !ok || ipNet.IP.IsLoopback() {
			continue
		}

		ip := ipNet.IP.To4()
		if isPrivateIPv4(ip) {
			return ip, nil
		}
	}
	return nil, errors.New("no private ip address")<commit_msg>improve snowflake number generating for package guid<commit_after>// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package guid

import (
	"errors"
	"github.com/gogf/gf/container/gtype"
	"net"
	"time"
)

const (
	BitLenTime      = 39                               // Bit length of time.
	BitLenSequence  = 8                                // Bit length of sequence number.
	BitLenMachineID = 63 - BitLenTime - BitLenSequence // Bit length of machine id.
)

var (
	// MachineId is 16 bits number composed with the last two parts of the first local private ip.
	// You can change it as your custom machine id in boot time, but do not change it in runtime.
	// Note that if there's no net card on the machine, it panics when the process boots.
	MachineId uint16

	// sequenceNumber is used for internal concurrent-safe sequence number counting.
	sequenceNumber = gtype.NewInt()
)

func init() {
	if MachineId != 0 {
		return
	}
	ip, err := getPrivateIPv4()
	if err != nil {
		panic(err)
	}
	MachineId = uint16(ip[2])<<8 | uint16(ip[3])
}

// I creates and returns an uint64 id which using improved SnowFlake algorithm.
// An Improved SnowFlake ID is composed of:
//     39 bits for time in units of 10 msec
//     16 bits for a machine id
//      8 bits for a sequence number.
func I() uint64 {
	return uint64(time.Now().UnixNano())<<(BitLenMachineID+BitLenSequence) | uint64(MachineId<<BitLenSequence) | uint64(sequenceNumber.Add(1)%0xFF)
}

func getPrivateIPv4() (net.IP, error) {
	as, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	for _, a := range as {
		ipNet, ok := a.(*net.IPNet)
		if !ok || ipNet.IP.IsLoopback() {
			continue
		}

		ip := ipNet.IP.To4()
		if isPrivateIPv4(ip) {
			return ip, nil
		}
	}
	return nil, errors.New("no private ip address")
{'fraction_non_alphanumeric': 0.10888832615852115, 'fraction_numerical': 0.022284122562674095, 'mean_word_length': 2.8461538461538463, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7555752', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1276, 'n_words': 475}