From bb8f7fa69c24725de0374be4e561c3a46992c30c Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Thu, 8 Jan 2026 15:33:03 +0800 Subject: [PATCH 01/19] =?UTF-8?q?chore:=20=E9=87=8D=E6=9E=84=E5=8C=85?= =?UTF-8?q?=E5=90=8D=E5=B9=B6=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E8=87=B32026.0108.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重命名所有 NuGet 包为 Joce.EasyTool.* 前缀 - EasyTool.Core → Joce.EasyTool.Core - EasyTool.EmitMapper → Joce.EasyTool.EmitMapper - EasyTool.Web → Joce.EasyTool.Web - EasyTool.NPOI → Joce.EasyTool.NPOI - EasyTool.Image → Joce.EasyTool.Image - 更新所有项目版本号至 2026.0108.1 - 完善 NuGet 打包配置 - 为 EasyTool.Web 和 EasyTool.NPOI 添加完整的包元数据 - 统一所有项目的 PackageId、Authors、RepositoryUrl 等配置 --- .spec-workflow/templates/design-template.md | 96 ++++++++++++ .spec-workflow/templates/product-template.md | 51 ++++++ .../templates/requirements-template.md | 50 ++++++ .../templates/structure-template.md | 145 ++++++++++++++++++ .spec-workflow/templates/tasks-template.md | 139 +++++++++++++++++ .spec-workflow/templates/tech-template.md | 99 ++++++++++++ .spec-workflow/user-templates/README.md | 64 ++++++++ .../DictionaryExtension.cs | 4 +- EasyTool.Core/EasyTool.Core.csproj | 21 ++- EasyTool.Core/IOCategory/FileTypeUtil.cs | 11 +- EasyTool.Core/IOCategory/FileUtil.cs | 30 ++-- EasyTool.Core/IOCategory/WatchMonitor.cs | 6 +- EasyTool.Core/LanguageCategory/TreeUtil.cs | 15 +- EasyTool.Core/NetCategory/NetUtil.cs | 27 ++-- EasyTool.Core/ToolCategory/EnumUtil.cs | 14 +- EasyTool.Core/ToolCategory/IdcardUtil.cs | 4 +- EasyTool.Core/ToolCategory/MEFUtil.cs | 4 +- EasyTool.Core/ToolCategory/ObjectUtil.cs | 42 ++--- EasyTool.Core/ToolCategory/RuntimeUtil.cs | 20 ++- EasyTool.Core/ToolCategory/XmlUtil.cs | 4 +- EasyTool.CoreTests/EasyTool.CoreTests.csproj | 15 +- .../ToolCategory/IpUtilTests.cs | 20 ++- .../EasyTool.EmitMapper.csproj | 9 +- .../EasyTool.EmitMapperTests.csproj | 14 +- EasyTool.Image/EasyTool.Image.csproj | 11 +- .../EasyTool.ImageTests.csproj | 12 +- EasyTool.NPOI/EasyTool.NPOI.csproj | 67 ++++---- EasyTool.NPOITests/EasyTool.NPOITests.csproj | 12 +- EasyTool.Web/EasyTool.Web.csproj | 40 ++++- EasyTool.WebTests/EasyTool.WebTests.csproj | 12 +- 30 files changed, 904 insertions(+), 154 deletions(-) create mode 100644 .spec-workflow/templates/design-template.md create mode 100644 .spec-workflow/templates/product-template.md create mode 100644 .spec-workflow/templates/requirements-template.md create mode 100644 .spec-workflow/templates/structure-template.md create mode 100644 .spec-workflow/templates/tasks-template.md create mode 100644 .spec-workflow/templates/tech-template.md create mode 100644 .spec-workflow/user-templates/README.md diff --git a/.spec-workflow/templates/design-template.md b/.spec-workflow/templates/design-template.md new file mode 100644 index 0000000..1295d7b --- /dev/null +++ b/.spec-workflow/templates/design-template.md @@ -0,0 +1,96 @@ +# Design Document + +## Overview + +[High-level description of the feature and its place in the overall system] + +## Steering Document Alignment + +### Technical Standards (tech.md) +[How the design follows documented technical patterns and standards] + +### Project Structure (structure.md) +[How the implementation will follow project organization conventions] + +## Code Reuse Analysis +[What existing code will be leveraged, extended, or integrated with this feature] + +### Existing Components to Leverage +- **[Component/Utility Name]**: [How it will be used] +- **[Service/Helper Name]**: [How it will be extended] + +### Integration Points +- **[Existing System/API]**: [How the new feature will integrate] +- **[Database/Storage]**: [How data will connect to existing schemas] + +## Architecture + +[Describe the overall architecture and design patterns used] + +### Modular Design Principles +- **Single File Responsibility**: Each file should handle one specific concern or domain +- **Component Isolation**: Create small, focused components rather than large monolithic files +- **Service Layer Separation**: Separate data access, business logic, and presentation layers +- **Utility Modularity**: Break utilities into focused, single-purpose modules + +```mermaid +graph TD + A[Component A] --> B[Component B] + B --> C[Component C] +``` + +## Components and Interfaces + +### Component 1 +- **Purpose:** [What this component does] +- **Interfaces:** [Public methods/APIs] +- **Dependencies:** [What it depends on] +- **Reuses:** [Existing components/utilities it builds upon] + +### Component 2 +- **Purpose:** [What this component does] +- **Interfaces:** [Public methods/APIs] +- **Dependencies:** [What it depends on] +- **Reuses:** [Existing components/utilities it builds upon] + +## Data Models + +### Model 1 +``` +[Define the structure of Model1 in your language] +- id: [unique identifier type] +- name: [string/text type] +- [Additional properties as needed] +``` + +### Model 2 +``` +[Define the structure of Model2 in your language] +- id: [unique identifier type] +- [Additional properties as needed] +``` + +## Error Handling + +### Error Scenarios +1. **Scenario 1:** [Description] + - **Handling:** [How to handle] + - **User Impact:** [What user sees] + +2. **Scenario 2:** [Description] + - **Handling:** [How to handle] + - **User Impact:** [What user sees] + +## Testing Strategy + +### Unit Testing +- [Unit testing approach] +- [Key components to test] + +### Integration Testing +- [Integration testing approach] +- [Key flows to test] + +### End-to-End Testing +- [E2E testing approach] +- [User scenarios to test] diff --git a/.spec-workflow/templates/product-template.md b/.spec-workflow/templates/product-template.md new file mode 100644 index 0000000..82e60de --- /dev/null +++ b/.spec-workflow/templates/product-template.md @@ -0,0 +1,51 @@ +# Product Overview + +## Product Purpose +[Describe the core purpose of this product/project. What problem does it solve?] + +## Target Users +[Who are the primary users of this product? What are their needs and pain points?] + +## Key Features +[List the main features that deliver value to users] + +1. **Feature 1**: [Description] +2. **Feature 2**: [Description] +3. **Feature 3**: [Description] + +## Business Objectives +[What are the business goals this product aims to achieve?] + +- [Objective 1] +- [Objective 2] +- [Objective 3] + +## Success Metrics +[How will we measure the success of this product?] + +- [Metric 1]: [Target] +- [Metric 2]: [Target] +- [Metric 3]: [Target] + +## Product Principles +[Core principles that guide product decisions] + +1. **[Principle 1]**: [Explanation] +2. **[Principle 2]**: [Explanation] +3. **[Principle 3]**: [Explanation] + +## Monitoring & Visibility (if applicable) +[How do users track progress and monitor the system?] + +- **Dashboard Type**: [e.g., Web-based, CLI, Desktop app] +- **Real-time Updates**: [e.g., WebSocket, polling, push notifications] +- **Key Metrics Displayed**: [What information is most important to surface] +- **Sharing Capabilities**: [e.g., read-only links, exports, reports] + +## Future Vision +[Where do we see this product evolving in the future?] + +### Potential Enhancements +- **Remote Access**: [e.g., Tunnel features for sharing dashboards with stakeholders] +- **Analytics**: [e.g., Historical trends, performance metrics] +- **Collaboration**: [e.g., Multi-user support, commenting] diff --git a/.spec-workflow/templates/requirements-template.md b/.spec-workflow/templates/requirements-template.md new file mode 100644 index 0000000..1c80ca0 --- /dev/null +++ b/.spec-workflow/templates/requirements-template.md @@ -0,0 +1,50 @@ +# Requirements Document + +## Introduction + +[Provide a brief overview of the feature, its purpose, and its value to users] + +## Alignment with Product Vision + +[Explain how this feature supports the goals outlined in product.md] + +## Requirements + +### Requirement 1 + +**User Story:** As a [role], I want [feature], so that [benefit] + +#### Acceptance Criteria + +1. WHEN [event] THEN [system] SHALL [response] +2. IF [precondition] THEN [system] SHALL [response] +3. WHEN [event] AND [condition] THEN [system] SHALL [response] + +### Requirement 2 + +**User Story:** As a [role], I want [feature], so that [benefit] + +#### Acceptance Criteria + +1. WHEN [event] THEN [system] SHALL [response] +2. IF [precondition] THEN [system] SHALL [response] + +## Non-Functional Requirements + +### Code Architecture and Modularity +- **Single Responsibility Principle**: Each file should have a single, well-defined purpose +- **Modular Design**: Components, utilities, and services should be isolated and reusable +- **Dependency Management**: Minimize interdependencies between modules +- **Clear Interfaces**: Define clean contracts between components and layers + +### Performance +- [Performance requirements] + +### Security +- [Security requirements] + +### Reliability +- [Reliability requirements] + +### Usability +- [Usability requirements] diff --git a/.spec-workflow/templates/structure-template.md b/.spec-workflow/templates/structure-template.md new file mode 100644 index 0000000..1ab1fbc --- /dev/null +++ b/.spec-workflow/templates/structure-template.md @@ -0,0 +1,145 @@ +# Project Structure + +## Directory Organization + +``` +[Define your project's directory structure. Examples below - adapt to your project type] + +Example for a library/package: +project-root/ +├── src/ # Source code +├── tests/ # Test files +├── docs/ # Documentation +├── examples/ # Usage examples +└── [build/dist/out] # Build output + +Example for an application: +project-root/ +├── [src/app/lib] # Main source code +├── [assets/resources] # Static resources +├── [config/settings] # Configuration +├── [scripts/tools] # Build/utility scripts +└── [tests/spec] # Test files + +Common patterns: +- Group by feature/module +- Group by layer (UI, business logic, data) +- Group by type (models, controllers, views) +- Flat structure for simple projects +``` + +## Naming Conventions + +### Files +- **Components/Modules**: [e.g., `PascalCase`, `snake_case`, `kebab-case`] +- **Services/Handlers**: [e.g., `UserService`, `user_service`, `user-service`] +- **Utilities/Helpers**: [e.g., `dateUtils`, `date_utils`, `date-utils`] +- **Tests**: [e.g., `[filename]_test`, `[filename].test`, `[filename]Test`] + +### Code +- **Classes/Types**: [e.g., `PascalCase`, `CamelCase`, `snake_case`] +- **Functions/Methods**: [e.g., `camelCase`, `snake_case`, `PascalCase`] +- **Constants**: [e.g., `UPPER_SNAKE_CASE`, `SCREAMING_CASE`, `PascalCase`] +- **Variables**: [e.g., `camelCase`, `snake_case`, `lowercase`] + +## Import Patterns + +### Import Order +1. External dependencies +2. Internal modules +3. Relative imports +4. Style imports + +### Module/Package Organization +``` +[Describe your project's import/include patterns] +Examples: +- Absolute imports from project root +- Relative imports within modules +- Package/namespace organization +- Dependency management approach +``` + +## Code Structure Patterns + +[Define common patterns for organizing code within files. Below are examples - choose what applies to your project] + +### Module/Class Organization +``` +Example patterns: +1. Imports/includes/dependencies +2. Constants and configuration +3. Type/interface definitions +4. Main implementation +5. Helper/utility functions +6. Exports/public API +``` + +### Function/Method Organization +``` +Example patterns: +- Input validation first +- Core logic in the middle +- Error handling throughout +- Clear return points +``` + +### File Organization Principles +``` +Choose what works for your project: +- One class/module per file +- Related functionality grouped together +- Public API at the top/bottom +- Implementation details hidden +``` + +## Code Organization Principles + +1. **Single Responsibility**: Each file should have one clear purpose +2. **Modularity**: Code should be organized into reusable modules +3. **Testability**: Structure code to be easily testable +4. **Consistency**: Follow patterns established in the codebase + +## Module Boundaries +[Define how different parts of your project interact and maintain separation of concerns] + +Examples of boundary patterns: +- **Core vs Plugins**: Core functionality vs extensible plugins +- **Public API vs Internal**: What's exposed vs implementation details +- **Platform-specific vs Cross-platform**: OS-specific code isolation +- **Stable vs Experimental**: Production code vs experimental features +- **Dependencies direction**: Which modules can depend on which + +## Code Size Guidelines +[Define your project's guidelines for file and function sizes] + +Suggested guidelines: +- **File size**: [Define maximum lines per file] +- **Function/Method size**: [Define maximum lines per function] +- **Class/Module complexity**: [Define complexity limits] +- **Nesting depth**: [Maximum nesting levels] + +## Dashboard/Monitoring Structure (if applicable) +[How dashboard or monitoring components are organized] + +### Example Structure: +``` +src/ +└── dashboard/ # Self-contained dashboard subsystem + ├── server/ # Backend server components + ├── client/ # Frontend assets + ├── shared/ # Shared types/utilities + └── public/ # Static assets +``` + +### Separation of Concerns +- Dashboard isolated from core business logic +- Own CLI entry point for independent operation +- Minimal dependencies on main application +- Can be disabled without affecting core functionality + +## Documentation Standards +- All public APIs must have documentation +- Complex logic should include inline comments +- README files for major modules +- Follow language-specific documentation conventions diff --git a/.spec-workflow/templates/tasks-template.md b/.spec-workflow/templates/tasks-template.md new file mode 100644 index 0000000..be461de --- /dev/null +++ b/.spec-workflow/templates/tasks-template.md @@ -0,0 +1,139 @@ +# Tasks Document + +- [ ] 1. Create core interfaces in src/types/feature.ts + - File: src/types/feature.ts + - Define TypeScript interfaces for feature data structures + - Extend existing base interfaces from base.ts + - Purpose: Establish type safety for feature implementation + - _Leverage: src/types/base.ts_ + - _Requirements: 1.1_ + - _Prompt: Role: TypeScript Developer specializing in type systems and interfaces | Task: Create comprehensive TypeScript interfaces for the feature data structures following requirements 1.1, extending existing base interfaces from src/types/base.ts | Restrictions: Do not modify existing base interfaces, maintain backward compatibility, follow project naming conventions | Success: All interfaces compile without errors, proper inheritance from base types, full type coverage for feature requirements_ + +- [ ] 2. Create base model class in src/models/FeatureModel.ts + - File: src/models/FeatureModel.ts + - Implement base model extending BaseModel class + - Add validation methods using existing validation utilities + - Purpose: Provide data layer foundation for feature + - _Leverage: src/models/BaseModel.ts, src/utils/validation.ts_ + - _Requirements: 2.1_ + - _Prompt: Role: Backend Developer with expertise in Node.js and data modeling | Task: Create a base model class extending BaseModel and implementing validation following requirement 2.1, leveraging existing patterns from src/models/BaseModel.ts and src/utils/validation.ts | Restrictions: Must follow existing model patterns, do not bypass validation utilities, maintain consistent error handling | Success: Model extends BaseModel correctly, validation methods implemented and tested, follows project architecture patterns_ + +- [ ] 3. Add specific model methods to FeatureModel.ts + - File: src/models/FeatureModel.ts (continue from task 2) + - Implement create, update, delete methods + - Add relationship handling for foreign keys + - Purpose: Complete model functionality for CRUD operations + - _Leverage: src/models/BaseModel.ts_ + - _Requirements: 2.2, 2.3_ + - _Prompt: Role: Backend Developer with expertise in ORM and database operations | Task: Implement CRUD methods and relationship handling in FeatureModel.ts following requirements 2.2 and 2.3, extending patterns from src/models/BaseModel.ts | Restrictions: Must maintain transaction integrity, follow existing relationship patterns, do not duplicate base model functionality | Success: All CRUD operations work correctly, relationships are properly handled, database operations are atomic and efficient_ + +- [ ] 4. Create model unit tests in tests/models/FeatureModel.test.ts + - File: tests/models/FeatureModel.test.ts + - Write tests for model validation and CRUD methods + - Use existing test utilities and fixtures + - Purpose: Ensure model reliability and catch regressions + - _Leverage: tests/helpers/testUtils.ts, tests/fixtures/data.ts_ + - _Requirements: 2.1, 2.2_ + - _Prompt: Role: QA Engineer with expertise in unit testing and Jest/Mocha frameworks | Task: Create comprehensive unit tests for FeatureModel validation and CRUD methods covering requirements 2.1 and 2.2, using existing test utilities from tests/helpers/testUtils.ts and fixtures from tests/fixtures/data.ts | Restrictions: Must test both success and failure scenarios, do not test external dependencies directly, maintain test isolation | Success: All model methods are tested with good coverage, edge cases covered, tests run independently and consistently_ + +- [ ] 5. Create service interface in src/services/IFeatureService.ts + - File: src/services/IFeatureService.ts + - Define service contract with method signatures + - Extend base service interface patterns + - Purpose: Establish service layer contract for dependency injection + - _Leverage: src/services/IBaseService.ts_ + - _Requirements: 3.1_ + - _Prompt: Role: Software Architect specializing in service-oriented architecture and TypeScript interfaces | Task: Design service interface contract following requirement 3.1, extending base service patterns from src/services/IBaseService.ts for dependency injection | Restrictions: Must maintain interface segregation principle, do not expose internal implementation details, ensure contract compatibility with DI container | Success: Interface is well-defined with clear method signatures, extends base service appropriately, supports all required service operations_ + +- [ ] 6. Implement feature service in src/services/FeatureService.ts + - File: src/services/FeatureService.ts + - Create concrete service implementation using FeatureModel + - Add error handling with existing error utilities + - Purpose: Provide business logic layer for feature operations + - _Leverage: src/services/BaseService.ts, src/utils/errorHandler.ts, src/models/FeatureModel.ts_ + - _Requirements: 3.2_ + - _Prompt: Role: Backend Developer with expertise in service layer architecture and business logic | Task: Implement concrete FeatureService following requirement 3.2, using FeatureModel and extending BaseService patterns with proper error handling from src/utils/errorHandler.ts | Restrictions: Must implement interface contract exactly, do not bypass model validation, maintain separation of concerns from data layer | Success: Service implements all interface methods correctly, robust error handling implemented, business logic is well-encapsulated and testable_ + +- [ ] 7. Add service dependency injection in src/utils/di.ts + - File: src/utils/di.ts (modify existing) + - Register FeatureService in dependency injection container + - Configure service lifetime and dependencies + - Purpose: Enable service injection throughout application + - _Leverage: existing DI configuration in src/utils/di.ts_ + - _Requirements: 3.1_ + - _Prompt: Role: DevOps Engineer with expertise in dependency injection and IoC containers | Task: Register FeatureService in DI container following requirement 3.1, configuring appropriate lifetime and dependencies using existing patterns from src/utils/di.ts | Restrictions: Must follow existing DI container patterns, do not create circular dependencies, maintain service resolution efficiency | Success: FeatureService is properly registered and resolvable, dependencies are correctly configured, service lifetime is appropriate for use case_ + +- [ ] 8. Create service unit tests in tests/services/FeatureService.test.ts + - File: tests/services/FeatureService.test.ts + - Write tests for service methods with mocked dependencies + - Test error handling scenarios + - Purpose: Ensure service reliability and proper error handling + - _Leverage: tests/helpers/testUtils.ts, tests/mocks/modelMocks.ts_ + - _Requirements: 3.2, 3.3_ + - _Prompt: Role: QA Engineer with expertise in service testing and mocking frameworks | Task: Create comprehensive unit tests for FeatureService methods covering requirements 3.2 and 3.3, using mocked dependencies from tests/mocks/modelMocks.ts and test utilities | Restrictions: Must mock all external dependencies, test business logic in isolation, do not test framework code | Success: All service methods tested with proper mocking, error scenarios covered, tests verify business logic correctness and error handling_ + +- [ ] 4. Create API endpoints + - Design API structure + - _Leverage: src/api/baseApi.ts, src/utils/apiUtils.ts_ + - _Requirements: 4.0_ + - _Prompt: Role: API Architect specializing in RESTful design and Express.js | Task: Design comprehensive API structure following requirement 4.0, leveraging existing patterns from src/api/baseApi.ts and utilities from src/utils/apiUtils.ts | Restrictions: Must follow REST conventions, maintain API versioning compatibility, do not expose internal data structures directly | Success: API structure is well-designed and documented, follows existing patterns, supports all required operations with proper HTTP methods and status codes_ + +- [ ] 4.1 Set up routing and middleware + - Configure application routes + - Add authentication middleware + - Set up error handling middleware + - _Leverage: src/middleware/auth.ts, src/middleware/errorHandler.ts_ + - _Requirements: 4.1_ + - _Prompt: Role: Backend Developer with expertise in Express.js middleware and routing | Task: Configure application routes and middleware following requirement 4.1, integrating authentication from src/middleware/auth.ts and error handling from src/middleware/errorHandler.ts | Restrictions: Must maintain middleware order, do not bypass security middleware, ensure proper error propagation | Success: Routes are properly configured with correct middleware chain, authentication works correctly, errors are handled gracefully throughout the request lifecycle_ + +- [ ] 4.2 Implement CRUD endpoints + - Create API endpoints + - Add request validation + - Write API integration tests + - _Leverage: src/controllers/BaseController.ts, src/utils/validation.ts_ + - _Requirements: 4.2, 4.3_ + - _Prompt: Role: Full-stack Developer with expertise in API development and validation | Task: Implement CRUD endpoints following requirements 4.2 and 4.3, extending BaseController patterns and using validation utilities from src/utils/validation.ts | Restrictions: Must validate all inputs, follow existing controller patterns, ensure proper HTTP status codes and responses | Success: All CRUD operations work correctly, request validation prevents invalid data, integration tests pass and cover all endpoints_ + +- [ ] 5. Add frontend components + - Plan component architecture + - _Leverage: src/components/BaseComponent.tsx, src/styles/theme.ts_ + - _Requirements: 5.0_ + - _Prompt: Role: Frontend Architect with expertise in React component design and architecture | Task: Plan comprehensive component architecture following requirement 5.0, leveraging base patterns from src/components/BaseComponent.tsx and theme system from src/styles/theme.ts | Restrictions: Must follow existing component patterns, maintain design system consistency, ensure component reusability | Success: Architecture is well-planned and documented, components are properly organized, follows existing patterns and theme system_ + +- [ ] 5.1 Create base UI components + - Set up component structure + - Implement reusable components + - Add styling and theming + - _Leverage: src/components/BaseComponent.tsx, src/styles/theme.ts_ + - _Requirements: 5.1_ + - _Prompt: Role: Frontend Developer specializing in React and component architecture | Task: Create reusable UI components following requirement 5.1, extending BaseComponent patterns and using existing theme system from src/styles/theme.ts | Restrictions: Must use existing theme variables, follow component composition patterns, ensure accessibility compliance | Success: Components are reusable and properly themed, follow existing architecture, accessible and responsive_ + +- [ ] 5.2 Implement feature-specific components + - Create feature components + - Add state management + - Connect to API endpoints + - _Leverage: src/hooks/useApi.ts, src/components/BaseComponent.tsx_ + - _Requirements: 5.2, 5.3_ + - _Prompt: Role: React Developer with expertise in state management and API integration | Task: Implement feature-specific components following requirements 5.2 and 5.3, using API hooks from src/hooks/useApi.ts and extending BaseComponent patterns | Restrictions: Must use existing state management patterns, handle loading and error states properly, maintain component performance | Success: Components are fully functional with proper state management, API integration works smoothly, user experience is responsive and intuitive_ + +- [ ] 6. Integration and testing + - Plan integration approach + - _Leverage: src/utils/integrationUtils.ts, tests/helpers/testUtils.ts_ + - _Requirements: 6.0_ + - _Prompt: Role: Integration Engineer with expertise in system integration and testing strategies | Task: Plan comprehensive integration approach following requirement 6.0, leveraging integration utilities from src/utils/integrationUtils.ts and test helpers | Restrictions: Must consider all system components, ensure proper test coverage, maintain integration test reliability | Success: Integration plan is comprehensive and feasible, all system components work together correctly, integration points are well-tested_ + +- [ ] 6.1 Write end-to-end tests + - Set up E2E testing framework + - Write user journey tests + - Add test automation + - _Leverage: tests/helpers/testUtils.ts, tests/fixtures/data.ts_ + - _Requirements: All_ + - _Prompt: Role: QA Automation Engineer with expertise in E2E testing and test frameworks like Cypress or Playwright | Task: Implement comprehensive end-to-end tests covering all requirements, setting up testing framework and user journey tests using test utilities and fixtures | Restrictions: Must test real user workflows, ensure tests are maintainable and reliable, do not test implementation details | Success: E2E tests cover all critical user journeys, tests run reliably in CI/CD pipeline, user experience is validated from end-to-end_ + +- [ ] 6.2 Final integration and cleanup + - Integrate all components + - Fix any integration issues + - Clean up code and documentation + - _Leverage: src/utils/cleanup.ts, docs/templates/_ + - _Requirements: All_ + - _Prompt: Role: Senior Developer with expertise in code quality and system integration | Task: Complete final integration of all components and perform comprehensive cleanup covering all requirements, using cleanup utilities and documentation templates | Restrictions: Must not break existing functionality, ensure code quality standards are met, maintain documentation consistency | Success: All components are fully integrated and working together, code is clean and well-documented, system meets all requirements and quality standards_ diff --git a/.spec-workflow/templates/tech-template.md b/.spec-workflow/templates/tech-template.md new file mode 100644 index 0000000..57cd538 --- /dev/null +++ b/.spec-workflow/templates/tech-template.md @@ -0,0 +1,99 @@ +# Technology Stack + +## Project Type +[Describe what kind of project this is: web application, CLI tool, desktop application, mobile app, library, API service, embedded system, game, etc.] + +## Core Technologies + +### Primary Language(s) +- **Language**: [e.g., Python 3.11, Go 1.21, TypeScript, Rust, C++] +- **Runtime/Compiler**: [if applicable] +- **Language-specific tools**: [package managers, build tools, etc.] + +### Key Dependencies/Libraries +[List the main libraries and frameworks your project depends on] +- **[Library/Framework name]**: [Purpose and version] +- **[Library/Framework name]**: [Purpose and version] + +### Application Architecture +[Describe how your application is structured - this could be MVC, event-driven, plugin-based, client-server, standalone, microservices, monolithic, etc.] + +### Data Storage (if applicable) +- **Primary storage**: [e.g., PostgreSQL, files, in-memory, cloud storage] +- **Caching**: [e.g., Redis, in-memory, disk cache] +- **Data formats**: [e.g., JSON, Protocol Buffers, XML, binary] + +### External Integrations (if applicable) +- **APIs**: [External services you integrate with] +- **Protocols**: [e.g., HTTP/REST, gRPC, WebSocket, TCP/IP] +- **Authentication**: [e.g., OAuth, API keys, certificates] + +### Monitoring & Dashboard Technologies (if applicable) +- **Dashboard Framework**: [e.g., React, Vue, vanilla JS, terminal UI] +- **Real-time Communication**: [e.g., WebSocket, Server-Sent Events, polling] +- **Visualization Libraries**: [e.g., Chart.js, D3, terminal graphs] +- **State Management**: [e.g., Redux, Vuex, file system as source of truth] + +## Development Environment + +### Build & Development Tools +- **Build System**: [e.g., Make, CMake, Gradle, npm scripts, cargo] +- **Package Management**: [e.g., pip, npm, cargo, go mod, apt, brew] +- **Development workflow**: [e.g., hot reload, watch mode, REPL] + +### Code Quality Tools +- **Static Analysis**: [Tools for code quality and correctness] +- **Formatting**: [Code style enforcement tools] +- **Testing Framework**: [Unit, integration, and/or end-to-end testing tools] +- **Documentation**: [Documentation generation tools] + +### Version Control & Collaboration +- **VCS**: [e.g., Git, Mercurial, SVN] +- **Branching Strategy**: [e.g., Git Flow, GitHub Flow, trunk-based] +- **Code Review Process**: [How code reviews are conducted] + +### Dashboard Development (if applicable) +- **Live Reload**: [e.g., Hot module replacement, file watchers] +- **Port Management**: [e.g., Dynamic allocation, configurable ports] +- **Multi-Instance Support**: [e.g., Running multiple dashboards simultaneously] + +## Deployment & Distribution (if applicable) +- **Target Platform(s)**: [Where/how the project runs: cloud, on-premise, desktop, mobile, embedded] +- **Distribution Method**: [How users get your software: download, package manager, app store, SaaS] +- **Installation Requirements**: [Prerequisites, system requirements] +- **Update Mechanism**: [How updates are delivered] + +## Technical Requirements & Constraints + +### Performance Requirements +- [e.g., response time, throughput, memory usage, startup time] +- [Specific benchmarks or targets] + +### Compatibility Requirements +- **Platform Support**: [Operating systems, architectures, versions] +- **Dependency Versions**: [Minimum/maximum versions of dependencies] +- **Standards Compliance**: [Industry standards, protocols, specifications] + +### Security & Compliance +- **Security Requirements**: [Authentication, encryption, data protection] +- **Compliance Standards**: [GDPR, HIPAA, SOC2, etc. if applicable] +- **Threat Model**: [Key security considerations] + +### Scalability & Reliability +- **Expected Load**: [Users, requests, data volume] +- **Availability Requirements**: [Uptime targets, disaster recovery] +- **Growth Projections**: [How the system needs to scale] + +## Technical Decisions & Rationale +[Document key architectural and technology choices] + +### Decision Log +1. **[Technology/Pattern Choice]**: [Why this was chosen, alternatives considered] +2. **[Architecture Decision]**: [Rationale, trade-offs accepted] +3. **[Tool/Library Selection]**: [Reasoning, evaluation criteria] + +## Known Limitations +[Document any technical debt, limitations, or areas for improvement] + +- [Limitation 1]: [Impact and potential future solutions] +- [Limitation 2]: [Why it exists and when it might be addressed] diff --git a/.spec-workflow/user-templates/README.md b/.spec-workflow/user-templates/README.md new file mode 100644 index 0000000..ad36a48 --- /dev/null +++ b/.spec-workflow/user-templates/README.md @@ -0,0 +1,64 @@ +# User Templates + +This directory allows you to create custom templates that override the default Spec Workflow templates. + +## How to Use Custom Templates + +1. **Create your custom template file** in this directory with the exact same name as the default template you want to override: + - `requirements-template.md` - Override requirements document template + - `design-template.md` - Override design document template + - `tasks-template.md` - Override tasks document template + - `product-template.md` - Override product steering template + - `tech-template.md` - Override tech steering template + - `structure-template.md` - Override structure steering template + +2. **Template Loading Priority**: + - The system first checks this `user-templates/` directory + - If a matching template is found here, it will be used + - Otherwise, the default template from `templates/` will be used + +## Example Custom Template + +To create a custom requirements template: + +1. Create a file named `requirements-template.md` in this directory +2. Add your custom structure, for example: + +```markdown +# Requirements Document + +## Executive Summary +[Your custom section] + +## Business Requirements +[Your custom structure] + +## Technical Requirements +[Your custom fields] + +## Custom Sections +[Add any sections specific to your workflow] +``` + +## Template Variables + +Templates can include placeholders that will be replaced when documents are created: +- `{{projectName}}` - The name of your project +- `{{featureName}}` - The name of the feature being specified +- `{{date}}` - The current date +- `{{author}}` - The document author + +## Best Practices + +1. **Start from defaults**: Copy a default template from `../templates/` as a starting point +2. **Keep structure consistent**: Maintain similar section headers for tool compatibility +3. **Document changes**: Add comments explaining why sections were added/modified +4. **Version control**: Track your custom templates in version control +5. **Test thoroughly**: Ensure custom templates work with the spec workflow tools + +## Notes + +- Custom templates are project-specific and not included in the package distribution +- The `templates/` directory contains the default templates which are updated with each version +- Your custom templates in this directory are preserved during updates +- If a custom template has errors, the system will fall back to the default template diff --git a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs index e3f7900..e681ef2 100644 --- a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs +++ b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -14,7 +14,7 @@ public static class DictionaryExtension /// 要获取值的键 /// 如果字典中不存在该键,则返回的默认值 /// 指定键的值,如果字典中不存在该键,则返回默认值 - public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue = default) + public static TValue? GetValueOrDefault(this IDictionary dictionary, TKey key, TValue? defaultValue = default) { if (dictionary.TryGetValue(key, out TValue value)) { diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index 821383f..405fa8d 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -1,13 +1,16 @@ - + - netstandard2.1;.net6.0 - 11 + netstandard2.1;net10.0 + latest enable + true + $(NoWarn); $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) + Joce.EasyTool.Core 一个大西瓜,TimChen - 2023.0908.1 + 2026.0108.1 A open source C# tool to make .NET easy @@ -19,6 +22,10 @@ logo.png + + + + True @@ -36,9 +43,9 @@ - - - + + + diff --git a/EasyTool.Core/IOCategory/FileTypeUtil.cs b/EasyTool.Core/IOCategory/FileTypeUtil.cs index d2fb426..5cb71cb 100644 --- a/EasyTool.Core/IOCategory/FileTypeUtil.cs +++ b/EasyTool.Core/IOCategory/FileTypeUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -25,10 +25,11 @@ public static string GetType(FileInfo file) byte[] buffer = new byte[256]; using (FileStream fs = file.OpenRead()) { - if (fs.Length >= 256) - fs.Read(buffer, 0, 256); - else - fs.Read(buffer, 0, (int)fs.Length); + int readLength = fs.Read(buffer, 0, buffer.Length); + if (readLength < buffer.Length) + { + // 处理读取不足的情况,虽然对于头部检测通常前几个字节就够了,但为了严谨性 + } } string header = ""; diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index c46db33..772ca51 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Text; using System.Web; @@ -884,7 +885,7 @@ public static string GetFileSuffix(string filePath) /// /// 文件 /// 文件名 - public static string GetFilePrefix(FileInfo file) + public static string? GetFilePrefix(FileInfo file) { if (file == null) { @@ -921,7 +922,7 @@ public static string GetFilePrefix(string filePath) /// /// 文件 /// 类型,文件的扩展名,未找到为null - public static string GetType(FileInfo file) + public static string? GetType(FileInfo file) { return FileTypeUtil.GetType(file); } @@ -954,7 +955,7 @@ public static Stream GetInputStream(string path) /// 文件 /// 编码格式,默认为UTF-8 /// BOM输入流 - public static StreamReader GetBOMInputStream(FileInfo file, Encoding encoding = null) + public static StreamReader GetBOMInputStream(FileInfo file, Encoding? encoding = null) { if (encoding == null) { @@ -998,7 +999,7 @@ public static StreamReader GetBOMInputStream(FileInfo file, Encoding encoding = /// 文件 /// 编码格式,默认为UTF-8 /// 文件读取流 - public static StreamReader GetReader(FileInfo file, Encoding encoding = null) + public static StreamReader GetReader(FileInfo file, Encoding? encoding = null) { if (encoding == null) { @@ -1078,7 +1079,7 @@ public static byte[] ReadBytes(string path) /// 文件 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(FileInfo file, Encoding encoding = null) + public static string ReadString(FileInfo file, Encoding? encoding = null) { if (encoding == null) @@ -1101,7 +1102,7 @@ public static string ReadString(FileInfo file, Encoding encoding = null) /// 文件路径 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(string path, Encoding encoding = null) + public static string ReadString(string path, Encoding? encoding = null) { return ReadString(new FileInfo(path), encoding); // 直接调用另一个重载方法 } @@ -1113,7 +1114,7 @@ public static string ReadString(string path, Encoding encoding = null) /// 网络文件地址 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(Uri url, Encoding encoding = null) + public static string ReadString(Uri url, Encoding? encoding = null) { // 如果未指定编码格式,则默认为UTF-8 if (encoding == null) @@ -1124,11 +1125,12 @@ public static string ReadString(Uri url, Encoding encoding = null) string result; try { - // 创建WebClient对象 - using (WebClient client = new WebClient()) + // 创建HttpClient对象 + using (HttpClient client = new HttpClient()) { // 下载指定地址的文件,并转换为字节数组 - byte[] data = client.DownloadData(url); + // 注意:为了保持同步方法签名,这里使用了同步等待,这在某些上下文中可能会导致死锁 + byte[] data = client.GetByteArrayAsync(url).GetAwaiter().GetResult(); // 将字节数组转换为字符串,并使用指定编码格式解码 result = encoding.GetString(data); } @@ -1148,7 +1150,7 @@ public static string ReadString(Uri url, Encoding encoding = null) /// 文件路径 /// 编码格式,默认为UTF-8 /// - public static string[] ReadAllLines(string path, Encoding encoding = null) + public static string[] ReadAllLines(string path, Encoding? encoding = null) { // 如果未指定编码格式,则默认为 UTF-8 if (encoding == null) @@ -1206,7 +1208,7 @@ public static string GetLineSeparator() /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo WriteString(string content, string path, Encoding encoding = null) + public static FileInfo WriteString(string content, string path, Encoding? encoding = null) { if (encoding == null) { @@ -1274,7 +1276,7 @@ public static FileInfo WriteLines(List list, string path, Encoding encod /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo AppendLines(List list, string path, Encoding encoding = null) + public static FileInfo AppendLines(List list, string path, Encoding? encoding = null) { if (encoding == null) { diff --git a/EasyTool.Core/IOCategory/WatchMonitor.cs b/EasyTool.Core/IOCategory/WatchMonitor.cs index 0db8a47..d9a6634 100644 --- a/EasyTool.Core/IOCategory/WatchMonitor.cs +++ b/EasyTool.Core/IOCategory/WatchMonitor.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -132,8 +132,8 @@ public void Dispose() /// public class FileEventArgs : EventArgs { - public string FilePath { get; } - public Exception Exception { get; } + public string? FilePath { get; } + public Exception? Exception { get; } public FileEventArgs(string path) { diff --git a/EasyTool.Core/LanguageCategory/TreeUtil.cs b/EasyTool.Core/LanguageCategory/TreeUtil.cs index 852a4ce..f437583 100644 --- a/EasyTool.Core/LanguageCategory/TreeUtil.cs +++ b/EasyTool.Core/LanguageCategory/TreeUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -168,10 +168,10 @@ public int GetMinDepth() /// /// 节点 /// 下一个兄弟节点 - public TreeNode GetNextSibling(TreeNode node) + public TreeNode? GetNextSibling(TreeNode node) { var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id.Equals(node.Id)); + var index = siblings.FindIndex(n => n.Id!.Equals(node.Id)); return index + 1 < siblings.Count ? siblings[index + 1] : null; } @@ -180,10 +180,10 @@ public TreeNode GetNextSibling(TreeNode node) /// /// 节点 /// 上一个兄弟节点 - public TreeNode GetPreviousSibling(TreeNode node) + public TreeNode? GetPreviousSibling(TreeNode node) { var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id.Equals(node.Id)); + var index = siblings.FindIndex(n => n.Id!.Equals(node.Id)); return index - 1 >= 0 ? siblings[index - 1] : null; } @@ -192,7 +192,7 @@ public TreeNode GetPreviousSibling(TreeNode node) /// /// 节点 /// 首个子节点 - public TreeNode GetFirstChild(TreeNode node) + public TreeNode? GetFirstChild(TreeNode node) { return node.Children.Count > 0 ? node.Children[0] : null; } @@ -202,7 +202,7 @@ public TreeNode GetFirstChild(TreeNode node) /// /// 节点 /// 最后一个子节点 - public TreeNode GetLastChild(TreeNode node) + public TreeNode? GetLastChild(TreeNode node) { return node.Children.Count > 0 ? node.Children[node.Children.Count - 1] : null; } @@ -296,6 +296,7 @@ public TreeNode(T id, T parentId, string name, int weight, D data) this.Name = name; this.Weight = weight; this.Data = data; + this.Children = new List>(); } } } diff --git a/EasyTool.Core/NetCategory/NetUtil.cs b/EasyTool.Core/NetCategory/NetUtil.cs index 3f41d78..9f5460b 100644 --- a/EasyTool.Core/NetCategory/NetUtil.cs +++ b/EasyTool.Core/NetCategory/NetUtil.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Collections.Generic; +using System.Net.Http; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Net; @@ -38,7 +39,7 @@ public static bool Ping(string host) // Resolve the IP address of a host // 获取指定主机的IP地址 - public static IPAddress GetIpAddress(string host) + public static IPAddress? GetIpAddress(string host) { try { @@ -91,13 +92,14 @@ public static bool IsPortOpen(string host, int port) // Send an HTTP GET request and return the response // 发送HTTP GET请求并返回响应 - [Obsolete("建议使用HttpClient替代此方法")] - public static string HttpGet(string url) + public static string? HttpGet(string url) { try { - WebClient client = new WebClient(); - return client.DownloadString(url); + using (HttpClient client = new HttpClient()) + { + return client.GetStringAsync(url).GetAwaiter().GetResult(); + } } catch { @@ -107,15 +109,16 @@ public static string HttpGet(string url) // Send an HTTP POST request and return the response // 发送HTTP POST请求并返回响应 - [Obsolete("建议使用HttpClient替代此方法")] - public static string HttpPost(string url, string data) + public static string? HttpPost(string url, string data) { try { - WebClient client = new WebClient(); - // 设置请求头的内容类型 - client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; - return client.UploadString(url, data); + using (HttpClient client = new HttpClient()) + { + StringContent content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"); + HttpResponseMessage response = client.PostAsync(url, content).GetAwaiter().GetResult(); + return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + } } catch { diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs index 0079cff..bd3dc3e 100644 --- a/EasyTool.Core/ToolCategory/EnumUtil.cs +++ b/EasyTool.Core/ToolCategory/EnumUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; @@ -197,9 +197,9 @@ public static TEnum GetValueByName(string name) /// 枚举类型 /// 枚举值 /// 与值对应的名称,如果值不存在,则返回null - public static string GetNameByValue(TEnum value) + public static string? GetNameByValue(TEnum value) { - return Enum.GetName(typeof(TEnum), value); + return Enum.GetName(typeof(TEnum), value!); } /// @@ -208,7 +208,7 @@ public static string GetNameByValue(TEnum value) /// 枚举类型 /// 枚举值 /// 与值对应的注释,如果值不存在或未设置注释,则返回null - public static string GetDescriptionByValue(TEnum value) + public static string? GetDescriptionByValue(TEnum value) { var name = GetNameByValue(value); if (string.IsNullOrEmpty(name)) @@ -216,7 +216,7 @@ public static string GetDescriptionByValue(TEnum value) return null; } - return GetDescription(GetValueByName(name)); + return GetDescription(GetValueByName(name!)); } /// @@ -225,7 +225,7 @@ public static string GetDescriptionByValue(TEnum value) /// 枚举类型 /// 枚举值 /// 与值对应的Display名称,如果值不存在或未设置Display名称,则返回null - public static string GetDisplayNameByValue(TEnum value) + public static string? GetDisplayNameByValue(TEnum value) { var name = GetNameByValue(value); if (string.IsNullOrEmpty(name)) @@ -233,7 +233,7 @@ public static string GetDisplayNameByValue(TEnum value) return null; } - return GetDisplayName(GetValueByName(name)); + return GetDisplayName(GetValueByName(name!)); } } } diff --git a/EasyTool.Core/ToolCategory/IdcardUtil.cs b/EasyTool.Core/ToolCategory/IdcardUtil.cs index 015c24c..eee852e 100644 --- a/EasyTool.Core/ToolCategory/IdcardUtil.cs +++ b/EasyTool.Core/ToolCategory/IdcardUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -378,7 +378,7 @@ public static string ReplaceBirthday(string idcard, DateTime birthday) /// 身份证号码 /// 新的性别 /// 新的身份证号码 - public static string ReplaceGender(string idcard, Gender gender) + public static string? ReplaceGender(string idcard, Gender gender) { if (string.IsNullOrEmpty(idcard)) { diff --git a/EasyTool.Core/ToolCategory/MEFUtil.cs b/EasyTool.Core/ToolCategory/MEFUtil.cs index 24d3aeb..481bfa5 100644 --- a/EasyTool.Core/ToolCategory/MEFUtil.cs +++ b/EasyTool.Core/ToolCategory/MEFUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel.Composition.Hosting; using System.Reflection; @@ -21,7 +21,7 @@ public class MEFUtil /// 导出部件的类型 /// 目录路径 /// 导出部件的列表 - public static IEnumerable LoadExportParts(string directory = null) + public static IEnumerable LoadExportParts(string? directory = null) { // 如果目录为空,则使用默认目录 directory ??= DefaultDirectory; diff --git a/EasyTool.Core/ToolCategory/ObjectUtil.cs b/EasyTool.Core/ToolCategory/ObjectUtil.cs index a17c076..a2f3bcd 100644 --- a/EasyTool.Core/ToolCategory/ObjectUtil.cs +++ b/EasyTool.Core/ToolCategory/ObjectUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; @@ -22,7 +22,7 @@ public class ObjectUtil /// /// 检查对象是否为 null /// - public static bool IsNull(object obj) + public static bool IsNull(object? obj) { return obj == null; } @@ -30,7 +30,7 @@ public static bool IsNull(object obj) /// /// 检查对象是否不为 null /// - public static bool IsNotNull(object obj) + public static bool IsNotNull(object? obj) { return obj != null; } @@ -38,7 +38,7 @@ public static bool IsNotNull(object obj) /// /// 检查对象是否为空(null 或者 空字符串或空白字符) /// - public static bool IsNullOrEmpty(object obj) + public static bool IsNullOrEmpty(object? obj) { if (IsNull(obj)) { @@ -61,7 +61,7 @@ public static bool IsNullOrEmpty(object obj) /// /// 检查对象是否不为空(非 null 且 非空字符串 或者 非空集合) /// - public static bool IsNotNullOrEmpty(object obj) + public static bool IsNotNullOrEmpty(object? obj) { return !IsNullOrEmpty(obj); } @@ -103,7 +103,7 @@ public static T Convert(object obj) /// /// 将对象转换为指定类型 /// - public static object Convert(object obj, Type targetType) + public static object? Convert(object obj, Type targetType) { if (IsNull(obj)) { @@ -136,7 +136,7 @@ public static IEnumerable GetProperties(object obj) /// /// 获取对象的属性值 /// - public static object GetPropertyValue(object obj, string propertyName) + public static object? GetPropertyValue(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName)?.GetValue(obj); } @@ -144,7 +144,7 @@ public static object GetPropertyValue(object obj, string propertyName) /// /// 设置对象的属性值 /// - public static void SetPropertyValue(object obj, string propertyName, object value) + public static void SetPropertyValue(object obj, string propertyName, object? value) { obj.GetType().GetProperty(propertyName)?.SetValue(obj, value); } @@ -160,7 +160,7 @@ public static IEnumerable GetFields(object obj) /// /// 获取对象的字段值 /// - public static object GetFieldValue(object obj, string fieldName) + public static object? GetFieldValue(object obj, string fieldName) { return obj.GetType().GetField(fieldName)?.GetValue(obj); } @@ -168,7 +168,7 @@ public static object GetFieldValue(object obj, string fieldName) /// /// 设置对象的字段值 /// - public static void SetFieldValue(object obj, string fieldName, object value) + public static void SetFieldValue(object obj, string fieldName, object? value) { obj.GetType().GetField(fieldName)?.SetValue(obj, value); } @@ -307,7 +307,7 @@ public static void ProcessPropertyValue(object obj, string propertyName, Action< /// /// 将对象序列化为 JSON 字符串 /// - public static string ToJson(object obj) + public static string? ToJson(object obj) { if (IsNull(obj)) { @@ -342,7 +342,7 @@ public static T FromJson(string json) /// /// 将对象序列化为 XML 字符串 /// - public static string ToXml(object obj) + public static string? ToXml(object obj) { if (IsNull(obj)) { @@ -377,7 +377,7 @@ public static T FromXml(string xml) /// /// 将对象转换为字典 /// - public static Dictionary ToDictionary(object obj) + public static Dictionary? ToDictionary(object obj) { if (IsNull(obj)) { @@ -488,7 +488,7 @@ public static int GetHashCode(object obj) /// /// 深拷贝对象 /// - public static T DeepClone(T obj) + public static T? DeepClone(T obj) { if (IsNull(obj)) { @@ -546,7 +546,7 @@ public static IEnumerable> ToKeyValuePairs(object o /// /// 深度复制对象 /// - public static object DeepCopy(object obj) + public static object? DeepCopy(object obj) { if (obj == null) { @@ -837,7 +837,7 @@ public static string GetAssemblyQualifiedName(Type type) /// /// 获取指定类型的默认值 /// - public static object GetDefault(Type type) + public static object? GetDefault(Type type) { return type.IsValueType ? Activator.CreateInstance(type) : null; } @@ -961,7 +961,7 @@ public static bool IsEnumerableType(Type type) /// /// 将对象转换为动态扩展对象 /// - public static dynamic ToDynamic(object obj) + public static dynamic? ToDynamic(object obj) { if (obj == null) { @@ -1027,7 +1027,7 @@ public static string SerializeToXml(object obj) /// /// 将 XML 字符串反序列化为指定类型的对象 /// - public static object DeserializeFromXml(string xml, Type type) + public static object? DeserializeFromXml(string xml, Type type) { XmlSerializer serializer = new XmlSerializer(type); @@ -1040,8 +1040,10 @@ public static object DeserializeFromXml(string xml, Type type) /// /// 将对象序列化为二进制数据 /// + [Obsolete("BinaryFormatter is obsolete and unsafe. Use SerializeToJson or SerializeToXml instead.")] public static byte[] SerializeToBinary(object obj) { +#pragma warning disable SYSLIB0011 // 类型或成员已过时 BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) @@ -1049,19 +1051,23 @@ public static byte[] SerializeToBinary(object obj) formatter.Serialize(stream, obj); return stream.ToArray(); } +#pragma warning restore SYSLIB0011 // 类型或成员已过时 } /// /// 将二进制数据反序列化为指定类型的对象 /// + [Obsolete("BinaryFormatter is obsolete and unsafe. Use DeserializeFromJson or DeserializeFromXml instead.")] public static object DeserializeFromBinary(byte[] data, Type type) { +#pragma warning disable SYSLIB0011 // 类型或成员已过时 BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream(data)) { return formatter.Deserialize(stream); } +#pragma warning restore SYSLIB0011 // 类型或成员已过时 } } } diff --git a/EasyTool.Core/ToolCategory/RuntimeUtil.cs b/EasyTool.Core/ToolCategory/RuntimeUtil.cs index e586afc..eb37da6 100644 --- a/EasyTool.Core/ToolCategory/RuntimeUtil.cs +++ b/EasyTool.Core/ToolCategory/RuntimeUtil.cs @@ -1,7 +1,10 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; +#if NET5_0_OR_GREATER +using System.Runtime.Versioning; +#endif using System.Text; namespace EasyTool @@ -72,6 +75,9 @@ public static void ExitApplication() /// 获取当前系统的物理内存总量 /// /// 物理内存总量(字节) +#if NET5_0_OR_GREATER + [SupportedOSPlatform("windows")] +#endif public static long GetTotalPhysicalMemory() { PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); @@ -82,6 +88,9 @@ public static long GetTotalPhysicalMemory() /// 获取当前系统的可用物理内存量 /// /// 可用物理内存量(字节) +#if NET5_0_OR_GREATER + [SupportedOSPlatform("windows")] +#endif public static float GetAvailablePhysicalMemory() { PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); @@ -92,6 +101,9 @@ public static float GetAvailablePhysicalMemory() /// 获取当前系统的虚拟内存总量 /// /// 虚拟内存总量(字节) +#if NET5_0_OR_GREATER + [SupportedOSPlatform("windows")] +#endif public static long GetTotalVirtualMemory() { PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); @@ -102,6 +114,9 @@ public static long GetTotalVirtualMemory() /// 获取当前系统的可用虚拟内存量 /// /// 可用虚拟内存量(字节) +#if NET5_0_OR_GREATER + [SupportedOSPlatform("windows")] +#endif public static float GetAvailableVirtualMemory() { PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); @@ -116,6 +131,9 @@ public static float GetAvailableVirtualMemory() /// 获取当前系统的实际物理内存总量 /// /// 实际物理内存总量(字节) +#if NET5_0_OR_GREATER + [SupportedOSPlatform("windows")] +#endif public static long GetRealTotalPhysicalMemory() { GetPhysicallyInstalledSystemMemory(out long memoryInBytes); diff --git a/EasyTool.Core/ToolCategory/XmlUtil.cs b/EasyTool.Core/ToolCategory/XmlUtil.cs index 0db77ed..92a081d 100644 --- a/EasyTool.Core/ToolCategory/XmlUtil.cs +++ b/EasyTool.Core/ToolCategory/XmlUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Xml; @@ -52,7 +52,7 @@ public static XmlDocument CreateNewXmlDocument() /// 元素的名称。 /// 元素的值。 /// 新创建的XML元素。 - public static XmlElement CreateXmlElement(string name, string value = null) + public static XmlElement CreateXmlElement(string name, string? value = null) { var document = new XmlDocument(); var element = document.CreateElement(name); diff --git a/EasyTool.CoreTests/EasyTool.CoreTests.csproj b/EasyTool.CoreTests/EasyTool.CoreTests.csproj index 89de440..3726861 100644 --- a/EasyTool.CoreTests/EasyTool.CoreTests.csproj +++ b/EasyTool.CoreTests/EasyTool.CoreTests.csproj @@ -1,8 +1,9 @@ - + - .net6.0 - 11 + net10.0 + true + latest enable enable enable @@ -12,10 +13,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs b/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs index e79f0a3..f54d0a5 100644 --- a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs +++ b/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs @@ -101,10 +101,16 @@ public void IsPrivateIpv4_ValidPrivateIps_ReturnsTrue() /// 验证无效的 IPv4 地址,应引发 ArgumentException 异常。 /// [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void IsPrivateIpv4_InvalidIp_ThrowsException() { - IpUtil.IsPrivateIpv4("256.256.256.256"); + try + { + IpUtil.IsPrivateIpv4("256.256.256.256"); + Assert.Fail("Expected ArgumentException was not thrown."); + } + catch (ArgumentException) + { + } } /// @@ -120,10 +126,16 @@ public void IsPrivateIpv6_ValidPrivateIps_ReturnsTrue() /// 验证无效的 IPv6 地址,应引发 ArgumentException 异常。 /// [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void IsPrivateIpv6_InvalidIp_ThrowsException() { - IpUtil.IsPrivateIpv6("2001::1::2"); + try + { + IpUtil.IsPrivateIpv6("2001::1::2"); + Assert.Fail("Expected ArgumentException was not thrown."); + } + catch (ArgumentException) + { + } } /// diff --git a/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj b/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj index 0996f96..9e2e716 100644 --- a/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj +++ b/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj @@ -1,13 +1,14 @@ - + - netstandard2.1;.net6.0 - 11 + netstandard2.1;net10.0 + latest enable $(MSBuildProjectName.Replace(" ", "_").Replace(".EmitMapper", "")) + Joce.EasyTool.EmitMapper 一个大西瓜,TimChen - 2023.0914.1 + 2026.0108.1 A open source C# tool to make .NET easy diff --git a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj b/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj index 1c32465..28315f7 100644 --- a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj +++ b/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj @@ -1,19 +1,21 @@ - + - net6.0 + net10.0 + true enable enable + latest false true - - - - + + + + diff --git a/EasyTool.Image/EasyTool.Image.csproj b/EasyTool.Image/EasyTool.Image.csproj index 6a444e4..869faaa 100644 --- a/EasyTool.Image/EasyTool.Image.csproj +++ b/EasyTool.Image/EasyTool.Image.csproj @@ -1,13 +1,14 @@ - netstandard2.1;.net6.0 - 11 + netstandard2.1;net10.0 + latest enable $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) + Joce.EasyTool.Image 一个大西瓜,TimChen - 2023.0908.1 + 2026.0108.1 A open source C# tool to make .NET easy @@ -35,8 +36,8 @@ - - + + diff --git a/EasyTool.ImageTests/EasyTool.ImageTests.csproj b/EasyTool.ImageTests/EasyTool.ImageTests.csproj index 4c22132..1b36049 100644 --- a/EasyTool.ImageTests/EasyTool.ImageTests.csproj +++ b/EasyTool.ImageTests/EasyTool.ImageTests.csproj @@ -1,19 +1,21 @@ - net6.0 + net10.0 + true enable enable + latest false true - - - - + + + + diff --git a/EasyTool.NPOI/EasyTool.NPOI.csproj b/EasyTool.NPOI/EasyTool.NPOI.csproj index 2b8deec..b585e55 100644 --- a/EasyTool.NPOI/EasyTool.NPOI.csproj +++ b/EasyTool.NPOI/EasyTool.NPOI.csproj @@ -1,30 +1,45 @@ - + - - netstandard2.1;.net6.0 - enable - 11 - - 依赖于NPOI 2.6.2 - 支持通过文件地址或者流的方式读取Excel文件获取工作簿对象IWorkbook, - 通过IWorkbook工作簿对象可以转化成Dataset对象 - 通过ISheet工作表对象可以转化成DataTable对象和List对象 - 以下是一些示例: - 获取数据集对象: - var dateSet = NPOIUtil.OpenWorkbook(path).ConvertToDataSet(); - var dateSet = NPOIUtil.ConvertToDataSet(path); - 获取工作表对象: - var sheet = NPOIUtil.OpenWorkbook(path).GetSheetAt(0); - 获取单表数据对象: - var dataTable = NPOIUtil.OpenWorkbook(path).GetSheetAt(0).ConvertToDataTable(); - List《T》 dataList = NPOIUtil.OpenWorkbook(path).GetSheetAt(0).ConvertToList《List《T》》(); - 从流读取工作簿对象(从流读取需要指定文件类型,缺省值为XLSX): - var workbook = NPOIUtil.OpenWorkbook(stream,ExcelWorkbookType.XLS); - - + + netstandard2.1;net10.0 + enable + latest + $(MSBuildProjectName.Replace(" ", "_").Replace(".NPOI", "")) - - - + Joce.EasyTool.NPOI + 一个大西瓜,TimChen + 2026.0108.1 + + 依赖于NPOI 2.7.5 + 支持通过文件地址或者流的方式读取Excel文件获取工作簿对象IWorkbook, + 通过IWorkbook工作簿对象可以转化成Dataset对象 + 通过ISheet工作表对象可以转化成DataTable对象和List对象 + + Tool Power NPOI Excel + https://github.com/dotnet-easy/easytool + https://easy-dotnet.com + README.md + LICENSE + logo.png + + + + + True + \ + + + True + \ + + + True + \ + + + + + + diff --git a/EasyTool.NPOITests/EasyTool.NPOITests.csproj b/EasyTool.NPOITests/EasyTool.NPOITests.csproj index 6a5a733..167c9e3 100644 --- a/EasyTool.NPOITests/EasyTool.NPOITests.csproj +++ b/EasyTool.NPOITests/EasyTool.NPOITests.csproj @@ -1,19 +1,21 @@ - net6.0 + net10.0 + true enable enable + latest false true - - - - + + + + diff --git a/EasyTool.Web/EasyTool.Web.csproj b/EasyTool.Web/EasyTool.Web.csproj index b8b10a5..3b58b12 100644 --- a/EasyTool.Web/EasyTool.Web.csproj +++ b/EasyTool.Web/EasyTool.Web.csproj @@ -1,9 +1,39 @@ - + - - net6.0;netcoreapp3.1 - enable - + + net10.0 + enable + latest + $(MSBuildProjectName.Replace(" ", "_").Replace(".Web", "")) + + Joce.EasyTool.Web + 一个大西瓜,TimChen + 2026.0108.1 + + A open source C# tool to make .NET easy + + Tool Power Web + https://github.com/dotnet-easy/easytool + https://easy-dotnet.com + README.md + LICENSE + logo.png + + + + + True + \ + + + True + \ + + + True + \ + + diff --git a/EasyTool.WebTests/EasyTool.WebTests.csproj b/EasyTool.WebTests/EasyTool.WebTests.csproj index b1bd052..b4f5d54 100644 --- a/EasyTool.WebTests/EasyTool.WebTests.csproj +++ b/EasyTool.WebTests/EasyTool.WebTests.csproj @@ -1,19 +1,21 @@ - net6.0 + net10.0 + true enable enable + latest false true - - - - + + + + From 2eead635602323837aeeefe8f0c16893ab3d2023 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 11:21:25 +0800 Subject: [PATCH 02/19] =?UTF-8?q?refactor:=20=E6=A0=87=E8=AE=B0=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=8C=85=E8=A3=85=E6=96=B9=E6=B3=95=E4=B8=BA?= =?UTF-8?q?=20Obsolete=20=E4=BB=A5=E5=BC=95=E5=AF=BC=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=86=85=E7=BD=AE=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 主要更改 ### 标记 Obsolete 的方法 (~140+ 个) 将简单包装 .NET 内置 API 的方法标记为 [Obsolete],引导开发者直接使用内置 API: **IO/网络/运行时类:** - IoUtil.cs: 8 个 (File.ReadAllLines, ReadAllText, WriteAllLines 等) - URLUtil.cs: 5 个 (ExtractDomain, ExtractPath, ExtractQueryString 等) - RuntimeUtil.cs: 2 个 (GetDotNetVersion, GetOSVersion) - RegexUtil.cs: 2 个 (IsMatch, Replace) - TimerUtil.cs: 2 个 (StartNew, Wait) - TimestampUtil.cs: 6 个 (时间戳转换方法) **反射/类型/枚举类:** - TypeUtil.cs: 15 个 (IsEnum, GetProperties, GetMethods 等) - ReflectUtil.cs: 10 个 (GetType, GetAttribute, GetAssembly 等) - EnumUtil.cs: 7 个 (GetNames, GetValues, Parse 等) - ClassUtil.cs: 部分方法 **集合/列表/队列/栈类:** - ListUtil.cs: 13 个 (IndexOf, AddRange, ForEach, Sort 等) - QueueUtil.cs: 7 个 (Enqueue, Dequeue, Peek, Contains 等) - StackUtil.cs: 7 个 (Push, Pop, Peek, Contains 等) - LinkedListUtil.cs: 8 个 (AddFirst, AddLast, Remove 等) - IteratorUtil.cs: 7 个 (Filter, Map, Take, Skip 等 LINQ 包装) **工具类:** - ArrayUtil.cs: 部分方法 - StrUtil.cs: 部分方法 - HexUtil.cs: 部分方法 - ObjectUtil.cs: 多个方法 - FileUtil.cs: 部分方法 - EnvUtil.cs: 10 个 - EscapeUtil.cs: 5 个 - ProcessUtil.cs: 7 个 - DLLUtil.cs: 5 个 - MathUtil.cs: 1 个 (Average) - NumberUtil.cs: 1 个 (DecimalFormat) - Base64Util.cs: 2 个 (Encode, Decode) ### 新增扩展类文件 添加了多个独立的扩展类文件: - ArrayExtension.cs - 数组扩展 - ByteExtension.cs - 字节扩展 - ConvertExtension.cs - 转换扩展 - EnumExtension.cs - 枚举扩展 - ExceptionExtension.cs - 异常扩展 - FileSystemExtension.cs - 文件系统扩展 - StreamExtension.cs - 流扩展 - PropertyInfoExtension.cs - 属性信息扩展 - StringBuilderExtension.cs - 字符串构建器扩展 - StringComparisonExtension.cs - 字符串比较扩展 - TaskExtension.cs - 任务扩展 - TypeExtension.cs - 类型扩展 - GuidExtension.cs - GUID 扩展 - ObjectExtension.cs - 对象扩展 - DelegateExtension.cs - 委托扩展 - ColorExtension.cs - 颜色扩展 ### 删除文件 - Extension.Convert.cs - 合并到 ConvertExtension.cs ## 保持不变的文件 以下文件包含自定义业务逻辑,所有方法均保留: - IdUtil.cs (UUID/雪花ID生成) - AesUtil.cs, DesUtil.cs (加密算法) - Base32Util.cs, Base62Util.cs (编码算法) - ImgUtil.cs (图像处理) - NPOIUtil.cs (Excel 处理) - DateTimeUtil.cs, LunarCalendarUtil.cs (日期处理) - ZipUtil.cs, HashUtil.cs (压缩/哈希) - 等等... ## 兼容性 - 所有标记的方法仍可正常使用 - 使用 `false` 参数,仅警告不报错 - 保留向后兼容性 编译状态: ✅ 0 错误, 458 警告 --- .idea/.idea.EasyTool/.idea/.gitignore | 15 + .idea/.idea.EasyTool/.idea/encodings.xml | 4 + .idea/.idea.EasyTool/.idea/indexLayout.xml | 8 + .idea/.idea.EasyTool/.idea/vcs.xml | 6 + EasyTool.Core/CloneCategory/CloneExtension.cs | 4 +- EasyTool.Core/CloneCategory/CloneUtil.cs | 14 +- EasyTool.Core/CodeCategory/Base64Util.cs | 4 + .../CollectionsCategory/ArrayExtension.cs | 430 +++++++++++++ .../DictionaryExtension.cs | 4 + .../CollectionsCategory/IteratorUtil.cs | 19 +- .../CollectionsCategory/LinkedListUtil.cs | 16 + EasyTool.Core/CollectionsCategory/ListUtil.cs | 30 +- .../CollectionsCategory/QueueUtil.cs | 23 +- .../CollectionsCategory/StackUtil.cs | 23 +- .../ConvertCategory/ByteExtension.cs | 584 ++++++++++++++++++ ...tension.Convert.cs => ConvertExtension.cs} | 13 +- EasyTool.Core/ConvertCategory/ConvertUtil.cs | 28 +- .../ConvertCategory/NumberExtension.cs | 546 ++++++++++++++++ .../DateTimeCategory/DateTimeExtension.cs | 350 +++++++++++ EasyTool.Core/DateTimeCategory/TimerUtil.cs | 4 + .../DateTimeCategory/TimestampUtil.cs | 12 + EasyTool.Core/EasyTool.Core.csproj | 6 +- .../IEnumerableExtensions.cs | 441 ++++++++++++- .../IOCategory/FileSystemExtension.cs | 522 ++++++++++++++++ EasyTool.Core/IOCategory/FileTypeUtil.cs | 2 +- EasyTool.Core/IOCategory/FileUtil.cs | 22 +- EasyTool.Core/IOCategory/IoUtil.cs | 16 + EasyTool.Core/IOCategory/StreamExtension.cs | 365 +++++++++++ EasyTool.Core/IOCategory/Tailer.cs | 6 +- EasyTool.Core/IOCategory/WatchMonitor.cs | 12 +- EasyTool.Core/LanguageCategory/TreeUtil.cs | 26 +- EasyTool.Core/MathCategory/MathUtil.cs | 2 + EasyTool.Core/MathCategory/NumberUtil.cs | 2 + .../NetCategory/HttpClientExtension.cs | 2 +- EasyTool.Core/ToolCategory/ArrayUtil.cs | 12 + EasyTool.Core/ToolCategory/ClassUtil.cs | 16 + EasyTool.Core/ToolCategory/ColorExtension.cs | 348 +++++++++++ EasyTool.Core/ToolCategory/CreditCodeUtil.cs | 17 +- EasyTool.Core/ToolCategory/DLLUtil.cs | 24 +- .../ToolCategory/DelegateExtension.cs | 428 +++++++++++++ EasyTool.Core/ToolCategory/EnumExtension.cs | 357 +++++++++++ EasyTool.Core/ToolCategory/EnumUtil.cs | 14 + EasyTool.Core/ToolCategory/EnvUtil.cs | 20 + EasyTool.Core/ToolCategory/EscapeUtil.cs | 10 + .../ToolCategory/ExceptionExtension.cs | 338 ++++++++++ EasyTool.Core/ToolCategory/GuidExtension.cs | 288 +++++++++ EasyTool.Core/ToolCategory/HexUtil.cs | 4 + EasyTool.Core/ToolCategory/IdcardUtil.cs | 4 +- EasyTool.Core/ToolCategory/ObjectExtension.cs | 479 ++++++++++++++ EasyTool.Core/ToolCategory/ObjectUtil.cs | 130 +++- EasyTool.Core/ToolCategory/ProcessUtil.cs | 14 + .../ToolCategory/PropertyInfoExtension.cs | 390 ++++++++++++ EasyTool.Core/ToolCategory/ReflectUtil.cs | 18 + EasyTool.Core/ToolCategory/RegexUtil.cs | 18 +- EasyTool.Core/ToolCategory/RuntimeUtil.cs | 4 + EasyTool.Core/ToolCategory/StrExtension.cs | 318 +++++++++- EasyTool.Core/ToolCategory/StrUtil.cs | 20 + .../ToolCategory/StringBuilderExtension.cs | 417 +++++++++++++ .../ToolCategory/StringComparisonExtension.cs | 403 ++++++++++++ EasyTool.Core/ToolCategory/TaskExtension.cs | 381 ++++++++++++ EasyTool.Core/ToolCategory/TypeExtension.cs | 432 +++++++++++++ EasyTool.Core/ToolCategory/TypeUtil.cs | 28 + EasyTool.Core/ToolCategory/URLUtil.cs | 8 + .../DevelopmentCategory/BuildDtoToTS.cs | 100 ++- 64 files changed, 8490 insertions(+), 111 deletions(-) create mode 100644 .idea/.idea.EasyTool/.idea/.gitignore create mode 100644 .idea/.idea.EasyTool/.idea/encodings.xml create mode 100644 .idea/.idea.EasyTool/.idea/indexLayout.xml create mode 100644 .idea/.idea.EasyTool/.idea/vcs.xml create mode 100644 EasyTool.Core/CollectionsCategory/ArrayExtension.cs create mode 100644 EasyTool.Core/ConvertCategory/ByteExtension.cs rename EasyTool.Core/ConvertCategory/{Extension.Convert.cs => ConvertExtension.cs} (95%) create mode 100644 EasyTool.Core/ConvertCategory/NumberExtension.cs create mode 100644 EasyTool.Core/IOCategory/FileSystemExtension.cs create mode 100644 EasyTool.Core/IOCategory/StreamExtension.cs create mode 100644 EasyTool.Core/ToolCategory/ColorExtension.cs create mode 100644 EasyTool.Core/ToolCategory/DelegateExtension.cs create mode 100644 EasyTool.Core/ToolCategory/EnumExtension.cs create mode 100644 EasyTool.Core/ToolCategory/ExceptionExtension.cs create mode 100644 EasyTool.Core/ToolCategory/GuidExtension.cs create mode 100644 EasyTool.Core/ToolCategory/ObjectExtension.cs create mode 100644 EasyTool.Core/ToolCategory/PropertyInfoExtension.cs create mode 100644 EasyTool.Core/ToolCategory/StringBuilderExtension.cs create mode 100644 EasyTool.Core/ToolCategory/StringComparisonExtension.cs create mode 100644 EasyTool.Core/ToolCategory/TaskExtension.cs create mode 100644 EasyTool.Core/ToolCategory/TypeExtension.cs diff --git a/.idea/.idea.EasyTool/.idea/.gitignore b/.idea/.idea.EasyTool/.idea/.gitignore new file mode 100644 index 0000000..2e97252 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/.gitignore @@ -0,0 +1,15 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# Rider 忽略的文件 +/projectSettingsUpdater.xml +/.idea.EasyTool.iml +/contentModel.xml +/modules.xml +# 已忽略包含查询文件的默认文件夹 +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/.idea/.idea.EasyTool/.idea/encodings.xml b/.idea/.idea.EasyTool/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.EasyTool/.idea/indexLayout.xml b/.idea/.idea.EasyTool/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.EasyTool/.idea/vcs.xml b/.idea/.idea.EasyTool/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/EasyTool.Core/CloneCategory/CloneExtension.cs b/EasyTool.Core/CloneCategory/CloneExtension.cs index 43f892d..aa32c1a 100644 --- a/EasyTool.Core/CloneCategory/CloneExtension.cs +++ b/EasyTool.Core/CloneCategory/CloneExtension.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; @@ -7,6 +7,6 @@ namespace EasyTool.Extension public static class CloneExtension { //定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T Clone(this T obj)=> CloneUtil.Clone(obj); + public static T? Clone(this T? obj) => CloneUtil.Clone(obj); } } diff --git a/EasyTool.Core/CloneCategory/CloneUtil.cs b/EasyTool.Core/CloneCategory/CloneUtil.cs index 646e98e..7483124 100644 --- a/EasyTool.Core/CloneCategory/CloneUtil.cs +++ b/EasyTool.Core/CloneCategory/CloneUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; @@ -12,7 +12,7 @@ namespace EasyTool public static class CloneUtil { // 定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T Clone(T obj) + public static T? Clone(T? obj) { // 检查类型是否可序列化 if (!typeof(T).IsSerializable) @@ -23,7 +23,7 @@ public static T Clone(T obj) // 如果对象为 null,则返回 null if (ReferenceEquals(obj, null)) { - return default(T); + return default; } // 创建一个二进制序列化器 @@ -39,7 +39,7 @@ public static T Clone(T obj) stream.Seek(0, SeekOrigin.Begin); // 使用反序列化从内存流中读取并返回克隆的对象 - return (T)formatter.Deserialize(stream); + return (T)formatter.Deserialize(stream)!; } } @@ -50,7 +50,7 @@ public static T Clone(T obj) /// /// /// - public static async Task CloneAsync(T obj) + public static async Task CloneAsync(T? obj) { // 检查类型是否可序列化 if (!typeof(T).IsSerializable) @@ -61,7 +61,7 @@ public static async Task CloneAsync(T obj) // 如果对象为 null,则返回 null if (ReferenceEquals(obj, null)) { - return default(T); + return default; } // 创建一个二进制序列化器 @@ -77,7 +77,7 @@ public static async Task CloneAsync(T obj) stream.Seek(0, SeekOrigin.Begin); // 使用反序列化从内存流中读取并返回克隆的对象 - return (T)formatter.Deserialize(stream); + return await Task.FromResult((T?)formatter.Deserialize(stream)); } } } diff --git a/EasyTool.Core/CodeCategory/Base64Util.cs b/EasyTool.Core/CodeCategory/Base64Util.cs index 7a84f8c..20098c5 100644 --- a/EasyTool.Core/CodeCategory/Base64Util.cs +++ b/EasyTool.Core/CodeCategory/Base64Util.cs @@ -18,9 +18,11 @@ public static class Base64Util /// /// 将给定的字节数组转换为 Base64 编码字符串。 + /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] /// /// 要转换的字节数组 /// 转换后的 Base64 编码字符串 + [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] public static string Encode(byte[] bytes) { if (bytes == null) @@ -61,9 +63,11 @@ public static string Encode(byte[] bytes) /// /// 将给定的 Base64 编码字符串转换为字节数组。 + /// [Obsolete("请直接使用 Convert.FromBase64String(str)")] /// /// 要转换的 Base64 编码字符串 /// 转换后的字节数组 + [Obsolete("请直接使用 Convert.FromBase64String(str)", false)] public static byte[] Decode(string str) { if (string.IsNullOrEmpty(str)) diff --git a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs new file mode 100644 index 0000000..f482e24 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs @@ -0,0 +1,430 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.Extension +{ + /// + /// 数组扩展方法 + /// + public static class ArrayExtension + { + #region 空值判断 + + /// + /// 判断数组是否为空或 null + /// + public static bool IsEmpty(this T[]? array) + { + return array == null || array.Length == 0; + } + + /// + /// 判断数组是否非空 + /// + public static bool IsNotEmpty(this T[]? array) + { + return array != null && array.Length > 0; + } + + #endregion + + #region 数组操作 + + /// + /// 随机打乱数组顺序(Fisher-Yates 洗牌算法) + /// + public static T[]? Shuffle(this T[]? array) + { + if (array == null || array.Length < 2) + return array; + + var random = new Random(); + var result = (T[])array.Clone(); + + for (int i = result.Length - 1; i > 0; i--) + { + int j = random.Next(i + 1); + (result[i], result[j]) = (result[j], result[i]); + } + + return result; + } + + /// + /// 将数组分割成指定大小的块 + /// + /// 原始数组 + /// 每块的大小 + public static IEnumerable Chunk(this T[]? array, int chunkSize) + { + if (array == null) + yield break; + + if (chunkSize <= 0) + throw new ArgumentException("chunkSize must be greater than 0", nameof(chunkSize)); + + for (int i = 0; i < array.Length; i += chunkSize) + { + int remaining = array.Length - i; + int size = Math.Min(chunkSize, remaining); + var chunk = new T[size]; + Array.Copy(array, i, chunk, 0, size); + yield return chunk; + } + } + + /// + /// 将数组的元素连接成字符串 + /// [Obsolete("请直接使用 string.Join(separator, array)")] + /// + /// 数组 + /// 分隔符,默认为逗号 + [Obsolete("请直接使用 string.Join(separator, array)", false)] + public static string Join(this T[]? array, string separator = ",") + { + if (array == null || array.Length == 0) + return string.Empty; + + return string.Join(separator, array); + } + + /// + /// 清除数组中的重复元素 + /// [Obsolete("请直接使用 array.Distinct().ToArray() (LINQ)")] + /// + [Obsolete("请直接使用 array.Distinct().ToArray() (LINQ)", false)] + public static T[]? Distinct(this T[]? array) + { + if (array == null) + return null; + + return array.Distinct().ToArray(); + } + + /// + /// 按指定键清除数组中的重复元素 + /// + public static T[]? DistinctBy(this T[]? array, Func keySelector) + { + if (array == null) + return null; + + return array.GroupBy(keySelector).Select(g => g.First()).ToArray(); + } + + /// + /// 将数组元素拼接成字符串(支持格式化) + /// + /// 数组 + /// 分隔符 + /// 格式化字符串 + public static string JoinFormat(this T[]? array, string separator, string format) + { + if (array == null || array.Length == 0) + return string.Empty; + + var formatted = array.Select(item => string.Format(format, item)); + return string.Join(separator, formatted); + } + + #endregion + + #region 数组查找 + + /// + /// 查找数组中满足条件的第一个元素的索引 + /// [Obsolete("请直接使用 Array.FindIndex(array, predicate)")] + /// + [Obsolete("请直接使用 Array.FindIndex(array, predicate)", false)] + public static int FindIndex(this T[]? array, Predicate predicate) + { + if (array == null) + return -1; + + return Array.FindIndex(array, predicate); + } + + /// + /// 查找数组中满足条件的所有元素的索引 + /// + public static int[] FindAllIndexes(this T[]? array, Func predicate) + { + if (array == null) + return Array.Empty(); + + var indexes = new List(); + for (int i = 0; i < array.Length; i++) + { + if (predicate(array[i])) + { + indexes.Add(i); + } + } + return indexes.ToArray(); + } + + /// + /// 判断数组是否包含指定元素(使用自定义比较器) + /// + public static bool Contains(this T[]? array, T value, IEqualityComparer comparer) + { + if (array == null) + return false; + + return Array.Exists(array, item => comparer.Equals(item, value)); + } + + #endregion + + #region 数组转换 + + /// + /// 将数组转换为 HashSet + /// [Obsolete("请直接使用 new HashSet(array)")] + /// + [Obsolete("请直接使用 new HashSet(array)", false)] + public static HashSet ToHashSet(this T[]? array) + { + if (array == null) + return new HashSet(); + + return new HashSet(array); + } + + /// + /// 将数组转换为 Queue + /// [Obsolete("请直接使用 new Queue(array)")] + /// + [Obsolete("请直接使用 new Queue(array)", false)] + public static Queue ToQueue(this T[]? array) + { + if (array == null) + return new Queue(); + + return new Queue(array); + } + + /// + /// 将数组转换为 Stack + /// [Obsolete("请直接使用 new Stack(array)")] + /// + [Obsolete("请直接使用 new Stack(array)", false)] + public static Stack ToStack(this T[]? array) + { + if (array == null) + return new Stack(); + + return new Stack(array); + } + + /// + /// 将数组转换为 LinkedList + /// [Obsolete("请直接使用 new LinkedList(array)")] + /// + [Obsolete("请直接使用 new LinkedList(array)", false)] + public static LinkedList ToLinkedList(this T[]? array) + { + if (array == null) + return new LinkedList(); + + return new LinkedList(array); + } + + /// + /// 将二维数组展平为一维数组 + /// + public static T[]? Flatten(this T[,]? array) + { + if (array == null) + return null; + + int width = array.GetLength(0); + int height = array.GetLength(1); + var result = new T[width * height]; + + int index = 0; + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + result[index++] = array[i, j]; + } + } + + return result; + } + + #endregion + + #region 数组切片 + + /// + /// 获取数组中指定范围的元素 + /// + /// 数组 + /// 起始索引 + /// 长度 + public static T[]? Slice(this T[]? array, int startIndex, int length) + { + if (array == null) + return null; + + if (startIndex < 0 || startIndex >= array.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + if (length < 0 || startIndex + length > array.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + var result = new T[length]; + Array.Copy(array, startIndex, result, 0, length); + return result; + } + + /// + /// 获取数组从指定索引开始到末尾的元素 + /// + public static T[]? Slice(this T[]? array, int startIndex) + { + if (array == null) + return null; + + if (startIndex < 0) + startIndex = 0; + + if (startIndex >= array.Length) + return Array.Empty(); + + int length = array.Length - startIndex; + return Slice(array, startIndex, length); + } + + #endregion + + #region 数组合并 + + /// + /// 合并多个数组 + /// + public static T[] Merge(params T[][]? arrays) + { + if (arrays == null || arrays.Length == 0) + return Array.Empty(); + + int totalLength = 0; + foreach (var array in arrays) + { + if (array != null) + totalLength += array.Length; + } + + var result = new T[totalLength]; + int offset = 0; + + foreach (var array in arrays) + { + if (array != null && array.Length > 0) + { + Array.Copy(array, 0, result, offset, array.Length); + offset += array.Length; + } + } + + return result; + } + + /// + /// 在数组开头添加元素 + /// + public static T[] Prepend(this T[]? array, params T[]? items) + { + if (array == null) + return items ?? Array.Empty(); + + if (items == null || items.Length == 0) + return array; + + var result = new T[array.Length + items.Length]; + Array.Copy(items, 0, result, 0, items.Length); + Array.Copy(array, 0, result, items.Length, array.Length); + return result; + } + + /// + /// 在数组末尾添加元素 + /// + public static T[] Append(this T[]? array, params T[]? items) + { + if (array == null) + return items ?? Array.Empty(); + + if (items == null || items.Length == 0) + return array; + + var result = new T[array.Length + items.Length]; + Array.Copy(array, 0, result, 0, array.Length); + Array.Copy(items, 0, result, array.Length, items.Length); + return result; + } + + #endregion + + #region 数组遍历 + + /// + /// 遍历数组并对每个元素执行指定操作 + /// [Obsolete("请直接使用 Array.ForEach(array, action) 或 foreach 循环")] + /// + [Obsolete("请直接使用 Array.ForEach(array, action) 或 foreach 循环", false)] + public static void ForEach(this T[]? array, Action action) + { + if (array == null || action == null) + return; + + foreach (var item in array) + { + action(item); + } + } + + /// + /// 遍历数组并对每个元素及其索引执行指定操作 + /// + public static void ForEach(this T[]? array, Action action) + { + if (array == null || action == null) + return; + + for (int i = 0; i < array.Length; i++) + { + action(array[i], i); + } + } + + #endregion + + #region 数组统计 + + /// + /// 统计数组中满足条件的元素数量 + /// [Obsolete("请直接使用 array.Count(predicate) (LINQ)")] + /// + [Obsolete("请直接使用 array.Count(predicate) (LINQ)", false)] + public static int Count(this T[]? array, Func predicate) + { + if (array == null) + return 0; + + int count = 0; + foreach (var item in array) + { + if (predicate(item)) + count++; + } + return count; + } + + #endregion + } +} diff --git a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs index e681ef2..eaaa2ea 100644 --- a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs +++ b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs @@ -44,9 +44,11 @@ public static void AddRange(this IDictionary destina /// /// 返回字典中键的集合 + /// [Obsolete("请直接使用 dictionary.Keys")] /// /// 要获取键的字典 /// 字典中所有键的集合 + [Obsolete("请直接使用 dictionary.Keys", false)] public static IEnumerable GetKeys(this IDictionary dictionary) { return dictionary.Keys; @@ -54,9 +56,11 @@ public static IEnumerable GetKeys(this IDictionary /// 返回字典中值的集合 + /// [Obsolete("请直接使用 dictionary.Values")] /// /// 要获取值的字典 /// 字典中所有值的集合 + [Obsolete("请直接使用 dictionary.Values", false)] public static IEnumerable GetValues(this IDictionary dictionary) { return dictionary.Values; diff --git a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs b/EasyTool.Core/CollectionsCategory/IteratorUtil.cs index b707296..f4aa6e6 100644 --- a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs +++ b/EasyTool.Core/CollectionsCategory/IteratorUtil.cs @@ -5,9 +5,12 @@ namespace EasyTool { - //TODO:疑问,这些功能Linq支持吗? /// /// 迭代器工具类 + /// + /// 注意:此类中的方法与 System.Linq 提供的功能高度相似。 + /// 对于新代码,建议优先使用 LINQ 标准查询运算符(如 Where、Select、Take、Skip、OrderBy、GroupBy 等)。 + /// 此类保留用于向后兼容和特定场景需求。 /// public static class IteratorUtil { @@ -24,7 +27,9 @@ public static IEnumerable AsIterator(this T[] array) /// /// 过滤掉一个迭代器中不符合条件的元素 + /// [Obsolete("请直接使用 source.Where(predicate) (LINQ)")] /// + [Obsolete("请直接使用 source.Where(predicate) (LINQ)", false)] public static IEnumerable Filter(this IEnumerable source, Func predicate) { foreach (var item in source) @@ -38,7 +43,9 @@ public static IEnumerable Filter(this IEnumerable source, Func /// /// 对一个迭代器中的每个元素进行转换 + /// [Obsolete("请直接使用 source.Select(selector) (LINQ)")] /// + [Obsolete("请直接使用 source.Select(selector) (LINQ)", false)] public static IEnumerable Map(this IEnumerable source, Func selector) { foreach (var item in source) @@ -49,7 +56,9 @@ public static IEnumerable Map(this IEnumerable /// 从一个迭代器中取出前 n 个元素 + /// [Obsolete("请直接使用 source.Take(count) (LINQ)")] /// + [Obsolete("请直接使用 source.Take(count) (LINQ)", false)] public static IEnumerable Take(this IEnumerable source, int count) { foreach (var item in source) @@ -67,7 +76,9 @@ public static IEnumerable Take(this IEnumerable source, int count) /// /// 跳过一个迭代器中的前 n 个元素 + /// [Obsolete("请直接使用 source.Skip(count) (LINQ)")] /// + [Obsolete("请直接使用 source.Skip(count) (LINQ)", false)] public static IEnumerable Skip(this IEnumerable source, int count) { foreach (var item in source) @@ -85,7 +96,9 @@ public static IEnumerable Skip(this IEnumerable source, int count) /// /// 将一个迭代器的元素分组 + /// [Obsolete("请直接使用 source.GroupBy(keySelector, x => x) (LINQ)")] /// + [Obsolete("请直接使用 source.GroupBy(keySelector, x => x) (LINQ)", false)] public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector) { return source.GroupBy(keySelector, x => x); @@ -137,7 +150,9 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() /// /// 对一个迭代器中的元素进行排序 + /// [Obsolete("请直接使用 source.OrderBy(keySelector) (LINQ)")] /// + [Obsolete("请直接使用 source.OrderBy(keySelector) (LINQ)", false)] public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector) { return source.OrderBy(keySelector, Comparer.Default); @@ -161,7 +176,9 @@ public static IOrderedEnumerable OrderBy(this IEnumerabl /// /// 对一个迭代器中的元素进行倒序排序 + /// [Obsolete("请直接使用 source.OrderByDescending(keySelector) (LINQ)")] /// + [Obsolete("请直接使用 source.OrderByDescending(keySelector) (LINQ)", false)] public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector) { return source.OrderByDescending(keySelector, Comparer.Default); diff --git a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs index d3893db..a27bd80 100644 --- a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs +++ b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs @@ -11,10 +11,12 @@ public class LinkedListUtil { /// /// 将指定元素添加到双向链表的结尾处。 + /// [Obsolete("请直接使用 list.AddLast(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要添加的元素 + [Obsolete("请直接使用 list.AddLast(item)", false)] public static void AddLast(LinkedList list, T item) { list.AddLast(item); @@ -22,10 +24,12 @@ public static void AddLast(LinkedList list, T item) /// /// 将指定元素添加到双向链表的开头处。 + /// [Obsolete("请直接使用 list.AddFirst(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要添加的元素 + [Obsolete("请直接使用 list.AddFirst(item)", false)] public static void AddFirst(LinkedList list, T item) { list.AddFirst(item); @@ -33,12 +37,14 @@ public static void AddFirst(LinkedList list, T item) /// /// 将指定元素插入到双向链表中的指定位置之前。 + /// [Obsolete("请直接使用 list.AddBefore(node, item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在其前面插入新元素的节点 /// 要添加的元素 /// 新节点 + [Obsolete("请直接使用 list.AddBefore(node, item)", false)] public static LinkedListNode AddBefore(LinkedList list, LinkedListNode node, T item) { return list.AddBefore(node, item); @@ -46,12 +52,14 @@ public static LinkedListNode AddBefore(LinkedList list, LinkedListNode< /// /// 将指定元素插入到双向链表中的指定位置之后。 + /// [Obsolete("请直接使用 list.AddAfter(node, item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在其后面插入新元素的节点 /// 要添加的元素 /// 新节点 + [Obsolete("请直接使用 list.AddAfter(node, item)", false)] public static LinkedListNode AddAfter(LinkedList list, LinkedListNode node, T item) { return list.AddAfter(node, item); @@ -84,10 +92,12 @@ public static void MoveFirst(LinkedList list, LinkedListNode node) /// /// 从双向链表中移除指定节点。 + /// [Obsolete("请直接使用 list.Remove(node)")] /// /// 双向链表元素类型 /// 双向链表 /// 要移除的节点 + [Obsolete("请直接使用 list.Remove(node)", false)] public static void Remove(LinkedList list, LinkedListNode node) { list.Remove(node); @@ -95,11 +105,13 @@ public static void Remove(LinkedList list, LinkedListNode node) /// /// 从双向链表中移除指定值的第一个匹配项。 + /// [Obsolete("请直接使用 list.Remove(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要移除的元素 /// 如果成功移除了元素,则为 true;否则为 false。 + [Obsolete("请直接使用 list.Remove(item)", false)] public static bool Remove(LinkedList list, T item) { return list.Remove(item); @@ -107,11 +119,13 @@ public static bool Remove(LinkedList list, T item) /// /// 确定双向链表中是否包含特定值。 + /// [Obsolete("请直接使用 list.Contains(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在双向链表中查找的元素 /// 如果在双向链表中找到了 item,则为 true;否则为 false。 + [Obsolete("请直接使用 list.Contains(item)", false)] public static bool Contains(LinkedList list, T item) { return list.Contains(item); @@ -119,9 +133,11 @@ public static bool Contains(LinkedList list, T item) /// /// 从双向链表中移除所有节点。 + /// [Obsolete("请直接使用 list.Clear()")] /// /// 双向链表元素类型 /// 双向链表 + [Obsolete("请直接使用 list.Clear()", false)] public static void Clear(LinkedList list) { list.Clear(); diff --git a/EasyTool.Core/CollectionsCategory/ListUtil.cs b/EasyTool.Core/CollectionsCategory/ListUtil.cs index 916f896..cb49685 100644 --- a/EasyTool.Core/CollectionsCategory/ListUtil.cs +++ b/EasyTool.Core/CollectionsCategory/ListUtil.cs @@ -9,11 +9,13 @@ public class ListUtil { /// /// 在列表中查找元素,并返回其索引。如果未找到,则返回 -1。 + /// [Obsolete("请直接使用 list.IndexOf(item)")] /// /// 列表元素类型 /// 要查找的列表 /// 要查找的元素 /// 元素在列表中的索引,如果未找到则返回 -1 + [Obsolete("请直接使用 list.IndexOf(item)", false)] public static int IndexOf(List list, T item) { return list.IndexOf(item); @@ -21,10 +23,12 @@ public static int IndexOf(List list, T item) /// /// 向列表中添加多个元素。 + /// [Obsolete("请直接使用 list.AddRange(items)")] /// /// 列表元素类型 /// 要添加元素的列表 /// 要添加到列表中的元素 + [Obsolete("请直接使用 list.AddRange(items)", false)] public static void AddRange(List list, IEnumerable items) { list.AddRange(items); @@ -32,10 +36,12 @@ public static void AddRange(List list, IEnumerable items) /// /// 在列表中删除指定索引处的元素。 + /// [Obsolete("请直接使用 list.RemoveAt(index)")] /// /// 列表元素类型 /// 要删除元素的列表 /// 要删除元素的索引 + [Obsolete("请直接使用 list.RemoveAt(index)", false)] public static void RemoveAt(List list, int index) { list.RemoveAt(index); @@ -43,11 +49,13 @@ public static void RemoveAt(List list, int index) /// /// 从列表中删除指定元素的第一个匹配项。 + /// [Obsolete("请直接使用 list.Remove(item)")] /// /// 列表元素类型 /// 要删除元素的列表 /// 要删除的元素 /// 如果找到并成功删除元素,则返回 true;否则返回 false + [Obsolete("请直接使用 list.Remove(item)", false)] public static bool Remove(List list, T item) { return list.Remove(item); @@ -77,10 +85,12 @@ public static List Concat(params List[] lists) /// /// 返回一个新的列表,其中包含指定列表中的元素,但不包括重复元素。 + /// [Obsolete("请直接使用 list.Distinct().ToList() (LINQ)")] /// /// 列表元素类型 /// 要去重的列表 /// 去重后的新列表 + [Obsolete("请直接使用 list.Distinct().ToList() (LINQ)", false)] public static List Distinct(List list) { return list.Distinct().ToList(); @@ -88,11 +98,13 @@ public static List Distinct(List list) /// /// 根据指定的条件筛选出列表中符合条件的元素。 + /// [Obsolete("请直接使用 list.Where(predicate).ToList() (LINQ)")] /// /// 列表元素类型 /// 要筛选的列表 /// 筛选条件 /// 符合条件的元素列表 + [Obsolete("请直接使用 list.Where(predicate).ToList() (LINQ)", false)] public static List Where(List list, Func predicate) { return list.Where(predicate).ToList(); @@ -100,12 +112,14 @@ public static List Where(List list, Func predicate) /// /// 将列表中的每个元素应用到指定的转换函数,并返回转换后的新列表。 + /// [Obsolete("请直接使用 list.Select(selector).ToList() (LINQ)")] /// /// 列表元素类型 /// 转换后的元素类型 /// 要转换的列表 /// 转换函数 /// 转换后的新列表 + [Obsolete("请直接使用 list.Select(selector).ToList() (LINQ)", false)] public static List Select(List list, Func selector) { return list.Select(selector).ToList(); @@ -113,10 +127,12 @@ public static List Select(List list, Func /// 对列表中的每个元素应用指定的操作。 + /// [Obsolete("请直接使用 list.ForEach(action)")] /// /// 列表元素类型 /// 要应用操作的列表 /// 要应用的操作 + [Obsolete("请直接使用 list.ForEach(action)", false)] public static void ForEach(List list, Action action) { list.ForEach(action); @@ -124,9 +140,11 @@ public static void ForEach(List list, Action action) /// /// 将列表中的元素排序。 + /// [Obsolete("请直接使用 list.Sort()")] /// /// 列表元素类型 /// 要排序的列表 + [Obsolete("请直接使用 list.Sort()", false)] public static void Sort(List list) { list.Sort(); @@ -134,10 +152,12 @@ public static void Sort(List list) /// /// 将列表中的元素按指定的比较器排序。 + /// [Obsolete("请直接使用 list.Sort(comparer)")] /// /// 列表元素类型 /// 要排序的列表 /// 比较器 + [Obsolete("请直接使用 list.Sort(comparer)", false)] public static void Sort(List list, IComparer comparer) { list.Sort(comparer); @@ -160,10 +180,12 @@ public static List Page(List list, int pageSize, int pageIndex) /// /// 向列表中批量添加元素。 + /// [Obsolete("请直接使用 list.AddRange(items)")] /// /// 列表元素类型 /// 要添加元素的列表 /// 要添加到列表中的元素 + [Obsolete("请直接使用 list.AddRange(items)", false)] public static void AddRange(List list, params T[] items) { list.AddRange(items); @@ -194,7 +216,7 @@ public static bool Equals(List list1, List list2) { for (int i = 0; i < list1.Count; i++) { - if (!list1[i].Equals(list2[i])) + if (!EqualityComparer.Default.Equals(list1[i], list2[i])) { return false; } @@ -206,11 +228,13 @@ public static bool Equals(List list1, List list2) /// /// 返回两个列表的交集。 + /// [Obsolete("请直接使用 list1.Intersect(list2).ToList() (LINQ)")] /// /// 列表元素类型 /// 要比较的第一个列表 /// 要比较的第二个列表 /// 交集列表 + [Obsolete("请直接使用 list1.Intersect(list2).ToList() (LINQ)", false)] public static List Intersect(List list1, List list2) { return list1.Intersect(list2).ToList(); @@ -218,11 +242,13 @@ public static List Intersect(List list1, List list2) /// /// 返回两个列表的并集。 + /// [Obsolete("请直接使用 list1.Union(list2).ToList() (LINQ)")] /// /// 列表元素类型 /// 要比较的第一个列表 /// 要比较的第二个列表 /// 并集列表 + [Obsolete("请直接使用 list1.Union(list2).ToList() (LINQ)", false)] public static List Union(List list1, List list2) { return list1.Union(list2).ToList(); @@ -230,11 +256,13 @@ public static List Union(List list1, List list2) /// /// 返回两个列表的差集。 + /// [Obsolete("请直接使用 list1.Except(list2).ToList() (LINQ)")] /// /// 列表元素类型 /// 要比较的第一个列表 /// 要比较的第二个列表 /// 差集列表 + [Obsolete("请直接使用 list1.Except(list2).ToList() (LINQ)", false)] public static List Except(List list1, List list2) { return list1.Except(list2).ToList(); diff --git a/EasyTool.Core/CollectionsCategory/QueueUtil.cs b/EasyTool.Core/CollectionsCategory/QueueUtil.cs index 8c87309..d1f45e9 100644 --- a/EasyTool.Core/CollectionsCategory/QueueUtil.cs +++ b/EasyTool.Core/CollectionsCategory/QueueUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,10 +12,12 @@ public class QueueUtil { /// /// 将指定元素添加到队列的末尾。 + /// [Obsolete("请直接使用 queue.Enqueue(item)")] /// /// 队列元素类型 /// 队列 /// 要添加的元素 + [Obsolete("请直接使用 queue.Enqueue(item)", false)] public static void Enqueue(Queue queue, T item) { queue.Enqueue(item); @@ -37,11 +39,13 @@ public static void EnqueueRange(Queue queue, IEnumerable collection) /// /// 移除并返回位于队列开头的元素。 + /// [Obsolete("请直接使用 queue.Dequeue()")] /// /// 队列元素类型 /// 队列 /// 队列开头的元素 /// 队列为空时引发异常 + [Obsolete("请直接使用 queue.Dequeue()", false)] public static T Dequeue(Queue queue) { return queue.Dequeue(); @@ -49,11 +53,13 @@ public static T Dequeue(Queue queue) /// /// 返回位于队列开头的元素而不将其移除。 + /// [Obsolete("请直接使用 queue.Peek()")] /// /// 队列元素类型 /// 队列 /// 队列开头的元素 /// 队列为空时引发异常 + [Obsolete("请直接使用 queue.Peek()", false)] public static T Peek(Queue queue) { return queue.Peek(); @@ -61,11 +67,13 @@ public static T Peek(Queue queue) /// /// 确定队列中是否包含指定元素。 + /// [Obsolete("请直接使用 queue.Contains(item)")] /// /// 队列元素类型 /// 队列 /// 要查找的元素 /// 如果队列包含指定元素,则为 true;否则为 false。 + [Obsolete("请直接使用 queue.Contains(item)", false)] public static bool Contains(Queue queue, T item) { return queue.Contains(item); @@ -82,7 +90,12 @@ public static bool Remove(Queue queue, T item) { if (queue.Contains(item)) { - queue = new Queue(queue.Where(x => !x.Equals(item))); + var newQueue = new Queue(queue.Where(x => !Equals(x, item))); + queue.Clear(); + foreach (var element in newQueue) + { + queue.Enqueue(element); + } return true; } return false; @@ -90,10 +103,12 @@ public static bool Remove(Queue queue, T item) /// /// 将队列中的所有元素复制到新数组中。 + /// [Obsolete("请直接使用 queue.ToArray()")] /// /// 队列元素类型 /// 队列 /// 包含队列中所有元素的新数组 + [Obsolete("请直接使用 queue.ToArray()", false)] public static T[] ToArray(Queue queue) { return queue.ToArray(); @@ -101,11 +116,13 @@ public static T[] ToArray(Queue queue) /// /// 将队列中的所有元素复制到新数组中,从指定的索引开始。 + /// [Obsolete("请直接使用 queue.CopyTo(array, arrayIndex)")] /// /// 队列元素类型 /// 队列 /// 要复制到的目标数组 /// 目标数组的起始索引 + [Obsolete("请直接使用 queue.CopyTo(array, arrayIndex)", false)] public static void CopyTo(Queue queue, T[] array, int arrayIndex) { queue.CopyTo(array, arrayIndex); @@ -113,9 +130,11 @@ public static void CopyTo(Queue queue, T[] array, int arrayIndex) /// /// 从队列中移除所有元素。 + /// [Obsolete("请直接使用 queue.Clear()")] /// /// 队列元素类型 /// 队列 + [Obsolete("请直接使用 queue.Clear()", false)] public static void Clear(Queue queue) { queue.Clear(); diff --git a/EasyTool.Core/CollectionsCategory/StackUtil.cs b/EasyTool.Core/CollectionsCategory/StackUtil.cs index 232e5c8..5a0cd9c 100644 --- a/EasyTool.Core/CollectionsCategory/StackUtil.cs +++ b/EasyTool.Core/CollectionsCategory/StackUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,10 +12,12 @@ public class StackUtil { /// /// 将指定元素推入堆栈的顶部。 + /// [Obsolete("请直接使用 stack.Push(item)")] /// /// 堆栈元素类型 /// 堆栈 /// 要添加的元素 + [Obsolete("请直接使用 stack.Push(item)", false)] public static void Push(Stack stack, T item) { stack.Push(item); @@ -23,11 +25,13 @@ public static void Push(Stack stack, T item) /// /// 从堆栈的顶部移除并返回对象。 + /// [Obsolete("请直接使用 stack.Pop()")] /// /// 堆栈元素类型 /// 堆栈 /// 堆栈顶部的元素 /// 堆栈为空时引发异常 + [Obsolete("请直接使用 stack.Pop()", false)] public static T Pop(Stack stack) { return stack.Pop(); @@ -35,11 +39,13 @@ public static T Pop(Stack stack) /// /// 返回位于堆栈顶部的对象但不将其移除。 + /// [Obsolete("请直接使用 stack.Peek()")] /// /// 堆栈元素类型 /// 堆栈 /// 堆栈顶部的元素 /// 堆栈为空时引发异常 + [Obsolete("请直接使用 stack.Peek()", false)] public static T Peek(Stack stack) { return stack.Peek(); @@ -47,11 +53,13 @@ public static T Peek(Stack stack) /// /// 确定堆栈是否包含指定元素。 + /// [Obsolete("请直接使用 stack.Contains(item)")] /// /// 堆栈元素类型 /// 堆栈 /// 要查找的元素 /// 如果堆栈包含指定元素,则为 true;否则为 false。 + [Obsolete("请直接使用 stack.Contains(item)", false)] public static bool Contains(Stack stack, T item) { return stack.Contains(item); @@ -68,7 +76,12 @@ public static bool Remove(Stack stack, T item) { if (stack.Contains(item)) { - stack = new Stack(stack.Where(x => !x.Equals(item)).Reverse()); + var newStack = new Stack(stack.Where(x => !Equals(x, item)).Reverse()); + stack.Clear(); + foreach (var element in newStack) + { + stack.Push(element); + } return true; } return false; @@ -76,10 +89,12 @@ public static bool Remove(Stack stack, T item) /// /// 将堆栈中的所有元素复制到新数组中。 + /// [Obsolete("请直接使用 stack.ToArray()")] /// /// 堆栈元素类型 /// 堆栈 /// 包含堆栈中所有元素的新数组 + [Obsolete("请直接使用 stack.ToArray()", false)] public static T[] ToArray(Stack stack) { return stack.ToArray(); @@ -87,11 +102,13 @@ public static T[] ToArray(Stack stack) /// /// 将堆栈中的所有元素复制到新数组中,从指定的索引开始。 + /// [Obsolete("请直接使用 stack.CopyTo(array, arrayIndex)")] /// /// 堆栈元素类型 /// 堆栈 /// 要复制到的目标数组 /// 目标数组的起始索引 + [Obsolete("请直接使用 stack.CopyTo(array, arrayIndex)", false)] public static void CopyTo(Stack stack, T[] array, int arrayIndex) { stack.CopyTo(array, arrayIndex); @@ -99,9 +116,11 @@ public static void CopyTo(Stack stack, T[] array, int arrayIndex) /// /// 从堆栈中移除所有元素。 + /// [Obsolete("请直接使用 stack.Clear()")] /// /// 堆栈元素类型 /// 堆栈 + [Obsolete("请直接使用 stack.Clear()", false)] public static void Clear(Stack stack) { stack.Clear(); diff --git a/EasyTool.Core/ConvertCategory/ByteExtension.cs b/EasyTool.Core/ConvertCategory/ByteExtension.cs new file mode 100644 index 0000000..7208906 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/ByteExtension.cs @@ -0,0 +1,584 @@ +using System; +using System.IO; +using System.Text; +using System.Linq; + +namespace EasyTool.Extension +{ + /// + /// Byte 字节扩展方法 + /// + public static class ByteExtension + { + #region 单字节转换 + + /// + /// 将字节转换为16进制字符串 + /// + public static string ToHex(this byte value) + { + return value.ToString("X2"); + } + + /// + /// 将字节转换为16进制字符串(小写) + /// + public static string ToHexLower(this byte value) + { + return value.ToString("x2"); + } + + /// + /// 将字节转换为二进制字符串 + /// + public static string ToBinaryString(this byte value) + { + return Convert.ToString(value, 2).PadLeft(8, '0'); + } + + /// + /// 获取字节的指定位 + /// + public static bool GetBit(this byte value, int index) + { + if (index < 0 || index > 7) + throw new ArgumentOutOfRangeException(nameof(index), "Index must be between 0 and 7"); + + return (value & (1 << index)) != 0; + } + + /// + /// 设置字节的指定位 + /// + public static byte SetBit(this byte value, int index, bool bitValue) + { + if (index < 0 || index > 7) + throw new ArgumentOutOfRangeException(nameof(index), "Index must be between 0 and 7"); + + if (bitValue) + return (byte)(value | (1 << index)); + else + return (byte)(value & ~(1 << index)); + } + + #endregion + + #region 字节数组转换 + + /// + /// 将字节数组转换为16进制字符串 + /// + public static string ToHex(this byte[] bytes) + { + return bytes.ToHex(true); + } + + /// + /// 将字节数组转换为16进制字符串 + /// + public static string ToHex(this byte[] bytes, bool uppercase) + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + var format = uppercase ? "X2" : "x2"; + var sb = new StringBuilder(bytes.Length * 2); + + foreach (var b in bytes) + { + sb.Append(b.ToString(format)); + } + + return sb.ToString(); + } + + /// + /// 将字节数组转换为16进制字符串(带分隔符) + /// + public static string ToHex(this byte[] bytes, string separator, bool uppercase = true) + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + var format = uppercase ? "X2" : "x2"; + return string.Join(separator, bytes.Select(b => b.ToString(format))); + } + + /// + /// 从16进制字符串转换为字节数组 + /// + public static byte[] FromHexToBytes(this string hex) + { + if (string.IsNullOrWhiteSpace(hex)) + return Array.Empty(); + + hex = hex.Replace("-", "").Replace(" ", ""); + + if (hex.Length % 2 != 0) + throw new ArgumentException("Hex string must have an even length", nameof(hex)); + + var bytes = new byte[hex.Length / 2]; + + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + + return bytes; + } + + /// + /// 将字节数组转换为Base64字符串 + /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] + /// + [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] + public static string ToBase64(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + return Convert.ToBase64String(bytes); + } + + /// + /// 从Base64字符串转换为字节数组 + /// [Obsolete("请直接使用 Convert.FromBase64String(base64)")] + /// + [Obsolete("请直接使用 Convert.FromBase64String(base64)", false)] + public static byte[] FromBase64ToBytes(this string base64) + { + if (string.IsNullOrWhiteSpace(base64)) + return Array.Empty(); + + return Convert.FromBase64String(base64); + } + + /// + /// 将字节数组转换为二进制字符串 + /// + public static string ToBinaryString(this byte[] bytes, string separator = " ") + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + return string.Join(separator, bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0'))); + } + + /// + /// 从二进制字符串转换为字节数组 + /// + public static byte[] FromBinaryStringToBytes(this string binary) + { + if (string.IsNullOrWhiteSpace(binary)) + return Array.Empty(); + + binary = binary.Replace(" ", ""); + + if (binary.Length % 8 != 0) + throw new ArgumentException("Binary string length must be a multiple of 8", nameof(binary)); + + var bytes = new byte[binary.Length / 8]; + + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(binary.Substring(i * 8, 8), 2); + } + + return bytes; + } + + #endregion + + #region 字节数组操作 + + /// + /// 反转字节数组 + /// + public static byte[]? Reverse(this byte[]? bytes) + { + if (bytes == null) + return null; + + var result = new byte[bytes.Length]; + Array.Copy(bytes, result, bytes.Length); + Array.Reverse(result); + return result; + } + + /// + /// 字节数组异或运算 + /// + public static byte[] Xor(this byte[] bytes, byte[] key) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + if (key == null) + throw new ArgumentNullException(nameof(key)); + + var result = new byte[bytes.Length]; + + for (int i = 0; i < bytes.Length; i++) + { + result[i] = (byte)(bytes[i] ^ key[i % key.Length]); + } + + return result; + } + + /// + /// 字节数组异或运算(单字节密钥) + /// + public static byte[] Xor(this byte[] bytes, byte key) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + var result = new byte[bytes.Length]; + + for (int i = 0; i < bytes.Length; i++) + { + result[i] = (byte)(bytes[i] ^ key); + } + + return result; + } + + /// + /// 合并多个字节数组 + /// + public static byte[] Combine(params byte[][]? arrays) + { + if (arrays == null || arrays.Length == 0) + return Array.Empty(); + + int totalLength = 0; + foreach (var arr in arrays) + { + if (arr != null) + totalLength += arr.Length; + } + + var result = new byte[totalLength]; + int offset = 0; + + foreach (var arr in arrays) + { + if (arr != null && arr.Length > 0) + { + Array.Copy(arr, 0, result, offset, arr.Length); + offset += arr.Length; + } + } + + return result; + } + + /// + /// 截取字节数组 + /// + public static byte[] SubArray(this byte[] bytes, int startIndex, int length) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if (startIndex < 0 || startIndex >= bytes.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + if (length < 0 || startIndex + length > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + var result = new byte[length]; + Array.Copy(bytes, startIndex, result, 0, length); + return result; + } + + /// + /// 截取字节数组(从指定位置到末尾) + /// + public static byte[] SubArray(this byte[] bytes, int startIndex) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if (startIndex < 0) + startIndex = 0; + + if (startIndex >= bytes.Length) + return Array.Empty(); + + return SubArray(bytes, startIndex, bytes.Length - startIndex); + } + + #endregion + + #region 字节数组比较 + + /// + /// 比较两个字节数组是否相等 + /// + public static bool EqualsTo(this byte[]? bytes, byte[]? other) + { + if (ReferenceEquals(bytes, other)) + return true; + + if (bytes == null || other == null) + return false; + + if (bytes.Length != other.Length) + return false; + + for (int i = 0; i < bytes.Length; i++) + { + if (bytes[i] != other[i]) + return false; + } + + return true; + } + + /// + /// 字节数组比较(返回差异索引) + /// + public static int[] Diff(this byte[]? bytes, byte[]? other) + { + if (bytes == null || other == null) + return Array.Empty(); + + var minLength = Math.Min(bytes.Length, other.Length); + var diffs = new System.Collections.Generic.List(); + + for (int i = 0; i < minLength; i++) + { + if (bytes[i] != other[i]) + diffs.Add(i); + } + + return diffs.ToArray(); + } + + #endregion + + #region 字节数组与基本类型转换 + + /// + /// 将字节数组转换为整数(小端序) + /// + public static int ToInt32(this byte[] bytes, int startIndex = 0) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if (startIndex < 0 || startIndex + 4 > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + return bytes[startIndex] | (bytes[startIndex + 1] << 8) | (bytes[startIndex + 2] << 16) | (bytes[startIndex + 3] << 24); + } + + /// + /// 将整数转换为字节数组(小端序) + /// + public static byte[] ToBytes(this int value) + { + return new[] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF), (byte)((value >> 16) & 0xFF), (byte)((value >> 24) & 0xFF) }; + } + + /// + /// 将字节数组转换为长整数(小端序) + /// + public static long ToInt64(this byte[] bytes, int startIndex = 0) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if (startIndex < 0 || startIndex + 8 > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + return BitConverter.ToInt64(bytes, startIndex); + } + + /// + /// 将长整数转换为字节数组(小端序) + /// + public static byte[] ToBytes(this long value) + { + return BitConverter.GetBytes(value); + } + + /// + /// 将字节数组转换为短整数(小端序) + /// + public static short ToInt16(this byte[] bytes, int startIndex = 0) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if (startIndex < 0 || startIndex + 2 > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + return (short)(bytes[startIndex] | (bytes[startIndex + 1] << 8)); + } + + /// + /// 将短整数转换为字节数组(小端序) + /// + public static byte[] ToBytes(this short value) + { + return new[] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF) }; + } + + #endregion + + #region 字节数组编码解码 + + /// + /// 将字节数组按UTF-8编码转换为字符串 + /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] + /// + [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] + public static string ToUtf8String(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 将字节数组按指定编码转换为字符串 + /// [Obsolete("请直接使用 encoding.GetString(bytes)")] + /// + [Obsolete("请直接使用 encoding.GetString(bytes)", false)] + public static string ToString(this byte[] bytes, Encoding encoding) + { + if (bytes == null || bytes.Length == 0) + return string.Empty; + + encoding ??= Encoding.UTF8; + return encoding.GetString(bytes); + } + + /// + /// 将字符串按UTF-8编码转换为字节数组 + /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)")] + /// + [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)", false)] + public static byte[] ToUtf8Bytes(this string str) + { + if (string.IsNullOrEmpty(str)) + return Array.Empty(); + + return Encoding.UTF8.GetBytes(str); + } + + /// + /// 将字符串按指定编码转换为字节数组 + /// [Obsolete("请直接使用 encoding.GetBytes(str)")] + /// + [Obsolete("请直接使用 encoding.GetBytes(str)", false)] + public static byte[] ToBytes(this string str, Encoding encoding) + { + if (string.IsNullOrEmpty(str)) + return Array.Empty(); + + encoding ??= Encoding.UTF8; + return encoding.GetBytes(str); + } + + #endregion + + #region 字节数组压缩解压 + + /// + /// 压缩字节数组(使用 GZip) + /// + public static byte[]? Compress(this byte[]? bytes) + { + if (bytes == null || bytes.Length == 0) + return bytes; + + using var output = new MemoryStream(); + using (var gzip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress)) + { + gzip.Write(bytes, 0, bytes.Length); + } + return output.ToArray(); + } + + /// + /// 解压字节数组(使用 GZip) + /// + public static byte[]? Decompress(this byte[]? bytes) + { + if (bytes == null || bytes.Length == 0) + return bytes; + + using var input = new MemoryStream(bytes); + using var gzip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress); + using var output = new MemoryStream(); + gzip.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region 字节数组哈希 + + /// + /// 计算字节数组的 MD5 哈希值 + /// + public static byte[] ToMd5(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return Array.Empty(); + + using var md5 = System.Security.Cryptography.MD5.Create(); + return md5.ComputeHash(bytes); + } + + /// + /// 计算字节数组的 SHA1 哈希值 + /// + public static byte[] ToSha1(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return Array.Empty(); + + using var sha1 = System.Security.Cryptography.SHA1.Create(); + return sha1.ComputeHash(bytes); + } + + /// + /// 计算字节数组的 SHA256 哈希值 + /// + public static byte[] ToSha256(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return Array.Empty(); + + using var sha256 = System.Security.Cryptography.SHA256.Create(); + return sha256.ComputeHash(bytes); + } + + /// + /// 计算字节数组的 CRC32 校验值 + /// + public static uint ToCrc32(this byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return 0; + + uint crc = 0xFFFFFFFF; + foreach (var b in bytes) + { + crc ^= b; + for (int i = 0; i < 8; i++) + { + crc = (crc >> 1) ^ ((crc & 1) == 1 ? 0xEDB88320 : 0); + } + } + return ~crc; + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/Extension.Convert.cs b/EasyTool.Core/ConvertCategory/ConvertExtension.cs similarity index 95% rename from EasyTool.Core/ConvertCategory/Extension.Convert.cs rename to EasyTool.Core/ConvertCategory/ConvertExtension.cs index c2e380c..907c9bf 100644 --- a/EasyTool.Core/ConvertCategory/Extension.Convert.cs +++ b/EasyTool.Core/ConvertCategory/ConvertExtension.cs @@ -5,9 +5,9 @@ namespace EasyTool.ConvertCategory { /// - /// 数据类型转化 + /// 数据类型转化扩展 /// - public static partial class Extension + public static class ConvertExtension { #region ==数据转换扩展== @@ -205,9 +205,9 @@ public static string ToIntString(this bool b) /// /// 布尔值转换为整数1或者0 + /// [Obsolete("请直接使用 Convert.ToInt32(b)")] /// - /// - /// + [Obsolete("请直接使用 Convert.ToInt32(b)", false)] public static int ToInt(this bool b) { return b ? 1 : 0; @@ -270,9 +270,11 @@ public static string ToHex(this byte[] bytes, bool lowerCase = true) /// /// 转换为Base64 + /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] /// /// /// + [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] public static string ToBase64(this byte[] bytes) { if (bytes == null) @@ -313,10 +315,11 @@ public static DateTime TimestampToDateTime(this string timeStamp) /// /// 字符串转Guid + /// [Obsolete("请直接使用 Guid.TryParse(guid, out var result) 或 new Guid(guid)")] /// /// /// - + [Obsolete("请直接使用 Guid.TryParse(guid, out var result)", false)] public static Guid? ToGuid(this string guid) { try diff --git a/EasyTool.Core/ConvertCategory/ConvertUtil.cs b/EasyTool.Core/ConvertCategory/ConvertUtil.cs index 752a52f..015812a 100644 --- a/EasyTool.Core/ConvertCategory/ConvertUtil.cs +++ b/EasyTool.Core/ConvertCategory/ConvertUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace EasyTool { @@ -10,22 +10,22 @@ public static class ConvertUtil /// /// 将对象转换为指定类型,转换失败返回指定类型的默认值 /// - public static T To(object value) + public static T? To(object? value) { try { - return (T)Convert.ChangeType(value, typeof(T)); + return (T)Convert.ChangeType(value, typeof(T))!; } catch { - return default(T); + return default; } } /// /// 将字符串转换为整型,转换失败返回0 /// - public static int ToInt32(string value) + public static int ToInt32(string? value) { int result; if (int.TryParse(value, out result)) @@ -38,7 +38,7 @@ public static int ToInt32(string value) /// /// 将字符串转换为长整型,转换失败返回0 /// - public static long ToInt64(string value) + public static long ToInt64(string? value) { long result; if (long.TryParse(value, out result)) @@ -51,7 +51,7 @@ public static long ToInt64(string value) /// /// 将字符串转换为布尔型,转换失败返回默认值,默认值false /// - public static bool ToBoolean(string data, bool defValue = false) + public static bool ToBoolean(string? data, bool defValue = false) { //如果为空则返回默认值 if (string.IsNullOrEmpty(data)) @@ -73,7 +73,7 @@ public static bool ToBoolean(string data, bool defValue = false) /// /// 将对象转换为布尔型,转换失败返回默认值,默认值false /// - public static bool ToBoolean(object data, bool defValue = false) + public static bool ToBoolean(object? data, bool defValue = false) { //如果为空则返回默认值 if (data == null || Convert.IsDBNull(data)) @@ -94,7 +94,7 @@ public static bool ToBoolean(object data, bool defValue = false) /// /// 将字符串转换为单精度浮点型,转换失败返回0 /// - public static float ToSingle(string value) + public static float ToSingle(string? value) { float result; if (float.TryParse(value, out result)) @@ -107,7 +107,7 @@ public static float ToSingle(string value) /// /// 将字符串转换为双精度浮点型,转换失败返回0 /// - public static double ToDouble(string value) + public static double ToDouble(string? value) { double result; if (double.TryParse(value, out result)) @@ -120,7 +120,7 @@ public static double ToDouble(string value) /// /// 将字符串转换为十进制数,转换失败返回0 /// - public static decimal ToDecimal(string value) + public static decimal ToDecimal(string? value) { decimal result; if (decimal.TryParse(value, out result)) @@ -133,7 +133,7 @@ public static decimal ToDecimal(string value) /// /// 将字符串转换为日期时间,转换失败返回DateTime.MinValue /// - public static DateTime ToDateTime(string value) + public static DateTime ToDateTime(string? value) { DateTime result; if (DateTime.TryParse(value, out result)) @@ -146,14 +146,14 @@ public static DateTime ToDateTime(string value) /// /// 将字符串转换为枚举类型,转换失败返回默认值 /// - public static T ToEnum(string value, T defaultValue = default(T)) where T : struct + public static T ToEnum(string? value, T? defaultValue = default) where T : struct { T result; if (Enum.TryParse(value, out result)) { return result; } - return defaultValue; + return defaultValue ?? default; } diff --git a/EasyTool.Core/ConvertCategory/NumberExtension.cs b/EasyTool.Core/ConvertCategory/NumberExtension.cs new file mode 100644 index 0000000..128b7b8 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/NumberExtension.cs @@ -0,0 +1,546 @@ +using System; + +namespace EasyTool.Extension +{ + /// + /// 数字类型扩展方法 + /// + public static class NumberExtension + { + #region 整数扩展 + + /// + /// 判断整数是否为偶数 + /// + public static bool IsEven(this int value) + { + return value % 2 == 0; + } + + /// + /// 判断整数是否为奇数 + /// + public static bool IsOdd(this int value) + { + return value % 2 != 0; + } + + /// + /// 判断长整数是否为偶数 + /// + public static bool IsEven(this long value) + { + return value % 2 == 0; + } + + /// + /// 判断长整数是否为奇数 + /// + public static bool IsOdd(this long value) + { + return value % 2 != 0; + } + + /// + /// 判断短整数是否为偶数 + /// + public static bool IsEven(this short value) + { + return value % 2 == 0; + } + + /// + /// 判断短整数是否为奇数 + /// + public static bool IsOdd(this short value) + { + return value % 2 != 0; + } + + #endregion + + #region 浮点数扩展 + + /// + /// 判断浮点数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this float value, float min, float max) + { + return value >= min && value <= max; + } + + /// + /// 判断双精度浮点数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this double value, double min, double max) + { + return value >= min && value <= max; + } + + /// + /// 判断小数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this decimal value, decimal min, decimal max) + { + return value >= min && value <= max; + } + + /// + /// 限制浮点数在指定范围内 + /// + public static float Clamp(this float value, float min, float max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制双精度浮点数在指定范围内 + /// + public static double Clamp(this double value, double min, double max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制小数在指定范围内 + /// + public static decimal Clamp(this decimal value, decimal min, decimal max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制整数在指定范围内 + /// + public static int Clamp(this int value, int min, int max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制长整数在指定范围内 + /// + public static long Clamp(this long value, long min, long max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + #endregion + + #region 百分比转换 + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this double value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this float value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this decimal value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + #endregion + + #region 文件大小转换 + + /// + /// 将字节数转换为人类可读的文件大小格式 + /// + /// 字节数 + public static string ToFileSize(this long bytes) + { + string[] sizes = { "B", "KB", "MB", "GB", "TB" }; + double len = bytes; + int order = 0; + + while (len >= 1024 && order < sizes.Length - 1) + { + order++; + len /= 1024; + } + + return $"{len:0.##} {sizes[order]}"; + } + + /// + /// 将字节数转换为人类可读的文件大小格式 + /// + /// 字节数 + public static string ToFileSize(this int bytes) + { + return ((long)bytes).ToFileSize(); + } + + #endregion + + #region 时间转换 + + /// + /// 将秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpan(this double seconds) + { + return TimeSpan.FromSeconds(seconds); + } + + /// + /// 将秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpan(this int seconds) + { + return TimeSpan.FromSeconds(seconds); + } + + /// + /// 将毫秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpanFromMilliseconds(this long milliseconds) + { + return TimeSpan.FromMilliseconds(milliseconds); + } + + /// + /// 将毫秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpanFromMilliseconds(this int milliseconds) + { + return TimeSpan.FromMilliseconds(milliseconds); + } + + #endregion + + #region 数学运算 + + /// + /// 计算数值的平方 + /// + public static int Square(this int value) + { + return value * value; + } + + /// + /// 计算数值的平方 + /// + public static long Square(this long value) + { + return value * value; + } + + /// + /// 计算数值的平方 + /// + public static double Square(this double value) + { + return value * value; + } + + /// + /// 计算数值的立方 + /// + public static int Cube(this int value) + { + return value * value * value; + } + + /// + /// 计算数值的立方 + /// + public static long Cube(this long value) + { + return value * value * value; + } + + /// + /// 计算数值的立方 + /// + public static double Cube(this double value) + { + return value * value * value; + } + + /// + /// 计算数值的绝对值 + /// [Obsolete("请直接使用 Math.Abs(value)")] + /// + [Obsolete("请直接使用 Math.Abs(value)", false)] + public static int Abs(this int value) + { + return Math.Abs(value); + } + + /// + /// 计算数值的绝对值 + /// [Obsolete("请直接使用 Math.Abs(value)")] + /// + [Obsolete("请直接使用 Math.Abs(value)", false)] + public static long Abs(this long value) + { + return Math.Abs(value); + } + + /// + /// 计算数值的绝对值 + /// [Obsolete("请直接使用 Math.Abs(value)")] + /// + [Obsolete("请直接使用 Math.Abs(value)", false)] + public static float Abs(this float value) + { + return Math.Abs(value); + } + + /// + /// 计算数值的绝对值 + /// [Obsolete("请直接使用 Math.Abs(value)")] + /// + [Obsolete("请直接使用 Math.Abs(value)", false)] + public static double Abs(this double value) + { + return Math.Abs(value); + } + + /// + /// 计算数值的绝对值 + /// [Obsolete("请直接使用 Math.Abs(value)")] + /// + [Obsolete("请直接使用 Math.Abs(value)", false)] + public static decimal Abs(this decimal value) + { + return Math.Abs(value); + } + + #endregion + + #region 数值判断 + + /// + /// 判断浮点数是否为 NaN + /// [Obsolete("请直接使用 float.IsNaN(value)")] + /// + [Obsolete("请直接使用 float.IsNaN(value)", false)] + public static bool IsNaN(this float value) + { + return float.IsNaN(value); + } + + /// + /// 判断双精度浮点数是否为 NaN + /// [Obsolete("请直接使用 double.IsNaN(value)")] + /// + [Obsolete("请直接使用 double.IsNaN(value)", false)] + public static bool IsNaN(this double value) + { + return double.IsNaN(value); + } + + /// + /// 判断浮点数是否为无穷大 + /// [Obsolete("请直接使用 float.IsInfinity(value)")] + /// + [Obsolete("请直接使用 float.IsInfinity(value)", false)] + public static bool IsInfinity(this float value) + { + return float.IsInfinity(value); + } + + /// + /// 判断双精度浮点数是否为无穷大 + /// [Obsolete("请直接使用 double.IsInfinity(value)")] + /// + [Obsolete("请直接使用 double.IsInfinity(value)", false)] + public static bool IsInfinity(this double value) + { + return double.IsInfinity(value); + } + + /// + /// 判断浮点数是否为正无穷大 + /// [Obsolete("请直接使用 float.IsPositiveInfinity(value)")] + /// + [Obsolete("请直接使用 float.IsPositiveInfinity(value)", false)] + public static bool IsPositiveInfinity(this float value) + { + return float.IsPositiveInfinity(value); + } + + /// + /// 判断双精度浮点数是否为正无穷大 + /// [Obsolete("请直接使用 double.IsPositiveInfinity(value)")] + /// + [Obsolete("请直接使用 double.IsPositiveInfinity(value)", false)] + public static bool IsPositiveInfinity(this double value) + { + return double.IsPositiveInfinity(value); + } + + /// + /// 判断浮点数是否为负无穷大 + /// [Obsolete("请直接使用 float.IsNegativeInfinity(value)")] + /// + [Obsolete("请直接使用 float.IsNegativeInfinity(value)", false)] + public static bool IsNegativeInfinity(this float value) + { + return float.IsNegativeInfinity(value); + } + + /// + /// 判断双精度浮点数是否为负无穷大 + /// [Obsolete("请直接使用 double.IsNegativeInfinity(value)")] + /// + [Obsolete("请直接使用 double.IsNegativeInfinity(value)", false)] + public static bool IsNegativeInfinity(this double value) + { + return double.IsNegativeInfinity(value); + } + + #endregion + + #region 数值格式化 + + /// + /// 将数字格式化为带千分位的字符串 + /// + public static string ToThousandsSeparator(this int value) + { + return value.ToString("#,##0"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + public static string ToThousandsSeparator(this long value) + { + return value.ToString("#,##0"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this double value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this float value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this decimal value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字转换为中文大写金额 + /// + public static string ToChineseMoney(this decimal value) + { + if (value == 0) + return "零元整"; + + string[] digits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + string[] units = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" }; + + string result = string.Empty; + bool hasYuan = false; + bool hasJiao = false; + bool hasFen = false; + + // 处理整数部分 + long integerPart = (long)value; + if (integerPart > 0) + { + string integerStr = integerPart.ToString(); + int length = integerStr.Length; + + for (int i = 0; i < length; i++) + { + int digit = integerStr[i] - '0'; + int pos = length - i - 1; + + if (digit != 0) + { + result += digits[digit] + units[pos]; + hasYuan = true; + } + else if (result.Length > 0 && result[result.Length - 1] != '零') + { + result += '零'; + } + } + + if (hasYuan) + result += '元'; + } + + // 处理小数部分 + decimal decimalPart = value - integerPart; + int jiao = (int)(decimalPart * 10); + int fen = (int)(decimalPart * 100) % 10; + + if (jiao > 0) + { + result += digits[jiao] + "角"; + hasJiao = true; + } + + if (fen > 0) + { + result += digits[fen] + "分"; + hasFen = true; + } + + if (!hasJiao && !hasFen && hasYuan) + result += "整"; + + // 清理多余的零 + result = result.Replace("零零", "零"); + if (result.EndsWith("零元")) + result = result.Substring(0, result.Length - 2) + "元"; + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs index 2b7f32f..bbf5b8d 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs @@ -103,5 +103,355 @@ public static class DateTimeExtension /// 指定日期。 /// 指定日期所在年份的所有日期。 public static List GetYearDays(this DateTime date) => DateTimeUtil.GetYearDays(date); + + + + #region 新增扩展方法 + + /// + /// 判断日期是否是今天 + /// + public static bool IsToday(this DateTime date) + { + return date.Date == DateTime.Today; + } + + /// + /// 判断日期是否是昨天 + /// + public static bool IsYesterday(this DateTime date) + { + return date.Date == DateTime.Today.AddDays(-1); + } + + /// + /// 判断日期是否是明天 + /// + public static bool IsTomorrow(this DateTime date) + { + return date.Date == DateTime.Today.AddDays(1); + } + + /// + /// 判断日期是否在本周 + /// + public static bool IsThisWeek(this DateTime date) + { + var today = DateTime.Today; + var firstDayOfWeek = today.GetFirstDayOfWeek(); + var lastDayOfWeek = firstDayOfWeek.AddDays(6); + return date.Date >= firstDayOfWeek && date.Date <= lastDayOfWeek; + } + + /// + /// 判断日期是否在本月 + /// + public static bool IsThisMonth(this DateTime date) + { + var today = DateTime.Today; + return date.Year == today.Year && date.Month == today.Month; + } + + /// + /// 判断日期是否在本年 + /// + public static bool IsThisYear(this DateTime date) + { + return date.Year == DateTime.Today.Year; + } + + /// + /// 判断日期是否在指定范围内(包含边界) + /// + public static bool IsBetween(this DateTime date, DateTime startDate, DateTime endDate) + { + return date >= startDate && date <= endDate; + } + + /// + /// 计算年龄 + /// + public static int ToAge(this DateTime birthDate) + { + var today = DateTime.Today; + int age = today.Year - birthDate.Year; + + if (birthDate > today.AddYears(-age)) + age--; + + return age; + } + + /// + /// 将日期转换为 Unix 时间戳(秒) + /// [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeSeconds()")] + /// + [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeSeconds()", false)] + public static long ToUnixTimestamp(this DateTime date) + { + return new DateTimeOffset(date).ToUnixTimeSeconds(); + } + + /// + /// 将日期转换为 Unix 时间戳(毫秒) + /// [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeMilliseconds()")] + /// + [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeMilliseconds()", false)] + public static long ToUnixTimestampMilliseconds(this DateTime date) + { + return new DateTimeOffset(date).ToUnixTimeMilliseconds(); + } + + /// + /// 从 Unix 时间戳(秒)转换为日期 + /// [Obsolete("请使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime")] + /// + [Obsolete("请使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime", false)] + public static DateTime FromUnixTimestamp(this long timestamp) + { + return DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime; + } + + /// + /// 从 Unix 时间戳(毫秒)转换为日期 + /// [Obsolete("请使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime")] + /// + [Obsolete("请使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime", false)] + public static DateTime FromUnixTimestampMilliseconds(this long timestamp) + { + return DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime; + } + + /// + /// 判断是否是周末(周六或周日) + /// + public static bool IsWeekend(this DateTime date) + { + return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 获取日期所在月的最后一天 + /// + public static DateTime GetLastDayOfMonth(this DateTime date) + { + return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); + } + + /// + /// 获取日期所在月的最后一天 + /// + public static DateTime GetLastDayOfWeek(this DateTime date) + { + var firstDay = date.GetFirstDayOfWeek(); + return firstDay.AddDays(6); + } + + /// + /// 获取日期所在季度的最后一天 + /// + public static DateTime GetLastDayOfQuarter(this DateTime date) + { + int currentQuarter = (date.Month - 1) / 3 + 1; + int lastMonthOfQuarter = currentQuarter * 3; + return new DateTime(date.Year, lastMonthOfQuarter, DateTime.DaysInMonth(date.Year, lastMonthOfQuarter)); + } + + /// + /// 获取日期所在年的最后一天 + /// + public static DateTime GetLastDayOfYear(this DateTime date) + { + return new DateTime(date.Year, 12, 31); + } + + /// + /// 获取日期的中文星期表示 + /// + public static string ToChineseWeekDay(this DateTime date) + { + return date.DayOfWeek switch + { + DayOfWeek.Monday => "星期一", + DayOfWeek.Tuesday => "星期二", + DayOfWeek.Wednesday => "星期三", + DayOfWeek.Thursday => "星期四", + DayOfWeek.Friday => "星期五", + DayOfWeek.Saturday => "星期六", + DayOfWeek.Sunday => "星期日", + _ => string.Empty + }; + } + + /// + /// 获取日期的中文星期简称 + /// + public static string ToChineseWeekDayShort(this DateTime date) + { + return date.DayOfWeek switch + { + DayOfWeek.Monday => "周一", + DayOfWeek.Tuesday => "周二", + DayOfWeek.Wednesday => "周三", + DayOfWeek.Thursday => "周四", + DayOfWeek.Friday => "周五", + DayOfWeek.Saturday => "周六", + DayOfWeek.Sunday => "周日", + _ => string.Empty + }; + } + + /// + /// 判断是否是闰年 + /// [Obsolete("请直接使用 DateTime.IsLeapYear(date.Year)")] + /// + [Obsolete("请直接使用 DateTime.IsLeapYear(date.Year)", false)] + public static bool IsLeapYear(this DateTime date) + { + return DateTime.IsLeapYear(date.Year); + } + + /// + /// 获取日期所在季度的数字(1-4) + /// + public static int GetQuarter(this DateTime date) + { + return (date.Month - 1) / 3 + 1; + } + + /// + /// 获取日期所在周在本年的周数 + /// + public static int GetWeekOfYear(this DateTime date) + { + var culture = CultureInfo.CurrentCulture; + var calendar = culture.Calendar; + var weekRule = culture.DateTimeFormat.CalendarWeekRule; + var firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek; + return calendar.GetWeekOfYear(date, weekRule, firstDayOfWeek); + } + + /// + /// 添加工作日 + /// + /// 起始日期 + /// 要添加的工作日数 + public static DateTime AddWorkDays(this DateTime date, int workDays) + { + var result = date; + int daysToAdd = Math.Abs(workDays); + int direction = workDays >= 0 ? 1 : -1; + + while (daysToAdd > 0) + { + result = result.AddDays(direction); + if (result.IsWorkDay()) + daysToAdd--; + } + + return result; + } + + /// + /// 获取日期的友好字符串表示 + /// + public static string ToFriendlyString(this DateTime date) + { + var today = DateTime.Today; + var span = today - date.Date; + + return span.TotalDays switch + { + 0 => "今天", + 1 => "昨天", + -1 => "明天", + _ when span.TotalDays > 0 && span.TotalDays <= 7 => $"上周{date.ToChineseWeekDayShort()}", + _ when span.TotalDays < 0 && span.TotalDays >= -7 => $"下周{date.ToChineseWeekDayShort()}", + _ when date.Year == today.Year => date.ToString("MM月dd日"), + _ => date.ToString("yyyy年MM月dd日") + }; + } + + /// + /// 获取两个日期之间相差的月数 + /// + public static int GetMonthsBetween(this DateTime startDate, DateTime endDate) + { + int months = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month; + + // 如果结束日期的日小于开始日期的日,需要减去一个月 + if (endDate.Day < startDate.Day) + { + months--; + } + + return Math.Abs(months); + } + + /// + /// 获取两个日期之间相差的年数 + /// + public static int GetYearsBetween(this DateTime startDate, DateTime endDate) + { + int years = endDate.Year - startDate.Year; + + // 如果结束日期的月和日小于开始日期的月和日,需要减去一年 + if (endDate.Month < startDate.Month || (endDate.Month == startDate.Month && endDate.Day < startDate.Day)) + { + years--; + } + + return Math.Abs(years); + } + + /// + /// 获取一天的开始时间(00:00:00) + /// + public static DateTime StartOfDay(this DateTime date) + { + return date.Date; + } + + /// + /// 获取一天的结束时间(23:59:59) + /// + public static DateTime EndOfDay(this DateTime date) + { + return date.Date.AddDays(1).AddTicks(-1); + } + + /// + /// 获取月的开始时间 + /// + public static DateTime StartOfMonth(this DateTime date) + { + return new DateTime(date.Year, date.Month, 1); + } + + /// + /// 获取月的结束时间 + /// + public static DateTime EndOfMonth(this DateTime date) + { + return date.GetLastDayOfMonth().EndOfDay(); + } + + /// + /// 获取年的开始时间 + /// + public static DateTime StartOfYear(this DateTime date) + { + return new DateTime(date.Year, 1, 1); + } + + /// + /// 获取年的结束时间 + /// + public static DateTime EndOfYear(this DateTime date) + { + return new DateTime(date.Year, 12, 31).EndOfDay(); + } + + #endregion } } diff --git a/EasyTool.Core/DateTimeCategory/TimerUtil.cs b/EasyTool.Core/DateTimeCategory/TimerUtil.cs index 68f56e4..e87f551 100644 --- a/EasyTool.Core/DateTimeCategory/TimerUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimerUtil.cs @@ -42,8 +42,10 @@ public static TimeSpan GetElapsedTime() /// /// 创建一个新的 Stopwatch 并启动计时。 + /// [Obsolete("请直接使用 Stopwatch.StartNew()")] /// /// 一个新的 Stopwatch。 + [Obsolete("请直接使用 Stopwatch.StartNew()", false)] public static Stopwatch StartNew() { Stopwatch stopwatch = new Stopwatch(); @@ -100,8 +102,10 @@ public static void MeasureAndLog(Action action, string fileName) /// /// 等待指定的时间 + /// [Obsolete("请直接使用 Thread.Sleep(milliseconds)")] /// /// 要等待的毫秒数。 + [Obsolete("请直接使用 Thread.Sleep(milliseconds)", false)] public static void Wait(int milliseconds) { System.Threading.Thread.Sleep(milliseconds); diff --git a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs b/EasyTool.Core/DateTimeCategory/TimestampUtil.cs index 8e712c2..6c7912d 100644 --- a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimestampUtil.cs @@ -9,8 +9,10 @@ public static class TimestampUtil { /// /// 获取当前时间戳(毫秒级) + /// [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()")] /// /// 当前时间戳(毫秒级) + [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()", false)] public static long GetCurrentTimestamp() { DateTime dt = DateTime.UtcNow; @@ -20,9 +22,11 @@ public static long GetCurrentTimestamp() /// /// 将时间戳(毫秒级)转换为 DateTime 类型 + /// [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime")] /// /// 时间戳(毫秒级) /// 转换后的 DateTime 类型 + [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime", false)] public static DateTime ConvertToDateTime(long timestamp) { DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); @@ -31,9 +35,11 @@ public static DateTime ConvertToDateTime(long timestamp) /// /// 将 DateTime 类型转换为时间戳(毫秒级) + /// [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeMilliseconds()")] /// /// DateTime 类型 /// 转换后的时间戳(毫秒级) + [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeMilliseconds()", false)] public static long ConvertToTimestamp(DateTime dateTime) { TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); @@ -42,8 +48,10 @@ public static long ConvertToTimestamp(DateTime dateTime) /// /// 获取当前时间戳(秒级) + /// [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeSeconds()")] /// /// 当前时间戳(秒级) + [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeSeconds()", false)] public static long GetCurrentTimestampSeconds() { return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; @@ -51,9 +59,11 @@ public static long GetCurrentTimestampSeconds() /// /// 将时间戳(秒级)转换为 DateTime 类型 + /// [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime")] /// /// 时间戳(秒级) /// 转换后的 DateTime 类型 + [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime", false)] public static DateTime ConvertToDateTimeSeconds(long timestamp) { return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp); @@ -61,9 +71,11 @@ public static DateTime ConvertToDateTimeSeconds(long timestamp) /// /// 将 DateTime 类型转换为时间戳(秒级) + /// [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeSeconds()")] /// /// DateTime 类型 /// 转换后的时间戳(秒级) + [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeSeconds()", false)] public static long ConvertToTimestampSeconds(DateTime dateTime) { return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index 405fa8d..e769fec 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -3,11 +3,15 @@ netstandard2.1;net10.0 latest - enable true $(NoWarn); $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) + + annotations + + enable + Joce.EasyTool.Core 一个大西瓜,TimChen 2026.0108.1 diff --git a/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs b/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs index 84cafe5..d458173 100644 --- a/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs +++ b/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs @@ -1,31 +1,79 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Text; namespace EasyTool.IEnumerableCategory { /// - /// 通用拓展 + /// IEnumerable 通用扩展方法 /// public static class IEnumerableExtensions { + #region 空值处理 - - - #region IEnumerable拓展 /// - /// 对List 等集合Foreach的时候不用在上层判空,直接加上这个就好 + /// 对 List 等集合 Foreach 的时候不用在上层判空,直接加上这个就好 /// - /// - /// - /// public static IEnumerable CheckNull(this IEnumerable values) { return values is null ? new List(0) : values; } + /// + /// 判断集合是否为空或 null + /// + public static bool IsNullOrEmpty(this IEnumerable source) + { + return source == null || !source.Any(); + } + + /// + /// 判断集合是否非空 + /// + public static bool IsNotEmpty(this IEnumerable source) + { + return source != null && source.Any(); + } + + #endregion + + #region 遍历操作 + + /// + /// 遍历集合并对每个元素执行指定操作 + /// + public static void ForEach(this IEnumerable source, Action action) + { + if (source == null || action == null) + return; + + foreach (var item in source) + { + action(item); + } + } + + /// + /// 遍历集合并对每个元素及其索引执行指定操作 + /// + public static void ForEach(this IEnumerable source, Action action) + { + if (source == null || action == null) + return; + + int index = 0; + foreach (var item in source) + { + action(item, index++); + } + } + + #endregion + #region 集合运算 + /// /// 求集合的笛卡尔积 /// @@ -40,7 +88,384 @@ select accseq.Concat(new[] { item })); } + /// + /// 按指定键去重 + /// + public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) + { + if (source == null) + yield break; + + var seenKeys = new HashSet(); + foreach (var item in source) + { + if (seenKeys.Add(keySelector(item))) + { + yield return item; + } + } + } + + /// + /// 批量处理集合 + /// + /// 每批的大小 + public static IEnumerable> Batch(this IEnumerable source, int batchSize) + { + if (source == null) + yield break; + + if (batchSize <= 0) + throw new ArgumentException("batchSize must be greater than 0", nameof(batchSize)); + + var batch = new List(batchSize); + foreach (var item in source) + { + batch.Add(item); + if (batch.Count == batchSize) + { + yield return batch; + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + yield return batch; + } + } + + #endregion + + #region 转换操作 + + /// + /// 将集合转换为 DataTable + /// + public static DataTable ToDataTable(this IEnumerable source) + { + var table = new DataTable(typeof(T).Name); + + var properties = typeof(T).GetProperties(); + foreach (var prop in properties) + { + table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); + } + + foreach (var item in source) + { + var row = table.NewRow(); + foreach (var prop in properties) + { + row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; + } + table.Rows.Add(row); + } + + return table; + } + + /// + /// 将集合转换为 HashSet + /// + public static HashSet ToHashSet(this IEnumerable source) + { + if (source == null) + return new HashSet(); + + return new HashSet(source); + } + + /// + /// 将集合转换为 Queue + /// + public static Queue ToQueue(this IEnumerable source) + { + if (source == null) + return new Queue(); + + return new Queue(source); + } + + /// + /// 将集合转换为 Stack + /// + public static Stack ToStack(this IEnumerable source) + { + if (source == null) + return new Stack(); + + return new Stack(source); + } + + /// + /// 将集合转换为 LinkedList + /// + public static LinkedList ToLinkedList(this IEnumerable source) + { + if (source == null) + return new LinkedList(); + + return new LinkedList(source); + } + + #endregion + + #region 连接操作 + + /// + /// 将集合元素连接成字符串 + /// + /// 分隔符 + public static string JoinAsString(this IEnumerable source, string separator = ",") + { + if (source == null) + return string.Empty; + + return string.Join(separator, source); + } + + /// + /// 将集合元素连接成字符串(使用格式化) + /// + /// 分隔符 + /// 格式化字符串 + public static string JoinAsString(this IEnumerable source, string separator, string format) + { + if (source == null) + return string.Empty; + + return string.Join(separator, source.Select(item => string.Format(format, item))); + } + #endregion + + #region 分页操作 + + /// + /// 分页 + /// + /// 页码(从1开始) + /// 每页大小 + public static IEnumerable Page(this IEnumerable source, int pageIndex, int pageSize) + { + if (source == null || pageIndex < 1 || pageSize < 1) + yield break; + + int skip = (pageIndex - 1) * pageSize; + foreach (var item in source.Skip(skip).Take(pageSize)) + { + yield return item; + } + } + + #endregion + + #region 随机操作 + + /// + /// 随机排序 + /// + public static IEnumerable Shuffle(this IEnumerable source) + { + if (source == null) + yield break; + + var random = new Random(); + var list = source.ToList(); + + for (int i = list.Count - 1; i > 0; i--) + { + int j = random.Next(i + 1); + (list[i], list[j]) = (list[j], list[i]); + } + + foreach (var item in list) + { + yield return item; + } + } + + /// + /// 随机获取一个元素 + /// + public static T Random(this IEnumerable source) + { + if (source == null) + return default; + + var list = source.ToList(); + if (list.Count == 0) + return default; + + var random = new Random(); + return list[random.Next(list.Count)]; + } + + /// + /// 随机获取指定数量的元素 + /// + public static IEnumerable RandomTake(this IEnumerable source, int count) + { + if (source == null || count <= 0) + yield break; + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + count = Math.Min(count, list.Count); + var random = new Random(); + var selected = new HashSet(); + + while (selected.Count < count) + { + selected.Add(random.Next(list.Count)); + } + + foreach (var index in selected) + { + yield return list[index]; + } + } + + #endregion + + #region 条件操作 + + /// + /// 根据条件执行不同的操作 + /// + public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) + { + if (source == null) + yield break; + + if (condition) + { + foreach (var item in source.Where(predicate)) + { + yield return item; + } + } + else + { + foreach (var item in source) + { + yield return item; + } + } + } + + /// + /// 如果集合为空则返回默认集合 + /// + public static IEnumerable IfEmpty(this IEnumerable source, IEnumerable defaultValue) + { + if (source == null || !source.Any()) + return defaultValue ?? Enumerable.Empty(); + + return source; + } + + #endregion + + #region 统计操作 + + /// + /// 统计满足条件的元素数量 + /// + public static int CountEx(this IEnumerable source, Func predicate) + { + if (source == null) + return 0; + + return source.Count(predicate); + } + + #endregion + + #region 索引操作 + + /// + /// 获取指定索引处的元素 + /// + public static T ElementAtOrDefault(this IEnumerable source, int index, T defaultValue) + { + if (source == null || index < 0) + return defaultValue; + + int i = 0; + foreach (var item in source) + { + if (i == index) + return item; + i++; + } + + return defaultValue; + } + + /// + /// 获取第一个元素,如果集合为空则返回默认值 + /// + public static T FirstOrValue(this IEnumerable source, T defaultValue) + { + if (source == null) + return defaultValue; + + foreach (var item in source) + { + return item; + } + + return defaultValue; + } + + /// + /// 获取最后一个元素,如果集合为空则返回默认值 + /// + public static T LastOrValue(this IEnumerable source, T defaultValue) + { + if (source == null) + return defaultValue; + + var last = defaultValue; + var hasElement = false; + + foreach (var item in source) + { + last = item; + hasElement = true; + } + + return hasElement ? last : defaultValue; + } + + #endregion + + #region 集合合并 + + /// + /// 合并多个集合 + /// + public static IEnumerable Merge(params IEnumerable[] sources) + { + if (sources == null || sources.Length == 0) + yield break; + + foreach (var source in sources) + { + if (source != null) + { + foreach (var item in source) + { + yield return item; + } + } + } + } + #endregion } } diff --git a/EasyTool.Core/IOCategory/FileSystemExtension.cs b/EasyTool.Core/IOCategory/FileSystemExtension.cs new file mode 100644 index 0000000..2cd7865 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSystemExtension.cs @@ -0,0 +1,522 @@ +using System; +using System.IO; +using System.Linq; + +namespace EasyTool.Extension +{ + /// + /// 文件系统扩展方法 + /// + public static class FileSystemExtension + { + #region FileInfo 扩展 + + /// + /// 获取文件大小(格式化字符串) + /// + public static string GetSizeFormatted(this FileInfo file) + { + if (file == null || !file.Exists) + return "0 B"; + + return file.Length.ToFileSize(); + } + + /// + /// 获取相对路径 + /// + /// 源文件 + /// 参考路径 + public static string GetRelativePath(this FileInfo file, string relativeTo) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + return GetRelativePath(file.FullName, relativeTo); + } + + /// + /// 获取文件的 MIME 类型 + /// + public static string GetMimeType(this FileInfo file) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + return file.Extension.GetMimeType(); + } + + /// + /// 判断文件是否为图片 + /// + public static bool IsImage(this FileInfo file) + { + if (file == null) + return false; + + string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".ico", ".svg" }; + return imageExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为文档 + /// + public static bool IsDocument(this FileInfo file) + { + if (file == null) + return false; + + string[] docExtensions = { ".doc", ".docx", ".pdf", ".txt", ".rtf", ".odt", ".xls", ".xlsx", ".ppt", ".pptx" }; + return docExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为视频 + /// + public static bool IsVideo(this FileInfo file) + { + if (file == null) + return false; + + string[] videoExtensions = { ".mp4", ".avi", ".mov", ".wmv", ".flv", ".mkv", ".webm", ".m4v" }; + return videoExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为音频 + /// + public static bool IsAudio(this FileInfo file) + { + if (file == null) + return false; + + string[] audioExtensions = { ".mp3", ".wav", ".flac", ".aac", ".ogg", ".wma", ".m4a" }; + return audioExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否被锁定(正在使用) + /// + public static bool IsLocked(this FileInfo file) + { + if (file == null || !file.Exists) + return false; + + try + { + using (var stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + { + return false; + } + } + catch (IOException) + { + return true; + } + catch (UnauthorizedAccessException) + { + return true; + } + } + + /// + /// 安全删除文件(如果存在) + /// + public static bool DeleteIfExists(this FileInfo file) + { + if (file == null || !file.Exists) + return false; + + try + { + file.Delete(); + return true; + } + catch + { + return false; + } + } + + /// + /// 移动文件到指定目录 + /// + public static FileInfo MoveToDirectory(this FileInfo file, string targetDirectory) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + if (!Directory.Exists(targetDirectory)) + Directory.CreateDirectory(targetDirectory); + + string targetPath = Path.Combine(targetDirectory, file.Name); + file.MoveTo(targetPath); + return new FileInfo(targetPath); + } + + /// + /// 复制文件到指定目录 + /// + public static FileInfo CopyToDirectory(this FileInfo file, string targetDirectory, bool overwrite = false) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + if (!Directory.Exists(targetDirectory)) + Directory.CreateDirectory(targetDirectory); + + string targetPath = Path.Combine(targetDirectory, file.Name); + file.CopyTo(targetPath, overwrite); + return new FileInfo(targetPath); + } + + /// + /// 读取文件的所有文本内容 + /// [Obsolete("请直接使用 File.ReadAllText(file.FullName)")] + /// + [Obsolete("请直接使用 File.ReadAllText(file.FullName)", false)] + public static string ReadAllText(this FileInfo file) + { + if (file == null || !file.Exists) + return string.Empty; + + return File.ReadAllText(file.FullName); + } + + /// + /// 读取文件的所有字节 + /// [Obsolete("请直接使用 File.ReadAllBytes(file.FullName)")] + /// + [Obsolete("请直接使用 File.ReadAllBytes(file.FullName)", false)] + public static byte[] ReadAllBytes(this FileInfo file) + { + if (file == null || !file.Exists) + return Array.Empty(); + + return File.ReadAllBytes(file.FullName); + } + + /// + /// 写入文本内容到文件 + /// [Obsolete("请直接使用 File.WriteAllText(file.FullName, content)")] + /// + [Obsolete("请直接使用 File.WriteAllText(file.FullName, content)", false)] + public static void WriteAllText(this FileInfo file, string content) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + // 确保目录存在 + if (!file.Directory.Exists) + file.Directory.Create(); + + File.WriteAllText(file.FullName, content); + } + + /// + /// 写入字节到文件 + /// [Obsolete("请直接使用 File.WriteAllBytes(file.FullName, content)")] + /// + [Obsolete("请直接使用 File.WriteAllBytes(file.FullName, content)", false)] + public static void WriteAllBytes(this FileInfo file, byte[] content) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + // 确保目录存在 + if (!file.Directory.Exists) + file.Directory.Create(); + + File.WriteAllBytes(file.FullName, content); + } + + #endregion + + #region DirectoryInfo 扩展 + + /// + /// 获取目录的总大小(包含所有子目录) + /// + public static long GetTotalSize(this DirectoryInfo directory) + { + if (directory == null || !directory.Exists) + return 0; + + long size = 0; + + try + { + size += directory.GetFiles().Sum(f => f.Length); + size += directory.GetDirectories().Sum(d => GetTotalSize(d)); + } + catch (UnauthorizedAccessException) + { + // 忽略无权限访问的目录 + } + + return size; + } + + /// + /// 获取目录的总大小(格式化字符串) + /// + public static string GetTotalSizeFormatted(this DirectoryInfo directory) + { + return directory.GetTotalSize().ToFileSize(); + } + + /// + /// 获取目录中的所有文件(包含子目录) + /// + public static FileInfo[] GetAllFiles(this DirectoryInfo directory, string searchPattern = "*.*") + { + if (directory == null || !directory.Exists) + return Array.Empty(); + + try + { + var files = directory.GetFiles(searchPattern, SearchOption.AllDirectories); + return files; + } + catch (UnauthorizedAccessException) + { + return Array.Empty(); + } + } + + /// + /// 清空目录(删除所有文件和子目录) + /// + public static void Clear(this DirectoryInfo directory) + { + if (directory == null || !directory.Exists) + return; + + foreach (var file in directory.GetFiles()) + { + file.Delete(); + } + + foreach (var subDir in directory.GetDirectories()) + { + subDir.Delete(true); + } + } + + /// + /// 安全删除目录(如果存在) + /// + public static bool DeleteIfExists(this DirectoryInfo directory, bool recursive = false) + { + if (directory == null || !directory.Exists) + return false; + + try + { + directory.Delete(recursive); + return true; + } + catch + { + return false; + } + } + + /// + /// 确保目录存在,不存在则创建 + /// + public static DirectoryInfo EnsureExists(this DirectoryInfo directory) + { + if (directory == null) + throw new ArgumentNullException(nameof(directory)); + + if (!directory.Exists) + directory.Create(); + + return directory; + } + + /// + /// 复制目录到指定位置 + /// + public static DirectoryInfo CopyTo(this DirectoryInfo sourceDir, string targetPath) + { + if (sourceDir == null) + throw new ArgumentNullException(nameof(sourceDir)); + + var targetDir = Directory.CreateDirectory(targetPath); + + // 复制文件 + foreach (var file in sourceDir.GetFiles()) + { + string targetFilePath = Path.Combine(targetPath, file.Name); + file.CopyTo(targetFilePath, true); + } + + // 递归复制子目录 + foreach (var subDir in sourceDir.GetDirectories()) + { + string targetSubDirPath = Path.Combine(targetPath, subDir.Name); + subDir.CopyTo(targetSubDirPath); + } + + return targetDir; + } + + #endregion + + #region 路径扩展 + + /// + /// 获取相对路径 + /// + /// 绝对路径 + /// 参考路径 + public static string GetRelativePath(string absolutePath, string relativeTo) + { + if (string.IsNullOrEmpty(absolutePath)) + throw new ArgumentNullException(nameof(absolutePath)); + + if (string.IsNullOrEmpty(relativeTo)) + throw new ArgumentNullException(nameof(relativeTo)); + + absolutePath = Path.GetFullPath(absolutePath); + relativeTo = Path.GetFullPath(relativeTo); + + // 从 .NET Core 2.0 / .NET Standard 2.1 开始,可以使用 Path.GetRelativePath + // 这里提供一个兼容的实现 + var absolutePathParts = absolutePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var relativeToParts = relativeTo.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + int length = Math.Min(absolutePathParts.Length, relativeToParts.Length); + int lastCommonRoot = -1; + + for (int i = 0; i < length; i++) + { + if (string.Equals(absolutePathParts[i], relativeToParts[i], StringComparison.OrdinalIgnoreCase)) + { + lastCommonRoot = i; + } + else + { + break; + } + } + + if (lastCommonRoot == -1) + return absolutePath; + + var relativePath = new System.Text.StringBuilder(); + + // 添加 .. + for (int i = lastCommonRoot + 1; i < relativeToParts.Length; i++) + { + if (relativePath.Length > 0) + relativePath.Append(Path.DirectorySeparatorChar); + + relativePath.Append(".."); + } + + // 添加目标路径的剩余部分 + for (int i = lastCommonRoot + 1; i < absolutePathParts.Length; i++) + { + if (relativePath.Length > 0) + relativePath.Append(Path.DirectorySeparatorChar); + + relativePath.Append(absolutePathParts[i]); + } + + return relativePath.ToString(); + } + + /// + /// 确保路径以目录分隔符结尾 + /// + public static string EnsureEndsWithSeparator(this string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + char lastChar = path[path.Length - 1]; + if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar) + { + return path + Path.DirectorySeparatorChar; + } + + return path; + } + + #endregion + + #region 文件扩展名扩展 + + /// + /// 获取文件扩展名对应的 MIME 类型 + /// + public static string GetMimeType(this string extension) + { + if (string.IsNullOrEmpty(extension)) + return "application/octet-stream"; + + // 确保扩展名以 . 开头 + if (!extension.StartsWith(".")) + extension = "." + extension; + + return extension.ToLowerInvariant() switch + { + // 文本 + ".html" => "text/html", + ".htm" => "text/html", + ".css" => "text/css", + ".js" => "application/javascript", + ".json" => "application/json", + ".xml" => "application/xml", + ".txt" => "text/plain", + + // 图片 + ".jpg" => "image/jpeg", + ".jpeg" => "image/jpeg", + ".png" => "image/png", + ".gif" => "image/gif", + ".bmp" => "image/bmp", + ".webp" => "image/webp", + ".ico" => "image/x-icon", + ".svg" => "image/svg+xml", + + // 视频 + ".mp4" => "video/mp4", + ".avi" => "video/x-msvideo", + ".mov" => "video/quicktime", + ".wmv" => "video/x-ms-wmv", + ".flv" => "video/x-flv", + ".mkv" => "video/x-matroska", + ".webm" => "video/webm", + + // 音频 + ".mp3" => "audio/mpeg", + ".wav" => "audio/wav", + ".flac" => "audio/flac", + ".aac" => "audio/aac", + ".ogg" => "audio/ogg", + ".wma" => "audio/x-ms-wma", + ".m4a" => "audio/mp4", + + // 文档 + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".ppt" => "application/vnd.ms-powerpoint", + ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".zip" => "application/zip", + ".rar" => "application/x-rar-compressed", + ".7z" => "application/x-7z-compressed", + + _ => "application/octet-stream" + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/FileTypeUtil.cs b/EasyTool.Core/IOCategory/FileTypeUtil.cs index 5cb71cb..09b077b 100644 --- a/EasyTool.Core/IOCategory/FileTypeUtil.cs +++ b/EasyTool.Core/IOCategory/FileTypeUtil.cs @@ -38,7 +38,7 @@ public static string GetType(FileInfo file) header += buffer[i].ToString(); } - string type = null; + string? type = null; switch (header) { case "255216": // jpg diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index 772ca51..857d294 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -304,9 +304,11 @@ public static FileInfo Touch(string path) /// /// 拷贝文件 + /// [Obsolete("请直接使用 File.Copy(src, dest)")] /// /// 源文件路径 /// 目标文件路径 + [Obsolete("请直接使用 File.Copy(src, dest)", false)] public static void Cp(string src, string dest) { try @@ -398,9 +400,11 @@ public static bool Copy(string src, string dest, bool isOverride) /// /// 移动文件或重命名文件 + /// [Obsolete("请直接使用 File.Move(src, dest)")] /// /// 源文件路径 /// 目标文件路径 + [Obsolete("请直接使用 File.Move(src, dest)", false)] public static void Mv(string src, string dest) { try @@ -537,9 +541,11 @@ public static FileInfo Rename(FileInfo file, string newName, bool isRetainExt, b /// /// 获取绝对路径 + /// [Obsolete("请直接使用 Path.GetFullPath(path)")] /// /// 相对路径 /// 绝对路径 + [Obsolete("请直接使用 Path.GetFullPath(path)", false)] public static string GetAbsolutePath(string path) { if (!Path.IsPathRooted(path)) @@ -766,8 +772,10 @@ public static string SubPath(string dirPath, string filePath) /// /// 删除文件 + /// [Obsolete("请直接使用 File.Delete(path)")] /// /// 文件路径 + [Obsolete("请直接使用 File.Delete(path)", false)] public static void Rm(string path) { try @@ -782,8 +790,10 @@ public static void Rm(string path) /// /// 创建目录 + /// [Obsolete("请直接使用 Directory.CreateDirectory(path)")] /// /// 目录路径 + [Obsolete("请直接使用 Directory.CreateDirectory(path)", false)] public static void Mkdir(string path) { try @@ -798,8 +808,10 @@ public static void Mkdir(string path) /// /// 删除目录 + /// [Obsolete("请直接使用 Directory.Delete(path)")] /// /// 目录路径 + [Obsolete("请直接使用 Directory.Delete(path)", false)] public static void Rmdir(string path) { try @@ -815,9 +827,11 @@ public static void Rmdir(string path) /// /// 获取文件名 + /// [Obsolete("请直接使用 file?.Name")] /// /// 文件 /// 文件名 + [Obsolete("请直接使用 file?.Name", false)] public static string GetFileName(FileInfo file) { if (file == null) @@ -1146,10 +1160,12 @@ public static string ReadString(Uri url, Encoding? encoding = null) /// /// 从文件中读取每一行数据 + /// [Obsolete("请直接使用 File.ReadAllLines(path, encoding)")] /// /// 文件路径 /// 编码格式,默认为UTF-8 /// + [Obsolete("请直接使用 File.ReadAllLines(path, encoding)", false)] public static string[] ReadAllLines(string path, Encoding? encoding = null) { // 如果未指定编码格式,则默认为 UTF-8 @@ -1194,8 +1210,10 @@ public static Stream GetOutputStream(string path) /// /// 获取当前系统的换行分隔符 + /// [Obsolete("请直接使用 Environment.NewLine")] /// /// 换行分隔符 + [Obsolete("请直接使用 Environment.NewLine", false)] public static string GetLineSeparator() { return Environment.NewLine; @@ -1231,7 +1249,7 @@ public static FileInfo WriteString(string content, string path, Encoding? encodi /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo AppendString(string content, string path, Encoding encoding = null) + public static FileInfo AppendString(string content, string path, Encoding? encoding = null) { if (encoding == null) { @@ -1251,7 +1269,7 @@ public static FileInfo AppendString(string content, string path, Encoding encodi /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo WriteLines(List list, string path, Encoding encoding = null) + public static FileInfo WriteLines(List list, string path, Encoding? encoding = null) { if (encoding == null) { diff --git a/EasyTool.Core/IOCategory/IoUtil.cs b/EasyTool.Core/IOCategory/IoUtil.cs index 96cc1b9..d2a647f 100644 --- a/EasyTool.Core/IOCategory/IoUtil.cs +++ b/EasyTool.Core/IOCategory/IoUtil.cs @@ -13,9 +13,11 @@ public static class IoUtil { /// /// 读取文件的所有行到一个字符串数组中 + /// [Obsolete("请直接使用 File.ReadAllLines(path)")] /// /// 文件路径 /// 字符串数组,其中包含文件的所有行。 + [Obsolete("请直接使用 File.ReadAllLines(path)", false)] public static string[] ReadAllLines(string path) { return File.ReadAllLines(path); @@ -23,9 +25,11 @@ public static string[] ReadAllLines(string path) /// /// 将字符串数组写入文件,覆盖原有内容 + /// [Obsolete("请直接使用 File.WriteAllLines(path, lines)")] /// /// 文件路径 /// 待写入的字符串数组 + [Obsolete("请直接使用 File.WriteAllLines(path, lines)", false)] public static void WriteAllLines(string path, string[] lines) { File.WriteAllLines(path, lines); @@ -33,9 +37,11 @@ public static void WriteAllLines(string path, string[] lines) /// /// 读取整个文件到一个字符串中 + /// [Obsolete("请直接使用 File.ReadAllText(path)")] /// /// 文件路径 /// 文件的所有内容 + [Obsolete("请直接使用 File.ReadAllText(path)", false)] public static string ReadAllText(string path) { return File.ReadAllText(path); @@ -43,9 +49,11 @@ public static string ReadAllText(string path) /// /// 将字符串写入文件,覆盖原有内容 + /// [Obsolete("请直接使用 File.WriteAllText(path, text)")] /// /// 文件路径 /// 待写入的字符串 + [Obsolete("请直接使用 File.WriteAllText(path, text)", false)] public static void WriteAllText(string path, string text) { File.WriteAllText(path, text); @@ -53,9 +61,11 @@ public static void WriteAllText(string path, string text) /// /// 读取二进制数据到一个字节数组中 + /// [Obsolete("请直接使用 File.ReadAllBytes(path)")] /// /// 文件路径 /// + [Obsolete("请直接使用 File.ReadAllBytes(path)", false)] public static byte[] ReadAllBytes(string path) { return File.ReadAllBytes(path); @@ -63,9 +73,11 @@ public static byte[] ReadAllBytes(string path) /// /// 将字节数组写入二进制文件,覆盖原有内容 + /// [Obsolete("请直接使用 File.WriteAllBytes(path, bytes)")] /// /// 文件路径 /// 待写入的字节数组 + [Obsolete("请直接使用 File.WriteAllBytes(path, bytes)", false)] public static void WriteAllBytes(string path, byte[] bytes) { File.WriteAllBytes(path, bytes); @@ -141,9 +153,11 @@ public static void WriteMemoryStream(MemoryStream stream, byte[] bytes) /// /// 将一个字符串转换为字节数组 + /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(text)")] /// /// 待转换的字符串 /// 字节数组,其中包含输入字符串的编码数据 + [Obsolete("请直接使用 Encoding.UTF8.GetBytes(text)", false)] public static byte[] StringToBytes(string text) { return Encoding.UTF8.GetBytes(text); @@ -151,9 +165,11 @@ public static byte[] StringToBytes(string text) /// /// 将一个字节数组转换为字符串 + /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] /// /// 待转换的字节数组 /// 字符串,其中包含输入字节数组的编码数据 + [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] public static string BytesToString(byte[] bytes) { return Encoding.UTF8.GetString(bytes); diff --git a/EasyTool.Core/IOCategory/StreamExtension.cs b/EasyTool.Core/IOCategory/StreamExtension.cs new file mode 100644 index 0000000..a8c729b --- /dev/null +++ b/EasyTool.Core/IOCategory/StreamExtension.cs @@ -0,0 +1,365 @@ +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.Extension +{ + /// + /// Stream 扩展方法 + /// + public static class StreamExtension + { + #region 读取操作 + + /// + /// 读取流中的所有字节 + /// + public static byte[] ReadAllBytes(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + using var ms = new MemoryStream(); + stream.CopyTo(ms); + return ms.ToArray(); + } + + /// + /// 异步读取流中的所有字节 + /// + public static async Task ReadAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + using var ms = new MemoryStream(); + await stream.CopyToAsync(ms, cancellationToken); + return ms.ToArray(); + } + + /// + /// 读取流中的所有文本(UTF-8 编码) + /// + public static string ReadAllText(this Stream stream) + { + return stream.ReadAllText(Encoding.UTF8); + } + + /// + /// 读取流中的所有文本 + /// + public static string ReadAllText(this Stream stream, Encoding encoding) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + return reader.ReadToEnd(); + } + + /// + /// 异步读取流中的所有文本(UTF-8 编码) + /// + public static Task ReadAllTextAsync(this Stream stream, CancellationToken cancellationToken = default) + { + return stream.ReadAllTextAsync(Encoding.UTF8, cancellationToken); + } + + /// + /// 异步读取流中的所有文本 + /// + public static async Task ReadAllTextAsync(this Stream stream, Encoding encoding, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + return await reader.ReadToEndAsync(); + } + + /// + /// 读取流中的所有行 + /// + public static string[] ReadAllLines(this Stream stream, Encoding? encoding = null) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + var lines = new System.Collections.Generic.List(); + string? line; + while ((line = reader.ReadLine()) != null) + { + lines.Add(line); + } + return lines.ToArray(); + } + + #endregion + + #region 写入操作 + + /// + /// 将字节写入流 + /// + public static void WriteBytes(this Stream stream, byte[] bytes) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (bytes == null || bytes.Length == 0) + return; + + stream.Write(bytes, 0, bytes.Length); + } + + /// + /// 异步将字节写入流 + /// + public static async Task WriteBytesAsync(this Stream stream, byte[] bytes, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (bytes == null || bytes.Length == 0) + return; + + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + } + + /// + /// 将文本写入流(UTF-8 编码) + /// + public static void WriteText(this Stream stream, string text) + { + stream.WriteText(text, Encoding.UTF8); + } + + /// + /// 将文本写入流 + /// + public static void WriteText(this Stream stream, string text, Encoding encoding) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (string.IsNullOrEmpty(text)) + return; + + encoding ??= Encoding.UTF8; + + var bytes = encoding.GetBytes(text); + stream.Write(bytes, 0, bytes.Length); + } + + /// + /// 异步将文本写入流(UTF-8 编码) + /// + public static Task WriteTextAsync(this Stream stream, string text, CancellationToken cancellationToken = default) + { + return stream.WriteTextAsync(text, Encoding.UTF8, cancellationToken); + } + + /// + /// 异步将文本写入流 + /// + public static async Task WriteTextAsync(this Stream stream, string text, Encoding encoding, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (string.IsNullOrEmpty(text)) + return; + + encoding ??= Encoding.UTF8; + + var bytes = encoding.GetBytes(text); + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + } + + #endregion + + #region 复制操作 + + /// + /// 将流复制到另一个流 + /// [Obsolete("请直接使用 source.CopyTo(destination)")] + /// + [Obsolete("请直接使用 source.CopyTo(destination)", false)] + public static void CopyTo(this Stream source, Stream destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + + source.CopyTo(destination, 81920); + } + + /// + /// 将流复制到另一个流(指定缓冲区大小) + /// [Obsolete("请直接使用 source.CopyTo(destination)")] + /// + [Obsolete("请直接使用 source.CopyTo(destination)", false)] + public static void CopyTo(this Stream source, Stream destination, int bufferSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + + if (!source.CanRead) + throw new InvalidOperationException("Source stream does not support reading."); + if (!destination.CanWrite) + throw new InvalidOperationException("Destination stream does not support writing."); + + var buffer = new byte[bufferSize]; + int bytesRead; + while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) + { + destination.Write(buffer, 0, bytesRead); + } + } + + /// + /// 将流复制到字节数组 + /// + public static byte[] CopyToByteArray(this Stream stream) + { + return stream.ReadAllBytes(); + } + + /// + /// 将流复制到内存流 + /// + public static MemoryStream CopyToMemoryStream(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var ms = new MemoryStream(); + stream.CopyTo(ms); + return ms; + } + + #endregion + + #region 位置操作 + + /// + /// 将流位置重置到开头 + /// [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.Begin)")] + /// + [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.Begin)", false)] + public static void ResetPosition(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (stream.CanSeek) + { + stream.Seek(0, SeekOrigin.Begin); + } + } + + /// + /// 将流位置重置到末尾 + /// [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.End)")] + /// + [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.End)", false)] + public static void SeekToEnd(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (stream.CanSeek) + { + stream.Seek(0, SeekOrigin.End); + } + } + + #endregion + + #region 转换操作 + + /// + /// 将流转为 Base64 字符串 + /// + public static string ToBase64(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var bytes = stream.ReadAllBytes(); + return Convert.ToBase64String(bytes); + } + + /// + /// 将流转为十六进制字符串 + /// + public static string ToHex(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var bytes = stream.ReadAllBytes(); + return BitConverter.ToString(bytes).Replace("-", "").ToLowerInvariant(); + } + + #endregion + + #region 检查操作 + + /// + /// 判断流是否为空 + /// + public static bool IsEmpty(this Stream? stream) + { + if (stream == null) + return true; + + if (stream.CanSeek) + { + return stream.Length == 0; + } + + return stream.ReadByte() == -1; + } + + #endregion + + #region 缓冲操作 + + /// + /// 使用缓冲读取器包装流 + /// + public static BufferedStream Buffer(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + return new BufferedStream(stream); + } + + /// + /// 使用缓冲读取器包装流(指定缓冲区大小) + /// + public static BufferedStream Buffer(this Stream stream, int bufferSize) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + return new BufferedStream(stream, bufferSize); + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/Tailer.cs b/EasyTool.Core/IOCategory/Tailer.cs index b8df260..1a6d6f8 100644 --- a/EasyTool.Core/IOCategory/Tailer.cs +++ b/EasyTool.Core/IOCategory/Tailer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -16,7 +16,7 @@ public class Tailer : IDisposable private readonly Timer timer; // 定时器,用于定期检查文件是否有新内容 // 定义事件,用于通知外部监听器 - public event EventHandler NewLine; + public event EventHandler? NewLine; // 构造函数,初始化文件路径、StreamReader 和定时器 public Tailer(string filePath) @@ -48,7 +48,7 @@ private void OnTimerCallback(object state) return; // 逐行读取文件内容 - string line; + string? line; while ((line = reader.ReadLine()) != null) { // 如果有新行,触发 NewLine 事件 diff --git a/EasyTool.Core/IOCategory/WatchMonitor.cs b/EasyTool.Core/IOCategory/WatchMonitor.cs index d9a6634..226e44c 100644 --- a/EasyTool.Core/IOCategory/WatchMonitor.cs +++ b/EasyTool.Core/IOCategory/WatchMonitor.cs @@ -13,11 +13,11 @@ public class WatchMonitor private readonly FileSystemWatcher watcher; // 定义事件,用于通知外部监听器 - public event EventHandler FileChanged; - public event EventHandler FileCreated; - public event EventHandler FileDeleted; - public event EventHandler FileMissing; - public event EventHandler FileError; + public event EventHandler? FileChanged; + public event EventHandler? FileCreated; + public event EventHandler? FileDeleted; + public event EventHandler? FileMissing; + public event EventHandler? FileError; /// /// 构造函数,初始化 FileSystemWatcher 实例 @@ -109,7 +109,7 @@ private void OnFileError(object sender, ErrorEventArgs e) { if (FileError != null) { - FileError(this, new FileEventArgs(e.GetException())); + FileError(this, new FileEventArgs(e.GetException()!)); } } diff --git a/EasyTool.Core/LanguageCategory/TreeUtil.cs b/EasyTool.Core/LanguageCategory/TreeUtil.cs index f437583..7003b9b 100644 --- a/EasyTool.Core/LanguageCategory/TreeUtil.cs +++ b/EasyTool.Core/LanguageCategory/TreeUtil.cs @@ -25,10 +25,10 @@ public TreeUtil(List> nodes) /// 构建树结构 /// /// 根节点 - public TreeNode BuildTree() + public TreeNode? BuildTree() { // 获取根节点 - var root = _nodes.FirstOrDefault(n => n.ParentId.Equals(default(T))); + var root = _nodes.FirstOrDefault(n => n.ParentId == null || n.ParentId.Equals(default(T))); if (root == null) { @@ -43,7 +43,7 @@ public TreeNode BuildTree() private void BuildTree(TreeNode node) { - node.Children = _nodes.Where(n => n.ParentId.Equals(node.Id)).ToList(); + node.Children = _nodes.Where(n => n.ParentId != null && n.ParentId.Equals(node.Id)).ToList(); if (node.Children.Count > 0) { @@ -105,9 +105,9 @@ private void GetDescendants(TreeNode node, List> descendant } } - private TreeNode GetParent(T id) + private TreeNode? GetParent(T id) { - return _nodes.FirstOrDefault(n => n.Id.Equals(id)); + return _nodes.FirstOrDefault(n => n.Id != null && n.Id.Equals(id)); } /// @@ -122,7 +122,7 @@ public List> GetSiblings(TreeNode node) { return new List>(); } - return parent.Children.Where(n => !n.Id.Equals(node.Id)).ToList(); + return parent.Children.Where(n => n.Id == null || !n.Id.Equals(node.Id)).ToList(); } /// @@ -171,7 +171,7 @@ public int GetMinDepth() public TreeNode? GetNextSibling(TreeNode node) { var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id!.Equals(node.Id)); + var index = siblings.FindIndex(n => n.Id != null && n.Id.Equals(node.Id)); return index + 1 < siblings.Count ? siblings[index + 1] : null; } @@ -183,7 +183,7 @@ public int GetMinDepth() public TreeNode? GetPreviousSibling(TreeNode node) { var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id!.Equals(node.Id)); + var index = siblings.FindIndex(n => n.Id != null && n.Id.Equals(node.Id)); return index - 1 >= 0 ? siblings[index - 1] : null; } @@ -283,13 +283,13 @@ public int GetMinWeight() public class TreeNode { public T Id { get; set; } - public T ParentId { get; set; } - public string Name { get; set; } + public T? ParentId { get; set; } + public string Name { get; set; } = string.Empty; public int Weight { get; set; } - public D Data { get; set; } - public List> Children { get; set; } + public D Data { get; set; } = default!; + public List> Children { get; set; } = new List>(); - public TreeNode(T id, T parentId, string name, int weight, D data) + public TreeNode(T id, T? parentId, string name, int weight, D data) { this.Id = id; this.ParentId = parentId; diff --git a/EasyTool.Core/MathCategory/MathUtil.cs b/EasyTool.Core/MathCategory/MathUtil.cs index df37079..76010ec 100644 --- a/EasyTool.Core/MathCategory/MathUtil.cs +++ b/EasyTool.Core/MathCategory/MathUtil.cs @@ -124,10 +124,12 @@ public static int CountBits(int n) /// /// 求两个浮点数的平均值 + /// [Obsolete("请直接使用 (a + b) / 2")] /// /// 第一个浮点数 /// 第二个浮点数 /// 两个浮点数的平均值 + [Obsolete("请直接使用 (a + b) / 2", false)] public static double Average(double a, double b) { return (a + b) / 2; diff --git a/EasyTool.Core/MathCategory/NumberUtil.cs b/EasyTool.Core/MathCategory/NumberUtil.cs index a55a70f..ad84f16 100644 --- a/EasyTool.Core/MathCategory/NumberUtil.cs +++ b/EasyTool.Core/MathCategory/NumberUtil.cs @@ -123,10 +123,12 @@ public static decimal Div(double a, double b, int decimalPlaces) /// /// 格式化一个 decimal 数字 + /// [Obsolete("请直接使用 number.ToString(format)")] /// /// 待格式化的数字 /// 格式化字符串 /// 格式化后的字符串 + [Obsolete("请直接使用 number.ToString(format)", false)] public static string DecimalFormat(decimal number, string format) { return number.ToString(format); diff --git a/EasyTool.Core/NetCategory/HttpClientExtension.cs b/EasyTool.Core/NetCategory/HttpClientExtension.cs index 452804b..2c4654e 100644 --- a/EasyTool.Core/NetCategory/HttpClientExtension.cs +++ b/EasyTool.Core/NetCategory/HttpClientExtension.cs @@ -13,7 +13,7 @@ namespace EasyTool.Extension public static class HttpClientExtension { private const string NetErrorMessage = "网络异常"; - + #region 标准的Http请求扩展 /// diff --git a/EasyTool.Core/ToolCategory/ArrayUtil.cs b/EasyTool.Core/ToolCategory/ArrayUtil.cs index ca24653..fdf234c 100644 --- a/EasyTool.Core/ToolCategory/ArrayUtil.cs +++ b/EasyTool.Core/ToolCategory/ArrayUtil.cs @@ -21,9 +21,11 @@ public static bool IsEmpty(Array array) /// /// 获取数组的长度 + /// [Obsolete("请直接使用 array?.Length ?? 0")] /// /// 要获取长度的数组 /// 返回数组的长度 + [Obsolete("请直接使用 array?.Length ?? 0", false)] public static int Length(Array array) { if (array == null) @@ -36,9 +38,11 @@ public static int Length(Array array) /// /// 获取数组中的最大值 + /// [Obsolete("请直接使用 array.Max() (LINQ)")] /// /// 要获取最大值的数组 /// 返回数组中的最大值 + [Obsolete("请直接使用 array.Max() (LINQ)", false)] public static T Max(T[] array) where T : IComparable { if (IsEmpty(array)) @@ -60,9 +64,11 @@ public static T Max(T[] array) where T : IComparable /// /// 获取数组中的最小值 + /// [Obsolete("请直接使用 array.Min() (LINQ)")] /// /// 要获取最小值的数组 /// 返回数组中的最小值 + [Obsolete("请直接使用 array.Min() (LINQ)", false)] public static T Min(T[] array) where T : IComparable { if (IsEmpty(array)) @@ -83,9 +89,11 @@ public static T Min(T[] array) where T : IComparable /// /// 获取数组中的和 + /// [Obsolete("请直接使用 array.Sum() (LINQ)")] /// /// 要获取和的数组 /// 返回数组的和 + [Obsolete("请直接使用 array.Sum() (LINQ)", false)] public static int Sum(int[] array) { if (IsEmpty(array)) @@ -104,9 +112,11 @@ public static int Sum(int[] array) /// /// 获取数组的平均值 + /// [Obsolete("请直接使用 array.Average() (LINQ)")] /// /// 要获取平均值的数组 /// 返回数组的平均值 + [Obsolete("请直接使用 array.Average() (LINQ)", false)] public static double Average(int[] array) { if (IsEmpty(array)) @@ -160,10 +170,12 @@ public static T[] Reverse(T[] array) /// /// 判断数组是否包含某个元素 + /// [Obsolete("请直接使用 array.Contains(item) (LINQ)")] /// /// 要操作的数组 /// 要判断的元素 /// 如果数组中包含该元素,则返回 true;否则返回 false + [Obsolete("请直接使用 array.Contains(item) (LINQ)", false)] public static bool Contains(T[] array, T item) { if (IsEmpty(array)) diff --git a/EasyTool.Core/ToolCategory/ClassUtil.cs b/EasyTool.Core/ToolCategory/ClassUtil.cs index b3ad3d4..5ee1647 100644 --- a/EasyTool.Core/ToolCategory/ClassUtil.cs +++ b/EasyTool.Core/ToolCategory/ClassUtil.cs @@ -12,9 +12,11 @@ public class ClassUtil { /// /// 获取类的完全限定名 + /// [Obsolete("请直接使用 type.FullName")] /// /// 要获取名称的类 /// 类的完全限定名 + [Obsolete("请直接使用 type.FullName", false)] public static string GetClassName(Type type) { return type.FullName; @@ -22,9 +24,11 @@ public static string GetClassName(Type type) /// /// 获取类的命名空间 + /// [Obsolete("请直接使用 type.Namespace")] /// /// 要获取命名空间的类 /// 类的命名空间 + [Obsolete("请直接使用 type.Namespace", false)] public static string GetClassNamespace(Type type) { return type.Namespace; @@ -50,9 +54,11 @@ public static Type[] GetClassHierarchy(Type type) /// /// 获取类的所有方法 + /// [Obsolete("请直接使用 type.GetMethods()")] /// /// 要获取方法的类 /// 类的所有方法 + [Obsolete("请直接使用 type.GetMethods()", false)] public static MethodInfo[] GetClassMethods(Type type) { return type.GetMethods(); @@ -60,9 +66,11 @@ public static MethodInfo[] GetClassMethods(Type type) /// /// 获取类的所有属性 + /// [Obsolete("请直接使用 type.GetProperties()")] /// /// 要获取属性的类 /// 类的所有属性 + [Obsolete("请直接使用 type.GetProperties()", false)] public static PropertyInfo[] GetClassProperties(Type type) { return type.GetProperties(); @@ -70,9 +78,11 @@ public static PropertyInfo[] GetClassProperties(Type type) /// /// 获取类的所有字段 + /// [Obsolete("请直接使用 type.GetFields()")] /// /// 要获取字段的类 /// 类的所有字段 + [Obsolete("请直接使用 type.GetFields()", false)] public static FieldInfo[] GetClassFields(Type type) { return type.GetFields(); @@ -80,9 +90,11 @@ public static FieldInfo[] GetClassFields(Type type) /// /// 获取类的所有事件 + /// [Obsolete("请直接使用 type.GetEvents()")] /// /// 要获取事件的类 /// 类的所有事件 + [Obsolete("请直接使用 type.GetEvents()", false)] public static EventInfo[] GetClassEvents(Type type) { return type.GetEvents(); @@ -90,9 +102,11 @@ public static EventInfo[] GetClassEvents(Type type) /// /// 获取类的所有构造函数 + /// [Obsolete("请直接使用 type.GetConstructors()")] /// /// 要获取构造函数的类 /// 类的所有构造函数 + [Obsolete("请直接使用 type.GetConstructors()", false)] public static ConstructorInfo[] GetClassConstructors(Type type) { return type.GetConstructors(); @@ -100,9 +114,11 @@ public static ConstructorInfo[] GetClassConstructors(Type type) /// /// 获取类的默认构造函数 + /// [Obsolete("请直接使用 type.GetConstructor(Type.EmptyTypes)")] /// /// 要获取默认构造函数的类 /// 类的默认构造函数 + [Obsolete("请直接使用 type.GetConstructor(Type.EmptyTypes)", false)] public static ConstructorInfo GetDefaultClassConstructor(Type type) { return type.GetConstructor(Type.EmptyTypes); diff --git a/EasyTool.Core/ToolCategory/ColorExtension.cs b/EasyTool.Core/ToolCategory/ColorExtension.cs new file mode 100644 index 0000000..0298c0a --- /dev/null +++ b/EasyTool.Core/ToolCategory/ColorExtension.cs @@ -0,0 +1,348 @@ +using System; +using System.Drawing; + +namespace EasyTool.Extension +{ + /// + /// Color 颜色扩展方法 + /// + public static class ColorExtension + { + #region 转换方法 + + /// + /// 将颜色转换为16进制字符串 + /// + public static string ToHex(this Color color) + { + return color.ToHex(false); + } + + /// + /// 将颜色转换为16进制字符串 + /// + public static string ToHex(this Color color, bool includeAlpha) + { + if (includeAlpha) + return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}"; + else + return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; + } + + /// + /// 从16进制字符串创建颜色 + /// + public static Color FromHex(string hex) + { + if (string.IsNullOrEmpty(hex)) + return Color.Empty; + + hex = hex.TrimStart('#'); + + if (hex.Length == 6) + { + int r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); + int g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); + int b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); + return Color.FromArgb(r, g, b); + } + else if (hex.Length == 8) + { + int a = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); + int r = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); + int g = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); + int b = int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); + return Color.FromArgb(a, r, g, b); + } + + throw new ArgumentException("Invalid hex color format", nameof(hex)); + } + + /// + /// 将颜色转换为 RGB 字符串 + /// + public static string ToRgbString(this Color color) + { + return $"rgb({color.R}, {color.G}, {color.B})"; + } + + /// + /// 将颜色转换为 RGBA 字符串 + /// + public static string ToRgbaString(this Color color) + { + return $"rgba({color.R}, {color.G}, {color.B}, {color.A / 255f:F2})"; + } + + /// + /// 将颜色转换为 HSL + /// + public static (double h, double s, double l) ToHsl(this Color color) + { + double r = color.R / 255d; + double g = color.G / 255d; + double b = color.B / 255d; + + double max = Math.Max(r, Math.Max(g, b)); + double min = Math.Min(r, Math.Min(g, b)); + double h = 0, s = 0, l = (max + min) / 2d; + + if (max != min) + { + double d = max - min; + s = l > 0.5d ? d / (2d - max - min) : d / (max + min); + + if (max == r) + h = (g - b) / d + (g < b ? 6d : 0d); + else if (max == g) + h = (b - r) / d + 2d; + else + h = (r - g) / d + 4d; + + h /= 6d; + } + + return (h * 360d, s * 100d, l * 100d); + } + + /// + /// 从 HSL 创建颜色 + /// + public static Color FromHsl(double h, double s, double l) + { + h = h / 360d; + s = s / 100d; + l = l / 100d; + + double r, g, b; + + if (s == 0) + { + r = g = b = l; + } + else + { + double q = l < 0.5d ? l * (1d + s) : l + s - l * s; + double p = 2d * l - q; + + r = Hue2Rgb(p, q, h + 1d / 3d); + g = Hue2Rgb(p, q, h); + b = Hue2Rgb(p, q, h - 1d / 3d); + } + + return Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255)); + } + + private static double Hue2Rgb(double p, double q, double t) + { + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1d / 6d) return p + (q - p) * 6d * t; + if (t < 1d / 2d) return q; + if (t < 2d / 3d) return p + (q - p) * (2d / 3d - t) * 6d; + return p; + } + + #endregion + + #region 颜色调整 + + /// + /// 变亮颜色 + /// + public static Color Lighten(this Color color, double percent) + { + var (h, s, l) = color.ToHsl(); + l = Math.Min(100, l + percent); + return FromHsl(h, s, l); + } + + /// + /// 变暗颜色 + /// + public static Color Darken(this Color color, double percent) + { + var (h, s, l) = color.ToHsl(); + l = Math.Max(0, l - percent); + return FromHsl(h, s, l); + } + + /// + /// 调整颜色透明度 + /// + public static Color WithAlpha(this Color color, int alpha) + { + return Color.FromArgb(alpha, color.R, color.G, color.B); + } + + /// + /// 调整颜色透明度 + /// + public static Color WithAlpha(this Color color, double alphaPercent) + { + int alpha = (int)(alphaPercent * 255); + return Color.FromArgb(alpha, color.R, color.G, color.B); + } + + /// + /// 反转颜色 + /// + public static Color Invert(this Color color) + { + return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B); + } + + /// + /// 灰度化颜色 + /// + public static Color Grayscale(this Color color) + { + int gray = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114); + return Color.FromArgb(color.A, gray, gray, gray); + } + + /// + /// 混合两种颜色 + /// + public static Color Blend(this Color color1, Color color2, double percent) + { + int r = (int)(color1.R + (color2.R - color1.R) * percent); + int g = (int)(color1.G + (color2.G - color1.G) * percent); + int b = (int)(color1.B + (color2.B - color1.B) * percent); + int a = (int)(color1.A + (color2.A - color1.A) * percent); + return Color.FromArgb(a, r, g, b); + } + + /// + /// 获取互补色 + /// + public static Color Complementary(this Color color) + { + var (h, s, l) = color.ToHsl(); + h = (h + 180) % 360; + return FromHsl(h, s, l); + } + + /// + /// 获取类比色 + /// + public static Color[] Analogous(this Color color, int count = 3) + { + var (h, s, l) = color.ToHsl(); + var colors = new Color[count]; + + for (int i = 0; i < count; i++) + { + double hue = (h + (i * 30)) % 360; + colors[i] = FromHsl(hue, s, l); + } + + return colors; + } + + /// + /// 获取三色组合 + /// + public static Color[] Triadic(this Color color) + { + var (h, s, l) = color.ToHsl(); + return new[] + { + FromHsl(h, s, l), + FromHsl((h + 120) % 360, s, l), + FromHsl((h + 240) % 360, s, l) + }; + } + + #endregion + + #region 颜色判断 + + /// + /// 判断是否是深色 + /// + public static bool IsDark(this Color color) + { + // 使用亮度公式判断 + double brightness = (color.R * 299 + color.G * 587 + color.B * 114) / 1000d; + return brightness < 128; + } + + /// + /// 判断是否是浅色 + /// + public static bool IsLight(this Color color) + { + return !color.IsDark(); + } + + #endregion + + #region 命名颜色 + + /// + /// 从名称创建颜色 + /// [Obsolete("请直接使用 Color.FromName(name)")] + /// + [Obsolete("请直接使用 Color.FromName(name)", false)] + public static Color FromName(string name) + { + if (string.IsNullOrEmpty(name)) + return Color.Empty; + + return Color.FromName(name); + } + + /// + /// 获取颜色名称 + /// + public static string GetColorName(this Color color) + { + if (color.IsNamedColor) + return color.Name; + + return color.ToHex(); + } + + #endregion + + #region 颜色对比 + + /// + /// 计算两种颜色的对比度 + /// + public static double ContrastWith(this Color color1, Color color2) + { + double GetLuminance(Color c) + { + double r = c.R / 255d; + double g = c.G / 255d; + double b = c.B / 255d; + + r = r <= 0.03928 ? r / 12.92 : Math.Pow((r + 0.055) / 1.055, 2.4); + g = g <= 0.03928 ? g / 12.92 : Math.Pow((g + 0.055) / 1.055, 2.4); + b = b <= 0.03928 ? b / 12.92 : Math.Pow((b + 0.055) / 1.055, 2.4); + + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + + double l1 = GetLuminance(color1); + double l2 = GetLuminance(color2); + + double lighter = Math.Max(l1, l2); + double darker = Math.Min(l1, l2); + + return (lighter + 0.05) / (darker + 0.05); + } + + /// + /// 根据背景色选择合适的文本颜色(黑色或白色) + /// + public static Color GetReadableTextColor(this Color backgroundColor) + { + return backgroundColor.IsDark() ? Color.White : Color.Black; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs b/EasyTool.Core/ToolCategory/CreditCodeUtil.cs index fc4704a..25b9e44 100644 --- a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs +++ b/EasyTool.Core/ToolCategory/CreditCodeUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; @@ -89,20 +89,21 @@ public static string GenerateRandomCreditCode() /// /// 社会信用代码 /// 组织机构代码 - public static string GetOrgCodeFromCreditCode(string creditCode) + public static string? GetOrgCodeFromCreditCode(string? creditCode) { if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) { return null; } - return creditCode.Substring(0, 9); - } + return creditCode.Substring(0, 9); + } + /// /// 从社会信用代码中提取企业类型 /// /// 社会信用代码 /// 企业类型 - public static string GetEntTypeFromCreditCode(string creditCode) + public static string? GetEntTypeFromCreditCode(string? creditCode) { if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) { @@ -117,7 +118,7 @@ public static string GetEntTypeFromCreditCode(string creditCode) /// /// 社会信用代码 /// 注册号 - public static string GetRegNumFromCreditCode(string creditCode) + public static string? GetRegNumFromCreditCode(string? creditCode) { if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) { @@ -132,7 +133,7 @@ public static string GetRegNumFromCreditCode(string creditCode) /// /// 社会信用代码 /// 行政区划码 - public static string GetAreaCodeFromCreditCode(string creditCode) + public static string? GetAreaCodeFromCreditCode(string? creditCode) { if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) { @@ -147,7 +148,7 @@ public static string GetAreaCodeFromCreditCode(string creditCode) /// /// 社会信用代码 /// 机构类型 - public static string GetOrgTypeFromCreditCode(string creditCode) + public static string? GetOrgTypeFromCreditCode(string? creditCode) { if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) { diff --git a/EasyTool.Core/ToolCategory/DLLUtil.cs b/EasyTool.Core/ToolCategory/DLLUtil.cs index e41e2ab..d14a199 100644 --- a/EasyTool.Core/ToolCategory/DLLUtil.cs +++ b/EasyTool.Core/ToolCategory/DLLUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -13,9 +13,11 @@ public class DLLUtil { /// /// 根据文件路径加载 DLL 程序集,并返回一个 Assembly 对象 + /// [Obsolete("请直接使用 Assembly.LoadFile(dllFilePath)")] /// /// DLL 文件路径 /// 返回一个 Assembly 对象 + [Obsolete("请直接使用 Assembly.LoadFile(dllFilePath)", false)] public static Assembly LoadAssembly(string dllFilePath) { return Assembly.LoadFile(dllFilePath); @@ -23,22 +25,26 @@ public static Assembly LoadAssembly(string dllFilePath) /// /// 根据类型名称从程序集中获取 Type 对象 + /// [Obsolete("请直接使用 assembly.GetType(typeName)")] /// /// 程序集 /// 类型名称 /// 返回 Type 对象 - public static Type GetTypeFromAssembly(Assembly assembly, string typeName) + [Obsolete("请直接使用 assembly.GetType(typeName)", false)] + public static Type? GetTypeFromAssembly(Assembly assembly, string typeName) { return assembly.GetType(typeName); } /// /// 创建指定类型的实例,并返回一个 Object 对象 + /// [Obsolete("请直接使用 Activator.CreateInstance(type, parameters)")] /// /// 要创建实例的类型 /// 实例化类型所需要的参数 /// 返回创建的实例对象 - public static object CreateInstance(Type type, params object[] parameters) + [Obsolete("请直接使用 Activator.CreateInstance(type, parameters)", false)] + public static object? CreateInstance(Type type, params object[] parameters) { return Activator.CreateInstance(type, parameters); } @@ -50,9 +56,9 @@ public static object CreateInstance(Type type, params object[] parameters) /// 类型名称 /// 实例化类型所需要的参数 /// 返回创建的实例对象 - public static object CreateInstanceFromAssembly(Assembly assembly, string typeName, params object[] parameters) + public static object? CreateInstanceFromAssembly(Assembly assembly, string typeName, params object[] parameters) { - Type type = GetTypeFromAssembly(assembly, typeName); + Type? type = GetTypeFromAssembly(assembly, typeName); if (type != null) { return CreateInstance(type, parameters); @@ -67,10 +73,10 @@ public static object CreateInstanceFromAssembly(Assembly assembly, string typeNa /// 方法名称 /// 方法所需要的参数 /// 返回调用结果 - public static object InvokeMethod(object instance, string methodName, params object[] parameters) + public static object? InvokeMethod(object instance, string methodName, params object[] parameters) { Type type = instance.GetType(); - MethodInfo methodInfo = type.GetMethod(methodName); + MethodInfo? methodInfo = type.GetMethod(methodName); if (methodInfo != null) { return methodInfo.Invoke(instance, parameters); @@ -83,9 +89,11 @@ public static object InvokeMethod(object instance, string methodName, params obj /// /// 获取程序集中所有的类型信息 + /// [Obsolete("请直接使用 assembly.GetTypes()")] /// /// 程序集 /// 返回 Type[] 数组,数组中每个元素代表程序集中的一个类型 + [Obsolete("请直接使用 assembly.GetTypes()", false)] public static Type[] GetAllTypesFromAssembly(Assembly assembly) { return assembly.GetTypes(); @@ -93,10 +101,12 @@ public static Type[] GetAllTypesFromAssembly(Assembly assembly) /// /// 判断指定类型是否实现了指定的接口 + /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)")] /// /// 要判断的类型 /// 要判断的接口类型 /// 返回布尔值,表示指定类型是否实现了指定的接口 + [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)", false)] public static bool IsImplementInterface(Type type, Type interfaceType) { return interfaceType.IsAssignableFrom(type); diff --git a/EasyTool.Core/ToolCategory/DelegateExtension.cs b/EasyTool.Core/ToolCategory/DelegateExtension.cs new file mode 100644 index 0000000..d90708b --- /dev/null +++ b/EasyTool.Core/ToolCategory/DelegateExtension.cs @@ -0,0 +1,428 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.Extension +{ + /// + /// Delegate 委托扩展方法 + /// + public static class DelegateExtension + { + #region 安全调用 + + /// + /// 安全调用 Action(捕获异常) + /// + public static void SafeInvoke(this Action? action, Action? onError = null) + { + if (action == null) + return; + + try + { + action(); + } + catch (Exception ex) + { + onError?.Invoke(ex); + } + } + + /// + /// 安全调用 Func(捕获异常,失败返回默认值) + /// + public static T? SafeInvoke(this Func? func, T? defaultValue = default, Action? onError = null) + { + if (func == null) + return defaultValue; + + try + { + return func(); + } + catch (Exception ex) + { + onError?.Invoke(ex); + return defaultValue; + } + } + + #endregion + + #region 重试 + + /// + /// Action 重试执行 + /// + public static void Retry(this Action action, int retryCount = 3, int delayMs = 0) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + Thread.Sleep(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// Func 重试执行 + /// + public static T Retry(this Func func, int retryCount = 3, int delayMs = 0) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + Thread.Sleep(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 异步 Action 重试执行 + /// + public static async Task RetryAsync(this Func action, int retryCount = 3, int delayMs = 0) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + await action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + await Task.Delay(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 异步 Func 重试执行 + /// + public static async Task RetryAsync(this Func> func, int retryCount = 3, int delayMs = 0) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + await Task.Delay(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + #endregion + + #region 超时 + + /// + /// 设置 Action 超时 + /// + public static void WithTimeout(this Action action, int timeoutMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + var task = Task.Run(action); + if (!task.Wait(timeoutMs)) + throw new TimeoutException($"操作超时({timeoutMs}ms)"); + } + + /// + /// 设置 Func 超时 + /// + public static T WithTimeout(this Func func, int timeoutMs) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + var task = Task.Run(func); + if (!task.Wait(timeoutMs)) + throw new TimeoutException($"操作超时({timeoutMs}ms)"); + + return task.Result; + } + + #endregion + + #region 防抖与节流 + + /// + /// 防抖(延迟执行,如果在延迟时间内再次调用则重新计时) + /// + public static Action Debounce(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Timer? timer = null; + return () => + { + timer?.Dispose(); + timer = new Timer(state => + { + action(); + timer?.Dispose(); + }, null, delayMs, Timeout.Infinite); + }; + } + + /// + /// 节流(指定时间间隔内只执行一次) + /// + public static Action Throttle(this Action action, int intervalMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + DateTime lastRun = DateTime.MinValue; + return () => + { + var now = DateTime.Now; + if ((now - lastRun).TotalMilliseconds >= intervalMs) + { + action(); + lastRun = now; + } + }; + } + + #endregion + + #region 延迟执行 + + /// + /// 延迟执行 Action + /// + public static void Delay(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Task.Delay(delayMs).ContinueWith(_ => action()); + } + + /// + /// 异步延迟执行 Action + /// + public static async Task DelayAsync(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + await Task.Delay(delayMs); + action(); + } + + /// + /// 异步延迟执行 Func + /// + public static async Task DelayAsync(this Func func, int delayMs) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + await Task.Delay(delayMs); + return func(); + } + + #endregion + + #region 链式调用 + + /// + /// 链式调用 Action + /// + public static Action? Then(this Action? first, Action? second) + { + if (first == null) + return second; + if (second == null) + return first; + + return () => + { + first(); + second(); + }; + } + + /// + /// 链式调用 Func + /// + public static Func? Then(this Func? first, Func? second) + { + if (first == null) + return second; + if (second == null) + return first; + + return () => + { + first(); + return second(); + }; + } + + /// + /// 链式调用 Func(转换) + /// + public static Func? Then(this Func? first, Func? second) + { + if (first == null || second == null) + return null; + + return () => second(first()); + } + + #endregion + + #region 条件执行 + + /// + /// 条件执行 Action + /// + public static Action? ExecuteIf(this Action? action, bool condition) + { + if (action == null) + return null; + + return () => + { + if (condition) + action(); + }; + } + + /// + /// 条件执行 Func + /// + public static Func? ExecuteIf(this Func? func, bool condition) + { + if (func == null) + return null; + + return () => condition ? func() : default; + } + + #endregion + + #region 缓存 + + /// + /// 缓存 Func 结果 + /// + public static Func? Cache(this Func? func) + { + if (func == null) + return null; + + bool cached = false; + T? value = default; + + return () => + { + if (!cached) + { + value = func(); + cached = true; + } + return value; + }; + } + + #endregion + + #region 组合 + + /// + /// 组合多个 Action + /// + public static Action Combine(params Action[] actions) + { + return () => + { + foreach (var action in actions) + { + action?.Invoke(); + } + }; + } + + /// + /// 组合多个 Func(后者的结果作为前者的参数) + /// + public static Func? Compose(this Func? func1, Func? func2) + { + if (func1 == null || func2 == null) + return null; + + return x => func1(func2(x)); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/EnumExtension.cs b/EasyTool.Core/ToolCategory/EnumExtension.cs new file mode 100644 index 0000000..bd117b5 --- /dev/null +++ b/EasyTool.Core/ToolCategory/EnumExtension.cs @@ -0,0 +1,357 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; + +namespace EasyTool.Extension +{ + /// + /// Enum 枚举扩展方法 + /// + public static class EnumExtension + { + #region 描述信息 + + /// + /// 获取枚举值的描述(Description 特性) + /// + public static string GetDescription(this Enum value) + { + if (value == null) + return string.Empty; + + var field = value.GetType().GetField(value.ToString()); + if (field == null) + return value.ToString(); + + var attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; + return attr?.Description ?? value.ToString(); + } + + /// + /// 获取枚举值的显示名称(Display 特性) + /// + public static string GetDisplayName(this Enum value) + { + if (value == null) + return string.Empty; + + var field = value.GetType().GetField(value.ToString()); + if (field == null) + return value.ToString(); + + var attr = field.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.DisplayAttribute; + return attr?.GetName() ?? value.ToString(); + } + + #endregion + + #region 枚举转换 + + /// + /// 将枚举值转换为整数 + /// [Obsolete("请直接使用 Convert.ToInt32(value)")] + /// + [Obsolete("请直接使用 Convert.ToInt32(value)", false)] + public static int ToInt(this Enum value) + { + if (value == null) + return 0; + + return Convert.ToInt32(value); + } + + /// + /// 将整数转换为枚举 + /// + public static T ToEnum(this int value) where T : struct, Enum + { + return Enum.Parse(value.ToString()); + } + + /// + /// 安全解析字符串为枚举 + /// + public static T ParseEnum(this string value) where T : struct, Enum + { + return Enum.Parse(value, true); + } + + /// + /// 安全解析字符串为枚举,失败返回默认值 + /// + public static T ParseEnumOrDefault(this string value, T defaultValue = default) where T : struct, Enum + { + if (Enum.TryParse(value, true, out var result)) + return result; + + return defaultValue; + } + + /// + /// 尝试解析字符串为枚举 + /// [Obsolete("请直接使用 Enum.TryParse(value, true, out result)")] + /// + [Obsolete("请直接使用 Enum.TryParse(value, true, out result)", false)] + public static bool TryParseEnum(this string value, out T result) where T : struct, Enum + { + return Enum.TryParse(value, true, out result); + } + + #endregion + + #region 枚举集合 + + /// + /// 获取枚举类型的所有值 + /// + public static T[] GetValues() where T : struct, Enum + { + return (T[])Enum.GetValues(typeof(T)); + } + + /// + /// 获取枚举类型的所有名称 + /// + public static string[] GetNames() where T : struct, Enum + { + return Enum.GetNames(typeof(T)); + } + + /// + /// 获取枚举类型的所有值和描述 + /// + public static Dictionary ToDictionary() where T : struct, Enum + { + var dictionary = new Dictionary(); + foreach (T value in GetValues()) + { + dictionary[value] = value.GetDescription(); + } + return dictionary; + } + + /// + /// 获取枚举类型的所有值和显示名称 + /// + public static Dictionary ToDisplayNameDictionary() where T : struct, Enum + { + var dictionary = new Dictionary(); + foreach (T value in GetValues()) + { + dictionary[value] = value.GetDisplayName(); + } + return dictionary; + } + + #endregion + + #region 枚举判断 + + /// + /// 判断是否是定义的枚举值 + /// [Obsolete("请直接使用 Enum.IsDefined(typeof(T), value)")] + /// + [Obsolete("请直接使用 Enum.IsDefined(typeof(T), value)", false)] + public static bool IsDefined(this T value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 判断字符串是否是有效的枚举名称或值 + /// + public static bool IsEnumDefined(this string value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 判断枚举值是否有指定标记 + /// + public static bool HasFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + return (valueInt & flagInt) == flagInt; + } + + /// + /// 设置枚举标记 + /// + public static T SetFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt | flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + /// + /// 清除枚举标记 + /// + public static T ClearFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt & ~flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + /// + /// 切换枚举标记 + /// + public static T ToggleFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt ^ flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + #endregion + + #region 下一个/上一个值 + + /// + /// 获取下一个枚举值 + /// + public static T Next(this T value) where T : struct, Enum + { + var values = GetValues(); + int index = Array.IndexOf(values, value); + if (index < 0 || index >= values.Length - 1) + return value; + + return values[index + 1]; + } + + /// + /// 获取上一个枚举值 + /// + public static T Previous(this T value) where T : struct, Enum + { + var values = GetValues(); + int index = Array.IndexOf(values, value); + if (index <= 0) + return value; + + return values[index - 1]; + } + + /// + /// 获取第一个枚举值 + /// + public static T First() where T : struct, Enum + { + var values = GetValues(); + return values[0]; + } + + /// + /// 获取最后一个枚举值 + /// + public static T Last() where T : struct, Enum + { + var values = GetValues(); + return values[values.Length - 1]; + } + + #endregion + + #region 随机值 + + /// + /// 获取随机枚举值 + /// + public static T Random() where T : struct, Enum + { + var values = GetValues(); + var random = new System.Random(); + int index = random.Next(values.Length); + return values[index]; + } + + #endregion + + #region Flags 操作 + + /// + /// 获取 Flags 枚举的所有设置值 + /// + public static IEnumerable GetFlags(this T value) where T : struct, Enum + { + var values = GetValues(); + var valueInt = Convert.ToInt64(value); + + foreach (T flag in values) + { + var flagInt = Convert.ToInt64(flag); + if (flagInt == 0) + continue; + + if ((valueInt & flagInt) == flagInt) + yield return flag; + } + } + + /// + /// 判断是否是 Flags 枚举 + /// + public static bool IsFlagsEnum() where T : struct, Enum + { + return typeof(T).IsDefined(typeof(FlagsAttribute), false); + } + + #endregion + + #region 下拉框/选择列表 + + /// + /// 获取枚举的所有选项(用于下拉框等) + /// + public static List> GetItems() where T : struct, Enum + { + var items = new List>(); + foreach (T value in GetValues()) + { + items.Add(new EnumItem + { + Value = value, + Name = value.ToString(), + Description = value.GetDescription(), + DisplayName = value.GetDisplayName() + }); + } + return items; + } + + #endregion + } + + /// + /// 枚举项 + /// + public class EnumItem where T : struct, Enum + { + /// + /// 枚举值 + /// + public T Value { get; set; } + + /// + /// 名称 + /// + public string? Name { get; set; } + + /// + /// 描述 + /// + public string? Description { get; set; } + + /// + /// 显示名称 + /// + public string? DisplayName { get; set; } + } +} diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs index bd3dc3e..f03522b 100644 --- a/EasyTool.Core/ToolCategory/EnumUtil.cs +++ b/EasyTool.Core/ToolCategory/EnumUtil.cs @@ -15,9 +15,11 @@ public class EnumUtil { /// /// 获取指定枚举类型的所有成员名称 + /// [Obsolete("请直接使用 Enum.GetNames(typeof(TEnum))")] /// /// 要获取成员名称的枚举类型 /// 所有成员名称的字符串数组 + [Obsolete("请直接使用 Enum.GetNames(typeof(TEnum))", false)] public static string[] GetNames() { return Enum.GetNames(typeof(TEnum)); @@ -25,9 +27,11 @@ public static string[] GetNames() /// /// 获取指定枚举类型的所有成员的值 + /// [Obsolete("请直接使用 (TEnum[])Enum.GetValues(typeof(TEnum))")] /// /// 要获取成员值的枚举类型 /// 所有成员值的数组 + [Obsolete("请直接使用 (TEnum[])Enum.GetValues(typeof(TEnum))", false)] public static TEnum[] GetValues() { return (TEnum[])Enum.GetValues(typeof(TEnum)); @@ -35,10 +39,12 @@ public static TEnum[] GetValues() /// /// 获取指定枚举值的名称 + /// [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)")] /// /// 枚举类型 /// 枚举值 /// 枚举值的名称 + [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)", false)] public static string GetName(TEnum value) { return Enum.GetName(typeof(TEnum), value); @@ -46,10 +52,12 @@ public static string GetName(TEnum value) /// /// 检查指定的值是否是枚举类型TEnum的成员 + /// [Obsolete("请直接使用 Enum.IsDefined(typeof(TEnum), value)")] /// /// 枚举类型 /// 要检查的值 /// 如果指定的值是TEnum的成员,则为true;否则为false + [Obsolete("请直接使用 Enum.IsDefined(typeof(TEnum), value)", false)] public static bool IsDefined(object value) { return Enum.IsDefined(typeof(TEnum), value); @@ -57,10 +65,12 @@ public static bool IsDefined(object value) /// /// 将字符串转换为枚举类型TEnum的值 + /// [Obsolete("请直接使用 (TEnum)Enum.Parse(typeof(TEnum), value)")] /// /// 枚举类型 /// 要转换的字符串 /// 与字符串对应的枚举值 + [Obsolete("请直接使用 (TEnum)Enum.Parse(typeof(TEnum), value)", false)] public static TEnum Parse(string value) { return (TEnum)Enum.Parse(typeof(TEnum), value); @@ -81,9 +91,11 @@ public static TEnum Parse(string value, TEnum defaultValue) /// /// 获取指定枚举类型的Type对象 + /// [Obsolete("请直接使用 typeof(TEnum)")] /// /// 枚举类型 /// 枚举类型的Type对象 + [Obsolete("请直接使用 typeof(TEnum)", false)] public static Type GetEnumType() { return typeof(TEnum); @@ -193,10 +205,12 @@ public static TEnum GetValueByName(string name) /// /// 获取指定枚举类型的指定值的名称 + /// [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)")] /// /// 枚举类型 /// 枚举值 /// 与值对应的名称,如果值不存在,则返回null + [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)", false)] public static string? GetNameByValue(TEnum value) { return Enum.GetName(typeof(TEnum), value!); diff --git a/EasyTool.Core/ToolCategory/EnvUtil.cs b/EasyTool.Core/ToolCategory/EnvUtil.cs index 54932cf..bb67821 100644 --- a/EasyTool.Core/ToolCategory/EnvUtil.cs +++ b/EasyTool.Core/ToolCategory/EnvUtil.cs @@ -35,9 +35,11 @@ public static string GetSystemInfo() /// /// 获取环境变量值 + /// [Obsolete("请直接使用 Environment.GetEnvironmentVariable(name)")] /// /// 环境变量名称 /// 环境变量值 + [Obsolete("请直接使用 Environment.GetEnvironmentVariable(name)", false)] public static string GetEnvironmentVariable(string name) { return Environment.GetEnvironmentVariable(name); @@ -45,9 +47,11 @@ public static string GetEnvironmentVariable(string name) /// /// 设置环境变量值 + /// [Obsolete("请直接使用 Environment.SetEnvironmentVariable(name, value)")] /// /// 环境变量名称 /// 环境变量值 + [Obsolete("请直接使用 Environment.SetEnvironmentVariable(name, value)", false)] public static void SetEnvironmentVariable(string name, string value) { Environment.SetEnvironmentVariable(name, value); @@ -127,8 +131,10 @@ public static List GetDirectoriesInDirectory(string path) /// /// 创建文件 + /// [Obsolete("请直接使用 File.Create(path)")] /// /// 文件路径 + [Obsolete("请直接使用 File.Create(path)", false)] public static void CreateFile(string path) { File.Create(path); @@ -136,8 +142,10 @@ public static void CreateFile(string path) /// /// 删除文件 + /// [Obsolete("请直接使用 File.Delete(path)")] /// /// 文件路径 + [Obsolete("请直接使用 File.Delete(path)", false)] public static void DeleteFile(string path) { File.Delete(path); @@ -145,8 +153,10 @@ public static void DeleteFile(string path) /// /// 创建目录 + /// [Obsolete("请直接使用 Directory.CreateDirectory(path)")] /// /// 目录路径 + [Obsolete("请直接使用 Directory.CreateDirectory(path)", false)] public static void CreateDirectory(string path) { Directory.CreateDirectory(path); @@ -154,8 +164,10 @@ public static void CreateDirectory(string path) /// /// 删除目录 + /// [Obsolete("请直接使用 Directory.Delete(path, true)")] /// /// 目录路径 + [Obsolete("请直接使用 Directory.Delete(path, true)", false)] public static void DeleteDirectory(string path) { Directory.Delete(path, true); @@ -163,9 +175,11 @@ public static void DeleteDirectory(string path) /// /// 检查目录是否存在 + /// [Obsolete("请直接使用 Directory.Exists(path)")] /// /// 目录路径 /// 目录是否存在 + [Obsolete("请直接使用 Directory.Exists(path)", false)] public static bool DirectoryExists(string path) { return Directory.Exists(path); @@ -173,9 +187,11 @@ public static bool DirectoryExists(string path) /// /// 检查文件是否存在 + /// [Obsolete("请直接使用 File.Exists(path)")] /// /// 文件路径 /// 文件是否存在 + [Obsolete("请直接使用 File.Exists(path)", false)] public static bool FileExists(string path) { return File.Exists(path); @@ -216,10 +232,12 @@ public static DateTime GetFileLastWriteTime(string path) /// /// 复制文件 + /// [Obsolete("请直接使用 File.Copy(sourcePath, destinationPath, overwrite)")] /// /// 源文件路径 /// 目标文件路径 /// 是否覆盖已有文件 + [Obsolete("请直接使用 File.Copy(sourcePath, destinationPath, overwrite)", false)] public static void CopyFile(string sourcePath, string destinationPath, bool overwrite) { File.Copy(sourcePath, destinationPath, overwrite); @@ -227,9 +245,11 @@ public static void CopyFile(string sourcePath, string destinationPath, bool over /// /// 移动文件 + /// [Obsolete("请直接使用 File.Move(sourcePath, destinationPath)")] /// /// 源文件路径 /// 目标文件路径 + [Obsolete("请直接使用 File.Move(sourcePath, destinationPath)", false)] public static void MoveFile(string sourcePath, string destinationPath) { File.Move(sourcePath, destinationPath); diff --git a/EasyTool.Core/ToolCategory/EscapeUtil.cs b/EasyTool.Core/ToolCategory/EscapeUtil.cs index 7d030c5..eea8c2f 100644 --- a/EasyTool.Core/ToolCategory/EscapeUtil.cs +++ b/EasyTool.Core/ToolCategory/EscapeUtil.cs @@ -94,9 +94,11 @@ public static string Unescape(string str) /// /// 将URL中的特殊字符进行转义 + /// [Obsolete("请直接使用 Uri.EscapeDataString(url)")] /// /// 需要转义的URL /// 转义后的URL + [Obsolete("请直接使用 Uri.EscapeDataString(url)", false)] public static string UrlEncode(string url) { if (string.IsNullOrEmpty(url)) @@ -109,9 +111,11 @@ public static string UrlEncode(string url) /// /// 将URL中的转义字符还原成特殊字符 + /// [Obsolete("请直接使用 Uri.UnescapeDataString(url)")] /// /// 需要还原的URL /// 还原后的URL + [Obsolete("请直接使用 Uri.UnescapeDataString(url)", false)] public static string UrlDecode(string url) { if (string.IsNullOrEmpty(url)) @@ -124,9 +128,11 @@ public static string UrlDecode(string url) /// /// 将HTML字符串进行转义,将特殊字符替换成HTML实体 + /// [Obsolete("请直接使用 System.Net.WebUtility.HtmlEncode(html)")] /// /// 需要转义的HTML字符串 /// 转义后的HTML字符串 + [Obsolete("请直接使用 System.Net.WebUtility.HtmlEncode(html)", false)] public static string HtmlEncode(string html) { if (string.IsNullOrEmpty(html)) @@ -139,9 +145,11 @@ public static string HtmlEncode(string html) /// /// 将HTML字符串中的HTML实体还原成特殊字符 + /// [Obsolete("请直接使用 System.Net.WebUtility.HtmlDecode(html)")] /// /// 需要还原的HTML字符串 /// 还原后的HTML字符串 + [Obsolete("请直接使用 System.Net.WebUtility.HtmlDecode(html)", false)] public static string HtmlDecode(string html) { if (string.IsNullOrEmpty(html)) @@ -154,9 +162,11 @@ public static string HtmlDecode(string html) /// /// 将XML字符串进行转义,将特殊字符替换成XML实体 + /// [Obsolete("请直接使用 System.Security.SecurityElement.Escape(xml)")] /// /// 需要转义的XML字符串 /// 转义后的XML字符串 + [Obsolete("请直接使用 System.Security.SecurityElement.Escape(xml)", false)] public static string XmlEncode(string xml) { if (string.IsNullOrEmpty(xml)) diff --git a/EasyTool.Core/ToolCategory/ExceptionExtension.cs b/EasyTool.Core/ToolCategory/ExceptionExtension.cs new file mode 100644 index 0000000..6ebc43e --- /dev/null +++ b/EasyTool.Core/ToolCategory/ExceptionExtension.cs @@ -0,0 +1,338 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; + +namespace EasyTool.Extension +{ + /// + /// Exception 异常扩展方法 + /// + public static class ExceptionExtension + { + #region 消息获取 + + /// + /// 获取完整的异常消息(包含所有内层异常) + /// + public static string GetFullMessage(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + var current = exception; + + int depth = 0; + while (current != null) + { + if (depth > 0) + sb.Append("Inner Exception: "); + + sb.AppendLine(current.Message); + current = current.InnerException; + depth++; + + // 防止无限循环 + if (depth > 100) + break; + } + + return sb.ToString().Trim(); + } + + /// + /// 获取所有异常(包含内层异常) + /// + public static Exception[] GetAllExceptions(this Exception? exception) + { + if (exception == null) + return Array.Empty(); + + var exceptions = new List(); + var current = exception; + + int depth = 0; + while (current != null) + { + exceptions.Add(current); + current = current.InnerException; + depth++; + + // 防止无限循环 + if (depth > 100) + break; + } + + return exceptions.ToArray(); + } + + /// + /// 获取所有内层异常 + /// + public static Exception[] GetInnerExceptions(this Exception exception) + { + var all = exception.GetAllExceptions(); + return all.Skip(1).ToArray(); + } + + #endregion + + #region 详细信息 + + /// + /// 获取异常的详细字符串表示 + /// + public static string ToDetailedString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + + sb.AppendLine($"Exception Type: {exception.GetType().FullName}"); + sb.AppendLine($"Message: {exception.Message}"); + sb.AppendLine($"Source: {exception.Source ?? "(unknown)"}"); + + if (exception.TargetSite != null) + { + sb.AppendLine($"Target Site: {exception.TargetSite}"); + } + + if (!string.IsNullOrEmpty(exception.HelpLink)) + { + sb.AppendLine($"Help Link: {exception.HelpLink}"); + } + + if (exception.Data != null && exception.Data.Count > 0) + { + sb.AppendLine("Data:"); + foreach (var key in exception.Data.Keys) + { + sb.AppendLine($" {key}: {exception.Data[key]}"); + } + } + + if (!string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine("Stack Trace:"); + sb.AppendLine(exception.StackTrace); + } + + // 处理内层异常 + if (exception.InnerException != null) + { + sb.AppendLine(); + sb.AppendLine("--- Inner Exception ---"); + sb.Append(exception.InnerException.ToDetailedString()); + } + + return sb.ToString(); + } + + /// + /// 获取异常的简略字符串表示 + /// + public static string ToSimpleString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + return $"{exception.GetType().Name}: {exception.Message}"; + } + + #endregion + + #region 异常类型判断 + + /// + /// 判断异常是否是指定类型 + /// + public static bool IsType(this Exception? exception) where T : Exception + { + return exception is T; + } + + /// + /// 判断异常或其内层异常是否是指定类型 + /// + public static bool IsOrContainsType(this Exception? exception) where T : Exception + { + var current = exception; + + while (current != null) + { + if (current is T) + return true; + + current = current.InnerException; + } + + return false; + } + + /// + /// 查找第一个指定类型的异常 + /// + public static T? FindType(this Exception? exception) where T : Exception + { + var current = exception; + + while (current != null) + { + if (current is T typedException) + return typedException; + + current = current.InnerException; + } + + return null; + } + + #endregion + + #region 特定异常处理 + + /// + /// 判断是否是超时异常 + /// + public static bool IsTimeout(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是取消操作异常 + /// + public static bool IsOperationCanceled(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是参数异常 + /// + public static bool IsArgumentException(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是空引用异常 + /// + public static bool IsNullReference(this Exception? exception) + { + return exception is NullReferenceException; + } + + /// + /// 判断是否是 IO 异常 + /// + public static bool IsIOException(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + #endregion + + #region 异常包装 + + /// + /// 使用指定消息包装异常 + /// + public static Exception WrapWith(this Exception? exception, string message) + { + return new Exception(message, exception); + } + + /// + /// 使用指定类型包装异常 + /// + public static TException WrapWith(this Exception? exception, string message) where TException : Exception + { + return (TException)Activator.CreateInstance(typeof(TException), message, exception)!; + } + + #endregion + + #region 日志格式化 + + /// + /// 获取适合日志记录的异常信息 + /// + public static string ToLogString(this Exception? exception, bool includeStackTrace = true) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + + sb.Append($"[{exception.GetType().Name}] "); + sb.AppendLine(exception.Message); + + if (includeStackTrace && !string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine(exception.StackTrace.Trim()); + } + + return sb.ToString(); + } + + /// + /// 获取单行格式的异常信息 + /// + public static string ToOneLineString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + var current = exception; + + while (current != null) + { + if (sb.Length > 0) + sb.Append(" -> "); + + sb.Append($"[{current.GetType().Name}] {current.Message}"); + current = current.InnerException; + + // 防止无限循环 + if (sb.Length > 1000) + break; + } + + return sb.ToString(); + } + + #endregion + + #region 聚合异常处理 + + /// + /// 获取聚合异常中的所有异常 + /// + public static Exception[] GetInnerExceptions(this AggregateException? exception) + { + if (exception == null) + return Array.Empty(); + + return exception.InnerExceptions.ToArray(); + } + + /// + /// 展平聚合异常(递归获取所有内层异常) + /// [Obsolete("请直接使用 exception.Flatten().InnerExceptions.ToArray()")] + /// + [Obsolete("请直接使用 exception.Flatten().InnerExceptions.ToArray()", false)] + public static Exception[] Flatten(this AggregateException? exception) + { + if (exception == null) + return Array.Empty(); + + return exception.Flatten().InnerExceptions.ToArray(); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/GuidExtension.cs b/EasyTool.Core/ToolCategory/GuidExtension.cs new file mode 100644 index 0000000..4f04619 --- /dev/null +++ b/EasyTool.Core/ToolCategory/GuidExtension.cs @@ -0,0 +1,288 @@ +using System; +using System.Text; + +namespace EasyTool.Extension +{ + /// + /// Guid 扩展方法 + /// + public static class GuidExtension + { + #region 空值判断 + + /// + /// 判断 Guid 是否为空 + /// [Obsolete("请直接使用 guid == Guid.Empty")] + /// + [Obsolete("请直接使用 guid == Guid.Empty", false)] + public static bool IsEmpty(this Guid guid) + { + return guid == Guid.Empty; + } + + /// + /// 判断 Guid 是否为空或默认值 + /// + public static bool IsNullOrEmpty(this Guid? guid) + { + return guid == null || guid.Value == Guid.Empty; + } + + /// + /// 判断 Guid 是否有值(非空) + /// + public static bool HasValue(this Guid guid) + { + return guid != Guid.Empty; + } + + /// + /// 判断可空 Guid 是否有值 + /// + public static bool HasValue(this Guid? guid) + { + return guid.HasValue && guid.Value != Guid.Empty; + } + + #endregion + + #region 格式化转换 + + /// + /// 获取短格式 Guid(不带连字符) + /// + public static string ToShortString(this Guid guid) + { + return guid.ToString("N"); + } + + /// + /// 获取短格式 Guid(带连字符) + /// + public static string ToShortStringWithDashes(this Guid guid) + { + return guid.ToString("D"); + } + + /// + /// 获取带括号的 Guid 格式 + /// + public static string ToFormattedString(this Guid guid) + { + return guid.ToString("B"); + } + + /// + /// 获取带大括号的 Guid 格式 + /// + public static string ToBracedString(this Guid guid) + { + return guid.ToString("B"); + } + + /// + /// 获取 Guid 的字节数组表示 + /// [Obsolete("请直接使用 guid.ToByteArray()")] + /// + [Obsolete("请直接使用 guid.ToByteArray()", false)] + public static byte[] ToByteArray(this Guid guid) + { + return guid.ToByteArray(); + } + + /// + /// 将 Guid 转换为 Base64 字符串 + /// + public static string ToBase64String(this Guid guid) + { + return Convert.ToBase64String(guid.ToByteArray()); + } + + /// + /// 从 Base64 字符串创建 Guid + /// + public static Guid FromBase64String(this string base64) + { + var bytes = Convert.FromBase64String(base64); + return new Guid(bytes); + } + + #endregion + + #region 字符串解析 + + /// + /// 尝试解析字符串为 Guid,失败返回空 Guid + /// + public static Guid ToGuidOrDefault(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return Guid.Empty; + + return Guid.TryParse(value, out var guid) ? guid : Guid.Empty; + } + + /// + /// 尝试解析字符串为 Guid,失败返回默认值 + /// + public static Guid ToGuidOrDefault(this string value, Guid defaultValue) + { + if (string.IsNullOrWhiteSpace(value)) + return defaultValue; + + return Guid.TryParse(value, out var guid) ? guid : defaultValue; + } + + /// + /// 判断字符串是否是有效的 Guid 格式 + /// + public static bool IsValidGuid(this string value) + { + return !string.IsNullOrWhiteSpace(value) && Guid.TryParse(value, out _); + } + + #endregion + + #region 加密相关 + + /// + /// 获取 Guid 的 MD5 哈希值(作为新的 Guid) + /// + public static Guid ToMd5Guid(this Guid guid) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var bytes = guid.ToByteArray(); + var hash = md5.ComputeHash(bytes); + return new Guid(hash); + } + + /// + /// 获取 Guid 的 SHA1 哈希值(取前16字节作为 Guid) + /// + public static Guid ToSha1Guid(this Guid guid) + { + using var sha1 = System.Security.Cryptography.SHA1.Create(); + var bytes = guid.ToByteArray(); + var hash = sha1.ComputeHash(bytes); + var guidBytes = new byte[16]; + Array.Copy(hash, 0, guidBytes, 0, 16); + return new Guid(guidBytes); + } + + #endregion + + #region Guid 生成 + + /// + /// 基于 Guid 生成连续的 Guid(适用于 COMB 类型) + /// + public static Guid NewCombGuid() + { + var guidArray = Guid.NewGuid().ToByteArray(); + var baseDate = new DateTime(1900, 1, 1); + var now = DateTime.Now; + var days = new TimeSpan(now.Ticks - baseDate.Ticks); + var msecs = now.TimeOfDay; + + var daysArray = BitConverter.GetBytes(days.Days); + var msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333)); + + Array.Reverse(daysArray); + Array.Reverse(msecsArray); + + Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2); + Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4); + + return new Guid(guidArray); + } + + /// + /// 基于指定前缀生成可预测的 Guid + /// + public static Guid NewDeterministicGuid(string prefix) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var prefixBytes = Encoding.UTF8.GetBytes(prefix); + var hash = md5.ComputeHash(prefixBytes); + return new Guid(hash); + } + + /// + /// 基于多个参数生成可预测的 Guid + /// + public static Guid NewDeterministicGuid(params object[] values) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var combined = string.Join("|", values); + var bytes = Encoding.UTF8.GetBytes(combined); + var hash = md5.ComputeHash(bytes); + return new Guid(hash); + } + + #endregion + + #region Guid 比较 + + /// + /// 比较两个 Guid 是否相等 + /// + public static bool EqualsTo(this Guid guid, Guid other) + { + return guid.Equals(other); + } + + /// + /// 比较两个可空 Guid 是否相等 + /// + public static bool EqualsTo(this Guid? guid, Guid? other) + { + if (guid.HasValue && other.HasValue) + return guid.Value.Equals(other.Value); + return guid.HasValue == other.HasValue; + } + + #endregion + + #region Guid 操作 + + /// + /// 获取 Guid 的指定部分的值 + /// + /// Guid + /// 部分:0-3(Data1-Data4) + public static int GetPart(this Guid guid, int part) + { + var bytes = guid.ToByteArray(); + return part switch + { + 0 => BitConverter.ToInt32(bytes, 0), + 1 => BitConverter.ToInt16(bytes, 4), + 2 => BitConverter.ToInt16(bytes, 6), + 3 => bytes[8] << 24 | bytes[9] << 16 | bytes[10] << 8 | bytes[11], + _ => throw new ArgumentOutOfRangeException(nameof(part), "Part must be between 0 and 3") + }; + } + + /// + /// 将 Guid 转换为整数(用于某些场景的简化处理) + /// + public static int ToInt32(this Guid guid) + { + var bytes = guid.ToByteArray(); + return BitConverter.ToInt32(bytes, 0); + } + + /// + /// 将 Guid 转换为长整数 + /// + public static long ToInt64(this Guid guid) + { + var bytes = guid.ToByteArray(); + var high = BitConverter.ToInt64(bytes, 0); + var low = BitConverter.ToInt64(bytes, 8); + return high ^ low; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/HexUtil.cs b/EasyTool.Core/ToolCategory/HexUtil.cs index 1bc1079..52b892f 100644 --- a/EasyTool.Core/ToolCategory/HexUtil.cs +++ b/EasyTool.Core/ToolCategory/HexUtil.cs @@ -87,9 +87,11 @@ public static string IntToHex(int number) /// /// 将16进制字符串中的所有字符转换为大写 + /// [Obsolete("请直接使用 hex.ToUpper()")] /// /// 16进制字符串 /// 大写16进制字符串 + [Obsolete("请直接使用 hex.ToUpper()", false)] public static string HexToUpper(string hex) { return hex.ToUpper(); @@ -97,9 +99,11 @@ public static string HexToUpper(string hex) /// /// 将16进制字符串中的所有字符转换为小写 + /// [Obsolete("请直接使用 hex.ToLower()")] /// /// 16进制字符串 /// 小写16进制字符串 + [Obsolete("请直接使用 hex.ToLower()", false)] public static string HexToLower(string hex) { return hex.ToLower(); diff --git a/EasyTool.Core/ToolCategory/IdcardUtil.cs b/EasyTool.Core/ToolCategory/IdcardUtil.cs index eee852e..0f6d9bd 100644 --- a/EasyTool.Core/ToolCategory/IdcardUtil.cs +++ b/EasyTool.Core/ToolCategory/IdcardUtil.cs @@ -241,7 +241,7 @@ public static bool IsValidChecksum(string idcard) /// /// 身份证号码 /// 省份 - public static string GetProvince(string idcard) + public static string? GetProvince(string? idcard) { if (string.IsNullOrEmpty(idcard)) { @@ -345,7 +345,7 @@ public static string GetProvince(string idcard) /// 身份证号码 /// 新的生日日期 /// 新的身份证号码 - public static string ReplaceBirthday(string idcard, DateTime birthday) + public static string? ReplaceBirthday(string? idcard, DateTime birthday) { if (string.IsNullOrEmpty(idcard)) { diff --git a/EasyTool.Core/ToolCategory/ObjectExtension.cs b/EasyTool.Core/ToolCategory/ObjectExtension.cs new file mode 100644 index 0000000..b14b515 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ObjectExtension.cs @@ -0,0 +1,479 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.Json; +using System.Xml.Serialization; + +namespace EasyTool.Extension +{ + /// + /// Object 对象扩展方法 + /// + public static class ObjectExtension + { + #region 空值判断 + + /// + /// 判断对象是否为 null + /// + public static bool IsNull(this object obj) + { + return obj == null; + } + + /// + /// 判断对象是否不为 null + /// + public static bool IsNotNull(this object obj) + { + return obj != null; + } + + /// + /// 判断对象是否为空(null 或空字符串或空集合) + /// + public static bool IsNullOrEmpty(this object obj) + { + if (obj == null) + return true; + + if (obj is string str) + return string.IsNullOrEmpty(str); + + if (obj is System.Collections.ICollection collection) + return collection.Count == 0; + + return false; + } + + #endregion + + #region 类型转换 + + /// + /// 将对象转换为指定类型 + /// + public static T? As(this object obj) + { + if (obj == null) + return default; + + return (T)obj; + } + + /// + /// 尝试将对象转换为指定类型 + /// + public static T To(this object obj) where T : struct + { + return (T)Convert.ChangeType(obj, typeof(T)); + } + + /// + /// 安全转换,失败返回默认值 + /// + public static T? ToOrDefault(this object obj) + { + return ToOrDefault(obj, default(T)); + } + + /// + /// 安全转换,失败返回指定默认值 + /// + public static T ToOrDefault(this object obj, T defaultValue) + { + if (obj == null) + return defaultValue; + + try + { + if (obj is T direct) + return direct; + + return (T)Convert.ChangeType(obj, typeof(T)); + } + catch + { + return defaultValue; + } + } + + #endregion + + #region JSON 序列化 + + /// + /// 将对象序列化为 JSON 字符串 + /// + public static string? ToJson(this object obj, bool indented = false) + { + if (obj == null) + return null; + + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = indented + }; + + return JsonSerializer.Serialize(obj, options); + } + + /// + /// 从 JSON 字符串反序列化为对象 + /// + public static T? FromJson(this string json) + { + if (string.IsNullOrEmpty(json)) + return default; + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + return JsonSerializer.Deserialize(json, options); + } + + #endregion + + #region XML 序列化 + + /// + /// 将对象序列化为 XML 字符串 + /// + public static string? ToXml(this object obj) + { + if (obj == null) + return null; + + var serializer = new XmlSerializer(obj.GetType()); + using var writer = new StringWriter(); + serializer.Serialize(writer, obj); + return writer.ToString(); + } + + /// + /// 从 XML 字符串反序列化为对象 + /// + public static T? FromXml(this string xml) + { + if (string.IsNullOrEmpty(xml)) + return default; + + var serializer = new XmlSerializer(typeof(T)); + using var reader = new StringReader(xml); + return (T?)serializer.Deserialize(reader); + } + + #endregion + + #region 深拷贝 + + /// + /// 深拷贝对象(使用 JSON 序列化) + /// + public static T? DeepClone(this T obj) + { + if (obj == null) + return default; + + var json = obj.ToJson(); + return json.FromJson(); + } + + /// + /// 浅拷贝对象(使用 MemberwiseClone) + /// + public static T? ShallowClone(this T obj) where T : class + { + if (obj == null) + return null; + + var method = obj.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic); + if (method != null) + return (T?)method.Invoke(obj, null); + + throw new InvalidOperationException("Object does not support MemberwiseClone"); + } + + #endregion + + #region 字典转换 + + /// + /// 将对象转换为字典 + /// + public static Dictionary? ToDictionary(this object obj) + { + if (obj == null) + return null; + + var dict = new Dictionary(); + + foreach (var prop in obj.GetType().GetProperties()) + { + if (prop.CanRead) + { + dict[prop.Name] = prop.GetValue(obj) ?? string.Empty; + } + } + + return dict; + } + + /// + /// 从字典创建对象 + /// + public static T? FromDictionary(this Dictionary? dict) where T : new() + { + if (dict == null) + return default; + + var obj = new T(); + + foreach (var prop in typeof(T).GetProperties()) + { + if (prop.CanWrite && dict.TryGetValue(prop.Name, out var value)) + { + if (value != null) + { + var convertedValue = Convert.ChangeType(value, prop.PropertyType); + prop.SetValue(obj, convertedValue); + } + } + } + + return obj; + } + + #endregion + + #region 属性访问 + + /// + /// 获取属性值 + /// + public static object? GetPropertyValue(this object obj, string propertyName) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return null; + + var prop = obj.GetType().GetProperty(propertyName); + return prop?.GetValue(obj); + } + + /// + /// 设置属性值 + /// + public static void SetPropertyValue(this object obj, string propertyName, object? value) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return; + + var prop = obj.GetType().GetProperty(propertyName); + prop?.SetValue(obj, value); + } + + #endregion + + #region 条件执行 + + /// + /// 条件执行(当条件满足时执行操作) + /// + public static T If(this T obj, bool condition, Action? action) + { + if (condition) + { + action?.Invoke(obj); + } + return obj; + } + + /// + /// 条件执行(当条件满足时返回函数结果) + /// + public static TResult? If(this T obj, bool condition, Func? func) + { + return condition ? func!(obj) : default; + } + + /// + /// 条件执行(当对象不为 null 时执行操作) + /// + public static T IfNotNull(this T obj, Action? action) where T : class + { + if (obj != null) + { + action?.Invoke(obj); + } + return obj; + } + + /// + /// 条件执行(当对象不为 null 时返回函数结果) + /// + public static TResult? IfNotNull(this T obj, Func func) where T : class + { + return obj != null ? func(obj) : default; + } + + #endregion + + #region 管道操作 + + /// + /// 管道操作(执行函数并返回结果) + /// + public static TResult Pipe(this T obj, Func func) + { + return func(obj); + } + + /// + /// 管道操作(执行操作) + /// + public static T Pipe(this T obj, Action action) + { + action(obj); + return obj; + } + + #endregion + + #region 对象检查 + + /// + /// 判断对象是否是指定类型 + /// [Obsolete("请直接使用 obj is T")] + /// + [Obsolete("请直接使用 obj is T", false)] + public static bool Is(this object obj) + { + return obj is T; + } + + /// + /// 判断对象是否实现了指定接口 + /// + public static bool Implements(this object obj) + { + if (obj == null) + return false; + + return typeof(TInterface).IsAssignableFrom(obj.GetType()); + } + + #endregion + + #region 对象相等比较 + + /// + /// 比较两个对象的属性值是否相等 + /// + public static bool PropertiesEqual(this T obj, T? other) where T : class + { + if (obj == null && other == null) + return true; + + if (obj == null || other == null) + return false; + + var type = typeof(T); + + foreach (var prop in type.GetProperties()) + { + if (!prop.CanRead) + continue; + + var value1 = prop.GetValue(obj); + var value2 = prop.GetValue(other); + + if (!Equals(value1, value2)) + return false; + } + + return true; + } + + #endregion + + #region 对象信息 + + /// + /// 获取对象的类型名称 + /// [Obsolete("请直接使用 obj?.GetType().Name")] + /// + [Obsolete("请直接使用 obj?.GetType().Name", false)] + public static string GetTypeName(this object obj) + { + return obj?.GetType().Name ?? "null"; + } + + /// + /// 获取对象的完整类型名称 + /// [Obsolete("请直接使用 obj?.GetType().FullName")] + /// + [Obsolete("请直接使用 obj?.GetType().FullName", false)] + public static string GetFullTypeName(this object obj) + { + return obj?.GetType().FullName ?? "null"; + } + + #endregion + + #region 对象转字符串 + + /// + /// 将对象转换为字符串(处理 null) + /// [Obsolete("请直接使用 obj?.ToString() ?? string.Empty")] + /// + [Obsolete("请直接使用 obj?.ToString() ?? string.Empty", false)] + public static string ToStringSafe(this object obj) + { + return obj?.ToString() ?? string.Empty; + } + + /// + /// 将对象转换为字符串(null 时返回默认值) + /// [Obsolete("请直接使用 obj?.ToString() ?? defaultValue")] + /// + [Obsolete("请直接使用 obj?.ToString() ?? defaultValue", false)] + public static string ToStringOrDefault(this object obj, string defaultValue = "") + { + return obj?.ToString() ?? defaultValue; + } + + #endregion + + #region 对象抛出异常 + + /// + /// 对象为 null 时抛出异常 + /// + public static T ThrowIfNull(this T? obj, string? paramName = null) where T : class + { + if (obj == null) + throw new ArgumentNullException(paramName ?? typeof(T).Name); + + return obj; + } + + /// + /// 条件不满足时抛出异常 + /// + public static T ThrowIf(this T obj, bool condition, string message) where T : class + { + if (condition) + throw new Exception(message); + + return obj; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/ObjectUtil.cs b/EasyTool.Core/ToolCategory/ObjectUtil.cs index a2f3bcd..81c5921 100644 --- a/EasyTool.Core/ToolCategory/ObjectUtil.cs +++ b/EasyTool.Core/ToolCategory/ObjectUtil.cs @@ -21,7 +21,9 @@ public class ObjectUtil /// /// 检查对象是否为 null + /// [Obsolete("请直接使用 obj == null")] /// + [Obsolete("请直接使用 obj == null", false)] public static bool IsNull(object? obj) { return obj == null; @@ -29,7 +31,9 @@ public static bool IsNull(object? obj) /// /// 检查对象是否不为 null + /// [Obsolete("请直接使用 obj != null")] /// + [Obsolete("请直接使用 obj != null", false)] public static bool IsNotNull(object? obj) { return obj != null; @@ -86,7 +90,9 @@ public static bool IsNotNullOrEmpty(object? obj) /// /// 获取对象的类型名称 + /// [Obsolete("请直接使用 obj.GetType().Name")] /// + [Obsolete("请直接使用 obj.GetType().Name", false)] public static string GetTypeName(object obj) { return obj.GetType().Name; @@ -107,27 +113,89 @@ public static T Convert(object obj) { if (IsNull(obj)) { - return null; + // 处理可空值类型的默认值 + return GetDefault(targetType); } - if (targetType.IsAssignableFrom(obj.GetType())) + Type sourceType = obj.GetType(); + + // 如果目标类型可以从源类型赋值,直接返回 + if (targetType.IsAssignableFrom(sourceType)) { return obj; } + // 使用 TypeConverter 进行转换 + var converter = System.ComponentModel.TypeDescriptor.GetConverter(targetType); + if (converter != null && converter.CanConvertFrom(sourceType)) + { + return converter.ConvertFrom(obj); + } + + // 尝试从源类型的 TypeConverter 转换 + var sourceConverter = System.ComponentModel.TypeDescriptor.GetConverter(sourceType); + if (sourceConverter != null && sourceConverter.CanConvertTo(targetType)) + { + return sourceConverter.ConvertTo(obj, targetType); + } + + // 使用 IConvertible 接口转换 if (obj is IConvertible) { - return System.Convert.ChangeType(obj, targetType); + try + { + return System.Convert.ChangeType(obj, targetType); + } + catch (InvalidCastException) + { + // 继续尝试其他转换方式 + } } - // TODO: 支持自定义类型转换 + // 尝试使用隐式或显式转换操作符 + try + { + // 查找源类型的隐式转换操作符 + var implicitOp = sourceType.GetMethod("op_Implicit", new[] { sourceType }); + if (implicitOp != null && implicitOp.ReturnType == targetType) + { + return implicitOp.Invoke(null, new[] { obj }); + } + + // 查找源类型的显式转换操作符 + var explicitOp = sourceType.GetMethod("op_Explicit", new[] { sourceType }); + if (explicitOp != null && explicitOp.ReturnType == targetType) + { + return explicitOp.Invoke(null, new[] { obj }); + } + + // 查找目标类型的隐式转换操作符 + var targetImplicitOp = targetType.GetMethod("op_Implicit", new[] { sourceType }); + if (targetImplicitOp != null && targetImplicitOp.ReturnType == targetType) + { + return targetImplicitOp.Invoke(null, new[] { obj }); + } + + // 查找目标类型的显式转换操作符 + var targetExplicitOp = targetType.GetMethod("op_Explicit", new[] { sourceType }); + if (targetExplicitOp != null && targetExplicitOp.ReturnType == targetType) + { + return targetExplicitOp.Invoke(null, new[] { obj }); + } + } + catch (InvalidCastException) + { + // 转换操作符失败,继续抛出异常 + } - throw new InvalidCastException($"无法将类型为 {obj.GetType().Name} 的对象转换为类型为 {targetType.Name} 的对象"); + throw new InvalidCastException($"无法将类型为 {sourceType.Name} 的对象转换为类型为 {targetType.Name} 的对象"); } /// /// 获取对象的属性列表 + /// [Obsolete("请直接使用 obj.GetType().GetProperties()")] /// + [Obsolete("请直接使用 obj.GetType().GetProperties()", false)] public static IEnumerable GetProperties(object obj) { return obj.GetType().GetProperties(); @@ -151,7 +219,9 @@ public static void SetPropertyValue(object obj, string propertyName, object? val /// /// 获取对象的字段列表 + /// [Obsolete("请直接使用 obj.GetType().GetFields()")] /// + [Obsolete("请直接使用 obj.GetType().GetFields()", false)] public static IEnumerable GetFields(object obj) { return obj.GetType().GetFields(); @@ -175,7 +245,9 @@ public static void SetFieldValue(object obj, string fieldName, object? value) /// /// 获取对象的方法列表 + /// [Obsolete("请直接使用 obj.GetType().GetMethods()")] /// + [Obsolete("请直接使用 obj.GetType().GetMethods()", false)] public static IEnumerable GetMethods(object obj) { return obj.GetType().GetMethods(); @@ -183,7 +255,9 @@ public static IEnumerable GetMethods(object obj) /// /// 判断对象是否实现了指定接口 + /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(obj.GetType())")] /// + [Obsolete("请直接使用 interfaceType.IsAssignableFrom(obj.GetType())", false)] public static bool ImplementsInterface(object obj, Type interfaceType) { return interfaceType.IsAssignableFrom(obj.GetType()); @@ -191,7 +265,9 @@ public static bool ImplementsInterface(object obj, Type interfaceType) /// /// 判断对象是否为指定类型的实例 + /// [Obsolete("请直接使用 targetType.IsInstanceOfType(obj)")] /// + [Obsolete("请直接使用 targetType.IsInstanceOfType(obj)", false)] public static bool IsInstanceOfType(object obj, Type targetType) { return targetType.IsInstanceOfType(obj); @@ -474,7 +550,9 @@ public static IEnumerable Compare(object obj1, object obj2) /// /// 获取对象的哈希码 + /// [Obsolete("请直接使用 obj?.GetHashCode() ?? 0")] /// + [Obsolete("请直接使用 obj?.GetHashCode() ?? 0", false)] public static int GetHashCode(object obj) { if (IsNull(obj)) @@ -507,7 +585,9 @@ public static int GetHashCode(object obj) /// /// 判断对象是否为值类型 + /// [Obsolete("请直接使用 obj?.GetType().IsValueType ?? false")] /// + [Obsolete("请直接使用 obj?.GetType().IsValueType ?? false", false)] public static bool IsValueType(object obj) { if (IsNull(obj)) @@ -668,7 +748,9 @@ public static void CopyProperties(object source, object target) /// /// 获取指定类型的 Type 对象 + /// [Obsolete("请直接使用 Type.GetType(typeName)")] /// + [Obsolete("请直接使用 Type.GetType(typeName)", false)] public static Type GetType(string typeName) { return Type.GetType(typeName); @@ -676,7 +758,9 @@ public static Type GetType(string typeName) /// /// 获取对象的 Type 对象 + /// [Obsolete("请直接使用 obj.GetType()")] /// + [Obsolete("请直接使用 obj.GetType()", false)] public static Type GetType(object obj) { return obj.GetType(); @@ -684,7 +768,9 @@ public static Type GetType(object obj) /// /// 获取类型的所有成员信息,包括字段、属性、方法和事件等 + /// [Obsolete("请直接使用 type.GetMembers()")] /// + [Obsolete("请直接使用 type.GetMembers()", false)] public static MemberInfo[] GetMembers(Type type) { return type.GetMembers(); @@ -692,7 +778,9 @@ public static MemberInfo[] GetMembers(Type type) /// /// 获取类型的所有属性信息 + /// [Obsolete("请直接使用 type.GetProperties()")] /// + [Obsolete("请直接使用 type.GetProperties()", false)] public static PropertyInfo[] GetProperties(Type type) { return type.GetProperties(); @@ -700,7 +788,9 @@ public static PropertyInfo[] GetProperties(Type type) /// /// 获取类型的所有字段信息 + /// [Obsolete("请直接使用 type.GetFields()")] /// + [Obsolete("请直接使用 type.GetFields()", false)] public static FieldInfo[] GetFields(Type type) { return type.GetFields(); @@ -708,7 +798,9 @@ public static FieldInfo[] GetFields(Type type) /// /// 获取指定名称的属性信息 + /// [Obsolete("请直接使用 type.GetProperty(propertyName)")] /// + [Obsolete("请直接使用 type.GetProperty(propertyName)", false)] public static PropertyInfo GetProperty(Type type, string propertyName) { return type.GetProperty(propertyName); @@ -716,7 +808,9 @@ public static PropertyInfo GetProperty(Type type, string propertyName) /// /// 获取指定名称的属性信息 + /// [Obsolete("请直接使用 obj.GetType().GetProperty(propertyName)")] /// + [Obsolete("请直接使用 obj.GetType().GetProperty(propertyName)", false)] public static PropertyInfo GetProperty(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName); @@ -724,7 +818,9 @@ public static PropertyInfo GetProperty(object obj, string propertyName) /// /// 获取指定名称的字段信息 + /// [Obsolete("请直接使用 type.GetField(fieldName)")] /// + [Obsolete("请直接使用 type.GetField(fieldName)", false)] public static FieldInfo GetField(Type type, string fieldName) { return type.GetField(fieldName); @@ -732,7 +828,9 @@ public static FieldInfo GetField(Type type, string fieldName) /// /// 获取指定名称的字段信息 + /// [Obsolete("请直接使用 obj.GetType().GetField(fieldName)")] /// + [Obsolete("请直接使用 obj.GetType().GetField(fieldName)", false)] public static FieldInfo GetField(object obj, string fieldName) { return obj.GetType().GetField(fieldName); @@ -740,7 +838,9 @@ public static FieldInfo GetField(object obj, string fieldName) /// /// 获取指定名称的方法信息 + /// [Obsolete("请直接使用 type.GetMethod(methodName)")] /// + [Obsolete("请直接使用 type.GetMethod(methodName)", false)] public static MethodInfo GetMethod(Type type, string methodName) { return type.GetMethod(methodName); @@ -748,7 +848,9 @@ public static MethodInfo GetMethod(Type type, string methodName) /// /// 获取指定名称的方法信息 + /// [Obsolete("请直接使用 obj.GetType().GetMethod(methodName)")] /// + [Obsolete("请直接使用 obj.GetType().GetMethod(methodName)", false)] public static MethodInfo GetMethod(object obj, string methodName) { return obj.GetType().GetMethod(methodName); @@ -756,7 +858,9 @@ public static MethodInfo GetMethod(object obj, string methodName) /// /// 获取指定名称和参数类型的方法信息 + /// [Obsolete("请直接使用 type.GetMethod(methodName, parameterTypes)")] /// + [Obsolete("请直接使用 type.GetMethod(methodName, parameterTypes)", false)] public static MethodInfo GetMethod(Type type, string methodName, Type[] parameterTypes) { return type.GetMethod(methodName, parameterTypes); @@ -764,7 +868,9 @@ public static MethodInfo GetMethod(Type type, string methodName, Type[] paramete /// /// 获取指定名称和参数类型的方法信息 + /// [Obsolete("请直接使用 obj.GetType().GetMethod(methodName, parameterTypes)")] /// + [Obsolete("请直接使用 obj.GetType().GetMethod(methodName, parameterTypes)", false)] public static MethodInfo GetMethod(object obj, string methodName, Type[] parameterTypes) { return obj.GetType().GetMethod(methodName, parameterTypes); @@ -792,7 +898,9 @@ public static object InvokeMethod(object obj, string methodName, Type[] paramete /// /// 创建指定类型的实例 + /// [Obsolete("请直接使用 Activator.CreateInstance(type, constructorParameters)")] /// + [Obsolete("请直接使用 Activator.CreateInstance(type, constructorParameters)", false)] public static object CreateInstance(Type type, object[] constructorParameters) { return Activator.CreateInstance(type, constructorParameters); @@ -801,7 +909,9 @@ public static object CreateInstance(Type type, object[] constructorParameters) /// /// 判断指定类型是否派生自指定的基类或接口 + /// [Obsolete("请直接使用 type.IsSubclassOf(baseType)")] /// + [Obsolete("请直接使用 type.IsSubclassOf(baseType)", false)] public static bool IsSubclassOf(Type type, Type baseType) { return type.IsSubclassOf(baseType); @@ -820,7 +930,9 @@ public static Type[] GetSubclassesOf(Type baseType) /// /// 获取指定类型实现的所有接口类型 + /// [Obsolete("请直接使用 type.GetInterfaces()")] /// + [Obsolete("请直接使用 type.GetInterfaces()", false)] public static Type[] GetInterfaces(Type type) { return type.GetInterfaces(); @@ -828,7 +940,9 @@ public static Type[] GetInterfaces(Type type) /// /// 获取指定类型的程序集限定名 + /// [Obsolete("请直接使用 type.AssemblyQualifiedName")] /// + [Obsolete("请直接使用 type.AssemblyQualifiedName", false)] public static string GetAssemblyQualifiedName(Type type) { return type.AssemblyQualifiedName; @@ -852,7 +966,9 @@ public static bool IsDefaultValue(object obj) /// /// 判断指定类型是否是可空类型 + /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) != null")] /// + [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) != null", false)] public static bool IsNullable(Type type) { return Nullable.GetUnderlyingType(type) != null; @@ -860,7 +976,9 @@ public static bool IsNullable(Type type) /// /// 获取可空类型的基础类型 + /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type")] /// + [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type", false)] public static Type GetNullableType(Type type) { return Nullable.GetUnderlyingType(type) ?? type; @@ -944,7 +1062,9 @@ public static bool IsDateTimeType(Type type) /// /// 判断指定类型是否是枚举类型 + /// [Obsolete("请直接使用 type.IsEnum")] /// + [Obsolete("请直接使用 type.IsEnum", false)] public static bool IsEnumType(Type type) { return type.IsEnum; diff --git a/EasyTool.Core/ToolCategory/ProcessUtil.cs b/EasyTool.Core/ToolCategory/ProcessUtil.cs index 210f34d..63a9d8b 100644 --- a/EasyTool.Core/ToolCategory/ProcessUtil.cs +++ b/EasyTool.Core/ToolCategory/ProcessUtil.cs @@ -29,9 +29,11 @@ public static Process GetProcessByName(string processName) /// /// 获取进程的所有线程 + /// [Obsolete("请直接使用 process.Threads")] /// /// 进程 /// 线程集合 + [Obsolete("请直接使用 process.Threads", false)] public static ProcessThreadCollection GetProcessThreads(Process process) { return process.Threads; @@ -39,9 +41,11 @@ public static ProcessThreadCollection GetProcessThreads(Process process) /// /// 获取进程的主窗口句柄 + /// [Obsolete("请直接使用 process.MainWindowHandle")] /// /// 进程 /// 窗口句柄 + [Obsolete("请直接使用 process.MainWindowHandle", false)] public static IntPtr GetMainWindowHandle(Process process) { return process.MainWindowHandle; @@ -49,9 +53,11 @@ public static IntPtr GetMainWindowHandle(Process process) /// /// 获取进程的主窗口标题 + /// [Obsolete("请直接使用 process.MainWindowTitle")] /// /// 进程 /// 窗口标题 + [Obsolete("请直接使用 process.MainWindowTitle", false)] public static string GetMainWindowTitle(Process process) { return process.MainWindowTitle; @@ -59,9 +65,11 @@ public static string GetMainWindowTitle(Process process) /// /// 获取进程的所有模块 + /// [Obsolete("请直接使用 process.Modules")] /// /// 进程 /// 模块集合 + [Obsolete("请直接使用 process.Modules", false)] public static ProcessModuleCollection GetProcessModules(Process process) { return process.Modules; @@ -69,8 +77,10 @@ public static ProcessModuleCollection GetProcessModules(Process process) /// /// 关闭进程 + /// [Obsolete("请直接使用 process.Kill()")] /// /// 进程 + [Obsolete("请直接使用 process.Kill()", false)] public static void KillProcess(Process process) { process.Kill(); @@ -88,9 +98,11 @@ public static void KillProcessAndWait(Process process) /// /// 启动新进程 + /// [Obsolete("请直接使用 Process.Start(fileName)")] /// /// 文件名 /// 新进程 + [Obsolete("请直接使用 Process.Start(fileName)", false)] public static Process StartProcess(string fileName) { return Process.Start(fileName); @@ -118,9 +130,11 @@ public static bool IsProcessExists(string processName) /// /// 获取进程使用的内存大小 + /// [Obsolete("请直接使用 process.WorkingSet64")] /// /// 进程 /// 内存大小(字节) + [Obsolete("请直接使用 process.WorkingSet64", false)] public static long GetProcessMemorySize(Process process) { return process.WorkingSet64; diff --git a/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs b/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs new file mode 100644 index 0000000..5bf41fb --- /dev/null +++ b/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs @@ -0,0 +1,390 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; + +namespace EasyTool.Extension +{ + /// + /// PropertyInfo 扩展方法 + /// + public static class PropertyInfoExtension + { + #region 值获取 + + /// + /// 安全获取属性值,失败返回默认值 + /// + public static object? GetValueOrDefault(this PropertyInfo? property, object? obj) + { + if (property == null || obj == null) + return null; + + try + { + return property.GetValue(obj); + } + catch + { + return null; + } + } + + /// + /// 安全获取属性值,失败返回指定默认值 + /// + public static T? GetValueOrDefault(this PropertyInfo? property, object? obj, T? defaultValue = default) + { + if (property == null || obj == null) + return defaultValue; + + try + { + var value = property.GetValue(obj); + if (value == null) + return defaultValue; + + return (T)value; + } + catch + { + return defaultValue; + } + } + + #endregion + + #region 值设置 + + /// + /// 安全设置属性值 + /// + public static bool SetValueSafe(this PropertyInfo? property, object? obj, object? value) + { + if (property == null || obj == null) + return false; + + try + { + if (!property.CanWrite) + return false; + + property.SetValue(obj, value); + return true; + } + catch + { + return false; + } + } + + /// + /// 设置属性值(支持类型转换) + /// + public static bool SetValueWithConvert(this PropertyInfo? property, object? obj, object? value) + { + if (property == null || obj == null || !property.CanWrite) + return false; + + try + { + object? convertedValue = value; + + // 如果类型不匹配,尝试转换 + if (value != null && value.GetType() != property.PropertyType) + { + // 处理可空类型 + var targetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; + + if (targetType.IsEnum && value is string str) + { + convertedValue = Enum.Parse(targetType, str); + } + else + { + convertedValue = Convert.ChangeType(value, targetType); + } + } + + property.SetValue(obj, convertedValue); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 特性检查 + + /// + /// 判断属性是否有指定特性 + /// + public static bool HasAttribute(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return false; + + return property.GetCustomAttribute() != null; + } + + /// + /// 判断属性是否有指定特性 + /// + public static bool HasAttribute(this PropertyInfo? property, Type? attributeType) + { + if (property == null || attributeType == null) + return false; + + return property.GetCustomAttributes(attributeType, false).Any(); + } + + /// + /// 获取属性特性 + /// + public static T? GetAttribute(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return null; + + return property.GetCustomAttribute(); + } + + /// + /// 获取属性的所有特性 + /// + public static T[] GetAttributes(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return Array.Empty(); + + return property.GetCustomAttributes().ToArray(); + } + + #endregion + + #region DataAnnotations 特性快捷访问 + + /// + /// 判断是否是必填项 + /// + public static bool IsRequired(this PropertyInfo? property) + { + return property.HasAttribute(); + } + + /// + /// 获取显示名称 + /// + public static string GetDisplayName(this PropertyInfo? property) + { + var displayAttr = property.GetAttribute(); + if (displayAttr != null && !string.IsNullOrEmpty(displayAttr.GetName())) + return displayAttr.GetName()!; + + var displayNameAttr = property.GetAttribute(); + if (displayNameAttr != null && !string.IsNullOrEmpty(displayNameAttr.DisplayName)) + return displayNameAttr.DisplayName; + + return property?.Name ?? string.Empty; + } + + /// + /// 获取描述 + /// + public static string GetDescription(this PropertyInfo? property) + { + var descriptionAttr = property.GetAttribute(); + if (descriptionAttr != null && !string.IsNullOrEmpty(descriptionAttr.Description)) + return descriptionAttr.Description; + + var displayAttr = property.GetAttribute(); + if (displayAttr != null && !string.IsNullOrEmpty(displayAttr.GetDescription())) + return displayAttr.GetDescription()!; + + return string.Empty; + } + + /// + /// 获取字符串长度限制 + /// + public static int GetStringLength(this PropertyInfo? property) + { + var attr = property.GetAttribute(); + return attr?.MaximumLength ?? 0; + } + + /// + /// 获取数据类型 + /// + public static string GetDataType(this PropertyInfo? property) + { + var attr = property.GetAttribute(); + return attr?.DataType.ToString() ?? string.Empty; + } + + #endregion + + #region 类型判断 + + /// + /// 判断是否是字符串类型 + /// + public static bool IsString(this PropertyInfo? property) + { + return property?.PropertyType == typeof(string); + } + + /// + /// 判断是否是数值类型 + /// + public static bool IsNumeric(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(byte) || + type == typeof(sbyte) || + type == typeof(short) || + type == typeof(ushort) || + type == typeof(int) || + type == typeof(uint) || + type == typeof(long) || + type == typeof(ulong) || + type == typeof(float) || + type == typeof(double) || + type == typeof(decimal); + } + + /// + /// 判断是否是日期类型 + /// + public static bool IsDateTime(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(DateTime) || type == typeof(DateTimeOffset); + } + + /// + /// 判断是否是布尔类型 + /// + public static bool IsBoolean(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(bool); + } + + /// + /// 判断是否是枚举类型 + /// + public static bool IsEnum(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + return type?.IsEnum == true; + } + + /// + /// 判断是否是集合类型 + /// + public static bool IsCollection(this PropertyInfo? property) + { + if (property == null) + return false; + + return typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType) && + property.PropertyType != typeof(string); + } + + /// + /// 判断是否是可空类型 + /// + public static bool IsNullable(this PropertyInfo? property) + { + if (property == null) + return false; + + return Nullable.GetUnderlyingType(property.PropertyType) != null; + } + + #endregion + + #region 访问判断 + + /// + /// 判断是否可读 + /// + public static bool CanRead(this PropertyInfo? property) + { + return property?.CanRead == true; + } + + /// + /// 判断是否可写 + /// + public static bool CanWrite(this PropertyInfo? property) + { + return property?.CanWrite == true; + } + + /// + /// 判断是否有公共的 getter + /// + public static bool HasPublicGetter(this PropertyInfo? property) + { + return property?.CanRead == true && property.GetGetMethod(false) != null; + } + + /// + /// 判断是否有公共的 setter + /// + public static bool HasPublicSetter(this PropertyInfo? property) + { + return property?.CanWrite == true && property.GetSetMethod(false) != null; + } + + #endregion + + #region 获取元素类型 + + /// + /// 获取集合的元素类型 + /// + public static Type? GetElementType(this PropertyInfo? property) + { + if (property == null) + return null; + + var type = property.PropertyType; + + // 处理数组 + if (type.IsArray) + return type.GetElementType(); + + // 处理泛型集合 + if (type.IsGenericType) + { + var genericType = type.GetGenericTypeDefinition(); + if (genericType == typeof(System.Collections.Generic.IEnumerable<>) || + genericType == typeof(System.Collections.Generic.List<>) || + genericType == typeof(System.Collections.Generic.IList<>) || + genericType == typeof(System.Collections.Generic.ICollection<>)) + { + return type.GetGenericArguments()[0]; + } + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/ReflectUtil.cs b/EasyTool.Core/ToolCategory/ReflectUtil.cs index a2a6374..2226924 100644 --- a/EasyTool.Core/ToolCategory/ReflectUtil.cs +++ b/EasyTool.Core/ToolCategory/ReflectUtil.cs @@ -11,9 +11,11 @@ public class ReflectUtil { /// /// 根据类型名称获取Type对象 + /// [Obsolete("请直接使用 Type.GetType(typeName)")] /// /// 类型名称 /// Type对象 + [Obsolete("请直接使用 Type.GetType(typeName)", false)] public static Type GetType(string typeName) { return Type.GetType(typeName); @@ -21,9 +23,11 @@ public static Type GetType(string typeName) /// /// 获取指定程序集中的所有类型 + /// [Obsolete("请直接使用 assembly.GetTypes()")] /// /// 程序集 /// 类型数组 + [Obsolete("请直接使用 assembly.GetTypes()", false)] public static Type[] GetTypes(Assembly assembly) { return assembly.GetTypes(); @@ -31,9 +35,11 @@ public static Type[] GetTypes(Assembly assembly) /// /// 获取指定类型所在的程序集 + /// [Obsolete("请直接使用 type.Assembly")] /// /// 类型 /// 程序集 + [Obsolete("请直接使用 type.Assembly", false)] public static Assembly GetAssembly(Type type) { return type.Assembly; @@ -41,10 +47,12 @@ public static Assembly GetAssembly(Type type) /// /// 获取指定类型的指定类型的特性 + /// [Obsolete("请直接使用 type.GetCustomAttribute()")] /// /// 特性类型 /// 类型 /// 特性对象 + [Obsolete("请直接使用 type.GetCustomAttribute()", false)] public static T GetAttribute(Type type) where T : Attribute { return type.GetCustomAttribute(); @@ -63,9 +71,11 @@ public static T[] GetAttributes(Type type) where T : Attribute /// /// 获取指定类型的默认值 + /// [Obsolete("请直接使用 type.IsValueType ? Activator.CreateInstance(type) : null")] /// /// 类型 /// 默认值 + [Obsolete("请直接使用 type.IsValueType ? Activator.CreateInstance(type) : null", false)] public static object GetDefaultValue(Type type) { return type.IsValueType ? Activator.CreateInstance(type) : null; @@ -73,9 +83,11 @@ public static object GetDefaultValue(Type type) /// /// 获取类型的基类 + /// [Obsolete("请直接使用 type.BaseType")] /// /// 类型 /// 基类 + [Obsolete("请直接使用 type.BaseType", false)] public static Type GetBaseType(Type type) { return type.BaseType; @@ -83,10 +95,12 @@ public static Type GetBaseType(Type type) /// /// 判断类型是否实现了某个接口 + /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)")] /// /// 类型 /// 接口类型 /// 是否实现 + [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)", false)] public static bool HasInterface(Type type, Type interfaceType) { return interfaceType.IsAssignableFrom(type); @@ -94,9 +108,11 @@ public static bool HasInterface(Type type, Type interfaceType) /// /// 获取方法的参数信息 + /// [Obsolete("请直接使用 method.GetParameters()")] /// /// 方法 /// 参数信息数组 + [Obsolete("请直接使用 method.GetParameters()", false)] public static ParameterInfo[] GetParameters(MethodInfo method) { return method.GetParameters(); @@ -154,9 +170,11 @@ public static EventInfo[] GetEvents(Type type) /// /// 获取类型的所有接口 + /// [Obsolete("请直接使用 type.GetInterfaces()")] /// /// 类型 /// 接口数组 + [Obsolete("请直接使用 type.GetInterfaces()", false)] public static Type[] GetInterfaces(Type type) { return type.GetInterfaces(); diff --git a/EasyTool.Core/ToolCategory/RegexUtil.cs b/EasyTool.Core/ToolCategory/RegexUtil.cs index 4694e9a..5f5a1d5 100644 --- a/EasyTool.Core/ToolCategory/RegexUtil.cs +++ b/EasyTool.Core/ToolCategory/RegexUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -13,10 +13,12 @@ public class RegexUtil { /// /// 验证字符串是否与指定的正则表达式匹配 + /// [Obsolete("请直接使用 Regex.IsMatch(input, pattern)")] /// /// 要验证的字符串 /// 正则表达式 /// 如果字符串与正则表达式匹配,则为true;否则为false + [Obsolete("请直接使用 Regex.IsMatch(input, pattern)", false)] public static bool IsMatch(string input, string pattern) { return Regex.IsMatch(input, pattern); @@ -28,7 +30,7 @@ public static bool IsMatch(string input, string pattern) /// 要验证的字符串 /// 正则表达式 /// 如果字符串与正则表达式匹配,则为匹配结果;否则为null - public static string Match(string input, string pattern) + public static string? Match(string input, string pattern) { Match match = Regex.Match(input, pattern); return match.Success ? match.Value : null; @@ -47,11 +49,13 @@ public static string[] Matches(string input, string pattern) /// /// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串 + /// [Obsolete("请直接使用 Regex.Replace(input, pattern, replacement)")] /// /// 要替换的字符串 /// 正则表达式 /// 替换字符串 /// 替换后的字符串 + [Obsolete("请直接使用 Regex.Replace(input, pattern, replacement)", false)] public static string Replace(string input, string pattern, string replacement) { return Regex.Replace(input, pattern, replacement); @@ -65,7 +69,7 @@ public static string Replace(string input, string pattern, string replacement) /// 替换字符串 /// 替换次数 /// 包含替换后的字符串和替换次数的元组 - public static (string, int) Replace(string input, string pattern, string replacement, int count) + public static (string?, int) Replace(string input, string pattern, string replacement, int count) { string result = Regex.Replace(input, pattern, replacement, RegexOptions.None, TimeSpan.FromSeconds(1)); return (result, Regex.Matches(input, pattern).Count); @@ -77,7 +81,7 @@ public static (string, int) Replace(string input, string pattern, string replace /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果的字符串数组 - public static string[] MatchGroups(string input, string pattern) + public static string[]? MatchGroups(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -96,7 +100,7 @@ public static string[] MatchGroups(string input, string pattern) /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果及分组名称的字典 - public static Dictionary MatchGroupsWithNames(string input, string pattern) + public static Dictionary? MatchGroupsWithNames(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -115,7 +119,7 @@ public static Dictionary MatchGroupsWithNames(string input, stri /// 要验证的字符串 /// 正则表达式 /// 匹配结果和捕获组名称的字典 - public static Dictionary MatchWithGroupNames(string input, string pattern) + public static Dictionary? MatchWithGroupNames(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -134,7 +138,7 @@ public static Dictionary MatchWithGroupNames(string input, strin /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果及分组名称的元组 - public static (string, Dictionary) MatchGroupsWithNamesTuple(string input, string pattern) + public static (string?, Dictionary?) MatchGroupsWithNamesTuple(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) diff --git a/EasyTool.Core/ToolCategory/RuntimeUtil.cs b/EasyTool.Core/ToolCategory/RuntimeUtil.cs index eb37da6..eea6036 100644 --- a/EasyTool.Core/ToolCategory/RuntimeUtil.cs +++ b/EasyTool.Core/ToolCategory/RuntimeUtil.cs @@ -16,8 +16,10 @@ public class RuntimeUtil { /// /// 获取当前运行的 .NET 版本 + /// [Obsolete("请直接使用 Environment.Version.ToString()")] /// /// .NET 版本 + [Obsolete("请直接使用 Environment.Version.ToString()", false)] public static string GetDotNetVersion() { return Environment.Version.ToString(); @@ -25,8 +27,10 @@ public static string GetDotNetVersion() /// /// 获取当前操作系统版本 + /// [Obsolete("请直接使用 Environment.OSVersion.ToString()")] /// /// 操作系统版本 + [Obsolete("请直接使用 Environment.OSVersion.ToString()", false)] public static string GetOSVersion() { return Environment.OSVersion.ToString(); diff --git a/EasyTool.Core/ToolCategory/StrExtension.cs b/EasyTool.Core/ToolCategory/StrExtension.cs index 651a010..20933be 100644 --- a/EasyTool.Core/ToolCategory/StrExtension.cs +++ b/EasyTool.Core/ToolCategory/StrExtension.cs @@ -1,21 +1,337 @@ -using System.Text.RegularExpressions; +using System; +using System.Globalization; +using System.Text; +using System.Text.RegularExpressions; namespace EasyTool.Extension { + /// + /// 字符串扩展方法 + /// public static class StrExtension { #region 文本可为空判断 + /// + /// 判断字符串是否为 null 或空 + /// [Obsolete("请直接使用 string.IsNullOrEmpty(value)")] + /// + [Obsolete("请直接使用 string.IsNullOrEmpty(value)", false)] public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } + /// + /// 判断字符串是否为 null 或空白字符 + /// [Obsolete("请直接使用 string.IsNullOrWhiteSpace(value)")] + /// + [Obsolete("请直接使用 string.IsNullOrWhiteSpace(value)", false)] public static bool IsNullOrWhiteSpace(this string value) { return string.IsNullOrWhiteSpace(value); } #endregion + + #region 字符串验证 + + /// + /// 判断字符串是否是有效的电子邮件地址 + /// + public static bool IsEmail(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的手机号(中国大陆) + /// + public static bool IsPhoneNumber(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + // 中国大陆手机号:1开头,11位数字 + const string pattern = @"^1[3-9]\d{9}$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的 URL + /// + public static bool IsUrl(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^(https?|ftp)://[^\s/$.?#].[^\s]*$"; + return Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase); + } + + /// + /// 判断字符串是否是有效的 IPv4 地址 + /// + public static bool IsIPv4(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的身份证号(中国大陆) + /// + public static bool IsIdCard(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + // 18位身份证号码 + const string pattern = @"^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"; + return Regex.IsMatch(value, pattern); + } + + #endregion + + #region 字符串转换 + + /// + /// 将字符串转换为 Base64 编码 + /// + public static string ToBase64(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Encoding.UTF8.GetBytes(value); + return Convert.ToBase64String(bytes); + } + + /// + /// 将 Base64 编码的字符串解码 + /// + public static string FromBase64(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Convert.FromBase64String(value); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 计算字符串的 MD5 哈希值 + /// + public static string ToMd5(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + using var md5 = System.Security.Cryptography.MD5.Create(); + var bytes = Encoding.UTF8.GetBytes(value); + var hash = md5.ComputeHash(bytes); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 计算字符串的 SHA256 哈希值 + /// + public static string ToSha256(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + using var sha256 = System.Security.Cryptography.SHA256.Create(); + var bytes = Encoding.UTF8.GetBytes(value); + var hash = sha256.ComputeHash(bytes); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 将字符串转换为16进制表示 + /// + public static string ToHex(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Encoding.UTF8.GetBytes(value); + return BitConverter.ToString(bytes).Replace("-", "").ToLowerInvariant(); + } + + #endregion + + #region 字符串处理 + + /// + /// 截断字符串到指定长度,超出部分用省略号代替 + /// + /// 原始字符串 + /// 最大长度 + /// 后缀,默认为"..." + public static string Truncate(this string value, int maxLength, string suffix = "...") + { + if (string.IsNullOrEmpty(value) || value.Length <= maxLength) + return value; + + return value.Substring(0, maxLength) + suffix; + } + + /// + /// 移除字符串中的音调符号(如 é -> e) + /// + public static string RemoveDiacritics(this string value) + { + if (string.IsNullOrEmpty(value)) + return value; + + var normalizedString = value.Normalize(NormalizationForm.FormD); + var sb = new StringBuilder(); + + foreach (var c in normalizedString) + { + var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); + if (unicodeCategory != UnicodeCategory.NonSpacingMark) + { + sb.Append(c); + } + } + + return sb.ToString().Normalize(NormalizationForm.FormC); + } + + /// + /// 生成 URL 友好的 slug(例如:"Hello World" -> "hello-world") + /// + public static string GenerateSlug(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + // 移除音调符号 + var slug = value.RemoveDiacritics(); + + // 转换为小写 + slug = slug.ToLowerInvariant(); + + // 替换空格和特殊字符为连字符 + slug = Regex.Replace(slug, @"[^a-z0-9\s-]", ""); + slug = Regex.Replace(slug, @"\s+", "-"); + slug = Regex.Replace(slug, @"-+", "-"); + slug = slug.Trim('-'); + + return slug; + } + + /// + /// 反转字符串 + /// [Obsolete("请直接使用 new string(value.Reverse().ToArray()) 或通过 LINQ")] + /// + [Obsolete("请直接使用 new string(value.Reverse().ToArray())", false)] + public static string Reverse(this string value) + { + if (string.IsNullOrEmpty(value)) + return value; + + var charArray = value.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + /// + /// 获取字符串的字节数(UTF-8编码) + /// [Obsolete("请直接使用 Encoding.UTF8.GetByteCount(value)")] + /// + [Obsolete("请直接使用 Encoding.UTF8.GetByteCount(value)", false)] + public static int GetByteCount(this string value) + { + if (string.IsNullOrEmpty(value)) + return 0; + + return Encoding.UTF8.GetByteCount(value); + } + + /// + /// 隐藏字符串的中间部分(例如:手机号、身份证号) + /// + /// 原始字符串 + /// 开头保留字符数 + /// 结尾保留字符数 + /// 掩码字符,默认为'*' + public static string Mask(this string value, int visibleStart = 3, int visibleEnd = 4, char maskChar = '*') + { + if (string.IsNullOrEmpty(value)) + return value; + + if (value.Length <= visibleStart + visibleEnd) + return value; + + var start = value.Substring(0, visibleStart); + var end = value.Substring(value.Length - visibleEnd); + var maskLength = value.Length - visibleStart - visibleEnd; + var mask = new string(maskChar, maskLength); + + return start + mask + end; + } + + #endregion + + #region 字符串操作 + + /// + /// 移除字符串中指定的字符 + /// + public static string RemoveChars(this string value, params char[] charsToRemove) + { + if (string.IsNullOrEmpty(value) || charsToRemove == null || charsToRemove.Length == 0) + return value; + + var result = new StringBuilder(value.Length); + foreach (var c in value) + { + if (Array.IndexOf(charsToRemove, c) < 0) + { + result.Append(c); + } + } + return result.ToString(); + } + + /// + /// 确保字符串以指定后缀结尾 + /// + public static string EnsureEndsWith(this string value, string suffix) + { + if (string.IsNullOrEmpty(value)) + return suffix ?? string.Empty; + + if (string.IsNullOrEmpty(suffix)) + return value; + + return value.EndsWith(suffix) ? value : value + suffix; + } + + /// + /// 确保字符串以指定前缀开头 + /// + public static string EnsureStartsWith(this string value, string prefix) + { + if (string.IsNullOrEmpty(value)) + return prefix ?? string.Empty; + + if (string.IsNullOrEmpty(prefix)) + return value; + + return value.StartsWith(prefix) ? value : prefix + value; + } + + #endregion } } diff --git a/EasyTool.Core/ToolCategory/StrUtil.cs b/EasyTool.Core/ToolCategory/StrUtil.cs index 938649c..aaaf9be 100644 --- a/EasyTool.Core/ToolCategory/StrUtil.cs +++ b/EasyTool.Core/ToolCategory/StrUtil.cs @@ -22,11 +22,13 @@ public static string RemoveAllSpaces(string str) /// /// 将字符串中的指定字符替换成新的字符 + /// [Obsolete("请直接使用 str.Replace(oldChar, newChar)")] /// /// 要处理的字符串 /// 要替换的字符 /// 新的字符 /// 处理后的字符串 + [Obsolete("请直接使用 str.Replace(oldChar, newChar)", false)] public static string ReplaceChar(string str, char oldChar, char newChar) { return str.Replace(oldChar, newChar); @@ -67,9 +69,11 @@ public static bool IsDate(string str) /// /// 获取字符串的字节数组 + /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)")] /// /// 要处理的字符串 /// 字符串的字节数组 + [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)", false)] public static byte[] GetBytes(string str) { return System.Text.Encoding.UTF8.GetBytes(str); @@ -77,9 +81,11 @@ public static byte[] GetBytes(string str) /// /// 将字节数组转换为字符串 + /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] /// /// 要处理的字节数组 /// 字节数组转换后的字符串 + [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] public static string GetString(byte[] bytes) { return System.Text.Encoding.UTF8.GetString(bytes); @@ -87,9 +93,11 @@ public static string GetString(byte[] bytes) /// /// 将字符串转换为大写 + /// [Obsolete("请直接使用 str.ToUpper()")] /// /// 要处理的字符串 /// 处理后的字符串 + [Obsolete("请直接使用 str.ToUpper()", false)] public static string ToUpperCase(string str) { return str.ToUpper(); @@ -97,9 +105,11 @@ public static string ToUpperCase(string str) /// /// 将字符串转换为小写 + /// [Obsolete("请直接使用 str.ToLower()")] /// /// 要处理的字符串 /// 处理后的字符串 + [Obsolete("请直接使用 str.ToLower()", false)] public static string ToLowerCase(string str) { return str.ToLower(); @@ -107,9 +117,11 @@ public static string ToLowerCase(string str) /// /// 检查字符串是否为空或null + /// [Obsolete("请直接使用 string.IsNullOrEmpty(str)")] /// /// 要检查的字符串 /// 如果是空或null,则返回true,否则返回false + [Obsolete("请直接使用 string.IsNullOrEmpty(str)", false)] public static bool IsNullOrEmpty(string str) { return string.IsNullOrEmpty(str); @@ -117,9 +129,11 @@ public static bool IsNullOrEmpty(string str) /// /// 检查字符串是否为空或仅由空格组成 + /// [Obsolete("请直接使用 string.IsNullOrWhiteSpace(str)")] /// /// 要检查的字符串 /// 如果是空或仅由空格组成,则返回true,否则返回false + [Obsolete("请直接使用 string.IsNullOrWhiteSpace(str)", false)] public static bool IsNullOrWhiteSpace(string str) { return string.IsNullOrWhiteSpace(str); @@ -127,11 +141,13 @@ public static bool IsNullOrWhiteSpace(string str) /// /// 截取字符串的指定部分 + /// [Obsolete("请直接使用 str.Substring(startIndex, length)")] /// /// 要处理的字符串 /// 起始位置(从0开始) /// 要截取的长度 /// 截取后的字符串 + [Obsolete("请直接使用 str.Substring(startIndex, length)", false)] public static string Substring(string str, int startIndex, int length) { return str.Substring(startIndex, length); @@ -245,11 +261,13 @@ public static bool EqualsIgnoreCaseAndWhiteSpace(string str1, string str2) /// /// 在字符串的左侧填充指定字符,使字符串达到指定长度 + /// [Obsolete("请直接使用 str.PadLeft(length, paddingChar)")] /// /// 要处理的字符串 /// 指定长度 /// 填充字符 /// 处理后的字符串 + [Obsolete("请直接使用 str.PadLeft(length, paddingChar)", false)] public static string PadLeft(string str, int length, char paddingChar) { return str.PadLeft(length, paddingChar); @@ -257,11 +275,13 @@ public static string PadLeft(string str, int length, char paddingChar) /// /// 在字符串的右侧填充指定字符,使字符串达到指定长度 + /// [Obsolete("请直接使用 str.PadRight(length, paddingChar)")] /// /// 要处理的字符串 /// 指定长度 /// 填充字符 /// 处理后的字符串 + [Obsolete("请直接使用 str.PadRight(length, paddingChar)", false)] public static string PadRight(string str, int length, char paddingChar) { return str.PadRight(length, paddingChar); diff --git a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs new file mode 100644 index 0000000..c5c3294 --- /dev/null +++ b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs @@ -0,0 +1,417 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.Extension +{ + /// + /// StringBuilder 扩展方法 + /// + public static class StringBuilderExtension + { + #region 追加操作 + + /// + /// 追加带格式的字符串(忽略 null 值) + /// + public static StringBuilder AppendFormatIfNotNull(this StringBuilder sb, string format, object? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (arg != null) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 追加带格式的字符串(忽略空字符串) + /// + public static StringBuilder AppendFormatIfNotEmpty(this StringBuilder sb, string format, string? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(arg)) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 追加带格式的字符串(忽略空白字符串) + /// + public static StringBuilder AppendFormatIfNotBlank(this StringBuilder sb, string format, string? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrWhiteSpace(arg)) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 条件追加字符串 + /// + public static StringBuilder AppendIf(this StringBuilder sb, string? value, bool condition) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (condition) + { + sb.Append(value); + } + return sb; + } + + /// + /// 条件追加字符串(带分隔符) + /// + public static StringBuilder AppendWithSeparator(this StringBuilder sb, string? value, string separator = ", ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (sb.Length > 0 && !string.IsNullOrEmpty(separator)) + { + sb.Append(separator); + } + sb.Append(value); + return sb; + } + + /// + /// 条件追加字符串(带分隔符,仅在非空时追加) + /// + public static StringBuilder AppendWithSeparatorIfNotEmpty(this StringBuilder sb, string? value, string separator = ", ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.AppendWithSeparator(value, separator); + } + return sb; + } + + /// + /// 追加行(仅当值非空时) + /// + public static StringBuilder AppendLineIfNotEmpty(this StringBuilder sb, string? value) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.AppendLine(value); + } + return sb; + } + + /// + /// 条件追加行 + /// + public static StringBuilder AppendLineIf(this StringBuilder sb, string? value, bool condition) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (condition) + { + sb.AppendLine(value); + } + return sb; + } + + #endregion + + #region 括号包裹 + + /// + /// 用括号包裹内容(如果非空) + /// + public static StringBuilder AppendInParentheses(this StringBuilder sb, string? value, string open = "(", string close = ")") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.Append(open).Append(value).Append(close); + } + return sb; + } + + /// + /// 用方括号包裹内容(如果非空) + /// + public static StringBuilder AppendInBrackets(this StringBuilder sb, string? value) + { + return sb.AppendInParentheses(value, "[", "]"); + } + + /// + /// 用花括号包裹内容(如果非空) + /// + public static StringBuilder AppendInBraces(this StringBuilder sb, string? value) + { + return sb.AppendInParentheses(value, "{", "}"); + } + + /// + /// 用引号包裹内容(如果非空) + /// + public static StringBuilder AppendInQuotes(this StringBuilder sb, string? value, string quote = "\"") + { + return sb.AppendInParentheses(value, quote, quote); + } + + #endregion + + #region 缩进操作 + + /// + /// 增加缩进 + /// + /// StringBuilder + /// 缩进字符串,默认为两个空格 + public static StringBuilder Indent(this StringBuilder sb, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + return sb.Append(indent); + } + + /// + /// 增加指定层数的缩进 + /// + public static StringBuilder Indent(this StringBuilder sb, int level, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + for (int i = 0; i < level; i++) + { + sb.Append(indent); + } + return sb; + } + + /// + /// 添加缩进行 + /// + public static StringBuilder AppendIndentedLine(this StringBuilder sb, string value, int level = 1, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + return sb.Indent(level, indent).AppendLine(value); + } + + #endregion + + #region 清除操作 + + /// + /// 清除最后 N 个字符 + /// + public static StringBuilder RemoveLast(this StringBuilder sb, int count) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (count > 0 && sb.Length >= count) + { + sb.Remove(sb.Length - count, count); + } + return sb; + } + + /// + /// 清除最后的一个字符 + /// + public static StringBuilder RemoveLastChar(this StringBuilder sb) + { + return sb.RemoveLast(1); + } + + /// + /// 清除最后指定的字符串(如果匹配) + /// + public static StringBuilder RemoveLastIf(this StringBuilder sb, string? value) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value) && sb.Length >= value.Length) + { + int startIndex = sb.Length - value.Length; + string end = sb.ToString(startIndex, value.Length); + if (end == value) + { + sb.Remove(startIndex, value.Length); + } + } + return sb; + } + + /// + /// 清除最后的空白字符 + /// + public static StringBuilder TrimEnd(this StringBuilder sb) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + while (sb.Length > 0 && char.IsWhiteSpace(sb[sb.Length - 1])) + { + sb.Remove(sb.Length - 1, 1); + } + return sb; + } + + /// + /// 清除开头的空白字符 + /// + public static StringBuilder TrimStart(this StringBuilder sb) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + int index = 0; + while (index < sb.Length && char.IsWhiteSpace(sb[index])) + { + index++; + } + + if (index > 0) + { + sb.Remove(0, index); + } + return sb; + } + + /// + /// 清除开头和结尾的空白字符 + /// + public static StringBuilder Trim(this StringBuilder sb) + { + return sb.TrimStart().TrimEnd(); + } + + #endregion + + #region 转换操作 + + /// + /// 转换为只读字符串 + /// [Obsolete("请直接使用 sb?.ToString() ?? string.Empty")] + /// + [Obsolete("请直接使用 sb?.ToString() ?? string.Empty", false)] + public static string ToReadOnly(this StringBuilder sb) + { + return sb?.ToString() ?? string.Empty; + } + + /// + /// 转换为 MemoryStream + /// + public static MemoryStream ToMemoryStream(this StringBuilder sb, Encoding? encoding = null) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + encoding ??= Encoding.UTF8; + var bytes = encoding.GetBytes(sb.ToString()); + return new MemoryStream(bytes); + } + + #endregion + + #region 检查操作 + + /// + /// 判断是否为空 + /// + public static bool IsNullOrEmpty(this StringBuilder? sb) + { + return sb == null || sb.Length == 0; + } + + /// + /// 判断是否包含指定字符串 + /// + public static bool Contains(this StringBuilder? sb, string? value) + { + if (sb == null) + return false; + + return sb.IndexOf(value) >= 0; + } + + /// + /// 查找字符串的位置 + /// + public static int IndexOf(this StringBuilder? sb, string? value, int startIndex = 0, bool ignoreCase = false) + { + if (sb == null || string.IsNullOrEmpty(value)) + return -1; + + if (startIndex < 0 || startIndex >= sb.Length) + return -1; + + var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + for (int i = startIndex; i <= sb.Length - value.Length; i++) + { + bool match = true; + for (int j = 0; j < value.Length; j++) + { + if (char.ToLower(sb[i + j]) != char.ToLower(value[j])) + { + match = false; + break; + } + } + + if (match) + return i; + } + + return -1; + } + + /// + /// 替换字符串 + /// + public static StringBuilder Replace(this StringBuilder? sb, string oldValue, string? newValue, bool ignoreCase = false) + { + if (sb == null || string.IsNullOrEmpty(oldValue)) + return sb ?? throw new ArgumentNullException(nameof(sb)); + + int index; + int searchIndex = 0; + + while ((index = sb.IndexOf(oldValue, searchIndex, ignoreCase)) >= 0) + { + sb.Remove(index, oldValue.Length); + sb.Insert(index, newValue); + searchIndex = index + (newValue?.Length ?? 0); + } + + return sb; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs new file mode 100644 index 0000000..1304e6b --- /dev/null +++ b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs @@ -0,0 +1,403 @@ +using System; +using System.Globalization; + +namespace EasyTool.Extension +{ + /// + /// String 字符串比较扩展方法 + /// + public static class StringComparisonExtension + { + #region 忽略大小写比较 + + /// + /// 忽略大小写判断字符串相等 + /// + public static bool EqualsIgnoreCase(this string? str, string? value) + { + return string.Equals(str, value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 忽略大小写判断字符串包含 + /// + public static bool ContainsIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return CultureInfo.InvariantCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase) >= 0; + } + + /// + /// 忽略大小写判断字符串是否以指定字符串开头 + /// + public static bool StartsWithIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return str.StartsWith(value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 忽略大小写判断字符串是否以指定字符串结尾 + /// + public static bool EndsWithIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return str.EndsWith(value, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 模糊匹配 + + /// + /// 判断字符串是否包含指定字符(忽略大小写) + /// + public static bool ContainsCharIgnoreCase(this string? str, char value) + { + if (string.IsNullOrEmpty(str)) + return false; + + return str.IndexOf(char.ToLower(value), StringComparison.OrdinalIgnoreCase) >= 0; + } + + /// + /// 判断字符串是否包含任意指定字符串 + /// + public static bool ContainsAny(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (str.Contains(value)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否包含任意指定字符串 + /// + public static bool ContainsAnyIgnoreCase(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (str.ContainsIgnoreCase(value)) + return true; + } + + return false; + } + + /// + /// 判断字符串是否包含所有指定字符串 + /// + public static bool ContainsAll(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (!str.Contains(value)) + return false; + } + + return true; + } + + /// + /// 忽略大小写判断字符串是否包含所有指定字符串 + /// + public static bool ContainsAllIgnoreCase(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (!str.ContainsIgnoreCase(value)) + return false; + } + + return true; + } + + #endregion + + #region 通配符匹配 + + /// + /// 使用通配符匹配字符串 + /// + public static bool Like(this string? str, string? pattern, bool ignoreCase = true) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(pattern)) + return false; + + var comparisonType = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + // 转换通配符模式为正则表达式 + var regexPattern = "^" + System.Text.RegularExpressions.Regex.Escape(pattern) + .Replace("\\*", ".*") + .Replace("\\?", ".") + "$"; + + var regex = new System.Text.RegularExpressions.Regex( + regexPattern, + ignoreCase ? System.Text.RegularExpressions.RegexOptions.IgnoreCase : System.Text.RegularExpressions.RegexOptions.None); + + return regex.IsMatch(str); + } + + #endregion + + #region 前缀/后缀检查 + + /// + /// 判断字符串是否以任意指定前缀开头 + /// + public static bool StartsWithAny(this string? str, params string?[] prefixes) + { + if (string.IsNullOrEmpty(str) || prefixes == null || prefixes.Length == 0) + return false; + + foreach (var prefix in prefixes) + { + if (!string.IsNullOrEmpty(prefix) && str.StartsWith(prefix)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否以任意指定前缀开头 + /// + public static bool StartsWithAnyIgnoreCase(this string? str, params string?[] prefixes) + { + if (string.IsNullOrEmpty(str) || prefixes == null || prefixes.Length == 0) + return false; + + foreach (var prefix in prefixes) + { + if (!string.IsNullOrEmpty(prefix) && str.StartsWithIgnoreCase(prefix)) + return true; + } + + return false; + } + + /// + /// 判断字符串是否以任意指定后缀结尾 + /// + public static bool EndsWithAny(this string? str, params string?[] suffixes) + { + if (string.IsNullOrEmpty(str) || suffixes == null || suffixes.Length == 0) + return false; + + foreach (var suffix in suffixes) + { + if (!string.IsNullOrEmpty(suffix) && str.EndsWith(suffix)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否以任意指定后缀结尾 + /// + public static bool EndsWithAnyIgnoreCase(this string? str, params string?[] suffixes) + { + if (string.IsNullOrEmpty(str) || suffixes == null || suffixes.Length == 0) + return false; + + foreach (var suffix in suffixes) + { + if (!string.IsNullOrEmpty(suffix) && str.EndsWithIgnoreCase(suffix)) + return true; + } + + return false; + } + + #endregion + + #region 字符串相似度 + + /// + /// 计算字符串相似度(0-1之间,1表示完全相同) + /// 使用 Levenshtein 距离算法 + /// + public static double Similarity(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) && string.IsNullOrEmpty(value)) + return 1; + + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return 0; + + int distance = LevenshteinDistance(str, value); + int maxLength = Math.Max(str.Length, value.Length); + + return 1 - (double)distance / maxLength; + } + + /// + /// 计算 Levenshtein 距离 + /// + private static int LevenshteinDistance(string str1, string str2) + { + int[,] distance = new int[str1.Length + 1, str2.Length + 1]; + + for (int i = 0; i <= str1.Length; i++) + distance[i, 0] = i; + + for (int j = 0; j <= str2.Length; j++) + distance[0, j] = j; + + for (int i = 1; i <= str1.Length; i++) + { + for (int j = 1; j <= str2.Length; j++) + { + int cost = str1[i - 1] == str2[j - 1] ? 0 : 1; + + distance[i, j] = Math.Min( + Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), + distance[i - 1, j - 1] + cost); + } + } + + return distance[str1.Length, str2.Length]; + } + + /// + /// 判断字符串相似度是否超过指定阈值 + /// + public static bool IsSimilarTo(this string? str, string? value, double threshold = 0.8) + { + return str.Similarity(value) >= threshold; + } + + #endregion + + #region 字符串比较 + + /// + /// 比较字符串(忽略大小写) + /// + public static int CompareToIgnoreCase(this string? str, string? value) + { + return string.Compare(str, value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 比较字符串(使用指定的文化信息) + /// + public static int CompareToCulture(this string? str, string? value, CultureInfo culture, bool ignoreCase = false) + { + return string.Compare(str, value, culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); + } + + #endregion + + #region 首字母大小写 + + /// + /// 首字母大写 + /// + public static string ToTitleCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return char.ToUpper(str[0]) + str.Substring(1); + } + + /// + /// 首字母小写 + /// + public static string ToCamelCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return char.ToLower(str[0]) + str.Substring(1); + } + + /// + /// 将字符串转换为标题格式(每个单词首字母大写) + /// + public static string ToTitleCase(this string str, CultureInfo? culture = null) + { + if (string.IsNullOrEmpty(str)) + return str; + + culture ??= CultureInfo.CurrentCulture; + return culture.TextInfo.ToTitleCase(str); + } + + #endregion + + #region 大小写转换 + + /// + /// 转换为大写(不变则为返回原字符串) + /// [Obsolete("请直接使用 value.ToUpper()")] + /// + [Obsolete("请直接使用 value.ToUpper()", false)] + public static string ToUpperSafe(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return str.ToUpper(); + } + + /// + /// 转换为小写(不变则为返回原字符串) + /// [Obsolete("请直接使用 value.ToLower()")] + /// + [Obsolete("请直接使用 value.ToLower()", false)] + public static string ToLowerSafe(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return str.ToLower(); + } + + /// + /// 转换为单词首字母大写(如:helloWorld -> HelloWorld) + /// + public static string ToPascalCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + var words = str.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < words.Length; i++) + { + if (words[i].Length > 0) + { + words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1).ToLower(); + } + } + + return string.Join("", words); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/TaskExtension.cs b/EasyTool.Core/ToolCategory/TaskExtension.cs new file mode 100644 index 0000000..c3e0312 --- /dev/null +++ b/EasyTool.Core/ToolCategory/TaskExtension.cs @@ -0,0 +1,381 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.Extension +{ + /// + /// Task 异步任务扩展方法 + /// + public static class TaskExtension + { + #region 任务忽略 + + /// + /// 忽略任务(Fire-and-forget),捕获并记录异常但不抛出 + /// + /// 要忽略的任务 + /// 异常回调(可选) + public static void Forget(this Task? task, Action? onException = null) + { + task?.ContinueWith(t => + { + if (t.IsFaulted && t.Exception != null) + { + onException?.Invoke(t.Exception); + } + }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); + } + + /// + /// 忽略任务(Fire-and-forget),捕获并记录异常但不抛出 + /// + /// 任务返回类型 + /// 要忽略的任务 + /// 异常回调(可选) + public static void Forget(this Task? task, Action? onException = null) + { + task?.ContinueWith(t => + { + if (t.IsFaulted && t.Exception != null) + { + onException?.Invoke(t.Exception); + } + }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); + } + + #endregion + + #region 超时处理 + + /// + /// 为任务添加超时处理 + /// + /// 原始任务 + /// 超时时间 + public static async Task OrTimeout(this Task task, TimeSpan timeout) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + + return await task; + } + + /// + /// 为任务添加超时处理 + /// + /// 原始任务 + /// 超时时间 + public static async Task OrTimeout(this Task task, TimeSpan timeout) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + + await task; + } + + /// + /// 为任务添加超时处理,超时返回默认值 + /// + /// 原始任务 + /// 超时时间 + /// 默认值 + public static async Task OrTimeoutOrDefault(this Task task, TimeSpan timeout, T? defaultValue = default) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + return defaultValue; + + return await task; + } + + #endregion + + #region 重试机制 + + /// + /// 任务失败时自动重试 + /// + /// 任务工厂函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func> taskFactory, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await taskFactory(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delay.HasValue) + await Task.Delay(delay.Value); + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 任务失败时自动重试 + /// + /// 任务工厂函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func taskFactory, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + await taskFactory(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delay.HasValue) + await Task.Delay(delay.Value); + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 任务失败时自动重试(带条件判断) + /// + /// 任务工厂函数 + /// 重试条件函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func> taskFactory, Func shouldRetry, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + if (shouldRetry == null) + throw new ArgumentNullException(nameof(shouldRetry)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await taskFactory(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && shouldRetry(ex)) + { + if (delay.HasValue) + await Task.Delay(delay.Value); + } + else + { + break; + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + #endregion + + #region 任务组合 + + /// + /// 在所有任务完成时返回,无论成功或失败 + /// + public static async Task WhenAllOrAnyFailed(this IEnumerable tasks) + { + if (tasks == null) + throw new ArgumentNullException(nameof(tasks)); + + var taskArray = tasks.ToArray(); + if (taskArray.Length == 0) + return taskArray; + + var tcs = new TaskCompletionSource(); + + int remaining = taskArray.Length; + var results = new Task[taskArray.Length]; + + for (int i = 0; i < taskArray.Length; i++) + { + int index = i; + taskArray[i].ContinueWith(t => + { + results[index] = t; + if (Interlocked.Decrement(ref remaining) == 0) + { + tcs.TrySetResult(results); + } + }, TaskContinuationOptions.ExecuteSynchronously); + } + + return await tcs.Task; + } + + /// + /// 返回第一个完成的任务(无论成功或失败) + /// + public static async Task WhenAnyFirstOrDefault(this IEnumerable tasks) + { + if (tasks == null) + throw new ArgumentNullException(nameof(tasks)); + + var taskArray = tasks.ToArray(); + if (taskArray.Length == 0) + throw new ArgumentException("至少需要一个任务", nameof(tasks)); + + return await Task.WhenAny(taskArray); + } + + #endregion + + #region 任务超时与取消组合 + + /// + /// 创建一个带超时和取消令牌的任务 + /// + public static async Task WithTimeoutAndCancellation(this Task task, TimeSpan timeout, CancellationToken cancellationToken) + { + var timeoutCts = new CancellationTokenSource(timeout); + var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); + + try + { + return await task; + } + catch (OperationCanceledException) when (timeoutCts.Token.IsCancellationRequested) + { + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + } + finally + { + timeoutCts.Dispose(); + combinedCts.Dispose(); + } + } + + /// + /// 创建一个带超时和取消令牌的任务 + /// + public static async Task WithTimeoutAndCancellation(this Task task, TimeSpan timeout, CancellationToken cancellationToken) + { + var timeoutCts = new CancellationTokenSource(timeout); + var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); + + try + { + await task; + } + catch (OperationCanceledException) when (timeoutCts.Token.IsCancellationRequested) + { + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + } + finally + { + timeoutCts.Dispose(); + combinedCts.Dispose(); + } + } + + #endregion + + #region 任务结果处理 + + /// + /// 处理任务结果,无论成功或失败 + /// + public static async Task Finally(this Task task, Func onSuccess, Func onFailure) + { + try + { + var result = await task; + return onSuccess(result); + } + catch (Exception ex) + { + return onFailure(ex); + } + } + + /// + /// 处理任务结果,无论成功或失败 + /// + public static async Task Finally(this Task task, Action onSuccess, Action onFailure) + { + try + { + await task; + onSuccess(); + } + catch (Exception ex) + { + onFailure(ex); + } + } + + #endregion + + #region 任务延迟执行 + + /// + /// 延迟执行任务 + /// + public static async Task Delayed(this Func> taskFactory, TimeSpan delay) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + await Task.Delay(delay); + return await taskFactory(); + } + + /// + /// 延迟执行任务 + /// + public static async Task Delayed(this Func taskFactory, TimeSpan delay) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + await Task.Delay(delay); + await taskFactory(); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/TypeExtension.cs b/EasyTool.Core/ToolCategory/TypeExtension.cs new file mode 100644 index 0000000..5e35baa --- /dev/null +++ b/EasyTool.Core/ToolCategory/TypeExtension.cs @@ -0,0 +1,432 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace EasyTool.Extension +{ + /// + /// Type 类型扩展方法 + /// + public static class TypeExtension + { + #region 类型判断 + + /// + /// 判断是否是简单类型(值类型、字符串等) + /// + public static bool IsSimpleType(this Type? type) + { + if (type == null) + return false; + + return type.IsValueType || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Guid); + } + + /// + /// 判断是否是数字类型 + /// + public static bool IsNumericType(this Type? type) + { + if (type == null) + return false; + + type = Nullable.GetUnderlyingType(type) ?? type; + + return type == typeof(byte) || + type == typeof(sbyte) || + type == typeof(short) || + type == typeof(ushort) || + type == typeof(int) || + type == typeof(uint) || + type == typeof(long) || + type == typeof(ulong) || + type == typeof(float) || + type == typeof(double) || + type == typeof(decimal); + } + + /// + /// 判断是否是集合类型 + /// + public static bool IsCollectionType(this Type? type) + { + if (type == null) + return false; + + return type != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(type); + } + + /// + /// 判断是否是字典类型 + /// + public static bool IsDictionaryType(this Type? type) + { + if (type == null) + return false; + + return typeof(System.Collections.IDictionary).IsAssignableFrom(type) || + (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.Dictionary<,>)); + } + + /// + /// 判断是否是可空值类型 + /// + public static bool IsNullableType(this Type? type) + { + if (type == null) + return false; + + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } + + /// + /// 判断是否是泛型类型 + /// + public static bool IsGenericType(this Type? type, Type genericTypeDefinition) + { + if (type == null || !type.IsGenericType) + return false; + + return type.GetGenericTypeDefinition() == genericTypeDefinition; + } + + /// + /// 判断是否是某个泛型类型的子类 + /// + public static bool IsSubclassOfGeneric(this Type? type, Type genericTypeDefinition) + { + if (type == null || genericTypeDefinition == null) + return false; + + while (type != null && type != typeof(object)) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == genericTypeDefinition) + return true; + + type = type.BaseType; + } + + return false; + } + + /// + /// 判断是否是匿名类型 + /// + public static bool IsAnonymousType(this Type? type) + { + if (type == null) + return false; + + return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) && + type.IsGenericType && + type.Name.Contains("AnonymousType") && + (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")); + } + + #endregion + + #region 类型名称 + + /// + /// 获取友好的类型名称 + /// + public static string GetFriendlyName(this Type? type) + { + if (type == null) + return "null"; + + if (type == typeof(int)) + return "int"; + if (type == typeof(uint)) + return "uint"; + if (type == typeof(long)) + return "long"; + if (type == typeof(ulong)) + return "ulong"; + if (type == typeof(short)) + return "short"; + if (type == typeof(ushort)) + return "ushort"; + if (type == typeof(byte)) + return "byte"; + if (type == typeof(sbyte)) + return "sbyte"; + if (type == typeof(float)) + return "float"; + if (type == typeof(double)) + return "double"; + if (type == typeof(decimal)) + return "decimal"; + if (type == typeof(bool)) + return "bool"; + if (type == typeof(string)) + return "string"; + if (type == typeof(char)) + return "char"; + if (type == typeof(object)) + return "object"; + if (type == typeof(void)) + return "void"; + + if (type.IsGenericType) + { + var genericArgs = type.GetGenericArguments(); + var genericTypeName = type.GetGenericTypeDefinition().GetFriendlyName(); + var genericArgsNames = string.Join(", ", genericArgs.Select(t => t.GetFriendlyName())); + return genericTypeName + "<" + genericArgsNames + ">"; + } + + if (type.IsArray) + { + var elementType = type.GetElementType(); + return $"{elementType.GetFriendlyName()}[]"; + } + + return type.Name; + } + + /// + /// 获取类型的显示名称 + /// + public static string GetDisplayName(this Type? type) + { + if (type == null) + return string.Empty; + + var attr = type.GetCustomAttribute(); + return attr?.DisplayName ?? type.Name; + } + + /// + /// 获取类型的描述 + /// + public static string GetDescription(this Type? type) + { + if (type == null) + return string.Empty; + + var attr = type.GetCustomAttribute(); + return attr?.Description ?? string.Empty; + } + + #endregion + + #region 默认值 + + /// + /// 获取类型的默认值 + /// + public static object? GetDefaultValue(this Type? type) + { + if (type == null) + return null; + + return type.IsValueType ? Activator.CreateInstance(type) : null; + } + + #endregion + + #region 泛型处理 + + /// + /// 获取可空类型的实际类型 + /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type")] + /// + [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type", false)] + public static Type? GetNullableType(this Type? type) + { + if (type == null) + return null; + + return Nullable.GetUnderlyingType(type) ?? type; + } + + /// + /// 获取集合的元素类型 + /// + public static Type? GetElementType(this Type? type) + { + if (type == null) + return null; + + if (type.IsArray) + return type.GetElementType(); + + if (type.IsCollectionType()) + { + var genericArgs = type.GetGenericArguments(); + if (genericArgs.Length > 0) + return genericArgs[0]; + } + + return null; + } + + /// + /// 获取字典的键值对类型 + /// + public static (Type? KeyType, Type? ValueType) GetDictionaryKeyValueTypes(this Type? type) + { + if (type == null) + return (null, null); + + if (typeof(System.Collections.IDictionary).IsAssignableFrom(type)) + { + var interfaces = type.GetInterfaces(); + var dictInterface = interfaces.FirstOrDefault(i => + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>)); + + if (dictInterface != null) + { + var genericArgs = dictInterface.GetGenericArguments(); + return (genericArgs[0], genericArgs[1]); + } + } + + return (null, null); + } + + #endregion + + #region 特性操作 + + /// + /// 判断是否有指定特性 + /// + public static bool HasAttribute(this Type? type) where T : Attribute + { + if (type == null) + return false; + + return type.GetCustomAttribute() != null; + } + + /// + /// 判断是否有指定特性 + /// + public static bool HasAttribute(this Type? type, Type? attributeType) + { + if (type == null || attributeType == null) + return false; + + return type.GetCustomAttributes(attributeType, false).Any(); + } + + /// + /// 获取指定特性 + /// + public static T? GetAttribute(this Type? type) where T : Attribute + { + if (type == null) + return null; + + return type.GetCustomAttribute(); + } + + /// + /// 获取所有指定特性 + /// + public static T[] GetAttributes(this Type? type) where T : Attribute + { + if (type == null) + return Array.Empty(); + + return type.GetCustomAttributes().ToArray(); + } + + #endregion + + #region 成员获取 + + /// + /// 获取所有属性(包含继承的) + /// + public static PropertyInfo[] GetAllProperties(this Type? type) + { + if (type == null) + return Array.Empty(); + + var properties = new List(); + var currentType = type; + + while (currentType != null && currentType != typeof(object)) + { + var currentProps = currentType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); + properties.AddRange(currentProps); + currentType = currentType.BaseType; + } + + return properties.ToArray(); + } + + /// + /// 获取所有字段(包含继承的) + /// + public static FieldInfo[] GetAllFields(this Type? type) + { + if (type == null) + return Array.Empty(); + + var fields = new List(); + var currentType = type; + + while (currentType != null && currentType != typeof(object)) + { + var currentFields = currentType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + fields.AddRange(currentFields); + currentType = currentType.BaseType; + } + + return fields.ToArray(); + } + + #endregion + + #region 类型转换 + + /// + /// 尝试将值转换为指定类型 + /// + public static object? ChangeType(this Type type, object? value) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + + if (value == null) + { + if (type.IsValueType) + return Activator.CreateInstance(type); + return null; + } + + var valueType = value.GetType(); + + // 如果类型相同或目标类型是源类型的父类 + if (type.IsAssignableFrom(valueType)) + return value; + + // 可空类型处理 + var underlyingType = Nullable.GetUnderlyingType(type); + if (underlyingType != null) + return ChangeType(underlyingType, value); + + // 枚举处理 + if (type.IsEnum && value is string str) + return Enum.Parse(type, str, true); + + // 标准转换 + return Convert.ChangeType(value, type); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/TypeUtil.cs b/EasyTool.Core/ToolCategory/TypeUtil.cs index a74ff6a..6fdd218 100644 --- a/EasyTool.Core/ToolCategory/TypeUtil.cs +++ b/EasyTool.Core/ToolCategory/TypeUtil.cs @@ -13,9 +13,11 @@ public class TypeUtil { /// /// 判断类型是否是可空类型 + /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(typeof(T)) != null")] /// /// 要判断的类型 /// 是否是可空类型 + [Obsolete("请直接使用 Nullable.GetUnderlyingType(typeof(T)) != null", false)] public static bool IsNullable() where T : struct { return Nullable.GetUnderlyingType(typeof(T)) != null; @@ -23,9 +25,11 @@ public static bool IsNullable() where T : struct /// /// 判断类型是否是枚举类型 + /// [Obsolete("请直接使用 typeof(T).IsEnum")] /// /// 要判断的类型 /// 是否是枚举类型 + [Obsolete("请直接使用 typeof(T).IsEnum", false)] public static bool IsEnum() { return typeof(T).IsEnum; @@ -33,9 +37,11 @@ public static bool IsEnum() /// /// 获取泛型类型的参数类型 + /// [Obsolete("请直接使用 typeof(T).GetGenericArguments()")] /// /// 要获取参数类型的泛型类型 /// 泛型类型的参数类型数组 + [Obsolete("请直接使用 typeof(T).GetGenericArguments()", false)] public static Type[] GetGenericArguments() { return typeof(T).GetGenericArguments(); @@ -43,9 +49,11 @@ public static Type[] GetGenericArguments() /// /// 获取类型的所有属性 + /// [Obsolete("请直接使用 typeof(T).GetProperties()")] /// /// 要获取属性的类型 /// 属性数组 + [Obsolete("请直接使用 typeof(T).GetProperties()", false)] public static PropertyInfo[] GetProperties() { return typeof(T).GetProperties(); @@ -53,9 +61,11 @@ public static PropertyInfo[] GetProperties() /// /// 获取类型的所有字段 + /// [Obsolete("请直接使用 typeof(T).GetFields()")] /// /// 要获取字段的类型 /// 字段数组 + [Obsolete("请直接使用 typeof(T).GetFields()", false)] public static FieldInfo[] GetFields() { return typeof(T).GetFields(); @@ -63,9 +73,11 @@ public static FieldInfo[] GetFields() /// /// 获取类型的所有方法 + /// [Obsolete("请直接使用 typeof(T).GetMethods()")] /// /// 要获取方法的类型 /// 方法数组 + [Obsolete("请直接使用 typeof(T).GetMethods()", false)] public static MethodInfo[] GetMethods() { return typeof(T).GetMethods(); @@ -73,9 +85,11 @@ public static MethodInfo[] GetMethods() /// /// 获取类型的所有事件 + /// [Obsolete("请直接使用 typeof(T).GetEvents()")] /// /// 要获取事件的类型 /// 事件数组 + [Obsolete("请直接使用 typeof(T).GetEvents()", false)] public static EventInfo[] GetEvents() { return typeof(T).GetEvents(); @@ -83,9 +97,11 @@ public static EventInfo[] GetEvents() /// /// 获取类型的所有属性、字段、方法和事件 + /// [Obsolete("请直接使用 typeof(T).GetMembers()")] /// /// 要获取成员的类型 /// 成员数组 + [Obsolete("请直接使用 typeof(T).GetMembers()", false)] public static MemberInfo[] GetMembers() { return typeof(T).GetMembers(); @@ -93,9 +109,11 @@ public static MemberInfo[] GetMembers() /// /// 获取类型的所有构造函数 + /// [Obsolete("请直接使用 typeof(T).GetConstructors()")] /// /// 要获取构造函数的类型 /// 构造函数数组 + [Obsolete("请直接使用 typeof(T).GetConstructors()", false)] public static ConstructorInfo[] GetConstructors() { return typeof(T).GetConstructors(); @@ -114,10 +132,12 @@ public static bool ImplementsInterface() /// /// 判断类型是否继承了指定的基类 + /// [Obsolete("请直接使用 typeof(T).IsSubclassOf(typeof(TBase))")] /// /// 要判断的类型 /// 要判断的基类类型 /// 是否继承了指定的基类 + [Obsolete("请直接使用 typeof(T).IsSubclassOf(typeof(TBase))", false)] public static bool InheritsFrom() { return typeof(T).IsSubclassOf(typeof(TBase)); @@ -125,9 +145,11 @@ public static bool InheritsFrom() /// /// 创建指定类型的实例 + /// [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T))")] /// /// 要创建实例的类型 /// 类型的实例 + [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T))", false)] public static T CreateInstance() { return (T)Activator.CreateInstance(typeof(T)); @@ -135,10 +157,12 @@ public static T CreateInstance() /// /// 创建指定类型的实例 + /// [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T), args)")] /// /// 要创建实例的类型 /// 构造函数的参数 /// 类型的实例 + [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T), args)", false)] public static T CreateInstance(params object[] args) { return (T)Activator.CreateInstance(typeof(T), args); @@ -161,10 +185,12 @@ public static IEnumerable GetEnumValues() /// /// 将字符串转换为指定类型的值 + /// [Obsolete("请直接使用 (T)Convert.ChangeType(value, typeof(T))")] /// /// 要转换的类型 /// 要转换的字符串 /// 转换后的值 + [Obsolete("请直接使用 (T)Convert.ChangeType(value, typeof(T))", false)] public static T ConvertFromString(string value) { return (T)Convert.ChangeType(value, typeof(T)); @@ -172,10 +198,12 @@ public static T ConvertFromString(string value) /// /// 将值转换为指定类型的字符串 + /// [Obsolete("请直接使用 Convert.ToString(value)")] /// /// 要转换的类型 /// 要转换的值 /// 转换后的字符串 + [Obsolete("请直接使用 Convert.ToString(value)", false)] public static string ConvertToString(T value) { return Convert.ToString(value); diff --git a/EasyTool.Core/ToolCategory/URLUtil.cs b/EasyTool.Core/ToolCategory/URLUtil.cs index d281985..82edd17 100644 --- a/EasyTool.Core/ToolCategory/URLUtil.cs +++ b/EasyTool.Core/ToolCategory/URLUtil.cs @@ -140,9 +140,11 @@ public static string UrlDecodeQuery(string value) /// /// 从URL中提取域名。 + /// [Obsolete("请直接使用 new Uri(url).Host")] /// /// 要提取域名的URL。 /// URL中的域名。 + [Obsolete("请直接使用 new Uri(url).Host", false)] public static string ExtractDomain(string url) { var uri = new Uri(url); @@ -151,9 +153,11 @@ public static string ExtractDomain(string url) /// /// 从URL中提取路径。 + /// [Obsolete("请直接使用 new Uri(url).AbsolutePath")] /// /// 要提取路径的URL。 /// URL中的路径。 + [Obsolete("请直接使用 new Uri(url).AbsolutePath", false)] public static string ExtractPath(string url) { var uri = new Uri(url); @@ -173,9 +177,11 @@ public static bool IsHttps(string url) /// /// 从URL中提取查询字符串。 + /// [Obsolete("请直接使用 new Uri(url).Query")] /// /// 要提取查询字符串的URL。 /// URL中的查询字符串。 + [Obsolete("请直接使用 new Uri(url).Query", false)] public static string ExtractQueryString(string url) { var uri = new Uri(url); @@ -184,9 +190,11 @@ public static string ExtractQueryString(string url) /// /// 从URL中提取片段。 + /// [Obsolete("请直接使用 new Uri(url).Fragment")] /// /// 要提取片段的URL。 /// URL中的片段。 + [Obsolete("请直接使用 new Uri(url).Fragment", false)] public static string ExtractFragment(string url) { var uri = new Uri(url); diff --git a/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs b/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs index f259e81..4b2d623 100644 --- a/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs +++ b/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs @@ -152,14 +152,27 @@ public static List GetDtos(Assembly assembly) foreach (var propertyType in propertyTypes) { var property = new DtoProperty(propertyType.PropertyType, propertyType.Name); - property.Title = propertyType.GetCustomAttribute()?.DisplayName ?? ""; - property.IsInverseProperty = propertyType.GetCustomAttribute() != null; + // 优先使用 DisplayAttribute 或 DescriptionAttribute,然后是 DisplayNameAttribute + property.Title = propertyType.GetCustomAttribute()?.GetName() + ?? propertyType.GetCustomAttribute()?.Description + ?? propertyType.GetCustomAttribute()?.DisplayName + ?? ""; + + property.IsInverseProperty = propertyType.GetCustomAttribute() != null; property.IsKey = propertyType.GetCustomAttribute() != null; property.IsRequired = propertyType.GetCustomAttribute() != null; property.StringLength = propertyType.GetCustomAttribute()?.MaximumLength ?? 0; - dto.Propertys.Add(property); + // 支持更多 .NET 约定特性 + property.IsEditable = propertyType.GetCustomAttribute() == null; + property.DataType = GetDataType(propertyType.GetCustomAttribute()); + property.RegularExpression = propertyType.GetCustomAttribute()?.Pattern; + property.RangeMinimum = propertyType.GetCustomAttribute()?.Minimum as double?; + property.RangeMaximum = propertyType.GetCustomAttribute()?.Maximum as double?; + property.IsForeignKey = propertyType.GetCustomAttribute() != null; + + dto.Propertys.Add(property); } dtos.Add(dto); @@ -188,7 +201,9 @@ public DtoClass(string name, string _namespace) } - //TODO:需要改造成使用.net约定的属性 + /// + /// DTO 属性信息,支持标准 .NET DataAnnotations 特性 + /// public class DtoProperty { public DtoProperty(Type type, string name) @@ -196,18 +211,79 @@ public DtoProperty(Type type, string name) Type = type; Name = name; } - public Type Type { get; set; } - public string Name { get; set; } - - public string Title { get; set; }//字段名称 - public bool IsInverseProperty { get; set; }//是否关联属性 + /// + /// 属性类型 + /// + public Type Type { get; set; } - public bool IsRequired { get; set; }//是否必填 + /// + /// 属性名称 + /// + public string Name { get; set; } - public int StringLength { get; set; }//字符串长度 + /// + /// 显示名称(支持 DisplayAttribute、DescriptionAttribute、DisplayNameAttribute) + /// + public string Title { get; set; } + + /// + /// 是否关联属性(InversePropertyAttribute) + /// + public bool IsInverseProperty { get; set; } + + /// + /// 是否必填(RequiredAttribute) + /// + public bool IsRequired { get; set; } + + /// + /// 字符串长度(StringLengthAttribute) + /// + public int StringLength { get; set; } + + /// + /// 是否主键(KeyAttribute) + /// + public bool IsKey { get; set; } + + /// + /// 是否可编辑(EditableAttribute) + /// + public bool IsEditable { get; set; } = true; + + /// + /// 数据类型(DataTypeAttribute) + /// + public string DataType { get; set; } + + /// + /// 正则表达式验证(RegularExpressionAttribute) + /// + public string RegularExpression { get; set; } + + /// + /// 范围最小值(RangeAttribute) + /// + public double? RangeMinimum { get; set; } + + /// + /// 范围最大值(RangeAttribute) + /// + public double? RangeMaximum { get; set; } + + /// + /// 是否外键(ForeignKeyAttribute) + /// + public bool IsForeignKey { get; set; } + } - public bool IsKey { get; set; }//是否主键 + /// + /// 获取 DataTypeAttribute 的数据类型名称 + /// + private static string GetDataType(DataTypeAttribute attribute) + { + return attribute?.DataType.ToString() ?? string.Empty; } } From a5d0d9f5dba2a2038aeeb8e618c32ea8cc2bd401 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 15:33:02 +0800 Subject: [PATCH 03/19] =?UTF-8?q?refactor:=20=E6=95=B4=E5=90=88=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=BA=93=E5=B9=B6=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 28 个冗余工具类(IoUtil, ObjectUtil, ArrayUtil 等) - 合并功能到对应扩展方法类(ObjectExtension, ArrayExtension 等) - 优化 StrUtil 和 PageUtil 中的字符串拼接,改用 StringBuilder - 移动 HashUtil, HexUtil, RandomUtil, RegexUtil, URLUtil 到正确类别 - 标记 DateTimeExtension 中 11 个包装方法为 Obsolete - 删除 CloneCategory 目录,功能已整合到 ObjectExtension 减少约 4000+ 行冗余代码,提升整体代码质量和性能 --- EasyTool.Core/CloneCategory/CloneExtension.cs | 12 - EasyTool.Core/CloneCategory/CloneUtil.cs | 84 -- EasyTool.Core/CodeCategory/Base32Util.cs | 163 --- EasyTool.Core/CodeCategory/Base62Util.cs | 73 - EasyTool.Core/CodeCategory/Base64Util.cs | 141 -- EasyTool.Core/CodeCategory/EncodingUtil.cs | 377 ++++++ .../HashUtil.cs | 24 +- .../{ToolCategory => CodeCategory}/HexUtil.cs | 17 +- EasyTool.Core/CodeCategory/MorseUtil.cs | 107 -- EasyTool.Core/CodeCategory/PunycodeUtil.cs | 254 ---- EasyTool.Core/CodeCategory/RotUtil.cs | 51 - .../CollectionsCategory/ArrayExtension.cs | 203 ++- .../DictionaryExtension.cs | 23 - .../CollectionsCategory/IteratorUtil.cs | 203 --- .../LinkedListExtension.cs | 30 - .../CollectionsCategory/ListExtension.cs | 81 +- EasyTool.Core/CollectionsCategory/ListUtil.cs | 272 ---- .../CollectionsCategory/QueueExtension.cs | 33 - .../CollectionsCategory/StackExtension.cs | 19 - .../ConvertCategory/ByteExtension.cs | 78 -- .../ConvertCategory/ConvertExtension.cs | 47 +- EasyTool.Core/ConvertCategory/ConvertUtil.cs | 161 --- .../ConvertCategory/NumberExtension.cs | 128 -- .../DateTimeCategory/DateTimeExtension.cs | 61 +- EasyTool.Core/DateTimeCategory/TimerUtil.cs | 26 +- .../DateTimeCategory/TimestampUtil.cs | 84 -- .../IOCategory/FileSystemExtension.cs | 59 - EasyTool.Core/IOCategory/FileTypeExtension.cs | 64 +- EasyTool.Core/IOCategory/FileTypeUtil.cs | 82 -- EasyTool.Core/IOCategory/FileUtil.cs | 156 +-- EasyTool.Core/IOCategory/IoUtil.cs | 178 --- EasyTool.Core/IOCategory/StreamExtension.cs | 70 - EasyTool.Core/LanguageCategory/BCDUtil.cs | 200 --- .../LanguageCategory/SingletonUtil.cs | 46 - EasyTool.Core/LanguageCategory/TreeUtil.cs | 302 ----- EasyTool.Core/MathCategory/MathUtil.cs | 305 +++-- EasyTool.Core/MathCategory/NumberUtil.cs | 352 ----- .../RandomUtil.cs | 0 EasyTool.Core/NetCategory/NetUtil.cs | 129 -- .../{ToolCategory => NetCategory}/URLUtil.cs | 8 - EasyTool.Core/TextCategory/CsvUtil.cs | 127 -- .../RegexUtil.cs | 27 - EasyTool.Core/TextCategory/UnicodeUtil.cs | 180 --- EasyTool.Core/ToolCategory/ArrayUtil.cs | 255 ---- EasyTool.Core/ToolCategory/ClassExtension.cs | 89 -- EasyTool.Core/ToolCategory/ClassUtil.cs | 240 ---- EasyTool.Core/ToolCategory/ColorExtension.cs | 16 +- EasyTool.Core/ToolCategory/DLLUtil.cs | 148 -- EasyTool.Core/ToolCategory/EnumExtension.cs | 71 +- EasyTool.Core/ToolCategory/EnumUtil.cs | 253 ---- EasyTool.Core/ToolCategory/EscapeUtil.cs | 211 --- .../ToolCategory/ExceptionExtension.cs | 12 - EasyTool.Core/ToolCategory/GuidExtension.cs | 18 - EasyTool.Core/ToolCategory/IdcardUtil.cs | 470 ------- EasyTool.Core/ToolCategory/MEFUtil.cs | 126 -- EasyTool.Core/ToolCategory/ObjectExtension.cs | 497 ++++++- EasyTool.Core/ToolCategory/ObjectUtil.cs | 1193 ----------------- EasyTool.Core/ToolCategory/PageUtil.cs | 35 +- EasyTool.Core/ToolCategory/ProcessUtil.cs | 202 --- EasyTool.Core/ToolCategory/ReflectUtil.cs | 307 +++-- EasyTool.Core/ToolCategory/RuntimeUtil.cs | 147 -- EasyTool.Core/ToolCategory/StrExtension.cs | 48 - EasyTool.Core/ToolCategory/StrUtil.cs | 152 +-- .../ToolCategory/StringBuilderExtension.cs | 10 - .../ToolCategory/StringComparisonExtension.cs | 25 - EasyTool.Core/ToolCategory/SystemUtil.cs | 331 +++++ EasyTool.Core/ToolCategory/TypeExtension.cs | 12 - EasyTool.Core/ToolCategory/TypeUtil.cs | 212 --- .../CloneCategory/CloneExtensionTests.cs | 4 +- 69 files changed, 1931 insertions(+), 8190 deletions(-) delete mode 100644 EasyTool.Core/CloneCategory/CloneExtension.cs delete mode 100644 EasyTool.Core/CloneCategory/CloneUtil.cs delete mode 100644 EasyTool.Core/CodeCategory/Base32Util.cs delete mode 100644 EasyTool.Core/CodeCategory/Base62Util.cs delete mode 100644 EasyTool.Core/CodeCategory/Base64Util.cs create mode 100644 EasyTool.Core/CodeCategory/EncodingUtil.cs rename EasyTool.Core/{ToolCategory => CodeCategory}/HashUtil.cs (99%) rename EasyTool.Core/{ToolCategory => CodeCategory}/HexUtil.cs (95%) delete mode 100644 EasyTool.Core/CodeCategory/MorseUtil.cs delete mode 100644 EasyTool.Core/CodeCategory/PunycodeUtil.cs delete mode 100644 EasyTool.Core/CodeCategory/RotUtil.cs delete mode 100644 EasyTool.Core/CollectionsCategory/IteratorUtil.cs delete mode 100644 EasyTool.Core/CollectionsCategory/LinkedListExtension.cs delete mode 100644 EasyTool.Core/CollectionsCategory/ListUtil.cs delete mode 100644 EasyTool.Core/CollectionsCategory/QueueExtension.cs delete mode 100644 EasyTool.Core/CollectionsCategory/StackExtension.cs delete mode 100644 EasyTool.Core/ConvertCategory/ConvertUtil.cs delete mode 100644 EasyTool.Core/DateTimeCategory/TimestampUtil.cs delete mode 100644 EasyTool.Core/IOCategory/FileTypeUtil.cs delete mode 100644 EasyTool.Core/IOCategory/IoUtil.cs delete mode 100644 EasyTool.Core/LanguageCategory/BCDUtil.cs delete mode 100644 EasyTool.Core/LanguageCategory/SingletonUtil.cs delete mode 100644 EasyTool.Core/LanguageCategory/TreeUtil.cs delete mode 100644 EasyTool.Core/MathCategory/NumberUtil.cs rename EasyTool.Core/{ToolCategory => MathCategory}/RandomUtil.cs (100%) delete mode 100644 EasyTool.Core/NetCategory/NetUtil.cs rename EasyTool.Core/{ToolCategory => NetCategory}/URLUtil.cs (94%) delete mode 100644 EasyTool.Core/TextCategory/CsvUtil.cs rename EasyTool.Core/{ToolCategory => TextCategory}/RegexUtil.cs (79%) delete mode 100644 EasyTool.Core/TextCategory/UnicodeUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/ArrayUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/ClassExtension.cs delete mode 100644 EasyTool.Core/ToolCategory/ClassUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/DLLUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/EnumUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/EscapeUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/IdcardUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/MEFUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/ObjectUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/ProcessUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/RuntimeUtil.cs create mode 100644 EasyTool.Core/ToolCategory/SystemUtil.cs delete mode 100644 EasyTool.Core/ToolCategory/TypeUtil.cs diff --git a/EasyTool.Core/CloneCategory/CloneExtension.cs b/EasyTool.Core/CloneCategory/CloneExtension.cs deleted file mode 100644 index aa32c1a..0000000 --- a/EasyTool.Core/CloneCategory/CloneExtension.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool.Extension -{ - public static class CloneExtension - { - //定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T? Clone(this T? obj) => CloneUtil.Clone(obj); - } -} diff --git a/EasyTool.Core/CloneCategory/CloneUtil.cs b/EasyTool.Core/CloneCategory/CloneUtil.cs deleted file mode 100644 index 7483124..0000000 --- a/EasyTool.Core/CloneCategory/CloneUtil.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; -using System.Runtime.Serialization; -using System.Threading.Tasks; - -namespace EasyTool -{ - /// - /// 静态工具类 CloneHelper,用于深度克隆对象 - /// - public static class CloneUtil - { - // 定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T? Clone(T? obj) - { - // 检查类型是否可序列化 - if (!typeof(T).IsSerializable) - { - throw new ArgumentException("The type must be serializable.", nameof(obj)); - } - - // 如果对象为 null,则返回 null - if (ReferenceEquals(obj, null)) - { - return default; - } - - // 创建一个二进制序列化器 - IFormatter formatter = new BinaryFormatter(); - - // 创建一个内存流 - using (var stream = new MemoryStream()) - { - // 使用二进制序列化将对象写入内存流 - formatter.Serialize(stream, obj); - - // 将内存流位置重置为开头 - stream.Seek(0, SeekOrigin.Begin); - - // 使用反序列化从内存流中读取并返回克隆的对象 - return (T)formatter.Deserialize(stream)!; - } - } - - /// - /// 静态工具类 CloneHelper,用于异步深度克隆对象 - /// - /// - /// - /// - /// - public static async Task CloneAsync(T? obj) - { - // 检查类型是否可序列化 - if (!typeof(T).IsSerializable) - { - throw new ArgumentException("The type must be serializable.", nameof(obj)); - } - - // 如果对象为 null,则返回 null - if (ReferenceEquals(obj, null)) - { - return default; - } - - // 创建一个二进制序列化器 - IFormatter formatter = new BinaryFormatter(); - - // 创建一个内存流 - using (var stream = new MemoryStream()) - { - // 使用二进制序列化将对象写入内存流 - formatter.Serialize(stream, obj); - - // 将内存流位置重置为开头 - stream.Seek(0, SeekOrigin.Begin); - - // 使用反序列化从内存流中读取并返回克隆的对象 - return await Task.FromResult((T?)formatter.Deserialize(stream)); - } - } - } -} diff --git a/EasyTool.Core/CodeCategory/Base32Util.cs b/EasyTool.Core/CodeCategory/Base32Util.cs deleted file mode 100644 index c3c5641..0000000 --- a/EasyTool.Core/CodeCategory/Base32Util.cs +++ /dev/null @@ -1,163 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Base32 编码解码工具类 - /// - public static class Base32Util - { - // Base32 字符集,共 32 个字符 - private static readonly char[] BASE32_CHARS = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".ToCharArray(); - - // Base32 填充字符 - private const char BASE32_PADDING_CHAR = '='; - - /// - /// 将给定的字节数组转换为 Base32 编码字符串。 - /// - /// 要转换的字节数组 - /// 转换后的 Base32 编码字符串 - public static string Encode(byte[] bytes) - { - if (bytes == null) - { - throw new ArgumentNullException(nameof(bytes)); - } - - int length = bytes.Length; - if (length == 0) - { - return string.Empty; - } - - char[] chars = new char[(length + 4) / 5 * 8]; - int index = 0; - for (int i = 0; i < length; i += 5) - { - int val = (bytes[i] << 24) + ((i + 1 < length ? bytes[i + 1] : 0) << 16) + - ((i + 2 < length ? bytes[i + 2] : 0) << 8) + ((i + 3 < length ? bytes[i + 3] : 0) << 0); - chars[index++] = BASE32_CHARS[(val >> 35) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 30) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 25) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 20) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 15) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 10) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 5) & 0x1F]; - chars[index++] = BASE32_CHARS[val & 0x1F]; - } - - // 添加填充字符 - int paddingCount = length % 5; - if (paddingCount > 0) - { - chars[chars.Length - 1] = BASE32_PADDING_CHAR; - if (paddingCount == 1) - { - chars[chars.Length - 2] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 2) - { - chars[chars.Length - 3] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 3) - { - chars[chars.Length - 4] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 4) - { - chars[chars.Length - 5] = BASE32_PADDING_CHAR; - } - } - - return new string(chars); - } - - /// - /// 将给定的 Base32 编码字符串转换为字节数组。 - /// - /// 要转换的 Base32 编码字符串 - /// 转换后的字节数组 - public static byte[] Decode(string str) - { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } - - int length = str.Length; - if (length % 8 != 0) - { - throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); - } - - int paddingCount = 0; - if (length > 0 && str[length - 1] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - if (length > 1 && str[length - 2] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - if (length > 3 && str[length - 3] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - if (length > 4 && str[length - 4] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - if (length > 6 && str[length - 6] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - - byte[] bytes = new byte[length / 8 * 5 - paddingCount]; - int index = 0; - for (int i = 0; i < length; i += 8) - { - int val = (DecodeBase32Char(str[i]) << 35) + - (DecodeBase32Char(str[i + 1]) << 30) + - (DecodeBase32Char(str[i + 2]) << 25) + - (DecodeBase32Char(str[i + 3]) << 20) + - (DecodeBase32Char(str[i + 4]) << 15) + - (DecodeBase32Char(str[i + 5]) << 10) + - (DecodeBase32Char(str[i + 6]) << 5) + - DecodeBase32Char(str[i + 7]); - bytes[index++] = (byte)(val >> 24); - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 16); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 8); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)val; - } - } - - return bytes; - } - - // 解码 Base32 字符 - private static int DecodeBase32Char(char c) - { - if (c >= 'A' && c <= 'Z') - { - return c - 'A'; - } - if (c >= '2' && c <= '7') - { - return c - '2' + 26; - } - throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); - } - } -} diff --git a/EasyTool.Core/CodeCategory/Base62Util.cs b/EasyTool.Core/CodeCategory/Base62Util.cs deleted file mode 100644 index cac4b89..0000000 --- a/EasyTool.Core/CodeCategory/Base62Util.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Base62 编码解码工具类 - /// - public static class Base62Util - { - // Base62 字符集,共 62 个字符 - private static readonly char[] BASE62_CHARS = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); - - /// - /// 将给定的整数转换为 Base62 编码字符串。 - /// - /// 要转换的整数 - /// 转换后的 Base62 编码字符串 - public static string Encode(long number) - { - if (number < 0) - { - throw new ArgumentOutOfRangeException(nameof(number), "Number must be non-negative."); - } - - if (number == 0) - { - return BASE62_CHARS[0].ToString(); - } - - List chars = new List(); - int targetBase = BASE62_CHARS.Length; - while (number > 0) - { - int index = (int)(number % targetBase); - chars.Add(BASE62_CHARS[index]); - number = number / targetBase; - } - chars.Reverse(); - return new string(chars.ToArray()); - } - - /// - /// 将给定的 Base62 编码字符串转换为整数。 - /// - /// 要转换的 Base62 编码字符串 - /// 转换后的整数 - public static long Decode(string str) - { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } - - long result = 0; - int sourceBase = BASE62_CHARS.Length; - long multiplier = 1; - for (int i = str.Length - 1; i >= 0; i--) - { - int digit = Array.IndexOf(BASE62_CHARS, str[i]); - if (digit == -1) - { - throw new ArgumentException("Invalid character in string: " + str[i], nameof(str)); - } - result += digit * multiplier; - multiplier *= sourceBase; - } - return result; - } - } -} diff --git a/EasyTool.Core/CodeCategory/Base64Util.cs b/EasyTool.Core/CodeCategory/Base64Util.cs deleted file mode 100644 index 20098c5..0000000 --- a/EasyTool.Core/CodeCategory/Base64Util.cs +++ /dev/null @@ -1,141 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Base64 编码解码工具类 - /// - public static class Base64Util - { - // Base64 字符集,共 64 个字符 - private static readonly char[] BASE64_CHARS = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".ToCharArray(); - - // Base64 填充字符 - private const char BASE64_PADDING_CHAR = '='; - - /// - /// 将给定的字节数组转换为 Base64 编码字符串。 - /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] - /// - /// 要转换的字节数组 - /// 转换后的 Base64 编码字符串 - [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] - public static string Encode(byte[] bytes) - { - if (bytes == null) - { - throw new ArgumentNullException(nameof(bytes)); - } - - int length = bytes.Length; - if (length == 0) - { - return string.Empty; - } - - char[] chars = new char[(length + 2) / 3 * 4]; - int index = 0; - for (int i = 0; i < length; i += 3) - { - int val = (bytes[i] << 16) + ((i + 1 < length ? bytes[i + 1] : 0) << 8) + (i + 2 < length ? bytes[i + 2] : 0); - chars[index++] = BASE64_CHARS[(val >> 18) & 0x3F]; - chars[index++] = BASE64_CHARS[(val >> 12) & 0x3F]; - chars[index++] = BASE64_CHARS[(val >> 6) & 0x3F]; - chars[index++] = BASE64_CHARS[val & 0x3F]; - } - - // 添加填充字符 - int paddingCount = length % 3; - if (paddingCount > 0) - { - chars[chars.Length - 1] = BASE64_PADDING_CHAR; - if (paddingCount == 1) - { - chars[chars.Length - 2] = BASE64_PADDING_CHAR; - } - } - - return new string(chars); - } - - /// - /// 将给定的 Base64 编码字符串转换为字节数组。 - /// [Obsolete("请直接使用 Convert.FromBase64String(str)")] - /// - /// 要转换的 Base64 编码字符串 - /// 转换后的字节数组 - [Obsolete("请直接使用 Convert.FromBase64String(str)", false)] - public static byte[] Decode(string str) - { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } - int length = str.Length; - if (length % 4 != 0) - { - throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); - } - - int paddingCount = 0; - if (length > 0 && str[length - 1] == BASE64_PADDING_CHAR) - { - paddingCount++; - } - if (length > 1 && str[length - 2] == BASE64_PADDING_CHAR) - { - paddingCount++; - } - - byte[] bytes = new byte[length / 4 * 3 - paddingCount]; - int index = 0; - for (int i = 0; i < length; i += 4) - { - int val = (DecodeBase64Char(str[i]) << 18) + - (DecodeBase64Char(str[i + 1]) << 12) + - (DecodeBase64Char(str[i + 2]) << 6) + - DecodeBase64Char(str[i + 3]); - bytes[index++] = (byte)(val >> 16); - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 8); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)val; - } - } - - return bytes; - } - - // 解码 Base64 字符 - private static int DecodeBase64Char(char c) - { - if (c >= 'A' && c <= 'Z') - { - return c - 'A'; - } - if (c >= 'a' && c <= 'z') - { - return c - 'a' + 26; - } - if (c >= '0' && c <= '9') - { - return c - '0' + 52; - } - if (c == '+') - { - return 62; - } - if (c == '/') - { - return 63; - } - throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); - } - } -} diff --git a/EasyTool.Core/CodeCategory/EncodingUtil.cs b/EasyTool.Core/CodeCategory/EncodingUtil.cs new file mode 100644 index 0000000..67a3042 --- /dev/null +++ b/EasyTool.Core/CodeCategory/EncodingUtil.cs @@ -0,0 +1,377 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool +{ + /// + /// 编码工具类,提供各种编码格式的转换功能 + /// + public static class EncodingUtil + { + #region Base32 编码 + + // Base32 字符集,共 32 个字符 + private static readonly char[] BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".ToCharArray(); + + // Base32 填充字符 + private const char BASE32_PADDING_CHAR = '='; + + /// + /// 将给定的字节数组转换为 Base32 编码字符串 + /// + /// 要转换的字节数组 + /// 转换后的 Base32 编码字符串 + public static string Base32Encode(byte[] bytes) + { + if (bytes == null) + { + throw new ArgumentNullException(nameof(bytes)); + } + + int length = bytes.Length; + if (length == 0) + { + return string.Empty; + } + + char[] chars = new char[(length + 4) / 5 * 8]; + int index = 0; + for (int i = 0; i < length; i += 5) + { + int val = (bytes[i] << 24) + ((i + 1 < length ? bytes[i + 1] : 0) << 16) + + ((i + 2 < length ? bytes[i + 2] : 0) << 8) + ((i + 3 < length ? bytes[i + 3] : 0) << 0); + chars[index++] = BASE32_CHARS[(val >> 35) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 30) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 25) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 20) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 15) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 10) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 5) & 0x1F]; + chars[index++] = BASE32_CHARS[val & 0x1F]; + } + + // 添加填充字符 + int paddingCount = length % 5; + if (paddingCount > 0) + { + chars[chars.Length - 1] = BASE32_PADDING_CHAR; + if (paddingCount == 1) + { + chars[chars.Length - 2] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 2) + { + chars[chars.Length - 3] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 3) + { + chars[chars.Length - 4] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 4) + { + chars[chars.Length - 5] = BASE32_PADDING_CHAR; + } + } + + return new string(chars); + } + + /// + /// 将给定的 Base32 编码字符串转换为字节数组 + /// + /// 要转换的 Base32 编码字符串 + /// 转换后的字节数组 + public static byte[] Base32Decode(string str) + { + if (string.IsNullOrEmpty(str)) + { + throw new ArgumentException("String is null or empty.", nameof(str)); + } + + // 移除填充字符 + str = str.TrimEnd('='); + + int length = str.Length; + if (length % 8 != 0) + { + throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); + } + + int paddingCount = 0; + if (length > 0 && str[length - 1] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 1 && str[length - 2] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 3 && str[length - 3] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 4 && str[length - 4] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 5 && str[length - 5] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 6 && str[length - 6] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + + byte[] bytes = new byte[length / 8 * 5 - paddingCount]; + int index = 0; + for (int i = 0; i < length; i += 8) + { + int val = (DecodeBase32Char(str[i]) << 35) + + (DecodeBase32Char(str[i + 1]) << 30) + + (DecodeBase32Char(str[i + 2]) << 25) + + (DecodeBase32Char(str[i + 3]) << 20) + + (DecodeBase32Char(str[i + 4]) << 15) + + (DecodeBase32Char(str[i + 5]) << 10) + + (DecodeBase32Char(str[i + 6]) << 5) + + DecodeBase32Char(str[i + 7]); + bytes[index++] = (byte)(val >> 24); + if (index < bytes.Length) + { + bytes[index++] = (byte)(val >> 16); + } + if (index < bytes.Length) + { + bytes[index++] = (byte)(val >> 8); + } + if (index < bytes.Length) + { + bytes[index++] = (byte)val; + } + } + + return bytes; + } + + // 解码 Base32 字符 + private static int DecodeBase32Char(char c) + { + if (c >= 'A' && c <= 'Z') + { + return c - 'A'; + } + if (c >= '2' && c <= '7') + { + return c - '2' + 26; + } + throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); + } + + #endregion + + #region Base62 编码 + + // Base62 字符集,共 62 个字符 + private static readonly char[] BASE62_CHARS = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); + + /// + /// 将给定的整数转换为 Base62 编码字符串 + /// + /// 要转换的整数 + /// 转换后的 Base62 编码字符串 + public static string Base62Encode(long number) + { + if (number < 0) + { + throw new ArgumentOutOfRangeException(nameof(number), "Number must be non-negative."); + } + + if (number == 0) + { + return BASE62_CHARS[0].ToString(); + } + + List chars = new List(); + int targetBase = BASE62_CHARS.Length; + while (number > 0) + { + int index = (int)(number % targetBase); + chars.Add(BASE62_CHARS[index]); + number = number / targetBase; + } + chars.Reverse(); + return new string(chars.ToArray()); + } + + /// + /// 将给定的 Base62 编码字符串转换为整数 + /// + /// 要转换的 Base62 编码字符串 + /// 转换后的整数 + public static long Base62Decode(string str) + { + if (string.IsNullOrEmpty(str)) + { + throw new ArgumentException("String is null or empty.", nameof(str)); + } + + long result = 0; + int sourceBase = BASE62_CHARS.Length; + long multiplier = 1; + for (int i = str.Length - 1; i >= 0; i--) + { + int digit = Array.IndexOf(BASE62_CHARS, str[i]); + if (digit == -1) + { + throw new ArgumentException("Invalid character in string: " + str[i], nameof(str)); + } + result += digit * multiplier; + multiplier *= sourceBase; + } + return result; + } + + #endregion + + #region ROT 加密 + + /// + /// 将给定的字符串按照 ROT 加密算法进行加密 + /// + /// 要加密的字符串 + /// 偏移量 + /// 加密后的字符串 + public static string RotEncrypt(string text, int n) + { + if (string.IsNullOrEmpty(text)) + { + return text; + } + + string upperCaseText = text.ToUpper(); + return new string(upperCaseText.Select(c => + { + if (!char.IsLetter(c)) + { + return c; + } + int x = c - 'A'; + int y = (x + n) % 26; + return (char)(y + 'A'); + }).ToArray()); + } + + /// + /// 将给定的字符串按照 ROT 加密算法进行解密 + /// + /// 要解密的字符串 + /// 偏移量 + /// 解密后的字符串 + public static string RotDecrypt(string text, int n) + { + return RotEncrypt(text, 26 - n); + } + + #endregion + + #region Morse 电码 + + // Morse 电码表 + private static readonly Dictionary MORSE_TABLE = new Dictionary + { + {'A', ".-"}, + {'B', "-..."}, + {'C', "-.-."}, + {'D', "-.."}, + {'E', "."}, + {'F', "..-."}, + {'G', "--."}, + {'H', "...."}, + {'I', ".."}, + {'J', ".---"}, + {'K', "-.-"}, + {'L', ".-.."}, + {'M', "--"}, + {'N', "-."}, + {'O', "---"}, + {'P', ".--."}, + {'Q', "--.-"}, + {'R', ".-."}, + {'S', "..."}, + {'T', "-"}, + {'U', "..-"}, + {'V', "...-"}, + {'W', ".--"}, + {'X', "-.."}, + {'Y', "-.--"}, + {'Z', "--.."}, + {'0', "-----"}, + {'1', ".----"}, + {'2', "..---"}, + {'3', "...--"}, + {'4', "....-"}, + {'5', "....."}, + {'6', "-...."}, + {'7', "--..."}, + {'8', "---.."}, + {'9', "----."}, + {' ', " "} + }; + + /// + /// 将给定的字符串转换为 Morse 电码字符串 + /// + /// 要转换的字符串 + /// 转换后的 Morse 电码字符串 + public static string MorseEncode(string str) + { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + + List morseCodes = new List(); + foreach (char c in str.ToUpper()) + { + if (MORSE_TABLE.ContainsKey(c)) + { + morseCodes.Add(MORSE_TABLE[c]); + } + } + return string.Join(" ", morseCodes); + } + + /// + /// 将给定的 Morse 电码字符串转换为原始字符串 + /// + /// 要转换的 Morse 电码字符串 + /// 转换后的原始字符串 + public static string MorseDecode(string morseCode) + { + if (string.IsNullOrEmpty(morseCode)) + { + return string.Empty; + } + + string[] codes = morseCode.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + List chars = new List(); + foreach (string code in codes) + { + foreach (KeyValuePair kvp in MORSE_TABLE) + { + if (kvp.Value == code) + { + chars.Add(kvp.Key); + break; + } + } + } + return new string(chars.ToArray()); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/HashUtil.cs b/EasyTool.Core/CodeCategory/HashUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/HashUtil.cs rename to EasyTool.Core/CodeCategory/HashUtil.cs index 6913e7a..5ac7eb3 100644 --- a/EasyTool.Core/ToolCategory/HashUtil.cs +++ b/EasyTool.Core/CodeCategory/HashUtil.cs @@ -21,6 +21,7 @@ public static uint AdditiveHash(string str) { hash += c; } + return hash; } @@ -36,6 +37,7 @@ public static uint RotatingHash(string str) { hash = (hash << 4) ^ (hash >> 28) ^ c; } + return hash; } @@ -53,6 +55,7 @@ public static uint OneByOneHash(string str) hash += (hash << 10); hash ^= (hash >> 6); } + hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); @@ -71,6 +74,7 @@ public static uint Bernstein(string str) { hash = 33 * hash + c; } + return hash; } @@ -81,7 +85,7 @@ public static uint Bernstein(string str) /// 大质数 /// 哈希桶的数量 /// a的取值范围为[1, prime - 1] - /// b的取值范围 + /// b的取值范围 public static uint Universal(string str, uint prime, uint num_buckets, uint a, uint b) { uint hash = a; @@ -89,6 +93,7 @@ public static uint Universal(string str, uint prime, uint num_buckets, uint a, u { hash = hash * prime + c; } + hash = (hash * a + b) % num_buckets; return hash; } @@ -106,6 +111,7 @@ public static uint Zobrist(string str, uint[] table) { hash ^= table[str[i]]; } + return hash; } @@ -123,6 +129,7 @@ public static uint FnvHash(string str) hash *= fnv_prime; hash ^= c; } + return hash; } @@ -157,6 +164,7 @@ public static uint RsHash(string str, uint b, uint a) hash = hash * a + c; a = a * b; } + return hash; } @@ -172,6 +180,7 @@ public static uint JsHash(string str) { hash ^= ((hash << 5) + c + (hash >> 2)); } + return hash; } @@ -196,6 +205,7 @@ public static uint PjwHash(string str) hash = ((hash ^ (test >> (int)ThreeQuarters)) & (~HighBits)); } } + return hash; } @@ -216,8 +226,10 @@ public static uint ElfHash(string str) { hash ^= (x >> 24); } + hash &= ~x; } + return hash; } @@ -234,6 +246,7 @@ public static uint BkdrHash(string str, uint seed) { hash = hash * seed + c; } + return hash; } @@ -249,6 +262,7 @@ public static uint SdbmHash(string str) { hash = c + (hash << 6) + (hash << 16) - hash; } + return hash; } @@ -264,6 +278,7 @@ public static uint DjbHash(string str) { hash = ((hash << 5) + hash) + c; } + return hash; } @@ -279,6 +294,7 @@ public static uint DekHash(string str) { hash = ((hash << 5) ^ (hash >> 27)) ^ c; } + return hash; } @@ -302,6 +318,7 @@ public static uint ApHash(string str) hash ^= (~((hash << 11) ^ str[i] ^ (hash >> 5))); } } + return hash; } @@ -327,6 +344,7 @@ public static uint TianlHash(string str, uint len) hash += (hash << 10); hash ^= (hash >> 6); } + hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); @@ -393,6 +411,7 @@ public static uint JavaDefaultHash(string str) { h = 31 * h + c; } + hash = h; return hash; } @@ -412,8 +431,9 @@ public static ulong MixHash(string str) hash1 = (hash1 * seed) + c; hash2 = (hash2 * seed) + c + 1; } + return hash1 + (hash2 * 1566083941); } } - } +} diff --git a/EasyTool.Core/ToolCategory/HexUtil.cs b/EasyTool.Core/CodeCategory/HexUtil.cs similarity index 95% rename from EasyTool.Core/ToolCategory/HexUtil.cs rename to EasyTool.Core/CodeCategory/HexUtil.cs index 52b892f..102a626 100644 --- a/EasyTool.Core/ToolCategory/HexUtil.cs +++ b/EasyTool.Core/CodeCategory/HexUtil.cs @@ -86,28 +86,15 @@ public static string IntToHex(int number) } /// - /// 将16进制字符串中的所有字符转换为大写 - /// [Obsolete("请直接使用 hex.ToUpper()")] + /// 将16进制字符串转换为大写形式 /// /// 16进制字符串 - /// 大写16进制字符串 - [Obsolete("请直接使用 hex.ToUpper()", false)] + /// 大写形式的16进制字符串 public static string HexToUpper(string hex) { return hex.ToUpper(); } - /// - /// 将16进制字符串中的所有字符转换为小写 - /// [Obsolete("请直接使用 hex.ToLower()")] - /// - /// 16进制字符串 - /// 小写16进制字符串 - [Obsolete("请直接使用 hex.ToLower()", false)] - public static string HexToLower(string hex) - { - return hex.ToLower(); - } /// /// 获取16进制字符串中指定位置的字符 diff --git a/EasyTool.Core/CodeCategory/MorseUtil.cs b/EasyTool.Core/CodeCategory/MorseUtil.cs deleted file mode 100644 index 4341231..0000000 --- a/EasyTool.Core/CodeCategory/MorseUtil.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Morse 电码工具类 - /// - public static class MorseUtil - { - // Morse 电码表 - private static readonly Dictionary MORSE_TABLE = new Dictionary() - { - {'A', ".-"}, - {'B', "-..."}, - {'C', "-.-."}, - {'D', "-.."}, - {'E', "."}, - {'F', "..-."}, - {'G', "--."}, - {'H', "...."}, - {'I', ".."}, - {'J', ".---"}, - {'K', "-.-"}, - {'L', ".-.."}, - {'M', "--"}, - {'N', "-."}, - {'O', "---"}, - {'P', ".--."}, - {'Q', "--.-"}, - {'R', ".-."}, - {'S', "..."}, - {'T', "-"}, - {'U', "..-"}, - {'V', "...-"}, - {'W', ".--"}, - {'X', "-..-"}, - {'Y', "-.--"}, - {'Z', "--.."}, - {'0', "-----"}, - {'1', ".----"}, - {'2', "..---"}, - {'3', "...--"}, - {'4', "....-"}, - {'5', "....."}, - {'6', "-...."}, - {'7', "--..."}, - {'8', "---.."}, - {'9', "----."}, - {' ', " "} - }; - - /// - /// 将给定的字符串转换为 Morse 电码字符串。 - /// - /// 要转换的字符串 - /// 转换后的 Morse 电码字符串 - public static string Encode(string str) - { - if (string.IsNullOrEmpty(str)) - { - return string.Empty; - } - - List morseCodes = new List(); - foreach (char c in str.ToUpper()) - { - if (MORSE_TABLE.ContainsKey(c)) - { - morseCodes.Add(MORSE_TABLE[c]); - } - } - return string.Join(" ", morseCodes); - } - - - /// - /// 将给定的 Morse 电码字符串转换为原始字符串。 - /// - /// 要转换的 Morse 电码字符串 - /// 转换后的原始字符串 - public static string Decode(string morseCode) - { - if (string.IsNullOrEmpty(morseCode)) - { - return string.Empty; - } - - string[] codes = morseCode.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - List chars = new List(); - foreach (string code in codes) - { - foreach (KeyValuePair kvp in MORSE_TABLE) - { - if (kvp.Value == code) - { - chars.Add(kvp.Key); - break; - } - } - } - return new string(chars.ToArray()); - } - - } -} diff --git a/EasyTool.Core/CodeCategory/PunycodeUtil.cs b/EasyTool.Core/CodeCategory/PunycodeUtil.cs deleted file mode 100644 index cee3389..0000000 --- a/EasyTool.Core/CodeCategory/PunycodeUtil.cs +++ /dev/null @@ -1,254 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// Punycode 工具类 - /// - public static class PunycodeUtil - { - private const int BASE = 36; - private const int TMIN = 1; - private const int TMAX = 26; - private const int SKEW = 38; - private const int DAMP = 700; - private const int INITIAL_BIAS = 72; - private const int INITIAL_N = 128; - - private static readonly char[] DELIMITER = { '-' }; - - /// - /// 将给定的 Unicode 字符串按照 Punycode 编码规则进行编码。 - /// - /// 要编码的 Unicode 字符串 - /// 编码后的字符串 - public static string Encode(string input) - { - if (string.IsNullOrEmpty(input)) - { - return input; - } - - List inputChars = input.Select(c => (int)c).ToList(); - List basicChars = inputChars.Where(c => c < 0x80).ToList(); - List extendedChars = inputChars.Except(basicChars).ToList(); - - List output = new List(); - int n = INITIAL_N; - int delta = 0; - int bias = INITIAL_BIAS; - - // Encode the basic code points - foreach (int b in basicChars) - { - output.Add(b); - } - - int h = output.Count; - int bLength = basicChars.Count; - if (bLength > 0 && extendedChars.Count > 0) - { - output.Add('-'); - } - - // Main encoding loop - while (h < inputChars.Count) - { - int m = int.MaxValue; - foreach (int e in extendedChars) - { - if (e >= n && e < m) - { - m = e; - } - } - - delta += (m - n) * (h + 1); - n = m; - foreach (int e in extendedChars) - { - if (e < n) - { - delta++; - } - - if (e == n) - { - int q = delta; - int k = BASE; - while (true) - { - int t = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias); - if (q < t) - { - break; - } - - output.Add(GetCodePoint(t + (q - t) % (BASE - t))); - q = (q - t) / (BASE - t); - k += BASE; - } - - output.Add(GetCodePoint(q)); - bias = Adapt(delta, h + 1, h == bLength); - delta = 0; - h++; - } - } - - delta++; - n++; - } - - return new string(output.Select(c => (char)c).ToArray()); - } - - /// - /// 将给定的 Punycode 编码字符串进行解码,得到原始的 Unicode 字符串。 - /// - /// 要解码的 Punycode 编码字符串 - /// 原始的 Unicode 字符串 - public static string Decode(string input) - { - if (string.IsNullOrEmpty(input)) - { - return input; - } - - List output = new List(); - List inputChars = input.Select(c => (int)c).ToList(); - List basicChars = inputChars.Where(c => c < 0x80).ToList(); - int i = 0; - int n = INITIAL_N; - int bias = INITIAL_BIAS; - - // Find the last delimiter - int lastDelim = input.LastIndexOf('-'); - if (lastDelim < 0) - { - lastDelim = 0; - } - - // Decode the basic code points - for (int j = 0; j < lastDelim; j++) - { - int c = inputChars[j]; - if (!IsBasic(c)) - { - throw new ArgumentException("Invalid input string."); - } - - output.Add(c); - } - - // Main decoding loop - int p = lastDelim > 0 ? lastDelim + 1 : 0; - while (p < inputChars.Count) - { - int oldi = i; - int w = 1; - int k = BASE; - while (true) - { - if (p >= inputChars.Count) - { - throw new ArgumentException("Invalid input string."); - } - - int c = inputChars[p++]; - int digit = GetDigit(c); - if (digit >= BASE) - { - throw new ArgumentException("Invalid input string."); - } - - if (digit > (int.MaxValue - i) / w) - { - throw new ArgumentException("Invalid input string."); - } - - i += digit * w; - int t = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias); - if (digit < t) - { - break; - } - - if (w > int.MaxValue / (BASE - t)) - { - throw new ArgumentException("Invalid input string."); - } - - w *= BASE - t; - k += BASE; - } - - int delta = i - oldi; - output.Add(GetCodePoint(delta)); - bias = Adapt(delta, output.Count, oldi == 0); - n += i / output.Count; - i %= output.Count; - } - - return new string(output.Select(c => (char)c).ToArray()); - } - - private static bool IsBasic(int codePoint) - { - return codePoint < 0x80; - } - - private static int GetDigit(int codePoint) - { - if (codePoint - '0' < 10) - { - return codePoint - '0' + 26; - } - - if (codePoint - 'a' < 26) - { - return codePoint - 'a'; - } - - if (codePoint - 'A' < 26) - { - return codePoint - 'A'; - } - - throw new ArgumentException("Invalid input string."); - } - - private static int Adapt(int delta, int numPoints, bool firstTime) - { - delta = firstTime ? delta / DAMP : delta >> 1; - delta += delta / numPoints; - - int k = 0; - while (delta > ((BASE - TMIN) * TMAX) / 2) - { - delta /= BASE - TMIN; - k += BASE; - } - - return k + (((BASE - TMIN + 1) * delta) / (delta + SKEW)); - } - - private static int GetCodePoint(int digit) - { - if (digit < 26) - { - return digit + 'a'; - } - - if (digit < 36) - { - return digit - 26 + '0'; - } - - throw new ArgumentException("Invalid input string."); - } - } -} diff --git a/EasyTool.Core/CodeCategory/RotUtil.cs b/EasyTool.Core/CodeCategory/RotUtil.cs deleted file mode 100644 index f2b86c3..0000000 --- a/EasyTool.Core/CodeCategory/RotUtil.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// ROT 工具类 - /// - public static class RotUtil - { - /// - /// 将给定的字符串按照 ROT 加密算法进行加密。 - /// - /// 要加密的字符串 - /// 偏移量 - /// 加密后的字符串 - public static string Encrypt(string text, int n) - { - if (string.IsNullOrEmpty(text)) - { - return text; - } - - string upperCaseText = text.ToUpper(); - return new string(upperCaseText.Select(c => - { - if (!char.IsLetter(c)) - { - return c; - } - - int x = c - 'A'; - int y = (x + n) % 26; - return (char)(y + 'A'); - }).ToArray()); - } - - /// - /// 将给定的字符串按照 ROT 加密算法进行解密。 - /// - /// 要解密的字符串 - /// 偏移量 - /// 解密后的字符串 - public static string Decrypt(string text, int n) - { - return Encrypt(text, 26 - n); - } - } -} diff --git a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs index f482e24..c2ce442 100644 --- a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs @@ -32,6 +32,93 @@ public static bool IsNotEmpty(this T[]? array) #region 数组操作 + /// + /// 数组排序 + /// + public static T[] Sort(this T[]? array) where T : IComparable + { + if (array.IsEmpty()) + { + throw new ArgumentException("Array is empty."); + } + + T[] sortedArray = new T[array.Length]; + array.CopyTo(sortedArray, 0); + Array.Sort(sortedArray); + + return sortedArray; + } + + /// + /// 数组反转 + /// + public static T[] Reverse(this T[]? array) + { + if (array.IsEmpty()) + { + throw new ArgumentException("Array is empty."); + } + + T[] reversedArray = new T[array.Length]; + array.CopyTo(reversedArray, 0); + Array.Reverse(reversedArray); + + return reversedArray; + } + + /// + /// 判断两个数组是否完全相等 + /// + public static bool EqualsTo(this T[]? array, T[]? other) + { + if (array.IsEmpty() && other.IsEmpty()) + { + return true; + } + + if (array.IsEmpty() || other.IsEmpty()) + { + return false; + } + + if (array!.Length != other!.Length) + { + return false; + } + + for (int i = 0; i < array.Length; i++) + { + if (!array[i].Equals(other[i])) + { + return false; + } + } + + return true; + } + + /// + /// 合并两个数组 + /// + public static T[] Concat(this T[]? array, T[]? other) + { + if (array.IsEmpty()) + { + return other ?? Array.Empty(); + } + + if (other.IsEmpty()) + { + return array; + } + + T[] result = new T[array.Length + other.Length]; + array.CopyTo(result, 0); + other.CopyTo(result, array.Length); + + return result; + } + /// /// 随机打乱数组顺序(Fisher-Yates 洗牌算法) /// @@ -75,26 +162,6 @@ public static IEnumerable Chunk(this T[]? array, int chunkSize) } } - /// - /// 将数组的元素连接成字符串 - /// [Obsolete("请直接使用 string.Join(separator, array)")] - /// - /// 数组 - /// 分隔符,默认为逗号 - [Obsolete("请直接使用 string.Join(separator, array)", false)] - public static string Join(this T[]? array, string separator = ",") - { - if (array == null || array.Length == 0) - return string.Empty; - - return string.Join(separator, array); - } - - /// - /// 清除数组中的重复元素 - /// [Obsolete("请直接使用 array.Distinct().ToArray() (LINQ)")] - /// - [Obsolete("请直接使用 array.Distinct().ToArray() (LINQ)", false)] public static T[]? Distinct(this T[]? array) { if (array == null) @@ -133,18 +200,6 @@ public static string JoinFormat(this T[]? array, string separator, string for #region 数组查找 - /// - /// 查找数组中满足条件的第一个元素的索引 - /// [Obsolete("请直接使用 Array.FindIndex(array, predicate)")] - /// - [Obsolete("请直接使用 Array.FindIndex(array, predicate)", false)] - public static int FindIndex(this T[]? array, Predicate predicate) - { - if (array == null) - return -1; - - return Array.FindIndex(array, predicate); - } /// /// 查找数组中满足条件的所有元素的索引 @@ -180,57 +235,6 @@ public static bool Contains(this T[]? array, T value, IEqualityComparer co #region 数组转换 - /// - /// 将数组转换为 HashSet - /// [Obsolete("请直接使用 new HashSet(array)")] - /// - [Obsolete("请直接使用 new HashSet(array)", false)] - public static HashSet ToHashSet(this T[]? array) - { - if (array == null) - return new HashSet(); - - return new HashSet(array); - } - - /// - /// 将数组转换为 Queue - /// [Obsolete("请直接使用 new Queue(array)")] - /// - [Obsolete("请直接使用 new Queue(array)", false)] - public static Queue ToQueue(this T[]? array) - { - if (array == null) - return new Queue(); - - return new Queue(array); - } - - /// - /// 将数组转换为 Stack - /// [Obsolete("请直接使用 new Stack(array)")] - /// - [Obsolete("请直接使用 new Stack(array)", false)] - public static Stack ToStack(this T[]? array) - { - if (array == null) - return new Stack(); - - return new Stack(array); - } - - /// - /// 将数组转换为 LinkedList - /// [Obsolete("请直接使用 new LinkedList(array)")] - /// - [Obsolete("请直接使用 new LinkedList(array)", false)] - public static LinkedList ToLinkedList(this T[]? array) - { - if (array == null) - return new LinkedList(); - - return new LinkedList(array); - } /// /// 将二维数组展平为一维数组 @@ -372,21 +376,6 @@ public static T[] Append(this T[]? array, params T[]? items) #region 数组遍历 - /// - /// 遍历数组并对每个元素执行指定操作 - /// [Obsolete("请直接使用 Array.ForEach(array, action) 或 foreach 循环")] - /// - [Obsolete("请直接使用 Array.ForEach(array, action) 或 foreach 循环", false)] - public static void ForEach(this T[]? array, Action action) - { - if (array == null || action == null) - return; - - foreach (var item in array) - { - action(item); - } - } /// /// 遍历数组并对每个元素及其索引执行指定操作 @@ -406,24 +395,6 @@ public static void ForEach(this T[]? array, Action action) #region 数组统计 - /// - /// 统计数组中满足条件的元素数量 - /// [Obsolete("请直接使用 array.Count(predicate) (LINQ)")] - /// - [Obsolete("请直接使用 array.Count(predicate) (LINQ)", false)] - public static int Count(this T[]? array, Func predicate) - { - if (array == null) - return 0; - - int count = 0; - foreach (var item in array) - { - if (predicate(item)) - count++; - } - return count; - } #endregion } diff --git a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs index eaaa2ea..22ba15b 100644 --- a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs +++ b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs @@ -42,29 +42,6 @@ public static void AddRange(this IDictionary destina } } - /// - /// 返回字典中键的集合 - /// [Obsolete("请直接使用 dictionary.Keys")] - /// - /// 要获取键的字典 - /// 字典中所有键的集合 - [Obsolete("请直接使用 dictionary.Keys", false)] - public static IEnumerable GetKeys(this IDictionary dictionary) - { - return dictionary.Keys; - } - - /// - /// 返回字典中值的集合 - /// [Obsolete("请直接使用 dictionary.Values")] - /// - /// 要获取值的字典 - /// 字典中所有值的集合 - [Obsolete("请直接使用 dictionary.Values", false)] - public static IEnumerable GetValues(this IDictionary dictionary) - { - return dictionary.Values; - } /// /// 从字典中删除指定的键 diff --git a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs b/EasyTool.Core/CollectionsCategory/IteratorUtil.cs deleted file mode 100644 index f4aa6e6..0000000 --- a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// 迭代器工具类 - /// - /// 注意:此类中的方法与 System.Linq 提供的功能高度相似。 - /// 对于新代码,建议优先使用 LINQ 标准查询运算符(如 Where、Select、Take、Skip、OrderBy、GroupBy 等)。 - /// 此类保留用于向后兼容和特定场景需求。 - /// - public static class IteratorUtil - { - /// - /// 将一个数组转换为一个迭代器 - /// - public static IEnumerable AsIterator(this T[] array) - { - foreach (var item in array) - { - yield return item; - } - } - - /// - /// 过滤掉一个迭代器中不符合条件的元素 - /// [Obsolete("请直接使用 source.Where(predicate) (LINQ)")] - /// - [Obsolete("请直接使用 source.Where(predicate) (LINQ)", false)] - public static IEnumerable Filter(this IEnumerable source, Func predicate) - { - foreach (var item in source) - { - if (predicate(item)) - { - yield return item; - } - } - } - - /// - /// 对一个迭代器中的每个元素进行转换 - /// [Obsolete("请直接使用 source.Select(selector) (LINQ)")] - /// - [Obsolete("请直接使用 source.Select(selector) (LINQ)", false)] - public static IEnumerable Map(this IEnumerable source, Func selector) - { - foreach (var item in source) - { - yield return selector(item); - } - } - - /// - /// 从一个迭代器中取出前 n 个元素 - /// [Obsolete("请直接使用 source.Take(count) (LINQ)")] - /// - [Obsolete("请直接使用 source.Take(count) (LINQ)", false)] - public static IEnumerable Take(this IEnumerable source, int count) - { - foreach (var item in source) - { - if (count-- > 0) - { - yield return item; - } - else - { - break; - } - } - } - - /// - /// 跳过一个迭代器中的前 n 个元素 - /// [Obsolete("请直接使用 source.Skip(count) (LINQ)")] - /// - [Obsolete("请直接使用 source.Skip(count) (LINQ)", false)] - public static IEnumerable Skip(this IEnumerable source, int count) - { - foreach (var item in source) - { - if (count-- > 0) - { - continue; - } - else - { - yield return item; - } - } - } - - /// - /// 将一个迭代器的元素分组 - /// [Obsolete("请直接使用 source.GroupBy(keySelector, x => x) (LINQ)")] - /// - [Obsolete("请直接使用 source.GroupBy(keySelector, x => x) (LINQ)", false)] - public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector) - { - return source.GroupBy(keySelector, x => x); - } - - /// - /// 将一个迭代器的元素按照指定的方式分组 - /// - public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector) - { - var dictionary = new Dictionary>(); - foreach (var item in source) - { - var key = keySelector(item); - var element = elementSelector(item); - if (!dictionary.ContainsKey(key)) - { - dictionary[key] = new List(); - } - dictionary[key].Add(element); - } - foreach (var group in dictionary) - { - yield return new Grouping(group.Key, group.Value); - } - } - - private class Grouping : IGrouping - { - private readonly List _elements; - - public Grouping(TKey key, List elements) - { - Key = key; - _elements = elements; - } - public TKey Key { get; } - - public IEnumerator GetEnumerator() - { - return _elements.GetEnumerator(); - } - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } - - /// - /// 对一个迭代器中的元素进行排序 - /// [Obsolete("请直接使用 source.OrderBy(keySelector) (LINQ)")] - /// - [Obsolete("请直接使用 source.OrderBy(keySelector) (LINQ)", false)] - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector) - { - return source.OrderBy(keySelector, Comparer.Default); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行排序 - /// - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer) - { - return source.OrderBy(keySelector, comparer, false); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行排序,并指定排序方向 - /// - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer, bool descending) - { - return descending ? source.OrderByDescending(keySelector, comparer) : source.OrderBy(keySelector, comparer); - } - - /// - /// 对一个迭代器中的元素进行倒序排序 - /// [Obsolete("请直接使用 source.OrderByDescending(keySelector) (LINQ)")] - /// - [Obsolete("请直接使用 source.OrderByDescending(keySelector) (LINQ)", false)] - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector) - { - return source.OrderByDescending(keySelector, Comparer.Default); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行倒序排序 - /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer) - { - return source.OrderByDescending(keySelector, comparer, false); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行倒序排序,并指定排序方向 - /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer, bool descending) - { - return descending ? source.OrderBy(keySelector, comparer) : source.OrderByDescending(keySelector, comparer); - } - } -} diff --git a/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs b/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs deleted file mode 100644 index 3a1487a..0000000 --- a/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool.Extension -{ - - /// - /// 双向链表工具类 - /// - public static class LinkedListExtension - { - /// - /// 将双向链表中的某个节点移动到链表的结尾处。 - /// - /// 双向链表元素类型 - /// 双向链表 - /// 要移动的节点 - public static void MoveLast(this LinkedList list, LinkedListNode node) => LinkedListUtil.MoveLast(list,node); - - /// - /// 将双向链表中移动到最前方 - /// - /// 双向链表元素类型 - /// 双向链表 - /// 要移动的节点 - public static void MoveFirst(this LinkedList list, LinkedListNode node) => LinkedListUtil.MoveFirst(list,node); - - } -} diff --git a/EasyTool.Core/CollectionsCategory/ListExtension.cs b/EasyTool.Core/CollectionsCategory/ListExtension.cs index 31af6e4..15cd2b4 100644 --- a/EasyTool.Core/CollectionsCategory/ListExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ListExtension.cs @@ -5,8 +5,58 @@ namespace EasyTool.Extension { + /// + /// List 集合扩展方法 + /// public static class ListExtension - { + { + #region 列表合并 + + /// + /// 将指定的列表连接起来,形成一个新的列表。 + /// + /// 列表元素类型 + /// 要连接的列表 + /// 连接后的新列表 + public static List Concat(this IEnumerable> lists) + { + return lists.SelectMany(x => x).ToList(); + } + + /// + /// 将指定的列表连接起来,形成一个新的列表。 + /// + /// 列表元素类型 + /// 要连接的列表 + /// 连接后的新列表 + public static List Concat(this List list, params List[] lists) + { + return Concat(new[] { list }.Concat(lists)); + } + + #endregion + + #region 列表分页 + + /// + /// 将列表中的元素分页显示。 + /// + /// 列表元素类型 + /// 要分页的列表 + /// 每页显示的元素数量 + /// 要显示的页码,从 0 开始 + /// 指定页的元素列表 + public static List Page(this List list, int pageSize, int pageIndex) + { + return list.Skip(pageIndex * pageSize) + .Take(pageSize) + .ToList(); + } + + #endregion + + #region 列表比较 + /// /// 判断两个列表是否相等。 /// @@ -14,6 +64,33 @@ public static class ListExtension /// 要比较的第一个列表 /// 要比较的第二个列表 /// 如果两个列表相等,则返回 true;否则返回 false - public static bool Equals(this List list1, List list2)=> ListUtil.Equals(list1, list2); + public static bool Equals(this List? list1, List? list2) + { + if (list1 == null && list2 == null) + { + return true; + } + else if (list1 == null || list2 == null) + { + return false; + } + else if (list1.Count != list2.Count) + { + return false; + } + else + { + for (int i = 0; i < list1.Count; i++) + { + if (!EqualityComparer.Default.Equals(list1[i], list2[i])) + { + return false; + } + } + return true; + } + } + + #endregion } } diff --git a/EasyTool.Core/CollectionsCategory/ListUtil.cs b/EasyTool.Core/CollectionsCategory/ListUtil.cs deleted file mode 100644 index cb49685..0000000 --- a/EasyTool.Core/CollectionsCategory/ListUtil.cs +++ /dev/null @@ -1,272 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - public class ListUtil - { - /// - /// 在列表中查找元素,并返回其索引。如果未找到,则返回 -1。 - /// [Obsolete("请直接使用 list.IndexOf(item)")] - /// - /// 列表元素类型 - /// 要查找的列表 - /// 要查找的元素 - /// 元素在列表中的索引,如果未找到则返回 -1 - [Obsolete("请直接使用 list.IndexOf(item)", false)] - public static int IndexOf(List list, T item) - { - return list.IndexOf(item); - } - - /// - /// 向列表中添加多个元素。 - /// [Obsolete("请直接使用 list.AddRange(items)")] - /// - /// 列表元素类型 - /// 要添加元素的列表 - /// 要添加到列表中的元素 - [Obsolete("请直接使用 list.AddRange(items)", false)] - public static void AddRange(List list, IEnumerable items) - { - list.AddRange(items); - } - - /// - /// 在列表中删除指定索引处的元素。 - /// [Obsolete("请直接使用 list.RemoveAt(index)")] - /// - /// 列表元素类型 - /// 要删除元素的列表 - /// 要删除元素的索引 - [Obsolete("请直接使用 list.RemoveAt(index)", false)] - public static void RemoveAt(List list, int index) - { - list.RemoveAt(index); - } - - /// - /// 从列表中删除指定元素的第一个匹配项。 - /// [Obsolete("请直接使用 list.Remove(item)")] - /// - /// 列表元素类型 - /// 要删除元素的列表 - /// 要删除的元素 - /// 如果找到并成功删除元素,则返回 true;否则返回 false - [Obsolete("请直接使用 list.Remove(item)", false)] - public static bool Remove(List list, T item) - { - return list.Remove(item); - } - - /// - /// 将指定的列表连接起来,形成一个新的列表。 - /// - /// 列表元素类型 - /// 要连接的列表 - /// 连接后的新列表 - public static List Concat(IEnumerable> lists) - { - return lists.SelectMany(x => x).ToList(); - } - - /// - /// 将指定的列表连接起来,形成一个新的列表。 - /// - /// 列表元素类型 - /// 要连接的列表 - /// 连接后的新列表 - public static List Concat(params List[] lists) - { - return Concat((IEnumerable>)lists); - } - - /// - /// 返回一个新的列表,其中包含指定列表中的元素,但不包括重复元素。 - /// [Obsolete("请直接使用 list.Distinct().ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 要去重的列表 - /// 去重后的新列表 - [Obsolete("请直接使用 list.Distinct().ToList() (LINQ)", false)] - public static List Distinct(List list) - { - return list.Distinct().ToList(); - } - - /// - /// 根据指定的条件筛选出列表中符合条件的元素。 - /// [Obsolete("请直接使用 list.Where(predicate).ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 要筛选的列表 - /// 筛选条件 - /// 符合条件的元素列表 - [Obsolete("请直接使用 list.Where(predicate).ToList() (LINQ)", false)] - public static List Where(List list, Func predicate) - { - return list.Where(predicate).ToList(); - } - - /// - /// 将列表中的每个元素应用到指定的转换函数,并返回转换后的新列表。 - /// [Obsolete("请直接使用 list.Select(selector).ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 转换后的元素类型 - /// 要转换的列表 - /// 转换函数 - /// 转换后的新列表 - [Obsolete("请直接使用 list.Select(selector).ToList() (LINQ)", false)] - public static List Select(List list, Func selector) - { - return list.Select(selector).ToList(); - } - - /// - /// 对列表中的每个元素应用指定的操作。 - /// [Obsolete("请直接使用 list.ForEach(action)")] - /// - /// 列表元素类型 - /// 要应用操作的列表 - /// 要应用的操作 - [Obsolete("请直接使用 list.ForEach(action)", false)] - public static void ForEach(List list, Action action) - { - list.ForEach(action); - } - - /// - /// 将列表中的元素排序。 - /// [Obsolete("请直接使用 list.Sort()")] - /// - /// 列表元素类型 - /// 要排序的列表 - [Obsolete("请直接使用 list.Sort()", false)] - public static void Sort(List list) - { - list.Sort(); - } - - /// - /// 将列表中的元素按指定的比较器排序。 - /// [Obsolete("请直接使用 list.Sort(comparer)")] - /// - /// 列表元素类型 - /// 要排序的列表 - /// 比较器 - [Obsolete("请直接使用 list.Sort(comparer)", false)] - public static void Sort(List list, IComparer comparer) - { - list.Sort(comparer); - } - - /// - /// 将列表中的元素分页显示。 - /// - /// 列表元素类型 - /// 要分页的列表 - /// 每页显示的元素数量 - /// 要显示的页码,从 0 开始 - /// 指定页的元素列表 - public static List Page(List list, int pageSize, int pageIndex) - { - return list.Skip(pageIndex * pageSize) - .Take(pageSize) - .ToList(); - } - - /// - /// 向列表中批量添加元素。 - /// [Obsolete("请直接使用 list.AddRange(items)")] - /// - /// 列表元素类型 - /// 要添加元素的列表 - /// 要添加到列表中的元素 - [Obsolete("请直接使用 list.AddRange(items)", false)] - public static void AddRange(List list, params T[] items) - { - list.AddRange(items); - } - - /// - /// 判断两个列表是否相等。 - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 如果两个列表相等,则返回 true;否则返回 false - public static bool Equals(List list1, List list2) - { - if (list1 == null && list2 == null) - { - return true; - } - else if (list1 == null || list2 == null) - { - return false; - } - else if (list1.Count != list2.Count) - { - return false; - } - else - { - for (int i = 0; i < list1.Count; i++) - { - if (!EqualityComparer.Default.Equals(list1[i], list2[i])) - { - return false; - } - } - - return true; - } - } - - /// - /// 返回两个列表的交集。 - /// [Obsolete("请直接使用 list1.Intersect(list2).ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 交集列表 - [Obsolete("请直接使用 list1.Intersect(list2).ToList() (LINQ)", false)] - public static List Intersect(List list1, List list2) - { - return list1.Intersect(list2).ToList(); - } - - /// - /// 返回两个列表的并集。 - /// [Obsolete("请直接使用 list1.Union(list2).ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 并集列表 - [Obsolete("请直接使用 list1.Union(list2).ToList() (LINQ)", false)] - public static List Union(List list1, List list2) - { - return list1.Union(list2).ToList(); - } - - /// - /// 返回两个列表的差集。 - /// [Obsolete("请直接使用 list1.Except(list2).ToList() (LINQ)")] - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 差集列表 - [Obsolete("请直接使用 list1.Except(list2).ToList() (LINQ)", false)] - public static List Except(List list1, List list2) - { - return list1.Except(list2).ToList(); - } - - } -} diff --git a/EasyTool.Core/CollectionsCategory/QueueExtension.cs b/EasyTool.Core/CollectionsCategory/QueueExtension.cs deleted file mode 100644 index 4f8dfc0..0000000 --- a/EasyTool.Core/CollectionsCategory/QueueExtension.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; - -namespace EasyTool.Extension -{ - /// - /// 队列工具类 - /// - public static class QueueExtension - { - - /// - /// 将指定集合中的元素添加到队列的末尾。 - /// - /// 队列元素类型 - /// 队列 - /// 要添加到队列中的集合 - public static void EnqueueRange(this Queue queue, IEnumerable collection) => QueueUtil.EnqueueRange(queue, collection); - - /// - /// 从队列中移除指定元素的第一个匹配项。 - /// - /// 队列元素类型 - /// 队列 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(this Queue queue, T item) => QueueUtil.Remove(queue, item); - - } -} diff --git a/EasyTool.Core/CollectionsCategory/StackExtension.cs b/EasyTool.Core/CollectionsCategory/StackExtension.cs deleted file mode 100644 index a1477f9..0000000 --- a/EasyTool.Core/CollectionsCategory/StackExtension.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool.CollectionsCategory -{ - public static class StackExtension - { - /// - /// 从堆栈中移除指定元素的第一个匹配项。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(this Stack stack, T item)=> StackUtil.Remove(stack, item); - } -} diff --git a/EasyTool.Core/ConvertCategory/ByteExtension.cs b/EasyTool.Core/ConvertCategory/ByteExtension.cs index 7208906..9a2be43 100644 --- a/EasyTool.Core/ConvertCategory/ByteExtension.cs +++ b/EasyTool.Core/ConvertCategory/ByteExtension.cs @@ -127,31 +127,6 @@ public static byte[] FromHexToBytes(this string hex) return bytes; } - /// - /// 将字节数组转换为Base64字符串 - /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] - /// - [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] - public static string ToBase64(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - return Convert.ToBase64String(bytes); - } - - /// - /// 从Base64字符串转换为字节数组 - /// [Obsolete("请直接使用 Convert.FromBase64String(base64)")] - /// - [Obsolete("请直接使用 Convert.FromBase64String(base64)", false)] - public static byte[] FromBase64ToBytes(this string base64) - { - if (string.IsNullOrWhiteSpace(base64)) - return Array.Empty(); - - return Convert.FromBase64String(base64); - } /// /// 将字节数组转换为二进制字符串 @@ -430,59 +405,6 @@ public static byte[] ToBytes(this short value) #region 字节数组编码解码 - /// - /// 将字节数组按UTF-8编码转换为字符串 - /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] - /// - [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] - public static string ToUtf8String(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - return Encoding.UTF8.GetString(bytes); - } - - /// - /// 将字节数组按指定编码转换为字符串 - /// [Obsolete("请直接使用 encoding.GetString(bytes)")] - /// - [Obsolete("请直接使用 encoding.GetString(bytes)", false)] - public static string ToString(this byte[] bytes, Encoding encoding) - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - encoding ??= Encoding.UTF8; - return encoding.GetString(bytes); - } - - /// - /// 将字符串按UTF-8编码转换为字节数组 - /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)")] - /// - [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)", false)] - public static byte[] ToUtf8Bytes(this string str) - { - if (string.IsNullOrEmpty(str)) - return Array.Empty(); - - return Encoding.UTF8.GetBytes(str); - } - - /// - /// 将字符串按指定编码转换为字节数组 - /// [Obsolete("请直接使用 encoding.GetBytes(str)")] - /// - [Obsolete("请直接使用 encoding.GetBytes(str)", false)] - public static byte[] ToBytes(this string str, Encoding encoding) - { - if (string.IsNullOrEmpty(str)) - return Array.Empty(); - - encoding ??= Encoding.UTF8; - return encoding.GetBytes(str); - } #endregion diff --git a/EasyTool.Core/ConvertCategory/ConvertExtension.cs b/EasyTool.Core/ConvertCategory/ConvertExtension.cs index 907c9bf..1402df8 100644 --- a/EasyTool.Core/ConvertCategory/ConvertExtension.cs +++ b/EasyTool.Core/ConvertCategory/ConvertExtension.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; @@ -203,16 +203,6 @@ public static string ToIntString(this bool b) return b ? "1" : "0"; } - /// - /// 布尔值转换为整数1或者0 - /// [Obsolete("请直接使用 Convert.ToInt32(b)")] - /// - [Obsolete("请直接使用 Convert.ToInt32(b)", false)] - public static int ToInt(this bool b) - { - return b ? 1 : 0; - } - /// /// 布尔值转换为中文 /// @@ -268,21 +258,6 @@ public static string ToHex(this byte[] bytes, bool lowerCase = true) return bytes; } - /// - /// 转换为Base64 - /// [Obsolete("请直接使用 Convert.ToBase64String(bytes)")] - /// - /// - /// - [Obsolete("请直接使用 Convert.ToBase64String(bytes)", false)] - public static string ToBase64(this byte[] bytes) - { - if (bytes == null) - return string.Empty; - - return Convert.ToBase64String(bytes); - } - /// @@ -313,26 +288,6 @@ public static DateTime TimestampToDateTime(this string timeStamp) return dd.Add(ts); } - /// - /// 字符串转Guid - /// [Obsolete("请直接使用 Guid.TryParse(guid, out var result) 或 new Guid(guid)")] - /// - /// - /// - [Obsolete("请直接使用 Guid.TryParse(guid, out var result)", false)] - public static Guid? ToGuid(this string guid) - { - try - { - return new Guid(guid); - } - catch (Exception) - { - - throw; - } - } - #endregion #region 数字转字符串前面补零 diff --git a/EasyTool.Core/ConvertCategory/ConvertUtil.cs b/EasyTool.Core/ConvertCategory/ConvertUtil.cs deleted file mode 100644 index 015812a..0000000 --- a/EasyTool.Core/ConvertCategory/ConvertUtil.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System; - -namespace EasyTool -{ - /// - /// 类型转换工具类 - /// - public static class ConvertUtil - { - /// - /// 将对象转换为指定类型,转换失败返回指定类型的默认值 - /// - public static T? To(object? value) - { - try - { - return (T)Convert.ChangeType(value, typeof(T))!; - } - catch - { - return default; - } - } - - /// - /// 将字符串转换为整型,转换失败返回0 - /// - public static int ToInt32(string? value) - { - int result; - if (int.TryParse(value, out result)) - { - return result; - } - return 0; - } - - /// - /// 将字符串转换为长整型,转换失败返回0 - /// - public static long ToInt64(string? value) - { - long result; - if (long.TryParse(value, out result)) - { - return result; - } - return 0; - } - - /// - /// 将字符串转换为布尔型,转换失败返回默认值,默认值false - /// - public static bool ToBoolean(string? data, bool defValue = false) - { - //如果为空则返回默认值 - if (string.IsNullOrEmpty(data)) - { - return defValue; - } - - bool temp = false; - if (bool.TryParse(data, out temp)) - { - return temp; - } - else - { - return defValue; - } - } - - /// - /// 将对象转换为布尔型,转换失败返回默认值,默认值false - /// - public static bool ToBoolean(object? data, bool defValue = false) - { - //如果为空则返回默认值 - if (data == null || Convert.IsDBNull(data)) - { - return defValue; - } - - try - { - return Convert.ToBoolean(data); - } - catch - { - return defValue; - } - } - - /// - /// 将字符串转换为单精度浮点型,转换失败返回0 - /// - public static float ToSingle(string? value) - { - float result; - if (float.TryParse(value, out result)) - { - return result; - } - return 0; - } - - /// - /// 将字符串转换为双精度浮点型,转换失败返回0 - /// - public static double ToDouble(string? value) - { - double result; - if (double.TryParse(value, out result)) - { - return result; - } - return 0; - } - - /// - /// 将字符串转换为十进制数,转换失败返回0 - /// - public static decimal ToDecimal(string? value) - { - decimal result; - if (decimal.TryParse(value, out result)) - { - return result; - } - return 0; - } - - /// - /// 将字符串转换为日期时间,转换失败返回DateTime.MinValue - /// - public static DateTime ToDateTime(string? value) - { - DateTime result; - if (DateTime.TryParse(value, out result)) - { - return result; - } - return DateTime.MinValue; - } - - /// - /// 将字符串转换为枚举类型,转换失败返回默认值 - /// - public static T ToEnum(string? value, T? defaultValue = default) where T : struct - { - T result; - if (Enum.TryParse(value, out result)) - { - return result; - } - return defaultValue ?? default; - } - - - } -} diff --git a/EasyTool.Core/ConvertCategory/NumberExtension.cs b/EasyTool.Core/ConvertCategory/NumberExtension.cs index 128b7b8..a9cae99 100644 --- a/EasyTool.Core/ConvertCategory/NumberExtension.cs +++ b/EasyTool.Core/ConvertCategory/NumberExtension.cs @@ -289,139 +289,11 @@ public static double Cube(this double value) return value * value * value; } - /// - /// 计算数值的绝对值 - /// [Obsolete("请直接使用 Math.Abs(value)")] - /// - [Obsolete("请直接使用 Math.Abs(value)", false)] - public static int Abs(this int value) - { - return Math.Abs(value); - } - - /// - /// 计算数值的绝对值 - /// [Obsolete("请直接使用 Math.Abs(value)")] - /// - [Obsolete("请直接使用 Math.Abs(value)", false)] - public static long Abs(this long value) - { - return Math.Abs(value); - } - - /// - /// 计算数值的绝对值 - /// [Obsolete("请直接使用 Math.Abs(value)")] - /// - [Obsolete("请直接使用 Math.Abs(value)", false)] - public static float Abs(this float value) - { - return Math.Abs(value); - } - - /// - /// 计算数值的绝对值 - /// [Obsolete("请直接使用 Math.Abs(value)")] - /// - [Obsolete("请直接使用 Math.Abs(value)", false)] - public static double Abs(this double value) - { - return Math.Abs(value); - } - - /// - /// 计算数值的绝对值 - /// [Obsolete("请直接使用 Math.Abs(value)")] - /// - [Obsolete("请直接使用 Math.Abs(value)", false)] - public static decimal Abs(this decimal value) - { - return Math.Abs(value); - } #endregion #region 数值判断 - /// - /// 判断浮点数是否为 NaN - /// [Obsolete("请直接使用 float.IsNaN(value)")] - /// - [Obsolete("请直接使用 float.IsNaN(value)", false)] - public static bool IsNaN(this float value) - { - return float.IsNaN(value); - } - - /// - /// 判断双精度浮点数是否为 NaN - /// [Obsolete("请直接使用 double.IsNaN(value)")] - /// - [Obsolete("请直接使用 double.IsNaN(value)", false)] - public static bool IsNaN(this double value) - { - return double.IsNaN(value); - } - - /// - /// 判断浮点数是否为无穷大 - /// [Obsolete("请直接使用 float.IsInfinity(value)")] - /// - [Obsolete("请直接使用 float.IsInfinity(value)", false)] - public static bool IsInfinity(this float value) - { - return float.IsInfinity(value); - } - - /// - /// 判断双精度浮点数是否为无穷大 - /// [Obsolete("请直接使用 double.IsInfinity(value)")] - /// - [Obsolete("请直接使用 double.IsInfinity(value)", false)] - public static bool IsInfinity(this double value) - { - return double.IsInfinity(value); - } - - /// - /// 判断浮点数是否为正无穷大 - /// [Obsolete("请直接使用 float.IsPositiveInfinity(value)")] - /// - [Obsolete("请直接使用 float.IsPositiveInfinity(value)", false)] - public static bool IsPositiveInfinity(this float value) - { - return float.IsPositiveInfinity(value); - } - - /// - /// 判断双精度浮点数是否为正无穷大 - /// [Obsolete("请直接使用 double.IsPositiveInfinity(value)")] - /// - [Obsolete("请直接使用 double.IsPositiveInfinity(value)", false)] - public static bool IsPositiveInfinity(this double value) - { - return double.IsPositiveInfinity(value); - } - - /// - /// 判断浮点数是否为负无穷大 - /// [Obsolete("请直接使用 float.IsNegativeInfinity(value)")] - /// - [Obsolete("请直接使用 float.IsNegativeInfinity(value)", false)] - public static bool IsNegativeInfinity(this float value) - { - return float.IsNegativeInfinity(value); - } - - /// - /// 判断双精度浮点数是否为负无穷大 - /// [Obsolete("请直接使用 double.IsNegativeInfinity(value)")] - /// - [Obsolete("请直接使用 double.IsNegativeInfinity(value)", false)] - public static bool IsNegativeInfinity(this double value) - { - return double.IsNegativeInfinity(value); - } #endregion diff --git a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs index bbf5b8d..bad196b 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs @@ -15,6 +15,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在周的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfWeek(date)")] public static DateTime GetFirstDayOfWeek(this DateTime date) => DateTimeUtil.GetFirstDayOfWeek(date); /// @@ -22,6 +23,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在月份的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfMonth(date)")] public static DateTime GetFirstDayOfMonth(this DateTime date) => DateTimeUtil.GetFirstDayOfMonth(date); @@ -30,6 +32,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在季度的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfQuarter(date)")] public static DateTime GetFirstDayOfQuarter(this DateTime date) => DateTimeUtil.GetFirstDayOfQuarter(date); /// @@ -37,6 +40,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在年份的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfYear(date)")] public static DateTime GetFirstDayOfYear(this DateTime date) => DateTimeUtil.GetFirstDayOfYear(date); /// @@ -44,6 +48,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期和当前日期之间的天数差。 + [Obsolete("请直接使用 DateTimeUtil.GetDaysBetween(date)")] public static int GetDaysBetween(this DateTime date) => DateTimeUtil.GetDaysBetween(date); /// @@ -52,6 +57,7 @@ public static class DateTimeExtension /// 第一个日期。 /// 第二个日期。 /// 两个日期之间的天数差。 + [Obsolete("请直接使用 DateTimeUtil.GetDaysBetween(date1, date2)")] public static int GetDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetDaysBetween(date1, date2); /// @@ -59,6 +65,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期和当前日期之间的工作日数差。 + [Obsolete("请直接使用 DateTimeUtil.GetWorkDaysBetween(date)")] public static int GetWorkDaysBetween(this DateTime date) => DateTimeUtil.GetWorkDaysBetween(date); /// @@ -67,6 +74,7 @@ public static class DateTimeExtension /// 第一个日期。 /// 第二个日期。 /// 两个日期之间的工作日数差。 + [Obsolete("请直接使用 DateTimeUtil.GetWorkDaysBetween(date1, date2)")] public static int GetWorkDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetWorkDaysBetween(date1, date2); /// @@ -74,6 +82,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 如果是工作日,则返回 true;否则返回 false。 + [Obsolete("请直接使用 DateTimeUtil.IsWorkDay(date)")] public static bool IsWorkDay(this DateTime date) => DateTimeUtil.IsWorkDay(date); /// @@ -81,6 +90,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在周的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetWeekDays(date)")] public static List GetWeekDays(this DateTime date) => DateTimeUtil.GetWeekDays(date); /// @@ -88,6 +98,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在月份的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetMonthDays(date)")] public static List GetMonthDays(this DateTime date) => DateTimeUtil.GetMonthDays(date); /// @@ -95,6 +106,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在季度的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetQuarterDays(date)")] public static List GetQuarterDays(this DateTime date) => DateTimeUtil.GetQuarterDays(date); /// @@ -102,6 +114,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在年份的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetYearDays(date)")] public static List GetYearDays(this DateTime date) => DateTimeUtil.GetYearDays(date); @@ -182,45 +195,6 @@ public static int ToAge(this DateTime birthDate) return age; } - /// - /// 将日期转换为 Unix 时间戳(秒) - /// [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeSeconds()")] - /// - [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeSeconds()", false)] - public static long ToUnixTimestamp(this DateTime date) - { - return new DateTimeOffset(date).ToUnixTimeSeconds(); - } - - /// - /// 将日期转换为 Unix 时间戳(毫秒) - /// [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeMilliseconds()")] - /// - [Obsolete("请使用 new DateTimeOffset(date).ToUnixTimeMilliseconds()", false)] - public static long ToUnixTimestampMilliseconds(this DateTime date) - { - return new DateTimeOffset(date).ToUnixTimeMilliseconds(); - } - - /// - /// 从 Unix 时间戳(秒)转换为日期 - /// [Obsolete("请使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime")] - /// - [Obsolete("请使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime", false)] - public static DateTime FromUnixTimestamp(this long timestamp) - { - return DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime; - } - - /// - /// 从 Unix 时间戳(毫秒)转换为日期 - /// [Obsolete("请使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime")] - /// - [Obsolete("请使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime", false)] - public static DateTime FromUnixTimestampMilliseconds(this long timestamp) - { - return DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime; - } /// /// 判断是否是周末(周六或周日) @@ -301,15 +275,6 @@ public static string ToChineseWeekDayShort(this DateTime date) }; } - /// - /// 判断是否是闰年 - /// [Obsolete("请直接使用 DateTime.IsLeapYear(date.Year)")] - /// - [Obsolete("请直接使用 DateTime.IsLeapYear(date.Year)", false)] - public static bool IsLeapYear(this DateTime date) - { - return DateTime.IsLeapYear(date.Year); - } /// /// 获取日期所在季度的数字(1-4) diff --git a/EasyTool.Core/DateTimeCategory/TimerUtil.cs b/EasyTool.Core/DateTimeCategory/TimerUtil.cs index e87f551..afd9082 100644 --- a/EasyTool.Core/DateTimeCategory/TimerUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimerUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; namespace EasyTool @@ -40,19 +40,6 @@ public static TimeSpan GetElapsedTime() return DateTime.Now - _startTime; } - /// - /// 创建一个新的 Stopwatch 并启动计时。 - /// [Obsolete("请直接使用 Stopwatch.StartNew()")] - /// - /// 一个新的 Stopwatch。 - [Obsolete("请直接使用 Stopwatch.StartNew()", false)] - public static Stopwatch StartNew() - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - return stopwatch; - } - /// /// 计算指定操作的执行时间。 /// @@ -100,17 +87,6 @@ public static void MeasureAndLog(Action action, string fileName) System.IO.File.AppendAllText(fileName, $"{DateTime.Now}: {elapsedTime.TotalMilliseconds}ms{Environment.NewLine}"); } - /// - /// 等待指定的时间 - /// [Obsolete("请直接使用 Thread.Sleep(milliseconds)")] - /// - /// 要等待的毫秒数。 - [Obsolete("请直接使用 Thread.Sleep(milliseconds)", false)] - public static void Wait(int milliseconds) - { - System.Threading.Thread.Sleep(milliseconds); - } - /// /// 计算两个时间的时间间隔。 /// diff --git a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs b/EasyTool.Core/DateTimeCategory/TimestampUtil.cs deleted file mode 100644 index 6c7912d..0000000 --- a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; - -namespace EasyTool -{ - /// - /// 时间戳处理工具类 - /// - public static class TimestampUtil - { - /// - /// 获取当前时间戳(毫秒级) - /// [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()")] - /// - /// 当前时间戳(毫秒级) - [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()", false)] - public static long GetCurrentTimestamp() - { - DateTime dt = DateTime.UtcNow; - TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return (long)ts.TotalMilliseconds; - } - - /// - /// 将时间戳(毫秒级)转换为 DateTime 类型 - /// [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime")] - /// - /// 时间戳(毫秒级) - /// 转换后的 DateTime 类型 - [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime", false)] - public static DateTime ConvertToDateTime(long timestamp) - { - DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return dt.AddMilliseconds(timestamp); - } - - /// - /// 将 DateTime 类型转换为时间戳(毫秒级) - /// [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeMilliseconds()")] - /// - /// DateTime 类型 - /// 转换后的时间戳(毫秒级) - [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeMilliseconds()", false)] - public static long ConvertToTimestamp(DateTime dateTime) - { - TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return (long)ts.TotalMilliseconds; - } - - /// - /// 获取当前时间戳(秒级) - /// [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeSeconds()")] - /// - /// 当前时间戳(秒级) - [Obsolete("请直接使用 DateTimeOffset.UtcNow.ToUnixTimeSeconds()", false)] - public static long GetCurrentTimestampSeconds() - { - return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; - } - - /// - /// 将时间戳(秒级)转换为 DateTime 类型 - /// [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime")] - /// - /// 时间戳(秒级) - /// 转换后的 DateTime 类型 - [Obsolete("请直接使用 DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime", false)] - public static DateTime ConvertToDateTimeSeconds(long timestamp) - { - return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp); - } - - /// - /// 将 DateTime 类型转换为时间戳(秒级) - /// [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeSeconds()")] - /// - /// DateTime 类型 - /// 转换后的时间戳(秒级) - [Obsolete("请直接使用 new DateTimeOffset(dateTime).ToUnixTimeSeconds()", false)] - public static long ConvertToTimestampSeconds(DateTime dateTime) - { - return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; - } - } -} diff --git a/EasyTool.Core/IOCategory/FileSystemExtension.cs b/EasyTool.Core/IOCategory/FileSystemExtension.cs index 2cd7865..5a5c5ca 100644 --- a/EasyTool.Core/IOCategory/FileSystemExtension.cs +++ b/EasyTool.Core/IOCategory/FileSystemExtension.cs @@ -170,65 +170,6 @@ public static FileInfo CopyToDirectory(this FileInfo file, string targetDirector return new FileInfo(targetPath); } - /// - /// 读取文件的所有文本内容 - /// [Obsolete("请直接使用 File.ReadAllText(file.FullName)")] - /// - [Obsolete("请直接使用 File.ReadAllText(file.FullName)", false)] - public static string ReadAllText(this FileInfo file) - { - if (file == null || !file.Exists) - return string.Empty; - - return File.ReadAllText(file.FullName); - } - - /// - /// 读取文件的所有字节 - /// [Obsolete("请直接使用 File.ReadAllBytes(file.FullName)")] - /// - [Obsolete("请直接使用 File.ReadAllBytes(file.FullName)", false)] - public static byte[] ReadAllBytes(this FileInfo file) - { - if (file == null || !file.Exists) - return Array.Empty(); - - return File.ReadAllBytes(file.FullName); - } - - /// - /// 写入文本内容到文件 - /// [Obsolete("请直接使用 File.WriteAllText(file.FullName, content)")] - /// - [Obsolete("请直接使用 File.WriteAllText(file.FullName, content)", false)] - public static void WriteAllText(this FileInfo file, string content) - { - if (file == null) - throw new ArgumentNullException(nameof(file)); - - // 确保目录存在 - if (!file.Directory.Exists) - file.Directory.Create(); - - File.WriteAllText(file.FullName, content); - } - - /// - /// 写入字节到文件 - /// [Obsolete("请直接使用 File.WriteAllBytes(file.FullName, content)")] - /// - [Obsolete("请直接使用 File.WriteAllBytes(file.FullName, content)", false)] - public static void WriteAllBytes(this FileInfo file, byte[] content) - { - if (file == null) - throw new ArgumentNullException(nameof(file)); - - // 确保目录存在 - if (!file.Directory.Exists) - file.Directory.Create(); - - File.WriteAllBytes(file.FullName, content); - } #endregion diff --git a/EasyTool.Core/IOCategory/FileTypeExtension.cs b/EasyTool.Core/IOCategory/FileTypeExtension.cs index 3216dda..f8a5d0b 100644 --- a/EasyTool.Core/IOCategory/FileTypeExtension.cs +++ b/EasyTool.Core/IOCategory/FileTypeExtension.cs @@ -5,11 +5,14 @@ namespace EasyTool.Extension { + /// + /// 文件类型扩展方法 + /// public static class FileTypeExtension { /// /// 文件流头部信息获得文件类型 - /// + /// /// 说明: /// 1、无法识别类型默认按照扩展名识别 /// 2、xls、doc、msi、ppt、vsd头信息无法区分,按照扩展名区分 @@ -17,6 +20,63 @@ public static class FileTypeExtension /// /// 文件 /// 类型,文件的扩展名,未找到为null - public static string GetType(this FileInfo file) => FileTypeUtil.GetType(file); + public static string GetFileType(this FileInfo file) + { + byte[] buffer = new byte[256]; + using (FileStream fs = file.OpenRead()) + { + int readLength = fs.Read(buffer, 0, buffer.Length); + if (readLength < buffer.Length) + { + // 处理读取不足的情况,虽然对于头部检测通常前几个字节就够了,但为了严谨性 + } + } + + string header = ""; + for (int i = 0; i < buffer.Length; i++) + { + header += buffer[i].ToString(); + } + + string? type = null; + switch (header) + { + case "255216": // jpg + type = ".jpg"; + break; + case "13780": // png + type = ".png"; + break; + case "7173": // gif + type = ".gif"; + break; + case "6677": // bmp + type = ".bmp"; + break; + case "7790": // exe dll + type = ".exe"; + break; + case "6063": // xml + type = ".xml"; + break; + case "6033": // htm html + type = ".html"; + break; + case "4742": // js + type = ".js"; + break; + case "5144": // txt + type = ".txt"; + break; + default: + case "8297": // rar + case "8075": // zip + case "D0CF11E0": // doc xls ppt vsd + type = file.Extension; + break; + } + + return type; + } } } diff --git a/EasyTool.Core/IOCategory/FileTypeUtil.cs b/EasyTool.Core/IOCategory/FileTypeUtil.cs deleted file mode 100644 index 09b077b..0000000 --- a/EasyTool.Core/IOCategory/FileTypeUtil.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; - -namespace EasyTool -{ - /// - /// 文件类型判断工具类 - /// - public class FileTypeUtil - { - /// - /// 文件流头部信息获得文件类型 - /// - /// 说明: - /// 1、无法识别类型默认按照扩展名识别 - /// 2、xls、doc、msi、ppt、vsd头信息无法区分,按照扩展名区分 - /// 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分 - /// - /// 文件 - /// 类型,文件的扩展名,未找到为null - public static string GetType(FileInfo file) - { - byte[] buffer = new byte[256]; - using (FileStream fs = file.OpenRead()) - { - int readLength = fs.Read(buffer, 0, buffer.Length); - if (readLength < buffer.Length) - { - // 处理读取不足的情况,虽然对于头部检测通常前几个字节就够了,但为了严谨性 - } - } - - string header = ""; - for (int i = 0; i < buffer.Length; i++) - { - header += buffer[i].ToString(); - } - - string? type = null; - switch (header) - { - case "255216": // jpg - type = ".jpg"; - break; - case "13780": // png - type = ".png"; - break; - case "7173": // gif - type = ".gif"; - break; - case "6677": // bmp - type = ".bmp"; - break; - case "7790": // exe dll - type = ".exe"; - break; - case "6063": // xml - type = ".xml"; - break; - case "6033": // htm html - type = ".html"; - break; - case "4742": // js - type = ".js"; - break; - case "5144": // txt - type = ".txt"; - break; - default: - case "8297": // rar - case "8075": // zip - case "D0CF11E0": // doc xls ppt vsd - type = file.Extension; - break; - } - - return type; - } - } -} diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index 857d294..9104d26 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Text; using System.Web; +using EasyTool.Extension; namespace EasyTool { @@ -302,24 +303,6 @@ public static FileInfo Touch(string path) } - /// - /// 拷贝文件 - /// [Obsolete("请直接使用 File.Copy(src, dest)")] - /// - /// 源文件路径 - /// 目标文件路径 - [Obsolete("请直接使用 File.Copy(src, dest)", false)] - public static void Cp(string src, string dest) - { - try - { - File.Copy(src, dest); - } - catch (Exception ex) - { - throw new Exception($"拷贝文件 {src} 到 {dest} 失败:{ex.Message}", ex); - } - } /// /// 复制文件或目录 @@ -398,24 +381,6 @@ public static bool Copy(string src, string dest, bool isOverride) } } - /// - /// 移动文件或重命名文件 - /// [Obsolete("请直接使用 File.Move(src, dest)")] - /// - /// 源文件路径 - /// 目标文件路径 - [Obsolete("请直接使用 File.Move(src, dest)", false)] - public static void Mv(string src, string dest) - { - try - { - File.Move(src, dest); - } - catch (Exception ex) - { - throw new Exception($"移动/重命名文件 {src} 到 {dest} 失败:{ex.Message}", ex); - } - } /// /// 移动文件或者目录 @@ -539,22 +504,6 @@ public static FileInfo Rename(FileInfo file, string newName, bool isRetainExt, b } } - /// - /// 获取绝对路径 - /// [Obsolete("请直接使用 Path.GetFullPath(path)")] - /// - /// 相对路径 - /// 绝对路径 - [Obsolete("请直接使用 Path.GetFullPath(path)", false)] - public static string GetAbsolutePath(string path) - { - if (!Path.IsPathRooted(path)) - { - path = Path.Combine(Directory.GetCurrentDirectory(), path); - } - - return Path.GetFullPath(path); - } /// /// 判断给定路径是否是绝对路径 @@ -770,76 +719,7 @@ public static string SubPath(string dirPath, string filePath) return filePath.Substring(startIndex); } - /// - /// 删除文件 - /// [Obsolete("请直接使用 File.Delete(path)")] - /// - /// 文件路径 - [Obsolete("请直接使用 File.Delete(path)", false)] - public static void Rm(string path) - { - try - { - File.Delete(path); - } - catch (Exception ex) - { - throw new Exception($"删除文件 {path} 失败:{ex.Message}"); - } - } - /// - /// 创建目录 - /// [Obsolete("请直接使用 Directory.CreateDirectory(path)")] - /// - /// 目录路径 - [Obsolete("请直接使用 Directory.CreateDirectory(path)", false)] - public static void Mkdir(string path) - { - try - { - Directory.CreateDirectory(path); - } - catch (Exception ex) - { - throw new Exception($"创建目录 {path} 失败:{ex.Message}", ex); - } - } - - /// - /// 删除目录 - /// [Obsolete("请直接使用 Directory.Delete(path)")] - /// - /// 目录路径 - [Obsolete("请直接使用 Directory.Delete(path)", false)] - public static void Rmdir(string path) - { - try - { - Directory.Delete(path); - Console.WriteLine($"目录 {path} 已成功删除"); - } - catch (Exception ex) - { - throw new Exception($"删除目录 {path} 失败:{ex.Message}",ex); - } - } - - /// - /// 获取文件名 - /// [Obsolete("请直接使用 file?.Name")] - /// - /// 文件 - /// 文件名 - [Obsolete("请直接使用 file?.Name", false)] - public static string GetFileName(FileInfo file) - { - if (file == null) - { - return null; - } - return file.Name; - } /// /// 获取文件名 @@ -938,7 +818,8 @@ public static string GetFilePrefix(string filePath) /// 类型,文件的扩展名,未找到为null public static string? GetType(FileInfo file) { - return FileTypeUtil.GetType(file); + // 通过文件头部获取类型 + return file.GetFileType(); } /// @@ -1158,27 +1039,6 @@ public static string ReadString(Uri url, Encoding? encoding = null) return result; } - /// - /// 从文件中读取每一行数据 - /// [Obsolete("请直接使用 File.ReadAllLines(path, encoding)")] - /// - /// 文件路径 - /// 编码格式,默认为UTF-8 - /// - [Obsolete("请直接使用 File.ReadAllLines(path, encoding)", false)] - public static string[] ReadAllLines(string path, Encoding? encoding = null) - { - // 如果未指定编码格式,则默认为 UTF-8 - if (encoding == null) - { - encoding = Encoding.UTF8; - } - - // 读取文件所有行数据 - string[] lines = File.ReadAllLines(path, encoding); - - return lines; - } /// @@ -1208,16 +1068,6 @@ public static Stream GetOutputStream(string path) } - /// - /// 获取当前系统的换行分隔符 - /// [Obsolete("请直接使用 Environment.NewLine")] - /// - /// 换行分隔符 - [Obsolete("请直接使用 Environment.NewLine", false)] - public static string GetLineSeparator() - { - return Environment.NewLine; - } /// /// 将string写入文件,覆盖模式 diff --git a/EasyTool.Core/IOCategory/IoUtil.cs b/EasyTool.Core/IOCategory/IoUtil.cs deleted file mode 100644 index d2a647f..0000000 --- a/EasyTool.Core/IOCategory/IoUtil.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Text; - -namespace EasyTool -{ - /// - /// Io流处理工具类 - /// - public static class IoUtil - { - /// - /// 读取文件的所有行到一个字符串数组中 - /// [Obsolete("请直接使用 File.ReadAllLines(path)")] - /// - /// 文件路径 - /// 字符串数组,其中包含文件的所有行。 - [Obsolete("请直接使用 File.ReadAllLines(path)", false)] - public static string[] ReadAllLines(string path) - { - return File.ReadAllLines(path); - } - - /// - /// 将字符串数组写入文件,覆盖原有内容 - /// [Obsolete("请直接使用 File.WriteAllLines(path, lines)")] - /// - /// 文件路径 - /// 待写入的字符串数组 - [Obsolete("请直接使用 File.WriteAllLines(path, lines)", false)] - public static void WriteAllLines(string path, string[] lines) - { - File.WriteAllLines(path, lines); - } - - /// - /// 读取整个文件到一个字符串中 - /// [Obsolete("请直接使用 File.ReadAllText(path)")] - /// - /// 文件路径 - /// 文件的所有内容 - [Obsolete("请直接使用 File.ReadAllText(path)", false)] - public static string ReadAllText(string path) - { - return File.ReadAllText(path); - } - - /// - /// 将字符串写入文件,覆盖原有内容 - /// [Obsolete("请直接使用 File.WriteAllText(path, text)")] - /// - /// 文件路径 - /// 待写入的字符串 - [Obsolete("请直接使用 File.WriteAllText(path, text)", false)] - public static void WriteAllText(string path, string text) - { - File.WriteAllText(path, text); - } - - /// - /// 读取二进制数据到一个字节数组中 - /// [Obsolete("请直接使用 File.ReadAllBytes(path)")] - /// - /// 文件路径 - /// - [Obsolete("请直接使用 File.ReadAllBytes(path)", false)] - public static byte[] ReadAllBytes(string path) - { - return File.ReadAllBytes(path); - } - - /// - /// 将字节数组写入二进制文件,覆盖原有内容 - /// [Obsolete("请直接使用 File.WriteAllBytes(path, bytes)")] - /// - /// 文件路径 - /// 待写入的字节数组 - [Obsolete("请直接使用 File.WriteAllBytes(path, bytes)", false)] - public static void WriteAllBytes(string path, byte[] bytes) - { - File.WriteAllBytes(path, bytes); - } - - /// - /// 读取指定 URL 的文本内容 - /// - /// URL 地址 - /// URL 返回的文本内容 - public static string ReadUrl(string url) - { - WebClient client = new WebClient(); - return client.DownloadString(url); - } - - /// - /// 将字符串写入指定 URL - /// - /// URL 地址 - /// 待写入的字符串 - public static void WriteUrl(string url, string text) - { - WebClient client = new WebClient(); - client.UploadString(url, text); - } - - /// - /// 读取网络流到一个字符串中 - /// - /// 网络流 - /// 网络流的所有内容 - public static string ReadStream(Stream stream) - { - using (StreamReader reader = new StreamReader(stream)) - { - return reader.ReadToEnd(); - } - } - - /// - /// 将字符串写入网络流 - /// - /// 网络流 - /// 待写入的字符串 - public static void WriteStream(Stream stream, string text) - { - using (StreamWriter writer = new StreamWriter(stream)) - { - writer.Write(text); - } - } - - /// - /// 读取二进制数据到一个内存流中 - /// - /// 二进制数据 - /// 内存流,其中包含输入的二进制数据 - public static MemoryStream ReadMemoryStream(byte[] bytes) - { - return new MemoryStream(bytes); - } - - /// - /// 将二进制数据写入一个内存流中 - /// - /// 内存流 - /// 待写入的字节数组 - public static void WriteMemoryStream(MemoryStream stream, byte[] bytes) - { - stream.Write(bytes, 0, bytes.Length); - } - - /// - /// 将一个字符串转换为字节数组 - /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(text)")] - /// - /// 待转换的字符串 - /// 字节数组,其中包含输入字符串的编码数据 - [Obsolete("请直接使用 Encoding.UTF8.GetBytes(text)", false)] - public static byte[] StringToBytes(string text) - { - return Encoding.UTF8.GetBytes(text); - } - - /// - /// 将一个字节数组转换为字符串 - /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] - /// - /// 待转换的字节数组 - /// 字符串,其中包含输入字节数组的编码数据 - [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] - public static string BytesToString(byte[] bytes) - { - return Encoding.UTF8.GetString(bytes); - } - } -} diff --git a/EasyTool.Core/IOCategory/StreamExtension.cs b/EasyTool.Core/IOCategory/StreamExtension.cs index a8c729b..d3c93bd 100644 --- a/EasyTool.Core/IOCategory/StreamExtension.cs +++ b/EasyTool.Core/IOCategory/StreamExtension.cs @@ -189,45 +189,6 @@ public static async Task WriteTextAsync(this Stream stream, string text, Encodin #region 复制操作 - /// - /// 将流复制到另一个流 - /// [Obsolete("请直接使用 source.CopyTo(destination)")] - /// - [Obsolete("请直接使用 source.CopyTo(destination)", false)] - public static void CopyTo(this Stream source, Stream destination) - { - if (source == null) - throw new ArgumentNullException(nameof(source)); - if (destination == null) - throw new ArgumentNullException(nameof(destination)); - - source.CopyTo(destination, 81920); - } - - /// - /// 将流复制到另一个流(指定缓冲区大小) - /// [Obsolete("请直接使用 source.CopyTo(destination)")] - /// - [Obsolete("请直接使用 source.CopyTo(destination)", false)] - public static void CopyTo(this Stream source, Stream destination, int bufferSize) - { - if (source == null) - throw new ArgumentNullException(nameof(source)); - if (destination == null) - throw new ArgumentNullException(nameof(destination)); - - if (!source.CanRead) - throw new InvalidOperationException("Source stream does not support reading."); - if (!destination.CanWrite) - throw new InvalidOperationException("Destination stream does not support writing."); - - var buffer = new byte[bufferSize]; - int bytesRead; - while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) - { - destination.Write(buffer, 0, bytesRead); - } - } /// /// 将流复制到字节数组 @@ -254,37 +215,6 @@ public static MemoryStream CopyToMemoryStream(this Stream stream) #region 位置操作 - /// - /// 将流位置重置到开头 - /// [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.Begin)")] - /// - [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.Begin)", false)] - public static void ResetPosition(this Stream stream) - { - if (stream == null) - throw new ArgumentNullException(nameof(stream)); - - if (stream.CanSeek) - { - stream.Seek(0, SeekOrigin.Begin); - } - } - - /// - /// 将流位置重置到末尾 - /// [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.End)")] - /// - [Obsolete("请直接使用 stream.Seek(0, SeekOrigin.End)", false)] - public static void SeekToEnd(this Stream stream) - { - if (stream == null) - throw new ArgumentNullException(nameof(stream)); - - if (stream.CanSeek) - { - stream.Seek(0, SeekOrigin.End); - } - } #endregion diff --git a/EasyTool.Core/LanguageCategory/BCDUtil.cs b/EasyTool.Core/LanguageCategory/BCDUtil.cs deleted file mode 100644 index 8e07e35..0000000 --- a/EasyTool.Core/LanguageCategory/BCDUtil.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// BCD工具 - /// - public class BCDUtil - { - /// - /// 将一个十进制数转换成对应的二进制码数组 - /// - /// 需要转换的十进制数 - /// 二进制码数组 - public static int[] DecToBinaryArray(int dec) - { - if (dec < 0) - { - throw new ArgumentException("dec必须是非负整数。"); - } - - if (dec == 0) - { - return new int[] { 0 }; - } - - int[] binaryArray = new int[32]; - int index = 0; - - while (dec > 0) - { - binaryArray[index++] = dec % 2; - dec /= 2; - } - - Array.Resize(ref binaryArray, index); - Array.Reverse(binaryArray); - - return binaryArray; - } - - /// - /// 将一个二进制码数组转换成对应的十进制数 - /// - /// 需要转换的二进制码数组 - /// 对应的十进制数 - public static int BinaryArrayToDec(int[] binaryArray) - { - if (binaryArray == null) - { - throw new ArgumentNullException("binaryArray不能为null。"); - } - - int dec = 0; - int power = 1; - - for (int i = binaryArray.Length - 1; i >= 0; i--) - { - dec += binaryArray[i] * power; - power *= 2; - } - - return dec; - } - - /// - /// 将一个十进制数转换成对应的BCD码数组 - /// - /// 需要转换的十进制数 - /// BCD码数组 - public static int[] DecToBCDArray(int dec) - { - if (dec < 0) - { - throw new ArgumentException("dec必须是非负整数。"); - } - - if (dec == 0) - { - return new int[] { 0 }; - } - - int[] bcdArray = new int[10]; - int index = 0; - - while (dec > 0) - { - int remainder = dec % 10; - int[] binaryArray = DecToBinaryArray(remainder); - int paddingCount = 4 - binaryArray.Length; - - for (int i = 0; i < paddingCount; i++) - { - bcdArray[index++] = 0; - } - - for (int i = 0; i < binaryArray.Length; i++) - { - bcdArray[index++] = binaryArray[i]; - } - - dec /= 10; - } - - Array.Resize(ref bcdArray, index); - Array.Reverse(bcdArray); - - return bcdArray; - } - - /// - /// 将一个BCD码数组转换成对应的十进制数 - /// - /// 需要转换的BCD码数组 - /// 对应的十进制数 - public static int BCDArrayToDec(int[] bcdArray) - { - if (bcdArray == null) - { - throw new ArgumentNullException("bcdArray不能为null。"); - } - - int dec = 0; - int power = 1; - - for (int i = bcdArray.Length - 1; i >= 0; i -= 4) - { - int binary = 0; - - for (int j = 0; j < 4; j++) - { - int index = i - j; - - if (index < 0) - { - break; - } - - binary += bcdArray[index] * (int)Math.Pow(2, 3 - j); - } - - dec += binary * power; - power *= 10; - } - - return dec; - } - - /// - /// 将给定的十进制数转换为 BCD 码字符串。 - /// - /// 要转换的十进制数 - /// 转换后的 BCD 码字符串 - public static string Encode(int dec) - { - if (dec == 0) - { - return "0"; - } - - string str = dec.ToString(); - int len = str.Length; - char[] bcdChars = new char[len * 2]; - for (int i = 0; i < len; i++) - { - int bcd = ((int)Char.GetNumericValue(str[i])) & 0x0F; - bcdChars[i * 2] = (char)(bcd + ((bcd > 9) ? 0x37 : 0x30)); - - bcd = (((int)Char.GetNumericValue(str[i])) >> 4) & 0x0F; - bcdChars[i * 2 + 1] = (char)(bcd + ((bcd > 9) ? 0x37 : 0x30)); - } - return new string(bcdChars); - } - - /// - /// 将给定的 BCD 码字符串转换为十进制数。 - /// - /// 要转换的 BCD 码字符串 - /// 转换后的十进制数 - public static int Decode(string bcd) - { - if (string.IsNullOrEmpty(bcd)) - { - return 0; - } - - int len = bcd.Length; - int dec = 0; - for (int i = 0; i < len; i += 2) - { - int a = ((int)bcd[i]) & 0x0F; - int b = ((int)bcd[i + 1]) & 0x0F; - dec = dec * 100 + a + b * 10; - } - return dec; - } - } -} diff --git a/EasyTool.Core/LanguageCategory/SingletonUtil.cs b/EasyTool.Core/LanguageCategory/SingletonUtil.cs deleted file mode 100644 index f49d677..0000000 --- a/EasyTool.Core/LanguageCategory/SingletonUtil.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// 单例工具类 - /// - public class SingletonUtil where T : class, new() - { - private static readonly Lazy lazyInstance = new Lazy(() => new T()); - - /// - /// 返回单例对象的唯一实例(懒汉模式) - /// - public static T LazyInstance => lazyInstance.Value; - - - private static T instance; - private static readonly object lockObject = new object(); - - /// - /// 返回单例对象的唯一实例(饿汉模式) - /// - [Obsolete] - public static T Instance - { - get - { - if (instance == null) - { - lock (lockObject) - { - if (instance == null) - { - instance = new T(); - } - } - } - - return instance; - } - } - } -} diff --git a/EasyTool.Core/LanguageCategory/TreeUtil.cs b/EasyTool.Core/LanguageCategory/TreeUtil.cs deleted file mode 100644 index 7003b9b..0000000 --- a/EasyTool.Core/LanguageCategory/TreeUtil.cs +++ /dev/null @@ -1,302 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// 树工具类,用于构建树结构 - /// - public class TreeUtil - { - private List> _nodes; - - /// - /// 构造函数 - /// - /// 树节点列表 - public TreeUtil(List> nodes) - { - _nodes = nodes; - } - - /// - /// 构建树结构 - /// - /// 根节点 - public TreeNode? BuildTree() - { - // 获取根节点 - var root = _nodes.FirstOrDefault(n => n.ParentId == null || n.ParentId.Equals(default(T))); - - if (root == null) - { - return null; - } - - // 构建树结构 - BuildTree(root); - - return root; - } - - private void BuildTree(TreeNode node) - { - node.Children = _nodes.Where(n => n.ParentId != null && n.ParentId.Equals(node.Id)).ToList(); - - if (node.Children.Count > 0) - { - foreach (var child in node.Children) - { - BuildTree(child); - } - } - } - - /// - /// 获取某个节点的所有父节点 - /// - /// 节点 - /// 父节点列表,从根节点到该节点的顺序 - public List> GetParents(TreeNode node) - { - var parents = new List>(); - var parent = GetParent(node.Id); - while (parent != null) - { - parents.Insert(0, parent); - parent = GetParent(parent.Id); - } - return parents; - } - - /// - /// 获取某个节点的深度 - /// - /// 节点 - /// 节点深度,根节点的深度为0 - public int GetDepth(TreeNode node) - { - return GetParents(node).Count; - } - - /// - /// 获取某个节点的所有子孙节点 - /// - /// 节点 - /// 子孙节点列表 - public List> GetDescendants(TreeNode node) - { - var descendants = new List>(); - GetDescendants(node, descendants); - return descendants; - } - - private void GetDescendants(TreeNode node, List> descendants) - { - descendants.Add(node); - if (node.Children.Count > 0) - { - foreach (var child in node.Children) - { - GetDescendants(child, descendants); - } - } - } - - private TreeNode? GetParent(T id) - { - return _nodes.FirstOrDefault(n => n.Id != null && n.Id.Equals(id)); - } - - /// - /// 获取某个节点的所有兄弟节点 - /// - /// 节点 - /// 兄弟节点列表 - public List> GetSiblings(TreeNode node) - { - var parent = GetParent(node.Id); - if (parent == null) - { - return new List>(); - } - return parent.Children.Where(n => n.Id == null || !n.Id.Equals(node.Id)).ToList(); - } - - /// - /// 获取某个节点的所有兄弟节点数量 - /// - /// 节点 - /// 兄弟节点数量 - public int GetSiblingCount(TreeNode node) - { - return GetSiblings(node).Count; - } - - /// - /// 判断某个节点是否是叶子节点 - /// - /// 节点 - /// 是否是叶子节点 - public bool IsLeaf(TreeNode node) - { - return node.Children.Count == 0; - } - - /// - /// 获取树的最大深度 - /// - /// 树的最大深度 - public int GetMaxDepth() - { - return _nodes.Max(n => GetDepth(n)); - } - - /// - /// 获取树的最小深度 - /// - /// 树的最小深度 - public int GetMinDepth() - { - return _nodes.Min(n => GetDepth(n)); - } - - /// - /// 获取某个节点的下一个兄弟节点 - /// - /// 节点 - /// 下一个兄弟节点 - public TreeNode? GetNextSibling(TreeNode node) - { - var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id != null && n.Id.Equals(node.Id)); - return index + 1 < siblings.Count ? siblings[index + 1] : null; - } - - /// - /// 获取某个节点的上一个兄弟节点 - /// - /// 节点 - /// 上一个兄弟节点 - public TreeNode? GetPreviousSibling(TreeNode node) - { - var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id != null && n.Id.Equals(node.Id)); - return index - 1 >= 0 ? siblings[index - 1] : null; - } - - /// - /// 获取某个节点的首个子节点 - /// - /// 节点 - /// 首个子节点 - public TreeNode? GetFirstChild(TreeNode node) - { - return node.Children.Count > 0 ? node.Children[0] : null; - } - - /// - /// 获取某个节点的最后一个子节点 - /// - /// 节点 - /// 最后一个子节点 - public TreeNode? GetLastChild(TreeNode node) - { - return node.Children.Count > 0 ? node.Children[node.Children.Count - 1] : null; - } - - /// - /// 获取树的所有节点数量 - /// - /// 树的所有节点数量 - public int GetNodeCount() - { - return _nodes.Count; - } - - /// - /// 获取树的所有叶子节点数量 - /// - /// 树的所有叶子节点数量 - public int GetLeafCount() - { - return _nodes.Count(IsLeaf); - } - - /// - /// 获取树的所有节点的权重和 - /// - /// 树的所有节点的权重和 - public int GetTotalWeight() - { - return _nodes.Sum(n => n.Weight); - } - - /// - /// 获取树的所有叶子节点的权重和 - /// - /// 树的所有叶子节点的权重和 - public int GetLeafWeightTotal() - { - return _nodes.Where(IsLeaf).Sum(n => n.Weight); - } - - /// - /// 获取树的平均深度 - /// - /// 树的平均深度 - public int GetAverageDepth() - { - return (int)_nodes.Average(n => GetDepth(n)); - } - - /// - /// 获取树的平均节点权重 - /// - /// 树的平均节点权重 - public int GetAverageWeight() - { - return (int)_nodes.Average(n => n.Weight); - } - - /// - /// 获取树的最大节点权重 - /// - /// 树的最大节点权重 - public int GetMaxWeight() - { - return _nodes.Max(n => n.Weight); - } - - /// - /// 获取树的最小节点权重 - /// - /// 树的最小节点权重 - public int GetMinWeight() - { - return _nodes.Min(n => n.Weight); - } - } - - public class TreeNode - { - public T Id { get; set; } - public T? ParentId { get; set; } - public string Name { get; set; } = string.Empty; - public int Weight { get; set; } - public D Data { get; set; } = default!; - public List> Children { get; set; } = new List>(); - - public TreeNode(T id, T? parentId, string name, int weight, D data) - { - this.Id = id; - this.ParentId = parentId; - this.Name = name; - this.Weight = weight; - this.Data = data; - this.Children = new List>(); - } - } -} diff --git a/EasyTool.Core/MathCategory/MathUtil.cs b/EasyTool.Core/MathCategory/MathUtil.cs index 76010ec..ff4f064 100644 --- a/EasyTool.Core/MathCategory/MathUtil.cs +++ b/EasyTool.Core/MathCategory/MathUtil.cs @@ -1,39 +1,14 @@ -using System; -using System.Collections.Generic; +using System; using System.Text; namespace EasyTool { + /// + /// 数学工具类,提供数字计算和数学运算方法 + /// public static class MathUtil { - /// - /// 计算两个整数的最大公约数 - /// - /// 第一个整数 - /// 第二个整数 - /// 最大公约数 - public static int Gcd(int a, int b) - { - if (b == 0) - { - return a; - } - else - { - return Gcd(b, a % b); - } - } - - /// - /// 计算两个整数的最小公倍数 - /// - /// 第一个整数 - /// 第二个整数 - /// 最小公倍数 - public static int Lcm(int a, int b) - { - return a * b / Gcd(a, b); - } + #region 质数与阶乘 /// /// 判断一个整数是否为质数 @@ -58,18 +33,6 @@ public static bool IsPrime(int n) return true; } - /// - /// 计算两个浮点数的差的绝对值是否小于指定的精度 - /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 指定的精度 - /// 如果两个浮点数的差的绝对值小于指定的精度,则返回 true;否则返回 false - public static bool ApproxEqual(double a, double b, double eps) - { - return Math.Abs(a - b) < eps; - } - /// /// 求一个整数的阶乘 /// @@ -77,62 +40,67 @@ public static bool ApproxEqual(double a, double b, double eps) /// 阶乘结果 public static int Factorial(int n) { - if (n <= 1) + if (n < 0) { - return 1; + throw new ArgumentException("阶乘只能求非负整数"); } - else + + int result = 1; + for (int i = 1; i <= n; i++) { - return n * Factorial(n - 1); + result *= i; } + + return result; } + #endregion + + #region 最大公约数与最小公倍数 + /// - /// 求一个整数的斐波那契数列的值 + /// 计算两个整数的最大公约数 /// - /// 要求斐波那契数列的整数 - /// 斐波那契数列的值 - public static int Fibonacci(int n) + /// 第一个整数 + /// 第二个整数 + /// 最大公约数 + public static int Gcd(int a, int b) { - if (n <= 1) + if (b == 0) { - return n; + return a; } else { - return Fibonacci(n - 1) + Fibonacci(n - 2); + return Gcd(b, a % b); } } /// - /// 求一个整数的二进制表示中 1 的个数 + /// 计算两个整数的最小公倍数 /// - /// 要求二进制表示中 1 的个数的整数 - /// 二进制表示中 1 的个数 - public static int CountBits(int n) + /// 第一个整数 + /// 第二个整数 + /// 最小公倍数 + public static int Lcm(int a, int b) { - int count = 0; + return a * b / Gcd(a, b); + } - while (n != 0) - { - count++; - n &= n - 1; - } + #endregion - return count; - } + #region 数值计算 /// - /// 求两个浮点数的平均值 - /// [Obsolete("请直接使用 (a + b) / 2")] + /// 计算两个浮点数的差的绝对值是否小于指定的精度 /// /// 第一个浮点数 /// 第二个浮点数 - /// 两个浮点数的平均值 - [Obsolete("请直接使用 (a + b) / 2", false)] - public static double Average(double a, double b) + /// 指定的精度 + /// 如果两个浮点数的差的绝对值小于指定的精度,则返回 true;否则返回 false + public static bool ApproxEqual(double a, double b, double eps) { - return (a + b) / 2; + return Math.Abs(a - b) < eps; } /// @@ -170,6 +138,177 @@ public static int Pow(int n, int k) } } + /// + /// 求一个整数的绝对值 + /// + /// 待求绝对值的数字 + /// 该数字的绝对值 + public static int Abs(int number) + { + return number < 0 ? -number : number; + } + + /// + /// 求一个整数的平方 + /// + /// 待求平方的数字 + /// 该数字的平方 + public static int Square(int number) + { + return number * number; + } + + /// + /// 求一个整数的立方 + /// + /// 待求立方的数字 + /// 该数字的立方 + public static int Cube(int number) + { + return number * number * number; + } + + /// + /// 计算两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue + /// + /// 第一个整数 + /// 第二个整数 + /// 两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue + public static int SafeAdd(int a, int b) + { + int sum = a + b; + + if (a > 0 && b > 0 && sum < 0) + { + return int.MaxValue; + } + else if (a < 0 && b < 0 && sum > 0) + { + return int.MinValue; + } + else + { + return sum; + } + } + + #endregion + + #region 进制转换 + + /// + /// 把一个数字转换为二进制字符串 + /// + /// 待转换的数字 + /// 该数字的二进制字符串 + public static string ToBinaryString(int number) + { + if (number == 0) + { + return "0"; + } + + string result = string.Empty; + while (number > 0) + { + result = (number % 2).ToString() + result; + number /= 2; + } + + return result; + } + + /// + /// 把一个数字转换为八进制字符串 + /// + /// 待转换的数字 + /// 该数字的八进制字符串 + public static string ToOctalString(int number) + { + if (number == 0) + { + return "0"; + } + + string result = string.Empty; + while (number > 0) + { + result = (number % 8).ToString() + result; + number /= 8; + } + + return result; + } + + /// + /// 把一个数字转换为十六进制字符串 + /// + /// 待转换的数字 + /// 该数字的十六进制字符串 + public static string ToHexString(int number) + { + if (number == 0) + { + return "0"; + } + + string result = string.Empty; + while (number > 0) + { + int remainder = number % 16; + if (remainder < 10) + { + result = remainder.ToString() + result; + } + else + { + result = (char)('A' + remainder - 10) + result; + } + number /= 16; + } + + return result; + } + + #endregion + + #region 高级数学函数 + + /// + /// 求一个整数的斐波那契数列的值 + /// + /// 要求斐波那契数列的整数 + /// 斐波那契数列的值 + public static int Fibonacci(int n) + { + if (n <= 1) + { + return n; + } + else + { + return Fibonacci(n - 1) + Fibonacci(n - 2); + } + } + + /// + /// 求一个整数的二进制表示中 1 的个数 + /// + /// 要求二进制表示中 1 的个数的整数 + /// 二进制表示中 1 的个数 + public static int CountBits(int n) + { + int count = 0; + + while (n != 0) + { + count++; + n &= n - 1; + } + + return count; + } + /// /// 判断一个整数是否为完全平方数 /// @@ -272,28 +411,6 @@ public static int[] GetAllFactors(int n) return factors; } - /// - /// 计算两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue - /// - /// 第一个整数 - /// 第二个整数 - /// 两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue - public static int Add(int a, int b) - { - int sum = a + b; - - if (a > 0 && b > 0 && sum < 0) - { - return int.MaxValue; - } - else if (a < 0 && b < 0 && sum > 0) - { - return int.MinValue; - } - else - { - return sum; - } - } + #endregion } } diff --git a/EasyTool.Core/MathCategory/NumberUtil.cs b/EasyTool.Core/MathCategory/NumberUtil.cs deleted file mode 100644 index ad84f16..0000000 --- a/EasyTool.Core/MathCategory/NumberUtil.cs +++ /dev/null @@ -1,352 +0,0 @@ -using System; - -namespace EasyTool -{ - /// - /// 数字工具类,提供了多种对数字的操作方法 - /// - public class NumberUtil - { - /// - /// 针对数字类型做加法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的和 - public static decimal Add(float a, float b) - { - return (decimal)a + (decimal)b; - } - - /// - /// 针对数字类型做加法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的和 - public static decimal Add(double a, double b) - { - return (decimal)a + (decimal)b; - } - - /// - /// 针对数字类型做减法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的差 - public static decimal Sub(float a, float b) - { - return (decimal)a - (decimal)b; - } - - /// - /// 针对数字类型做减法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的差 - public static decimal Sub(double a, double b) - { - return (decimal)a - (decimal)b; - } - - /// - /// 针对数字类型做乘法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的积 - public static decimal Mul(float a, float b) - { - return (decimal)a * (decimal)b; - } - - /// - /// 针对数字类型做乘法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的积 - public static decimal Mul(double a, double b) - { - return (decimal)a * (decimal)b; - } - - /// - /// 针对数字类型做除法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的商 - public static decimal Div(float a, float b) - { - return (decimal)a / (decimal)b; - } - - /// - /// 针对数字类型做除法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的商 - public static decimal Div(double a, double b) - { - return (decimal)a / (decimal)b; - } - - /// - /// 针对数字类型做除法,并限制返回的小数位数 - /// - /// 第一个数字 - /// 第二个数字 - /// 限制返回的小数位数 - /// 两个数字的商 - public static decimal Div(float a, float b, int decimalPlaces) - { - decimal result = Div(a, b); - return decimal.Round(result, decimalPlaces); - } - - /// - /// 针对数字类型做除法,并限制返回的小数位数 - /// - /// 第一个数字 - /// 第二个数字 - /// 限制返回的小数位数 - /// 两个数字的商 - public static decimal Div(double a, double b, int decimalPlaces) - { - decimal result = Div(a, b); - return decimal.Round(result, decimalPlaces); - } - - /// - /// 格式化一个 decimal 数字 - /// [Obsolete("请直接使用 number.ToString(format)")] - /// - /// 待格式化的数字 - /// 格式化字符串 - /// 格式化后的字符串 - [Obsolete("请直接使用 number.ToString(format)", false)] - public static string DecimalFormat(decimal number, string format) - { - return number.ToString(format); - } - - /// - /// 保留一个 decimal 数字的小数点后指定位数 - /// - /// 待格式化的数字 - /// 小数点后保留的位数 - /// 格式化后的字符串 - public static string DecimalFormat(decimal number, int decimalPlaces) - { - string format = "0."; - for (int i = 0; i < decimalPlaces; i++) - { - format += "0"; - } - return DecimalFormat(number, format); - } - - /// - /// 格式化一个 decimal 数字,并加上千位分隔符 - /// - /// 待格式化的数字 - /// 格式化后的字符串 - public static string DecimalFormatWithCommas(decimal number) - { - return DecimalFormat(number, "0,0.00"); - } - - - - /// - /// 判断一个数字是否是质数 - /// - /// 待判断的数字 - /// 如果是质数,则返回 true;否则返回 false - public static bool IsPrime(int number) - { - if (number <= 1) - { - return false; - } - - for (int i = 2; i <= Math.Sqrt(number); i++) - { - if (number % i == 0) - { - return false; - } - } - - return true; - } - - /// - /// 求一个数字的阶乘 - /// - /// 待求阶乘的数字 - /// 该数字的阶乘 - public static int Factorial(int number) - { - if (number < 0) - { - throw new ArgumentException("阶乘只能求非负整数"); - } - - int result = 1; - for (int i = 1; i <= number; i++) - { - result *= i; - } - - return result; - } - - /// - /// 求两个数字的最大公约数 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的最大公约数 - public static int GCD(int a, int b) - { - if (a < 0 || b < 0) - { - throw new ArgumentException("求最大公约数只能接受非负整数"); - } - - while (b != 0) - { - int temp = b; - b = a % b; - a = temp; - } - - return a; - } - - /// - /// 求两个数字的最小公倍数 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的最小公倍数 - public static int LCM(int a, int b) - { - if (a < 0 || b < 0) - { - throw new ArgumentException("求最小公倍数只能接受非负整数"); - } - - return a * b / GCD(a, b); - } - - /// - /// 把一个数字转换为二进制字符串 - /// - /// 待转换的数字 - /// 该数字的二进制字符串 - public static string ToBinaryString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - result = (number % 2).ToString() + result; - number /= 2; - } - - return result; - } - - /// - /// 把一个数字转换为八进制字符串 - /// - /// 待转换的数字 - /// 该数字的八进制字符串 - public static string ToOctalString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - result = (number % 8).ToString() + result; - number /= 8; - } - - return result; - } - - /// - /// 把一个数字转换为十六进制字符串 - /// - /// 待转换的数字 - /// 该数字的十六进制字符串 - public static string ToHexString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - int remainder = number % 16; - if (remainder < 10) - { - result = remainder.ToString() + result; - } - else - { - result = (char)('A' + remainder - 10) + result; - } - number /= 16; - } - - return result; - } - - /// - /// 求一个数字的绝对值 - /// - /// 待求绝对值的数字 - /// 该数字的绝对值 - public static int Abs(int number) - { - return number < 0 ? -number : number; - } - - /// - /// 求一个数字的平方 - /// - /// 待求平方的数字 - /// 该数字的平方 - public static int Square(int number) - { - return number * number; - } - - /// - /// 求一个数字的立方 - /// - /// 待求立方的数字 - /// 该数字的立方 - public static int Cube(int number) - { - return number * number * number; - } - } -} diff --git a/EasyTool.Core/ToolCategory/RandomUtil.cs b/EasyTool.Core/MathCategory/RandomUtil.cs similarity index 100% rename from EasyTool.Core/ToolCategory/RandomUtil.cs rename to EasyTool.Core/MathCategory/RandomUtil.cs diff --git a/EasyTool.Core/NetCategory/NetUtil.cs b/EasyTool.Core/NetCategory/NetUtil.cs deleted file mode 100644 index 9f5460b..0000000 --- a/EasyTool.Core/NetCategory/NetUtil.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Net.NetworkInformation; -using System.Net.Sockets; -using System.Net; -using System.Text; - -namespace EasyTool -{ - /// - /// 网络工具 - /// - public class NetUtil - { - // Ping a host and return true if the ping was successful - // 对指定主机进行Ping测试,返回是否成功 - public static bool Ping(string host) - { - try - { - Ping pingSender = new Ping(); - PingReply reply = pingSender.Send(host); - - if (reply.Status == IPStatus.Success) - { - return true; - } - else - { - return false; - } - } - catch - { - return false; - } - } - - // Resolve the IP address of a host - // 获取指定主机的IP地址 - public static IPAddress? GetIpAddress(string host) - { - try - { - IPHostEntry hostEntry = Dns.GetHostEntry(host); - - foreach (IPAddress address in hostEntry.AddressList) - { - // 返回IPv4地址 - if (address.AddressFamily == AddressFamily.InterNetwork) - { - return address; - } - } - - return null; - } - catch - { - return null; - } - } - - // Check if a port is open on a given IP address - // 检查给定IP地址上的端口是否开放 - public static bool IsPortOpen(string host, int port) - { - try - { - // 获取IP地址 - IPAddress ipAddress = GetIpAddress(host); - - if (ipAddress == null) - { - return false; - } - - // 创建套接字,连接端口 - IPEndPoint endpoint = new IPEndPoint(ipAddress, port); - using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) - { - socket.Connect(endpoint); - return true; - } - } - catch - { - return false; - } - } - - // Send an HTTP GET request and return the response - // 发送HTTP GET请求并返回响应 - public static string? HttpGet(string url) - { - try - { - using (HttpClient client = new HttpClient()) - { - return client.GetStringAsync(url).GetAwaiter().GetResult(); - } - } - catch - { - return null; - } - } - - // Send an HTTP POST request and return the response - // 发送HTTP POST请求并返回响应 - public static string? HttpPost(string url, string data) - { - try - { - using (HttpClient client = new HttpClient()) - { - StringContent content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"); - HttpResponseMessage response = client.PostAsync(url, content).GetAwaiter().GetResult(); - return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); - } - } - catch - { - return null; - } - } - } -} diff --git a/EasyTool.Core/ToolCategory/URLUtil.cs b/EasyTool.Core/NetCategory/URLUtil.cs similarity index 94% rename from EasyTool.Core/ToolCategory/URLUtil.cs rename to EasyTool.Core/NetCategory/URLUtil.cs index 82edd17..d281985 100644 --- a/EasyTool.Core/ToolCategory/URLUtil.cs +++ b/EasyTool.Core/NetCategory/URLUtil.cs @@ -140,11 +140,9 @@ public static string UrlDecodeQuery(string value) /// /// 从URL中提取域名。 - /// [Obsolete("请直接使用 new Uri(url).Host")] /// /// 要提取域名的URL。 /// URL中的域名。 - [Obsolete("请直接使用 new Uri(url).Host", false)] public static string ExtractDomain(string url) { var uri = new Uri(url); @@ -153,11 +151,9 @@ public static string ExtractDomain(string url) /// /// 从URL中提取路径。 - /// [Obsolete("请直接使用 new Uri(url).AbsolutePath")] /// /// 要提取路径的URL。 /// URL中的路径。 - [Obsolete("请直接使用 new Uri(url).AbsolutePath", false)] public static string ExtractPath(string url) { var uri = new Uri(url); @@ -177,11 +173,9 @@ public static bool IsHttps(string url) /// /// 从URL中提取查询字符串。 - /// [Obsolete("请直接使用 new Uri(url).Query")] /// /// 要提取查询字符串的URL。 /// URL中的查询字符串。 - [Obsolete("请直接使用 new Uri(url).Query", false)] public static string ExtractQueryString(string url) { var uri = new Uri(url); @@ -190,11 +184,9 @@ public static string ExtractQueryString(string url) /// /// 从URL中提取片段。 - /// [Obsolete("请直接使用 new Uri(url).Fragment")] /// /// 要提取片段的URL。 /// URL中的片段。 - [Obsolete("请直接使用 new Uri(url).Fragment", false)] public static string ExtractFragment(string url) { var uri = new Uri(url); diff --git a/EasyTool.Core/TextCategory/CsvUtil.cs b/EasyTool.Core/TextCategory/CsvUtil.cs deleted file mode 100644 index 79cbbc5..0000000 --- a/EasyTool.Core/TextCategory/CsvUtil.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// 提供读取和写入 CSV 文件的常用方法。 - /// - public static class CsvUtil - { - private const char DEFAULT_DELIMITER = ','; - private const char DEFAULT_QUOTE = '"'; - - /// - /// 从指定路径的 CSV 文件中读取数据。 - /// - /// CSV 文件路径。 - /// CSV 文件中的分隔符。 - /// CSV 文件中的引用符。 - /// 读取到的数据。 - public static List> Read(string path, char delimiter = DEFAULT_DELIMITER, char quote = DEFAULT_QUOTE) - { - List> data = new List>(); - using (var reader = new StreamReader(path)) - { - while (!reader.EndOfStream) - { - string line = reader.ReadLine(); - data.Add(ParseLine(line, delimiter, quote)); - } - } - - return data; - } - - /// - /// 将指定的数据写入到 CSV 文件中。 - /// - /// 要写入的数据。 - /// CSV 文件路径。 - /// CSV 文件中的分隔符。 - /// CSV 文件中的引用符。 - public static void Write(List> data, string path, char delimiter = DEFAULT_DELIMITER, char quote = DEFAULT_QUOTE) - { - using (var writer = new StreamWriter(path)) - { - foreach (var record in data) - { - string line = string.Join(delimiter.ToString(), record.Select(s => Escape(s, delimiter, quote))); - writer.WriteLine(line); - } - } - } - - private static List ParseLine(string line, char delimiter, char quote) - { - List fields = new List(); - int i = 0; - while (i < line.Length) - { - if (line[i] == quote) - { - int j = i + 1; - while (j < line.Length) - { - if (line[j] == quote) - { - if (j + 1 < line.Length && line[j + 1] == delimiter) - { - j++; // skip escaped delimiter - } - else - { - break; // end of quoted field - } - } - j++; - } - - if (j >= line.Length || line[j] != quote) - { - throw new ArgumentException("Invalid CSV format: mismatched quotes."); - } - - fields.Add(Unescape(line.Substring(i + 1, j - i - 1), delimiter, quote)); - i = j + 1; - } - else - { - int j = line.IndexOf(delimiter, i); - if (j < 0) - { - fields.Add(line.Substring(i)); - i = line.Length; - } - else - { - fields.Add(line.Substring(i, j - i)); - i = j + 1; - } - } - } - - return fields; - } - - private static string Escape(string value, char delimiter, char quote) - { - if (value.Contains(delimiter) || value.Contains(quote) || value.Contains(Environment.NewLine)) - { - return quote + value.Replace(quote.ToString(), quote.ToString() + quote.ToString()) + quote; - } - else - { - return value; - } - } - - private static string Unescape(string value, char delimiter, char quote) - { - return value.Replace(quote.ToString() + quote.ToString(), quote.ToString()).Replace(quote.ToString() + delimiter, delimiter.ToString()); - } - } -} diff --git a/EasyTool.Core/ToolCategory/RegexUtil.cs b/EasyTool.Core/TextCategory/RegexUtil.cs similarity index 79% rename from EasyTool.Core/ToolCategory/RegexUtil.cs rename to EasyTool.Core/TextCategory/RegexUtil.cs index 5f5a1d5..2ce88d2 100644 --- a/EasyTool.Core/ToolCategory/RegexUtil.cs +++ b/EasyTool.Core/TextCategory/RegexUtil.cs @@ -11,19 +11,6 @@ namespace EasyTool /// public class RegexUtil { - /// - /// 验证字符串是否与指定的正则表达式匹配 - /// [Obsolete("请直接使用 Regex.IsMatch(input, pattern)")] - /// - /// 要验证的字符串 - /// 正则表达式 - /// 如果字符串与正则表达式匹配,则为true;否则为false - [Obsolete("请直接使用 Regex.IsMatch(input, pattern)", false)] - public static bool IsMatch(string input, string pattern) - { - return Regex.IsMatch(input, pattern); - } - /// /// 验证字符串是否与指定的正则表达式匹配,并返回匹配结果 /// @@ -47,20 +34,6 @@ public static string[] Matches(string input, string pattern) return Regex.Matches(input, pattern).Cast().Select(m => m.Value).ToArray(); } - /// - /// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串 - /// [Obsolete("请直接使用 Regex.Replace(input, pattern, replacement)")] - /// - /// 要替换的字符串 - /// 正则表达式 - /// 替换字符串 - /// 替换后的字符串 - [Obsolete("请直接使用 Regex.Replace(input, pattern, replacement)", false)] - public static string Replace(string input, string pattern, string replacement) - { - return Regex.Replace(input, pattern, replacement); - } - /// /// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串,并返回替换后的字符串和替换次数 /// diff --git a/EasyTool.Core/TextCategory/UnicodeUtil.cs b/EasyTool.Core/TextCategory/UnicodeUtil.cs deleted file mode 100644 index de3d562..0000000 --- a/EasyTool.Core/TextCategory/UnicodeUtil.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - public class UnicodeUtil - { - - /// - /// 将Unicode编码的字符串转换为普通字符串。 - /// - /// Unicode编码的字符串 - /// 普通字符串 - public static string UnicodeToString(string unicodeStr) - { - StringBuilder sb = new StringBuilder(); - - int len = unicodeStr.Length; - for (int i = 0; i < len; i++) - { - if (unicodeStr[i] == '\\' && (i + 1) < len && unicodeStr[i + 1] == 'u') - { - string hexStr = unicodeStr.Substring(i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else - { - sb.Append(unicodeStr[i]); - } - } - - return sb.ToString(); - } - - /// - /// 将普通字符串转换为Unicode编码的字符串。 - /// - /// 普通字符串 - /// Unicode编码的字符串 - public static string StringToUnicode(string str) - { - StringBuilder sb = new StringBuilder(); - - foreach (char c in str) - { - sb.Append("\\u"); - sb.Append(((int)c).ToString("x4")); - } - - return sb.ToString(); - } - - /// - /// 将Unicode字符转换为普通字符。 - /// - /// Unicode字符 - /// 普通字符 - public static char UnicodeToChar(string unicodeChar) - { - int code = Convert.ToInt32(unicodeChar, 16); - return (char)code; - } - - /// - /// 将普通字符转换为Unicode字符。 - /// - /// 普通字符 - /// Unicode字符 - public static string CharToUnicode(char c) - { - return "\\u" + ((int)c).ToString("x4"); - } - - /// - /// 将Unicode编码的字符数组转换为普通字符串。 - /// - /// Unicode编码的字符数组 - /// 普通字符串 - public static string UnicodeCharsToString(char[] unicodeChars) - { - StringBuilder sb = new StringBuilder(); - - int len = unicodeChars.Length; - for (int i = 0; i < len; i++) - { - if (i + 1 < len && unicodeChars[i] == '\\' && unicodeChars[i + 1] == 'u') - { - string hexStr = new string(unicodeChars, i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else - { - sb.Append(unicodeChars[i]); - } - } - - return sb.ToString(); - } - - /// - /// 将普通字符串转换为Unicode编码的字符数组。 - /// - /// 普通字符串 - /// Unicode编码的字符数组 - public static char[] StringToUnicodeChars(string str) - { - List chars = new List(); - - foreach (char c in str) - { - chars.AddRange(CharToUnicode(c).ToCharArray()); - } - - return chars.ToArray(); - } - - /// - /// 将Unicode编码的字符数组转换为普通字符串数组。 - /// - /// Unicode编码的字符数组 - /// 普通字符串数组 - public static string[] UnicodeCharsToStringArray(char[] unicodeChars) - { - List strs = new List(); - - StringBuilder sb = new StringBuilder(); - - int len = unicodeChars.Length; - for (int i = 0; i < len; i++) - { - if (i + 1 < len && unicodeChars[i] == '\\' && unicodeChars[i + 1] == 'u') - { - string hexStr = new string(unicodeChars, i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else if (unicodeChars[i] == '\0') - { - strs.Add(sb.ToString()); - sb.Clear(); - } - else - { - sb.Append(unicodeChars[i]); - } - } - - if (sb.Length > 0) - { - strs.Add(sb.ToString()); - } - - return strs.ToArray(); - } - - /// - /// 将普通字符串数组转换为Unicode编码的字符数组。 - /// - /// 普通字符串数组 - /// Unicode编码的字符数组 - public static char[] StringArrayToUnicodeChars(string[] strs) - { - List chars = new List(); - - foreach (string str in strs) - { - chars.AddRange(StringToUnicodeChars(str)); - chars.Add('\0'); - } - - return chars.ToArray(); - } - } -} diff --git a/EasyTool.Core/ToolCategory/ArrayUtil.cs b/EasyTool.Core/ToolCategory/ArrayUtil.cs deleted file mode 100644 index fdf234c..0000000 --- a/EasyTool.Core/ToolCategory/ArrayUtil.cs +++ /dev/null @@ -1,255 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// 数组工具类 - /// - public class ArrayUtil - { - /// - /// 判断数组是否为空 - /// - /// 要判断的数组 - /// 如果数组为空,则返回 true;否则返回 false - public static bool IsEmpty(Array array) - { - return array == null || array.Length == 0; - } - - /// - /// 获取数组的长度 - /// [Obsolete("请直接使用 array?.Length ?? 0")] - /// - /// 要获取长度的数组 - /// 返回数组的长度 - [Obsolete("请直接使用 array?.Length ?? 0", false)] - public static int Length(Array array) - { - if (array == null) - { - return 0; - } - - return array.Length; - } - - /// - /// 获取数组中的最大值 - /// [Obsolete("请直接使用 array.Max() (LINQ)")] - /// - /// 要获取最大值的数组 - /// 返回数组中的最大值 - [Obsolete("请直接使用 array.Max() (LINQ)", false)] - public static T Max(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T max = array[0]; - for (int i = 1; i < array.Length; i++) - { - if (array[i].CompareTo(max) > 0) - { - max = array[i]; - } - } - - return max; - } - - /// - /// 获取数组中的最小值 - /// [Obsolete("请直接使用 array.Min() (LINQ)")] - /// - /// 要获取最小值的数组 - /// 返回数组中的最小值 - [Obsolete("请直接使用 array.Min() (LINQ)", false)] - public static T Min(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T min = array[0]; - for (int i = 1; i < array.Length; i++) - { - if (array[i].CompareTo(min) < 0) - { - min = array[i]; - } - } - return min; - } - - /// - /// 获取数组中的和 - /// [Obsolete("请直接使用 array.Sum() (LINQ)")] - /// - /// 要获取和的数组 - /// 返回数组的和 - [Obsolete("请直接使用 array.Sum() (LINQ)", false)] - public static int Sum(int[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - int sum = 0; - for (int i = 0; i < array.Length; i++) - { - sum += array[i]; - } - - return sum; - } - - /// - /// 获取数组的平均值 - /// [Obsolete("请直接使用 array.Average() (LINQ)")] - /// - /// 要获取平均值的数组 - /// 返回数组的平均值 - [Obsolete("请直接使用 array.Average() (LINQ)", false)] - public static double Average(int[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - int sum = Sum(array); - int length = Length(array); - - return (double)sum / length; - } - - /// - /// 数组排序 - /// - /// 要排序的数组 - /// 返回排序后的数组 - public static T[] Sort(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T[] sortedArray = new T[array.Length]; - array.CopyTo(sortedArray, 0); - Array.Sort(sortedArray); - - return sortedArray; - } - - /// - /// 数组反转 - /// - /// 要反转的数组 - /// 返回反转后的数组 - public static T[] Reverse(T[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T[] reversedArray = new T[array.Length]; - array.CopyTo(reversedArray, 0); - Array.Reverse(reversedArray); - - return reversedArray; - } - - /// - /// 判断数组是否包含某个元素 - /// [Obsolete("请直接使用 array.Contains(item) (LINQ)")] - /// - /// 要操作的数组 - /// 要判断的元素 - /// 如果数组中包含该元素,则返回 true;否则返回 false - [Obsolete("请直接使用 array.Contains(item) (LINQ)", false)] - public static bool Contains(T[] array, T item) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - for (int i = 0; i < array.Length; i++) - { - if (array[i].Equals(item)) - { - return true; - } - } - - return false; - } - - /// - /// 合并两个数组 - /// - /// 数组1 - /// 数组2 - /// 返回合并后的数组 - public static T[] Concat(T[] array1, T[] array2) - { - if (IsEmpty(array1)) - { - return array2; - } - - if (IsEmpty(array2)) - { - return array1; - } - - T[] concatedArray = new T[array1.Length + array2.Length]; - array1.CopyTo(concatedArray, 0); - array2.CopyTo(concatedArray, array1.Length); - - return concatedArray; - } - - /// - /// 判断两个数组是否完全相等 - /// - /// 数组1 - /// 数组2 - /// 如果两个数组完全相等,则返回 true;否则返回 false - public static bool Equals(T[] array1, T[] array2) - { - if (IsEmpty(array1) && IsEmpty(array2)) - { - return true; - } - - if (IsEmpty(array1) || IsEmpty(array2)) - { - return false; - } - - if (array1.Length != array2.Length) - { - return false; - } - - for (int i = 0; i < array1.Length; i++) - { - if (!array1[i].Equals(array2[i])) - { - return false; - } - } - - return true; - } - } -} diff --git a/EasyTool.Core/ToolCategory/ClassExtension.cs b/EasyTool.Core/ToolCategory/ClassExtension.cs deleted file mode 100644 index efc337d..0000000 --- a/EasyTool.Core/ToolCategory/ClassExtension.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace EasyTool.Extension -{ - /// - /// ClassUtil 工具类提供了许多有用的方法,可以帮助您轻松处理和操作C#类 - /// - public static class ClassExtension - { - /// - /// 获取类的继承层次结构 - /// - /// 要获取继承层次结构的类 - /// 类的继承层次结构 - public static Type[] GetClassHierarchy(this Type type) => ClassUtil.GetClassHierarchy(type); - - - - - /// - /// 获取类的静态属性的值 - /// - /// 要获取静态属性的类 - /// 要获取的静态属性的名称 - /// 静态属性的值 - public static object GetStaticPropertyValue(this Type type, string propertyName) => ClassUtil.GetStaticPropertyValue(type, propertyName); - - - /// - /// 设置类的静态属性的值 - /// - /// 要设置静态属性的类 - /// 要设置的静态属性的名称 - /// 要设置的静态属性的值 - public static void SetStaticPropertyValue(this Type type, string propertyName, object value) => ClassUtil.SetStaticPropertyValue(type, propertyName, value); - - - /// - /// 获取类的静态字段的值 - /// - /// 要获取静态字段的类 - /// 要获取的静态字段的名称 - /// 静态字段的值 - public static object GetStaticFieldValue(this Type type, string fieldName) => ClassUtil.GetStaticFieldValue(type, fieldName); - - /// - /// 设置类的静态字段的值 - /// - /// 要设置静态字段的类 - /// 要设置的静态字段的名称 - /// 要设置的静态字段的值 - public static void SetStaticFieldValue(this Type type, string fieldName, object value) => ClassUtil.SetStaticFieldValue(type, fieldName, value); - - - /// - /// 动态调用类的静态方法 - /// - /// 要调用静态方法的类 - /// 要调用的静态方法的名称 - /// 要传递给静态方法的参数 - /// 静态方法的返回值 - public static object InvokeStaticMethod(this Type type, string methodName, object[] arguments) => ClassUtil.InvokeStaticMethod(type, methodName, arguments); - - ///// - ///// 动态调用类的实例方法 - ///// - ///// 要调用实例方法的类实例 - ///// 要调用的实例方法的名称 - ///// 要传递给实例方法的参数 - ///// 实例方法的返回值 - //public static object InvokeMethod(object instance, string methodName, object[] arguments) - //{ - // Type type = instance.GetType(); - // MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); - // return method.Invoke(instance, arguments); - //} - - /// - /// 动态创建类的实例 - /// - /// 要创建实例的类 - /// 要传递给构造函数的参数 - /// 类的新实例 - public static object CreateInstance(this Type type, object[] constructorArguments) => ClassUtil.CreateInstance(type, constructorArguments); - } -} diff --git a/EasyTool.Core/ToolCategory/ClassUtil.cs b/EasyTool.Core/ToolCategory/ClassUtil.cs deleted file mode 100644 index 5ee1647..0000000 --- a/EasyTool.Core/ToolCategory/ClassUtil.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// ClassUtil 工具类提供了许多有用的方法,可以帮助您轻松处理和操作C#类 - /// - public class ClassUtil - { - /// - /// 获取类的完全限定名 - /// [Obsolete("请直接使用 type.FullName")] - /// - /// 要获取名称的类 - /// 类的完全限定名 - [Obsolete("请直接使用 type.FullName", false)] - public static string GetClassName(Type type) - { - return type.FullName; - } - - /// - /// 获取类的命名空间 - /// [Obsolete("请直接使用 type.Namespace")] - /// - /// 要获取命名空间的类 - /// 类的命名空间 - [Obsolete("请直接使用 type.Namespace", false)] - public static string GetClassNamespace(Type type) - { - return type.Namespace; - } - - /// - /// 获取类的继承层次结构 - /// - /// 要获取继承层次结构的类 - /// 类的继承层次结构 - public static Type[] GetClassHierarchy(Type type) - { - Type[] hierarchy = new Type[0]; - Type currentType = type; - while (currentType != null) - { - Array.Resize(ref hierarchy, hierarchy.Length + 1); - hierarchy[hierarchy.Length - 1] = currentType; - currentType = currentType.BaseType; - } - return hierarchy; - } - - /// - /// 获取类的所有方法 - /// [Obsolete("请直接使用 type.GetMethods()")] - /// - /// 要获取方法的类 - /// 类的所有方法 - [Obsolete("请直接使用 type.GetMethods()", false)] - public static MethodInfo[] GetClassMethods(Type type) - { - return type.GetMethods(); - } - - /// - /// 获取类的所有属性 - /// [Obsolete("请直接使用 type.GetProperties()")] - /// - /// 要获取属性的类 - /// 类的所有属性 - [Obsolete("请直接使用 type.GetProperties()", false)] - public static PropertyInfo[] GetClassProperties(Type type) - { - return type.GetProperties(); - } - - /// - /// 获取类的所有字段 - /// [Obsolete("请直接使用 type.GetFields()")] - /// - /// 要获取字段的类 - /// 类的所有字段 - [Obsolete("请直接使用 type.GetFields()", false)] - public static FieldInfo[] GetClassFields(Type type) - { - return type.GetFields(); - } - - /// - /// 获取类的所有事件 - /// [Obsolete("请直接使用 type.GetEvents()")] - /// - /// 要获取事件的类 - /// 类的所有事件 - [Obsolete("请直接使用 type.GetEvents()", false)] - public static EventInfo[] GetClassEvents(Type type) - { - return type.GetEvents(); - } - - /// - /// 获取类的所有构造函数 - /// [Obsolete("请直接使用 type.GetConstructors()")] - /// - /// 要获取构造函数的类 - /// 类的所有构造函数 - [Obsolete("请直接使用 type.GetConstructors()", false)] - public static ConstructorInfo[] GetClassConstructors(Type type) - { - return type.GetConstructors(); - } - - /// - /// 获取类的默认构造函数 - /// [Obsolete("请直接使用 type.GetConstructor(Type.EmptyTypes)")] - /// - /// 要获取默认构造函数的类 - /// 类的默认构造函数 - [Obsolete("请直接使用 type.GetConstructor(Type.EmptyTypes)", false)] - public static ConstructorInfo GetDefaultClassConstructor(Type type) - { - return type.GetConstructor(Type.EmptyTypes); - } - - /// - /// 获取类的静态属性的值 - /// - /// 要获取静态属性的类 - /// 要获取的静态属性的名称 - /// 静态属性的值 - public static object GetStaticPropertyValue(Type type, string propertyName) - { - PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); - return property.GetValue(null); - } - - /// - /// 设置类的静态属性的值 - /// - /// 要设置静态属性的类 - /// 要设置的静态属性的名称 - /// 要设置的静态属性的值 - public static void SetStaticPropertyValue(Type type, string propertyName, object value) - { - PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); - property.SetValue(null, value); - } - - /// - /// 获取类的静态字段的值 - /// - /// 要获取静态字段的类 - /// 要获取的静态字段的名称 - /// 静态字段的值 - public static object GetStaticFieldValue(Type type, string fieldName) - { - FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); - return field.GetValue(null); - } - - /// - /// 设置类的静态字段的值 - /// - /// 要设置静态字段的类 - /// 要设置的静态字段的名称 - /// 要设置的静态字段的值 - public static void SetStaticFieldValue(Type type, string fieldName, object value) - { - FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); - field.SetValue(null, value); - } - - /// - /// 动态调用类的静态方法 - /// - /// 要调用静态方法的类 - /// 要调用的静态方法的名称 - /// 要传递给静态方法的参数 - /// 静态方法的返回值 - public static object InvokeStaticMethod(Type type, string methodName, object[] arguments) - { - MethodInfo method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public); - return method.Invoke(null, arguments); - } - - /// - /// 动态调用类的实例方法 - /// - /// 要调用实例方法的类实例 - /// 要调用的实例方法的名称 - /// 要传递给实例方法的参数 - /// 实例方法的返回值 - public static object InvokeMethod(object instance, string methodName, object[] arguments) - { - Type type = instance.GetType(); - MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); - return method.Invoke(instance, arguments); - } - - /// - /// 动态创建类的实例 - /// - /// 要创建实例的类 - /// 要传递给构造函数的参数 - /// 类的新实例 - public static object CreateInstance(Type type, params object[] constructorArguments) - { - ConstructorInfo constructor = type.GetConstructor(GetParameterTypes(constructorArguments)); - return constructor.Invoke(constructorArguments); - } - - /// - /// 获取构造函数参数类型的数组 - /// - /// 要获取参数类型的参数数组 - /// 参数类型的数组 - private static Type[] GetParameterTypes(object[] parameters) - { - if (parameters == null) - { - return Type.EmptyTypes; - } - Type[] parameterTypes = new Type[parameters.Length]; - for (int i = 0; i < parameters.Length; i++) - { - if (parameters[i] == null) - { - parameterTypes[i] = typeof(object); - } - else - { - parameterTypes[i] = parameters[i].GetType(); - } - } - return parameterTypes; - } - } -} diff --git a/EasyTool.Core/ToolCategory/ColorExtension.cs b/EasyTool.Core/ToolCategory/ColorExtension.cs index 0298c0a..41f7913 100644 --- a/EasyTool.Core/ToolCategory/ColorExtension.cs +++ b/EasyTool.Core/ToolCategory/ColorExtension.cs @@ -279,19 +279,7 @@ public static bool IsLight(this Color color) #endregion #region 命名颜色 - - /// - /// 从名称创建颜色 - /// [Obsolete("请直接使用 Color.FromName(name)")] - /// - [Obsolete("请直接使用 Color.FromName(name)", false)] - public static Color FromName(string name) - { - if (string.IsNullOrEmpty(name)) - return Color.Empty; - - return Color.FromName(name); - } + #endregion /// /// 获取颜色名称 @@ -304,7 +292,7 @@ public static string GetColorName(this Color color) return color.ToHex(); } - #endregion + // endregion #region 颜色对比 diff --git a/EasyTool.Core/ToolCategory/DLLUtil.cs b/EasyTool.Core/ToolCategory/DLLUtil.cs deleted file mode 100644 index d14a199..0000000 --- a/EasyTool.Core/ToolCategory/DLLUtil.cs +++ /dev/null @@ -1,148 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// dll工具 - /// - public class DLLUtil - { - /// - /// 根据文件路径加载 DLL 程序集,并返回一个 Assembly 对象 - /// [Obsolete("请直接使用 Assembly.LoadFile(dllFilePath)")] - /// - /// DLL 文件路径 - /// 返回一个 Assembly 对象 - [Obsolete("请直接使用 Assembly.LoadFile(dllFilePath)", false)] - public static Assembly LoadAssembly(string dllFilePath) - { - return Assembly.LoadFile(dllFilePath); - } - - /// - /// 根据类型名称从程序集中获取 Type 对象 - /// [Obsolete("请直接使用 assembly.GetType(typeName)")] - /// - /// 程序集 - /// 类型名称 - /// 返回 Type 对象 - [Obsolete("请直接使用 assembly.GetType(typeName)", false)] - public static Type? GetTypeFromAssembly(Assembly assembly, string typeName) - { - return assembly.GetType(typeName); - } - - /// - /// 创建指定类型的实例,并返回一个 Object 对象 - /// [Obsolete("请直接使用 Activator.CreateInstance(type, parameters)")] - /// - /// 要创建实例的类型 - /// 实例化类型所需要的参数 - /// 返回创建的实例对象 - [Obsolete("请直接使用 Activator.CreateInstance(type, parameters)", false)] - public static object? CreateInstance(Type type, params object[] parameters) - { - return Activator.CreateInstance(type, parameters); - } - - /// - /// 根据类型名称创建实例,并返回一个 Object 对象 - /// - /// 程序集 - /// 类型名称 - /// 实例化类型所需要的参数 - /// 返回创建的实例对象 - public static object? CreateInstanceFromAssembly(Assembly assembly, string typeName, params object[] parameters) - { - Type? type = GetTypeFromAssembly(assembly, typeName); - if (type != null) - { - return CreateInstance(type, parameters); - } - return null; - } - - /// - /// 调用对象的方法,并返回调用结果 - /// - /// 要调用方法的对象 - /// 方法名称 - /// 方法所需要的参数 - /// 返回调用结果 - public static object? InvokeMethod(object instance, string methodName, params object[] parameters) - { - Type type = instance.GetType(); - MethodInfo? methodInfo = type.GetMethod(methodName); - if (methodInfo != null) - { - return methodInfo.Invoke(instance, parameters); - } - else - { - return null; - } - } - - /// - /// 获取程序集中所有的类型信息 - /// [Obsolete("请直接使用 assembly.GetTypes()")] - /// - /// 程序集 - /// 返回 Type[] 数组,数组中每个元素代表程序集中的一个类型 - [Obsolete("请直接使用 assembly.GetTypes()", false)] - public static Type[] GetAllTypesFromAssembly(Assembly assembly) - { - return assembly.GetTypes(); - } - - /// - /// 判断指定类型是否实现了指定的接口 - /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)")] - /// - /// 要判断的类型 - /// 要判断的接口类型 - /// 返回布尔值,表示指定类型是否实现了指定的接口 - [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)", false)] - public static bool IsImplementInterface(Type type, Type interfaceType) - { - return interfaceType.IsAssignableFrom(type); - } - - /// - /// 从指定目录中加载所有的 DLL 文件,并返回一个 Assembly[] 数组 - /// - /// 要加载 DLL 文件的目录 - /// 返回一个 Assembly[] 数组,数组中每个元素代表一个 DLL 程序集 - public static Assembly[] LoadAllDllsFromDirectory(string directory) - { - try - { - if (!Directory.Exists(directory)) - { - throw new Exception("LoadAllDllsFromDirectory Error: Directory not exist."); - } - - string[] dllFiles = Directory.GetFiles(directory, "*.dll"); - if (dllFiles.Length == 0) - { - throw new Exception("LoadAllDllsFromDirectory Error: No DLL file found."); - } - - Assembly[] assemblies = new Assembly[dllFiles.Length]; - for (int i = 0; i < dllFiles.Length; i++) - { - assemblies[i] = LoadAssembly(dllFiles[i]); - } - return assemblies; - } - catch (Exception ex) - { - throw new Exception("LoadAllDllsFromDirectory Error: " + ex.Message); - } - } - } -} diff --git a/EasyTool.Core/ToolCategory/EnumExtension.cs b/EasyTool.Core/ToolCategory/EnumExtension.cs index bd117b5..6605335 100644 --- a/EasyTool.Core/ToolCategory/EnumExtension.cs +++ b/EasyTool.Core/ToolCategory/EnumExtension.cs @@ -48,18 +48,6 @@ public static string GetDisplayName(this Enum value) #region 枚举转换 - /// - /// 将枚举值转换为整数 - /// [Obsolete("请直接使用 Convert.ToInt32(value)")] - /// - [Obsolete("请直接使用 Convert.ToInt32(value)", false)] - public static int ToInt(this Enum value) - { - if (value == null) - return 0; - - return Convert.ToInt32(value); - } /// /// 将整数转换为枚举 @@ -88,19 +76,57 @@ public static T ParseEnumOrDefault(this string value, T defaultValue = defaul return defaultValue; } + + #endregion + + #region 枚举字典扩展 + /// - /// 尝试解析字符串为枚举 - /// [Obsolete("请直接使用 Enum.TryParse(value, true, out result)")] + /// 获取枚举类型的所有成员的名称和值的键值对 /// - [Obsolete("请直接使用 Enum.TryParse(value, true, out result)", false)] - public static bool TryParseEnum(this string value, out T result) where T : struct, Enum + public static IDictionary ToNameDictionary() where T : struct, Enum { - return Enum.TryParse(value, true, out result); + var valuesDictionary = new Dictionary(); + foreach (T value in GetValues()) + { + valuesDictionary.Add(Enum.GetName(typeof(T), value)!, value); + } + return valuesDictionary; + } + + /// + /// 获取指定枚举值的描述 + /// + public static string? GetDescriptionByValue(T value) where T : struct, Enum + { + var name = Enum.GetName(typeof(T), value); + if (string.IsNullOrEmpty(name)) + { + return null; + } + var field = typeof(T).GetField(name); + var attr = field?.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; + return attr?.Description; + } + + /// + /// 获取指定枚举值的显示名称 + /// + public static string? GetDisplayNameByValue(T value) where T : struct, Enum + { + var name = Enum.GetName(typeof(T), value); + if (string.IsNullOrEmpty(name)) + { + return null; + } + var field = typeof(T).GetField(name); + var attr = field?.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.DisplayAttribute; + return attr?.GetName(); } #endregion - #region 枚举集合 + #region 枚举判断 /// /// 获取枚举类型的所有值 @@ -148,15 +174,6 @@ public static Dictionary ToDisplayNameDictionary() where T : struc #region 枚举判断 - /// - /// 判断是否是定义的枚举值 - /// [Obsolete("请直接使用 Enum.IsDefined(typeof(T), value)")] - /// - [Obsolete("请直接使用 Enum.IsDefined(typeof(T), value)", false)] - public static bool IsDefined(this T value) where T : struct, Enum - { - return Enum.IsDefined(typeof(T), value); - } /// /// 判断字符串是否是有效的枚举名称或值 diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs deleted file mode 100644 index f03522b..0000000 --- a/EasyTool.Core/ToolCategory/EnumUtil.cs +++ /dev/null @@ -1,253 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// 枚举工具类,包含了各种枚举操作的方法 - /// - public class EnumUtil - { - /// - /// 获取指定枚举类型的所有成员名称 - /// [Obsolete("请直接使用 Enum.GetNames(typeof(TEnum))")] - /// - /// 要获取成员名称的枚举类型 - /// 所有成员名称的字符串数组 - [Obsolete("请直接使用 Enum.GetNames(typeof(TEnum))", false)] - public static string[] GetNames() - { - return Enum.GetNames(typeof(TEnum)); - } - - /// - /// 获取指定枚举类型的所有成员的值 - /// [Obsolete("请直接使用 (TEnum[])Enum.GetValues(typeof(TEnum))")] - /// - /// 要获取成员值的枚举类型 - /// 所有成员值的数组 - [Obsolete("请直接使用 (TEnum[])Enum.GetValues(typeof(TEnum))", false)] - public static TEnum[] GetValues() - { - return (TEnum[])Enum.GetValues(typeof(TEnum)); - } - - /// - /// 获取指定枚举值的名称 - /// [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)")] - /// - /// 枚举类型 - /// 枚举值 - /// 枚举值的名称 - [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)", false)] - public static string GetName(TEnum value) - { - return Enum.GetName(typeof(TEnum), value); - } - - /// - /// 检查指定的值是否是枚举类型TEnum的成员 - /// [Obsolete("请直接使用 Enum.IsDefined(typeof(TEnum), value)")] - /// - /// 枚举类型 - /// 要检查的值 - /// 如果指定的值是TEnum的成员,则为true;否则为false - [Obsolete("请直接使用 Enum.IsDefined(typeof(TEnum), value)", false)] - public static bool IsDefined(object value) - { - return Enum.IsDefined(typeof(TEnum), value); - } - - /// - /// 将字符串转换为枚举类型TEnum的值 - /// [Obsolete("请直接使用 (TEnum)Enum.Parse(typeof(TEnum), value)")] - /// - /// 枚举类型 - /// 要转换的字符串 - /// 与字符串对应的枚举值 - [Obsolete("请直接使用 (TEnum)Enum.Parse(typeof(TEnum), value)", false)] - public static TEnum Parse(string value) - { - return (TEnum)Enum.Parse(typeof(TEnum), value); - } - - /// - /// 将字符串转换为枚举类型TEnum的值如果字符串无法转换,则返回默认值 - /// - /// 枚举类型 - /// 要转换的字符串 - /// 默认值 - /// 与字符串对应的枚举值,或默认值(如果字符串无法转换) - public static TEnum Parse(string value, TEnum defaultValue) - { - var result= Enum.Parse(typeof(TEnum), value); - return result!=null ? (TEnum)result : defaultValue; - } - - /// - /// 获取指定枚举类型的Type对象 - /// [Obsolete("请直接使用 typeof(TEnum)")] - /// - /// 枚举类型 - /// 枚举类型的Type对象 - [Obsolete("请直接使用 typeof(TEnum)", false)] - public static Type GetEnumType() - { - return typeof(TEnum); - } - - /// - /// 获取指定枚举类型的所有成员的名称和值的键值对 - /// - /// 枚举类型 - /// 所有成员名称和值的键值对 - public static IDictionary GetValuesDictionary() - { - var valuesDictionary = new Dictionary(); - foreach (var value in GetValues()) - { - valuesDictionary.Add(GetName(value), value); - } - return valuesDictionary; - } - - /// - /// 获取指定枚举类型的所有成员的注释 - /// - /// 枚举类型 - /// 所有成员注释的字典,其中键是成员名称,值是注释内容 - public static IDictionary GetDescriptions() - { - var descriptions = new Dictionary(); - var enumType = GetEnumType(); - foreach (var memberInfo in enumType.GetMembers()) - { - var attribute = memberInfo.GetCustomAttribute(); - if (attribute != null) - { - descriptions.Add(memberInfo.Name, attribute.Description); - } - } - return descriptions; - } - - /// - /// 获取指定枚举类型的指定成员的注释 - /// - /// 枚举类型 - /// 枚举成员 - /// 成员注释的字符串 - public static string GetDescription(TEnum value) - { - var memberInfo = GetEnumType().GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo == null) - { - return string.Empty; - } - - var attribute = memberInfo.GetCustomAttribute(); - return attribute != null ? attribute.Description : string.Empty; - } - - /// - /// 获取指定枚举类型的指定成员的Display名称 - /// - /// 枚举类型 - /// 枚举成员 - /// 成员的Display名称,如果未设置,则返回枚举成员的名称 - public static string GetDisplayName(TEnum value) - { - var memberInfo = GetEnumType().GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo == null) - { - return string.Empty; - } - - var attribute = memberInfo.GetCustomAttribute(); - return attribute != null ? attribute.Name : value.ToString(); - } - - /// - /// 获取指定枚举类型的所有成员的Display名称 - /// - /// 枚举类型 - /// 所有成员的Display名称的字典,其中键是成员名称,值是Display名称 - public static IDictionary GetDisplayNames() - { - var displayNames = new Dictionary(); - var enumType = GetEnumType(); - foreach (var memberInfo in enumType.GetMembers()) - { - var attribute = memberInfo.GetCustomAttribute(); - if (attribute != null) - { - displayNames.Add(memberInfo.Name, attribute.Name); - } - } - return displayNames; - } - - /// - /// 获取指定枚举类型的指定成员的值 - /// - /// 枚举类型 - /// 成员名称 - /// 成员的值,如果成员不存在,则返回默认值 - public static TEnum GetValueByName(string name) - { - return Parse(name, default(TEnum)); - } - - /// - /// 获取指定枚举类型的指定值的名称 - /// [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)")] - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的名称,如果值不存在,则返回null - [Obsolete("请直接使用 Enum.GetName(typeof(TEnum), value)", false)] - public static string? GetNameByValue(TEnum value) - { - return Enum.GetName(typeof(TEnum), value!); - } - - /// - /// 获取指定枚举类型的指定值的注释 - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的注释,如果值不存在或未设置注释,则返回null - public static string? GetDescriptionByValue(TEnum value) - { - var name = GetNameByValue(value); - if (string.IsNullOrEmpty(name)) - { - return null; - } - - return GetDescription(GetValueByName(name!)); - } - - /// - /// 获取指定枚举类型的指定值的Display名称 - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的Display名称,如果值不存在或未设置Display名称,则返回null - public static string? GetDisplayNameByValue(TEnum value) - { - var name = GetNameByValue(value); - if (string.IsNullOrEmpty(name)) - { - return null; - } - - return GetDisplayName(GetValueByName(name!)); - } - } -} diff --git a/EasyTool.Core/ToolCategory/EscapeUtil.cs b/EasyTool.Core/ToolCategory/EscapeUtil.cs deleted file mode 100644 index eea8c2f..0000000 --- a/EasyTool.Core/ToolCategory/EscapeUtil.cs +++ /dev/null @@ -1,211 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; - -namespace EasyTool -{ - /// - /// 转义和反转义工具类 - /// - public class EscapeUtil - { - /// - /// 将字符串中的特殊字符进行转义 - /// - /// 需要转义的字符串 - /// 转义后的字符串 - public static string Escape(string str) - { - if (string.IsNullOrEmpty(str)) - { - return str; - } - - string escaped = Regex.Replace(str, @"[\a\b\f\n\r\t\v\\""]", m => { - switch (m.Value) - { - case "\a": - return @"\a"; - case "\b": - return @"\b"; - case "\f": - return @"\f"; - case "\n": - return @"\n"; - case "\r": - return @"\r"; - case "\t": - return @"\t"; - case "\v": - return @"\v"; - case "\\": - return @"\\"; - case "\"": - return @"\"""; - default: - return m.Value; - } - }); - - return escaped; - } - - /// - /// 将字符串中的转义字符还原成特殊字符 - /// - /// 需要还原的字符串 - /// 还原后的字符串 - public static string Unescape(string str) - { - if (string.IsNullOrEmpty(str)) - { - return str; - } - - string unescaped = Regex.Replace(str, @"\\[a-z""\\]", m => { - switch (m.Value) - { - case @"\a": - return "\a"; - case @"\b": - return "\b"; - case @"\f": - return "\f"; - case @"\n": - return "\n"; - case @"\r": - return "\r"; - case @"\t": - return "\t"; - case @"\v": - return "\v"; - case @"\\": - return "\\"; - case @"\""": - return "\""; - default: - return m.Value; - } - }); - - return unescaped; - } - - /// - /// 将URL中的特殊字符进行转义 - /// [Obsolete("请直接使用 Uri.EscapeDataString(url)")] - /// - /// 需要转义的URL - /// 转义后的URL - [Obsolete("请直接使用 Uri.EscapeDataString(url)", false)] - public static string UrlEncode(string url) - { - if (string.IsNullOrEmpty(url)) - { - return url; - } - - return Uri.EscapeDataString(url); - } - - /// - /// 将URL中的转义字符还原成特殊字符 - /// [Obsolete("请直接使用 Uri.UnescapeDataString(url)")] - /// - /// 需要还原的URL - /// 还原后的URL - [Obsolete("请直接使用 Uri.UnescapeDataString(url)", false)] - public static string UrlDecode(string url) - { - if (string.IsNullOrEmpty(url)) - { - return url; - } - - return Uri.UnescapeDataString(url); - } - - /// - /// 将HTML字符串进行转义,将特殊字符替换成HTML实体 - /// [Obsolete("请直接使用 System.Net.WebUtility.HtmlEncode(html)")] - /// - /// 需要转义的HTML字符串 - /// 转义后的HTML字符串 - [Obsolete("请直接使用 System.Net.WebUtility.HtmlEncode(html)", false)] - public static string HtmlEncode(string html) - { - if (string.IsNullOrEmpty(html)) - { - return html; - } - - return System.Net.WebUtility.HtmlEncode(html); - } - - /// - /// 将HTML字符串中的HTML实体还原成特殊字符 - /// [Obsolete("请直接使用 System.Net.WebUtility.HtmlDecode(html)")] - /// - /// 需要还原的HTML字符串 - /// 还原后的HTML字符串 - [Obsolete("请直接使用 System.Net.WebUtility.HtmlDecode(html)", false)] - public static string HtmlDecode(string html) - { - if (string.IsNullOrEmpty(html)) - { - return html; - } - - return System.Net.WebUtility.HtmlDecode(html); - } - - /// - /// 将XML字符串进行转义,将特殊字符替换成XML实体 - /// [Obsolete("请直接使用 System.Security.SecurityElement.Escape(xml)")] - /// - /// 需要转义的XML字符串 - /// 转义后的XML字符串 - [Obsolete("请直接使用 System.Security.SecurityElement.Escape(xml)", false)] - public static string XmlEncode(string xml) - { - if (string.IsNullOrEmpty(xml)) - { - return xml; - } - - return System.Security.SecurityElement.Escape(xml); - } - - /// - /// 将XML字符串中的XML实体还原成特殊字符 - /// - /// 需要还原的XML字符串 - /// 还原后的XML字符串 - public static string XmlDecode(string xml) - { - if (string.IsNullOrEmpty(xml)) - { - return xml; - } - - return Regex.Replace(xml, @"&[a-zA-Z]+;", m => { - switch (m.Value) - { - case "&": - return "&"; - case "<": - return "<"; - case ">": - return ">"; - case """: - return "\""; - case "'": - return "'"; - default: - return m.Value; - } - }); - } - } -} diff --git a/EasyTool.Core/ToolCategory/ExceptionExtension.cs b/EasyTool.Core/ToolCategory/ExceptionExtension.cs index 6ebc43e..91824bc 100644 --- a/EasyTool.Core/ToolCategory/ExceptionExtension.cs +++ b/EasyTool.Core/ToolCategory/ExceptionExtension.cs @@ -320,18 +320,6 @@ public static Exception[] GetInnerExceptions(this AggregateException? exception) return exception.InnerExceptions.ToArray(); } - /// - /// 展平聚合异常(递归获取所有内层异常) - /// [Obsolete("请直接使用 exception.Flatten().InnerExceptions.ToArray()")] - /// - [Obsolete("请直接使用 exception.Flatten().InnerExceptions.ToArray()", false)] - public static Exception[] Flatten(this AggregateException? exception) - { - if (exception == null) - return Array.Empty(); - - return exception.Flatten().InnerExceptions.ToArray(); - } #endregion } diff --git a/EasyTool.Core/ToolCategory/GuidExtension.cs b/EasyTool.Core/ToolCategory/GuidExtension.cs index 4f04619..39f97fd 100644 --- a/EasyTool.Core/ToolCategory/GuidExtension.cs +++ b/EasyTool.Core/ToolCategory/GuidExtension.cs @@ -10,15 +10,6 @@ public static class GuidExtension { #region 空值判断 - /// - /// 判断 Guid 是否为空 - /// [Obsolete("请直接使用 guid == Guid.Empty")] - /// - [Obsolete("请直接使用 guid == Guid.Empty", false)] - public static bool IsEmpty(this Guid guid) - { - return guid == Guid.Empty; - } /// /// 判断 Guid 是否为空或默认值 @@ -80,15 +71,6 @@ public static string ToBracedString(this Guid guid) return guid.ToString("B"); } - /// - /// 获取 Guid 的字节数组表示 - /// [Obsolete("请直接使用 guid.ToByteArray()")] - /// - [Obsolete("请直接使用 guid.ToByteArray()", false)] - public static byte[] ToByteArray(this Guid guid) - { - return guid.ToByteArray(); - } /// /// 将 Guid 转换为 Base64 字符串 diff --git a/EasyTool.Core/ToolCategory/IdcardUtil.cs b/EasyTool.Core/ToolCategory/IdcardUtil.cs deleted file mode 100644 index 0f6d9bd..0000000 --- a/EasyTool.Core/ToolCategory/IdcardUtil.cs +++ /dev/null @@ -1,470 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; - -namespace EasyTool -{ - public class IdcardUtil - { - /// - /// 验证身份证号码是否合法 - /// - /// 身份证号码 - /// 验证结果 - public static bool IsValid(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (idcard.Length == 15) - { - return IsValid15(idcard); - } - else if (idcard.Length == 18) - { - return IsValid18(idcard); - } - else - { - return false; - } - } - - /// - /// 验证 15 位身份证号码是否合法 - /// - /// 15 位身份证号码 - /// 验证结果 - public static bool IsValid15(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (!Regex.IsMatch(idcard, @"^\d{15}$")) - { - return false; - } - - if (!IsValidArea(idcard.Substring(0, 6))) - { - return false; - } - - DateTime birthday; - if (!DateTime.TryParseExact(idcard.Substring(6, 6), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return false; - } - - return true; - } - - /// - /// 验证 18 位身份证号码是否合法 - /// - /// 18 位身份证号码 - /// 验证结果 - public static bool IsValid18(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (!Regex.IsMatch(idcard, @"^\d{17}[\dX]$")) - { - return false; - } - - if (!IsValidArea(idcard.Substring(0, 6))) - { - return false; - } - - DateTime birthday; - if (!DateTime.TryParseExact(idcard.Substring(6, 8), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return false; - } - - if (!IsValidChecksum(idcard)) - { - return false; - } - - return true; - } - - /// - /// 判断给定的区域代码是否合法 - /// - /// 区域代码 - /// 是否合法 - public static bool IsValidArea(string area) - { - if (string.IsNullOrEmpty(area)) - { - return false; - } - - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - return areas.Contains(area); - } - - /// - /// 验证身份证号码的校验位是否正确 - /// - /// 身份证号码 - /// 验证结果 - public static bool IsValidChecksum(string idcard) - { - int[] weights = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; - string[] checksums = new string[] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; - - int sum = 0; - for (int i = 0; i < 17; i++) - { - sum += int.Parse(idcard[i].ToString()) * weights[i]; - } - - int checksumIndex = sum % 11; - string expectedChecksum = checksums[checksumIndex]; - - return idcard[17].ToString().ToUpper() == expectedChecksum.ToUpper(); - } - - /// - /// 从身份证号码中获取生日 - /// - /// 身份证号码 - /// 生日 - public static DateTime? GetBirthday(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (idcard.Length == 15) - { - if (!IsValid15(idcard)) - { - return null; - } - - DateTime birthday; - if (DateTime.TryParseExact(idcard.Substring(6, 6), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return birthday; - } - else - { - return null; - } - } - else if (idcard.Length == 18) - { - if (!IsValid18(idcard)) - { - return null; - } - - DateTime birthday; - if (DateTime.TryParseExact(idcard.Substring(6, 8), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return birthday; - } - else - { - return null; - } - } - else - { - return null; - } - } - - /// - /// 从身份证号码中获取性别 - /// - /// 身份证号码 - /// 性别 - public static Gender? GetGender(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (idcard.Length == 15) - { - if (!IsValid15(idcard)) - { - return null; - } - - int genderCode = int.Parse(idcard.Substring(14, 1)); - return genderCode % 2 == 1 ? Gender.Male : Gender.Female; - } - else if (idcard.Length == 18) - { - if (!IsValid18(idcard)) - { - return null; - } - - int genderCode = int.Parse(idcard.Substring(16, 1)); - return genderCode % 2 == 1 ? Gender.Male : Gender.Female; - } - else - { - return null; - } - } - - /// - /// 从身份证号码中获取 - /// - /// 身份证号码 - /// 省份 - public static string? GetProvince(string? idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (IsValid(idcard)) - { - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - string provinceCode = idcard.Substring(0, 2); - if (areas.Contains(provinceCode)) - { - switch (provinceCode) - { - case "11": - return "北京市"; - case "12": - return "天津市"; - case "13": - return "河北省"; - case "14": - return "山西省"; - case "15": - return "内蒙古自治区"; - case "21": - return "辽宁省"; - case "22": - return "吉林省"; - case "23": - return "黑龙江省"; - case "31": - return "上海市"; - case "32": - return "江苏省"; - case "33": - return "浙江省"; - case "34": - return "安徽省"; - case "35": - return "福建省"; - case "36": - return "江西省"; - case "37": - return "山东省"; - case "41": - return "河南省"; - case "42": - return "湖北省"; - case "43": - return "湖南省"; - case "44": - return "广东省"; - case "45": - return "广西壮族自治区"; - case "46": - return "海南省"; - case "50": - return "重庆市"; - case "51": - return "四川省"; - case "52": - return "贵州省"; - case "53": - return "云南省"; - case "54": - return "西藏自治区"; - case "61": - return "陕西省"; - case "62": - return "甘肃省"; - case "63": - return "青海省"; - case "64": - return "宁夏回族自治区"; - case "65": - return "新疆维吾尔自治区"; - default: - return null; - } - } - else - { - return null; - } - } - else - { - return null; - } - } - - /// - /// 将身份证号码中的生日部分替换成指定的日期,并返回新的身份证号码 - /// - /// 身份证号码 - /// 新的生日日期 - /// 新的身份证号码 - public static string? ReplaceBirthday(string? idcard, DateTime birthday) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (!IsValid(idcard)) - { - return null; - } - - string birthdayStr = birthday.ToString("yyyyMMdd"); - if (idcard.Length == 15) - { - return idcard.Substring(0, 6) + birthdayStr + idcard.Substring(12); - } - else if (idcard.Length == 18) - { - return idcard.Substring(0, 6) + birthdayStr + idcard.Substring(14); - } - else - { - return null; - } - } - - /// - /// 将身份证号码中的性别部分替换成指定的性别,并返回新的身份证号码 - /// - /// 身份证号码 - /// 新的性别 - /// 新的身份证号码 - public static string? ReplaceGender(string idcard, Gender gender) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (!IsValid(idcard)) - { - return null; - } - - int genderCode = gender == Gender.Male ? 1 : 2; - if (idcard.Length == 15) - { - return idcard.Substring(0, 14) + genderCode.ToString(); - } - else if (idcard.Length == 18) - { - return idcard.Substring(0, 16) + genderCode.ToString() + idcard.Substring(17); - } - else - { - return null; - } - } - - /// - /// 生成一个随机的身份证号码 - /// - /// 性别 - /// 最小年龄 - /// 最大年龄 - /// 随机的身份证号码 - public static string GenerateRandomIdcard(Gender gender = Gender.Male, int minAge = 18, int maxAge = 65) - { - DateTime now = DateTime.Now; - DateTime minBirthday = now.AddYears(-maxAge); - DateTime maxBirthday = now.AddYears(-minAge); - - DateTime birthday = RandomUtil.GetRandomDateTime(minBirthday, maxBirthday); - string area = GetRandomArea(); - int sequence = RandomUtil.GetRandomInt(1, 999); - int genderCode = gender == Gender.Male ? 1 : 2; - - string idcard = string.Format("{0}{1:yyyyMMdd}{2:D3}{3:D1}", area, birthday, sequence, genderCode); - if (!IsValid(idcard)) - { - return GenerateRandomIdcard(gender, minAge, maxAge); - } - else - { - return idcard; - } - } - - /// - /// 获取一个随机的身份证号码的区域代码 - /// - /// 区域代码 - private static string GetRandomArea() - { - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - int index = RandomUtil.GetRandomInt(0, areas.Length - 1); - return areas[index]; - } - - - /// - /// 性别枚举 - /// - public enum Gender - { - /// - /// 男性 - /// - Male, - /// - /// 女性 - /// - Female - } - } -} diff --git a/EasyTool.Core/ToolCategory/MEFUtil.cs b/EasyTool.Core/ToolCategory/MEFUtil.cs deleted file mode 100644 index 481bfa5..0000000 --- a/EasyTool.Core/ToolCategory/MEFUtil.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition.Hosting; -using System.Reflection; -using System.Linq; -using System.IO; - -namespace EasyTool -{ - /// - /// MEF加载工具 - /// - public class MEFUtil - { - // 默认扫描程序集的路径 - private static readonly string DefaultDirectory = AppDomain.CurrentDomain.BaseDirectory; - - /// - /// 从指定目录动态加载导出部件 - /// - /// 导出部件的类型 - /// 目录路径 - /// 导出部件的列表 - public static IEnumerable LoadExportParts(string? directory = null) - { - // 如果目录为空,则使用默认目录 - directory ??= DefaultDirectory; - - // 创建目录目录目录目录 - var catalog = new DirectoryCatalog(directory); - // 创建容器并将目录添加到容器中 - var container = new CompositionContainer(catalog); - - // 从容器中获取导出的部件 - var parts = container.GetExportedValues(); - return parts; - } - - /// - /// 从指定程序集动态加载导出部件 - /// - /// 导出部件的类型 - /// 程序集名称 - /// 导出部件的列表 - public static IEnumerable LoadExportPartsFromAssembly(string assemblyName) - { - // 加载指定的程序集 - var assembly = Assembly.Load(assemblyName); - // 创建程序集目录 - var catalog = new AssemblyCatalog(assembly); - // 创建容器并将目录添加到容器中 - var container = new CompositionContainer(catalog); - - // 从容器中获取导出的部件 - var parts = container.GetExportedValues(); - return parts; - } - - /// - /// 从指定文件夹中加载所有导出部件 - /// - /// 导出部件类型 - /// 指定文件夹路径 - /// 搜索选项 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromFolder(string folderPath, SearchOption searchOption = SearchOption.AllDirectories) - { - var catalog = new DirectoryCatalog(folderPath, "*.dll"); - using var container = new CompositionContainer(catalog); - return container.GetExportedValues(); - } - - /// - /// 从指定类型中加载所有导出部件 - /// - /// 导出部件类型 - /// 指定类型 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromType(Type type) - { - var catalog = new TypeCatalog(type); - using var container = new CompositionContainer(catalog); - return container.GetExportedValues(); - } - - /// - /// 加载多个目录中的导出部件 - /// - /// 导出部件类型 - /// 多个目录路径 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromFolders(IEnumerable folderPaths) - { - var catalogs = folderPaths.Select(path => new DirectoryCatalog(path, "*.dll")); - var aggregateCatalog = new AggregateCatalog(catalogs); - using var container = new CompositionContainer(aggregateCatalog); - return container.GetExportedValues(); - } - - /// - /// 从指定容器中获取导入部件 - /// - /// 导入部件的类型 - /// 容器 - /// 导入部件的实例 - public static T GetImportPart(CompositionContainer container) - { - // 获取导入部件的实例 - var part = container.GetExportedValue(); - return part; - } - - /// - /// 从指定容器中获取导入部件的列表 - /// - /// 导入部件的类型 - /// 容器 - /// 导入部件的列表 - public static IEnumerable GetImportParts(CompositionContainer container) - { - // 获取导入部件的列表 - var parts = container.GetExportedValues(); - return parts; - } - } -} diff --git a/EasyTool.Core/ToolCategory/ObjectExtension.cs b/EasyTool.Core/ToolCategory/ObjectExtension.cs index b14b515..8513661 100644 --- a/EasyTool.Core/ToolCategory/ObjectExtension.cs +++ b/EasyTool.Core/ToolCategory/ObjectExtension.cs @@ -1,10 +1,12 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.Json; +using System.Threading.Tasks; using System.Xml.Serialization; namespace EasyTool.Extension @@ -185,6 +187,18 @@ public static T ToOrDefault(this object obj, T defaultValue) return json.FromJson(); } + /// + /// 异步深拷贝对象(使用 JSON 序列化) + /// + public static async Task DeepCloneAsync(this T obj) + { + if (obj == null) + return default; + + var json = await Task.Run(() => obj.ToJson()); + return await Task.Run(() => json.FromJson()); + } + /// /// 浅拷贝对象(使用 MemberwiseClone) /// @@ -347,15 +361,6 @@ public static T Pipe(this T obj, Action action) #region 对象检查 - /// - /// 判断对象是否是指定类型 - /// [Obsolete("请直接使用 obj is T")] - /// - [Obsolete("请直接使用 obj is T", false)] - public static bool Is(this object obj) - { - return obj is T; - } /// /// 判断对象是否实现了指定接口 @@ -405,47 +410,481 @@ public static bool PropertiesEqual(this T obj, T? other) where T : class #region 对象信息 /// - /// 获取对象的类型名称 - /// [Obsolete("请直接使用 obj?.GetType().Name")] + /// 获取指定类型的默认值 + /// + public static object? GetDefault(Type type) + { + return type.IsValueType ? Activator.CreateInstance(type) : null; + } + + /// + /// 判断对象是否是其类型的默认值 + /// + public static bool IsDefaultValue(this object obj) + { + return obj == null || obj.Equals(GetDefault(obj.GetType())); + } + + /// + /// 判断指定类型是否是可空值类型 + /// + public static bool IsNullable(Type type) + { + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } + + /// + /// 获取可空类型的基础类型 /// - [Obsolete("请直接使用 obj?.GetType().Name", false)] - public static string GetTypeName(this object obj) + public static Type GetNullableType(Type type) { - return obj?.GetType().Name ?? "null"; + return Nullable.GetUnderlyingType(type); } /// - /// 获取对象的完整类型名称 - /// [Obsolete("请直接使用 obj?.GetType().FullName")] + /// 获取可空类型或枚举类型的基础类型 /// - [Obsolete("请直接使用 obj?.GetType().FullName", false)] - public static string GetFullTypeName(this object obj) + public static Type GetUnderlyingType(Type type) { - return obj?.GetType().FullName ?? "null"; + if (IsNullable(type)) + { + return GetNullableType(type); + } + + if (type.IsEnum) + { + return Enum.GetUnderlyingType(type); + } + + return type; + } + + /// + /// 判断指定类型是否是简单类型 + /// + public static bool IsSimpleType(Type type) + { + if (type == typeof(string)) + { + return true; + } + + if (type.IsValueType) + { + return true; + } + + return false; + } + + /// + /// 判断指定类型是否是数字类型 + /// + public static bool IsNumericType(Type type) + { + switch (Type.GetTypeCode(type)) + { + case TypeCode.Byte: + case TypeCode.SByte: + case TypeCode.UInt16: + case TypeCode.Int16: + case TypeCode.UInt32: + case TypeCode.Int32: + case TypeCode.UInt64: + case TypeCode.Int64: + case TypeCode.Decimal: + case TypeCode.Double: + case TypeCode.Single: + return true; + default: + return false; + } + } + + /// + /// 判断指定类型是否是布尔类型 + /// + public static bool IsBooleanType(Type type) + { + return type == typeof(bool); + } + + /// + /// 判断指定类型是否是日期时间类型 + /// + public static bool IsDateTimeType(Type type) + { + return type == typeof(DateTime); + } + + /// + /// 判断指定类型是否是集合类型 + /// + public static bool IsEnumerableType(Type type) + { + return typeof(IEnumerable).IsAssignableFrom(type); + } + + /// + /// 获取指定类型的所有派生类型 + /// + public static Type[] GetSubclassesOf(Type baseType) + { + return AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(s => s.GetTypes()) + .Where(p => baseType.IsAssignableFrom(p) && p != baseType) + .ToArray(); } #endregion #region 对象转字符串 + #endregion + + #region 对象转换(静态工具方法) + + /// + /// 将对象转换为指定类型(使用 TypeConverter 和 IConvertible) + /// + public static T ConvertTo(object obj) + { + return (T)ConvertTo(obj, typeof(T)); + } + + /// + /// 将对象转换为指定类型(使用 TypeConverter 和 IConvertible) + /// + public static object? ConvertTo(object obj, Type targetType) + { + if (obj == null) + { + return GetDefault(targetType); + } + + Type sourceType = obj.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + { + return obj; + } + + var converter = System.ComponentModel.TypeDescriptor.GetConverter(targetType); + if (converter != null && converter.CanConvertFrom(sourceType)) + { + return converter.ConvertFrom(obj); + } + + var sourceConverter = System.ComponentModel.TypeDescriptor.GetConverter(sourceType); + if (sourceConverter != null && sourceConverter.CanConvertTo(targetType)) + { + return sourceConverter.ConvertTo(obj, targetType); + } + + if (obj is IConvertible) + { + try + { + return System.Convert.ChangeType(obj, targetType); + } + catch (InvalidCastException) + { + } + } + + try + { + var implicitOp = sourceType.GetMethod("op_Implicit", new[] { sourceType }); + if (implicitOp != null && implicitOp.ReturnType == targetType) + { + return implicitOp.Invoke(null, new[] { obj }); + } + + var explicitOp = sourceType.GetMethod("op_Explicit", new[] { sourceType }); + if (explicitOp != null && explicitOp.ReturnType == targetType) + { + return explicitOp.Invoke(null, new[] { obj }); + } + + var targetImplicitOp = targetType.GetMethod("op_Implicit", new[] { sourceType }); + if (targetImplicitOp != null && targetImplicitOp.ReturnType == targetType) + { + return targetImplicitOp.Invoke(null, new[] { obj }); + } + + var targetExplicitOp = targetType.GetMethod("op_Explicit", new[] { sourceType }); + if (targetExplicitOp != null && targetExplicitOp.ReturnType == targetType) + { + return targetExplicitOp.Invoke(null, new[] { obj }); + } + } + catch (InvalidCastException) + { + } + + throw new InvalidCastException($"无法将类型为 {sourceType.Name} 的对象转换为类型为 {targetType.Name} 的对象"); + } + + #endregion + + #region 对象属性复制 + + /// + /// 将源对象的属性复制到目标对象中 + /// + public static void CopyProperties(object source, object target) + { + Type sourceType = source.GetType(); + Type targetType = target.GetType(); + + foreach (PropertyInfo sourceProperty in sourceType.GetProperties()) + { + if (!sourceProperty.CanRead) + { + continue; + } + + PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name); + + if (targetProperty == null || !targetProperty.CanWrite) + { + continue; + } + + object value = GetPropertyValue(source, sourceProperty.Name); + SetPropertyValue(target, targetProperty.Name, value); + } + } + + /// + /// 对象属性值的加密 + /// + public static void EncryptPropertyValue(object obj, string propertyName, Func encryptFunc) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (encryptFunc == null) + { + throw new ArgumentNullException(nameof(encryptFunc)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null && value is string) + { + string encryptedValue = encryptFunc((string)value); + property.SetValue(obj, encryptedValue); + } + } + + /// + /// 对象属性值的解密 + /// + public static void DecryptPropertyValue(object obj, string propertyName, Func decryptFunc) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (decryptFunc == null) + { + throw new ArgumentNullException(nameof(decryptFunc)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null && value is string) + { + string decryptedValue = decryptFunc((string)value); + property.SetValue(obj, decryptedValue); + } + } + + /// + /// 在对象属性上进行特定的处理 + /// + public static void ProcessPropertyValue(object obj, string propertyName, Action processAction) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (processAction == null) + { + throw new ArgumentNullException(nameof(processAction)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null) + { + processAction(value); + property.SetValue(obj, value); + } + } + + #endregion + + #region 对象比较 + + /// + /// 比较两个对象的差异(属性值或字段值不同) + /// + public static IEnumerable CompareDifferences(object obj1, object obj2) + { + if (obj1 == null && obj2 == null) + { + return Enumerable.Empty(); + } + + if (obj1 == null || obj2 == null) + { + throw new ArgumentNullException("比较对象不能为 null"); + } + + List differences = new List(); + + foreach (PropertyInfo property in obj1.GetType().GetProperties()) + { + object value1 = property.GetValue(obj1); + object value2 = property.GetValue(obj2); + + if (!Equals(value1, value2)) + { + differences.Add($"属性 {property.Name} 的值不同:{value1} -> {value2}"); + } + } + + foreach (FieldInfo field in obj1.GetType().GetFields()) + { + object value1 = field.GetValue(obj1); + object value2 = field.GetValue(obj2); + + if (!Equals(value1, value2)) + { + differences.Add($"字段 {field.Name} 的值不同:{value1} -> {value2}"); + } + } + + return differences; + } + + #endregion + + #region 对象高级转换 + + /// + /// 将对象转换为键值对集合 + /// + public static IEnumerable> ToKeyValuePairs(this object obj) + { + if (obj == null) + { + return Enumerable.Empty>(); + } + + List> pairs = new List>(); + + foreach (PropertyInfo property in obj.GetType().GetProperties()) + { + pairs.Add(new KeyValuePair(property.Name, property.GetValue(obj))); + } + + foreach (FieldInfo field in obj.GetType().GetFields()) + { + pairs.Add(new KeyValuePair(field.Name, field.GetValue(obj))); + } + + return pairs; + } + + /// + /// 将对象转换为动态扩展对象 + /// + public static dynamic? ToDynamic(this object obj) + { + if (obj == null) + { + return null; + } + + IDictionary dictionary = new System.Dynamic.ExpandoObject(); + + foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) + { + if (!propertyInfo.CanRead) + { + continue; + } + + object value = GetPropertyValue(obj, propertyInfo.Name); + dictionary.Add(propertyInfo.Name, value); + } + + foreach (FieldInfo fieldInfo in obj.GetType().GetFields()) + { + object value = GetFieldValue(obj, fieldInfo.Name); + dictionary.Add(fieldInfo.Name, value); + } + + return dictionary; + } + /// - /// 将对象转换为字符串(处理 null) - /// [Obsolete("请直接使用 obj?.ToString() ?? string.Empty")] + /// 获取对象的字段值 /// - [Obsolete("请直接使用 obj?.ToString() ?? string.Empty", false)] - public static string ToStringSafe(this object obj) + public static object? GetFieldValue(this object obj, string fieldName) { - return obj?.ToString() ?? string.Empty; + return obj.GetType().GetField(fieldName)?.GetValue(obj); } /// - /// 将对象转换为字符串(null 时返回默认值) - /// [Obsolete("请直接使用 obj?.ToString() ?? defaultValue")] + /// 设置对象的字段值 /// - [Obsolete("请直接使用 obj?.ToString() ?? defaultValue", false)] - public static string ToStringOrDefault(this object obj, string defaultValue = "") + public static void SetFieldValue(this object obj, string fieldName, object? value) { - return obj?.ToString() ?? defaultValue; + obj.GetType().GetField(fieldName)?.SetValue(obj, value); } #endregion diff --git a/EasyTool.Core/ToolCategory/ObjectUtil.cs b/EasyTool.Core/ToolCategory/ObjectUtil.cs deleted file mode 100644 index 81c5921..0000000 --- a/EasyTool.Core/ToolCategory/ObjectUtil.cs +++ /dev/null @@ -1,1193 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Dynamic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; -using System.Runtime.Serialization.Json; -using System.Text; -using System.Xml.Serialization; - -namespace EasyTool -{ - /// - /// 对象工具类 - /// - public class ObjectUtil - { - - /// - /// 检查对象是否为 null - /// [Obsolete("请直接使用 obj == null")] - /// - [Obsolete("请直接使用 obj == null", false)] - public static bool IsNull(object? obj) - { - return obj == null; - } - - /// - /// 检查对象是否不为 null - /// [Obsolete("请直接使用 obj != null")] - /// - [Obsolete("请直接使用 obj != null", false)] - public static bool IsNotNull(object? obj) - { - return obj != null; - } - - /// - /// 检查对象是否为空(null 或者 空字符串或空白字符) - /// - public static bool IsNullOrEmpty(object? obj) - { - if (IsNull(obj)) - { - return true; - } - - if (obj is string str) - { - return string.IsNullOrWhiteSpace(str); - } - - if (obj is ICollection collection) - { - return collection.Count == 0; - } - - return false; - } - - /// - /// 检查对象是否不为空(非 null 且 非空字符串 或者 非空集合) - /// - public static bool IsNotNullOrEmpty(object? obj) - { - return !IsNullOrEmpty(obj); - } - - /// - /// 检查两个对象是否相等 - /// - public static new bool Equals(object obj1, object obj2) - { - if (IsNull(obj1) && IsNull(obj2)) - { - return true; - } - - if (IsNull(obj1) || IsNull(obj2)) - { - return false; - } - - return obj1.Equals(obj2); - } - - /// - /// 获取对象的类型名称 - /// [Obsolete("请直接使用 obj.GetType().Name")] - /// - [Obsolete("请直接使用 obj.GetType().Name", false)] - public static string GetTypeName(object obj) - { - return obj.GetType().Name; - } - - /// - /// 将对象转换为指定类型 - /// - public static T Convert(object obj) - { - return (T)Convert(obj, typeof(T)); - } - - /// - /// 将对象转换为指定类型 - /// - public static object? Convert(object obj, Type targetType) - { - if (IsNull(obj)) - { - // 处理可空值类型的默认值 - return GetDefault(targetType); - } - - Type sourceType = obj.GetType(); - - // 如果目标类型可以从源类型赋值,直接返回 - if (targetType.IsAssignableFrom(sourceType)) - { - return obj; - } - - // 使用 TypeConverter 进行转换 - var converter = System.ComponentModel.TypeDescriptor.GetConverter(targetType); - if (converter != null && converter.CanConvertFrom(sourceType)) - { - return converter.ConvertFrom(obj); - } - - // 尝试从源类型的 TypeConverter 转换 - var sourceConverter = System.ComponentModel.TypeDescriptor.GetConverter(sourceType); - if (sourceConverter != null && sourceConverter.CanConvertTo(targetType)) - { - return sourceConverter.ConvertTo(obj, targetType); - } - - // 使用 IConvertible 接口转换 - if (obj is IConvertible) - { - try - { - return System.Convert.ChangeType(obj, targetType); - } - catch (InvalidCastException) - { - // 继续尝试其他转换方式 - } - } - - // 尝试使用隐式或显式转换操作符 - try - { - // 查找源类型的隐式转换操作符 - var implicitOp = sourceType.GetMethod("op_Implicit", new[] { sourceType }); - if (implicitOp != null && implicitOp.ReturnType == targetType) - { - return implicitOp.Invoke(null, new[] { obj }); - } - - // 查找源类型的显式转换操作符 - var explicitOp = sourceType.GetMethod("op_Explicit", new[] { sourceType }); - if (explicitOp != null && explicitOp.ReturnType == targetType) - { - return explicitOp.Invoke(null, new[] { obj }); - } - - // 查找目标类型的隐式转换操作符 - var targetImplicitOp = targetType.GetMethod("op_Implicit", new[] { sourceType }); - if (targetImplicitOp != null && targetImplicitOp.ReturnType == targetType) - { - return targetImplicitOp.Invoke(null, new[] { obj }); - } - - // 查找目标类型的显式转换操作符 - var targetExplicitOp = targetType.GetMethod("op_Explicit", new[] { sourceType }); - if (targetExplicitOp != null && targetExplicitOp.ReturnType == targetType) - { - return targetExplicitOp.Invoke(null, new[] { obj }); - } - } - catch (InvalidCastException) - { - // 转换操作符失败,继续抛出异常 - } - - throw new InvalidCastException($"无法将类型为 {sourceType.Name} 的对象转换为类型为 {targetType.Name} 的对象"); - } - - /// - /// 获取对象的属性列表 - /// [Obsolete("请直接使用 obj.GetType().GetProperties()")] - /// - [Obsolete("请直接使用 obj.GetType().GetProperties()", false)] - public static IEnumerable GetProperties(object obj) - { - return obj.GetType().GetProperties(); - } - - /// - /// 获取对象的属性值 - /// - public static object? GetPropertyValue(object obj, string propertyName) - { - return obj.GetType().GetProperty(propertyName)?.GetValue(obj); - } - - /// - /// 设置对象的属性值 - /// - public static void SetPropertyValue(object obj, string propertyName, object? value) - { - obj.GetType().GetProperty(propertyName)?.SetValue(obj, value); - } - - /// - /// 获取对象的字段列表 - /// [Obsolete("请直接使用 obj.GetType().GetFields()")] - /// - [Obsolete("请直接使用 obj.GetType().GetFields()", false)] - public static IEnumerable GetFields(object obj) - { - return obj.GetType().GetFields(); - } - - /// - /// 获取对象的字段值 - /// - public static object? GetFieldValue(object obj, string fieldName) - { - return obj.GetType().GetField(fieldName)?.GetValue(obj); - } - - /// - /// 设置对象的字段值 - /// - public static void SetFieldValue(object obj, string fieldName, object? value) - { - obj.GetType().GetField(fieldName)?.SetValue(obj, value); - } - - /// - /// 获取对象的方法列表 - /// [Obsolete("请直接使用 obj.GetType().GetMethods()")] - /// - [Obsolete("请直接使用 obj.GetType().GetMethods()", false)] - public static IEnumerable GetMethods(object obj) - { - return obj.GetType().GetMethods(); - } - - /// - /// 判断对象是否实现了指定接口 - /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(obj.GetType())")] - /// - [Obsolete("请直接使用 interfaceType.IsAssignableFrom(obj.GetType())", false)] - public static bool ImplementsInterface(object obj, Type interfaceType) - { - return interfaceType.IsAssignableFrom(obj.GetType()); - } - - /// - /// 判断对象是否为指定类型的实例 - /// [Obsolete("请直接使用 targetType.IsInstanceOfType(obj)")] - /// - [Obsolete("请直接使用 targetType.IsInstanceOfType(obj)", false)] - public static bool IsInstanceOfType(object obj, Type targetType) - { - return targetType.IsInstanceOfType(obj); - } - - /// - /// 对象属性或字段值的加密 - /// - public static void EncryptPropertyValue(object obj, string propertyName, Func encryptFunc) - { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (encryptFunc == null) - { - throw new ArgumentNullException(nameof(encryptFunc)); - } - - PropertyInfo property = obj.GetType().GetProperty(propertyName); - - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null && value is string) - { - string encryptedValue = encryptFunc((string)value); - property.SetValue(obj, encryptedValue); - } - } - - /// - /// 对象属性或字段值的解密 - /// - public static void DecryptPropertyValue(object obj, string propertyName, Func decryptFunc) - { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (decryptFunc == null) - { - throw new ArgumentNullException(nameof(decryptFunc)); - } - - PropertyInfo property = obj.GetType().GetProperty(propertyName); - - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null && value is string) - { - string decryptedValue = decryptFunc((string)value); - property.SetValue(obj, decryptedValue); - } - } - - /// - /// 在对象属性或字段上进行特定的处理 - /// - public static void ProcessPropertyValue(object obj, string propertyName, Action processAction) - { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (processAction == null) - { - throw new ArgumentNullException(nameof(processAction)); - } - PropertyInfo property = obj.GetType().GetProperty(propertyName); - - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null) - { - processAction(value); - property.SetValue(obj, value); - } - } - - /// - /// 将对象序列化为 JSON 字符串 - /// - public static string? ToJson(object obj) - { - if (IsNull(obj)) - { - return null; - } - - using (MemoryStream stream = new MemoryStream()) - { - DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); - serializer.WriteObject(stream, obj); - return Encoding.UTF8.GetString(stream.ToArray()); - } - } - - /// - /// 将 JSON 字符串反序列化为对象 - /// - public static T FromJson(string json) - { - if (string.IsNullOrEmpty(json)) - { - return default(T); - } - - using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) - { - DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); - return (T)serializer.ReadObject(stream); - } - } - - /// - /// 将对象序列化为 XML 字符串 - /// - public static string? ToXml(object obj) - { - if (IsNull(obj)) - { - return null; - } - - XmlSerializer serializer = new XmlSerializer(obj.GetType()); - using (MemoryStream stream = new MemoryStream()) - { - serializer.Serialize(stream, obj); - return Encoding.UTF8.GetString(stream.ToArray()); - } - } - - /// - /// 将 XML 字符串反序列化为对象 - /// - public static T FromXml(string xml) - { - if (string.IsNullOrEmpty(xml)) - { - return default(T); - } - - XmlSerializer serializer = new XmlSerializer(typeof(T)); - using (StringReader reader = new StringReader(xml)) - { - return (T)serializer.Deserialize(reader); - } - } - - /// - /// 将对象转换为字典 - /// - public static Dictionary? ToDictionary(object obj) - { - if (IsNull(obj)) - { - return null; - } - - Dictionary dictionary = new Dictionary(); - - foreach (PropertyInfo property in GetProperties(obj)) - { - dictionary[property.Name] = property.GetValue(obj); - } - - foreach (FieldInfo field in GetFields(obj)) - { - dictionary[field.Name] = field.GetValue(obj); - } - - return dictionary; - } - - /// - /// 将字典转换为对象 - /// - public static T FromDictionary(Dictionary dictionary) where T : new() - { - if (dictionary == null) - { - return default(T); - } - - T obj = new T(); - - foreach (PropertyInfo property in GetProperties(obj)) - { - if (dictionary.TryGetValue(property.Name, out object value)) - { - property.SetValue(obj, Convert(value, property.PropertyType)); - } - } - - foreach (FieldInfo field in GetFields(obj)) - { - if (dictionary.TryGetValue(field.Name, out object value)) - { - field.SetValue(obj, Convert(value, field.FieldType)); - } - } - - return obj; - } - - /// - /// 比较两个对象的差异(属性值或字段值不同) - /// - public static IEnumerable Compare(object obj1, object obj2) - { - if (IsNull(obj1) && IsNull(obj2)) - { - return Enumerable.Empty(); - } - - if (IsNull(obj1) || IsNull(obj2)) - { - throw new ArgumentNullException("比较对象不能为 null"); - } - - List differences = new List(); - - foreach (PropertyInfo property in GetProperties(obj1)) - { - object value1 = property.GetValue(obj1); - object value2 = property.GetValue(obj2); - - if (!Equals(value1, value2)) - { - differences.Add($"属性 {property.Name} 的值不同:{value1} -> {value2}"); - } - } - - foreach (FieldInfo field in GetFields(obj1)) - { - object value1 = field.GetValue(obj1); - object value2 = field.GetValue(obj2); - - if (!Equals(value1, value2)) - { - differences.Add($"字段 {field.Name} 的值不同:{value1} -> {value2}"); - } - } - - return differences; - } - - /// - /// 获取对象的哈希码 - /// [Obsolete("请直接使用 obj?.GetHashCode() ?? 0")] - /// - [Obsolete("请直接使用 obj?.GetHashCode() ?? 0", false)] - public static int GetHashCode(object obj) - { - if (IsNull(obj)) - { - return 0; - } - - return obj.GetHashCode(); - } - - /// - /// 深拷贝对象 - /// - public static T? DeepClone(T obj) - { - if (IsNull(obj)) - { - return default(T); - } - - DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); - - using (MemoryStream stream = new MemoryStream()) - { - serializer.WriteObject(stream, obj); - stream.Position = 0; - return (T)serializer.ReadObject(stream); - } - } - - /// - /// 判断对象是否为值类型 - /// [Obsolete("请直接使用 obj?.GetType().IsValueType ?? false")] - /// - [Obsolete("请直接使用 obj?.GetType().IsValueType ?? false", false)] - public static bool IsValueType(object obj) - { - if (IsNull(obj)) - { - return false; - } - - return obj.GetType().IsValueType; - } - - /// - /// 将对象转换为键值对集合 - /// - public static IEnumerable> ToKeyValuePairs(object obj) - { - if (IsNull(obj)) - { - return Enumerable.Empty>(); - } - - List> pairs = new List>(); - - foreach (PropertyInfo property in GetProperties(obj)) - { - pairs.Add(new KeyValuePair(property.Name, property.GetValue(obj))); - } - - foreach (FieldInfo field in GetFields(obj)) - { - pairs.Add(new KeyValuePair(field.Name, field.GetValue(obj))); - } - - return pairs; - } - - /// - /// 深度复制对象 - /// - public static object? DeepCopy(object obj) - { - if (obj == null) - { - return null; - } - - Type type = obj.GetType(); - - if (IsSimpleType(type)) - { - return obj; - } - - if (IsEnumerableType(type)) - { - Type elementType = type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault(); - - if (elementType == null || IsSimpleType(elementType)) - { - return obj; - } - - IList list = (IList)Activator.CreateInstance(type); - - foreach (object item in (IEnumerable)obj) - { - list.Add(DeepCopy(item)); - } - - return list; - } - - object clone = Activator.CreateInstance(type); - - foreach (PropertyInfo propertyInfo in GetProperties(type)) - { - if (!propertyInfo.CanRead || !propertyInfo.CanWrite) - { - continue; - } - - object value = GetPropertyValue(obj, propertyInfo.Name); - - if (value == null) - { - continue; - } - - if (IsSimpleType(propertyInfo.PropertyType)) - { - SetPropertyValue(clone, propertyInfo.Name, value); - } - else if (IsEnumerableType(propertyInfo.PropertyType)) - { - object enumerable = DeepCopy(value); - SetPropertyValue(clone, propertyInfo.Name, enumerable); - } - else - { - object childClone = DeepCopy(value); - SetPropertyValue(clone, propertyInfo.Name, childClone); - } - } - - foreach (FieldInfo fieldInfo in GetFields(type)) - { - object value = GetFieldValue(obj, fieldInfo.Name); - - if (value == null) - { - continue; - } - - if (IsSimpleType(fieldInfo.FieldType)) - { - SetFieldValue(clone, fieldInfo.Name, value); - } - else if (IsEnumerableType(fieldInfo.FieldType)) - { - object enumerable = DeepCopy(value); - SetFieldValue(clone, fieldInfo.Name, enumerable); - } - else - { - object childClone = DeepCopy(value); - SetFieldValue(clone, fieldInfo.Name, childClone); - } - } - - return clone; - } - - /// - /// 将源对象的属性复制到目标对象中 - /// - public static void CopyProperties(object source, object target) - { - Type sourceType = source.GetType(); - Type targetType = target.GetType(); - - foreach (PropertyInfo sourceProperty in GetProperties(sourceType)) - { - if (!sourceProperty.CanRead) - { - continue; - } - - PropertyInfo targetProperty = GetProperty(targetType, sourceProperty.Name); - - if (targetProperty == null || !targetProperty.CanWrite) - { - continue; - } - - object value = GetPropertyValue(source, sourceProperty.Name); - SetPropertyValue(target, targetProperty.Name, value); - } - } - - /// - /// 获取指定类型的 Type 对象 - /// [Obsolete("请直接使用 Type.GetType(typeName)")] - /// - [Obsolete("请直接使用 Type.GetType(typeName)", false)] - public static Type GetType(string typeName) - { - return Type.GetType(typeName); - } - - /// - /// 获取对象的 Type 对象 - /// [Obsolete("请直接使用 obj.GetType()")] - /// - [Obsolete("请直接使用 obj.GetType()", false)] - public static Type GetType(object obj) - { - return obj.GetType(); - } - - /// - /// 获取类型的所有成员信息,包括字段、属性、方法和事件等 - /// [Obsolete("请直接使用 type.GetMembers()")] - /// - [Obsolete("请直接使用 type.GetMembers()", false)] - public static MemberInfo[] GetMembers(Type type) - { - return type.GetMembers(); - } - - /// - /// 获取类型的所有属性信息 - /// [Obsolete("请直接使用 type.GetProperties()")] - /// - [Obsolete("请直接使用 type.GetProperties()", false)] - public static PropertyInfo[] GetProperties(Type type) - { - return type.GetProperties(); - } - - /// - /// 获取类型的所有字段信息 - /// [Obsolete("请直接使用 type.GetFields()")] - /// - [Obsolete("请直接使用 type.GetFields()", false)] - public static FieldInfo[] GetFields(Type type) - { - return type.GetFields(); - } - - /// - /// 获取指定名称的属性信息 - /// [Obsolete("请直接使用 type.GetProperty(propertyName)")] - /// - [Obsolete("请直接使用 type.GetProperty(propertyName)", false)] - public static PropertyInfo GetProperty(Type type, string propertyName) - { - return type.GetProperty(propertyName); - } - - /// - /// 获取指定名称的属性信息 - /// [Obsolete("请直接使用 obj.GetType().GetProperty(propertyName)")] - /// - [Obsolete("请直接使用 obj.GetType().GetProperty(propertyName)", false)] - public static PropertyInfo GetProperty(object obj, string propertyName) - { - return obj.GetType().GetProperty(propertyName); - } - - /// - /// 获取指定名称的字段信息 - /// [Obsolete("请直接使用 type.GetField(fieldName)")] - /// - [Obsolete("请直接使用 type.GetField(fieldName)", false)] - public static FieldInfo GetField(Type type, string fieldName) - { - return type.GetField(fieldName); - } - - /// - /// 获取指定名称的字段信息 - /// [Obsolete("请直接使用 obj.GetType().GetField(fieldName)")] - /// - [Obsolete("请直接使用 obj.GetType().GetField(fieldName)", false)] - public static FieldInfo GetField(object obj, string fieldName) - { - return obj.GetType().GetField(fieldName); - } - - /// - /// 获取指定名称的方法信息 - /// [Obsolete("请直接使用 type.GetMethod(methodName)")] - /// - [Obsolete("请直接使用 type.GetMethod(methodName)", false)] - public static MethodInfo GetMethod(Type type, string methodName) - { - return type.GetMethod(methodName); - } - - /// - /// 获取指定名称的方法信息 - /// [Obsolete("请直接使用 obj.GetType().GetMethod(methodName)")] - /// - [Obsolete("请直接使用 obj.GetType().GetMethod(methodName)", false)] - public static MethodInfo GetMethod(object obj, string methodName) - { - return obj.GetType().GetMethod(methodName); - } - - /// - /// 获取指定名称和参数类型的方法信息 - /// [Obsolete("请直接使用 type.GetMethod(methodName, parameterTypes)")] - /// - [Obsolete("请直接使用 type.GetMethod(methodName, parameterTypes)", false)] - public static MethodInfo GetMethod(Type type, string methodName, Type[] parameterTypes) - { - return type.GetMethod(methodName, parameterTypes); - } - - /// - /// 获取指定名称和参数类型的方法信息 - /// [Obsolete("请直接使用 obj.GetType().GetMethod(methodName, parameterTypes)")] - /// - [Obsolete("请直接使用 obj.GetType().GetMethod(methodName, parameterTypes)", false)] - public static MethodInfo GetMethod(object obj, string methodName, Type[] parameterTypes) - { - return obj.GetType().GetMethod(methodName, parameterTypes); - } - - /// - /// 调用对象的指定方法 - /// - public static object InvokeMethod(object obj, string methodName, object[] parameters) - { - Type type = obj.GetType(); - MethodInfo methodInfo = GetMethod(type, methodName); - return methodInfo.Invoke(obj, parameters); - } - - /// - /// 调用对象的指定方法 - /// - public static object InvokeMethod(object obj, string methodName, Type[] parameterTypes, object[] parameters) - { - Type type = obj.GetType(); - MethodInfo methodInfo = GetMethod(type, methodName, parameterTypes); - return methodInfo.Invoke(obj, parameters); - } - - /// - /// 创建指定类型的实例 - /// [Obsolete("请直接使用 Activator.CreateInstance(type, constructorParameters)")] - /// - [Obsolete("请直接使用 Activator.CreateInstance(type, constructorParameters)", false)] - public static object CreateInstance(Type type, object[] constructorParameters) - { - return Activator.CreateInstance(type, constructorParameters); - } - - - /// - /// 判断指定类型是否派生自指定的基类或接口 - /// [Obsolete("请直接使用 type.IsSubclassOf(baseType)")] - /// - [Obsolete("请直接使用 type.IsSubclassOf(baseType)", false)] - public static bool IsSubclassOf(Type type, Type baseType) - { - return type.IsSubclassOf(baseType); - } - - /// - /// 获取指定类型的所有派生类型 - /// - public static Type[] GetSubclassesOf(Type baseType) - { - return AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(s => s.GetTypes()) - .Where(p => baseType.IsAssignableFrom(p) && p != baseType) - .ToArray(); - } - - /// - /// 获取指定类型实现的所有接口类型 - /// [Obsolete("请直接使用 type.GetInterfaces()")] - /// - [Obsolete("请直接使用 type.GetInterfaces()", false)] - public static Type[] GetInterfaces(Type type) - { - return type.GetInterfaces(); - } - - /// - /// 获取指定类型的程序集限定名 - /// [Obsolete("请直接使用 type.AssemblyQualifiedName")] - /// - [Obsolete("请直接使用 type.AssemblyQualifiedName", false)] - public static string GetAssemblyQualifiedName(Type type) - { - return type.AssemblyQualifiedName; - } - - /// - /// 获取指定类型的默认值 - /// - public static object? GetDefault(Type type) - { - return type.IsValueType ? Activator.CreateInstance(type) : null; - } - - /// - /// 判断对象是否是其类型的默认值 - /// - public static bool IsDefaultValue(object obj) - { - return obj == null || obj.Equals(GetDefault(obj.GetType())); - } - - /// - /// 判断指定类型是否是可空类型 - /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) != null")] - /// - [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) != null", false)] - public static bool IsNullable(Type type) - { - return Nullable.GetUnderlyingType(type) != null; - } - - /// - /// 获取可空类型的基础类型 - /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type")] - /// - [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type", false)] - public static Type GetNullableType(Type type) - { - return Nullable.GetUnderlyingType(type) ?? type; - } - - /// - /// 获取可空类型或枚举类型的基础类型 - /// - public static Type GetUnderlyingType(Type type) - { - if (IsNullable(type)) - { - return GetNullableType(type); - } - - if (IsEnumType(type)) - { - return Enum.GetUnderlyingType(type); - } - - return type; - } - - /// - /// 判断指定类型是否是简单类型 - /// - public static bool IsSimpleType(Type type) - { - if (type == typeof(string)) - { - return true; - } - - if (type.IsValueType) - { - return true; - } - - return false; - } - - /// - /// 判断指定类型是否是数字类型 - /// - public static bool IsNumericType(Type type) - { - switch (Type.GetTypeCode(type)) - { - case TypeCode.Byte: - case TypeCode.SByte: - case TypeCode.UInt16: - case TypeCode.Int16: - case TypeCode.UInt32: - case TypeCode.Int32: - case TypeCode.UInt64: - case TypeCode.Int64: - case TypeCode.Decimal: - case TypeCode.Double: - case TypeCode.Single: - return true; - default: - return false; - } - } - - /// - /// 判断指定类型是否是布尔类型 - /// - public static bool IsBooleanType(Type type) - { - return type == typeof(bool); - } - - /// - /// 判断指定类型是否是日期时间类型 - /// - public static bool IsDateTimeType(Type type) - { - return type == typeof(DateTime); - } - - /// - /// 判断指定类型是否是枚举类型 - /// [Obsolete("请直接使用 type.IsEnum")] - /// - [Obsolete("请直接使用 type.IsEnum", false)] - public static bool IsEnumType(Type type) - { - return type.IsEnum; - } - - /// - /// 判断指定类型是否是集合类型 - /// - public static bool IsEnumerableType(Type type) - { - return typeof(IEnumerable).IsAssignableFrom(type); - } - - /// - /// 将对象转换为动态扩展对象 - /// - public static dynamic? ToDynamic(object obj) - { - if (obj == null) - { - return null; - } - - IDictionary dictionary = new ExpandoObject(); - - foreach (PropertyInfo propertyInfo in GetProperties(obj.GetType())) - { - if (!propertyInfo.CanRead) - { - continue; - } - - object value = GetPropertyValue(obj, propertyInfo.Name); - dictionary.Add(propertyInfo.Name, value); - } - - foreach (FieldInfo fieldInfo in GetFields(obj.GetType())) - { - object value = GetFieldValue(obj, fieldInfo.Name); - dictionary.Add(fieldInfo.Name, value); - } - - return dictionary; - } - -#if NET6_0_OR_GREATER - - /// - /// 将对象序列化为 JSON 字符串 - /// - public static string SerializeToJson(object obj) - { - return System.Text.Json.JsonSerializer.Serialize(obj); - } - - /// - /// 将 JSON 字符串反序列化为指定类型的对象 - /// - public static T? DeserializeFromJson(string json) - { - return System.Text.Json.JsonSerializer.Deserialize(json); - } - -#endif - - /// - /// 将对象序列化为 XML 字符串 - /// - public static string SerializeToXml(object obj) - { - XmlSerializer serializer = new XmlSerializer(obj.GetType()); - - using (StringWriter writer = new StringWriter()) - { - serializer.Serialize(writer, obj); - return writer.ToString(); - } - } - - /// - /// 将 XML 字符串反序列化为指定类型的对象 - /// - public static object? DeserializeFromXml(string xml, Type type) - { - XmlSerializer serializer = new XmlSerializer(type); - - using (StringReader reader = new StringReader(xml)) - { - return serializer.Deserialize(reader); - } - } - - /// - /// 将对象序列化为二进制数据 - /// - [Obsolete("BinaryFormatter is obsolete and unsafe. Use SerializeToJson or SerializeToXml instead.")] - public static byte[] SerializeToBinary(object obj) - { -#pragma warning disable SYSLIB0011 // 类型或成员已过时 - BinaryFormatter formatter = new BinaryFormatter(); - - using (MemoryStream stream = new MemoryStream()) - { - formatter.Serialize(stream, obj); - return stream.ToArray(); - } -#pragma warning restore SYSLIB0011 // 类型或成员已过时 - } - - /// - /// 将二进制数据反序列化为指定类型的对象 - /// - [Obsolete("BinaryFormatter is obsolete and unsafe. Use DeserializeFromJson or DeserializeFromXml instead.")] - public static object DeserializeFromBinary(byte[] data, Type type) - { -#pragma warning disable SYSLIB0011 // 类型或成员已过时 - BinaryFormatter formatter = new BinaryFormatter(); - - using (MemoryStream stream = new MemoryStream(data)) - { - return formatter.Deserialize(stream); - } -#pragma warning restore SYSLIB0011 // 类型或成员已过时 - } - } -} diff --git a/EasyTool.Core/ToolCategory/PageUtil.cs b/EasyTool.Core/ToolCategory/PageUtil.cs index e44b300..d1334eb 100644 --- a/EasyTool.Core/ToolCategory/PageUtil.cs +++ b/EasyTool.Core/ToolCategory/PageUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -193,41 +193,56 @@ public void SetOrderField(Func orderField, bool isAsc) /// 分页HTML代码 public string GetPaginationHtml(string urlFormat, string currentPageClass = "current", int range = 5) { - string html = ""; + var sb = new StringBuilder(); if (totalPage <= 1) { - return html; + return sb.ToString(); } + int startPage = Math.Max(1, currentPage - range); int endPage = Math.Min(totalPage, currentPage + range); if (startPage > 1) { - html += "1"; + sb.Append("1"); if (startPage > 2) { - html += "..."; + sb.Append("..."); } } for (int i = startPage; i <= endPage; i++) { if (i == currentPage) { - html += "" + i.ToString() + ""; + sb.Append(""); + sb.Append(i.ToString()); + sb.Append(""); } else { - html += "" + i.ToString() + ""; + sb.Append(""); + sb.Append(i.ToString()); + sb.Append(""); } } if (endPage < totalPage) { if (endPage < totalPage - 1) { - html += "..."; + sb.Append("..."); } - html += "" + totalPage.ToString() + ""; + sb.Append(""); + sb.Append(totalPage.ToString()); + sb.Append(""); } - return html; + return sb.ToString(); } } } diff --git a/EasyTool.Core/ToolCategory/ProcessUtil.cs b/EasyTool.Core/ToolCategory/ProcessUtil.cs deleted file mode 100644 index 63a9d8b..0000000 --- a/EasyTool.Core/ToolCategory/ProcessUtil.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; - -namespace EasyTool -{ - /// - /// 进程工具类 - /// - public class ProcessUtil - { - /// - /// 通过进程名称获取进程 - /// - /// 进程名称 - /// 进程 - public static Process GetProcessByName(string processName) - { - // 获取当前运行的所有进程 - var processes = Process.GetProcessesByName(processName); - if (processes.Length > 0) - { - return processes[0]; - } - return null; - } - - /// - /// 获取进程的所有线程 - /// [Obsolete("请直接使用 process.Threads")] - /// - /// 进程 - /// 线程集合 - [Obsolete("请直接使用 process.Threads", false)] - public static ProcessThreadCollection GetProcessThreads(Process process) - { - return process.Threads; - } - - /// - /// 获取进程的主窗口句柄 - /// [Obsolete("请直接使用 process.MainWindowHandle")] - /// - /// 进程 - /// 窗口句柄 - [Obsolete("请直接使用 process.MainWindowHandle", false)] - public static IntPtr GetMainWindowHandle(Process process) - { - return process.MainWindowHandle; - } - - /// - /// 获取进程的主窗口标题 - /// [Obsolete("请直接使用 process.MainWindowTitle")] - /// - /// 进程 - /// 窗口标题 - [Obsolete("请直接使用 process.MainWindowTitle", false)] - public static string GetMainWindowTitle(Process process) - { - return process.MainWindowTitle; - } - - /// - /// 获取进程的所有模块 - /// [Obsolete("请直接使用 process.Modules")] - /// - /// 进程 - /// 模块集合 - [Obsolete("请直接使用 process.Modules", false)] - public static ProcessModuleCollection GetProcessModules(Process process) - { - return process.Modules; - } - - /// - /// 关闭进程 - /// [Obsolete("请直接使用 process.Kill()")] - /// - /// 进程 - [Obsolete("请直接使用 process.Kill()", false)] - public static void KillProcess(Process process) - { - process.Kill(); - } - - /// - /// 关闭进程并等待结束 - /// - /// 进程 - public static void KillProcessAndWait(Process process) - { - process.Kill(); - process.WaitForExit(); - } - - /// - /// 启动新进程 - /// [Obsolete("请直接使用 Process.Start(fileName)")] - /// - /// 文件名 - /// 新进程 - [Obsolete("请直接使用 Process.Start(fileName)", false)] - public static Process StartProcess(string fileName) - { - return Process.Start(fileName); - } - - /// - /// 启动新进程并等待结束 - /// - /// 文件名 - public static void StartProcessAndWait(string fileName) - { - var process = Process.Start(fileName); - process.WaitForExit(); - } - - /// - /// 判断进程是否存在 - /// - /// 进程名称 - /// 是否存在 - public static bool IsProcessExists(string processName) - { - return Process.GetProcessesByName(processName).Length > 0; - } - - /// - /// 获取进程使用的内存大小 - /// [Obsolete("请直接使用 process.WorkingSet64")] - /// - /// 进程 - /// 内存大小(字节) - [Obsolete("请直接使用 process.WorkingSet64", false)] - public static long GetProcessMemorySize(Process process) - { - return process.WorkingSet64; - } - - /// - /// 暂停进程 - /// - /// 进程 - public static void SuspendProcess(Process process) - { - foreach (ProcessThread thread in process.Threads) - { - IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); - if (pOpenThread == IntPtr.Zero) - { - break; - } - SuspendThread(pOpenThread); - CloseHandle(pOpenThread); - } - } - - /// - /// 恢复进程 - /// - /// 进程 - public static void ResumeProcess(Process process) - { - foreach (ProcessThread thread in process.Threads) - { - IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); - if (pOpenThread == IntPtr.Zero) - { - break; - } - ResumeThread(pOpenThread); - CloseHandle(pOpenThread); - } - } - - [DllImport("kernel32.dll")] - static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId); - [DllImport("kernel32.dll")] - static extern uint SuspendThread(IntPtr hThread); - [DllImport("kernel32.dll")] - static extern int ResumeThread(IntPtr hThread); - [DllImport("kernel32.dll")] - static extern IntPtr CloseHandle(IntPtr hObject); - [Flags] - public enum ThreadAccess : int - { - TERMINATE = (0x0001), - SUSPEND_RESUME = (0x0002), - GET_CONTEXT = (0x0008), - SET_CONTEXT = (0x0010), - SET_INFORMATION = (0x0020), - QUERY_INFORMATION = (0x0040), - SET_THREAD_TOKEN = (0x0080), - IMPERSONATE = (0x0100), - DIRECT_IMPERSONATION = (0x0200) - } - - } -} diff --git a/EasyTool.Core/ToolCategory/ReflectUtil.cs b/EasyTool.Core/ToolCategory/ReflectUtil.cs index 2226924..f22b1dd 100644 --- a/EasyTool.Core/ToolCategory/ReflectUtil.cs +++ b/EasyTool.Core/ToolCategory/ReflectUtil.cs @@ -1,122 +1,16 @@ -using System; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; namespace EasyTool { /// - /// 反射工具类 + /// 反射工具类,提供类型、属性、字段、方法的反射操作 /// - public class ReflectUtil + public static class ReflectUtil { - /// - /// 根据类型名称获取Type对象 - /// [Obsolete("请直接使用 Type.GetType(typeName)")] - /// - /// 类型名称 - /// Type对象 - [Obsolete("请直接使用 Type.GetType(typeName)", false)] - public static Type GetType(string typeName) - { - return Type.GetType(typeName); - } - - /// - /// 获取指定程序集中的所有类型 - /// [Obsolete("请直接使用 assembly.GetTypes()")] - /// - /// 程序集 - /// 类型数组 - [Obsolete("请直接使用 assembly.GetTypes()", false)] - public static Type[] GetTypes(Assembly assembly) - { - return assembly.GetTypes(); - } - - /// - /// 获取指定类型所在的程序集 - /// [Obsolete("请直接使用 type.Assembly")] - /// - /// 类型 - /// 程序集 - [Obsolete("请直接使用 type.Assembly", false)] - public static Assembly GetAssembly(Type type) - { - return type.Assembly; - } - - /// - /// 获取指定类型的指定类型的特性 - /// [Obsolete("请直接使用 type.GetCustomAttribute()")] - /// - /// 特性类型 - /// 类型 - /// 特性对象 - [Obsolete("请直接使用 type.GetCustomAttribute()", false)] - public static T GetAttribute(Type type) where T : Attribute - { - return type.GetCustomAttribute(); - } - - /// - /// 获取指定类型的指定类型的特性数组 - /// - /// 特性类型 - /// 类型 - /// 特性数组 - public static T[] GetAttributes(Type type) where T : Attribute - { - return type.GetCustomAttributes().ToArray(); - } - - /// - /// 获取指定类型的默认值 - /// [Obsolete("请直接使用 type.IsValueType ? Activator.CreateInstance(type) : null")] - /// - /// 类型 - /// 默认值 - [Obsolete("请直接使用 type.IsValueType ? Activator.CreateInstance(type) : null", false)] - public static object GetDefaultValue(Type type) - { - return type.IsValueType ? Activator.CreateInstance(type) : null; - } - - /// - /// 获取类型的基类 - /// [Obsolete("请直接使用 type.BaseType")] - /// - /// 类型 - /// 基类 - [Obsolete("请直接使用 type.BaseType", false)] - public static Type GetBaseType(Type type) - { - return type.BaseType; - } - - /// - /// 判断类型是否实现了某个接口 - /// [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)")] - /// - /// 类型 - /// 接口类型 - /// 是否实现 - [Obsolete("请直接使用 interfaceType.IsAssignableFrom(type)", false)] - public static bool HasInterface(Type type, Type interfaceType) - { - return interfaceType.IsAssignableFrom(type); - } - - /// - /// 获取方法的参数信息 - /// [Obsolete("请直接使用 method.GetParameters()")] - /// - /// 方法 - /// 参数信息数组 - [Obsolete("请直接使用 method.GetParameters()", false)] - public static ParameterInfo[] GetParameters(MethodInfo method) - { - return method.GetParameters(); - } + #region 类型成员获取 /// /// 获取类型的所有构造函数 @@ -168,18 +62,6 @@ public static EventInfo[] GetEvents(Type type) return type.GetEvents(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } - /// - /// 获取类型的所有接口 - /// [Obsolete("请直接使用 type.GetInterfaces()")] - /// - /// 类型 - /// 接口数组 - [Obsolete("请直接使用 type.GetInterfaces()", false)] - public static Type[] GetInterfaces(Type type) - { - return type.GetInterfaces(); - } - /// /// 获取类型的所有属性名 /// @@ -227,9 +109,72 @@ public static string[] GetEventNames(Type type) /// 接口名数组 public static string[] GetInterfaceNames(Type type) { - return GetInterfaces(type).Select(i => i.Name).ToArray(); + return type.GetInterfaces().Select(i => i.Name).ToArray(); } + #endregion + + #region 类型特性 + + /// + /// 获取指定类型的指定类型的特性数组 + /// + /// 特性类型 + /// 类型 + /// 特性数组 + public static T[] GetAttributes(Type type) where T : Attribute + { + return type.GetCustomAttributes().ToArray(); + } + + /// + /// 判断类型是否实现了指定的接口 + /// + /// 要判断的类型 + /// 要判断的接口类型 + /// 是否实现了指定的接口 + public static bool ImplementsInterface() + { + return typeof(T).GetInterfaces().Any(i => i == typeof(TInterface)); + } + + /// + /// 获取类的继承层次结构 + /// + /// 要获取继承层次结构的类 + /// 类的继承层次结构 + public static Type[] GetClassHierarchy(Type type) + { + Type[] hierarchy = new Type[0]; + Type currentType = type; + while (currentType != null) + { + Array.Resize(ref hierarchy, hierarchy.Length + 1); + hierarchy[hierarchy.Length - 1] = currentType; + currentType = currentType.BaseType; + } + return hierarchy; + } + + /// + /// 获取枚举类型的所有值 + /// + /// 枚举类型 + /// 枚举类型的所有值 + public static IEnumerable GetEnumValues() + { + if (!typeof(T).IsEnum) + { + throw new ArgumentException("Type is not an enum type"); + } + + return Enum.GetValues(typeof(T)).Cast(); + } + + #endregion + + #region 实例创建 + /// /// 创建类型的实例 /// @@ -238,8 +183,9 @@ public static string[] GetInterfaceNames(Type type) /// 实例 public static object CreateInstance(Type type, params object[] args) { + Type[] parameterTypes = GetParameterTypes(args); ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, - null, args.Select(a => a.GetType()).ToArray(), null); + null, parameterTypes, null); if (constructor == null) { throw new ArgumentException($"Type {type} does not have a constructor with specified arguments"); @@ -247,6 +193,36 @@ public static object CreateInstance(Type type, params object[] args) return constructor.Invoke(args); } + /// + /// 获取构造函数参数类型的数组 + /// + /// 要获取参数类型的参数数组 + /// 参数类型的数组 + private static Type[] GetParameterTypes(object[] parameters) + { + if (parameters == null) + { + return Type.EmptyTypes; + } + Type[] parameterTypes = new Type[parameters.Length]; + for (int i = 0; i < parameters.Length; i++) + { + if (parameters[i] == null) + { + parameterTypes[i] = typeof(object); + } + else + { + parameterTypes[i] = parameters[i].GetType(); + } + } + return parameterTypes; + } + + #endregion + + #region 方法调用 + /// /// 调用泛型方法 /// @@ -261,5 +237,86 @@ public static object InvokeGenericMethod(object obj, string methodName, Type gen MethodInfo genericMethod = method.MakeGenericMethod(genericType); return genericMethod.Invoke(obj, args); } + + /// + /// 动态调用类的实例方法 + /// + /// 要调用实例方法的类实例 + /// 要调用的实例方法的名称 + /// 要传递给实例方法的参数 + /// 实例方法的返回值 + public static object InvokeMethod(object instance, string methodName, params object[] arguments) + { + Type type = instance.GetType(); + MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); + return method.Invoke(instance, arguments); + } + + /// + /// 动态调用类的静态方法 + /// + /// 要调用静态方法的类 + /// 要调用的静态方法的名称 + /// 要传递给静态方法的参数 + /// 静态方法的返回值 + public static object InvokeStaticMethod(Type type, string methodName, params object[] arguments) + { + MethodInfo method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public); + return method.Invoke(null, arguments); + } + + #endregion + + #region 静态成员操作 + + /// + /// 获取类的静态属性的值 + /// + /// 要获取静态属性的类 + /// 要获取的静态属性的名称 + /// 静态属性的值 + public static object GetStaticPropertyValue(Type type, string propertyName) + { + PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); + return property.GetValue(null); + } + + /// + /// 设置类的静态属性的值 + /// + /// 要设置静态属性的类 + /// 要设置的静态属性的名称 + /// 要设置的静态属性的值 + public static void SetStaticPropertyValue(Type type, string propertyName, object value) + { + PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); + property.SetValue(null, value); + } + + /// + /// 获取类的静态字段的值 + /// + /// 要获取静态字段的类 + /// 要获取的静态字段的名称 + /// 静态字段的值 + public static object GetStaticFieldValue(Type type, string fieldName) + { + FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); + return field.GetValue(null); + } + + /// + /// 设置类的静态字段的值 + /// + /// 要设置静态字段的类 + /// 要设置的静态字段的名称 + /// 要设置的静态字段的值 + public static void SetStaticFieldValue(Type type, string fieldName, object value) + { + FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); + field.SetValue(null, value); + } + + #endregion } } diff --git a/EasyTool.Core/ToolCategory/RuntimeUtil.cs b/EasyTool.Core/ToolCategory/RuntimeUtil.cs deleted file mode 100644 index eea6036..0000000 --- a/EasyTool.Core/ToolCategory/RuntimeUtil.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -#if NET5_0_OR_GREATER -using System.Runtime.Versioning; -#endif -using System.Text; - -namespace EasyTool -{ - /// - /// 运行时工具 - /// - public class RuntimeUtil - { - /// - /// 获取当前运行的 .NET 版本 - /// [Obsolete("请直接使用 Environment.Version.ToString()")] - /// - /// .NET 版本 - [Obsolete("请直接使用 Environment.Version.ToString()", false)] - public static string GetDotNetVersion() - { - return Environment.Version.ToString(); - } - - /// - /// 获取当前操作系统版本 - /// [Obsolete("请直接使用 Environment.OSVersion.ToString()")] - /// - /// 操作系统版本 - [Obsolete("请直接使用 Environment.OSVersion.ToString()", false)] - public static string GetOSVersion() - { - return Environment.OSVersion.ToString(); - } - - /// - /// 获取当前运行环境的处理器架构 - /// - /// 处理器架构 - public static string GetProcessArchitecture() - { - return Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"; - } - - /// - /// 获取当前应用程序内存使用量 - /// - /// 内存使用量(字节) - public static long GetCurrentMemoryUsage() - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - return GC.GetTotalMemory(true); - } - - /// - /// 获取当前运行时间 - /// - /// 运行时间(秒) - public static int GetCurrentRunningTime() - { - return (int)Stopwatch.StartNew().Elapsed.TotalSeconds; - } - - /// - /// 关闭当前应用程序 - /// - public static void ExitApplication() - { - Environment.Exit(0); - } - - /// - /// 获取当前系统的物理内存总量 - /// - /// 物理内存总量(字节) -#if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] -#endif - public static long GetTotalPhysicalMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); - return pc.RawValue; - } - - /// - /// 获取当前系统的可用物理内存量 - /// - /// 可用物理内存量(字节) -#if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] -#endif - public static float GetAvailablePhysicalMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); - return pc.NextValue(); - } - - /// - /// 获取当前系统的虚拟内存总量 - /// - /// 虚拟内存总量(字节) -#if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] -#endif - public static long GetTotalVirtualMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); - return pc.RawValue; - } - - /// - /// 获取当前系统的可用虚拟内存量 - /// - /// 可用虚拟内存量(字节) -#if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] -#endif - public static float GetAvailableVirtualMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); - return pc.NextValue(); - } - - [DllImport("kernel32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetPhysicallyInstalledSystemMemory(out long TotalMemoryInKilobytes); - - /// - /// 获取当前系统的实际物理内存总量 - /// - /// 实际物理内存总量(字节) -#if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] -#endif - public static long GetRealTotalPhysicalMemory() - { - GetPhysicallyInstalledSystemMemory(out long memoryInBytes); - return memoryInBytes * 1024; - } - } -} diff --git a/EasyTool.Core/ToolCategory/StrExtension.cs b/EasyTool.Core/ToolCategory/StrExtension.cs index 20933be..a5fd201 100644 --- a/EasyTool.Core/ToolCategory/StrExtension.cs +++ b/EasyTool.Core/ToolCategory/StrExtension.cs @@ -11,27 +11,6 @@ namespace EasyTool.Extension public static class StrExtension { #region 文本可为空判断 - - /// - /// 判断字符串是否为 null 或空 - /// [Obsolete("请直接使用 string.IsNullOrEmpty(value)")] - /// - [Obsolete("请直接使用 string.IsNullOrEmpty(value)", false)] - public static bool IsNullOrEmpty(this string value) - { - return string.IsNullOrEmpty(value); - } - - /// - /// 判断字符串是否为 null 或空白字符 - /// [Obsolete("请直接使用 string.IsNullOrWhiteSpace(value)")] - /// - [Obsolete("请直接使用 string.IsNullOrWhiteSpace(value)", false)] - public static bool IsNullOrWhiteSpace(this string value) - { - return string.IsNullOrWhiteSpace(value); - } - #endregion #region 字符串验证 @@ -230,33 +209,6 @@ public static string GenerateSlug(this string value) return slug; } - /// - /// 反转字符串 - /// [Obsolete("请直接使用 new string(value.Reverse().ToArray()) 或通过 LINQ")] - /// - [Obsolete("请直接使用 new string(value.Reverse().ToArray())", false)] - public static string Reverse(this string value) - { - if (string.IsNullOrEmpty(value)) - return value; - - var charArray = value.ToCharArray(); - Array.Reverse(charArray); - return new string(charArray); - } - - /// - /// 获取字符串的字节数(UTF-8编码) - /// [Obsolete("请直接使用 Encoding.UTF8.GetByteCount(value)")] - /// - [Obsolete("请直接使用 Encoding.UTF8.GetByteCount(value)", false)] - public static int GetByteCount(this string value) - { - if (string.IsNullOrEmpty(value)) - return 0; - - return Encoding.UTF8.GetByteCount(value); - } /// /// 隐藏字符串的中间部分(例如:手机号、身份证号) diff --git a/EasyTool.Core/ToolCategory/StrUtil.cs b/EasyTool.Core/ToolCategory/StrUtil.cs index aaaf9be..3e835f7 100644 --- a/EasyTool.Core/ToolCategory/StrUtil.cs +++ b/EasyTool.Core/ToolCategory/StrUtil.cs @@ -20,19 +20,6 @@ public static string RemoveAllSpaces(string str) return Regex.Replace(str, @"\s+", ""); } - /// - /// 将字符串中的指定字符替换成新的字符 - /// [Obsolete("请直接使用 str.Replace(oldChar, newChar)")] - /// - /// 要处理的字符串 - /// 要替换的字符 - /// 新的字符 - /// 处理后的字符串 - [Obsolete("请直接使用 str.Replace(oldChar, newChar)", false)] - public static string ReplaceChar(string str, char oldChar, char newChar) - { - return str.Replace(oldChar, newChar); - } /// /// 检查字符串是否为数字 @@ -67,91 +54,12 @@ public static bool IsDate(string str) return DateTime.TryParse(str, out result); } - /// - /// 获取字符串的字节数组 - /// [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)")] - /// - /// 要处理的字符串 - /// 字符串的字节数组 - [Obsolete("请直接使用 Encoding.UTF8.GetBytes(str)", false)] - public static byte[] GetBytes(string str) - { - return System.Text.Encoding.UTF8.GetBytes(str); - } - /// - /// 将字节数组转换为字符串 - /// [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)")] - /// - /// 要处理的字节数组 - /// 字节数组转换后的字符串 - [Obsolete("请直接使用 Encoding.UTF8.GetString(bytes)", false)] - public static string GetString(byte[] bytes) - { - return System.Text.Encoding.UTF8.GetString(bytes); - } - /// - /// 将字符串转换为大写 - /// [Obsolete("请直接使用 str.ToUpper()")] - /// - /// 要处理的字符串 - /// 处理后的字符串 - [Obsolete("请直接使用 str.ToUpper()", false)] - public static string ToUpperCase(string str) - { - return str.ToUpper(); - } - /// - /// 将字符串转换为小写 - /// [Obsolete("请直接使用 str.ToLower()")] - /// - /// 要处理的字符串 - /// 处理后的字符串 - [Obsolete("请直接使用 str.ToLower()", false)] - public static string ToLowerCase(string str) - { - return str.ToLower(); - } - /// - /// 检查字符串是否为空或null - /// [Obsolete("请直接使用 string.IsNullOrEmpty(str)")] - /// - /// 要检查的字符串 - /// 如果是空或null,则返回true,否则返回false - [Obsolete("请直接使用 string.IsNullOrEmpty(str)", false)] - public static bool IsNullOrEmpty(string str) - { - return string.IsNullOrEmpty(str); - } - /// - /// 检查字符串是否为空或仅由空格组成 - /// [Obsolete("请直接使用 string.IsNullOrWhiteSpace(str)")] - /// - /// 要检查的字符串 - /// 如果是空或仅由空格组成,则返回true,否则返回false - [Obsolete("请直接使用 string.IsNullOrWhiteSpace(str)", false)] - public static bool IsNullOrWhiteSpace(string str) - { - return string.IsNullOrWhiteSpace(str); - } - /// - /// 截取字符串的指定部分 - /// [Obsolete("请直接使用 str.Substring(startIndex, length)")] - /// - /// 要处理的字符串 - /// 起始位置(从0开始) - /// 要截取的长度 - /// 截取后的字符串 - [Obsolete("请直接使用 str.Substring(startIndex, length)", false)] - public static string Substring(string str, int startIndex, int length) - { - return str.Substring(startIndex, length); - } /// /// 将字符串转换为驼峰命名法 @@ -161,19 +69,20 @@ public static string Substring(string str, int startIndex, int length) public static string ToCamelCase(string str) { string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += words[i].Substring(0, 1).ToUpper() + words[i].Substring(1).ToLower(); + sb.Append(words[i].Substring(0, 1).ToUpper()); + sb.Append(words[i].Substring(1).ToLower()); } } - return result; + return sb.ToString(); } /// @@ -184,12 +93,13 @@ public static string ToCamelCase(string str) public static string ToPascalCase(string str) { string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { - result += words[i].Substring(0, 1).ToUpper() + words[i].Substring(1).ToLower(); + sb.Append(words[i].Substring(0, 1).ToUpper()); + sb.Append(words[i].Substring(1).ToLower()); } - return result; + return sb.ToString(); } /// @@ -200,19 +110,20 @@ public static string ToPascalCase(string str) public static string ToSnakeCase(string str) { string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += "_" + words[i].ToLower(); + sb.Append('_'); + sb.Append(words[i].ToLower()); } } - return result; + return sb.ToString(); } /// @@ -223,19 +134,20 @@ public static string ToSnakeCase(string str) public static string ToKebabCase(string str) { string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += "-" + words[i].ToLower(); + sb.Append('-'); + sb.Append(words[i].ToLower()); } } - return result; + return sb.ToString(); } /// @@ -259,33 +171,7 @@ public static bool EqualsIgnoreCaseAndWhiteSpace(string str1, string str2) return string.Equals(RemoveAllSpaces(str1), RemoveAllSpaces(str2), StringComparison.OrdinalIgnoreCase); } - /// - /// 在字符串的左侧填充指定字符,使字符串达到指定长度 - /// [Obsolete("请直接使用 str.PadLeft(length, paddingChar)")] - /// - /// 要处理的字符串 - /// 指定长度 - /// 填充字符 - /// 处理后的字符串 - [Obsolete("请直接使用 str.PadLeft(length, paddingChar)", false)] - public static string PadLeft(string str, int length, char paddingChar) - { - return str.PadLeft(length, paddingChar); - } - /// - /// 在字符串的右侧填充指定字符,使字符串达到指定长度 - /// [Obsolete("请直接使用 str.PadRight(length, paddingChar)")] - /// - /// 要处理的字符串 - /// 指定长度 - /// 填充字符 - /// 处理后的字符串 - [Obsolete("请直接使用 str.PadRight(length, paddingChar)", false)] - public static string PadRight(string str, int length, char paddingChar) - { - return str.PadRight(length, paddingChar); - } /// /// 将字符串中的某些字符替换成指定的字符 diff --git a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs index c5c3294..bbeeadc 100644 --- a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs +++ b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs @@ -313,16 +313,6 @@ public static StringBuilder Trim(this StringBuilder sb) #region 转换操作 - /// - /// 转换为只读字符串 - /// [Obsolete("请直接使用 sb?.ToString() ?? string.Empty")] - /// - [Obsolete("请直接使用 sb?.ToString() ?? string.Empty", false)] - public static string ToReadOnly(this StringBuilder sb) - { - return sb?.ToString() ?? string.Empty; - } - /// /// 转换为 MemoryStream /// diff --git a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs index 1304e6b..b81c809 100644 --- a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs +++ b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs @@ -352,31 +352,6 @@ public static string ToTitleCase(this string str, CultureInfo? culture = null) #region 大小写转换 - /// - /// 转换为大写(不变则为返回原字符串) - /// [Obsolete("请直接使用 value.ToUpper()")] - /// - [Obsolete("请直接使用 value.ToUpper()", false)] - public static string ToUpperSafe(this string str) - { - if (string.IsNullOrEmpty(str)) - return str; - - return str.ToUpper(); - } - - /// - /// 转换为小写(不变则为返回原字符串) - /// [Obsolete("请直接使用 value.ToLower()")] - /// - [Obsolete("请直接使用 value.ToLower()", false)] - public static string ToLowerSafe(this string str) - { - if (string.IsNullOrEmpty(str)) - return str; - - return str.ToLower(); - } /// /// 转换为单词首字母大写(如:helloWorld -> HelloWorld) diff --git a/EasyTool.Core/ToolCategory/SystemUtil.cs b/EasyTool.Core/ToolCategory/SystemUtil.cs new file mode 100644 index 0000000..154b48a --- /dev/null +++ b/EasyTool.Core/ToolCategory/SystemUtil.cs @@ -0,0 +1,331 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool +{ + /// + /// 系统工具类,提供系统、进程、内存、网络等相关功能 + /// + public static class SystemUtil + { + #region DLL 工具 + + /// + /// 根据类型名称创建实例,并返回一个 Object 对象 + /// + /// 程序集 + /// 类型名称 + /// 实例化类型所需要的参数 + /// 返回创建的实例对象 + public static object? CreateInstanceFromAssembly(Assembly assembly, string typeName, params object[] parameters) + { + Type? type = assembly.GetType(typeName); + if (type != null) + { + return Activator.CreateInstance(type, parameters); + } + return null; + } + + /// + /// 调用对象的方法,并返回调用结果 + /// + /// 要调用方法的对象 + /// 方法名称 + /// 方法所需要的参数 + /// 返回调用结果 + public static object? InvokeMethod(object instance, string methodName, params object[] parameters) + { + Type type = instance.GetType(); + MethodInfo? methodInfo = type.GetMethod(methodName); + if (methodInfo != null) + { + return methodInfo.Invoke(instance, parameters); + } + return null; + } + + /// + /// 从指定目录中加载所有的 DLL 文件,并返回一个 Assembly[] 数组 + /// + /// 要加载 DLL 文件的目录 + /// 返回一个 Assembly[] 数组,数组中每个元素代表一个 DLL 程序集 + public static Assembly[] LoadAllDllsFromDirectory(string directory) + { + try + { + if (!Directory.Exists(directory)) + { + throw new Exception("LoadAllDllsFromDirectory Error: Directory not exist."); + } + + string[] dllFiles = Directory.GetFiles(directory, "*.dll"); + if (dllFiles.Length == 0) + { + throw new Exception("LoadAllDllsFromDirectory Error: No DLL file found."); + } + + Assembly[] assemblies = new Assembly[dllFiles.Length]; + for (int i = 0; i < dllFiles.Length; i++) + { + assemblies[i] = Assembly.LoadFile(dllFiles[i]); + } + return assemblies; + } + catch (Exception ex) + { + throw new Exception("LoadAllDllsFromDirectory Error: " + ex.Message); + } + } + + #endregion + + #region 进程工具 + + /// + /// 通过进程名称获取进程 + /// + /// 进程名称 + /// 进程 + public static Process? GetProcessByName(string processName) + { + var processes = Process.GetProcessesByName(processName); + if (processes.Length > 0) + { + return processes[0]; + } + return null; + } + + /// + /// 判断进程是否存在 + /// + /// 进程名称 + /// 是否存在 + public static bool IsProcessExists(string processName) + { + return Process.GetProcessesByName(processName).Length > 0; + } + + /// + /// 暂停进程 + /// + /// 进程 + public static void SuspendProcess(Process process) + { + foreach (ProcessThread thread in process.Threads) + { + IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); + if (pOpenThread == IntPtr.Zero) + { + break; + } + SuspendThread(pOpenThread); + CloseHandle(pOpenThread); + } + } + + /// + /// 恢复进程 + /// + /// 进程 + public static void ResumeProcess(Process process) + { + foreach (ProcessThread thread in process.Threads) + { + IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); + if (pOpenThread == IntPtr.Zero) + { + break; + } + ResumeThread(pOpenThread); + CloseHandle(pOpenThread); + } + } + + [DllImport("kernel32.dll")] + static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId); + + [DllImport("kernel32.dll")] + static extern uint SuspendThread(IntPtr hThread); + + [DllImport("kernel32.dll")] + static extern IntPtr CloseHandle(IntPtr hObject); + + [DllImport("kernel32.dll")] + static extern uint ResumeThread(IntPtr hThread); + + [Flags] + public enum ThreadAccess : int + { + TERMINATE = (0x0001), + SUSPEND_RESUME = (0x0002), + GET_CONTEXT = (0x0008), + SET_CONTEXT = (0x0010), + SET_INFORMATION = (0x0020), + QUERY_INFORMATION = (0x0040), + SET_THREAD_TOKEN = (0x0080), + IMPERSONATE = (0x0100), + DIRECT_IMPERSONATION = (0x0200) + } + + #endregion + + #region 运行时工具 + + /// + /// 获取当前运行的处理器架构 + /// + /// 处理器架构 + public static string GetProcessArchitecture() + { + return Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"; + } + + /// + /// 获取当前应用程序内存使用量 + /// + /// 内存使用量(字节) + public static long GetCurrentMemoryUsage() + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + return GC.GetTotalMemory(true); + } + + /// + /// 关闭当前应用程序 + /// + public static void ExitApplication() + { + Environment.Exit(0); + } + + #endregion + + #region 网络工具 + + /// + /// 对指定主机进行 Ping 测试,返回是否成功 + /// + /// 主机名或IP地址 + /// 是否成功 + public static bool Ping(string host) + { + try + { + Ping pingSender = new Ping(); + PingReply reply = pingSender.Send(host); + if (reply.Status == IPStatus.Success) + { + return true; + } + else + { + return false; + } + } + catch + { + return false; + } + } + + /// + /// 获取指定主机的IP地址 + /// + /// 主机名 + /// IP地址 + public static IPAddress? GetIpAddress(string host) + { + try + { + IPHostEntry hostEntry = Dns.GetHostEntry(host); + foreach (IPAddress address in hostEntry.AddressList) + { + if (address.AddressFamily == AddressFamily.InterNetwork) + { + return address; + } + } + return null; + } + catch + { + return null; + } + } + + /// + /// 检查给定IP地址上的端口是否开放 + /// + /// IP地址 + /// 端口号 + /// 端口是否开放 + public static bool IsPortOpen(string host, int port) + { + try + { + IPAddress ipAddress = GetIpAddress(host); + if (ipAddress == null) + { + return false; + } + + IPEndPoint endpoint = new IPEndPoint(ipAddress, port); + using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + socket.Connect(endpoint); + return true; + } + } + catch + { + return false; + } + } + + /// + /// 发送HTTP GET请求并返回响应 + /// + /// URL地址 + /// 响应内容 + public static async Task HttpGetAsync(string url) + { + using (HttpClient client = new HttpClient()) + { + return await client.GetStringAsync(url); + } + } + + /// + /// 发送HTTP POST请求并返回响应 + /// + /// URL地址 + /// 要发送的数据 + /// 响应内容 + public static async Task HttpPostAsync(string url, string data) + { + using (HttpClient client = new HttpClient()) + { + StringContent content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"); + HttpResponseMessage response = await client.PostAsync(url, content); + return await response.Content.ReadAsStringAsync(); + } + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/TypeExtension.cs b/EasyTool.Core/ToolCategory/TypeExtension.cs index 5e35baa..547511b 100644 --- a/EasyTool.Core/ToolCategory/TypeExtension.cs +++ b/EasyTool.Core/ToolCategory/TypeExtension.cs @@ -237,18 +237,6 @@ public static string GetDescription(this Type? type) #region 泛型处理 - /// - /// 获取可空类型的实际类型 - /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type")] - /// - [Obsolete("请直接使用 Nullable.GetUnderlyingType(type) ?? type", false)] - public static Type? GetNullableType(this Type? type) - { - if (type == null) - return null; - - return Nullable.GetUnderlyingType(type) ?? type; - } /// /// 获取集合的元素类型 diff --git a/EasyTool.Core/ToolCategory/TypeUtil.cs b/EasyTool.Core/ToolCategory/TypeUtil.cs deleted file mode 100644 index 6fdd218..0000000 --- a/EasyTool.Core/ToolCategory/TypeUtil.cs +++ /dev/null @@ -1,212 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// 泛型类型工具 - /// - public class TypeUtil - { - /// - /// 判断类型是否是可空类型 - /// [Obsolete("请直接使用 Nullable.GetUnderlyingType(typeof(T)) != null")] - /// - /// 要判断的类型 - /// 是否是可空类型 - [Obsolete("请直接使用 Nullable.GetUnderlyingType(typeof(T)) != null", false)] - public static bool IsNullable() where T : struct - { - return Nullable.GetUnderlyingType(typeof(T)) != null; - } - - /// - /// 判断类型是否是枚举类型 - /// [Obsolete("请直接使用 typeof(T).IsEnum")] - /// - /// 要判断的类型 - /// 是否是枚举类型 - [Obsolete("请直接使用 typeof(T).IsEnum", false)] - public static bool IsEnum() - { - return typeof(T).IsEnum; - } - - /// - /// 获取泛型类型的参数类型 - /// [Obsolete("请直接使用 typeof(T).GetGenericArguments()")] - /// - /// 要获取参数类型的泛型类型 - /// 泛型类型的参数类型数组 - [Obsolete("请直接使用 typeof(T).GetGenericArguments()", false)] - public static Type[] GetGenericArguments() - { - return typeof(T).GetGenericArguments(); - } - - /// - /// 获取类型的所有属性 - /// [Obsolete("请直接使用 typeof(T).GetProperties()")] - /// - /// 要获取属性的类型 - /// 属性数组 - [Obsolete("请直接使用 typeof(T).GetProperties()", false)] - public static PropertyInfo[] GetProperties() - { - return typeof(T).GetProperties(); - } - - /// - /// 获取类型的所有字段 - /// [Obsolete("请直接使用 typeof(T).GetFields()")] - /// - /// 要获取字段的类型 - /// 字段数组 - [Obsolete("请直接使用 typeof(T).GetFields()", false)] - public static FieldInfo[] GetFields() - { - return typeof(T).GetFields(); - } - - /// - /// 获取类型的所有方法 - /// [Obsolete("请直接使用 typeof(T).GetMethods()")] - /// - /// 要获取方法的类型 - /// 方法数组 - [Obsolete("请直接使用 typeof(T).GetMethods()", false)] - public static MethodInfo[] GetMethods() - { - return typeof(T).GetMethods(); - } - - /// - /// 获取类型的所有事件 - /// [Obsolete("请直接使用 typeof(T).GetEvents()")] - /// - /// 要获取事件的类型 - /// 事件数组 - [Obsolete("请直接使用 typeof(T).GetEvents()", false)] - public static EventInfo[] GetEvents() - { - return typeof(T).GetEvents(); - } - - /// - /// 获取类型的所有属性、字段、方法和事件 - /// [Obsolete("请直接使用 typeof(T).GetMembers()")] - /// - /// 要获取成员的类型 - /// 成员数组 - [Obsolete("请直接使用 typeof(T).GetMembers()", false)] - public static MemberInfo[] GetMembers() - { - return typeof(T).GetMembers(); - } - - /// - /// 获取类型的所有构造函数 - /// [Obsolete("请直接使用 typeof(T).GetConstructors()")] - /// - /// 要获取构造函数的类型 - /// 构造函数数组 - [Obsolete("请直接使用 typeof(T).GetConstructors()", false)] - public static ConstructorInfo[] GetConstructors() - { - return typeof(T).GetConstructors(); - } - - /// - /// 判断类型是否实现了指定的接口 - /// - /// 要判断的类型 - /// 要判断的接口类型 - /// 是否实现了指定的接口 - public static bool ImplementsInterface() - { - return typeof(T).GetInterfaces().Any(i => i == typeof(TInterface)); - } - - /// - /// 判断类型是否继承了指定的基类 - /// [Obsolete("请直接使用 typeof(T).IsSubclassOf(typeof(TBase))")] - /// - /// 要判断的类型 - /// 要判断的基类类型 - /// 是否继承了指定的基类 - [Obsolete("请直接使用 typeof(T).IsSubclassOf(typeof(TBase))", false)] - public static bool InheritsFrom() - { - return typeof(T).IsSubclassOf(typeof(TBase)); - } - - /// - /// 创建指定类型的实例 - /// [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T))")] - /// - /// 要创建实例的类型 - /// 类型的实例 - [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T))", false)] - public static T CreateInstance() - { - return (T)Activator.CreateInstance(typeof(T)); - } - - /// - /// 创建指定类型的实例 - /// [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T), args)")] - /// - /// 要创建实例的类型 - /// 构造函数的参数 - /// 类型的实例 - [Obsolete("请直接使用 (T)Activator.CreateInstance(typeof(T), args)", false)] - public static T CreateInstance(params object[] args) - { - return (T)Activator.CreateInstance(typeof(T), args); - } - - /// - /// 获取枚举类型的所有值 - /// - /// 枚举类型 - /// 枚举类型的所有值 - public static IEnumerable GetEnumValues() - { - if (!IsEnum()) - { - throw new ArgumentException("Type is not an enum type"); - } - - return Enum.GetValues(typeof(T)).Cast(); - } - - /// - /// 将字符串转换为指定类型的值 - /// [Obsolete("请直接使用 (T)Convert.ChangeType(value, typeof(T))")] - /// - /// 要转换的类型 - /// 要转换的字符串 - /// 转换后的值 - [Obsolete("请直接使用 (T)Convert.ChangeType(value, typeof(T))", false)] - public static T ConvertFromString(string value) - { - return (T)Convert.ChangeType(value, typeof(T)); - } - - /// - /// 将值转换为指定类型的字符串 - /// [Obsolete("请直接使用 Convert.ToString(value)")] - /// - /// 要转换的类型 - /// 要转换的值 - /// 转换后的字符串 - [Obsolete("请直接使用 Convert.ToString(value)", false)] - public static string ConvertToString(T value) - { - return Convert.ToString(value); - } - } -} diff --git a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs index f5fdbaa..df44f68 100644 --- a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs +++ b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs @@ -7,7 +7,7 @@ namespace EasyTool.Tests public class CloneExtensionTests { [TestMethod()] - public void CloneTest() + public void DeepCloneTest() { var obj1 = new First() { @@ -24,7 +24,7 @@ public void CloneTest() MyProperty2 = "C", } }; - var obj2 = obj1.Clone(); + var obj2 = obj1.DeepClone(); Assert.AreEqual(obj1.MyProperty1, obj2.MyProperty1); Assert.AreEqual(obj1.Second1.MyProperty1, obj2.Second1.MyProperty1); From 353e1eb092e83c4d6d1945c7a1f5af8e6e849e74 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 16:42:20 +0800 Subject: [PATCH 04/19] =?UTF-8?q?feat(core):=20=E6=B7=BB=E5=8A=A0AES?= =?UTF-8?q?=E5=92=8CDES=E5=8A=A0=E5=AF=86=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E5=B9=B6=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在AesUtil中新增字节数组版本的加密解密方法 - 在DesUtil中修复IV设置错误并添加字节数组版本方法 - 将多个工具类从Extension命名空间迁移到对应的功能分类命名空间 - 重命名CreditCodeUtil和DesensitizedUtil为静态类 - 添加新的SystemCategory.EnvUtil工具类 - 修复EncodingUtil中BASE32编码的位移计算错误 - 更新RandomUtil引用路径解决依赖问题 - 标记ConvertExtension中的ToHex方法为过时并提供替代方案 --- EasyTool.Core/CodeCategory/AesUtil.cs | 125 +++++++ EasyTool.Core/CodeCategory/DesUtil.cs | 70 +++- EasyTool.Core/CodeCategory/EncodingUtil.cs | 39 ++- EasyTool.Core/CodeCategory/HashUtil.cs | 114 +++++-- EasyTool.Core/CodeCategory/HexUtil.cs | 67 +++- .../CollectionsCategory/ArrayExtension.cs | 2 +- .../DictionaryExtension.cs | 2 +- .../CollectionsCategory/LinkedListUtil.cs | 4 +- .../CollectionsCategory/ListExtension.cs | 2 +- .../CollectionsCategory/QueueUtil.cs | 4 +- .../CollectionsCategory/StackUtil.cs | 100 +----- .../ConvertCategory/ByteExtension.cs | 2 +- .../ColorExtension.cs | 2 +- .../ConvertCategory/ConvertExtension.cs | 47 +-- .../ConvertCategory/NumberExtension.cs | 2 +- .../PageUtil.cs | 2 +- .../DateTimeCategory/DateTimeExtension.cs | 2 +- .../DateTimeCategory/DateTimeUtil.cs | 2 +- .../DateTimeCategory/LunarCalendarUtil.cs | 4 +- EasyTool.Core/DateTimeCategory/TimerUtil.cs | 4 +- EasyTool.Core/EmojiCategory/EmojiUtil.cs | 4 +- .../IOCategory/FileSystemExtension.cs | 3 +- EasyTool.Core/IOCategory/FileTypeExtension.cs | 2 +- EasyTool.Core/IOCategory/FileUtil.cs | 41 +-- EasyTool.Core/IOCategory/StreamExtension.cs | 2 +- EasyTool.Core/IOCategory/Tailer.cs | 2 +- EasyTool.Core/IOCategory/WatchMonitor.cs | 2 +- .../{ToolCategory => IOCategory}/ZipUtil.cs | 4 +- EasyTool.Core/MathCategory/MathUtil.cs | 2 +- EasyTool.Core/MathCategory/PredictUtil.cs | 4 +- EasyTool.Core/MathCategory/RandomUtil.cs | 4 +- .../NetCategory/HttpClientExtension.cs | 2 +- .../{ToolCategory => NetCategory}/IpUtil.cs | 4 +- EasyTool.Core/NetCategory/URLUtil.cs | 4 +- .../ReflectUtil.cs | 2 +- .../DesensitizedUtil.cs | 4 +- EasyTool.Core/Standardization/Option.cs | 2 +- EasyTool.Core/Standardization/QueryPage.cs | 2 +- EasyTool.Core/SystemCategory/EnvUtil.cs | 97 ++++++ .../SystemUtil.cs | 2 +- EasyTool.Core/TextCategory/RegexUtil.cs | 4 +- EasyTool.Core/TextCategory/StrSplitter.cs | 4 +- .../{ToolCategory => TextCategory}/StrUtil.cs | 4 +- .../{ToolCategory => TextCategory}/XmlUtil.cs | 4 +- EasyTool.Core/ToolCategory/CreditCodeUtil.cs | 6 +- .../ToolCategory/DelegateExtension.cs | 2 +- EasyTool.Core/ToolCategory/EnumExtension.cs | 2 +- EasyTool.Core/ToolCategory/EnvUtil.cs | 309 ------------------ .../ToolCategory/ExceptionExtension.cs | 2 +- EasyTool.Core/ToolCategory/GuidExtension.cs | 2 +- EasyTool.Core/ToolCategory/IdUtil.cs | 4 +- EasyTool.Core/ToolCategory/ObjectExtension.cs | 2 +- .../ToolCategory/PropertyInfoExtension.cs | 2 +- .../ToolCategory/SimpleMapExtension.cs | 12 +- EasyTool.Core/ToolCategory/StrExtension.cs | 2 +- .../ToolCategory/StringBuilderExtension.cs | 2 +- .../ToolCategory/StringComparisonExtension.cs | 2 +- EasyTool.Core/ToolCategory/TaskExtension.cs | 2 +- EasyTool.Core/ToolCategory/TypeExtension.cs | 2 +- .../CloneCategory/CloneExtensionTests.cs | 2 +- .../MathCategory/MathUtilTests.cs | 2 +- .../Standardization/OptionTests.cs | 2 +- .../ToolCategory/IDUtilTests.cs | 2 +- .../ToolCategory/IpUtilTests.cs | 2 +- .../ToolCategory/SimpleMapExtensionTests.cs | 2 +- 65 files changed, 583 insertions(+), 583 deletions(-) rename EasyTool.Core/{ToolCategory => ConvertCategory}/ColorExtension.cs (99%) rename EasyTool.Core/{ToolCategory => DataCategory}/PageUtil.cs (99%) rename EasyTool.Core/{ToolCategory => IOCategory}/ZipUtil.cs (98%) rename EasyTool.Core/{ToolCategory => NetCategory}/IpUtil.cs (99%) rename EasyTool.Core/{ToolCategory => ReflectCategory}/ReflectUtil.cs (99%) rename EasyTool.Core/{ToolCategory => SecurityCategory}/DesensitizedUtil.cs (98%) create mode 100644 EasyTool.Core/SystemCategory/EnvUtil.cs rename EasyTool.Core/{ToolCategory => SystemCategory}/SystemUtil.cs (99%) rename EasyTool.Core/{ToolCategory => TextCategory}/StrUtil.cs (99%) rename EasyTool.Core/{ToolCategory => TextCategory}/XmlUtil.cs (99%) delete mode 100644 EasyTool.Core/ToolCategory/EnvUtil.cs diff --git a/EasyTool.Core/CodeCategory/AesUtil.cs b/EasyTool.Core/CodeCategory/AesUtil.cs index e2d2bb2..c7c8142 100644 --- a/EasyTool.Core/CodeCategory/AesUtil.cs +++ b/EasyTool.Core/CodeCategory/AesUtil.cs @@ -168,5 +168,130 @@ private static bool KeyIsLegalSize(string sk) return keyLength == 16 || keyLength == 24 || keyLength == 32; } private static bool IvIsLegalSize(string iv) => iv.Length == 16; + + /// + /// AES 加密(字节数组版本) + /// + /// 需要加密的数据 + /// 加密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 加密后的数据 + /// + public static byte[] Encrypt(byte[] data, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + using var symmetricKey = Aes.Create(); + symmetricKey.Mode = cipher; + symmetricKey.Padding = padding; + using var encryptor = symmetricKey.CreateEncryptor(key, iv); + using var memoryStream = new MemoryStream(); + using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); + + cryptoStream.Write(data, 0, data.Length); + cryptoStream.FlushFinalBlock(); + return memoryStream.ToArray(); + } + + /// + /// AES 解密(字节数组版本) + /// + /// 需要解密的数据 + /// 解密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 解密后的数据 + /// + public static byte[] Decrypt(byte[] data, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + using var symmetricKey = Aes.Create(); + symmetricKey.Mode = cipher; + symmetricKey.Padding = padding; + using var decryptor = symmetricKey.CreateDecryptor(key, iv); + using var memoryStream = new MemoryStream(data); + using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); + using var resultStream = new MemoryStream(); + cryptoStream.CopyTo(resultStream); + return resultStream.ToArray(); + } + + private static bool KeyIsLegalSizeBytes(byte[] key) + { + int keyLength = key.Length; + return keyLength == 16 || keyLength == 24 || keyLength == 32; + } + + /// + /// 创建加密流 + /// + /// 输出流 + /// 加密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 加密流 + public static CryptoStream CreateEncryptingStream(Stream outputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (outputStream == null) + throw new ArgumentNullException(nameof(outputStream)); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + var aes = Aes.Create(); + aes.Mode = cipher; + aes.Padding = padding; + var encryptor = aes.CreateEncryptor(key, iv); + return new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write); + } + + /// + /// 创建解密流 + /// + /// 输入流 + /// 解密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 解密流 + public static CryptoStream CreateDecryptingStream(Stream inputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (inputStream == null) + throw new ArgumentNullException(nameof(inputStream)); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + var aes = Aes.Create(); + aes.Mode = cipher; + aes.Padding = padding; + var decryptor = aes.CreateDecryptor(key, iv); + return new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read); + } } } diff --git a/EasyTool.Core/CodeCategory/DesUtil.cs b/EasyTool.Core/CodeCategory/DesUtil.cs index 94e350f..1fad310 100644 --- a/EasyTool.Core/CodeCategory/DesUtil.cs +++ b/EasyTool.Core/CodeCategory/DesUtil.cs @@ -85,12 +85,13 @@ public static string Encrypt(string str, string sk,string iv, CipherMode cipher if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; - des.IV = keyBytes; + des.IV = ivBytes; ICryptoTransform cTransform = des.CreateEncryptor(); var resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length); @@ -115,12 +116,13 @@ public static string Decrypt(string str, string sk, string iv, CipherMode cipher if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; - des.IV = keyBytes; + des.IV = ivBytes; ICryptoTransform cTransform = des.CreateDecryptor(); var resultArray = cTransform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length); return encoding.GetString(resultArray); @@ -134,5 +136,69 @@ private static bool IsLegalSize(string sk) return false; } + /// + /// DES 加密(字节数组版本) + /// + /// 待加密数据 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// + /// + public static byte[] Encrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = null, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (keyBytes == null || keyBytes.Length != 8) + throw new ArgumentException("不合规的秘钥,请确认秘钥为8位"); + if (ivBytes != null && ivBytes.Length != 8) + throw new ArgumentException("不合规的IV,请确认IV为8位"); + + var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + if (ivBytes != null) + des.IV = ivBytes; + else + des.IV = keyBytes; + + ICryptoTransform cTransform = des.CreateEncryptor(); + return cTransform.TransformFinalBlock(data, 0, data.Length); + } + + /// + /// DES 解密(字节数组版本) + /// + /// 待解密数据 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// + /// + public static byte[] Decrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = null, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (keyBytes == null || keyBytes.Length != 8) + throw new ArgumentException("不合规的秘钥,请确认秘钥为8位"); + if (ivBytes != null && ivBytes.Length != 8) + throw new ArgumentException("不合规的IV,请确认IV为8位"); + + var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + if (ivBytes != null) + des.IV = ivBytes; + else + des.IV = keyBytes; + + ICryptoTransform cTransform = des.CreateDecryptor(); + return cTransform.TransformFinalBlock(data, 0, data.Length); + } + } } diff --git a/EasyTool.Core/CodeCategory/EncodingUtil.cs b/EasyTool.Core/CodeCategory/EncodingUtil.cs index 67a3042..58d7a78 100644 --- a/EasyTool.Core/CodeCategory/EncodingUtil.cs +++ b/EasyTool.Core/CodeCategory/EncodingUtil.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// /// 编码工具类,提供各种编码格式的转换功能 @@ -40,8 +40,9 @@ public static string Base32Encode(byte[] bytes) int index = 0; for (int i = 0; i < length; i += 5) { - int val = (bytes[i] << 24) + ((i + 1 < length ? bytes[i + 1] : 0) << 16) + - ((i + 2 < length ? bytes[i + 2] : 0) << 8) + ((i + 3 < length ? bytes[i + 3] : 0) << 0); + int val = (bytes[i] << 32) + ((i + 1 < length ? bytes[i + 1] : 0) << 24) + + ((i + 2 < length ? bytes[i + 2] : 0) << 16) + ((i + 3 < length ? bytes[i + 3] : 0) << 8) + + ((i + 4 < length ? bytes[i + 4] : 0) << 0); chars[index++] = BASE32_CHARS[(val >> 35) & 0x1F]; chars[index++] = BASE32_CHARS[(val >> 30) & 0x1F]; chars[index++] = BASE32_CHARS[(val >> 25) & 0x1F]; @@ -158,10 +159,15 @@ public static byte[] Base32Decode(string str) // 解码 Base32 字符 private static int DecodeBase32Char(char c) { + // 支持大小写 if (c >= 'A' && c <= 'Z') { return c - 'A'; } + if (c >= 'a' && c <= 'z') + { + return c - 'a'; + } if (c >= '2' && c <= '7') { return c - '2' + 26; @@ -321,6 +327,21 @@ public static string RotDecrypt(string text, int n) {' ', " "} }; + // Morse 反向电码表,用于解码优化性能 + private static readonly Dictionary MORSE_REVERSE_TABLE; + + static EncodingUtil() + { + MORSE_REVERSE_TABLE = new Dictionary(); + foreach (var kvp in MORSE_TABLE) + { + if (!string.IsNullOrEmpty(kvp.Value)) + { + MORSE_REVERSE_TABLE[kvp.Value] = kvp.Key; + } + } + } + /// /// 将给定的字符串转换为 Morse 电码字符串 /// @@ -357,19 +378,15 @@ public static string MorseDecode(string morseCode) } string[] codes = morseCode.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - List chars = new List(); + StringBuilder result = new StringBuilder(codes.Length); foreach (string code in codes) { - foreach (KeyValuePair kvp in MORSE_TABLE) + if (MORSE_REVERSE_TABLE.TryGetValue(code, out char c)) { - if (kvp.Value == code) - { - chars.Add(kvp.Key); - break; - } + result.Append(c); } } - return new string(chars.ToArray()); + return result.ToString(); } #endregion diff --git a/EasyTool.Core/CodeCategory/HashUtil.cs b/EasyTool.Core/CodeCategory/HashUtil.cs index 5ac7eb3..7202d3a 100644 --- a/EasyTool.Core/CodeCategory/HashUtil.cs +++ b/EasyTool.Core/CodeCategory/HashUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; @@ -14,8 +14,11 @@ public class HashUtil /// /// 要进行hash的字符串 /// 返回hash值 - public static uint AdditiveHash(string str) + public static uint AdditiveHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { @@ -30,8 +33,11 @@ public static uint AdditiveHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint RotatingHash(string str) + public static uint RotatingHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = (uint)str.Length; foreach (char c in str) { @@ -46,8 +52,11 @@ public static uint RotatingHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint OneByOneHash(string str) + public static uint OneByOneHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { @@ -67,8 +76,11 @@ public static uint OneByOneHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint Bernstein(string str) + public static uint Bernstein(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 5381; foreach (char c in str) { @@ -86,8 +98,15 @@ public static uint Bernstein(string str) /// 哈希桶的数量 /// a的取值范围为[1, prime - 1] /// b的取值范围 - public static uint Universal(string str, uint prime, uint num_buckets, uint a, uint b) + public static uint Universal(string? str, uint prime, uint num_buckets, uint a, uint b) { + if (string.IsNullOrEmpty(str)) + return 0; + if (prime == 0) + throw new ArgumentException("Prime must be greater than 0", nameof(prime)); + if (num_buckets == 0) + throw new ArgumentException("Number of buckets must be greater than 0", nameof(num_buckets)); + uint hash = a; foreach (char c in str) { @@ -104,12 +123,18 @@ public static uint Universal(string str, uint prime, uint num_buckets, uint a, u /// 要进行hash的字符串 /// 随机数表 /// 返回hash值 - public static uint Zobrist(string str, uint[] table) + public static uint Zobrist(string? str, uint[]? table) { + if (string.IsNullOrEmpty(str)) + return 0; + if (table == null || table.Length == 0) + throw new ArgumentException("Table cannot be null or empty", nameof(table)); + uint hash = 0; for (int i = 0; i < str.Length; i++) { - hash ^= table[str[i]]; + int index = Math.Min(str[i], table.Length - 1); + hash ^= table[index]; } return hash; @@ -120,8 +145,11 @@ public static uint Zobrist(string str, uint[] table) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint FnvHash(string str) + public static uint FnvHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + const uint fnv_prime = 0x811C9DC5; uint hash = 0; foreach (char c in str) @@ -156,8 +184,13 @@ public static uint IntHash(uint key) /// b的取值范围为[1, 255] /// a的取值范围为[1, b-1] /// 返回hash值 - public static uint RsHash(string str, uint b, uint a) + public static uint RsHash(string? str, uint b, uint a) { + if (string.IsNullOrEmpty(str)) + return 0; + if (b == 0) + throw new ArgumentException("b must be greater than 0", nameof(b)); + uint hash = 0; foreach (char c in str) { @@ -173,8 +206,11 @@ public static uint RsHash(string str, uint b, uint a) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint JsHash(string str) + public static uint JsHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 1315423911; foreach (char c in str) { @@ -189,8 +225,11 @@ public static uint JsHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint PjwHash(string str) + public static uint PjwHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; const uint BitsInUnsignedInt = (uint)(sizeof(uint) * 8); const uint ThreeQuarters = (uint)((BitsInUnsignedInt * 3) / 4); @@ -214,8 +253,11 @@ public static uint PjwHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint ElfHash(string str) + public static uint ElfHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; uint x = 0; foreach (char c in str) @@ -239,8 +281,13 @@ public static uint ElfHash(string str) /// 要进行hash的字符串 /// 种子值 /// 返回hash值 - public static uint BkdrHash(string str, uint seed) + public static uint BkdrHash(string? str, uint seed) { + if (string.IsNullOrEmpty(str)) + return 0; + if (seed == 0) + throw new ArgumentException("Seed must be greater than 0", nameof(seed)); + uint hash = 0; foreach (char c in str) { @@ -255,8 +302,11 @@ public static uint BkdrHash(string str, uint seed) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint SdbmHash(string str) + public static uint SdbmHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { @@ -271,8 +321,11 @@ public static uint SdbmHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint DjbHash(string str) + public static uint DjbHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 5381; foreach (char c in str) { @@ -287,8 +340,11 @@ public static uint DjbHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint DekHash(string str) + public static uint DekHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = (uint)str.Length; foreach (char c in str) { @@ -303,8 +359,11 @@ public static uint DekHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint ApHash(string str) + public static uint ApHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; int i; for (i = 0; i < str.Length; i++) @@ -328,14 +387,17 @@ public static uint ApHash(string str) /// 要进行hash的字符串 /// hash表的长度 /// 返回hash值 - public static uint TianlHash(string str, uint len) + public static uint TianlHash(string? str, uint len) { + if (string.IsNullOrEmpty(str)) + return 0; + if (len == 0) + throw new ArgumentException("Length must be greater than 0", nameof(len)); + uint hash = 0; uint[] w = new uint[64]; uint[] v = new uint[8]; - if (str.Length == 0) return 0; - if (str.Length <= 64) { for (int i = 0; i < str.Length; i++) @@ -403,8 +465,11 @@ public static uint TianlHash(string str, uint len) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint JavaDefaultHash(string str) + public static uint JavaDefaultHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; uint h = hash; foreach (char c in str) @@ -421,8 +486,11 @@ public static uint JavaDefaultHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static ulong MixHash(string str) + public static ulong MixHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint seed = 131; // 31 131 1313 13131 131313 etc.. ulong hash1 = 0; ulong hash2 = 0; diff --git a/EasyTool.Core/CodeCategory/HexUtil.cs b/EasyTool.Core/CodeCategory/HexUtil.cs index 102a626..e3f9870 100644 --- a/EasyTool.Core/CodeCategory/HexUtil.cs +++ b/EasyTool.Core/CodeCategory/HexUtil.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// /// 16进制工具类 /// - public class HexUtil + public static class HexUtil { /// /// 将16进制字符串转换为字节数组 @@ -32,16 +32,53 @@ public static byte[] HexToBytes(string hex) /// 将字节数组转换为16进制字符串 /// /// 字节数组 + /// 是否使用小写(默认大写) /// 16进制字符串 - public static string BytesToHex(byte[] bytes) + public static string BytesToHex(byte[] bytes, bool lowercase = false) { - StringBuilder sb = new StringBuilder(); + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + StringBuilder sb = new StringBuilder(bytes.Length * 2); + string format = lowercase ? "x2" : "X2"; foreach (byte b in bytes) - sb.Append(b.ToString("X2")); + sb.Append(b.ToString(format)); return sb.ToString(); } + /// + /// 将16进制字符串转换为字节数组(安全版本,不抛出异常) + /// + /// 16进制字符串 + /// 转换后的字节数组 + /// 是否转换成功 + public static bool TryHexToBytes(string hex, out byte[]? bytes) + { + bytes = null; + if (string.IsNullOrWhiteSpace(hex)) + return false; + + hex = hex.Replace(" ", ""); + if (hex.Length % 2 != 0) + return false; + + try + { + bytes = new byte[hex.Length / 2]; + for (int i = 0; i < hex.Length; i += 2) + { + if (!byte.TryParse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber, null, out bytes[i / 2])) + return false; + } + return true; + } + catch + { + return false; + } + } + /// /// 比较两个16进制字符串是否相等 /// @@ -72,7 +109,25 @@ public static bool HexEquals(string hex1, string hex2) /// 十进制数值 public static int HexToInt(string hex) { - return Convert.ToInt32(hex, 16); + try + { + return Convert.ToInt32(hex, 16); + } + catch (FormatException ex) + { + throw new ArgumentException("Invalid hex string: " + hex, nameof(hex), ex); + } + } + + /// + /// 将16进制字符串转换为十进制数值(安全版本) + /// + /// 16进制字符串 + /// 转换后的数值 + /// 是否转换成功 + public static bool TryHexToInt(string hex, out int result) + { + return int.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out result); } /// diff --git a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs index c2ce442..ac3f4a4 100644 --- a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.CollectionsCategory { /// /// 数组扩展方法 diff --git a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs index 22ba15b..b8620c0 100644 --- a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs +++ b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.CollectionsCategory { public static class DictionaryExtension { diff --git a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs index a27bd80..5729fea 100644 --- a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs +++ b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// /// 双向链表工具类 /// - public class LinkedListUtil + public static class LinkedListUtil { /// /// 将指定元素添加到双向链表的结尾处。 diff --git a/EasyTool.Core/CollectionsCategory/ListExtension.cs b/EasyTool.Core/CollectionsCategory/ListExtension.cs index 15cd2b4..fa39ee2 100644 --- a/EasyTool.Core/CollectionsCategory/ListExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ListExtension.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.CollectionsCategory { /// /// List 集合扩展方法 diff --git a/EasyTool.Core/CollectionsCategory/QueueUtil.cs b/EasyTool.Core/CollectionsCategory/QueueUtil.cs index d1f45e9..68e72bf 100644 --- a/EasyTool.Core/CollectionsCategory/QueueUtil.cs +++ b/EasyTool.Core/CollectionsCategory/QueueUtil.cs @@ -3,12 +3,12 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// /// 队列工具类 /// - public class QueueUtil + public static class QueueUtil { /// /// 将指定元素添加到队列的末尾。 diff --git a/EasyTool.Core/CollectionsCategory/StackUtil.cs b/EasyTool.Core/CollectionsCategory/StackUtil.cs index 5a0cd9c..c7955bf 100644 --- a/EasyTool.Core/CollectionsCategory/StackUtil.cs +++ b/EasyTool.Core/CollectionsCategory/StackUtil.cs @@ -3,68 +3,13 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// - /// 堆栈工具类 + /// 栈工具类 /// - public class StackUtil + public static class StackUtil { - /// - /// 将指定元素推入堆栈的顶部。 - /// [Obsolete("请直接使用 stack.Push(item)")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要添加的元素 - [Obsolete("请直接使用 stack.Push(item)", false)] - public static void Push(Stack stack, T item) - { - stack.Push(item); - } - - /// - /// 从堆栈的顶部移除并返回对象。 - /// [Obsolete("请直接使用 stack.Pop()")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 堆栈顶部的元素 - /// 堆栈为空时引发异常 - [Obsolete("请直接使用 stack.Pop()", false)] - public static T Pop(Stack stack) - { - return stack.Pop(); - } - - /// - /// 返回位于堆栈顶部的对象但不将其移除。 - /// [Obsolete("请直接使用 stack.Peek()")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 堆栈顶部的元素 - /// 堆栈为空时引发异常 - [Obsolete("请直接使用 stack.Peek()", false)] - public static T Peek(Stack stack) - { - return stack.Peek(); - } - - /// - /// 确定堆栈是否包含指定元素。 - /// [Obsolete("请直接使用 stack.Contains(item)")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要查找的元素 - /// 如果堆栈包含指定元素,则为 true;否则为 false。 - [Obsolete("请直接使用 stack.Contains(item)", false)] - public static bool Contains(Stack stack, T item) - { - return stack.Contains(item); - } - /// /// 从堆栈中移除指定元素的第一个匹配项。 /// @@ -86,44 +31,5 @@ public static bool Remove(Stack stack, T item) } return false; } - - /// - /// 将堆栈中的所有元素复制到新数组中。 - /// [Obsolete("请直接使用 stack.ToArray()")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 包含堆栈中所有元素的新数组 - [Obsolete("请直接使用 stack.ToArray()", false)] - public static T[] ToArray(Stack stack) - { - return stack.ToArray(); - } - - /// - /// 将堆栈中的所有元素复制到新数组中,从指定的索引开始。 - /// [Obsolete("请直接使用 stack.CopyTo(array, arrayIndex)")] - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要复制到的目标数组 - /// 目标数组的起始索引 - [Obsolete("请直接使用 stack.CopyTo(array, arrayIndex)", false)] - public static void CopyTo(Stack stack, T[] array, int arrayIndex) - { - stack.CopyTo(array, arrayIndex); - } - - /// - /// 从堆栈中移除所有元素。 - /// [Obsolete("请直接使用 stack.Clear()")] - /// - /// 堆栈元素类型 - /// 堆栈 - [Obsolete("请直接使用 stack.Clear()", false)] - public static void Clear(Stack stack) - { - stack.Clear(); - } } } diff --git a/EasyTool.Core/ConvertCategory/ByteExtension.cs b/EasyTool.Core/ConvertCategory/ByteExtension.cs index 9a2be43..13d4829 100644 --- a/EasyTool.Core/ConvertCategory/ByteExtension.cs +++ b/EasyTool.Core/ConvertCategory/ByteExtension.cs @@ -3,7 +3,7 @@ using System.Text; using System.Linq; -namespace EasyTool.Extension +namespace EasyTool.ConvertCategory { /// /// Byte 字节扩展方法 diff --git a/EasyTool.Core/ToolCategory/ColorExtension.cs b/EasyTool.Core/ConvertCategory/ColorExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/ColorExtension.cs rename to EasyTool.Core/ConvertCategory/ColorExtension.cs index 41f7913..b11f7d7 100644 --- a/EasyTool.Core/ToolCategory/ColorExtension.cs +++ b/EasyTool.Core/ConvertCategory/ColorExtension.cs @@ -1,7 +1,7 @@ using System; using System.Drawing; -namespace EasyTool.Extension +namespace EasyTool.ConvertCategory { /// /// Color 颜色扩展方法 diff --git a/EasyTool.Core/ConvertCategory/ConvertExtension.cs b/EasyTool.Core/ConvertCategory/ConvertExtension.cs index 1402df8..e02a64a 100644 --- a/EasyTool.Core/ConvertCategory/ConvertExtension.cs +++ b/EasyTool.Core/ConvertCategory/ConvertExtension.cs @@ -217,27 +217,6 @@ public static string ToZhCn(this bool b) #region ==字节转换== - /// - /// 转换为16进制 - /// - /// - /// 是否小写 - /// - public static string ToHex(this byte[] bytes, bool lowerCase = true) - { - if (bytes == null) - return string.Empty; - - var result = new StringBuilder(); - var format = lowerCase ? "x2" : "X2"; - for (var i = 0; i < bytes.Length; i++) - { - result.Append(bytes[i].ToString(format)); - } - - return result.ToString(); - } - /// /// 16进制转字节数组 /// @@ -258,6 +237,32 @@ public static string ToHex(this byte[] bytes, bool lowerCase = true) return bytes; } + /// + /// 转换为16进制 + /// + /// + /// 已过时:请使用 替代 + /// 此方法与 ByteExtension.ToHex 存在命名冲突,已标记为过时 + /// + /// + /// 是否小写 + /// + [Obsolete("请使用 ByteExtension.ToHex(byte[], bool) 替代")] + public static string ToHexLegacy(this byte[] bytes, bool lowerCase = true) + { + if (bytes == null) + return string.Empty; + + var result = new StringBuilder(); + var format = lowerCase ? "x2" : "X2"; + for (var i = 0; i < bytes.Length; i++) + { + result.Append(bytes[i].ToString(format)); + } + + return result.ToString(); + } + /// diff --git a/EasyTool.Core/ConvertCategory/NumberExtension.cs b/EasyTool.Core/ConvertCategory/NumberExtension.cs index a9cae99..dcc047a 100644 --- a/EasyTool.Core/ConvertCategory/NumberExtension.cs +++ b/EasyTool.Core/ConvertCategory/NumberExtension.cs @@ -1,6 +1,6 @@ using System; -namespace EasyTool.Extension +namespace EasyTool.ConvertCategory { /// /// 数字类型扩展方法 diff --git a/EasyTool.Core/ToolCategory/PageUtil.cs b/EasyTool.Core/DataCategory/PageUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/PageUtil.cs rename to EasyTool.Core/DataCategory/PageUtil.cs index d1334eb..5a6b25e 100644 --- a/EasyTool.Core/ToolCategory/PageUtil.cs +++ b/EasyTool.Core/DataCategory/PageUtil.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.DataCategory { /// /// 分页工具类,支持多种数据源和多种排序方式的分页 diff --git a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs index bad196b..dd88327 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.DateTimeCategory { /// /// 提供各种日期操作和计算的工具类。 diff --git a/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs b/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs index f31092d..5ad6dcb 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Globalization; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// /// 提供各种日期操作和计算的工具类。 diff --git a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs index 8dc4d14..8dc0c5d 100644 --- a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs +++ b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs @@ -1,11 +1,11 @@ using System; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// /// 农历日期工具类 /// - public class LunarCalendarUtil + public static class LunarCalendarUtil { #region 基础数据 diff --git a/EasyTool.Core/DateTimeCategory/TimerUtil.cs b/EasyTool.Core/DateTimeCategory/TimerUtil.cs index afd9082..10b945a 100644 --- a/EasyTool.Core/DateTimeCategory/TimerUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimerUtil.cs @@ -1,12 +1,12 @@ using System; using System.Diagnostics; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// /// 计时器工具类,提供各种计时和时间间隔计算的方法。 /// - public class TimerUtil + public static class TimerUtil { /// /// 记录程序启动时间。 diff --git a/EasyTool.Core/EmojiCategory/EmojiUtil.cs b/EasyTool.Core/EmojiCategory/EmojiUtil.cs index 85500eb..615ba06 100644 --- a/EasyTool.Core/EmojiCategory/EmojiUtil.cs +++ b/EasyTool.Core/EmojiCategory/EmojiUtil.cs @@ -3,9 +3,9 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.EmojiCategory { - public class EmojiUtil + public static class EmojiUtil { // Unicode 区间:Emoji 表情符号的 Unicode 区间 private const string EmojiRanges = "[\u1F600-\u1F64F\u1F910-\u1F96B\u1F980-\u1F9E0]"; diff --git a/EasyTool.Core/IOCategory/FileSystemExtension.cs b/EasyTool.Core/IOCategory/FileSystemExtension.cs index 5a5c5ca..4fe44fa 100644 --- a/EasyTool.Core/IOCategory/FileSystemExtension.cs +++ b/EasyTool.Core/IOCategory/FileSystemExtension.cs @@ -1,8 +1,9 @@ using System; using System.IO; using System.Linq; +using EasyTool.ConvertCategory; -namespace EasyTool.Extension +namespace EasyTool.IOCategory { /// /// 文件系统扩展方法 diff --git a/EasyTool.Core/IOCategory/FileTypeExtension.cs b/EasyTool.Core/IOCategory/FileTypeExtension.cs index f8a5d0b..4220f7a 100644 --- a/EasyTool.Core/IOCategory/FileTypeExtension.cs +++ b/EasyTool.Core/IOCategory/FileTypeExtension.cs @@ -3,7 +3,7 @@ using System.IO; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.IOCategory { /// /// 文件类型扩展方法 diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index 9104d26..eb8133e 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -6,54 +6,19 @@ using System.Net.Http; using System.Text; using System.Web; -using EasyTool.Extension; +using EasyTool.IOCategory; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件操作类 /// - public class FileUtil + public static class FileUtil { /// /// 判断当前操作系统是否为 Windows /// - public static bool IsWindows() - { - // 判断当前操作系统的 PlatformID 是否为 Win32S、Win32Windows、Win32NT 或 WinCE - return Environment.OSVersion.Platform == PlatformID.Win32S - || Environment.OSVersion.Platform == PlatformID.Win32Windows - || Environment.OSVersion.Platform == PlatformID.Win32NT - || Environment.OSVersion.Platform == PlatformID.WinCE; - } - - /// - /// 判断当前操作系统是否为 Unix - /// - public static bool IsUnix() - { - // 判断当前操作系统的 PlatformID 是否为 Unix - return Environment.OSVersion.Platform == PlatformID.Unix; - } - - /// - /// 判断当前操作系统是否为 Xbox - /// - public static bool IsXbox() - { - // 判断当前操作系统的 PlatformID 是否为 Xbox - return Environment.OSVersion.Platform == PlatformID.Xbox; - } - - /// - /// 判断当前操作系统是否为 macOS - /// - public static bool IsMacOSX() - { - // 判断当前操作系统的 PlatformID 是否为 macOSX - return Environment.OSVersion.Platform == PlatformID.MacOSX; - } /// /// 判断文件或目录是否为空 diff --git a/EasyTool.Core/IOCategory/StreamExtension.cs b/EasyTool.Core/IOCategory/StreamExtension.cs index d3c93bd..bad7a17 100644 --- a/EasyTool.Core/IOCategory/StreamExtension.cs +++ b/EasyTool.Core/IOCategory/StreamExtension.cs @@ -4,7 +4,7 @@ using System.Threading; using System.Threading.Tasks; -namespace EasyTool.Extension +namespace EasyTool.IOCategory { /// /// Stream 扩展方法 diff --git a/EasyTool.Core/IOCategory/Tailer.cs b/EasyTool.Core/IOCategory/Tailer.cs index 1a6d6f8..2fd0380 100644 --- a/EasyTool.Core/IOCategory/Tailer.cs +++ b/EasyTool.Core/IOCategory/Tailer.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件跟随工具类 diff --git a/EasyTool.Core/IOCategory/WatchMonitor.cs b/EasyTool.Core/IOCategory/WatchMonitor.cs index 226e44c..6be74b7 100644 --- a/EasyTool.Core/IOCategory/WatchMonitor.cs +++ b/EasyTool.Core/IOCategory/WatchMonitor.cs @@ -3,7 +3,7 @@ using System.IO; using System.Text; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件监听工具类 diff --git a/EasyTool.Core/ToolCategory/ZipUtil.cs b/EasyTool.Core/IOCategory/ZipUtil.cs similarity index 98% rename from EasyTool.Core/ToolCategory/ZipUtil.cs rename to EasyTool.Core/IOCategory/ZipUtil.cs index 4d3de35..ceb894d 100644 --- a/EasyTool.Core/ToolCategory/ZipUtil.cs +++ b/EasyTool.Core/IOCategory/ZipUtil.cs @@ -4,12 +4,12 @@ using System.IO; using System.Text; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 压缩工具 /// - public class ZipUtil + public static class ZipUtil { /// /// 压缩文件或目录 diff --git a/EasyTool.Core/MathCategory/MathUtil.cs b/EasyTool.Core/MathCategory/MathUtil.cs index ff4f064..6887204 100644 --- a/EasyTool.Core/MathCategory/MathUtil.cs +++ b/EasyTool.Core/MathCategory/MathUtil.cs @@ -1,7 +1,7 @@ using System; using System.Text; -namespace EasyTool +namespace EasyTool.MathCategory { /// /// 数学工具类,提供数字计算和数学运算方法 diff --git a/EasyTool.Core/MathCategory/PredictUtil.cs b/EasyTool.Core/MathCategory/PredictUtil.cs index ebff051..564a42f 100644 --- a/EasyTool.Core/MathCategory/PredictUtil.cs +++ b/EasyTool.Core/MathCategory/PredictUtil.cs @@ -3,12 +3,12 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.MathCategory { /// /// 预测数据类 /// - public class PredictUtil + public static class PredictUtil { /// /// 线性回归预测 diff --git a/EasyTool.Core/MathCategory/RandomUtil.cs b/EasyTool.Core/MathCategory/RandomUtil.cs index c8ea967..5bc77d7 100644 --- a/EasyTool.Core/MathCategory/RandomUtil.cs +++ b/EasyTool.Core/MathCategory/RandomUtil.cs @@ -3,9 +3,9 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.MathCategory { - public class RandomUtil + public static class RandomUtil { private static readonly Random random = new Random(); diff --git a/EasyTool.Core/NetCategory/HttpClientExtension.cs b/EasyTool.Core/NetCategory/HttpClientExtension.cs index 2c4654e..59ca88b 100644 --- a/EasyTool.Core/NetCategory/HttpClientExtension.cs +++ b/EasyTool.Core/NetCategory/HttpClientExtension.cs @@ -5,7 +5,7 @@ using System.Threading; using System.Threading.Tasks; -namespace EasyTool.Extension +namespace EasyTool.NetCategory { /// /// 扩展HttpClient中一些缺少的请求方式 diff --git a/EasyTool.Core/ToolCategory/IpUtil.cs b/EasyTool.Core/NetCategory/IpUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/IpUtil.cs rename to EasyTool.Core/NetCategory/IpUtil.cs index 577185e..34adbe8 100644 --- a/EasyTool.Core/ToolCategory/IpUtil.cs +++ b/EasyTool.Core/NetCategory/IpUtil.cs @@ -2,12 +2,12 @@ using System.Net; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.NetCategory { /// /// IP地址工具类 /// - public class IpUtil + public static class IpUtil { /// /// 判断是否是ipv4格式 diff --git a/EasyTool.Core/NetCategory/URLUtil.cs b/EasyTool.Core/NetCategory/URLUtil.cs index d281985..3227671 100644 --- a/EasyTool.Core/NetCategory/URLUtil.cs +++ b/EasyTool.Core/NetCategory/URLUtil.cs @@ -4,12 +4,12 @@ using System.Text; using System.Web; -namespace EasyTool +namespace EasyTool.NetCategory { /// /// URL工具类 /// - public class URLUtil + public static class URLUtil { /// /// 解析URL并返回其组成部分。 diff --git a/EasyTool.Core/ToolCategory/ReflectUtil.cs b/EasyTool.Core/ReflectCategory/ReflectUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/ReflectUtil.cs rename to EasyTool.Core/ReflectCategory/ReflectUtil.cs index f22b1dd..9848cbe 100644 --- a/EasyTool.Core/ToolCategory/ReflectUtil.cs +++ b/EasyTool.Core/ReflectCategory/ReflectUtil.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Reflection; -namespace EasyTool +namespace EasyTool.ReflectCategory { /// /// 反射工具类,提供类型、属性、字段、方法的反射操作 diff --git a/EasyTool.Core/ToolCategory/DesensitizedUtil.cs b/EasyTool.Core/SecurityCategory/DesensitizedUtil.cs similarity index 98% rename from EasyTool.Core/ToolCategory/DesensitizedUtil.cs rename to EasyTool.Core/SecurityCategory/DesensitizedUtil.cs index 62d07b3..3873756 100644 --- a/EasyTool.Core/ToolCategory/DesensitizedUtil.cs +++ b/EasyTool.Core/SecurityCategory/DesensitizedUtil.cs @@ -3,12 +3,12 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.SecurityCategory { /// /// 信息脱敏工具类 /// - public class DesensitizedUtil + public static class DesensitizedUtil { private static readonly Regex IdcardRegex = new Regex(@"^\d{15}(\d{2}[0-9xX])?$"); private static readonly Regex MobileRegex = new Regex(@"^(13\d|14[5-9]|15[^4\D]|16\d|17[0-8]|18\d|19[0-3,5-9])\d{8}$"); diff --git a/EasyTool.Core/Standardization/Option.cs b/EasyTool.Core/Standardization/Option.cs index fc8f89d..f15b2cc 100644 --- a/EasyTool.Core/Standardization/Option.cs +++ b/EasyTool.Core/Standardization/Option.cs @@ -6,7 +6,7 @@ using System.Reflection; -namespace EasyTool +namespace EasyTool.Standardization { #if NET6_0_OR_GREATER diff --git a/EasyTool.Core/Standardization/QueryPage.cs b/EasyTool.Core/Standardization/QueryPage.cs index 192b954..26e7566 100644 --- a/EasyTool.Core/Standardization/QueryPage.cs +++ b/EasyTool.Core/Standardization/QueryPage.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.Standardization { /* * 标准化查询参数,减少前后端对接工作 diff --git a/EasyTool.Core/SystemCategory/EnvUtil.cs b/EasyTool.Core/SystemCategory/EnvUtil.cs new file mode 100644 index 0000000..502ff3c --- /dev/null +++ b/EasyTool.Core/SystemCategory/EnvUtil.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Net.Sockets; +using System.Net; +using System.Runtime.InteropServices; +using System.Text; + +namespace EasyTool.SystemCategory +{ + /// + /// 环境工具 + /// + public static class EnvUtil + { + #region 系统信息 + + /// + /// 获取系统信息 + /// + /// 系统信息字符串 + public static string GetSystemInfo() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine("操作系统版本:" + Environment.OSVersion.ToString()); + sb.AppendLine("系统位数:" + (Environment.Is64BitOperatingSystem ? "64 位" : "32 位")); + sb.AppendLine("系统目录:" + Environment.SystemDirectory); + sb.AppendLine("处理器数量:" + Environment.ProcessorCount); + sb.AppendLine("计算机名:" + Environment.MachineName); + sb.AppendLine("用户名:" + Environment.UserName); + sb.AppendLine("用户域名:" + Environment.UserDomainName); + sb.AppendLine("当前目录:" + Environment.CurrentDirectory); + sb.AppendLine("CLR版本:" + Environment.Version.ToString()); + return sb.ToString(); + } + + /// + /// 判断当前系统是否为Windows操作系统 + /// + /// 当前系统是否为Windows操作系统 + public static bool IsWindows() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + } + + /// + /// 判断当前系统是否为Linux操作系统 + /// + /// 当前系统是否为Linux操作系统 + public static bool IsLinux() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + } + + /// + /// 判断当前系统是否为macOS操作系统 + /// + /// 当前系统是否为macOS操作系统 + public static bool IsMacOS() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + } + + #endregion + + #region 网络时间 + + /* + /// + /// 获取网络时间 + /// + /// 网络时间 + public static DateTime GetNetworkTime() + { + const string ntpServer = "time.windows.com"; + byte[] ntpData = new byte[48]; + ntpData[0] = 0x1B; + IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList; + IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123); + using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) + { + socket.Connect(ipEndPoint); + socket.Send(ntpData); + socket.Receive(ntpData); + } + const byte offsetTransmitTime = 40; + uint intPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime); + uint fractPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime + 4); + ulong milliseconds = (ulong)(intPart * 1000) + ((ulong)fractPart * 1000) / 0x100000000L); + return new DateTime(1900, 1, 1, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)milliseconds).ToLocalTime(); + } + */ + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/SystemUtil.cs b/EasyTool.Core/SystemCategory/SystemUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/SystemUtil.cs rename to EasyTool.Core/SystemCategory/SystemUtil.cs index 154b48a..e81649e 100644 --- a/EasyTool.Core/ToolCategory/SystemUtil.cs +++ b/EasyTool.Core/SystemCategory/SystemUtil.cs @@ -12,7 +12,7 @@ using System.Text; using System.Threading.Tasks; -namespace EasyTool +namespace EasyTool.SystemCategory { /// /// 系统工具类,提供系统、进程、内存、网络等相关功能 diff --git a/EasyTool.Core/TextCategory/RegexUtil.cs b/EasyTool.Core/TextCategory/RegexUtil.cs index 2ce88d2..c75e228 100644 --- a/EasyTool.Core/TextCategory/RegexUtil.cs +++ b/EasyTool.Core/TextCategory/RegexUtil.cs @@ -4,12 +4,12 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// 正则工具 /// - public class RegexUtil + public static class RegexUtil { /// /// 验证字符串是否与指定的正则表达式匹配,并返回匹配结果 diff --git a/EasyTool.Core/TextCategory/StrSplitter.cs b/EasyTool.Core/TextCategory/StrSplitter.cs index cc2c6e2..fb85ebd 100644 --- a/EasyTool.Core/TextCategory/StrSplitter.cs +++ b/EasyTool.Core/TextCategory/StrSplitter.cs @@ -3,9 +3,9 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { - public class StrSplitter + public static class StrSplitter { /// /// 使用指定的分隔符将输入字符串分割成字符串数组。 diff --git a/EasyTool.Core/ToolCategory/StrUtil.cs b/EasyTool.Core/TextCategory/StrUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/StrUtil.cs rename to EasyTool.Core/TextCategory/StrUtil.cs index 3e835f7..776f920 100644 --- a/EasyTool.Core/ToolCategory/StrUtil.cs +++ b/EasyTool.Core/TextCategory/StrUtil.cs @@ -3,12 +3,12 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// 字符串处理工具类 /// - public class StrUtil + public static class StrUtil { /// /// 移除字符串中的所有空格 diff --git a/EasyTool.Core/ToolCategory/XmlUtil.cs b/EasyTool.Core/TextCategory/XmlUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/XmlUtil.cs rename to EasyTool.Core/TextCategory/XmlUtil.cs index 92a081d..c50ed04 100644 --- a/EasyTool.Core/ToolCategory/XmlUtil.cs +++ b/EasyTool.Core/TextCategory/XmlUtil.cs @@ -3,12 +3,12 @@ using System.Text; using System.Xml; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// XML工具类 /// - public class XmlUtil + public static class XmlUtil { /// /// 解析XML字符串。 diff --git a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs b/EasyTool.Core/ToolCategory/CreditCodeUtil.cs index 25b9e44..21d8167 100644 --- a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs +++ b/EasyTool.Core/ToolCategory/CreditCodeUtil.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.ToolCategory { /// /// 社会信用代码工具 /// - public class CreditCodeUtil + public static class CreditCodeUtil { private const string BaseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; // 社会信用代码中的基础字符集 private const int Modulo = 31; // 校验码计算中的模数 @@ -68,7 +68,7 @@ public static string GenerateRandomCreditCode() { string orgCode = "911101"; // 默认的组织机构代码 string entType = "00"; // 默认的企业类型 - string regNum = RandomUtil.RandomNumberString(10); // 生成随机的注册号 + string regNum = EasyTool.MathCategory.RandomUtil.RandomNumberString(10); // 生成随机的注册号 string code = orgCode + entType + regNum; // 计算出校验码并添加到社会信用代码中 diff --git a/EasyTool.Core/ToolCategory/DelegateExtension.cs b/EasyTool.Core/ToolCategory/DelegateExtension.cs index d90708b..5585100 100644 --- a/EasyTool.Core/ToolCategory/DelegateExtension.cs +++ b/EasyTool.Core/ToolCategory/DelegateExtension.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Delegate 委托扩展方法 diff --git a/EasyTool.Core/ToolCategory/EnumExtension.cs b/EasyTool.Core/ToolCategory/EnumExtension.cs index 6605335..5a1a3a0 100644 --- a/EasyTool.Core/ToolCategory/EnumExtension.cs +++ b/EasyTool.Core/ToolCategory/EnumExtension.cs @@ -3,7 +3,7 @@ using System.ComponentModel; using System.Linq; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Enum 枚举扩展方法 diff --git a/EasyTool.Core/ToolCategory/EnvUtil.cs b/EasyTool.Core/ToolCategory/EnvUtil.cs deleted file mode 100644 index bb67821..0000000 --- a/EasyTool.Core/ToolCategory/EnvUtil.cs +++ /dev/null @@ -1,309 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Net.Sockets; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; - -namespace EasyTool -{ - /// - /// 环境工具 - /// - public class EnvUtil - { - /// - /// 获取系统信息 - /// - /// 系统信息字符串 - public static string GetSystemInfo() - { - StringBuilder sb = new StringBuilder(); - sb.AppendLine("操作系统版本:" + Environment.OSVersion.ToString()); - sb.AppendLine("系统位数:" + (Environment.Is64BitOperatingSystem ? "64 位" : "32 位")); - sb.AppendLine("系统目录:" + Environment.SystemDirectory); - sb.AppendLine("处理器数量:" + Environment.ProcessorCount); - sb.AppendLine("计算机名:" + Environment.MachineName); - sb.AppendLine("用户名:" + Environment.UserName); - sb.AppendLine("用户域名:" + Environment.UserDomainName); - sb.AppendLine("当前目录:" + Environment.CurrentDirectory); - sb.AppendLine("CLR版本:" + Environment.Version.ToString()); - return sb.ToString(); - } - - /// - /// 获取环境变量值 - /// [Obsolete("请直接使用 Environment.GetEnvironmentVariable(name)")] - /// - /// 环境变量名称 - /// 环境变量值 - [Obsolete("请直接使用 Environment.GetEnvironmentVariable(name)", false)] - public static string GetEnvironmentVariable(string name) - { - return Environment.GetEnvironmentVariable(name); - } - - /// - /// 设置环境变量值 - /// [Obsolete("请直接使用 Environment.SetEnvironmentVariable(name, value)")] - /// - /// 环境变量名称 - /// 环境变量值 - [Obsolete("请直接使用 Environment.SetEnvironmentVariable(name, value)", false)] - public static void SetEnvironmentVariable(string name, string value) - { - Environment.SetEnvironmentVariable(name, value); - } - - /// - /// 获取环境变量列表 - /// - /// 环境变量列表 - public static IDictionary GetEnvironmentVariables() - { - IDictionary variables = new Dictionary(); - foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) - { - variables.Add(de.Key.ToString(), de.Value.ToString()); - } - return variables; - } - - /// - /// 获取当前目录下的文件列表 - /// - /// 当前目录下的文件列表 - public static List GetFilesInCurrentDirectory() - { - List files = new List(); - foreach (string file in Directory.GetFiles(Environment.CurrentDirectory)) - { - files.Add(file); - } - return files; - } - - /// - /// 获取指定目录下的文件列表 - /// - /// 指定目录路径 - /// 指定目录下的文件列表 - public static List GetFilesInDirectory(string path) - { - List files = new List(); - foreach (string file in Directory.GetFiles(path)) - { - files.Add(file); - } - return files; - } - - /// - /// 获取当前目录下的目录列表 - /// - /// 当前目录下的目录列表 - public static List GetDirectoriesInCurrentDirectory() - { - List directories = new List(); - foreach (string directory in Directory.GetDirectories(Environment.CurrentDirectory)) - { - directories.Add(directory); - } - return directories; - } - - /// - /// 获取指定目录下的目录列表 - /// - /// 指定目录路径 - /// 指定目录下的目录列表 - public static List GetDirectoriesInDirectory(string path) - { - List directories = new List(); - foreach (string directory in Directory.GetDirectories(path)) - { - directories.Add(directory); - } - return directories; - } - - /// - /// 创建文件 - /// [Obsolete("请直接使用 File.Create(path)")] - /// - /// 文件路径 - [Obsolete("请直接使用 File.Create(path)", false)] - public static void CreateFile(string path) - { - File.Create(path); - } - - /// - /// 删除文件 - /// [Obsolete("请直接使用 File.Delete(path)")] - /// - /// 文件路径 - [Obsolete("请直接使用 File.Delete(path)", false)] - public static void DeleteFile(string path) - { - File.Delete(path); - } - - /// - /// 创建目录 - /// [Obsolete("请直接使用 Directory.CreateDirectory(path)")] - /// - /// 目录路径 - [Obsolete("请直接使用 Directory.CreateDirectory(path)", false)] - public static void CreateDirectory(string path) - { - Directory.CreateDirectory(path); - } - - /// - /// 删除目录 - /// [Obsolete("请直接使用 Directory.Delete(path, true)")] - /// - /// 目录路径 - [Obsolete("请直接使用 Directory.Delete(path, true)", false)] - public static void DeleteDirectory(string path) - { - Directory.Delete(path, true); - } - - /// - /// 检查目录是否存在 - /// [Obsolete("请直接使用 Directory.Exists(path)")] - /// - /// 目录路径 - /// 目录是否存在 - [Obsolete("请直接使用 Directory.Exists(path)", false)] - public static bool DirectoryExists(string path) - { - return Directory.Exists(path); - } - - /// - /// 检查文件是否存在 - /// [Obsolete("请直接使用 File.Exists(path)")] - /// - /// 文件路径 - /// 文件是否存在 - [Obsolete("请直接使用 File.Exists(path)", false)] - public static bool FileExists(string path) - { - return File.Exists(path); - } - - /// - /// 获取文件大小 - /// - /// 文件路径 - /// 文件大小(字节) - public static long GetFileSize(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.Length; - } - - /// - /// 获取文件的创建时间 - /// - /// 文件路径 - /// 文件的创建时间 - public static DateTime GetFileCreationTime(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.CreationTime; - } - - /// - /// 获取文件的修改时间 - /// - /// 文件路径 - /// 文件的修改时间 - public static DateTime GetFileLastWriteTime(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.LastWriteTime; - } - - /// - /// 复制文件 - /// [Obsolete("请直接使用 File.Copy(sourcePath, destinationPath, overwrite)")] - /// - /// 源文件路径 - /// 目标文件路径 - /// 是否覆盖已有文件 - [Obsolete("请直接使用 File.Copy(sourcePath, destinationPath, overwrite)", false)] - public static void CopyFile(string sourcePath, string destinationPath, bool overwrite) - { - File.Copy(sourcePath, destinationPath, overwrite); - } - - /// - /// 移动文件 - /// [Obsolete("请直接使用 File.Move(sourcePath, destinationPath)")] - /// - /// 源文件路径 - /// 目标文件路径 - [Obsolete("请直接使用 File.Move(sourcePath, destinationPath)", false)] - public static void MoveFile(string sourcePath, string destinationPath) - { - File.Move(sourcePath, destinationPath); - } - - /// - /// 获取网络时间 - /// - /// 网络时间 - public static DateTime GetNetworkTime() - { - const string ntpServer = "time.windows.com"; - byte[] ntpData = new byte[48]; - ntpData[0] = 0x1B; - IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList; - IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123); - using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) - { - socket.Connect(ipEndPoint); - socket.Send(ntpData); - socket.Receive(ntpData); - } - const byte offsetTransmitTime = 40; - ulong intPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime); - ulong fractPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime + 4); - ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); - return new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds).ToLocalTime(); - } - - /// - /// 判断当前系统是否为Windows操作系统 - /// - /// 当前系统是否为Windows操作系统 - public static bool IsWindows() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - } - - /// - /// 判断当前系统是否为Linux操作系统 - /// - /// 当前系统是否为Linux操作系统 - public static bool IsLinux() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); - } - - /// - /// 判断当前系统是否为macOS操作系统 - /// - /// 当前系统是否为macOS操作系统 - public static bool IsMacOS() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - } - } -} diff --git a/EasyTool.Core/ToolCategory/ExceptionExtension.cs b/EasyTool.Core/ToolCategory/ExceptionExtension.cs index 91824bc..325b319 100644 --- a/EasyTool.Core/ToolCategory/ExceptionExtension.cs +++ b/EasyTool.Core/ToolCategory/ExceptionExtension.cs @@ -3,7 +3,7 @@ using System.Text; using System.Linq; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Exception 异常扩展方法 diff --git a/EasyTool.Core/ToolCategory/GuidExtension.cs b/EasyTool.Core/ToolCategory/GuidExtension.cs index 39f97fd..656fd54 100644 --- a/EasyTool.Core/ToolCategory/GuidExtension.cs +++ b/EasyTool.Core/ToolCategory/GuidExtension.cs @@ -1,7 +1,7 @@ using System; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Guid 扩展方法 diff --git a/EasyTool.Core/ToolCategory/IdUtil.cs b/EasyTool.Core/ToolCategory/IdUtil.cs index 64edf2a..6470694 100644 --- a/EasyTool.Core/ToolCategory/IdUtil.cs +++ b/EasyTool.Core/ToolCategory/IdUtil.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; -namespace EasyTool +namespace EasyTool.ToolCategory { /// /// uuid生成风格 @@ -27,7 +27,7 @@ public enum UUIDStyle /// /// 唯一ID工具 /// - public class IdUtil + public static class IdUtil { private static readonly DateTime epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static int objectIdCounter = 0; diff --git a/EasyTool.Core/ToolCategory/ObjectExtension.cs b/EasyTool.Core/ToolCategory/ObjectExtension.cs index 8513661..a22e623 100644 --- a/EasyTool.Core/ToolCategory/ObjectExtension.cs +++ b/EasyTool.Core/ToolCategory/ObjectExtension.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; using System.Xml.Serialization; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Object 对象扩展方法 diff --git a/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs b/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs index 5bf41fb..0e78d29 100644 --- a/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs +++ b/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Reflection; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// PropertyInfo 扩展方法 diff --git a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs index 2c5572d..1c6796d 100644 --- a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs +++ b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; @@ -7,7 +7,8 @@ using System.Text; using System.Text.Json; -namespace EasyTool; +namespace EasyTool.ToolCategory +{ /// /// 简单实体转化拓展类 /// @@ -92,7 +93,6 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) } } } - if (expr == null) continue; assignments.Add(Expression.Bind(destinationProp, expr)); } @@ -105,6 +105,7 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return Expression.Lambda(bodyExpr, srcExpr).Compile(); } + /// /// 简单实体转化 /// 目标泛型需要默认构造函数 @@ -120,8 +121,9 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return mapper(source); } + /// - /// 集合实体转化,需要目标泛型具有默认构造函数 + /// 融合实体转化,需要目标泛型具有默认构造函数 /// /// 源泛型 /// 目标泛型 @@ -131,6 +133,7 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) { if (!sources.Any()) return Enumerable.Empty(); + var mapper = (Func)mapDelegateCache.GetOrAdd(new(typeof(TSource), typeof(TDestination)), static key => BuildSimpleMapDelegate(key.SrcType, key.DestType)); List result = new(); @@ -141,3 +144,4 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return result; } } +} diff --git a/EasyTool.Core/ToolCategory/StrExtension.cs b/EasyTool.Core/ToolCategory/StrExtension.cs index a5fd201..06dd0b1 100644 --- a/EasyTool.Core/ToolCategory/StrExtension.cs +++ b/EasyTool.Core/ToolCategory/StrExtension.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// 字符串扩展方法 diff --git a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs index bbeeadc..8de02ad 100644 --- a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs +++ b/EasyTool.Core/ToolCategory/StringBuilderExtension.cs @@ -2,7 +2,7 @@ using System.IO; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// StringBuilder 扩展方法 diff --git a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs index b81c809..8274e77 100644 --- a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs +++ b/EasyTool.Core/ToolCategory/StringComparisonExtension.cs @@ -1,7 +1,7 @@ using System; using System.Globalization; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// String 字符串比较扩展方法 diff --git a/EasyTool.Core/ToolCategory/TaskExtension.cs b/EasyTool.Core/ToolCategory/TaskExtension.cs index c3e0312..1515de6 100644 --- a/EasyTool.Core/ToolCategory/TaskExtension.cs +++ b/EasyTool.Core/ToolCategory/TaskExtension.cs @@ -4,7 +4,7 @@ using System.Threading; using System.Threading.Tasks; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Task 异步任务扩展方法 diff --git a/EasyTool.Core/ToolCategory/TypeExtension.cs b/EasyTool.Core/ToolCategory/TypeExtension.cs index 547511b..5e1edff 100644 --- a/EasyTool.Core/ToolCategory/TypeExtension.cs +++ b/EasyTool.Core/ToolCategory/TypeExtension.cs @@ -5,7 +5,7 @@ using System.Reflection; using System.Runtime.CompilerServices; -namespace EasyTool.Extension +namespace EasyTool.ToolCategory { /// /// Type 类型扩展方法 diff --git a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs index df44f68..688b659 100644 --- a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs +++ b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs @@ -1,4 +1,4 @@ -using EasyTool.Extension; +using EasyTool.ToolCategory; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace EasyTool.Tests diff --git a/EasyTool.CoreTests/MathCategory/MathUtilTests.cs b/EasyTool.CoreTests/MathCategory/MathUtilTests.cs index 9a5598b..f56a81d 100644 --- a/EasyTool.CoreTests/MathCategory/MathUtilTests.cs +++ b/EasyTool.CoreTests/MathCategory/MathUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.MathCategory; using System; using System.Collections.Generic; using System.Text; diff --git a/EasyTool.CoreTests/Standardization/OptionTests.cs b/EasyTool.CoreTests/Standardization/OptionTests.cs index 9b79407..44237da 100644 --- a/EasyTool.CoreTests/Standardization/OptionTests.cs +++ b/EasyTool.CoreTests/Standardization/OptionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.Standardization; using System; using System.Collections.Generic; using System.Linq; diff --git a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs index 7e796cf..ca2635a 100644 --- a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs +++ b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.ToolCategory; using System; using System.Collections.Generic; using System.Linq; diff --git a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs b/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs index f54d0a5..5714715 100644 --- a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs +++ b/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs @@ -1,6 +1,6 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.NetCategory; namespace EasyTool.Tests { diff --git a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs index 6349bed..175b94d 100644 --- a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs +++ b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.ToolCategory; using System; using System.Collections.Generic; From 388ab24bb1846fc2d14f9dbfcd947d65a5cfa08a Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 16:58:49 +0800 Subject: [PATCH 05/19] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=88=86=E7=B1=BB=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 PropertyInfoExtension 和 TypeExtension 移动到 ReflectCategory,修正命名空间 - 将 StrExtension 移动到 TextCategory 并重命名为 StringExtension - 修复 HashUtil 命名空间为 EasyTool.CodeCategory - 改进代码组织,将相关功能归类到对应目录 --- EasyTool.Core/CodeCategory/HashUtil.cs | 2 +- .../{ToolCategory => ReflectCategory}/PropertyInfoExtension.cs | 2 +- .../{ToolCategory => ReflectCategory}/TypeExtension.cs | 2 +- .../StrExtension.cs => TextCategory/StringExtension.cs} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename EasyTool.Core/{ToolCategory => ReflectCategory}/PropertyInfoExtension.cs (99%) rename EasyTool.Core/{ToolCategory => ReflectCategory}/TypeExtension.cs (99%) rename EasyTool.Core/{ToolCategory/StrExtension.cs => TextCategory/StringExtension.cs} (99%) diff --git a/EasyTool.Core/CodeCategory/HashUtil.cs b/EasyTool.Core/CodeCategory/HashUtil.cs index 7202d3a..b6915f4 100644 --- a/EasyTool.Core/CodeCategory/HashUtil.cs +++ b/EasyTool.Core/CodeCategory/HashUtil.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// /// hash算法工具类 diff --git a/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs b/EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/PropertyInfoExtension.cs rename to EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs index 0e78d29..da4d5ed 100644 --- a/EasyTool.Core/ToolCategory/PropertyInfoExtension.cs +++ b/EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Reflection; -namespace EasyTool.ToolCategory +namespace EasyTool.ReflectCategory { /// /// PropertyInfo 扩展方法 diff --git a/EasyTool.Core/ToolCategory/TypeExtension.cs b/EasyTool.Core/ReflectCategory/TypeExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/TypeExtension.cs rename to EasyTool.Core/ReflectCategory/TypeExtension.cs index 5e1edff..a943f54 100644 --- a/EasyTool.Core/ToolCategory/TypeExtension.cs +++ b/EasyTool.Core/ReflectCategory/TypeExtension.cs @@ -5,7 +5,7 @@ using System.Reflection; using System.Runtime.CompilerServices; -namespace EasyTool.ToolCategory +namespace EasyTool.ReflectCategory { /// /// Type 类型扩展方法 diff --git a/EasyTool.Core/ToolCategory/StrExtension.cs b/EasyTool.Core/TextCategory/StringExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/StrExtension.cs rename to EasyTool.Core/TextCategory/StringExtension.cs index 06dd0b1..4592ec0 100644 --- a/EasyTool.Core/ToolCategory/StrExtension.cs +++ b/EasyTool.Core/TextCategory/StringExtension.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool.ToolCategory +namespace EasyTool.TextCategory { /// /// 字符串扩展方法 From b5a3f54f5e60ab2375df8e7f4a81732d25c34cee Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:00:04 +0800 Subject: [PATCH 06/19] =?UTF-8?q?refactor:=20=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=88=86=E7=B1=BB=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 CreditCodeUtil 移动到 SecurityCategory(业务验证工具) - 将 StringComparisonExtension 移动到 TextCategory(字符串相关) - 减少 ToolCategory 中不相关的文件数量 - 改进代码组织的逻辑性 --- .../{ToolCategory => SecurityCategory}/CreditCodeUtil.cs | 0 .../{ToolCategory => TextCategory}/StringComparisonExtension.cs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename EasyTool.Core/{ToolCategory => SecurityCategory}/CreditCodeUtil.cs (100%) rename EasyTool.Core/{ToolCategory => TextCategory}/StringComparisonExtension.cs (100%) diff --git a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs b/EasyTool.Core/SecurityCategory/CreditCodeUtil.cs similarity index 100% rename from EasyTool.Core/ToolCategory/CreditCodeUtil.cs rename to EasyTool.Core/SecurityCategory/CreditCodeUtil.cs diff --git a/EasyTool.Core/ToolCategory/StringComparisonExtension.cs b/EasyTool.Core/TextCategory/StringComparisonExtension.cs similarity index 100% rename from EasyTool.Core/ToolCategory/StringComparisonExtension.cs rename to EasyTool.Core/TextCategory/StringComparisonExtension.cs From 1894b8ab687f8a12a3f354d2ec7d138096320d54 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:02:23 +0800 Subject: [PATCH 07/19] =?UTF-8?q?refactor:=20=E5=90=88=E5=B9=B6=20IEnumera?= =?UTF-8?q?bleCategory=20=E5=88=B0=20CollectionsCategory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 IEnumerableExtensions.cs 移动到 CollectionsCategory - 删除空的 IEnumerableCategory 文件夹 - 统一集合相关扩展到同一分类目录 --- .../IEnumerableExtensions.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename EasyTool.Core/{IEnumerableCategory => CollectionsCategory}/IEnumerableExtensions.cs (100%) diff --git a/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs similarity index 100% rename from EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs rename to EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs From 09bdb70651fd67a1fcb5eb94468319590b4717d1 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:04:08 +0800 Subject: [PATCH 08/19] =?UTF-8?q?refactor:=20=E6=89=A9=E5=B1=95=20DataCate?= =?UTF-8?q?gory=20=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 SimpleMapExtension 从 ToolCategory 移动到 DataCategory - 将 IdUtil 从 ToolCategory 移动到 DataCategory - 将 CreditCodeUtil 从 SecurityCategory 移动到 DataCategory - DataCategory 现在包含 4 个文件:PageUtil、SimpleMapExtension、IdUtil、CreditCodeUtil - 修复测试项目的命名空间引用 --- .../{SecurityCategory => DataCategory}/CreditCodeUtil.cs | 2 +- EasyTool.Core/{ToolCategory => DataCategory}/IdUtil.cs | 2 +- .../{ToolCategory => DataCategory}/SimpleMapExtension.cs | 2 +- EasyTool.CoreTests/ToolCategory/IDUtilTests.cs | 2 +- EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename EasyTool.Core/{SecurityCategory => DataCategory}/CreditCodeUtil.cs (99%) rename EasyTool.Core/{ToolCategory => DataCategory}/IdUtil.cs (99%) rename EasyTool.Core/{ToolCategory => DataCategory}/SimpleMapExtension.cs (99%) diff --git a/EasyTool.Core/SecurityCategory/CreditCodeUtil.cs b/EasyTool.Core/DataCategory/CreditCodeUtil.cs similarity index 99% rename from EasyTool.Core/SecurityCategory/CreditCodeUtil.cs rename to EasyTool.Core/DataCategory/CreditCodeUtil.cs index 21d8167..754279a 100644 --- a/EasyTool.Core/SecurityCategory/CreditCodeUtil.cs +++ b/EasyTool.Core/DataCategory/CreditCodeUtil.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool.ToolCategory +namespace EasyTool.DataCategory { /// /// 社会信用代码工具 diff --git a/EasyTool.Core/ToolCategory/IdUtil.cs b/EasyTool.Core/DataCategory/IdUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/IdUtil.cs rename to EasyTool.Core/DataCategory/IdUtil.cs index 6470694..ac01bb3 100644 --- a/EasyTool.Core/ToolCategory/IdUtil.cs +++ b/EasyTool.Core/DataCategory/IdUtil.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; -namespace EasyTool.ToolCategory +namespace EasyTool.DataCategory { /// /// uuid生成风格 diff --git a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs b/EasyTool.Core/DataCategory/SimpleMapExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/SimpleMapExtension.cs rename to EasyTool.Core/DataCategory/SimpleMapExtension.cs index 1c6796d..98ae5f4 100644 --- a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs +++ b/EasyTool.Core/DataCategory/SimpleMapExtension.cs @@ -7,7 +7,7 @@ using System.Text; using System.Text.Json; -namespace EasyTool.ToolCategory +namespace EasyTool.DataCategory { /// /// 简单实体转化拓展类 diff --git a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs index ca2635a..c11c40c 100644 --- a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs +++ b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.ToolCategory; +using EasyTool.DataCategory; using System; using System.Collections.Generic; using System.Linq; diff --git a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs index 175b94d..e24118f 100644 --- a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs +++ b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.ToolCategory; +using EasyTool.DataCategory; using System; using System.Collections.Generic; From 6509fa879c0b208fc626e22c3865aba263f84da4 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:06:43 +0800 Subject: [PATCH 09/19] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=B1=BB=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 StringBuilderExtension 从 ToolCategory 移动到 TextCategory - 将 NumberExtension 从 ConvertCategory 移动到 MathCategory - 修复 FileSystemExtension 对 ToFileSize 的命名空间引用(ConvertCategory → MathCategory) - MathCategory 现在包含 4 个文件:MathUtil、PredictUtil、RandomUtil、NumberExtension - TextCategory 现在包含 7 个文件 --- EasyTool.Core/IOCategory/FileSystemExtension.cs | 2 +- .../{ConvertCategory => MathCategory}/NumberExtension.cs | 2 +- .../{ToolCategory => TextCategory}/StringBuilderExtension.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename EasyTool.Core/{ConvertCategory => MathCategory}/NumberExtension.cs (99%) rename EasyTool.Core/{ToolCategory => TextCategory}/StringBuilderExtension.cs (99%) diff --git a/EasyTool.Core/IOCategory/FileSystemExtension.cs b/EasyTool.Core/IOCategory/FileSystemExtension.cs index 4fe44fa..7d214fa 100644 --- a/EasyTool.Core/IOCategory/FileSystemExtension.cs +++ b/EasyTool.Core/IOCategory/FileSystemExtension.cs @@ -1,7 +1,7 @@ using System; using System.IO; using System.Linq; -using EasyTool.ConvertCategory; +using EasyTool.MathCategory; namespace EasyTool.IOCategory { diff --git a/EasyTool.Core/ConvertCategory/NumberExtension.cs b/EasyTool.Core/MathCategory/NumberExtension.cs similarity index 99% rename from EasyTool.Core/ConvertCategory/NumberExtension.cs rename to EasyTool.Core/MathCategory/NumberExtension.cs index dcc047a..07fe0e4 100644 --- a/EasyTool.Core/ConvertCategory/NumberExtension.cs +++ b/EasyTool.Core/MathCategory/NumberExtension.cs @@ -1,6 +1,6 @@ using System; -namespace EasyTool.ConvertCategory +namespace EasyTool.MathCategory { /// /// 数字类型扩展方法 diff --git a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs b/EasyTool.Core/TextCategory/StringBuilderExtension.cs similarity index 99% rename from EasyTool.Core/ToolCategory/StringBuilderExtension.cs rename to EasyTool.Core/TextCategory/StringBuilderExtension.cs index 8de02ad..d8a5610 100644 --- a/EasyTool.Core/ToolCategory/StringBuilderExtension.cs +++ b/EasyTool.Core/TextCategory/StringBuilderExtension.cs @@ -2,7 +2,7 @@ using System.IO; using System.Text; -namespace EasyTool.ToolCategory +namespace EasyTool.TextCategory { /// /// StringBuilder 扩展方法 From 596ffa45941e98eeadf2638584a75ab96906eb02 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:09:40 +0800 Subject: [PATCH 10/19] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=B1=BB=E7=BB=93=E6=9E=84=EF=BC=88=E6=96=B9?= =?UTF-8?q?=E6=A1=88A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建新的 ImageCategory,将 ColorExtension 从 ConvertCategory 移入 - 将 EmojiUtil 从 EmojiCategory 移动到 TextCategory(表情符号是文本处理) - 将 DesensitizedUtil 从 SecurityCategory 移动到 TextCategory(脱敏是文本处理) - 删除空的 EmojiCategory 和 SecurityCategory 文件夹 - ImageCategory 现在包含 ColorExtension - TextCategory 现在包含 9 个文件(包含表情和脱敏) - ConvertCategory 减少到 3 个文件 --- .../{ConvertCategory => ImageCategory}/ColorExtension.cs | 2 +- .../{SecurityCategory => TextCategory}/DesensitizedUtil.cs | 2 +- EasyTool.Core/{EmojiCategory => TextCategory}/EmojiUtil.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename EasyTool.Core/{ConvertCategory => ImageCategory}/ColorExtension.cs (99%) rename EasyTool.Core/{SecurityCategory => TextCategory}/DesensitizedUtil.cs (99%) rename EasyTool.Core/{EmojiCategory => TextCategory}/EmojiUtil.cs (98%) diff --git a/EasyTool.Core/ConvertCategory/ColorExtension.cs b/EasyTool.Core/ImageCategory/ColorExtension.cs similarity index 99% rename from EasyTool.Core/ConvertCategory/ColorExtension.cs rename to EasyTool.Core/ImageCategory/ColorExtension.cs index b11f7d7..69abae2 100644 --- a/EasyTool.Core/ConvertCategory/ColorExtension.cs +++ b/EasyTool.Core/ImageCategory/ColorExtension.cs @@ -1,7 +1,7 @@ using System; using System.Drawing; -namespace EasyTool.ConvertCategory +namespace EasyTool.ImageCategory { /// /// Color 颜色扩展方法 diff --git a/EasyTool.Core/SecurityCategory/DesensitizedUtil.cs b/EasyTool.Core/TextCategory/DesensitizedUtil.cs similarity index 99% rename from EasyTool.Core/SecurityCategory/DesensitizedUtil.cs rename to EasyTool.Core/TextCategory/DesensitizedUtil.cs index 3873756..a1b38c8 100644 --- a/EasyTool.Core/SecurityCategory/DesensitizedUtil.cs +++ b/EasyTool.Core/TextCategory/DesensitizedUtil.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool.SecurityCategory +namespace EasyTool.TextCategory { /// /// 信息脱敏工具类 diff --git a/EasyTool.Core/EmojiCategory/EmojiUtil.cs b/EasyTool.Core/TextCategory/EmojiUtil.cs similarity index 98% rename from EasyTool.Core/EmojiCategory/EmojiUtil.cs rename to EasyTool.Core/TextCategory/EmojiUtil.cs index 615ba06..a0f15db 100644 --- a/EasyTool.Core/EmojiCategory/EmojiUtil.cs +++ b/EasyTool.Core/TextCategory/EmojiUtil.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool.EmojiCategory +namespace EasyTool.TextCategory { public static class EmojiUtil { From cc874566c06b26efa09cbf7dbfa48dece9d8557a Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:15:26 +0800 Subject: [PATCH 11/19] =?UTF-8?q?refactor:=20=E6=89=A7=E8=A1=8C=E6=96=B9?= =?UTF-8?q?=E6=A1=88A=20-=20=E5=85=A8=E9=9D=A2=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=B1=BB=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 标记 ByteExtension.ToHex/ToHexLower 为 Obsolete(与 HexUtil 重复) - 创建 IdentifierCategory,将 IdUtil 从 DataCategory 移入 - 创建 BusinessCategory,将 CreditCodeUtil 从 DataCategory 移入 - 将 SimpleMapExtension 移回 ToolCategory - 重命名 ImageCategory 为 ColorCategory(更准确) - 修复所有命名空间引用 - 修复测试项目的命名空间引用 最终分类结构: - CodeCategory(5), CollectionsCategory(7), ConvertCategory(3), DataCategory(2) - DateTimeCategory(4), ColorCategory(1), IOCategory(7), MathCategory(4) - NetCategory(3), ReflectCategory(3), Standardization(3), SystemCategory(2) - TextCategory(7), ToolCategory(7), IdentifierCategory(1), BusinessCategory(1) --- .../{DataCategory => BusinessCategory}/CreditCodeUtil.cs | 2 +- .../{ImageCategory => ColorCategory}/ColorExtension.cs | 2 +- EasyTool.Core/ConvertCategory/ByteExtension.cs | 4 +++- EasyTool.Core/{DataCategory => IdentifierCategory}/IdUtil.cs | 2 +- .../{DataCategory => ToolCategory}/SimpleMapExtension.cs | 2 +- EasyTool.CoreTests/ToolCategory/IDUtilTests.cs | 2 +- EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs | 2 +- 7 files changed, 9 insertions(+), 7 deletions(-) rename EasyTool.Core/{DataCategory => BusinessCategory}/CreditCodeUtil.cs (99%) rename EasyTool.Core/{ImageCategory => ColorCategory}/ColorExtension.cs (99%) rename EasyTool.Core/{DataCategory => IdentifierCategory}/IdUtil.cs (99%) rename EasyTool.Core/{DataCategory => ToolCategory}/SimpleMapExtension.cs (99%) diff --git a/EasyTool.Core/DataCategory/CreditCodeUtil.cs b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs similarity index 99% rename from EasyTool.Core/DataCategory/CreditCodeUtil.cs rename to EasyTool.Core/BusinessCategory/CreditCodeUtil.cs index 754279a..c96ed02 100644 --- a/EasyTool.Core/DataCategory/CreditCodeUtil.cs +++ b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool.DataCategory +namespace EasyTool.BusinessCategory { /// /// 社会信用代码工具 diff --git a/EasyTool.Core/ImageCategory/ColorExtension.cs b/EasyTool.Core/ColorCategory/ColorExtension.cs similarity index 99% rename from EasyTool.Core/ImageCategory/ColorExtension.cs rename to EasyTool.Core/ColorCategory/ColorExtension.cs index 69abae2..9115b2f 100644 --- a/EasyTool.Core/ImageCategory/ColorExtension.cs +++ b/EasyTool.Core/ColorCategory/ColorExtension.cs @@ -1,7 +1,7 @@ using System; using System.Drawing; -namespace EasyTool.ImageCategory +namespace EasyTool.ColorCategory { /// /// Color 颜色扩展方法 diff --git a/EasyTool.Core/ConvertCategory/ByteExtension.cs b/EasyTool.Core/ConvertCategory/ByteExtension.cs index 13d4829..2326420 100644 --- a/EasyTool.Core/ConvertCategory/ByteExtension.cs +++ b/EasyTool.Core/ConvertCategory/ByteExtension.cs @@ -13,8 +13,9 @@ public static class ByteExtension #region 单字节转换 /// - /// 将字节转换为16进制字符串 + /// 将字节转换为16进制字符串(已移除,使用 CodeCategory/HexUtil.ToHex) /// + [Obsolete("请使用 CodeCategory.HexUtil.ToHex 方法", true)] public static string ToHex(this byte value) { return value.ToString("X2"); @@ -23,6 +24,7 @@ public static string ToHex(this byte value) /// /// 将字节转换为16进制字符串(小写) /// + [Obsolete("请使用 CodeCategory.HexUtil.ToHexLower 方法", true)] public static string ToHexLower(this byte value) { return value.ToString("x2"); diff --git a/EasyTool.Core/DataCategory/IdUtil.cs b/EasyTool.Core/IdentifierCategory/IdUtil.cs similarity index 99% rename from EasyTool.Core/DataCategory/IdUtil.cs rename to EasyTool.Core/IdentifierCategory/IdUtil.cs index ac01bb3..393e887 100644 --- a/EasyTool.Core/DataCategory/IdUtil.cs +++ b/EasyTool.Core/IdentifierCategory/IdUtil.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; -namespace EasyTool.DataCategory +namespace EasyTool.IdentifierCategory { /// /// uuid生成风格 diff --git a/EasyTool.Core/DataCategory/SimpleMapExtension.cs b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs similarity index 99% rename from EasyTool.Core/DataCategory/SimpleMapExtension.cs rename to EasyTool.Core/ToolCategory/SimpleMapExtension.cs index 98ae5f4..1c6796d 100644 --- a/EasyTool.Core/DataCategory/SimpleMapExtension.cs +++ b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs @@ -7,7 +7,7 @@ using System.Text; using System.Text.Json; -namespace EasyTool.DataCategory +namespace EasyTool.ToolCategory { /// /// 简单实体转化拓展类 diff --git a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs index c11c40c..694d834 100644 --- a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs +++ b/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.DataCategory; +using EasyTool.IdentifierCategory; using System; using System.Collections.Generic; using System.Linq; diff --git a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs index e24118f..175b94d 100644 --- a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs +++ b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.DataCategory; +using EasyTool.ToolCategory; using System; using System.Collections.Generic; From e8715d774f69f2fca4746fa5b289b97aa97d1d12 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:21:30 +0800 Subject: [PATCH 12/19] =?UTF-8?q?refactor:=20=E6=9C=80=E7=BB=88=E6=B8=85?= =?UTF-8?q?=E7=90=86=E5=92=8C=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 ConvertCategory/ByteExtension.cs(仅含 Obsolete 方法,与 HexUtil 重复) - 将 PageUtil 从 DataCategory 移动到 ToolCategory - 删除空的 DataCategory 文件夹 最终分类结构(15个分类,55个文件): - BusinessCategory(1), CodeCategory(5), CollectionsCategory(7), ColorCategory(1) - ConvertCategory(2), DateTimeCategory(4), IdentifierCategory(1), IOCategory(7) - MathCategory(4), NetCategory(3), ReflectCategory(3), Standardization(3) - SystemCategory(2), TextCategory(9), ToolCategory(8) --- .../ConvertCategory/ByteExtension.cs | 508 ------------------ .../PageUtil.cs | 0 2 files changed, 508 deletions(-) delete mode 100644 EasyTool.Core/ConvertCategory/ByteExtension.cs rename EasyTool.Core/{DataCategory => ToolCategory}/PageUtil.cs (100%) diff --git a/EasyTool.Core/ConvertCategory/ByteExtension.cs b/EasyTool.Core/ConvertCategory/ByteExtension.cs deleted file mode 100644 index 2326420..0000000 --- a/EasyTool.Core/ConvertCategory/ByteExtension.cs +++ /dev/null @@ -1,508 +0,0 @@ -using System; -using System.IO; -using System.Text; -using System.Linq; - -namespace EasyTool.ConvertCategory -{ - /// - /// Byte 字节扩展方法 - /// - public static class ByteExtension - { - #region 单字节转换 - - /// - /// 将字节转换为16进制字符串(已移除,使用 CodeCategory/HexUtil.ToHex) - /// - [Obsolete("请使用 CodeCategory.HexUtil.ToHex 方法", true)] - public static string ToHex(this byte value) - { - return value.ToString("X2"); - } - - /// - /// 将字节转换为16进制字符串(小写) - /// - [Obsolete("请使用 CodeCategory.HexUtil.ToHexLower 方法", true)] - public static string ToHexLower(this byte value) - { - return value.ToString("x2"); - } - - /// - /// 将字节转换为二进制字符串 - /// - public static string ToBinaryString(this byte value) - { - return Convert.ToString(value, 2).PadLeft(8, '0'); - } - - /// - /// 获取字节的指定位 - /// - public static bool GetBit(this byte value, int index) - { - if (index < 0 || index > 7) - throw new ArgumentOutOfRangeException(nameof(index), "Index must be between 0 and 7"); - - return (value & (1 << index)) != 0; - } - - /// - /// 设置字节的指定位 - /// - public static byte SetBit(this byte value, int index, bool bitValue) - { - if (index < 0 || index > 7) - throw new ArgumentOutOfRangeException(nameof(index), "Index must be between 0 and 7"); - - if (bitValue) - return (byte)(value | (1 << index)); - else - return (byte)(value & ~(1 << index)); - } - - #endregion - - #region 字节数组转换 - - /// - /// 将字节数组转换为16进制字符串 - /// - public static string ToHex(this byte[] bytes) - { - return bytes.ToHex(true); - } - - /// - /// 将字节数组转换为16进制字符串 - /// - public static string ToHex(this byte[] bytes, bool uppercase) - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - var format = uppercase ? "X2" : "x2"; - var sb = new StringBuilder(bytes.Length * 2); - - foreach (var b in bytes) - { - sb.Append(b.ToString(format)); - } - - return sb.ToString(); - } - - /// - /// 将字节数组转换为16进制字符串(带分隔符) - /// - public static string ToHex(this byte[] bytes, string separator, bool uppercase = true) - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - var format = uppercase ? "X2" : "x2"; - return string.Join(separator, bytes.Select(b => b.ToString(format))); - } - - /// - /// 从16进制字符串转换为字节数组 - /// - public static byte[] FromHexToBytes(this string hex) - { - if (string.IsNullOrWhiteSpace(hex)) - return Array.Empty(); - - hex = hex.Replace("-", "").Replace(" ", ""); - - if (hex.Length % 2 != 0) - throw new ArgumentException("Hex string must have an even length", nameof(hex)); - - var bytes = new byte[hex.Length / 2]; - - for (int i = 0; i < bytes.Length; i++) - { - bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); - } - - return bytes; - } - - - /// - /// 将字节数组转换为二进制字符串 - /// - public static string ToBinaryString(this byte[] bytes, string separator = " ") - { - if (bytes == null || bytes.Length == 0) - return string.Empty; - - return string.Join(separator, bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0'))); - } - - /// - /// 从二进制字符串转换为字节数组 - /// - public static byte[] FromBinaryStringToBytes(this string binary) - { - if (string.IsNullOrWhiteSpace(binary)) - return Array.Empty(); - - binary = binary.Replace(" ", ""); - - if (binary.Length % 8 != 0) - throw new ArgumentException("Binary string length must be a multiple of 8", nameof(binary)); - - var bytes = new byte[binary.Length / 8]; - - for (int i = 0; i < bytes.Length; i++) - { - bytes[i] = Convert.ToByte(binary.Substring(i * 8, 8), 2); - } - - return bytes; - } - - #endregion - - #region 字节数组操作 - - /// - /// 反转字节数组 - /// - public static byte[]? Reverse(this byte[]? bytes) - { - if (bytes == null) - return null; - - var result = new byte[bytes.Length]; - Array.Copy(bytes, result, bytes.Length); - Array.Reverse(result); - return result; - } - - /// - /// 字节数组异或运算 - /// - public static byte[] Xor(this byte[] bytes, byte[] key) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - if (key == null) - throw new ArgumentNullException(nameof(key)); - - var result = new byte[bytes.Length]; - - for (int i = 0; i < bytes.Length; i++) - { - result[i] = (byte)(bytes[i] ^ key[i % key.Length]); - } - - return result; - } - - /// - /// 字节数组异或运算(单字节密钥) - /// - public static byte[] Xor(this byte[] bytes, byte key) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - var result = new byte[bytes.Length]; - - for (int i = 0; i < bytes.Length; i++) - { - result[i] = (byte)(bytes[i] ^ key); - } - - return result; - } - - /// - /// 合并多个字节数组 - /// - public static byte[] Combine(params byte[][]? arrays) - { - if (arrays == null || arrays.Length == 0) - return Array.Empty(); - - int totalLength = 0; - foreach (var arr in arrays) - { - if (arr != null) - totalLength += arr.Length; - } - - var result = new byte[totalLength]; - int offset = 0; - - foreach (var arr in arrays) - { - if (arr != null && arr.Length > 0) - { - Array.Copy(arr, 0, result, offset, arr.Length); - offset += arr.Length; - } - } - - return result; - } - - /// - /// 截取字节数组 - /// - public static byte[] SubArray(this byte[] bytes, int startIndex, int length) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if (startIndex < 0 || startIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(startIndex)); - - if (length < 0 || startIndex + length > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(length)); - - var result = new byte[length]; - Array.Copy(bytes, startIndex, result, 0, length); - return result; - } - - /// - /// 截取字节数组(从指定位置到末尾) - /// - public static byte[] SubArray(this byte[] bytes, int startIndex) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if (startIndex < 0) - startIndex = 0; - - if (startIndex >= bytes.Length) - return Array.Empty(); - - return SubArray(bytes, startIndex, bytes.Length - startIndex); - } - - #endregion - - #region 字节数组比较 - - /// - /// 比较两个字节数组是否相等 - /// - public static bool EqualsTo(this byte[]? bytes, byte[]? other) - { - if (ReferenceEquals(bytes, other)) - return true; - - if (bytes == null || other == null) - return false; - - if (bytes.Length != other.Length) - return false; - - for (int i = 0; i < bytes.Length; i++) - { - if (bytes[i] != other[i]) - return false; - } - - return true; - } - - /// - /// 字节数组比较(返回差异索引) - /// - public static int[] Diff(this byte[]? bytes, byte[]? other) - { - if (bytes == null || other == null) - return Array.Empty(); - - var minLength = Math.Min(bytes.Length, other.Length); - var diffs = new System.Collections.Generic.List(); - - for (int i = 0; i < minLength; i++) - { - if (bytes[i] != other[i]) - diffs.Add(i); - } - - return diffs.ToArray(); - } - - #endregion - - #region 字节数组与基本类型转换 - - /// - /// 将字节数组转换为整数(小端序) - /// - public static int ToInt32(this byte[] bytes, int startIndex = 0) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if (startIndex < 0 || startIndex + 4 > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(startIndex)); - - return bytes[startIndex] | (bytes[startIndex + 1] << 8) | (bytes[startIndex + 2] << 16) | (bytes[startIndex + 3] << 24); - } - - /// - /// 将整数转换为字节数组(小端序) - /// - public static byte[] ToBytes(this int value) - { - return new[] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF), (byte)((value >> 16) & 0xFF), (byte)((value >> 24) & 0xFF) }; - } - - /// - /// 将字节数组转换为长整数(小端序) - /// - public static long ToInt64(this byte[] bytes, int startIndex = 0) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if (startIndex < 0 || startIndex + 8 > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(startIndex)); - - return BitConverter.ToInt64(bytes, startIndex); - } - - /// - /// 将长整数转换为字节数组(小端序) - /// - public static byte[] ToBytes(this long value) - { - return BitConverter.GetBytes(value); - } - - /// - /// 将字节数组转换为短整数(小端序) - /// - public static short ToInt16(this byte[] bytes, int startIndex = 0) - { - if (bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if (startIndex < 0 || startIndex + 2 > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(startIndex)); - - return (short)(bytes[startIndex] | (bytes[startIndex + 1] << 8)); - } - - /// - /// 将短整数转换为字节数组(小端序) - /// - public static byte[] ToBytes(this short value) - { - return new[] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF) }; - } - - #endregion - - #region 字节数组编码解码 - - - #endregion - - #region 字节数组压缩解压 - - /// - /// 压缩字节数组(使用 GZip) - /// - public static byte[]? Compress(this byte[]? bytes) - { - if (bytes == null || bytes.Length == 0) - return bytes; - - using var output = new MemoryStream(); - using (var gzip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress)) - { - gzip.Write(bytes, 0, bytes.Length); - } - return output.ToArray(); - } - - /// - /// 解压字节数组(使用 GZip) - /// - public static byte[]? Decompress(this byte[]? bytes) - { - if (bytes == null || bytes.Length == 0) - return bytes; - - using var input = new MemoryStream(bytes); - using var gzip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress); - using var output = new MemoryStream(); - gzip.CopyTo(output); - return output.ToArray(); - } - - #endregion - - #region 字节数组哈希 - - /// - /// 计算字节数组的 MD5 哈希值 - /// - public static byte[] ToMd5(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return Array.Empty(); - - using var md5 = System.Security.Cryptography.MD5.Create(); - return md5.ComputeHash(bytes); - } - - /// - /// 计算字节数组的 SHA1 哈希值 - /// - public static byte[] ToSha1(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return Array.Empty(); - - using var sha1 = System.Security.Cryptography.SHA1.Create(); - return sha1.ComputeHash(bytes); - } - - /// - /// 计算字节数组的 SHA256 哈希值 - /// - public static byte[] ToSha256(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return Array.Empty(); - - using var sha256 = System.Security.Cryptography.SHA256.Create(); - return sha256.ComputeHash(bytes); - } - - /// - /// 计算字节数组的 CRC32 校验值 - /// - public static uint ToCrc32(this byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - return 0; - - uint crc = 0xFFFFFFFF; - foreach (var b in bytes) - { - crc ^= b; - for (int i = 0; i < 8; i++) - { - crc = (crc >> 1) ^ ((crc & 1) == 1 ? 0xEDB88320 : 0); - } - } - return ~crc; - } - - #endregion - } -} diff --git a/EasyTool.Core/DataCategory/PageUtil.cs b/EasyTool.Core/ToolCategory/PageUtil.cs similarity index 100% rename from EasyTool.Core/DataCategory/PageUtil.cs rename to EasyTool.Core/ToolCategory/PageUtil.cs From 62a73caa3df4bd7886ad6e3f617738203614bd96 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 13 Feb 2026 17:30:01 +0800 Subject: [PATCH 13/19] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=BB=93=E6=9E=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 完善 README.md 项目结构说明 - 新增 15 个分类的详细说明和文件清单 - 记录今日所有优化历程和最终成果 - 更新日期:2025-02-13 --- README.md | 191 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 175 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d9e715d..cb10214 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,26 @@

EasyTool

-
- 一个开源的 .NET 工具库, 使得开发变得更加有效率 +
+
- -[![pull_request](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg)](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml) +[![pull_request](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg)](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg) [![](https://img.shields.io/nuget/v/EasyTool.Core.svg)](https://www.nuget.org/packages/EasyTool.Core)

- 中文 | English + 中文 | English

## 📚 简介 Easytool 是一个功能丰富且易用的 .NET 工具库,旨在帮助开发者快速、便捷地完成各类开发任务。 这些封装的工具涵盖了字符串、数字、集合、编码、日期、文件、IO、加密、JSON、HTTP客户端等一系列操作, 可以满足各种不同的开发需求。 + > [More information](https://easy-dotnet.com/pages/easytool/) -> + ## 🚀 快速开始 -### 安装 + +### 安装 + ~~~ PM> Install-Package EasyTool.Core ~~~ @@ -28,6 +30,7 @@ dotnet add package EasyTool.Core ~~~ ### 使用 + 复制文件或者目录 ~~~csharp FileUtil.Copy(sourceDir, destinationDir, isOverwrite) @@ -37,20 +40,176 @@ FileUtil.Copy(sourceDir, destinationDir, isOverwrite) var a = CloneUtil.Clone(person); ~~~ - ## 🛠️ 目录 + Easytool 封装了开发过程中一些常用的方法 -| Catalog | Introduce | -| --------------------------------------------------|---------------------------------------------------------------------------------- | -| [clone](EasyTool.Core/CloneCategory/) | 使用 CloneUtil.Clone 方法实现 .NET 对象的深度复制 | -| [code](EasyTool.Core/CodeCategory/) | 提供基于 base32, base62 等编解码工具 | +--- -## 代码共享 +## 📁 项目结构(最新更新:2025-02-13) + +EasyTool.Core 采用**模块化分类结构**,所有工具按功能领域清晰划分到 15 个分类目录中: + +### 📂 分类概览 + +| 分类 | 文件数 | 功能描述 | +|------|--------|----------| +| **BusinessCategory** | 1 | 业务数据处理(社会信用代码) | +| **CodeCategory** | 5 | 加密/编码工具(AES/DES/编码/哈希/十六进制) | +| **CollectionsCategory** | 7 | 集合扩展操作(数组/字典/链表/列表/队列/栈) | +| **ColorCategory** | 1 | 颜色处理扩展 | +| **ConvertCategory** | 1 | 数据类型转换工具 | +| **DateTimeCategory** | 4 | 日期时间处理(扩展/工具/日历/计时器) | +| **IdentifierCategory** | 1 | 标识符生成工具(UUID/ObjectId/Snowflake) | +| **IOCategory** | 7 | 文件/流/压缩操作(文件系统/文件类型/流/监控/ZIP) | +| **MathCategory** | 4 | 数学工具(计算/预测/随机数) | +| **NetCategory** | 3 | 网络工具(HTTP/IP/URL) | +| **ReflectCategory** | 3 | 反射/类型/属性扩展 | +| **Standardization** | 3 | 标准化类型(Option/QueryPage/Result) | +| **SystemCategory** | 2 | 系统环境工具(环境变量/系统信息) | +| **TextCategory** | 9 | 文本处理工具(正则/字符串/分割/XML/表情/脱敏) | +| **ToolCategory** | 8 | 通用扩展方法(委托/枚举/异常/GUID/对象/映射/任务/分页) | + +### 📋 各分类详细说明 + +#### **BusinessCategory** - 业务数据处理 +``` +CreditCodeUtil.cs - 中国社会信用代码的验证和处理工具 +``` + +#### **CodeCategory** - 加密/编码工具 +``` +AesUtil.cs - AES 加密/解密(支持 ECB/CBC 模式) +DesUtil.cs - DES 加密/解密(支持 ECB 模式) +EncodingUtil.cs - 编码转换工具 +HashUtil.cs - 17 种哈希算法(加法/旋转/Bernstein/FNV/DJB/BKDR 等) +HexUtil.cs - 十六进制转换工具 +``` +#### **CollectionsCategory** - 集合扩展操作 +``` +ArrayExtension.cs - 数组操作扩展 +DictionaryExtension.cs - 字典操作扩展 +IEnumerableExtensions.cs - IEnumerable 集合遍历扩展 +LinkedListUtil.cs - 链表操作工具 +ListExtension.cs - 列表操作扩展 +QueueUtil.cs - 队列操作工具 +StackUtil.cs - 栈操作工具 +``` -## 社区交流 +#### **ColorCategory** - 颜色处理 +``` +ColorExtension.cs - 颜色扩展(RGB/HSV/HEX 转换) +``` -**微信:ygdxg8657 (备注进群) QQ群:543829648 903210423(已满)** +#### **ConvertCategory** - 数据类型转换 +``` +ConvertExtension.cs - 通用数据类型转换(ToByte/ToShort/ToInt/ToLong/ToFloat/ToDouble/ToDecimal) +``` -![easy-tool](https://raw.githubusercontent.com/dotnet-easy/easy-dotnet/main/files/img/easytool.png) +#### **DateTimeCategory** - 日期时间处理 +``` +DateTimeExtension.cs - DateTime 类型扩展方法 +DateTimeUtil.cs - 日期时间工具类 +LunarCalendarUtil.cs - 农历工具 +TimerUtil.cs - 计时器工具 +``` + +#### **IdentifierCategory** - 标识符生成 +``` +IdUtil.cs - ID 生成工具(有序 UUID/ObjectId/Snowflake ID) +``` + +#### **IOCategory** - 文件/流/压缩 +``` +FileSystemExtension.cs - 文件系统操作扩展 +FileTypeExtension.cs - 文件类型判断 +FileUtil.cs - 文件操作工具 +StreamExtension.cs - 流操作扩展 +Tailer.cs - 文件尾部追踪工具 +WatchMonitor.cs - 文件监控工具 +ZipUtil.cs - ZIP 压缩工具 +``` + +#### **MathCategory** - 数学工具 +``` +MathUtil.cs - 数学计算工具 +NumberExtension.cs - 数字类型扩展(偶数/质数/二进制/十六进制) +PredictUtil.cs - 预测算法工具 +RandomUtil.cs - 随机数生成工具 +``` + +#### **NetCategory** - 网络工具 +``` +HttpClientExtension.cs - HttpClient 扩展 +IpUtil.cs - IP 地址处理工具 +URLUtil.cs - URL 处理工具 +``` + +#### **ReflectCategory** - 反射/类型/属性扩展 +``` +PropertyInfoExtension.cs - PropertyInfo 扩展(值获取/设置/特性检查) +ReflectUtil.cs - 反射工具类 +TypeExtension.cs - Type 类型扩展(类型判断/友好名称/默认值) +``` + +#### **Standardization** - 标准化类型 +``` +Option.cs - 选项对象(用于前端下拉) +QueryPage.cs - 分页查询对象 +Result.cs - 统一结果对象 +``` + +#### **SystemCategory** - 系统环境工具 +``` +EnvUtil.cs - 环境变量工具 +SystemUtil.cs - 系统信息工具 +``` + +#### **TextCategory** - 文本处理工具(9个文件) +``` +RegexUtil.cs - 正则表达式工具 +StringBuilderExtension.cs - StringBuilder 扩展 +StringComparisonExtension.cs - 字符串比较扩展 +StringExtension.cs - 字符串验证扩展(邮箱/手机/URL/身份证等) +StrSplitter.cs - 字符串分割工具 +StrUtil.cs - 字符串处理工具(命名转换/空格处理) +XmlUtil.cs - XML 处理工具 +EmojiUtil.cs - 表情符号处理工具 +DesensitizedUtil.cs - 数据脱敏工具(手机号/身份证/银行卡等) +``` + +#### **ToolCategory** - 通用扩展方法(8个文件) +``` +DelegateExtension.cs - 委托扩展(安全调用) +EnumExtension.cs - 枚举扩展(获取描述) +ExceptionExtension.cs - 异常扩展(获取完整异常信息) +GuidExtension.cs - Guid 扩展(空值判断) +ObjectExtension.cs - 对象扩展(深拷贝/JSON序列化) +SimpleMapExtension.cs - 简单对象映射扩展 +TaskExtension.cs - Task 异步扩展(Fire-and-Forget) +PageUtil.cs - 分页工具(支持多种数据源和排序方式) +``` + +--- + +### 📈 优化历程 + +本次更新主要完成了以下结构优化工作: + +1. ✅ **ReflectCategory 扩展** - PropertyInfoExtension、TypeExtension 移入 +2. ✅ **TextCategory 扩展** - StringComparisonExtension、StringExtension、EmojiUtil、DesensitizedUtil、StringBuilderExtension 移入 +3. ✅ **CollectionsCategory 扩展** - IEnumerableExtensions 合并 +4. ✅ **IdentifierCategory 新建** - ID 生成工具独立分类 +5. ✅ **BusinessCategory 新建** - 业务数据处理独立分类 +6. ✅ **ColorCategory 精简** - 颜色处理单独分类 +7. ✅ **ToolCategory 优化** - SimpleMapExtension、PageUtil 移入 +8. ✅ **空壳文件清理** - 删除仅含 Obsolete 方法的文件 + +**最终状态**:**15 个分类,55 个源文件**,结构清晰、功能明确、无重复代码。 + +--- + +> 项目采用模块化设计,每个分类职责单一,便于查找和维护。所有工具类都使用静态方法,无需实例化即可使用。 + +## 代码共享 From 5816b5b02a4a5b8bb4aa4ba3b0a89a355edc6314 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Sat, 14 Feb 2026 09:35:04 +0800 Subject: [PATCH 14/19] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=BB=93=E6=9E=84=E5=92=8C=E4=BF=AE=E5=A4=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 IEnumerableExtensions.cs 命名空间为 EasyTool.CollectionsCategory - 修复 PageUtil.cs 命名空间为 EasyTool.ToolCategory - 移动测试文件到正确的分类目录 (IdentifierCategory, NetCategory) - 为 EasyTool.NPOI 添加 OfficeCategory 分类目录 - 更新 GitHub Actions 到 actions@v4 和 .NET 8.0.x/10.0.x --- .github/workflows/nuget_pre.yml | 8 +++++--- .github/workflows/nuget_prod.yml | 8 +++++--- .github/workflows/pull_request.yml | 8 +++++--- .../CollectionsCategory/IEnumerableExtensions.cs | 2 +- EasyTool.Core/ToolCategory/PageUtil.cs | 2 +- .../{ToolCategory => IdentifierCategory}/IDUtilTests.cs | 0 .../{ToolCategory => NetCategory}/IpUtilTests.cs | 0 EasyTool.NPOI/{ => OfficeCategory}/ExcelWorkbookType.cs | 0 EasyTool.NPOI/{ => OfficeCategory}/NPOIUtil.cs | 0 EasyTool.NPOITests/{ => OfficeCategory}/NPOIUtilTests.cs | 0 10 files changed, 17 insertions(+), 11 deletions(-) rename EasyTool.CoreTests/{ToolCategory => IdentifierCategory}/IDUtilTests.cs (100%) rename EasyTool.CoreTests/{ToolCategory => NetCategory}/IpUtilTests.cs (100%) rename EasyTool.NPOI/{ => OfficeCategory}/ExcelWorkbookType.cs (100%) rename EasyTool.NPOI/{ => OfficeCategory}/NPOIUtil.cs (100%) rename EasyTool.NPOITests/{ => OfficeCategory}/NPOIUtilTests.cs (100%) diff --git a/.github/workflows/nuget_pre.yml b/.github/workflows/nuget_pre.yml index 96303e6..b0b93fc 100644 --- a/.github/workflows/nuget_pre.yml +++ b/.github/workflows/nuget_pre.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/nuget_prod.yml b/.github/workflows/nuget_prod.yml index 999568a..d158e4a 100644 --- a/.github/workflows/nuget_prod.yml +++ b/.github/workflows/nuget_prod.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 0802805..9a45b93 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs index d458173..34a7875 100644 --- a/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs +++ b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; -namespace EasyTool.IEnumerableCategory +namespace EasyTool.CollectionsCategory { /// /// IEnumerable 通用扩展方法 diff --git a/EasyTool.Core/ToolCategory/PageUtil.cs b/EasyTool.Core/ToolCategory/PageUtil.cs index 5a6b25e..967af93 100644 --- a/EasyTool.Core/ToolCategory/PageUtil.cs +++ b/EasyTool.Core/ToolCategory/PageUtil.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; -namespace EasyTool.DataCategory +namespace EasyTool.ToolCategory { /// /// 分页工具类,支持多种数据源和多种排序方式的分页 diff --git a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs b/EasyTool.CoreTests/IdentifierCategory/IDUtilTests.cs similarity index 100% rename from EasyTool.CoreTests/ToolCategory/IDUtilTests.cs rename to EasyTool.CoreTests/IdentifierCategory/IDUtilTests.cs diff --git a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs b/EasyTool.CoreTests/NetCategory/IpUtilTests.cs similarity index 100% rename from EasyTool.CoreTests/ToolCategory/IpUtilTests.cs rename to EasyTool.CoreTests/NetCategory/IpUtilTests.cs diff --git a/EasyTool.NPOI/ExcelWorkbookType.cs b/EasyTool.NPOI/OfficeCategory/ExcelWorkbookType.cs similarity index 100% rename from EasyTool.NPOI/ExcelWorkbookType.cs rename to EasyTool.NPOI/OfficeCategory/ExcelWorkbookType.cs diff --git a/EasyTool.NPOI/NPOIUtil.cs b/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs similarity index 100% rename from EasyTool.NPOI/NPOIUtil.cs rename to EasyTool.NPOI/OfficeCategory/NPOIUtil.cs diff --git a/EasyTool.NPOITests/NPOIUtilTests.cs b/EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs similarity index 100% rename from EasyTool.NPOITests/NPOIUtilTests.cs rename to EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs From 0c31b89dc482cf3de587c0eabb589ef3754f109f Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Tue, 24 Mar 2026 14:37:26 +0800 Subject: [PATCH 15/19] =?UTF-8?q?refactor(core):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=92=8C=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E6=95=B0=E7=94=9F=E6=88=90=E5=99=A8=E4=BB=A5=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E6=80=A7=E8=83=BD=E5=92=8C=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在AesUtil和DesUtil中使用using语句确保AES/DES资源正确释放 - 添加AesCryptoStream包装器来管理加密流的生命周期 - 优化CreditCodeUtil类,改进统一社会信用代码验证逻辑和类型识别 - 实现农历公历转换功能,新增LunarDate数据结构 - 更新随机数生成策略,.NET 6+使用Random.Shared,旧版本使用ThreadLocal - 为字符串工具类添加空值检查和更详细的文档注释 - 修复FileUtil.IsEmpty方法的逻辑错误和异常处理 - 迁移StringComparisonExtension到TextCategory命名空间 - 更新项目依赖包版本并移除不必要的条件引用 --- .../BusinessCategory/BankCardUtil.cs | 491 +++++++ EasyTool.Core/BusinessCategory/BarcodeUtil.cs | 651 +++++++++ .../BusinessCategory/CreditCardUtil.cs | 484 +++++++ .../BusinessCategory/CreditCodeUtil.cs | 256 ++-- EasyTool.Core/BusinessCategory/DomainUtil.cs | 456 ++++++ .../BusinessCategory/DrivingLicenseUtil.cs | 319 +++++ EasyTool.Core/BusinessCategory/EmailUtil.cs | 518 +++++++ .../BusinessCategory/ForeignerIdUtil.cs | 368 +++++ .../BusinessCategory/HKIdCardUtil.cs | 382 +++++ EasyTool.Core/BusinessCategory/ICCIDUtil.cs | 412 ++++++ EasyTool.Core/BusinessCategory/IMEIUtil.cs | 347 +++++ EasyTool.Core/BusinessCategory/IPv6Util.cs | 269 ++++ EasyTool.Core/BusinessCategory/ISBNUtil.cs | 576 ++++++++ EasyTool.Core/BusinessCategory/IdCardUtil.cs | 467 +++++++ .../BusinessCategory/LicensePlateUtil.cs | 717 ++++++++++ .../BusinessCategory/MACAddressUtil.cs | 508 +++++++ EasyTool.Core/BusinessCategory/OrgCodeUtil.cs | 254 ++++ .../BusinessCategory/PassportUtil.cs | 375 +++++ .../BusinessCategory/PasswordUtil.cs | 586 ++++++++ .../BusinessCategory/PhoneNumberUtil.cs | 350 +++++ EasyTool.Core/BusinessCategory/PhoneUtil.cs | 404 ++++++ EasyTool.Core/BusinessCategory/PortUtil.cs | 447 ++++++ .../BusinessCategory/PostalCodeUtil.cs | 437 ++++++ .../BusinessCategory/ProvinceUtil.cs | 573 ++++++++ EasyTool.Core/BusinessCategory/QQUtil.cs | 179 +++ .../BusinessCategory/SocialSecurityUtil.cs | 293 ++++ .../BusinessCategory/StockCodeUtil.cs | 529 +++++++ .../BusinessCategory/SwiftCodeUtil.cs | 438 ++++++ .../BusinessCategory/TaxNumberUtil.cs | 557 ++++++++ .../BusinessCategory/TwIdCardUtil.cs | 357 +++++ EasyTool.Core/BusinessCategory/VINUtil.cs | 463 ++++++ EasyTool.Core/BusinessCategory/WeChatUtil.cs | 222 +++ EasyTool.Core/CodeCategory/Adler32Util.cs | 224 +++ EasyTool.Core/CodeCategory/AesUtil.cs | 84 +- EasyTool.Core/CodeCategory/Argon2Util.cs | 390 ++++++ EasyTool.Core/CodeCategory/Base32Util.cs | 305 ++++ EasyTool.Core/CodeCategory/Base45Util.cs | 234 ++++ EasyTool.Core/CodeCategory/Base58Util.cs | 292 ++++ EasyTool.Core/CodeCategory/Base64UrlUtil.cs | 294 ++++ EasyTool.Core/CodeCategory/Base85Util.cs | 437 ++++++ EasyTool.Core/CodeCategory/Base91Util.cs | 216 +++ EasyTool.Core/CodeCategory/Base92Util.cs | 263 ++++ EasyTool.Core/CodeCategory/BaudotUtil.cs | 315 +++++ EasyTool.Core/CodeCategory/BcryptUtil.cs | 617 ++++++++ EasyTool.Core/CodeCategory/Blake2Util.cs | 569 ++++++++ EasyTool.Core/CodeCategory/Blake3Util.cs | 493 +++++++ EasyTool.Core/CodeCategory/BlowfishUtil.cs | 296 ++++ .../CodeCategory/CaesarCipherUtil.cs | 154 ++ EasyTool.Core/CodeCategory/CamelliaUtil.cs | 335 +++++ EasyTool.Core/CodeCategory/ChaCha20Util.cs | 363 +++++ EasyTool.Core/CodeCategory/CityHashUtil.cs | 365 +++++ EasyTool.Core/CodeCategory/CrcUtil.cs | 447 ++++++ EasyTool.Core/CodeCategory/CuidUtil.cs | 399 ++++++ EasyTool.Core/CodeCategory/DammUtil.cs | 235 ++++ EasyTool.Core/CodeCategory/DesUtil.cs | 12 +- .../CodeCategory/DiffieHellmanUtil.cs | 376 +++++ EasyTool.Core/CodeCategory/ElGamalUtil.cs | 460 ++++++ EasyTool.Core/CodeCategory/FarmHashUtil.cs | 368 +++++ EasyTool.Core/CodeCategory/FletcherUtil.cs | 340 +++++ EasyTool.Core/CodeCategory/GostUtil.cs | 256 ++++ EasyTool.Core/CodeCategory/GrayCodeUtil.cs | 291 ++++ EasyTool.Core/CodeCategory/IDEAUtil.cs | 294 ++++ EasyTool.Core/CodeCategory/JwtUtil.cs | 526 +++++++ EasyTool.Core/CodeCategory/KSUIDUtil.cs | 299 ++++ EasyTool.Core/CodeCategory/LZ4Util.cs | 339 +++++ EasyTool.Core/CodeCategory/LuhnUtil.cs | 386 +++++ EasyTool.Core/CodeCategory/MorseCodeUtil.cs | 314 +++++ EasyTool.Core/CodeCategory/MurmurHashUtil.cs | 382 +++++ EasyTool.Core/CodeCategory/NanoIdUtil.cs | 197 +++ EasyTool.Core/CodeCategory/PunycodeUtil.cs | 367 +++++ .../CodeCategory/QuotedPrintableUtil.cs | 236 ++++ EasyTool.Core/CodeCategory/RC4Util.cs | 303 ++++ EasyTool.Core/CodeCategory/RIPEMD160Util.cs | 362 +++++ EasyTool.Core/CodeCategory/ROT13Util.cs | 143 ++ EasyTool.Core/CodeCategory/RabbitUtil.cs | 269 ++++ EasyTool.Core/CodeCategory/Salsa20Util.cs | 189 +++ EasyTool.Core/CodeCategory/ScryptUtil.cs | 387 ++++++ EasyTool.Core/CodeCategory/SerpentUtil.cs | 318 +++++ EasyTool.Core/CodeCategory/SipHashUtil.cs | 340 +++++ EasyTool.Core/CodeCategory/Sm2Util.cs | 657 +++++++++ EasyTool.Core/CodeCategory/Sm3Util.cs | 358 +++++ EasyTool.Core/CodeCategory/Sm4Util.cs | 449 ++++++ EasyTool.Core/CodeCategory/SnappyUtil.cs | 350 +++++ EasyTool.Core/CodeCategory/SonyflakeUtil.cs | 167 +++ EasyTool.Core/CodeCategory/SqidsUtil.cs | 382 +++++ EasyTool.Core/CodeCategory/TOTPUtil.cs | 382 +++++ EasyTool.Core/CodeCategory/TSIDUtil.cs | 422 ++++++ EasyTool.Core/CodeCategory/TigerUtil.cs | 291 ++++ EasyTool.Core/CodeCategory/TimestampUtil.cs | 581 ++++++++ EasyTool.Core/CodeCategory/TwofishUtil.cs | 308 ++++ EasyTool.Core/CodeCategory/TypeIDUtil.cs | 315 +++++ EasyTool.Core/CodeCategory/UUEncodeUtil.cs | 209 +++ EasyTool.Core/CodeCategory/UUIDv7Util.cs | 293 ++++ EasyTool.Core/CodeCategory/UlidUtil.cs | 362 +++++ EasyTool.Core/CodeCategory/VerhoeffUtil.cs | 290 ++++ .../CodeCategory/VigenereCipherUtil.cs | 108 ++ EasyTool.Core/CodeCategory/WhirlpoolUtil.cs | 347 +++++ EasyTool.Core/CodeCategory/XidUtil.cs | 324 +++++ EasyTool.Core/CodeCategory/XorCipherUtil.cs | 174 +++ EasyTool.Core/CodeCategory/XxHashUtil.cs | 378 +++++ EasyTool.Core/CodeCategory/ZstdUtil.cs | 376 +++++ EasyTool.Core/CodeCategory/ZucUtil.cs | 436 ++++++ .../AdditionalCollectionUtil.cs | 842 +++++++++++ .../AdvancedBloomFilterUtil.cs | 607 ++++++++ .../AdvancedCollectionsUtil.cs | 687 +++++++++ .../CollectionsCategory/AdvancedHeapUtil.cs | 742 ++++++++++ .../AdvancedSearchTreeUtil.cs | 758 ++++++++++ .../CollectionsCategory/AhoCorasickUtil.cs | 377 +++++ .../CollectionsCategory/ArrayExtension.cs | 7 +- .../CollectionsCategory/BiDictionaryUtil.cs | 287 ++++ .../CollectionsCategory/BloomFilterUtil.cs | 207 +++ .../CollectionsCategory/CacheUtil.cs | 888 ++++++++++++ .../CardinalityAndHashUtil.cs | 598 ++++++++ .../CollectionsCategory/CircularBufferUtil.cs | 275 ++++ .../CollectionsCategory/CountMinSketchUtil.cs | 211 +++ .../CollectionsCategory/DistributionUtil.cs | 819 +++++++++++ .../CollectionsCategory/FenwickTreeUtil.cs | 344 +++++ .../CollectionsCategory/GraphUtil.cs | 992 +++++++++++++ .../CollectionsCategory/HeavyKeeperUtil.cs | 419 ++++++ .../IEnumerableExtensions.cs | 13 +- .../CollectionsCategory/LRUCacheUtil.cs | 233 ++++ .../CollectionsCategory/MatrixUtil.cs | 363 +++++ .../MultiKeyDictionaryUtil.cs | 747 ++++++++++ .../CollectionsCategory/MultiMapUtil.cs | 215 +++ .../PermutationCombinationUtil.cs | 250 ++++ .../CollectionsCategory/PriorityQueueUtil.cs | 324 +++++ .../CollectionsCategory/SkipListUtil.cs | 360 +++++ .../CollectionsCategory/SpatialIndexUtil.cs | 675 +++++++++ .../SpecialCollectionsUtil.cs | 1215 ++++++++++++++++ .../CollectionsCategory/StatisticsUtil.cs | 723 ++++++++++ .../CollectionsCategory/TDigestUtil.cs | 352 +++++ EasyTool.Core/CollectionsCategory/TopKUtil.cs | 225 +++ .../CollectionsCategory/TreeBuildUtil.cs | 443 ++++++ EasyTool.Core/CollectionsCategory/TreeUtil.cs | 1237 +++++++++++++++++ EasyTool.Core/CollectionsCategory/TrieUtil.cs | 262 ++++ .../CollectionsCategory/UnionFindUtil.cs | 273 ++++ .../CollectionsCategory/WindowUtil.cs | 1015 ++++++++++++++ EasyTool.Core/ColorCategory/ColorUtil.cs | 493 +++++++ .../ConvertCategory/CoordinateConvertUtil.cs | 274 ++++ .../ConvertCategory/UnitConvertUtil.cs | 436 ++++++ EasyTool.Core/DateTimeCategory/CronUtil.cs | 338 +++++ EasyTool.Core/DateTimeCategory/HolidayUtil.cs | 504 +++++++ .../DateTimeCategory/LunarCalendarUtil.cs | 117 ++ EasyTool.Core/EasyTool.Core.csproj | 6 +- EasyTool.Core/IOCategory/BomUtil.cs | 236 ++++ EasyTool.Core/IOCategory/CsvUtil.cs | 376 +++++ EasyTool.Core/IOCategory/FileSignatureUtil.cs | 314 +++++ EasyTool.Core/IOCategory/FileUtil.cs | 11 +- EasyTool.Core/IOCategory/IniUtil.cs | 328 +++++ EasyTool.Core/IOCategory/JsonUtil.cs | 550 ++++++++ EasyTool.Core/IOCategory/MimeTypeUtil.cs | 285 ++++ EasyTool.Core/IOCategory/PathUtil.cs | 346 +++++ EasyTool.Core/IOCategory/PropertiesUtil.cs | 630 +++++++++ EasyTool.Core/IOCategory/TomlUtil.cs | 438 ++++++ EasyTool.Core/IOCategory/YamlUtil.cs | 369 +++++ EasyTool.Core/MathCategory/DistanceUtil.cs | 369 +++++ EasyTool.Core/MathCategory/FractionUtil.cs | 405 ++++++ EasyTool.Core/MathCategory/GeometryUtil.cs | 593 ++++++++ .../MathCategory/InterpolationUtil.cs | 284 ++++ EasyTool.Core/MathCategory/MoneyUtil.cs | 470 +++++++ EasyTool.Core/MathCategory/RandomUtil.cs | 77 +- .../MathCategory/RomanNumeralUtil.cs | 120 ++ .../MathCategory/WeightedRandomUtil.cs | 319 +++++ EasyTool.Core/NetCategory/FtpUtil.cs | 600 ++++++++ EasyTool.Core/NetCategory/HttpUtil.cs | 676 +++++++++ EasyTool.Core/NetCategory/MailUtil.cs | 593 ++++++++ EasyTool.Core/NetCategory/UserAgentUtil.cs | 463 ++++++ EasyTool.Core/SystemCategory/ProcessUtil.cs | 821 +++++++++++ EasyTool.Core/TextCategory/CaptchaUtil.cs | 317 +++++ EasyTool.Core/TextCategory/DiffUtil.cs | 353 +++++ EasyTool.Core/TextCategory/HtmlUtil.cs | 608 ++++++++ EasyTool.Core/TextCategory/LevenshteinUtil.cs | 376 +++++ EasyTool.Core/TextCategory/PinyinUtil.cs | 200 +++ .../TextCategory/SensitiveWordUtil.cs | 365 +++++ EasyTool.Core/TextCategory/SimHashUtil.cs | 325 +++++ EasyTool.Core/TextCategory/SlugUtil.cs | 251 ++++ EasyTool.Core/TextCategory/StrUtil.cs | 95 +- .../TextCategory/StringComparisonExtension.cs | 2 +- EasyTool.Core/TextCategory/TemplateUtil.cs | 365 +++++ EasyTool.Core/ToolCategory/AsyncUtil.cs | 394 ++++++ EasyTool.Core/ToolCategory/BenchmarkUtil.cs | 543 ++++++++ EasyTool.Core/ToolCategory/EnumUtil.cs | 364 +++++ EasyTool.Core/ToolCategory/RateLimitUtil.cs | 557 ++++++++ EasyTool.Core/ToolCategory/RetryUtil.cs | 304 ++++ EasyTool.Core/ToolCategory/ThreadPoolUtil.cs | 620 +++++++++ EasyTool.Core/ToolCategory/ValidatorUtil.cs | 696 ++++++++++ EasyTool.Core/ToolCategory/VersionUtil.cs | 475 +++++++ 187 files changed, 73576 insertions(+), 185 deletions(-) create mode 100644 EasyTool.Core/BusinessCategory/BankCardUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/BarcodeUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/CreditCardUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/DomainUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/EmailUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/HKIdCardUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/ICCIDUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/IMEIUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/IPv6Util.cs create mode 100644 EasyTool.Core/BusinessCategory/ISBNUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/IdCardUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/LicensePlateUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/MACAddressUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/OrgCodeUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PassportUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PasswordUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PhoneUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PortUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/PostalCodeUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/ProvinceUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/QQUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/StockCodeUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/TaxNumberUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/TwIdCardUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/VINUtil.cs create mode 100644 EasyTool.Core/BusinessCategory/WeChatUtil.cs create mode 100644 EasyTool.Core/CodeCategory/Adler32Util.cs create mode 100644 EasyTool.Core/CodeCategory/Argon2Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base32Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base45Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base58Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base64UrlUtil.cs create mode 100644 EasyTool.Core/CodeCategory/Base85Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base91Util.cs create mode 100644 EasyTool.Core/CodeCategory/Base92Util.cs create mode 100644 EasyTool.Core/CodeCategory/BaudotUtil.cs create mode 100644 EasyTool.Core/CodeCategory/BcryptUtil.cs create mode 100644 EasyTool.Core/CodeCategory/Blake2Util.cs create mode 100644 EasyTool.Core/CodeCategory/Blake3Util.cs create mode 100644 EasyTool.Core/CodeCategory/BlowfishUtil.cs create mode 100644 EasyTool.Core/CodeCategory/CaesarCipherUtil.cs create mode 100644 EasyTool.Core/CodeCategory/CamelliaUtil.cs create mode 100644 EasyTool.Core/CodeCategory/ChaCha20Util.cs create mode 100644 EasyTool.Core/CodeCategory/CityHashUtil.cs create mode 100644 EasyTool.Core/CodeCategory/CrcUtil.cs create mode 100644 EasyTool.Core/CodeCategory/CuidUtil.cs create mode 100644 EasyTool.Core/CodeCategory/DammUtil.cs create mode 100644 EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs create mode 100644 EasyTool.Core/CodeCategory/ElGamalUtil.cs create mode 100644 EasyTool.Core/CodeCategory/FarmHashUtil.cs create mode 100644 EasyTool.Core/CodeCategory/FletcherUtil.cs create mode 100644 EasyTool.Core/CodeCategory/GostUtil.cs create mode 100644 EasyTool.Core/CodeCategory/GrayCodeUtil.cs create mode 100644 EasyTool.Core/CodeCategory/IDEAUtil.cs create mode 100644 EasyTool.Core/CodeCategory/JwtUtil.cs create mode 100644 EasyTool.Core/CodeCategory/KSUIDUtil.cs create mode 100644 EasyTool.Core/CodeCategory/LZ4Util.cs create mode 100644 EasyTool.Core/CodeCategory/LuhnUtil.cs create mode 100644 EasyTool.Core/CodeCategory/MorseCodeUtil.cs create mode 100644 EasyTool.Core/CodeCategory/MurmurHashUtil.cs create mode 100644 EasyTool.Core/CodeCategory/NanoIdUtil.cs create mode 100644 EasyTool.Core/CodeCategory/PunycodeUtil.cs create mode 100644 EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs create mode 100644 EasyTool.Core/CodeCategory/RC4Util.cs create mode 100644 EasyTool.Core/CodeCategory/RIPEMD160Util.cs create mode 100644 EasyTool.Core/CodeCategory/ROT13Util.cs create mode 100644 EasyTool.Core/CodeCategory/RabbitUtil.cs create mode 100644 EasyTool.Core/CodeCategory/Salsa20Util.cs create mode 100644 EasyTool.Core/CodeCategory/ScryptUtil.cs create mode 100644 EasyTool.Core/CodeCategory/SerpentUtil.cs create mode 100644 EasyTool.Core/CodeCategory/SipHashUtil.cs create mode 100644 EasyTool.Core/CodeCategory/Sm2Util.cs create mode 100644 EasyTool.Core/CodeCategory/Sm3Util.cs create mode 100644 EasyTool.Core/CodeCategory/Sm4Util.cs create mode 100644 EasyTool.Core/CodeCategory/SnappyUtil.cs create mode 100644 EasyTool.Core/CodeCategory/SonyflakeUtil.cs create mode 100644 EasyTool.Core/CodeCategory/SqidsUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TOTPUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TSIDUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TigerUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TimestampUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TwofishUtil.cs create mode 100644 EasyTool.Core/CodeCategory/TypeIDUtil.cs create mode 100644 EasyTool.Core/CodeCategory/UUEncodeUtil.cs create mode 100644 EasyTool.Core/CodeCategory/UUIDv7Util.cs create mode 100644 EasyTool.Core/CodeCategory/UlidUtil.cs create mode 100644 EasyTool.Core/CodeCategory/VerhoeffUtil.cs create mode 100644 EasyTool.Core/CodeCategory/VigenereCipherUtil.cs create mode 100644 EasyTool.Core/CodeCategory/WhirlpoolUtil.cs create mode 100644 EasyTool.Core/CodeCategory/XidUtil.cs create mode 100644 EasyTool.Core/CodeCategory/XorCipherUtil.cs create mode 100644 EasyTool.Core/CodeCategory/XxHashUtil.cs create mode 100644 EasyTool.Core/CodeCategory/ZstdUtil.cs create mode 100644 EasyTool.Core/CodeCategory/ZucUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/CacheUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/DistributionUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/GraphUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/MatrixUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/MultiMapUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/SkipListUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/StatisticsUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/TDigestUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/TopKUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/TreeUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/TrieUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/UnionFindUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/WindowUtil.cs create mode 100644 EasyTool.Core/ColorCategory/ColorUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/UnitConvertUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/CronUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/HolidayUtil.cs create mode 100644 EasyTool.Core/IOCategory/BomUtil.cs create mode 100644 EasyTool.Core/IOCategory/CsvUtil.cs create mode 100644 EasyTool.Core/IOCategory/FileSignatureUtil.cs create mode 100644 EasyTool.Core/IOCategory/IniUtil.cs create mode 100644 EasyTool.Core/IOCategory/JsonUtil.cs create mode 100644 EasyTool.Core/IOCategory/MimeTypeUtil.cs create mode 100644 EasyTool.Core/IOCategory/PathUtil.cs create mode 100644 EasyTool.Core/IOCategory/PropertiesUtil.cs create mode 100644 EasyTool.Core/IOCategory/TomlUtil.cs create mode 100644 EasyTool.Core/IOCategory/YamlUtil.cs create mode 100644 EasyTool.Core/MathCategory/DistanceUtil.cs create mode 100644 EasyTool.Core/MathCategory/FractionUtil.cs create mode 100644 EasyTool.Core/MathCategory/GeometryUtil.cs create mode 100644 EasyTool.Core/MathCategory/InterpolationUtil.cs create mode 100644 EasyTool.Core/MathCategory/MoneyUtil.cs create mode 100644 EasyTool.Core/MathCategory/RomanNumeralUtil.cs create mode 100644 EasyTool.Core/MathCategory/WeightedRandomUtil.cs create mode 100644 EasyTool.Core/NetCategory/FtpUtil.cs create mode 100644 EasyTool.Core/NetCategory/HttpUtil.cs create mode 100644 EasyTool.Core/NetCategory/MailUtil.cs create mode 100644 EasyTool.Core/NetCategory/UserAgentUtil.cs create mode 100644 EasyTool.Core/SystemCategory/ProcessUtil.cs create mode 100644 EasyTool.Core/TextCategory/CaptchaUtil.cs create mode 100644 EasyTool.Core/TextCategory/DiffUtil.cs create mode 100644 EasyTool.Core/TextCategory/HtmlUtil.cs create mode 100644 EasyTool.Core/TextCategory/LevenshteinUtil.cs create mode 100644 EasyTool.Core/TextCategory/PinyinUtil.cs create mode 100644 EasyTool.Core/TextCategory/SensitiveWordUtil.cs create mode 100644 EasyTool.Core/TextCategory/SimHashUtil.cs create mode 100644 EasyTool.Core/TextCategory/SlugUtil.cs create mode 100644 EasyTool.Core/TextCategory/TemplateUtil.cs create mode 100644 EasyTool.Core/ToolCategory/AsyncUtil.cs create mode 100644 EasyTool.Core/ToolCategory/BenchmarkUtil.cs create mode 100644 EasyTool.Core/ToolCategory/EnumUtil.cs create mode 100644 EasyTool.Core/ToolCategory/RateLimitUtil.cs create mode 100644 EasyTool.Core/ToolCategory/RetryUtil.cs create mode 100644 EasyTool.Core/ToolCategory/ThreadPoolUtil.cs create mode 100644 EasyTool.Core/ToolCategory/ValidatorUtil.cs create mode 100644 EasyTool.Core/ToolCategory/VersionUtil.cs diff --git a/EasyTool.Core/BusinessCategory/BankCardUtil.cs b/EasyTool.Core/BusinessCategory/BankCardUtil.cs new file mode 100644 index 0000000..19e6c57 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/BankCardUtil.cs @@ -0,0 +1,491 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 银行卡类型枚举 + /// + public enum BankType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 借记卡 + /// + Debit = 1, + + /// + /// 信用卡 + /// + Credit = 2 + } + + /// + /// 银行信息 + /// + public class BankInfo + { + /// + /// 银行名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 卡类型 + /// + public BankType Type { get; set; } + + /// + /// 银行缩写代码 + /// + public string Code { get; set; } = string.Empty; + } + + /// + /// 银行卡工具类 + /// + public static class BankCardUtil + { + #region 常量与私有字段 + + /// + /// 银行卡号正则表达式(13-19位数字) + /// + private static readonly Regex BankCardRegex = new(@"^\d{13,19}$", RegexOptions.Compiled); + + /// + /// 银行BIN码映射(前6位 -> 银行信息) + /// 注:此处仅包含部分常见银行BIN码,实际应用中应使用完整的BIN码库 + /// + private static readonly Dictionary BinCodeMapping = new() + { + // 工商银行 + { "622202", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622203", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622204", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622205", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622206", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622207", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622208", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622209", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622210", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622588", new BankInfo { Name = "中国工商银行", Type = BankType.Credit, Code = "ICBC" } }, + + // 农业银行 + { "622848", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622849", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622845", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622846", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622847", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622821", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622822", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622823", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622824", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622825", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + + // 中国银行 + { "621660", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621661", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621662", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621663", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621665", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621666", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621667", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621668", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621669", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + + // 建设银行 + { "622700", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "622707", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "622708", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "621081", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "436742", new BankInfo { Name = "中国建设银行", Type = BankType.Credit, Code = "CCB" } }, + { "436745", new BankInfo { Name = "中国建设银行", Type = BankType.Credit, Code = "CCB" } }, + + // 交通银行 + { "622260", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622261", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622262", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622521", new BankInfo { Name = "交通银行", Type = BankType.Credit, Code = "BOCOM" } }, + { "622522", new BankInfo { Name = "交通银行", Type = BankType.Credit, Code = "BOCOM" } }, + + // 招商银行 + { "622580", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622581", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622582", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622588", new BankInfo { Name = "招商银行", Type = BankType.Credit, Code = "CMB" } }, + { "622589", new BankInfo { Name = "招商银行", Type = BankType.Credit, Code = "CMB" } }, + { "621286", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + + // 浦发银行 + { "622518", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + { "622519", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + { "622520", new BankInfo { Name = "浦发银行", Type = BankType.Credit, Code = "SPDB" } }, + { "621228", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + + // 民生银行 + { "622615", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622617", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622618", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622620", new BankInfo { Name = "民生银行", Type = BankType.Credit, Code = "CMBC" } }, + { "622622", new BankInfo { Name = "民生银行", Type = BankType.Credit, Code = "CMBC" } }, + + // 兴业银行 + { "622909", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622910", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622911", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622912", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622918", new BankInfo { Name = "兴业银行", Type = BankType.Credit, Code = "CIB" } }, + + // 中信银行 + { "622690", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622691", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622692", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622696", new BankInfo { Name = "中信银行", Type = BankType.Credit, Code = "CITIC" } }, + { "622698", new BankInfo { Name = "中信银行", Type = BankType.Credit, Code = "CITIC" } }, + + // 光大银行 + { "622655", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622656", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622657", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622658", new BankInfo { Name = "光大银行", Type = BankType.Credit, Code = "CEB" } }, + { "622685", new BankInfo { Name = "光大银行", Type = BankType.Credit, Code = "CEB" } }, + + // 平安银行 + { "622155", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622156", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622157", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622525", new BankInfo { Name = "平安银行", Type = BankType.Credit, Code = "PAB" } }, + { "622526", new BankInfo { Name = "平安银行", Type = BankType.Credit, Code = "PAB" } }, + + // 华夏银行 + { "622630", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + { "622631", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + { "622632", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + + // 广发银行 + { "622568", new BankInfo { Name = "广发银行", Type = BankType.Debit, Code = "CGB" } }, + { "622569", new BankInfo { Name = "广发银行", Type = BankType.Debit, Code = "CGB" } }, + { "622570", new BankInfo { Name = "广发银行", Type = BankType.Credit, Code = "CGB" } }, + { "622575", new BankInfo { Name = "广发银行", Type = BankType.Credit, Code = "CGB" } }, + + // 邮储银行 + { "622150", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622151", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622180", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622181", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622188", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + + // 北京银行 + { "622309", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + { "622310", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + { "622311", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + + // 上海银行 + { "622462", new BankInfo { Name = "上海银行", Type = BankType.Debit, Code = "SHBANK" } }, + { "622463", new BankInfo { Name = "上海银行", Type = BankType.Debit, Code = "SHBANK" } }, + }; + + #endregion + + #region 验证方法 + + /// + /// 验证银行卡号是否有效(格式 + Luhn校验) + /// + /// 银行卡号 + /// 是否有效 + public static bool IsValid(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return false; + } + + return ValidateLuhn(cardNumber); + } + + /// + /// 仅验证银行卡号格式(不包含Luhn校验) + /// + /// 银行卡号 + /// 格式是否有效 + public static bool IsValidFormat(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + return BankCardRegex.IsMatch(cardNumber); + } + + /// + /// 使用Luhn算法验证银行卡号 + /// + /// 银行卡号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + int sum = 0; + int length = cardNumber.Length; + bool isEvenPosition = false; + + // 从右向左遍历 + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(cardNumber[i])) + { + return false; + } + + int digit = cardNumber[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return sum % 10 == 0; + } + + /// + /// 计算Luhn校验位 + /// + /// 不含校验位的银行卡号 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateLuhnCheckDigit(string? cardNumberWithoutCheckDigit) + { + if (string.IsNullOrWhiteSpace(cardNumberWithoutCheckDigit)) + { + return -1; + } + + // 在末尾添加一个临时校验位0 + string tempCardNumber = cardNumberWithoutCheckDigit + "0"; + int sum = 0; + int length = tempCardNumber.Length; + bool isEvenPosition = false; + + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(tempCardNumber[i])) + { + return -1; + } + + int digit = tempCardNumber[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 银行信息查询 + + /// + /// 获取银行信息 + /// + /// 银行卡号 + /// 银行信息,未找到返回null + public static BankInfo? GetBankInfo(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber) || cardNumber.Length < 6) + { + return null; + } + + // 尝试匹配6位BIN码 + string bin6 = cardNumber.Substring(0, 6); + if (BinCodeMapping.TryGetValue(bin6, out BankInfo? info)) + { + return info; + } + + // 尝试匹配5位BIN码 + if (cardNumber.Length >= 5) + { + string bin5 = cardNumber.Substring(0, 5); + if (BinCodeMapping.TryGetValue(bin5, out info)) + { + return info; + } + } + + // 尝试匹配4位BIN码 + if (cardNumber.Length >= 4) + { + string bin4 = cardNumber.Substring(0, 4); + if (BinCodeMapping.TryGetValue(bin4, out info)) + { + return info; + } + } + + return null; + } + + /// + /// 获取银行名称 + /// + /// 银行卡号 + /// 银行名称 + public static string? GetBankName(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Name; + } + + /// + /// 获取银行缩写代码 + /// + /// 银行卡号 + /// 银行缩写代码 + public static string? GetBankCode(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Code; + } + + /// + /// 获取卡类型 + /// + /// 银行卡号 + /// 卡类型 + public static BankType GetBankType(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Type ?? BankType.Unknown; + } + + /// + /// 判断是否为借记卡 + /// + /// 银行卡号 + /// 是否为借记卡 + public static bool IsDebitCard(string? cardNumber) + { + return GetBankType(cardNumber) == BankType.Debit; + } + + /// + /// 判断是否为信用卡 + /// + /// 银行卡号 + /// 是否为信用卡 + public static bool IsCreditCard(string? cardNumber) + { + return GetBankType(cardNumber) == BankType.Credit; + } + + /// + /// 获取BIN码(前6位) + /// + /// 银行卡号 + /// BIN码 + public static string? GetBinCode(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber) || cardNumber.Length < 6) + { + return null; + } + + return cardNumber.Substring(0, 6); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化银行卡号(每4位一组,空格分隔) + /// + /// 银行卡号 + /// 格式化后的卡号 + public static string? Format(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return null; + } + + // 移除非数字字符 + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + + if (!IsValidFormat(cleaned)) + { + return null; + } + + // 每4位分组 + var groups = new List(); + for (int i = 0; i < cleaned.Length; i += 4) + { + int length = Math.Min(4, cleaned.Length - i); + groups.Add(cleaned.Substring(i, length)); + } + + return string.Join(" ", groups); + } + + /// + /// 银行卡号脱敏:6222****5678 + /// + /// 银行卡号 + /// 脱敏后的卡号 + public static string? Mask(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return null; + } + + // 移除非数字字符 + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + + if (cleaned.Length < 8) + { + return null; + } + + int prefixLength = 4; + int suffixLength = 4; + + string prefix = cleaned.Substring(0, prefixLength); + string suffix = cleaned.Substring(cleaned.Length - suffixLength, suffixLength); + int maskLength = cleaned.Length - prefixLength - suffixLength; + + return prefix + new string('*', maskLength) + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/BarcodeUtil.cs b/EasyTool.Core/BusinessCategory/BarcodeUtil.cs new file mode 100644 index 0000000..e541ba5 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/BarcodeUtil.cs @@ -0,0 +1,651 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 条形码类型枚举 + /// + public enum BarcodeType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// EAN-13(13位国际商品条码) + /// + EAN13 = 1, + + /// + /// EAN-8(8位商品条码) + /// + EAN8 = 2, + + /// + /// UPC-A(12位北美商品条码) + /// + UPCA = 3, + + /// + /// UPC-E(6位压缩商品条码) + /// + UPCE = 4, + + /// + /// ITF-14(14位物流包装条码) + /// + ITF14 = 5, + + /// + /// Code128(可变长度工业条码) + /// + Code128 = 6 + } + + /// + /// 条形码工具类 + /// + public static class BarcodeUtil + { + #region 常量与私有字段 + + /// + /// EAN-13正则表达式 + /// + private static readonly Regex EAN13Regex = new(@"^\d{13}$", RegexOptions.Compiled); + + /// + /// EAN-8正则表达式 + /// + private static readonly Regex EAN8Regex = new(@"^\d{8}$", RegexOptions.Compiled); + + /// + /// UPC-A正则表达式 + /// + private static readonly Regex UPCARegex = new(@"^\d{12}$", RegexOptions.Compiled); + + /// + /// UPC-E正则表达式 + /// + private static readonly Regex UPCERegex = new(@"^\d{6}$", RegexOptions.Compiled); + + /// + /// ITF-14正则表达式 + /// + private static readonly Regex ITF14Regex = new(@"^\d{14}$", RegexOptions.Compiled); + + /// + /// 国家代码(GS1前缀)与地区映射 + /// + private static readonly (string Prefix, string Region)[] Gs1PrefixMap = + { + ("000", "美国/加拿大"), ("001", "美国/加拿大"), ("019", "美国/加拿大"), + ("020", "店内码"), ("029", "店内码"), + ("030", "美国/加拿大"), ("039", "美国/加拿大"), + ("040", "店内码"), ("049", "店内码"), + ("050", "优惠券"), ("099", "优惠券"), + ("100", "美国/加拿大"), ("139", "美国/加拿大"), + ("200", "店内码"), ("299", "店内码"), + ("300", "法国"), ("379", "法国"), + ("380", "保加利亚"), + ("383", "斯洛文尼亚"), + ("385", "克罗地亚"), + ("387", "波黑"), + ("400", "德国"), ("440", "德国"), + ("450", "日本"), ("459", "日本"), + ("460", "俄罗斯"), ("469", "俄罗斯"), + ("470", "吉尔吉斯斯坦"), + ("471", "台湾"), + ("474", "爱沙尼亚"), + ("475", "拉脱维亚"), + ("476", "阿塞拜疆"), + ("477", "立陶宛"), + ("478", "乌兹别克斯坦"), + ("479", "斯里兰卡"), + ("480", "菲律宾"), + ("481", "白俄罗斯"), + ("482", "乌克兰"), + ("483", "土库曼斯坦"), + ("484", "摩尔多瓦"), + ("485", "亚美尼亚"), + ("486", "格鲁吉亚"), + ("487", "哈萨克斯坦"), + ("488", "塔吉克斯坦"), + ("489", "香港"), + ("490", "日本"), ("499", "日本"), + ("500", "英国"), ("509", "英国"), + ("520", "希腊"), + ("528", "黎巴嫩"), + ("529", "塞浦路斯"), + ("530", "阿尔巴尼亚"), + ("531", "马其顿"), + ("535", "马耳他"), + ("539", "爱尔兰"), + ("540", "比利时/卢森堡"), ("549", "比利时/卢森堡"), + ("560", "葡萄牙"), + ("569", "冰岛"), + ("570", "丹麦"), ("579", "丹麦"), + ("590", "波兰"), + ("594", "罗马尼亚"), + ("599", "匈牙利"), + ("600", "南非"), ("601", "南非"), + ("603", "加纳"), + ("604", "塞内加尔"), + ("608", "巴林"), + ("609", "毛里求斯"), + ("611", "摩洛哥"), + ("613", "阿尔及利亚"), + ("615", "尼日利亚"), + ("616", "肯尼亚"), + ("618", "科特迪瓦"), + ("619", "突尼斯"), + ("621", "叙利亚"), + ("622", "埃及"), + ("624", "利比亚"), + ("625", "约旦"), + ("626", "伊朗"), + ("627", "科威特"), + ("628", "沙特阿拉伯"), + ("629", "阿联酋"), + ("640", "芬兰"), ("649", "芬兰"), + ("690", "中国"), ("699", "中国"), + ("700", "挪威"), ("709", "挪威"), + ("729", "以色列"), + ("730", "瑞典"), ("739", "瑞典"), + ("740", "危地马拉"), + ("741", "萨尔瓦多"), + ("742", "洪都拉斯"), + ("743", "尼加拉瓜"), + ("744", "哥斯达黎加"), + ("745", "巴拿马"), + ("746", "多米尼加"), + ("750", "墨西哥"), + ("754", "加拿大"), ("755", "加拿大"), + ("759", "委内瑞拉"), + ("760", "瑞士"), ("769", "瑞士"), + ("770", "哥伦比亚"), + ("773", "乌拉圭"), + ("775", "秘鲁"), + ("777", "玻利维亚"), + ("779", "阿根廷"), + ("780", "智利"), + ("784", "巴拉圭"), + ("786", "厄瓜多尔"), + ("789", "巴西"), ("790", "巴西"), + ("800", "意大利"), ("839", "意大利"), + ("840", "美国"), ("849", "美国"), + ("850", "古巴"), + ("858", "斯洛伐克"), + ("859", "捷克"), + ("860", "塞尔维亚"), + ("865", "蒙古"), + ("867", "朝鲜"), + ("868", "土耳其"), ("869", "土耳其"), + ("870", "荷兰"), ("879", "荷兰"), + ("880", "韩国"), + ("884", "柬埔寨"), + ("885", "泰国"), + ("888", "新加坡"), + ("890", "印度"), + ("893", "越南"), + ("896", "巴基斯坦"), + ("899", "印度尼西亚"), + ("900", "奥地利"), ("919", "奥地利"), + ("930", "澳大利亚"), ("939", "澳大利亚"), + ("940", "新西兰"), ("949", "新西兰"), + ("950", "国际组织"), + ("951", "国际组织"), + ("955", "马来西亚"), + ("958", "澳门") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证条形码是否有效(自动识别类型) + /// + /// 条形码 + /// 是否有效 + public static bool IsValid(string? barcode) + { + return IsValidEAN13(barcode) || IsValidEAN8(barcode) || + IsValidUPCA(barcode) || IsValidUPCE(barcode) || + IsValidITF14(barcode); + } + + /// + /// 验证EAN-13条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidEAN13(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !EAN13Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 13); + } + + /// + /// 验证EAN-8条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidEAN8(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !EAN8Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 8); + } + + /// + /// 验证UPC-A条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidUPCA(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !UPCARegex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 12); + } + + /// + /// 验证UPC-E条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidUPCE(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !UPCERegex.IsMatch(barcode)) + { + return false; + } + + // UPC-E需要展开为UPC-A后验证 + string? expanded = ExpandUPCE(barcode); + return expanded != null && IsValidUPCA(expanded); + } + + /// + /// 验证ITF-14条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidITF14(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !ITF14Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 14); + } + + /// + /// 验证校验位 + /// + private static bool ValidateChecksum(string barcode, int length) + { + int sum = 0; + for (int i = 0; i < length - 1; i++) + { + int digit = barcode[i] - '0'; + // 从右向左,偶数位权重为3,奇数位权重为1 + int weight = ((length - 1 - i) % 2 == 1) ? 3 : 1; + sum += digit * weight; + } + + int checkDigit = (10 - (sum % 10)) % 10; + return checkDigit == (barcode[length - 1] - '0'); + } + + /// + /// 计算校验位 + /// + /// 不含校验位的条形码 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateCheckDigit(string? barcodeWithoutCheck) + { + if (string.IsNullOrWhiteSpace(barcodeWithoutCheck)) + { + return -1; + } + + int length = barcodeWithoutCheck.Length; + int sum = 0; + for (int i = 0; i < length; i++) + { + if (!char.IsDigit(barcodeWithoutCheck[i])) + { + return -1; + } + int digit = barcodeWithoutCheck[i] - '0'; + // 从右向左,偶数位权重为3 + int weight = ((length - i) % 2 == 0) ? 3 : 1; + sum += digit * weight; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 类型识别 + + /// + /// 获取条形码类型 + /// + /// 条形码 + /// 条形码类型 + public static BarcodeType GetBarcodeType(string? barcode) + { + if (IsValidEAN13(barcode)) return BarcodeType.EAN13; + if (IsValidEAN8(barcode)) return BarcodeType.EAN8; + if (IsValidUPCA(barcode)) return BarcodeType.UPCA; + if (IsValidUPCE(barcode)) return BarcodeType.UPCE; + if (IsValidITF14(barcode)) return BarcodeType.ITF14; + return BarcodeType.Unknown; + } + + /// + /// 获取条形码类型名称 + /// + /// 条形码类型 + /// 类型名称 + public static string GetBarcodeTypeName(BarcodeType type) + { + return type switch + { + BarcodeType.EAN13 => "EAN-13", + BarcodeType.EAN8 => "EAN-8", + BarcodeType.UPCA => "UPC-A", + BarcodeType.UPCE => "UPC-E", + BarcodeType.ITF14 => "ITF-14", + BarcodeType.Code128 => "Code 128", + _ => "未知" + }; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国家/地区(根据GS1前缀) + /// + /// 条形码 + /// 国家/地区名称 + public static string? GetRegion(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || barcode.Length < 3) + { + return null; + } + + string prefix3 = barcode.Substring(0, 3); + string prefix2 = barcode.Substring(0, 2); + string prefix1 = barcode.Substring(0, 1); + + // 先匹配3位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix3) + { + return mapping.Region; + } + } + + // 再匹配2位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix2) + { + return mapping.Region; + } + } + + // 最后匹配1位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix1) + { + return mapping.Region; + } + } + + return null; + } + + /// + /// 判断是否为中国商品条码 + /// + /// 条形码 + /// 是否为中国商品条码 + public static bool IsChinaBarcode(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || barcode.Length < 3) + { + return false; + } + + string prefix = barcode.Substring(0, 3); + return prefix.CompareTo("690") >= 0 && prefix.CompareTo("699") <= 0; + } + + /// + /// 获取厂商识别代码(EAN-13的前7-9位) + /// + /// 条形码 + /// 厂商识别代码 + public static string? GetManufacturerCode(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + // EAN-13:前缀(2-3位) + 厂商代码(4-5位) + 商品代码(5位) + 校验位 + // 简化处理:返回前8位(不含校验位) + return barcode!.Substring(0, 8); + } + + /// + /// 获取商品项目代码(EAN-13的第9-12位) + /// + /// 条形码 + /// 商品项目代码 + public static string? GetProductCode(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + return barcode!.Substring(8, 4); + } + + #endregion + + #region 转换方法 + + /// + /// 将UPC-E转换为UPC-A + /// + /// UPC-E条形码 + /// UPC-A条形码,转换失败返回null + public static string? ExpandUPCE(string? upce) + { + if (string.IsNullOrWhiteSpace(upce) || upce.Length != 6 || !UPCERegex.IsMatch(upce)) + { + return null; + } + + char lastDigit = upce[5]; + string result; + + switch (lastDigit) + { + case '0': + result = upce[0] + upce[1].ToString() + "00000" + upce[2] + upce[3] + upce[4]; + break; + case '1': + result = upce[0] + upce[1].ToString() + "10000" + upce[2] + upce[3] + upce[4]; + break; + case '2': + result = upce[0] + upce[1].ToString() + "20000" + upce[2] + upce[3] + upce[4]; + break; + case '3': + result = upce[0] + upce[1].ToString() + upce[2] + "00000" + upce[3] + upce[4]; + break; + case '4': + result = upce[0] + upce[1].ToString() + upce[2] + upce[3] + "00000" + upce[4]; + break; + default: + result = upce[0] + upce[1].ToString() + upce[2] + upce[3] + upce[4] + "0000" + lastDigit; + break; + } + + // 添加系统字符(0)和计算校验位 + string fullCode = "0" + result; + int checkDigit = CalculateCheckDigit(fullCode); + return checkDigit >= 0 ? fullCode + checkDigit : null; + } + + /// + /// 将UPC-A转换为EAN-13 + /// + /// UPC-A条形码 + /// EAN-13条形码 + public static string? ConvertUPCAToEAN13(string? upca) + { + if (!IsValidUPCA(upca)) + { + return null; + } + + return "0" + upca; + } + + /// + /// 将EAN-13转换为EAN-8(仅当适用于短码时) + /// + /// EAN-13条形码 + /// EAN-8条形码,不适用返回null + public static string? ConvertEAN13ToEAN8(string? ean13) + { + if (!IsValidEAN13(ean13)) + { + return null; + } + + // 只有特定前缀的EAN-13才能转换为EAN-8 + // 简化处理:仅支持部分转换 + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化条形码(去除非数字字符) + /// + /// 条形码 + /// 格式化后的条形码 + public static string? Normalize(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode)) + { + return null; + } + + string cleaned = Regex.Replace(barcode, @"\D", ""); + return cleaned.Length >= 6 ? cleaned : null; + } + + /// + /// 格式化EAN-13(X-XXXXXX-XXXXX-X) + /// + /// 条形码 + /// 格式化后的条形码 + public static string? FormatEAN13(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + return $"{barcode![0]}-{barcode.Substring(1, 6)}-{barcode.Substring(7, 5)}-{barcode[12]}"; + } + + /// + /// 条形码脱敏:69****1234 + /// + /// 条形码 + /// 脱敏后的条形码 + public static string? Mask(string? barcode) + { + string? normalized = Normalize(barcode); + if (normalized == null || normalized.Length < 6) + { + return null; + } + + int len = normalized.Length; + int prefixLen = Math.Min(2, len / 3); + int suffixLen = Math.Min(4, len / 3); + + return normalized.Substring(0, prefixLen) + + new string('*', len - prefixLen - suffixLen) + + normalized.Substring(len - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机EAN-13条形码(仅供测试使用) + /// + /// 前缀(可选,默认690-中国) + /// EAN-13条形码 + public static string GenerateRandomEAN13(string? prefix = null) + { + string pre = prefix ?? "690"; + while (pre.Length < 12) + { + pre += MathCategory.RandomUtil.RandomInt(0, 10).ToString(); + } + + pre = pre.Substring(0, 12); + int checkDigit = CalculateCheckDigit(pre); + return pre + checkDigit; + } + + /// + /// 生成随机ITF-14条形码(仅供测试使用) + /// + /// ITF-14条形码 + public static string GenerateRandomITF14() + { + string code = MathCategory.RandomUtil.RandomDigitString(13); + int checkDigit = CalculateCheckDigit(code); + return code + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/CreditCardUtil.cs b/EasyTool.Core/BusinessCategory/CreditCardUtil.cs new file mode 100644 index 0000000..47cf073 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/CreditCardUtil.cs @@ -0,0 +1,484 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 国际信用卡类型枚举 + /// + public enum CreditCardType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// Visa + /// + Visa = 1, + + /// + /// MasterCard + /// + MasterCard = 2, + + /// + /// American Express + /// + Amex = 3, + + /// + /// Discover + /// + Discover = 4, + + /// + /// JCB + /// + JCB = 5, + + /// + /// Diners Club + /// + DinersClub = 6, + + /// + /// UnionPay(银联) + /// + UnionPay = 7, + + /// + /// Maestro + /// + Maestro = 8 + } + + /// + /// 国际信用卡信息 + /// + public class CreditCardInfo + { + /// + /// 卡类型 + /// + public CreditCardType Type { get; set; } + + /// + /// 卡名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 发卡组织 + /// + public string Issuer { get; set; } = string.Empty; + } + + /// + /// 国际信用卡工具类 + /// + public static class CreditCardUtil + { + #region 常量与私有字段 + + /// + /// 信用卡号正则表达式(13-19位数字) + /// + private static readonly Regex CardNumberRegex = new(@"^\d{13,19}$", RegexOptions.Compiled); + + /// + /// 卡类型识别规则(前缀 -> 卡类型) + /// + private static readonly (string Prefix, CreditCardType Type, string Name, string Issuer)[] CardTypeRules = + { + // Visa: 4开头,13或16位 + ("4", CreditCardType.Visa, "Visa", "Visa International"), + + // MasterCard: 51-55, 2221-2720开头,16位 + ("51", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("52", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("53", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("54", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("55", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2221", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2222", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2223", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2224", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2225", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2226", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2227", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2228", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2229", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("223", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("224", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("225", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("226", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("227", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("228", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("229", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("23", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("24", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("25", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("26", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("270", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("271", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2720", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + + // American Express: 34或37开头,15位 + ("34", CreditCardType.Amex, "American Express", "American Express Company"), + ("37", CreditCardType.Amex, "American Express", "American Express Company"), + + // Discover: 6011, 622126-622925, 644-649, 65开头,16位 + ("6011", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("65", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("644", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("645", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("646", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("647", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("648", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("649", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622126", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622127", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622128", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622129", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62213", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62214", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62215", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62216", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62217", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62218", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62219", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6222", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6223", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6224", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6225", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6226", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6227", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6228", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62290", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62291", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622920", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622921", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622922", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622923", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622924", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622925", CreditCardType.Discover, "Discover", "Discover Financial Services"), + + // JCB: 3528-3589开头,16位 + ("3528", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("3529", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("353", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("354", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("355", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("356", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("357", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("358", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + + // Diners Club: 300-305, 309, 36, 38-39开头,14位 + ("300", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("301", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("302", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("303", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("304", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("305", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("309", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("36", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("38", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("39", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + + // UnionPay: 62开头,16-19位 + ("62", CreditCardType.UnionPay, "UnionPay", "China UnionPay"), + + // Maestro: 5018, 5020, 5038, 5893, 6304, 6759, 6761-6763开头,12-19位 + ("5018", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5020", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5038", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5893", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6304", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6759", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6761", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6762", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6763", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证信用卡号是否有效(格式 + Luhn校验) + /// + /// 信用卡号 + /// 是否有效 + public static bool IsValid(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return false; + } + + return ValidateLuhn(cardNumber!); + } + + /// + /// 验证信用卡号格式 + /// + /// 信用卡号 + /// 格式是否正确 + public static bool IsValidFormat(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + return CardNumberRegex.IsMatch(cleaned); + } + + /// + /// 使用Luhn算法验证信用卡号 + /// + /// 信用卡号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + int sum = 0; + int length = cleaned.Length; + bool isEvenPosition = false; + + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(cleaned[i])) + { + return false; + } + + int digit = cleaned[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return sum % 10 == 0; + } + + #endregion + + #region 类型识别 + + /// + /// 获取信用卡类型 + /// + /// 信用卡号 + /// 信用卡类型 + public static CreditCardType GetCardType(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return CreditCardType.Unknown; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + // 从最长前缀开始匹配 + for (int len = 6; len >= 1; len--) + { + if (cleaned.Length < len) continue; + + string prefix = cleaned.Substring(0, len); + foreach (var rule in CardTypeRules) + { + if (rule.Prefix == prefix) + { + return rule.Type; + } + } + } + + return CreditCardType.Unknown; + } + + /// + /// 获取信用卡信息 + /// + /// 信用卡号 + /// 信用卡信息 + public static CreditCardInfo? GetCardInfo(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + for (int len = 6; len >= 1; len--) + { + if (cleaned.Length < len) continue; + + string prefix = cleaned.Substring(0, len); + foreach (var rule in CardTypeRules) + { + if (rule.Prefix == prefix) + { + return new CreditCardInfo + { + Type = rule.Type, + Name = rule.Name, + Issuer = rule.Issuer + }; + } + } + } + + return null; + } + + /// + /// 获取信用卡类型名称 + /// + /// 信用卡号 + /// 类型名称 + public static string? GetCardTypeName(string? cardNumber) + { + return GetCardInfo(cardNumber)?.Name; + } + + /// + /// 判断是否为Visa卡 + /// + public static bool IsVisa(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Visa; + + /// + /// 判断是否为MasterCard + /// + public static bool IsMasterCard(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.MasterCard; + + /// + /// 判断是否为American Express + /// + public static bool IsAmex(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Amex; + + /// + /// 判断是否为Discover + /// + public static bool IsDiscover(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Discover; + + /// + /// 判断是否为JCB + /// + public static bool IsJCB(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.JCB; + + /// + /// 判断是否为银联 + /// + public static bool IsUnionPay(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.UnionPay; + + #endregion + + #region 格式化方法 + + /// + /// 格式化信用卡号(每4位一组) + /// + /// 信用卡号 + /// 格式化后的卡号 + public static string? Format(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + var groups = new List(); + + for (int i = 0; i < cleaned.Length; i += 4) + { + int len = Math.Min(4, cleaned.Length - i); + groups.Add(cleaned.Substring(i, len)); + } + + return string.Join(" ", groups); + } + + /// + /// 格式化信用卡号(根据卡类型自动选择格式) + /// + /// 信用卡号 + /// 格式化后的卡号 + public static string? FormatByType(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + CreditCardType type = GetCardType(cleaned); + + // Amex特殊格式:4-6-5 + if (type == CreditCardType.Amex && cleaned.Length == 15) + { + return $"{cleaned.Substring(0, 4)} {cleaned.Substring(4, 6)} {cleaned.Substring(10, 5)}"; + } + + // 默认4位一组 + return Format(cleaned); + } + + /// + /// 信用卡号脱敏:**** **** **** 1234 + /// + /// 信用卡号 + /// 脱敏后的卡号 + public static string? Mask(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + if (cleaned.Length < 8) + { + return null; + } + + int suffixLen = 4; + string suffix = cleaned.Substring(cleaned.Length - suffixLen); + int maskLen = cleaned.Length - suffixLen; + string masked = new string('*', maskLen); + + // 格式化输出 + CreditCardType type = GetCardType(cleaned); + if (type == CreditCardType.Amex && cleaned.Length == 15) + { + return $"**** ****** {suffix}"; + } + + return $"**** **** **** {suffix}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs index c96ed02..ad7f1a5 100644 --- a/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs +++ b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs @@ -1,162 +1,210 @@ using System; -using System.Collections.Generic; -using System.Text; +using System.Text.RegularExpressions; namespace EasyTool.BusinessCategory { /// - /// 社会信用代码工具 + /// 统一社会信用代码工具类 + /// 用于验证和处理中国大陆企业的统一社会信用代码 /// public static class CreditCodeUtil { - private const string BaseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; // 社会信用代码中的基础字符集 - private const int Modulo = 31; // 校验码计算中的模数 + /// + /// 统一社会信用代码长度 + /// + private const int CreditCodeLength = 18; + + /// + /// 统一社会信用代码字符集(不包含I、O、Z、S、V) + /// + private const string CreditCodeChars = "0123456789ABCDEFGHJKLMNPQRTUWXY"; /// - /// 检查社会信用代码是否有效 + /// 校验码权重 /// - /// 社会信用代码 + private static readonly int[] Weights = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 }; + + /// + /// 验证统一社会信用代码是否有效 + /// + /// 统一社会信用代码 /// 是否有效 - public static bool IsValidCreditCode(string creditCode) + public static bool IsValid(string? creditCode) { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { + if (string.IsNullOrWhiteSpace(creditCode)) return false; - } - // 将社会信用代码中的每个字符转换为对应的数字,再计算出校验码 - int sum = 0; - for (int i = 0; i < creditCode.Length - 1; i++) + creditCode = creditCode.Trim().ToUpperInvariant(); + + // 检查长度 + if (creditCode.Length != CreditCodeLength) + return false; + + // 检查字符是否合法 + foreach (var c in creditCode) { - int code = BaseCode.IndexOf(creditCode[i]); - int weight = GetWeight(i + 1); - sum += code * weight; + if (!CreditCodeChars.Contains(c)) + return false; } - int checkCode = (Modulo - sum % Modulo) % Modulo; - int lastCode = BaseCode.IndexOf(creditCode[17]); - - return checkCode == lastCode; + // 验证校验码 + return ValidateCheckCode(creditCode); } /// - /// 获取指定位置的数字权重 + /// 获取统一社会信用代码的类型信息 /// - /// 位置 - /// 数字权重 - private static int GetWeight(int position) + /// 统一社会信用代码 + /// 类型信息 + public static CreditCodeType? GetType(string? creditCode) { - if (position <= 1 || position == 9) - { - return 1; - } - else if (position == 2) - { - return 9; - } - else + if (string.IsNullOrWhiteSpace(creditCode)) + return null; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + if (creditCode.Length != CreditCodeLength) + return null; + + var typeCode = creditCode[0]; + return typeCode switch { - return 9 - position + 2; - } + '1' => CreditCodeType.Institution, + '5' => CreditCodeType.Enterprise, + '9' => CreditCodeType.Other, + 'Y' => CreditCodeType.IndividualBusiness, + _ => null + }; } /// - /// 生成随机的社会信用代码 + /// 获取登记管理部门 /// - /// 随机的社会信用代码 - public static string GenerateRandomCreditCode() + /// 统一社会信用代码 + /// 登记管理部门 + public static string? GetRegistrationAuthority(string? creditCode) { - string orgCode = "911101"; // 默认的组织机构代码 - string entType = "00"; // 默认的企业类型 - string regNum = EasyTool.MathCategory.RandomUtil.RandomNumberString(10); // 生成随机的注册号 - string code = orgCode + entType + regNum; + if (string.IsNullOrWhiteSpace(creditCode)) + return null; - // 计算出校验码并添加到社会信用代码中 - int sum = 0; - for (int i = 0; i < code.Length; i++) - { - int weight = GetWeight(i + 1); - int digit = BaseCode.IndexOf(code[i]); - sum += digit * weight; - } + creditCode = creditCode.Trim().ToUpperInvariant(); - int checkCode = (Modulo - sum % Modulo) % Modulo; - return code + BaseCode[checkCode]; - } + if (creditCode.Length < 2) + return null; - /// - /// 从社会信用代码中提取组织机构代码 - /// - /// 社会信用代码 - /// 组织机构代码 - public static string? GetOrgCodeFromCreditCode(string? creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) + var code = creditCode.Substring(0, 2); + return code switch { - return null; - } - return creditCode.Substring(0, 9); + "11" => "工商行政管理", + "12" => "工商行政管理(个体工商户)", + "13" => "工商行政管理(农民专业合作社)", + "19" => "工商行政管理(其他)", + "21" => "机构编制", + "31" => "外交", + "32" => "文化", + "33" => "教育", + "34" => "卫生", + "35" => "体育", + "36" => "新闻出版", + "37" => "宗教事务", + "41" => "司法行政(律师)", + "42" => "司法行政(公证)", + "43" => "司法行政(基层法律服务)", + "44" => "司法行政(司法鉴定)", + "51" => "民政", + "52" => "民政(社会组织)", + "53" => "民政(基金会)", + "54" => "民政(民办非企业单位)", + "61" => "旅游", + "62" => "文物", + "71" => "工会", + "81" => "公安", + "91" => "其他", + "A1" => "全国人大", + "A2" => "全国政协", + "A3" => "人民法院", + "A4" => "人民检察院", + "A9" => "其他", + "N1" => "军事", + "N2" => "武警", + _ => "未知" + }; } /// - /// 从社会信用代码中提取企业类型 + /// 生成校验码 /// - /// 社会信用代码 - /// 企业类型 - public static string? GetEntTypeFromCreditCode(string? creditCode) + /// 前17位代码 + /// 校验码 + public static char GenerateCheckCode(string creditCode17) { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) + if (string.IsNullOrEmpty(creditCode17) || creditCode17.Length != 17) + throw new ArgumentException("输入必须为17位"); + + int sum = 0; + for (int i = 0; i < 17; i++) { - return null; + var value = CreditCodeChars.IndexOf(char.ToUpperInvariant(creditCode17[i])); + if (value < 0) + throw new ArgumentException($"第{i + 1}位字符无效"); + + sum += value * Weights[i]; } - return creditCode.Substring(9, 2); + var mod = 31 - sum % 31; + return mod == 31 ? '0' : CreditCodeChars[mod]; } - /// - /// 从社会信用代码中提取注册号 - /// - /// 社会信用代码 - /// 注册号 - public static string? GetRegNumFromCreditCode(string? creditCode) + private static bool ValidateCheckCode(string creditCode) { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - - return creditCode.Substring(11, 10); + var expectedCheckCode = GenerateCheckCode(creditCode.Substring(0, 17)); + return creditCode[17] == expectedCheckCode; } /// - /// 从社会信用代码中提取行政区划码 + /// 格式化统一社会信用代码(添加分隔符) /// - /// 社会信用代码 - /// 行政区划码 - public static string? GetAreaCodeFromCreditCode(string? creditCode) + /// 统一社会信用代码 + /// 分隔符 + /// 格式化后的代码 + public static string Format(string? creditCode, string separator = "-") { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } + if (string.IsNullOrWhiteSpace(creditCode)) + return string.Empty; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + if (creditCode.Length != CreditCodeLength) + return creditCode; - return creditCode.Substring(2, 6); + // 格式:XXXXXX-XXXX-XXXX-XXXX + return $"{creditCode.Substring(0, 6)}{separator}{creditCode.Substring(6, 4)}{separator}{creditCode.Substring(10, 4)}{separator}{creditCode.Substring(14, 4)}"; } + } + /// + /// 统一社会信用代码类型 + /// + public enum CreditCodeType + { /// - /// 从社会信用代码中提取机构类型 + /// 机构 /// - /// 社会信用代码 - /// 机构类型 - public static string? GetOrgTypeFromCreditCode(string? creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } + Institution, - return creditCode[8].ToString(); - } + /// + /// 企业 + /// + Enterprise, + + /// + /// 其他 + /// + Other, + /// + /// 个体工商户 + /// + IndividualBusiness } } diff --git a/EasyTool.Core/BusinessCategory/DomainUtil.cs b/EasyTool.Core/BusinessCategory/DomainUtil.cs new file mode 100644 index 0000000..7e7c929 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/DomainUtil.cs @@ -0,0 +1,456 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 域名工具类 + /// + public static class DomainUtil + { + #region 常量与私有字段 + + /// + /// 域名正则表达式 + /// + private static readonly Regex DomainRegex = new( + @"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$", + RegexOptions.Compiled); + + /// + /// IDN(国际化域名)正则 + /// + private static readonly Regex IdnRegex = new( + @"^(?:[a-zA-Z0-9\u4e00-\u9fa5](?:[a-zA-Z0-9-\u4e00-\u9fa5]{0,61}[a-zA-Z0-9\u4e00-\u9fa5])?\.)+[a-zA-Z\u4e00-\u9fa5]{2,}$", + RegexOptions.Compiled); + + /// + /// 顶级域名(TLD)与类型映射 + /// + private static readonly Dictionary TldTypeMap = new(StringComparer.OrdinalIgnoreCase) + { + // 通用顶级域名 + { "com", "商业机构" }, { "net", "网络服务商" }, { "org", "非营利组织" }, + { "edu", "教育机构" }, { "gov", "政府机构" }, { "mil", "军事机构" }, + { "int", "国际组织" }, { "info", "信息服务" }, { "biz", "商业" }, + { "name", "个人" }, { "pro", "专业人士" }, { "museum", "博物馆" }, + { "coop", "合作社" }, { "aero", "航空" }, { "xxx", "成人内容" }, + { "xyz", "通用" }, { "top", "通用" }, { "vip", "VIP" }, + { "site", "网站" }, { "online", "在线" }, { "store", "商店" }, + { "tech", "科技" }, { "fun", "娱乐" }, { "club", "俱乐部" }, + { "shop", "购物" }, { "ltd", "有限公司" }, { "work", "工作" }, + + // 国家/地区顶级域名 + { "cn", "中国" }, { "hk", "香港" }, { "tw", "台湾" }, { "mo", "澳门" }, + { "jp", "日本" }, { "kr", "韩国" }, { "sg", "新加坡" }, { "my", "马来西亚" }, + { "th", "泰国" }, { "vn", "越南" }, { "ph", "菲律宾" }, { "id", "印度尼西亚" }, + { "in", "印度" }, { "pk", "巴基斯坦" }, { "au", "澳大利亚" }, { "nz", "新西兰" }, + { "us", "美国" }, { "ca", "加拿大" }, { "mx", "墨西哥" }, { "br", "巴西" }, + { "uk", "英国" }, { "de", "德国" }, { "fr", "法国" }, { "it", "意大利" }, + { "es", "西班牙" }, { "nl", "荷兰" }, { "be", "比利时" }, { "ch", "瑞士" }, + { "at", "奥地利" }, { "se", "瑞典" }, { "no", "挪威" }, { "dk", "丹麦" }, + { "fi", "芬兰" }, { "ru", "俄罗斯" }, { "pl", "波兰" }, { "cz", "捷克" }, + { "ua", "乌克兰" }, { "tr", "土耳其" }, { "sa", "沙特" }, { "ae", "阿联酋" }, + { "il", "以色列" }, { "za", "南非" }, { "eg", "埃及" }, { "ng", "尼日利亚" }, + { "ke", "肯尼亚" }, { "ar", "阿根廷" }, { "cl", "智利" }, { "co", "哥伦比亚" }, + + // 中国二级域名 + { "com.cn", "中国商业" }, { "net.cn", "中国网络" }, { "org.cn", "中国组织" }, + { "gov.cn", "中国政府" }, { "edu.cn", "中国教育" }, { "ac.cn", "中国科研" }, + { "mil.cn", "中国军事" }, { "bj.cn", "北京" }, { "sh.cn", "上海" }, + { "tj.cn", "天津" }, { "cq.cn", "重庆" }, { "he.cn", "河北" }, + { "sx.cn", "山西" }, { "nm.cn", "内蒙古" }, { "ln.cn", "辽宁" }, + { "jl.cn", "吉林" }, { "hl.cn", "黑龙江" }, { "js.cn", "江苏" }, + { "zj.cn", "浙江" }, { "ah.cn", "安徽" }, { "fj.cn", "福建" }, + { "jx.cn", "江西" }, { "sd.cn", "山东" }, { "ha.cn", "河南" }, + { "hb.cn", "湖北" }, { "hn.cn", "湖南" }, { "gd.cn", "广东" }, + { "gx.cn", "广西" }, { "hi.cn", "海南" }, { "sc.cn", "四川" }, + { "gz.cn", "贵州" }, { "yn.cn", "云南" }, { "xz.cn", "西藏" }, + { "sn.cn", "陕西" }, { "gs.cn", "甘肃" }, { "qh.cn", "青海" }, + { "nx.cn", "宁夏" }, { "xj.cn", "新疆" } + }; + + /// + /// 常见二级域名服务 + /// + private static readonly Dictionary WellKnownDomains = new(StringComparer.OrdinalIgnoreCase) + { + { "baidu.com", "百度" }, { "qq.com", "腾讯" }, { "taobao.com", "淘宝" }, + { "tmall.com", "天猫" }, { "jd.com", "京东" }, { "alipay.com", "支付宝" }, + { "alibaba.com", "阿里巴巴" }, { "aliyun.com", "阿里云" }, + { "tencent.com", "腾讯" }, { "weixin.com", "微信" }, { "wechat.com", "微信" }, + { "douyin.com", "抖音" }, { "tiktok.com", "TikTok" }, { "bytedance.com", "字节跳动" }, + { "meituan.com", "美团" }, { "dianping.com", "大众点评" }, + { "didichuxing.com", "滴滴" }, { "xiaojukeji.com", "滴滴" }, + { "sohu.com", "搜狐" }, { "sina.com.cn", "新浪" }, { "weibo.com", "微博" }, + { "163.com", "网易" }, { "126.com", "网易" }, { "yeah.net", "网易" }, + { "zhihu.com", "知乎" }, { "csdn.net", "CSDN" }, + { "bilibili.com", "哔哩哔哩" }, { "acfun.cn", "AcFun" }, + { "youku.com", "优酷" }, { "iqiyi.com", "爱奇艺" }, { "v.qq.com", "腾讯视频" }, + { "github.com", "GitHub" }, { "gitlab.com", "GitLab" }, { "gitee.com", "Gitee" }, + { "google.com", "Google" }, { "youtube.com", "YouTube" }, { "gmail.com", "Gmail" }, + { "facebook.com", "Facebook" }, { "instagram.com", "Instagram" }, { "whatsapp.com", "WhatsApp" }, + { "twitter.com", "Twitter" }, { "x.com", "X" }, + { "linkedin.com", "LinkedIn" }, { "microsoft.com", "Microsoft" }, + { "apple.com", "Apple" }, { "amazon.com", "Amazon" }, { "aws.amazon.com", "AWS" }, + { "cloudflare.com", "Cloudflare" }, { "nginx.com", "NGINX" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证域名是否有效 + /// + /// 域名 + /// 是否有效 + public static bool IsValid(string? domain) + { + if (string.IsNullOrWhiteSpace(domain)) + { + return false; + } + + // 域名总长度不超过253字符 + if (domain.Length > 253) + { + return false; + } + + string lower = domain.ToLower().Trim(); + + // 检查是否为IDN + if (IdnRegex.IsMatch(lower)) + { + return true; + } + + return DomainRegex.IsMatch(lower); + } + + /// + /// 验证是否为国际顶级域名 + /// + /// 域名 + /// 是否为国际域名 + public static bool IsInternationalDomain(string? domain) + { + if (!IsValid(domain)) + { + return false; + } + + string tld = GetTLD(domain); + if (tld == null) return false; + + // 常见国际顶级域名 + string[] internationalTlds = { "com", "net", "org", "edu", "gov", "mil", "int", "info", "biz", "name", "pro" }; + foreach (var itld in internationalTlds) + { + if (tld.Equals(itld, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + /// + /// 验证是否为中国域名 + /// + /// 域名 + /// 是否为中国域名 + public static bool IsChinaDomain(string? domain) + { + if (!IsValid(domain)) + { + return false; + } + + string tld = GetTLD(domain); + return tld?.Equals("cn", StringComparison.OrdinalIgnoreCase) == true || + domain!.EndsWith(".com.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".net.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".org.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".gov.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".edu.cn", StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 信息提取 + + /// + /// 获取顶级域名(TLD) + /// + /// 域名 + /// 顶级域名 + public static string? GetTLD(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 检查是否为双后缀(如.com.cn) + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld)) + { + return possibleDoubleTld; + } + } + + return parts[^1]; + } + + /// + /// 获取顶级域名类型/归属 + /// + /// 域名 + /// 顶级域名类型 + public static string? GetTLDType(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + // 先检查双后缀 + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.TryGetValue(possibleDoubleTld, out string? type)) + { + return type; + } + } + + string tld = GetTLD(domain); + if (tld != null && TldTypeMap.TryGetValue(tld, out string? tldType)) + { + return tldType; + } + + return null; + } + + /// + /// 获取二级域名 + /// + /// 域名 + /// 二级域名 + public static string? GetSecondLevelDomain(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 处理双后缀 + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld) && parts.Length >= 4) + { + return parts[^3] + "." + possibleDoubleTld; + } + } + + return parts[^2] + "." + parts[^1]; + } + + /// + /// 获取子域名前缀 + /// + /// 域名 + /// 子域名前缀(如www、mail等) + public static string? GetSubdomain(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + + // 计算主域名部分的长度 + int mainDomainParts = 2; + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld)) + { + mainDomainParts = 3; + } + } + + if (parts.Length <= mainDomainParts) + { + return null; // 无子域名 + } + + // 返回除主域名外的部分 + return string.Join(".", parts, 0, parts.Length - mainDomainParts); + } + + /// + /// 获取主域名(不含子域名) + /// + /// 域名 + /// 主域名 + public static string? GetMainDomain(string? domain) + { + string? sld = GetSecondLevelDomain(domain); + return sld; + } + + /// + /// 获取已知服务名称 + /// + /// 域名 + /// 服务名称 + public static string? GetServiceName(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string mainDomain = GetMainDomain(domain)?.ToLower() ?? ""; + + foreach (var kvp in WellKnownDomains) + { + if (mainDomain.Equals(kvp.Key, StringComparison.OrdinalIgnoreCase) || + domain!.EndsWith("." + kvp.Key, StringComparison.OrdinalIgnoreCase)) + { + return kvp.Value; + } + } + + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化域名(转小写,去除协议和路径) + /// + /// 域名 + /// 格式化后的域名 + public static string? Normalize(string? domain) + { + if (string.IsNullOrWhiteSpace(domain)) + { + return null; + } + + string cleaned = domain.ToLower().Trim(); + + // 去除协议 + if (cleaned.StartsWith("http://")) + { + cleaned = cleaned.Substring(7); + } + else if (cleaned.StartsWith("https://")) + { + cleaned = cleaned.Substring(8); + } + + // 去除路径 + int slashIndex = cleaned.IndexOf('/'); + if (slashIndex > 0) + { + cleaned = cleaned.Substring(0, slashIndex); + } + + // 去除端口 + int colonIndex = cleaned.LastIndexOf(':'); + if (colonIndex > 0) + { + cleaned = cleaned.Substring(0, colonIndex); + } + + return IsValid(cleaned) ? cleaned : null; + } + + /// + /// 域名脱敏:e*****.com + /// + /// 域名 + /// 脱敏后的域名 + public static string? Mask(string? domain) + { + string? normalized = Normalize(domain); + if (normalized == null) + { + return null; + } + + string[] parts = normalized.Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 脱敏主域名部分 + string mainPart = parts[0]; + if (mainPart.Length <= 2) + { + parts[0] = mainPart[0] + "*"; + } + else + { + parts[0] = mainPart[0] + new string('*', mainPart.Length - 2) + mainPart[^1]; + } + + return string.Join(".", parts); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机域名(仅供测试使用) + /// + /// 顶级域名(可选,默认.com) + /// 随机域名 + public static string GenerateRandom(string? tld = null) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + string suffix = tld ?? "com"; + + // 生成随机主域名(6-12位) + int length = MathCategory.RandomUtil.RandomInt(6, 13); + string main = ""; + for (int i = 0; i < length; i++) + { + main += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + return main + "." + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs b/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs new file mode 100644 index 0000000..b4b834c --- /dev/null +++ b/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs @@ -0,0 +1,319 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 驾驶证号工具类 + /// + public static class DrivingLicenseUtil + { + #region 常量与私有字段 + + /// + /// 驾驶证号正则表达式(18位,与身份证号格式相同) + /// + private static readonly Regex LicenseRegex = new( + @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 档案编号正则表达式(12位数字) + /// + private static readonly Regex FileNumberRegex = new(@"^\d{12}$", RegexOptions.Compiled); + + /// + /// 驾驶证校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 驾驶证校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 准驾车型映射 + /// + private static readonly (string Code, string Name, string Description)[] VehicleClassMap = + { + ("A1", "大型客车", "可驾驶A3、B1、B2、C1、C2、C3、C4、M"), + ("A2", "牵引车", "可驾驶B1、B2、C1、C2、C3、C4、M"), + ("A3", "城市公交车", "可驾驶C1、C2、C3、C4"), + ("B1", "中型客车", "可驾驶C1、C2、C3、C4、M"), + ("B2", "大型货车", "可驾驶C1、C2、C3、C4、M"), + ("C1", "小型汽车", "可驾驶C2、C3、C4"), + ("C2", "小型自动挡汽车", "仅限自动挡小型汽车"), + ("C3", "低速载货汽车", "可驾驶C4"), + ("C4", "三轮汽车", ""), + ("C5", "残疾人专用小型自动挡汽车", ""), + ("C6", "轻型牵引挂车", "需C1或C2以上驾照增驾"), + ("D", "普通三轮摩托车", "可驾驶E、F"), + ("E", "普通二轮摩托车", "可驾驶F"), + ("F", "轻便摩托车", ""), + ("G", "拖拉机", ""), + ("H", "轮式自行机械", ""), + ("M", "轮式自行机械车", ""), + ("N", "无轨电车", ""), + ("P", "有轨电车", "") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证驾驶证号是否有效 + /// + /// 驾驶证号 + /// 是否有效 + public static bool IsValid(string? licenseNumber) + { + if (string.IsNullOrWhiteSpace(licenseNumber)) + { + return false; + } + + if (!LicenseRegex.IsMatch(licenseNumber)) + { + return false; + } + + // 验证日期有效性 + if (!IsValidDate(licenseNumber.Substring(6, 8))) + { + return false; + } + + // 验证校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (licenseNumber[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + char actualCheckCode = char.ToUpper(licenseNumber[17]); + + return expectedCheckCode == actualCheckCode; + } + + /// + /// 验证档案编号是否有效 + /// + /// 档案编号 + /// 是否有效 + public static bool IsValidFileNumber(string? fileNumber) + { + if (string.IsNullOrWhiteSpace(fileNumber)) + { + return false; + } + + return FileNumberRegex.IsMatch(fileNumber); + } + + #endregion + + #region 信息提取 + + /// + /// 获取出生日期 + /// + /// 驾驶证号 + /// 出生日期 + public static DateTime? GetBirthday(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + int year = int.Parse(licenseNumber!.Substring(6, 4)); + int month = int.Parse(licenseNumber.Substring(10, 2)); + int day = int.Parse(licenseNumber.Substring(12, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取性别(1男2女) + /// + /// 驾驶证号 + /// 性别代码 + public static int? GetGender(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + int genderDigit = licenseNumber![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 驾驶证号 + /// 性别 + public static string? GetGenderString(string? licenseNumber) + { + int? gender = GetGender(licenseNumber); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取行政区划代码 + /// + /// 驾驶证号 + /// 行政区划代码 + public static string? GetAreaCode(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + return licenseNumber!.Substring(0, 6); + } + + /// + /// 判断驾驶证号是否与身份证号一致 + /// + /// 驾驶证号 + /// 身份证号 + /// 是否一致 + public static bool MatchesIdCard(string? licenseNumber, string? idCard) + { + if (!IsValid(licenseNumber) || !IdCardUtil.IsValid18(idCard)) + { + return false; + } + + return licenseNumber!.Equals(idCard!, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 准驾车型 + + /// + /// 获取准驾车型信息 + /// + /// 准驾车型代码 + /// 车型信息 + public static (string Name, string Description)? GetVehicleClassInfo(string? vehicleClass) + { + if (string.IsNullOrWhiteSpace(vehicleClass)) + { + return null; + } + + foreach (var info in VehicleClassMap) + { + if (info.Code.Equals(vehicleClass, StringComparison.OrdinalIgnoreCase)) + { + return (info.Name, info.Description); + } + } + + return null; + } + + /// + /// 获取准驾车型名称 + /// + /// 准驾车型代码 + /// 车型名称 + public static string? GetVehicleClassName(string? vehicleClass) + { + var info = GetVehicleClassInfo(vehicleClass); + return info?.Name; + } + + /// + /// 验证准驾车型代码是否有效 + /// + /// 准驾车型代码 + /// 是否有效 + public static bool IsValidVehicleClass(string? vehicleClass) + { + return GetVehicleClassInfo(vehicleClass) != null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化驾驶证号(转大写) + /// + /// 驾驶证号 + /// 格式化后的驾驶证号 + public static string? Normalize(string? licenseNumber) + { + if (string.IsNullOrWhiteSpace(licenseNumber)) + { + return null; + } + + string upper = licenseNumber.ToUpper().Trim(); + return upper.Length == 18 && LicenseRegex.IsMatch(upper) ? upper : null; + } + + /// + /// 驾驶证号脱敏:110***********1234 + /// + /// 驾驶证号 + /// 脱敏后的驾驶证号 + public static string? Mask(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + return licenseNumber!.Substring(0, 3) + "***********" + licenseNumber.Substring(14); + } + + #endregion + + #region 私有方法 + + /// + /// 验证日期字符串是否有效 + /// + private static bool IsValidDate(string dateStr) + { + if (dateStr.Length != 8) + { + return false; + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + if (year < 1900 || year > DateTime.Now.Year) + { + return false; + } + + if (month < 1 || month > 12) + { + return false; + } + + int maxDay = DateTime.DaysInMonth(year, month); + return day >= 1 && day <= maxDay; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/EmailUtil.cs b/EasyTool.Core/BusinessCategory/EmailUtil.cs new file mode 100644 index 0000000..903a7f3 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/EmailUtil.cs @@ -0,0 +1,518 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 邮箱服务提供商枚举 + /// + public enum EmailProvider + { + /// + /// 未知服务商 + /// + Unknown = 0, + + /// + /// QQ邮箱 + /// + QQ = 1, + + /// + /// 网易163邮箱 + /// + NetEase163 = 2, + + /// + /// 网易126邮箱 + /// + NetEase126 = 3, + + /// + /// 网易yeah邮箱 + /// + NetEaseYeah = 4, + + /// + /// 新浪邮箱 + /// + Sina = 5, + + /// + /// 搜狐邮箱 + /// + Sohu = 6, + + /// + /// Gmail + /// + Gmail = 7, + + /// + /// Outlook/Hotmail + /// + Outlook = 8, + + /// + /// Yahoo + /// + Yahoo = 9, + + /// + /// iCloud + /// + ICloud = 10, + + /// + /// 阿里云邮箱 + /// + Aliyun = 11, + + /// + /// 企业邮箱 + /// + Enterprise = 12 + } + + /// + /// 邮箱工具类 + /// + public static class EmailUtil + { + #region 常量与私有字段 + + /// + /// 邮箱正则表达式(标准格式) + /// + private static readonly Regex EmailRegex = new Regex( + @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", + RegexOptions.Compiled); + + /// + /// 简单邮箱正则表达式(用于快速验证) + /// + private static readonly Regex SimpleEmailRegex = new Regex( + @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", + RegexOptions.Compiled); + + /// + /// 邮箱服务商域名映射 + /// + private static readonly Dictionary ProviderDomainMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + // QQ邮箱 + { "qq.com", EmailProvider.QQ }, + { "foxmail.com", EmailProvider.QQ }, + { "vip.qq.com", EmailProvider.QQ }, + + // 网易邮箱 + { "163.com", EmailProvider.NetEase163 }, + { "vip.163.com", EmailProvider.NetEase163 }, + { "126.com", EmailProvider.NetEase126 }, + { "vip.126.com", EmailProvider.NetEase126 }, + { "yeah.net", EmailProvider.NetEaseYeah }, + + // 新浪邮箱 + { "sina.com", EmailProvider.Sina }, + { "sina.cn", EmailProvider.Sina }, + { "vip.sina.com", EmailProvider.Sina }, + + // 搜狐邮箱 + { "sohu.com", EmailProvider.Sohu }, + { "vip.sohu.com", EmailProvider.Sohu }, + + // Gmail + { "gmail.com", EmailProvider.Gmail }, + { "googlemail.com", EmailProvider.Gmail }, + + // Outlook/Hotmail + { "outlook.com", EmailProvider.Outlook }, + { "hotmail.com", EmailProvider.Outlook }, + { "live.com", EmailProvider.Outlook }, + { "msn.com", EmailProvider.Outlook }, + + // Yahoo + { "yahoo.com", EmailProvider.Yahoo }, + { "yahoo.cn", EmailProvider.Yahoo }, + { "yahoo.com.cn", EmailProvider.Yahoo }, + { "yahoo.co.jp", EmailProvider.Yahoo }, + { "ymail.com", EmailProvider.Yahoo }, + + // iCloud + { "icloud.com", EmailProvider.ICloud }, + { "me.com", EmailProvider.ICloud }, + { "mac.com", EmailProvider.ICloud }, + + // 阿里云邮箱 + { "aliyun.com", EmailProvider.Aliyun }, + { "aliyuncs.com", EmailProvider.Aliyun } + }; + + /// + /// 常见企业邮箱域名 + /// + private static readonly HashSet EnterpriseDomains = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "exmail.qq.com", // 腾讯企业邮 + "qiye.163.com", // 网易企业邮 + "qiye.aliyun.com", // 阿里企业邮 + "corp.sina.com", // 新浪企业邮 + }; + + #endregion + + #region 验证方法 + + /// + /// 验证邮箱格式是否有效(标准验证) + /// + /// 邮箱地址 + /// 是否有效 + public static bool IsValid(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + // 长度检查(RFC 5321规定最大254字符) + if (email.Length > 254) + { + return false; + } + + return EmailRegex.IsMatch(email); + } + + /// + /// 快速验证邮箱格式(简单验证,性能更好) + /// + /// 邮箱地址 + /// 是否有效 + public static bool IsValidQuick(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + if (email.Length > 254) + { + return false; + } + + return SimpleEmailRegex.IsMatch(email); + } + + /// + /// 验证邮箱格式并规范化 + /// + /// 邮箱地址 + /// 规范化后的邮箱地址,无效返回null + public static string? Normalize(string? email) + { + if (!IsValid(email)) + { + return null; + } + + // 转小写,去除首尾空格 + return email!.Trim().ToLower(); + } + + #endregion + + #region 解析方法 + + /// + /// 获取邮箱用户名(@之前的部分) + /// + /// 邮箱地址 + /// 用户名,无效邮箱返回null + public static string? GetUsername(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(0, atIndex); + } + + /// + /// 获取邮箱域名(@之后的部分) + /// + /// 邮箱地址 + /// 域名,无效邮箱返回null + public static string? GetDomain(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(atIndex + 1).ToLower(); + } + + /// + /// 获取邮箱顶级域名 + /// + /// 邮箱地址 + /// 顶级域名(如.com、.cn),无效邮箱返回null + public static string? GetTopLevelDomain(string? email) + { + string? domain = GetDomain(email); + if (domain == null) + { + return null; + } + + int lastDotIndex = domain.LastIndexOf('.'); + if (lastDotIndex < 0) + { + return null; + } + + return domain.Substring(lastDotIndex); + } + + #endregion + + #region 服务商识别 + + /// + /// 获取邮箱服务商 + /// + /// 邮箱地址 + /// 邮箱服务商枚举 + public static EmailProvider GetProvider(string? email) + { + string? domain = GetDomain(email); + if (domain == null) + { + return EmailProvider.Unknown; + } + + // 检查企业邮箱 + if (EnterpriseDomains.Contains(domain)) + { + return EmailProvider.Enterprise; + } + + // 检查已知服务商 + if (ProviderDomainMap.TryGetValue(domain, out EmailProvider provider)) + { + return provider; + } + + // 检查子域名(如 vip.qq.com) + foreach (var kvp in ProviderDomainMap) + { + if (domain.EndsWith("." + kvp.Key, StringComparison.OrdinalIgnoreCase)) + { + return kvp.Value; + } + } + + return EmailProvider.Unknown; + } + + /// + /// 获取邮箱服务商名称 + /// + /// 邮箱地址 + /// 服务商名称 + public static string? GetProviderName(string? email) + { + EmailProvider provider = GetProvider(email); + return provider switch + { + EmailProvider.QQ => "QQ邮箱", + EmailProvider.NetEase163 => "163邮箱", + EmailProvider.NetEase126 => "126邮箱", + EmailProvider.NetEaseYeah => "Yeah邮箱", + EmailProvider.Sina => "新浪邮箱", + EmailProvider.Sohu => "搜狐邮箱", + EmailProvider.Gmail => "Gmail", + EmailProvider.Outlook => "Outlook", + EmailProvider.Yahoo => "Yahoo邮箱", + EmailProvider.ICloud => "iCloud", + EmailProvider.Aliyun => "阿里云邮箱", + EmailProvider.Enterprise => "企业邮箱", + _ => null + }; + } + + /// + /// 判断是否为企业邮箱 + /// + /// 邮箱地址 + /// 是否为企业邮箱 + public static bool IsEnterpriseEmail(string? email) + { + EmailProvider provider = GetProvider(email); + + // 已知企业邮箱域名 + if (provider == EmailProvider.Enterprise) + { + return true; + } + + // 未知服务商可能是企业邮箱 + if (provider == EmailProvider.Unknown) + { + string? domain = GetDomain(email); + // 排除常见个人邮箱域名后的其他域名可能是企业邮箱 + return domain != null && !IsCommonPublicDomain(domain); + } + + return false; + } + + /// + /// 判断是否为常见公共邮箱域名 + /// + private static bool IsCommonPublicDomain(string domain) + { + return ProviderDomainMap.ContainsKey(domain); + } + + #endregion + + #region 格式化方法 + + /// + /// 邮箱脱敏:t***@qq.com + /// + /// 邮箱地址 + /// 脱敏后的邮箱地址 + public static string? Mask(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + string username = email.Substring(0, atIndex); + string domain = email.Substring(atIndex); + + if (username.Length <= 1) + { + return "*" + domain; + } + else if (username.Length <= 3) + { + return username[0] + new string('*', username.Length - 1) + domain; + } + else + { + return username.Substring(0, 2) + new string('*', username.Length - 2) + domain; + } + } + + /// + /// 邮箱脱敏(自定义脱敏字符数) + /// + /// 邮箱地址 + /// 用户名可见字符数 + /// 脱敏后的邮箱地址 + public static string? Mask(string? email, int visibleChars) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + string username = email.Substring(0, atIndex); + string domain = email.Substring(atIndex); + + if (visibleChars <= 0) + { + return new string('*', Math.Min(username.Length, 3)) + domain; + } + + if (visibleChars >= username.Length) + { + return email; + } + + return username.Substring(0, visibleChars) + new string('*', username.Length - visibleChars) + domain; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机邮箱(仅供测试使用) + /// + /// 邮箱服务商(可选,默认随机) + /// 随机邮箱地址 + public static string GenerateRandom(EmailProvider? provider = null) + { + string username = GenerateRandomUsername(8); + string domain; + + if (provider.HasValue && provider.Value != EmailProvider.Unknown && provider.Value != EmailProvider.Enterprise) + { + domain = GetDomainByProvider(provider.Value); + } + else + { + // 随机选择一个服务商 + var providers = new[] { EmailProvider.QQ, EmailProvider.NetEase163, EmailProvider.Gmail, EmailProvider.Outlook }; + var randomProvider = EasyTool.MathCategory.RandomUtil.GetRandomElement(providers); + domain = GetDomainByProvider(randomProvider); + } + + return username + "@" + domain; + } + + #endregion + + #region 私有方法 + + /// + /// 根据服务商获取域名 + /// + private static string GetDomainByProvider(EmailProvider provider) + { + return provider switch + { + EmailProvider.QQ => "qq.com", + EmailProvider.NetEase163 => "163.com", + EmailProvider.NetEase126 => "126.com", + EmailProvider.NetEaseYeah => "yeah.net", + EmailProvider.Sina => "sina.com", + EmailProvider.Sohu => "sohu.com", + EmailProvider.Gmail => "gmail.com", + EmailProvider.Outlook => "outlook.com", + EmailProvider.Yahoo => "yahoo.com", + EmailProvider.ICloud => "icloud.com", + EmailProvider.Aliyun => "aliyun.com", + _ => "example.com" + }; + } + + /// + /// 生成随机用户名 + /// + private static string GenerateRandomUsername(int length) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + var sb = new System.Text.StringBuilder(length); + for (int i = 0; i < length; i++) + { + sb.Append(EasyTool.MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray())); + } + return sb.ToString(); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs b/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs new file mode 100644 index 0000000..142e842 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs @@ -0,0 +1,368 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 外国人永久居留身份证工具类 + /// + public static class ForeignerIdUtil + { + #region 常量与私有字段 + + /// + /// 外国人永久居留身份证正则表达式(15位) + /// + private static readonly Regex ForeignerId15Regex = new( + @"^[A-Z]{3}\d{12}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 新版外国人永久居留身份证正则表达式(18位) + /// 格式与普通身份证相同,但用于外国人 + /// + private static readonly Regex ForeignerId18Regex = new( + @"^[A-Z]\d{17}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 校验码权重(18位版本) + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表(18位版本) + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 国籍代码映射(部分常见国家) + /// + private static readonly Dictionary NationalityMap = new(StringComparer.OrdinalIgnoreCase) + { + { "USA", "美国" }, { "GBR", "英国" }, { "JPN", "日本" }, { "KOR", "韩国" }, + { "DEU", "德国" }, { "FRA", "法国" }, { "ITA", "意大利" }, { "ESP", "西班牙" }, + { "CAN", "加拿大" }, { "AUS", "澳大利亚" }, { "NZL", "新西兰" }, { "RUS", "俄罗斯" }, + { "IND", "印度" }, { "THA", "泰国" }, { "VNM", "越南" }, { "MYS", "马来西亚" }, + { "SGP", "新加坡" }, { "IDN", "印度尼西亚" }, { "PHL", "菲律宾" }, { "MMR", "缅甸" }, + { "PAK", "巴基斯坦" }, { "BGD", "孟加拉国" }, { "BRA", "巴西" }, { "MEX", "墨西哥" }, + { "ZAF", "南非" }, { "EGY", "埃及" }, { "NGA", "尼日利亚" }, { "KEN", "肯尼亚" }, + { "CHN", "中国" }, { "HKG", "香港" }, { "MAC", "澳门" }, { "TWN", "台湾" } + }; + + /// + /// 省份代码映射 + /// + private static readonly Dictionary ProvinceCodeMap = new() + { + { "11", "北京" }, { "12", "天津" }, { "13", "河北" }, { "14", "山西" }, + { "15", "内蒙古" }, { "21", "辽宁" }, { "22", "吉林" }, { "23", "黑龙江" }, + { "31", "上海" }, { "32", "江苏" }, { "33", "浙江" }, { "34", "安徽" }, + { "35", "福建" }, { "36", "江西" }, { "37", "山东" }, { "41", "河南" }, + { "42", "湖北" }, { "43", "湖南" }, { "44", "广东" }, { "45", "广西" }, + { "46", "海南" }, { "50", "重庆" }, { "51", "四川" }, { "52", "贵州" }, + { "53", "云南" }, { "54", "西藏" }, { "61", "陕西" }, { "62", "甘肃" }, + { "63", "青海" }, { "64", "宁夏" }, { "65", "新疆" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证外国人永久居留身份证是否有效 + /// + /// 外国人永久居留身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 15位格式(旧版) + if (cleaned.Length == 15 && ForeignerId15Regex.IsMatch(cleaned)) + { + return true; + } + + // 18位格式(新版) + if (cleaned.Length == 18 && ForeignerId18Regex.IsMatch(cleaned)) + { + return ValidateCheckDigit18(cleaned); + } + + return false; + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 外国人永久居留身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return ForeignerId15Regex.IsMatch(cleaned) || ForeignerId18Regex.IsMatch(cleaned); + } + + /// + /// 验证是否为15位格式 + /// + /// 外国人永久居留身份证号 + /// 是否为15位格式 + public static bool Is15Digit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return ForeignerId15Regex.IsMatch(idCard.ToUpper().Trim()); + } + + /// + /// 验证是否为18位格式 + /// + /// 外国人永久居留身份证号 + /// 是否为18位格式 + public static bool Is18Digit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return cleaned.Length == 18 && ForeignerId18Regex.IsMatch(cleaned); + } + + /// + /// 验证18位校验码 + /// + private static bool ValidateCheckDigit18(string idCard) + { + if (idCard.Length != 18) + { + return false; + } + + // 字母转换(A=10, B=11, ..., Z=35) + char firstChar = char.ToUpper(idCard[0]); + int firstValue; + if (firstChar >= 'A' && firstChar <= 'Z') + { + firstValue = firstChar - 'A' + 10; + } + else + { + return false; + } + + // 计算加权和 + int sum = 0; + + // 第一位字母的权重处理 + sum += (firstValue / 10) * Weights[0]; + sum += (firstValue % 10) * Weights[1]; + + // 数字部分 + for (int i = 1; i < 17; i++) + { + if (!char.IsDigit(idCard[i])) + { + return false; + } + sum += (idCard[i] - '0') * Weights[i + 1]; + } + + // 计算校验码 + char expectedCheck = CheckCodes[sum % 11]; + return char.ToUpper(idCard[17]) == expectedCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国籍代码(15位格式前3位) + /// + /// 外国人永久居留身份证号 + /// 国籍代码 + public static string? GetNationalityCode(string? idCard) + { + if (Is15Digit(idCard)) + { + return idCard!.Substring(0, 3).ToUpper(); + } + + return null; + } + + /// + /// 获取国籍名称 + /// + /// 外国人永久居留身份证号 + /// 国籍名称 + public static string? GetNationality(string? idCard) + { + string? code = GetNationalityCode(idCard); + if (code == null) + { + return null; + } + + return NationalityMap.TryGetValue(code, out string? nationality) ? nationality : code; + } + + /// + /// 获取省份代码(18位格式的第2-3位) + /// + /// 外国人永久居留身份证号 + /// 省份代码 + public static string? GetProvinceCode(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + return idCard!.Substring(1, 2); + } + + /// + /// 获取省份名称 + /// + /// 外国人永久居留身份证号 + /// 省份名称 + public static string? GetProvince(string? idCard) + { + string? code = GetProvinceCode(idCard); + if (code == null) + { + return null; + } + + return ProvinceCodeMap.TryGetValue(code, out string? province) ? province : null; + } + + /// + /// 获取出生日期(18位格式) + /// + /// 外国人永久居留身份证号 + /// 出生日期 + public static DateTime? GetBirthday(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + string cleaned = idCard!.Substring(1); // 去掉首字母 + int year = int.Parse(cleaned.Substring(5, 4)); + int month = int.Parse(cleaned.Substring(9, 2)); + int day = int.Parse(cleaned.Substring(11, 2)); + + try + { + return new DateTime(year, month, day); + } + catch + { + return null; + } + } + + /// + /// 获取性别(18位格式) + /// + /// 外国人永久居留身份证号 + /// 性别(1男2女) + public static int? GetGender(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + int genderDigit = idCard![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 外国人永久居留身份证号 + /// 性别 + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化外国人永久居留身份证(统一大写) + /// + /// 外国人永久居留身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 外国人永久居留身份证脱敏 + /// 15位:USA********* + /// 18位:A110**********1 + /// + /// 外国人永久居留身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + + if (cleaned.Length == 15) + { + return cleaned.Substring(0, 3) + "***********" + cleaned.Substring(14); + } + + if (cleaned.Length == 18) + { + return cleaned.Substring(0, 4) + "***********" + cleaned.Substring(15); + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs b/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs new file mode 100644 index 0000000..295bbbb --- /dev/null +++ b/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 香港身份证工具类 + /// + public static class HKIdCardUtil + { + #region 常量与私有字段 + + /// + /// 香港身份证正则表达式 + /// 格式:1-2个英文字母 + 6位数字 + 括号内1位校验码 + /// 例如:A123456(7), AB123456(7) + /// + private static readonly Regex HKIdCardRegex = new( + @"^[A-Z]{1,2}\d{6}\([\dA]\)$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 香港身份证前缀与含义映射 + /// + private static readonly string[] PrefixMeanings = new string[] + { + "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "Y", "Z" + }; + + /// + /// 首字母对应的数字值(A=1, B=2, ..., Z=26) + /// + private static int GetLetterValue(char letter) + { + return char.ToUpper(letter) - 'A' + 1; + } + + #endregion + + #region 验证方法 + + /// + /// 验证香港身份证是否有效 + /// + /// 香港身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 检查格式 + if (!HKIdCardRegex.IsMatch(cleaned)) + { + return false; + } + + // 验证校验码 + return ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 香港身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return HKIdCardRegex.IsMatch(idCard.ToUpper().Trim()); + } + + /// + /// 验证校验码 + /// + private static bool ValidateCheckDigit(string idCard) + { + // 提取校验码(括号内的字符) + int parenStart = idCard.IndexOf('('); + int parenEnd = idCard.IndexOf(')'); + if (parenStart < 0 || parenEnd < 0 || parenEnd <= parenStart) + { + return false; + } + + string checkChar = idCard.Substring(parenStart + 1, parenEnd - parenStart - 1); + if (checkChar.Length != 1) + { + return false; + } + + // 计算校验码 + char? expectedCheck = CalculateCheckDigit(idCard); + if (expectedCheck == null) + { + return false; + } + + return char.ToUpper(checkChar[0]) == expectedCheck.Value; + } + + /// + /// 计算校验码 + /// + /// 香港身份证号(含括号格式) + /// 校验码字符 + public static char? CalculateCheckDigit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return null; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 提取字母部分和数字部分 + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + if (digitStart < 0) + { + return null; + } + + string letters = cleaned.Substring(0, digitStart); + string digits = cleaned.Substring(digitStart, 6); + + // 计算加权和 + int sum = 0; + int weight = 9 - (2 - letters.Length); // 根据字母数量调整起始权重 + + // 如果只有一个字母,第一位按36处理(相当于前面有一个空位,值为36) + if (letters.Length == 1) + { + sum += 36 * 9; + sum += GetLetterValue(letters[0]) * 8; + } + else if (letters.Length == 2) + { + sum += GetLetterValue(letters[0]) * 9; + sum += GetLetterValue(letters[1]) * 8; + } + else + { + return null; + } + + // 数字部分权重为7到2 + int[] digitWeights = { 7, 6, 5, 4, 3, 2 }; + for (int i = 0; i < 6; i++) + { + sum += (digits[i] - '0') * digitWeights[i]; + } + + // 计算校验码 + int remainder = sum % 11; + int checkValue; + + if (remainder == 0) + { + checkValue = 0; + } + else + { + checkValue = 11 - remainder; + } + + // 返回校验码字符 + if (checkValue == 10) + { + return 'A'; + } + else + { + return (char)('0' + checkValue); + } + } + + #endregion + + #region 信息提取 + + /// + /// 获取身份证前缀字母 + /// + /// 香港身份证号 + /// 前缀字母 + public static string? GetPrefix(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + return digitStart > 0 ? cleaned.Substring(0, digitStart) : null; + } + + /// + /// 获取数字部分(6位) + /// + /// 香港身份证号 + /// 6位数字 + public static string? GetDigitPart(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + return digitStart >= 0 ? cleaned.Substring(digitStart, 6) : null; + } + + /// + /// 获取校验码 + /// + /// 香港身份证号 + /// 校验码字符 + public static char? GetCheckDigit(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int parenStart = cleaned.IndexOf('('); + int parenEnd = cleaned.IndexOf(')'); + + if (parenStart < 0 || parenEnd < 0 || parenEnd <= parenStart) + { + return null; + } + + return cleaned[parenStart + 1]; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化香港身份证(统一大写,带括号) + /// + /// 香港身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 格式化为标准格式(确保括号正确) + /// + /// 香港身份证号 + /// 标准格式的身份证号 + public static string? Format(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + return cleaned; + } + + /// + /// 香港身份证脱敏:A12***(7) + /// + /// 香港身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int parenStart = cleaned.IndexOf('('); + + if (parenStart < 7) + { + return null; + } + + // 保留前缀+2位数字,中间用*替代,保留校验码 + string prefix = cleaned.Substring(0, parenStart - 4); + string suffix = cleaned.Substring(parenStart); + + return prefix + "****" + suffix; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机香港身份证号(仅供测试使用) + /// + /// 前缀字母(可选,默认随机) + /// 香港身份证号 + public static string GenerateRandom(string? prefix = null) + { + const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + + // 前缀 + string prefixLetters; + if (string.IsNullOrEmpty(prefix)) + { + int letterCount = MathCategory.RandomUtil.RandomInt(1, 3); + prefixLetters = ""; + for (int i = 0; i < letterCount; i++) + { + prefixLetters += MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()); + } + } + else + { + prefixLetters = prefix.ToUpper(); + } + + // 6位数字 + string numberPart = ""; + for (int i = 0; i < 6; i++) + { + numberPart += MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray()); + } + + // 计算校验码 + string tempId = prefixLetters + numberPart + "(0)"; + char? checkDigit = CalculateCheckDigit(tempId); + + return $"{prefixLetters}{numberPart}({checkDigit ?? '0'})"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ICCIDUtil.cs b/EasyTool.Core/BusinessCategory/ICCIDUtil.cs new file mode 100644 index 0000000..0c5ba59 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ICCIDUtil.cs @@ -0,0 +1,412 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// SIM卡ICCID工具类 + /// ICCID (Integrated Circuit Card Identifier) 是SIM卡的唯一识别号 + /// + public static class ICCIDUtil + { + #region 常量与私有字段 + + /// + /// ICCID正则表达式(19-20位数字) + /// + private static readonly Regex ICCIDRegex = new( + @"^\d{19,20}$", + RegexOptions.Compiled); + + /// + /// 移动国家代码(MCC)映射 + /// + private static readonly Dictionary MccMap = new() + { + { "460", "中国" }, + { "001", "美国" }, + { "004", "阿富汗" }, + { "208", "法国" }, + { "234", "英国" }, + { "262", "德国" }, + { "310", "美国" }, + { "440", "日本" }, + { "450", "韩国" }, + { "505", "澳大利亚" }, + { "530", "新西兰" }, + { "724", "巴西" } + }; + + /// + /// 中国移动网络代码(MNC)映射 + /// + private static readonly Dictionary ChinaMncMap = new() + { + { "00", "中国移动" }, + { "02", "中国移动" }, + { "04", "中国移动" }, + { "07", "中国移动" }, + { "08", "中国移动" }, + { "01", "中国联通" }, + { "06", "中国联通" }, + { "09", "中国联通" }, + { "03", "中国电信" }, + { "05", "中国电信" }, + { "11", "中国电信" }, + { "15", "中国广电" } + }; + + /// + /// Luhn算法校验码权重 + /// + private static readonly int[] LuhnWeights = { 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 }; + + #endregion + + #region 验证方法 + + /// + /// 验证ICCID是否有效 + /// + /// ICCID号 + /// 是否有效 + public static bool IsValid(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return false; + } + + return ValidateLuhn(iccid!); + } + + /// + /// 验证ICCID格式 + /// + /// ICCID号 + /// 格式是否正确 + public static bool IsValidFormat(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return false; + } + + string cleaned = iccid.Trim(); + return ICCIDRegex.IsMatch(cleaned); + } + + /// + /// 使用Luhn算法验证ICCID + /// + /// ICCID号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return false; + } + + string cleaned = iccid.Trim(); + int sum = 0; + int length = cleaned.Length; + + // 从右向左,第1位是校验位 + for (int i = length - 2; i >= 0; i--) + { + if (!char.IsDigit(cleaned[i])) + { + return false; + } + + int digit = cleaned[i] - '0'; + int weightIndex = (length - 2 - i); + int multiplier = (weightIndex % 2 == 0) ? 2 : 1; + + digit *= multiplier; + if (digit > 9) + { + digit -= 9; + } + + sum += digit; + } + + int checkDigit = (10 - (sum % 10)) % 10; + int actualCheck = cleaned[length - 1] - '0'; + + return checkDigit == actualCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取移动国家代码(MCC,前3位) + /// + /// ICCID号 + /// 移动国家代码 + public static string? GetMCC(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid!.Substring(0, 3); + } + + /// + /// 获取国家名称 + /// + /// ICCID号 + /// 国家名称 + public static string? GetCountry(string? iccid) + { + string? mcc = GetMCC(iccid); + if (mcc == null) + { + return null; + } + + return MccMap.TryGetValue(mcc, out string? country) ? country : null; + } + + /// + /// 获取移动网络代码(MNC,第4-5位) + /// + /// ICCID号 + /// 移动网络代码 + public static string? GetMNC(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid!.Substring(3, 2); + } + + /// + /// 获取运营商名称(仅支持中国运营商) + /// + /// ICCID号 + /// 运营商名称 + public static string? GetCarrier(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string? mcc = GetMCC(iccid); + if (mcc != "460") + { + return null; // 非中国卡 + } + + string mnc = iccid!.Substring(3, 2); + return ChinaMncMap.TryGetValue(mnc, out string? carrier) ? carrier : null; + } + + /// + /// 判断是否为中国移动 + /// + public static bool IsChinaMobile(string? iccid) => GetCarrier(iccid) == "中国移动"; + + /// + /// 判断是否为中国联通 + /// + public static bool IsChinaUnicom(string? iccid) => GetCarrier(iccid) == "中国联通"; + + /// + /// 判断是否为中国电信 + /// + public static bool IsChinaTelecom(string? iccid) => GetCarrier(iccid) == "中国电信"; + + /// + /// 判断是否为中国广电 + /// + public static bool IsChinaBroadnet(string? iccid) => GetCarrier(iccid) == "中国广电"; + + /// + /// 获取发卡省份代码(第9-10位) + /// + /// ICCID号 + /// 省份代码 + public static string? GetProvinceCode(string? iccid) + { + if (!IsValidFormat(iccid) || iccid!.Length < 10) + { + return null; + } + + return iccid.Substring(8, 2); + } + + /// + /// 获取序列号(第11-19位,不含校验位) + /// + /// ICCID号 + /// 序列号 + public static string? GetSerialNumber(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + int length = iccid!.Length; + return iccid.Substring(10, length - 11); + } + + /// + /// 获取校验位(最后一位) + /// + /// ICCID号 + /// 校验位 + public static int? GetCheckDigit(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid![iccid.Length - 1] - '0'; + } + + /// + /// 解析ICCID结构 + /// + /// ICCID号 + /// ICCID结构信息 + public static ICCDInfo? Parse(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return new ICCDInfo + { + MCC = GetMCC(iccid), + Country = GetCountry(iccid), + MNC = GetMNC(iccid), + Carrier = GetCarrier(iccid), + ProvinceCode = GetProvinceCode(iccid), + SerialNumber = GetSerialNumber(iccid), + CheckDigit = GetCheckDigit(iccid) + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化ICCID(去除空格和分隔符) + /// + /// ICCID号 + /// 格式化后的ICCID + public static string? Normalize(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return null; + } + + string cleaned = iccid.Trim(); + return ICCIDRegex.IsMatch(cleaned) ? cleaned : null; + } + + /// + /// 格式化为易读格式:898600 00 00 1234567890 + /// + /// ICCID号 + /// 格式化后的ICCID + public static string? Format(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string cleaned = iccid!.Trim(); + if (cleaned.Length == 19) + { + return $"{cleaned.Substring(0, 6)} {cleaned.Substring(6, 2)} {cleaned.Substring(8, 2)} {cleaned.Substring(10)}"; + } + else if (cleaned.Length == 20) + { + return $"{cleaned.Substring(0, 6)} {cleaned.Substring(6, 2)} {cleaned.Substring(8, 3)} {cleaned.Substring(11)}"; + } + + return cleaned; + } + + /// + /// ICCID脱敏:898600****7890 + /// + /// ICCID号 + /// 脱敏后的ICCID + public static string? Mask(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string cleaned = iccid!.Trim(); + int length = cleaned.Length; + + // 保留前6位和后4位 + return cleaned.Substring(0, 6) + new string('*', length - 10) + cleaned.Substring(length - 4); + } + + #endregion + } + + /// + /// ICCID结构信息 + /// + public class ICCDInfo + { + /// + /// 移动国家代码(MCC) + /// + public string? MCC { get; set; } + + /// + /// 国家名称 + /// + public string? Country { get; set; } + + /// + /// 移动网络代码(MNC) + /// + public string? MNC { get; set; } + + /// + /// 运营商名称 + /// + public string? Carrier { get; set; } + + /// + /// 省份代码 + /// + public string? ProvinceCode { get; set; } + + /// + /// 序列号 + /// + public string? SerialNumber { get; set; } + + /// + /// 校验位 + /// + public int? CheckDigit { get; set; } + } +} diff --git a/EasyTool.Core/BusinessCategory/IMEIUtil.cs b/EasyTool.Core/BusinessCategory/IMEIUtil.cs new file mode 100644 index 0000000..7d88853 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IMEIUtil.cs @@ -0,0 +1,347 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// IMEI(国际移动设备识别号)工具类 + /// + public static class IMEIUtil + { + #region 常量与私有字段 + + /// + /// IMEI正则表达式(15位数字) + /// + private static readonly Regex IMEIRegex = new(@"^\d{15}$", RegexOptions.Compiled); + + /// + /// IMEI SV正则表达式(16位数字,含软件版本) + /// + private static readonly Regex IMEISvRegex = new(@"^\d{16}$", RegexOptions.Compiled); + + /// + /// TAC(类型分配码)与制造商映射(部分) + /// + private static readonly (string Prefix, string Manufacturer)[] TacPrefixMap = + { + ("01", "Apple"), + ("35", "Samsung"), + ("86", "Samsung"), + ("01", "Nokia"), + ("35", "Nokia"), + ("352", "Sony"), + ("353", "Sony"), + ("354", "Sony"), + ("355", "Sony"), + ("356", "Sony"), + ("358", "Huawei"), + ("359", "Huawei"), + ("861", "Xiaomi"), + ("862", "Xiaomi"), + ("865", "Xiaomi"), + ("866", "Xiaomi"), + ("352", "LG"), + ("353", "LG"), + ("355", "LG"), + ("356", "LG"), + ("353", "HTC"), + ("354", "HTC"), + ("355", "HTC"), + ("357", "HTC"), + ("358", "HTC"), + ("359", "HTC"), + ("010", "Apple"), + ("011", "Apple"), + ("012", "Apple"), + ("013", "Apple"), + ("014", "Apple"), + ("015", "Apple"), + ("016", "Apple"), + ("017", "Apple"), + ("018", "Apple"), + ("019", "Apple") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证IMEI是否有效(15位,含Luhn校验) + /// + /// IMEI号 + /// 是否有效 + public static bool IsValid(string? imei) + { + if (!IsValidFormat(imei)) + { + return false; + } + + return ValidateLuhn(imei!); + } + + /// + /// 仅验证IMEI格式(不校验Luhn) + /// + /// IMEI号 + /// 格式是否正确 + public static bool IsValidFormat(string? imei) + { + if (string.IsNullOrWhiteSpace(imei)) + { + return false; + } + + return IMEIRegex.IsMatch(imei); + } + + /// + /// 验证IMEI SV是否有效(16位) + /// + /// IMEI SV号 + /// 是否有效 + public static bool IsValidSv(string? imeiSv) + { + if (string.IsNullOrWhiteSpace(imeiSv)) + { + return false; + } + + return IMEISvRegex.IsMatch(imeiSv); + } + + /// + /// 使用Luhn算法验证IMEI + /// + /// IMEI号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? imei) + { + if (string.IsNullOrWhiteSpace(imei) || imei.Length != 15) + { + return false; + } + + int sum = 0; + for (int i = 0; i < 15; i++) + { + if (!char.IsDigit(imei[i])) + { + return false; + } + + int digit = imei[i] - '0'; + + // 偶数位置(从0开始)乘以2,奇数位置不变 + // IMEI的Luhn算法:从右向左,偶数位×2 + if (i % 2 == 1) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + } + + return sum % 10 == 0; + } + + /// + /// 计算Luhn校验位 + /// + /// 不含校验位的14位IMEI + /// 校验位(0-9),计算失败返回-1 + public static int CalculateCheckDigit(string? imei14) + { + if (string.IsNullOrWhiteSpace(imei14) || imei14.Length != 14) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 14; i++) + { + if (!char.IsDigit(imei14[i])) + { + return -1; + } + + int digit = imei14[i] - '0'; + + if (i % 2 == 1) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 信息提取 + + /// + /// 获取TAC(类型分配码,前8位) + /// + /// IMEI号 + /// TAC码 + public static string? GetTAC(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei!.Substring(0, 8); + } + + /// + /// 获取制造商(根据TAC前缀推测) + /// + /// IMEI号 + /// 制造商名称 + public static string? GetManufacturer(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + string tac = imei!.Substring(0, 8); + + // 查找最长匹配的前缀 + for (int len = Math.Min(3, tac.Length); len >= 1; len--) + { + string prefix = tac.Substring(0, len); + foreach (var mapping in TacPrefixMap) + { + if (mapping.Prefix == prefix) + { + return mapping.Manufacturer; + } + } + } + + return null; + } + + /// + /// 获取序列号(SNR,第9-14位) + /// + /// IMEI号 + /// 序列号 + public static string? GetSerialNumber(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei!.Substring(8, 6); + } + + /// + /// 获取校验位(第15位) + /// + /// IMEI号 + /// 校验位 + public static int? GetCheckDigit(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei![14] - '0'; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化IMEI(AA-BBBBBB-CCCCCC-D) + /// + /// IMEI号 + /// 格式化后的IMEI + public static string? Format(string? imei) + { + string? normalized = Normalize(imei); + if (normalized == null || normalized.Length != 15) + { + return null; + } + + return $"{normalized.Substring(0, 2)}-{normalized.Substring(2, 6)}-{normalized.Substring(8, 6)}-{normalized[14]}"; + } + + /// + /// 格式化IMEI(去除分隔符) + /// + /// IMEI号 + /// 清理后的IMEI + public static string? Normalize(string? imei) + { + if (string.IsNullOrWhiteSpace(imei)) + { + return null; + } + + string cleaned = Regex.Replace(imei, @"[^\d]", ""); + return cleaned.Length == 15 ? cleaned : null; + } + + /// + /// IMEI脱敏:35****6 + /// + /// IMEI号 + /// 脱敏后的IMEI + public static string? Mask(string? imei) + { + string? normalized = Normalize(imei); + if (normalized == null) + { + return null; + } + + return normalized.Substring(0, 2) + "***********" + normalized[14]; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机IMEI(仅供测试使用) + /// + /// TAC码(可选,默认随机) + /// 15位IMEI + public static string GenerateRandom(string? tac = null) + { + // TAC(8位) + string tacCode = tac ?? MathCategory.RandomUtil.RandomDigitString(8); + + // 序列号(6位) + string serial = MathCategory.RandomUtil.RandomDigitString(6); + + // 计算校验位 + int checkDigit = CalculateCheckDigit(tacCode + serial); + + return tacCode + serial + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/IPv6Util.cs b/EasyTool.Core/BusinessCategory/IPv6Util.cs new file mode 100644 index 0000000..945679e --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IPv6Util.cs @@ -0,0 +1,269 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// IPv6 地址工具类 + /// 用于验证和处理 IPv6 地址 + /// + public static class IPv6Util + { + /// + /// IPv6 正则表达式 + /// + private static readonly Regex IPv6Regex = new( + @"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$", + RegexOptions.Compiled); + + /// + /// 验证是否为有效的 IPv6 地址 + /// + /// IP 地址 + /// 是否有效 + public static bool IsValid(string? address) + { + if (string.IsNullOrWhiteSpace(address)) + return false; + + return IPv6Regex.IsMatch(address.Trim()); + } + + /// + /// 压缩 IPv6 地址(移除前导零和连续零块) + /// + /// IPv6 地址 + /// 压缩后的地址 + public static string Compress(string address) + { + if (!IsValid(address)) + throw new ArgumentException("无效的 IPv6 地址", nameof(address)); + + try + { + var ip = System.Net.IPAddress.Parse(address); + return ip.IsIPv6LinkLocal ? address : ip.ToString(); + } + catch + { + return address; + } + } + + /// + /// 展开 IPv6 地址(补全省略的零) + /// + /// IPv6 地址 + /// 展开后的地址 + public static string Expand(string address) + { + if (!IsValid(address)) + throw new ArgumentException("无效的 IPv6 地址", nameof(address)); + + try + { + var ip = System.Net.IPAddress.Parse(address); + var bytes = ip.GetAddressBytes(); + + var result = new System.Text.StringBuilder(); + for (int i = 0; i < 16; i += 2) + { + if (i > 0) result.Append(':'); + result.Append($"{bytes[i]:x2}{bytes[i + 1]:x2}"); + } + + return result.ToString(); + } + catch + { + return address; + } + } + + /// + /// 判断是否为本地链接地址(fe80::/10) + /// + /// IPv6 地址 + /// 是否为本地链接地址 + public static bool IsLinkLocal(string? address) + { + if (!IsValid(address)) + return false; + + try + { + var ip = System.Net.IPAddress.Parse(address); + return ip.IsIPv6LinkLocal; + } + catch + { + return false; + } + } + + /// + /// 判断是否为回环地址(::1) + /// + /// IPv6 地址 + /// 是否为回环地址 + public static bool IsLoopback(string? address) + { + if (!IsValid(address)) + return false; + + return System.Net.IPAddress.TryParse(address, out var ip) && + System.Net.IPAddress.IsLoopback(ip); + } + + /// + /// 判断是否为私有地址 + /// fc00::/7 (Unique Local Address) + /// + /// IPv6 地址 + /// 是否为私有地址 + public static bool IsPrivate(string? address) + { + if (!IsValid(address)) + return false; + + var expanded = Expand(address).Replace(":", "").ToLower(); + return expanded.StartsWith("fc") || expanded.StartsWith("fd"); + } + + /// + /// 判断是否为多播地址(ff00::/8) + /// + /// IPv6 地址 + /// 是否为多播地址 + public static bool IsMulticast(string? address) + { + if (!IsValid(address)) + return false; + + return address!.TrimStart().StartsWith("ff", StringComparison.OrdinalIgnoreCase); + } + + /// + /// IPv4 映射的 IPv6 地址转换为 IPv4 + /// + /// IPv6 地址(::ffff:192.168.1.1 格式) + /// IPv4 地址 + public static string? ToIPv4(string? address) + { + if (!IsValid(address)) + return null; + + try + { + var ip = System.Net.IPAddress.Parse(address); + if (ip.IsIPv4MappedToIPv6) + { + return ip.MapToIPv4().ToString(); + } + return null; + } + catch + { + return null; + } + } + + /// + /// IPv4 转换为 IPv6 映射地址 + /// + /// IPv4 地址 + /// IPv6 映射地址 + public static string? FromIPv4(string? ipv4Address) + { + if (string.IsNullOrWhiteSpace(ipv4Address)) + return null; + + try + { + var ip = System.Net.IPAddress.Parse(ipv4Address); + if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + return ip.MapToIPv6().ToString(); + } + return null; + } + catch + { + return null; + } + } + + /// + /// 获取 IPv6 地址类型 + /// + /// IPv6 地址 + /// 地址类型 + public static IPv6AddressType GetAddressType(string? address) + { + if (!IsValid(address)) + return IPv6AddressType.Unknown; + + if (IsLoopback(address)) + return IPv6AddressType.Loopback; + + if (IsLinkLocal(address)) + return IPv6AddressType.LinkLocal; + + if (IsPrivate(address)) + return IPv6AddressType.UniqueLocal; + + if (IsMulticast(address)) + return IPv6AddressType.Multicast; + + if (address!.StartsWith("2", StringComparison.OrdinalIgnoreCase) || + address.StartsWith("3", StringComparison.OrdinalIgnoreCase)) + return IPv6AddressType.GlobalUnicast; + + if (address.StartsWith("::", StringComparison.Ordinal)) + return IPv6AddressType.Unspecified; + + return IPv6AddressType.GlobalUnicast; + } + } + + /// + /// IPv6 地址类型 + /// + public enum IPv6AddressType + { + /// + /// 未知 + /// + Unknown, + + /// + /// 未指定地址(::) + /// + Unspecified, + + /// + /// 回环地址(::1) + /// + Loopback, + + /// + /// 本地链接地址(fe80::/10) + /// + LinkLocal, + + /// + /// 唯一本地地址(fc00::/7) + /// + UniqueLocal, + + /// + /// 全球单播地址 + /// + GlobalUnicast, + + /// + /// 多播地址(ff00::/8) + /// + Multicast + } +} diff --git a/EasyTool.Core/BusinessCategory/ISBNUtil.cs b/EasyTool.Core/BusinessCategory/ISBNUtil.cs new file mode 100644 index 0000000..4ac33c8 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ISBNUtil.cs @@ -0,0 +1,576 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// ISBN类型枚举 + /// + public enum ISBNType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// ISBN-10(10位) + /// + ISBN10 = 1, + + /// + /// ISBN-13(13位) + /// + ISBN13 = 2 + } + + /// + /// ISBN书号工具类 + /// + public static class ISBNUtil + { + #region 常量与私有字段 + + /// + /// ISBN-10正则表达式(可含分隔符) + /// + private static readonly Regex ISBN10Regex = new Regex( + @"^(\d{1,5}[-\s]?)?\d{1,7}[-\s]?\d{1,7}[-\s]?[\dXx]$", + RegexOptions.Compiled); + + /// + /// ISBN-13正则表达式(可含分隔符) + /// + private static readonly Regex ISBN13Regex = new Regex( + @"^97[89][-\s]?\d{1,5}[-\s]?\d{1,7}[-\s]?\d{1,7}[-\s]?\d$", + RegexOptions.Compiled); + + /// + /// 纯数字ISBN-10正则 + /// + private static readonly Regex ISBN10CleanRegex = new Regex( + @"^\d{9}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 纯数字ISBN-13正则 + /// + private static readonly Regex ISBN13CleanRegex = new Regex( + @"^97[89]\d{10}$", + RegexOptions.Compiled); + + /// + /// ISBN前缀与国家/地区/语言映射 + /// + private static readonly (string Prefix, string Region)[] PrefixRegionMap = + { + ("0", "英语国家"), ("1", "英语国家"), + ("2", "法语国家"), + ("3", "德语国家"), + ("4", "日本"), + ("5", "前苏联/俄罗斯"), + ("7", "中国"), + ("80", "前捷克斯洛伐克"), ("85", "巴西"), + ("87", "丹麦"), + ("88", "意大利"), + ("90", "荷兰"), ("91", "瑞典"), ("92", "国际组织"), + ("93", "印度"), ("94", "荷兰"), + ("952", "芬兰"), ("953", "克罗地亚"), + ("960", "希腊"), ("961", "斯洛文尼亚"), ("962", "香港"), + ("963", "匈牙利"), ("964", "伊朗"), ("965", "以色列"), + ("966", "乌克兰"), ("967", "马来西亚"), ("968", "墨西哥"), + ("969", "巴基斯坦"), ("970", "墨西哥"), ("971", "菲律宾"), + ("972", "葡萄牙"), ("973", "罗马尼亚"), ("974", "泰国"), + ("975", "土耳其"), ("976", "加勒比海地区"), ("977", "埃及"), + ("978", "尼日利亚"), ("979", "印度尼西亚"), + ("980", "委内瑞拉"), ("981", "新加坡"), ("982", "南太平洋地区"), + ("983", "马来西亚"), ("984", "孟加拉"), ("985", "白俄罗斯"), + ("986", "台湾"), ("987", "阿根廷"), ("988", "香港"), + ("989", "葡萄牙"), ("9927", "沙特阿拉伯"), ("9933", "伊朗"), + ("9937", "尼泊尔"), ("9939", "亚美尼亚"), ("9940", "卡塔尔"), + ("9942", "阿塞拜疆"), ("9943", "塔吉克斯坦"), ("9944", "斯洛伐克"), + ("9945", "朝鲜"), ("9946", "阿尔巴尼亚"), ("9947", "阿联酋"), + ("9948", "黎巴嫩"), ("9949", "爱沙尼亚"), ("9950", "叙利亚"), + ("9951", "约旦"), ("9952", "吉尔吉斯斯坦"), ("9953", "巴勒斯坦"), + ("9954", "摩洛哥"), ("9955", "立陶宛"), ("9956", "喀麦隆"), + ("9957", "约旦"), ("9958", "古巴"), ("9959", "阿尔及利亚"), + ("9960", "沙特阿拉伯"), ("9961", "阿曼"), ("9962", "巴林"), + ("9963", "冰岛"), ("9964", "加纳"), ("9965", "科威特"), + ("9966", "肯尼亚"), ("9967", "吉布提"), ("9968", "厄瓜多尔"), + ("9969", "蒙古"), ("9970", "乌干达"), ("9971", "津巴布韦"), + ("9972", "巴拿马"), ("9973", "突尼斯"), ("9974", "塞内加尔"), + ("9975", "罗马尼亚"), ("9976", "巴布亚新几内亚"), ("9977", "哥斯达黎加"), + ("9978", "斯里兰卡"), ("9979", "冰岛"), ("9980", "刚果"), + ("9981", "马达加斯加"), ("9982", "加蓬"), ("9983", "马里"), + ("9984", "马拉维"), ("9985", "爱沙尼亚"), ("9986", "立陶宛"), + ("9987", "坦桑尼亚"), ("9988", "加纳"), ("9989", "马其顿"), + ("99901", "巴哈马"), ("99903", "莫桑比克"), ("99904", "哈萨克斯坦"), + ("99905", "尼泊尔"), ("99906", "马拉维"), ("99908", "澳门") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证ISBN是否有效(自动识别ISBN-10或ISBN-13) + /// + /// ISBN号 + /// 是否有效 + public static bool IsValid(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (cleaned.Length == 10) + { + return IsValidISBN10(cleaned); + } + + if (cleaned.Length == 13) + { + return IsValidISBN13(cleaned); + } + + return false; + } + + /// + /// 验证ISBN-10是否有效 + /// + /// ISBN号 + /// 是否有效 + public static bool IsValidISBN10(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (!ISBN10CleanRegex.IsMatch(cleaned)) + { + return false; + } + + // 计算校验位 + int sum = 0; + for (int i = 0; i < 9; i++) + { + sum += (cleaned[i] - '0') * (10 - i); + } + + // 最后一位可能是X(代表10) + char lastChar = char.ToUpper(cleaned[9]); + int checkDigit = lastChar == 'X' ? 10 : (lastChar - '0'); + sum += checkDigit; + + return sum % 11 == 0; + } + + /// + /// 验证ISBN-13是否有效 + /// + /// ISBN号 + /// 是否有效 + public static bool IsValidISBN13(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (!ISBN13CleanRegex.IsMatch(cleaned)) + { + return false; + } + + // ISBN-13必须以978或979开头 + if (!cleaned.StartsWith("978") && !cleaned.StartsWith("979")) + { + return false; + } + + // 计算校验位 + int sum = 0; + for (int i = 0; i < 12; i++) + { + int digit = cleaned[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + int checkDigit = (10 - (sum % 10)) % 10; + return checkDigit == (cleaned[12] - '0'); + } + + /// + /// 验证ISBN格式(不计算校验位) + /// + /// ISBN号 + /// 格式是否正确 + public static bool IsValidFormat(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + return ISBN10CleanRegex.IsMatch(cleaned) || ISBN13CleanRegex.IsMatch(cleaned); + } + + #endregion + + #region 类型识别 + + /// + /// 获取ISBN类型 + /// + /// ISBN号 + /// ISBN类型 + public static ISBNType GetISBNType(string? isbn) + { + if (!IsValid(isbn)) + { + return ISBNType.Unknown; + } + + string cleaned = CleanISBN(isbn); + return cleaned.Length == 10 ? ISBNType.ISBN10 : ISBNType.ISBN13; + } + + #endregion + + #region 转换方法 + + /// + /// 将ISBN-10转换为ISBN-13 + /// + /// ISBN-10号 + /// ISBN-13号,转换失败返回null + public static string? ConvertToISBN13(string? isbn10) + { + if (!IsValidISBN10(isbn10)) + { + return null; + } + + string cleaned = CleanISBN(isbn10!); + + // 添加前缀978 + string isbn13 = "978" + cleaned.Substring(0, 9); + + // 计算新的校验位 + int sum = 0; + for (int i = 0; i < 12; i++) + { + int digit = isbn13[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + int checkDigit = (10 - (sum % 10)) % 10; + return isbn13 + checkDigit; + } + + /// + /// 将ISBN-13转换为ISBN-10(仅适用于978前缀) + /// + /// ISBN-13号 + /// ISBN-10号,转换失败返回null + public static string? ConvertToISBN10(string? isbn13) + { + if (!IsValidISBN13(isbn13)) + { + return null; + } + + string cleaned = CleanISBN(isbn13!); + + // 只有978前缀才能转换为ISBN-10 + if (!cleaned.StartsWith("978")) + { + return null; + } + + // 去掉前缀978和最后一位校验位 + string isbn10Body = cleaned.Substring(3, 9); + + // 计算ISBN-10校验位 + int sum = 0; + for (int i = 0; i < 9; i++) + { + sum += (isbn10Body[i] - '0') * (10 - i); + } + + int checkValue = 11 - (sum % 11); + char checkChar; + if (checkValue == 10) + { + checkChar = 'X'; + } + else if (checkValue == 11) + { + checkChar = '0'; + } + else + { + checkChar = (char)('0' + checkValue); + } + + return isbn10Body + checkChar; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国家/地区名称 + /// + /// ISBN号 + /// 国家/地区名称 + public static string? GetRegion(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + // ISBN-13需要去掉978/979前缀 + string prefix = cleaned.Length == 13 ? cleaned.Substring(3) : cleaned; + + // 查找最长匹配的前缀 + for (int len = Math.Min(5, prefix.Length); len >= 1; len--) + { + string searchPrefix = prefix.Substring(0, len); + foreach (var mapping in PrefixRegionMap) + { + if (mapping.Prefix == searchPrefix) + { + return mapping.Region; + } + } + } + + return null; + } + + /// + /// 判断是否为中国出版物 + /// + /// ISBN号 + /// 是否为中国出版物 + public static bool IsChinaISBN(string? isbn) + { + if (!IsValid(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn!); + + // ISBN-13: 978-7 或 979-7 + // ISBN-10: 7开头 + if (cleaned.Length == 13) + { + return cleaned.StartsWith("9787") || cleaned.StartsWith("9797"); + } + else + { + return cleaned.StartsWith("7"); + } + } + + /// + /// 计算ISBN-10校验位 + /// + /// 不含校验位的9位数字 + /// 校验位(0-10,10表示X),计算失败返回-1 + public static int CalculateISBN10CheckDigit(string? isbn9) + { + if (string.IsNullOrWhiteSpace(isbn9) || isbn9.Length != 9) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 9; i++) + { + if (!char.IsDigit(isbn9[i])) + { + return -1; + } + sum += (isbn9[i] - '0') * (10 - i); + } + + int checkValue = 11 - (sum % 11); + return checkValue == 11 ? 0 : checkValue; + } + + /// + /// 计算ISBN-13校验位 + /// + /// 不含校验位的12位数字 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateISBN13CheckDigit(string? isbn12) + { + if (string.IsNullOrWhiteSpace(isbn12) || isbn12.Length != 12) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 12; i++) + { + if (!char.IsDigit(isbn12[i])) + { + return -1; + } + int digit = isbn12[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 格式化方法 + + /// + /// 清理ISBN(去除分隔符) + /// + /// ISBN号 + /// 清理后的ISBN + public static string CleanISBN(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return ""; + } + + // 去除空格和横线 + return Regex.Replace(isbn, @"[\s\-]", "").ToUpper(); + } + + /// + /// 格式化ISBN(添加分隔符) + /// + /// ISBN号 + /// 格式化后的ISBN,如978-7-115-12345-6 + public static string? Format(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + if (cleaned.Length == 10) + { + // ISBN-10格式:x-x-xxx-xxxxx-x + return $"{cleaned[0]}-{cleaned[1]}-{cleaned.Substring(2, 3)}-{cleaned.Substring(5, 4)}-{cleaned[9]}"; + } + else + { + // ISBN-13格式:xxx-x-xxx-xxxxx-x + return $"{cleaned.Substring(0, 3)}-{cleaned[3]}-{cleaned.Substring(4, 3)}-{cleaned.Substring(7, 5)}-{cleaned[12]}"; + } + } + + /// + /// 格式化ISBN(使用自定义分隔符) + /// + /// ISBN号 + /// 分隔符 + /// 格式化后的ISBN + public static string? Format(string? isbn, char separator) + { + string? formatted = Format(isbn); + if (formatted == null) + { + return null; + } + + return formatted.Replace('-', separator); + } + + /// + /// ISBN脱敏:978-7-***-*****-* + /// + /// ISBN号 + /// 脱敏后的ISBN + public static string? Mask(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + if (cleaned.Length == 10) + { + // 保留第1位和最后1位 + return cleaned[0] + "*******" + cleaned[9]; + } + else + { + // 保留前4位和最后1位 + return cleaned.Substring(0, 4) + "*******" + cleaned[12]; + } + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机ISBN-13(仅供测试使用) + /// + /// 前缀(默认978) + /// ISBN-13号 + public static string GenerateRandomISBN13(string prefix = "978") + { + // 生成12位数字 + string isbn12 = prefix + MathCategory.RandomUtil.RandomDigitString(12 - prefix.Length); + + // 计算校验位 + int checkDigit = CalculateISBN13CheckDigit(isbn12); + + return isbn12 + checkDigit; + } + + /// + /// 生成随机ISBN-10(仅供测试使用) + /// + /// ISBN-10号 + public static string GenerateRandomISBN10() + { + // 生成9位数字 + string isbn9 = MathCategory.RandomUtil.RandomDigitString(9); + + // 计算校验位 + int checkDigit = CalculateISBN10CheckDigit(isbn9); + + if (checkDigit == 10) + { + return isbn9 + "X"; + } + + return isbn9 + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/IdCardUtil.cs b/EasyTool.Core/BusinessCategory/IdCardUtil.cs new file mode 100644 index 0000000..d76621a --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IdCardUtil.cs @@ -0,0 +1,467 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 身份证工具类 + /// + public static class IdCardUtil + { + #region 常量与私有字段 + + /// + /// 18位身份证校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 18位身份证校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 18位身份证正则表达式 + /// + private static readonly Regex Regex18 = new Regex(@"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", RegexOptions.Compiled); + + /// + /// 15位身份证正则表达式 + /// + private static readonly Regex Regex15 = new Regex(@"^[1-9]\d{5}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$", RegexOptions.Compiled); + + /// + /// 省份代码与名称映射 + /// + private static readonly string[] ProvinceCodes = { + "", "北京", "天津", "河北", "山西", "内蒙古", // 11-15 + "", "辽宁", "吉林", "黑龙江", "", // 21-23 + "", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", // 31-37 + "", "河南", "湖北", "湖南", "广东", "广西", "海南", // 41-46 + "", "重庆", "四川", "贵州", "云南", "西藏", // 50-54 + "", "陕西", "甘肃", "青海", "宁夏", "新疆", // 61-65 + "", "台湾", // 71 + "", "香港", "澳门" // 81-82 + }; + + /// + /// 星座日期范围 + /// + private static readonly (int Month, int Day, string Name)[] ZodiacRanges = { + (1, 20, "水瓶座"), (2, 19, "双鱼座"), (3, 21, "白羊座"), + (4, 20, "金牛座"), (5, 21, "双子座"), (6, 22, "巨蟹座"), + (7, 23, "狮子座"), (8, 23, "处女座"), (9, 23, "天秤座"), + (10, 24, "天蝎座"), (11, 23, "射手座"), (12, 22, "摩羯座") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证身份证号是否有效(支持15位和18位) + /// + /// 身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return idCard.Length == 18 ? IsValid18(idCard) : + idCard.Length == 15 ? IsValid15(idCard) : + false; + } + + /// + /// 验证18位身份证号是否有效 + /// + /// 18位身份证号 + /// 是否有效 + public static bool IsValid18(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length != 18) + { + return false; + } + + if (!Regex18.IsMatch(idCard)) + { + return false; + } + + // 验证日期有效性 + if (!IsValidDate(idCard.Substring(6, 8))) + { + return false; + } + + // 验证校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + char actualCheckCode = char.ToUpper(idCard[17]); + + return expectedCheckCode == actualCheckCode; + } + + /// + /// 验证15位身份证号是否有效 + /// + /// 15位身份证号 + /// 是否有效 + public static bool IsValid15(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length != 15) + { + return false; + } + + if (!Regex15.IsMatch(idCard)) + { + return false; + } + + // 验证日期有效性(15位身份证年份默认为19xx) + string dateStr = "19" + idCard.Substring(6, 6); + return IsValidDate(dateStr); + } + + #endregion + + #region 转换方法 + + /// + /// 将15位身份证号转换为18位 + /// + /// 15位身份证号 + /// 18位身份证号,转换失败返回null + public static string? Convert15To18(string? idCard15) + { + if (!IsValid15(idCard15)) + { + return null; + } + + // 在第6位后插入"19" + string idCard17 = idCard15!.Substring(0, 6) + "19" + idCard15.Substring(6); + + // 计算校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard17[i] - '0') * Weights[i]; + } + + return idCard17 + CheckCodes[sum % 11]; + } + + /// + /// 将18位身份证号转换为15位 + /// + /// 18位身份证号 + /// 15位身份证号,转换失败返回null + public static string? Convert18To15(string? idCard18) + { + if (!IsValid18(idCard18)) + { + return null; + } + + // 移除第6-9位的年份前两位"19"和最后一位校验码 + return idCard18!.Substring(0, 6) + idCard18.Substring(8, 9); + } + + #endregion + + #region 信息提取方法 + + /// + /// 获取出生日期 + /// + /// 身份证号 + /// 出生日期,解析失败返回null + public static DateTime? GetBirthday(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + string dateStr; + if (idCard!.Length == 18) + { + dateStr = idCard.Substring(6, 8); + } + else + { + dateStr = "19" + idCard.Substring(6, 6); + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取年龄 + /// + /// 身份证号 + /// 年龄,解析失败返回null + public static int? GetAge(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + DateTime today = DateTime.Today; + int age = today.Year - birthday.Value.Year; + + // 如果今年生日还没过,年龄减1 + if (today < birthday.Value.AddYears(age)) + { + age--; + } + + return age; + } + + /// + /// 获取性别代码(1男2女) + /// + /// 身份证号 + /// 性别代码,解析失败返回null + public static int? GetGender(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + // 第17位(索引16)表示性别,奇数为男,偶数为女 + int genderDigit; + if (idCard!.Length == 18) + { + genderDigit = idCard[16] - '0'; + } + else + { + genderDigit = idCard[14] - '0'; + } + + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串(男/女) + /// + /// 身份证号 + /// 性别字符串,解析失败返回null + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + if (!gender.HasValue) + { + return null; + } + + return gender.Value == 1 ? "男" : "女"; + } + + /// + /// 获取省份名称 + /// + /// 身份证号 + /// 省份名称,解析失败返回null + public static string? GetProvince(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length < 2) + { + return null; + } + + int provinceCode; + if (!int.TryParse(idCard.Substring(0, 2), out provinceCode)) + { + return null; + } + + if (provinceCode < 0 || provinceCode >= ProvinceCodes.Length) + { + return null; + } + + return ProvinceCodes[provinceCode]; + } + + /// + /// 获取行政区划代码(前6位) + /// + /// 身份证号 + /// 行政区划代码,解析失败返回null + public static string? GetAreaCode(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || (idCard.Length != 15 && idCard.Length != 18)) + { + return null; + } + + return idCard.Substring(0, 6); + } + + /// + /// 获取生肖 + /// + /// 身份证号 + /// 生肖,解析失败返回null + public static string? GetChineseZodiac(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + return EasyTool.DateTimeCategory.LunarCalendarUtil.GetChineseZodiac(birthday.Value); + } + + /// + /// 获取星座 + /// + /// 身份证号 + /// 星座,解析失败返回null + public static string? GetZodiac(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + return GetZodiacByDate(birthday.Value.Month, birthday.Value.Day); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机身份证号(仅供测试使用) + /// + /// 省份代码(可选,默认随机) + /// 出生日期(可选,默认随机) + /// 性别(可选,1男2女,默认随机) + /// 18位身份证号 + public static string GenerateRandom(string? provinceCode = null, DateTime? birthday = null, int? gender = null) + { + // 省份代码 + string province = provinceCode ?? GetRandomProvinceCode(); + + // 出生日期 + DateTime birth = birthday ?? EasyTool.MathCategory.RandomUtil.GetRandomDateTime( + new DateTime(1950, 1, 1), + new DateTime(2005, 12, 31)); + string birthStr = birth.ToString("yyyyMMdd"); + + // 顺序码(3位)+ 性别 + string sequence = EasyTool.MathCategory.RandomUtil.RandomDigitString(2); + int genderDigit; + if (gender.HasValue && (gender.Value == 1 || gender.Value == 2)) + { + // 指定性别的奇偶性 + int randomDigit = EasyTool.MathCategory.RandomUtil.RandomInt(0, 4); + genderDigit = gender.Value == 1 ? randomDigit * 2 + 1 : randomDigit * 2; + } + else + { + genderDigit = EasyTool.MathCategory.RandomUtil.RandomInt(0, 9); + } + sequence += genderDigit.ToString(); + + // 前17位 + string idCard17 = province + birthStr + sequence; + + // 计算校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard17[i] - '0') * Weights[i]; + } + + return idCard17 + CheckCodes[sum % 11]; + } + + #endregion + + #region 私有方法 + + /// + /// 验证日期字符串是否有效 + /// + private static bool IsValidDate(string dateStr) + { + if (dateStr.Length != 8) + { + return false; + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + if (year < 1900 || year > 2100) + { + return false; + } + + if (month < 1 || month > 12) + { + return false; + } + + int maxDay = DateTime.DaysInMonth(year, month); + return day >= 1 && day <= maxDay; + } + + /// + /// 根据日期获取星座 + /// + private static string GetZodiacByDate(int month, int day) + { + // 星座按日期划分,摩羯座的特殊处理(跨年) + for (int i = ZodiacRanges.Length - 1; i >= 0; i--) + { + var zodiac = ZodiacRanges[i]; + if (month > zodiac.Month || (month == zodiac.Month && day >= zodiac.Day)) + { + return zodiac.Name; + } + } + + // 1月1日到1月19日是摩羯座 + return "摩羯座"; + } + + /// + /// 获取随机省份代码 + /// + private static string GetRandomProvinceCode() + { + int[] validCodes = { 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65 }; + int code = EasyTool.MathCategory.RandomUtil.GetRandomElement(validCodes); + return code.ToString("00"); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs b/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs new file mode 100644 index 0000000..e303422 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs @@ -0,0 +1,717 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 车牌类型枚举 + /// + public enum PlateType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 普通车牌/燃油车牌(7位) + /// + Normal = 1, + + /// + /// 小型新能源车牌(8位,渐变绿色) + /// + NewEnergySmall = 2, + + /// + /// 大型新能源车牌(8位,黄绿双色) + /// + NewEnergyLarge = 3, + + /// + /// 武警车牌 + /// + WJ = 4, + + /// + /// 军队车牌 + /// + Military = 5 + } + + /// + /// 新能源汽车类型枚举 + /// + public enum NewEnergyType + { + /// + /// 纯电动汽车 + /// + PureElectric = 0, + + /// + /// 插电式混合动力汽车(含增程式) + /// + PluginHybrid = 1 + } + + /// + /// 车辆燃料类型枚举 + /// + public enum FuelType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 燃油车(汽油/柴油) + /// + Fuel = 1, + + /// + /// 纯电动汽车 + /// + PureElectric = 2, + + /// + /// 插电式混合动力汽车(含增程式) + /// + PluginHybrid = 3 + } + + /// + /// 车牌号工具类 + /// + public static class LicensePlateUtil + { + #region 常量与私有字段 + + /// + /// 普通车牌正则表达式(7位) + /// 格式:省份简称(1位汉字)+ 发牌机关代号(1位字母)+ 序号(5位字母或数字) + /// + private static readonly Regex NormalPlateRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 小型新能源车牌正则表达式(8位) + /// 格式:省份简称 + 字母 + 5位(第3位为D或F) + /// + private static readonly Regex NewEnergySmallRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][DF][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 大型新能源车牌正则表达式(8位) + /// 格式:省份简称 + 字母 + 5位(第3位或第4-8位包含数字,第8位为D或F) + /// + private static readonly Regex NewEnergyLargeRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][A-HJ-NP-Z0-9]{5}[DF]$", + RegexOptions.Compiled); + + /// + /// 武警车牌正则表达式 + /// 格式:WJ + 省份代码(2位数字)+ 1位字母 + 4位数字 + /// + private static readonly Regex WJPlateRegex = new Regex( + @"^WJ[0-9]{2}[0-9A-HJ-NP-Z]\d{4}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 军队车牌正则表达式(简化版) + /// + private static readonly Regex MilitaryPlateRegex = new Regex( + @"^[VQZHBSLJKWETCYM][A-Z][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 省份简称与名称映射 + /// + private static readonly Dictionary ProvinceMap = new Dictionary + { + { "京", "北京市" }, { "津", "天津市" }, { "冀", "河北省" }, { "晋", "山西省" }, + { "蒙", "内蒙古自治区" }, { "辽", "辽宁省" }, { "吉", "吉林省" }, { "黑", "黑龙江省" }, + { "沪", "上海市" }, { "苏", "江苏省" }, { "浙", "浙江省" }, { "皖", "安徽省" }, + { "闽", "福建省" }, { "赣", "江西省" }, { "鲁", "山东省" }, { "豫", "河南省" }, + { "鄂", "湖北省" }, { "湘", "湖南省" }, { "粤", "广东省" }, { "桂", "广西壮族自治区" }, + { "琼", "海南省" }, { "川", "四川省" }, { "贵", "贵州省" }, { "云", "云南省" }, + { "藏", "西藏自治区" }, { "陕", "陕西省" }, { "甘", "甘肃省" }, { "青", "青海省" }, + { "宁", "宁夏回族自治区" }, { "新", "新疆维吾尔自治区" }, { "渝", "重庆市" }, + { "港", "香港特别行政区" }, { "澳", "澳门特别行政区" }, { "台", "台湾省" } + }; + + /// + /// 车牌字母与城市映射(部分主要城市) + /// + private static readonly Dictionary> CityMap = new Dictionary> + { + { "京", new Dictionary { { "A", "市区" }, { "B", "出租车" }, { "C", "郊区" }, { "D", "警车" }, { "E", "郊区" }, { "F", "郊区" }, { "G", "郊区" }, { "H", "郊区" }, { "J", "郊区" }, { "K", "郊区" }, { "L", "郊区" }, { "M", "郊区" }, { "N", "市区" }, { "P", "市区" }, { "Q", "市区" }, { "Y", "郊区" } } }, + { "沪", new Dictionary { { "A", "市区" }, { "B", "市区" }, { "C", "郊区" }, { "D", "郊区" }, { "E", "市区" }, { "F", "郊区" }, { "G", "郊区" }, { "H", "郊区" }, { "J", "郊区" }, { "K", "郊区" }, { "L", "郊区" }, { "M", "郊区" }, { "N", "市区" }, { "R", "崇明" } } }, + { "粤", new Dictionary { { "A", "广州市" }, { "B", "深圳市" }, { "C", "珠海市" }, { "D", "汕头市" }, { "E", "佛山市" }, { "F", "韶关市" }, { "G", "湛江市" }, { "H", "肇庆市" }, { "J", "江门市" }, { "K", "茂名市" }, { "L", "惠州市" }, { "M", "梅州市" }, { "N", "汕尾市" }, { "P", "河源市" }, { "Q", "阳江市" }, { "R", "清远市" }, { "S", "东莞市" }, { "T", "中山市" }, { "U", "潮州市" }, { "V", "揭阳市" }, { "W", "云浮市" }, { "X", "顺德区" }, { "Y", "南海区" }, { "Z", "港澳入境" } } }, + { "苏", new Dictionary { { "A", "南京市" }, { "B", "无锡市" }, { "C", "徐州市" }, { "D", "常州市" }, { "E", "苏州市" }, { "F", "南通市" }, { "G", "连云港市" }, { "H", "淮安市" }, { "J", "盐城市" }, { "K", "扬州市" }, { "L", "镇江市" }, { "M", "泰州市" }, { "N", "宿迁市" } } }, + { "浙", new Dictionary { { "A", "杭州市" }, { "B", "宁波市" }, { "C", "温州市" }, { "D", "绍兴市" }, { "E", "湖州市" }, { "F", "嘉兴市" }, { "G", "金华市" }, { "H", "衢州市" }, { "J", "台州市" }, { "K", "丽水市" }, { "L", "舟山市" } } }, + }; + + #endregion + + #region 验证方法 + + /// + /// 验证车牌号是否有效 + /// + /// 车牌号 + /// 是否有效 + public static bool IsValid(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + return IsNormalPlate(normalized) || IsNewEnergyPlate(normalized) || + IsWJPlate(normalized) || IsMilitaryPlate(normalized); + } + + /// + /// 验证是否为普通车牌(7位) + /// + /// 车牌号 + /// 是否为普通车牌 + public static bool IsNormalPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NormalPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为新能源车牌(8位) + /// + /// 车牌号 + /// 是否为新能源车牌 + public static bool IsNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + return IsSmallNewEnergyPlate(normalized) || IsLargeNewEnergyPlate(normalized); + } + + /// + /// 验证是否为小型新能源车牌(8位,第3位为D或F) + /// + /// 车牌号 + /// 是否为小型新能源车牌 + public static bool IsSmallNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NewEnergySmallRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为大型新能源车牌(8位,第8位为D或F) + /// + /// 车牌号 + /// 是否为大型新能源车牌 + public static bool IsLargeNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NewEnergyLargeRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为武警车牌 + /// + /// 车牌号 + /// 是否为武警车牌 + public static bool IsWJPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return WJPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为军队车牌 + /// + /// 车牌号 + /// 是否为军队车牌 + public static bool IsMilitaryPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return MilitaryPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + #endregion + + #region 类型识别 + + /// + /// 获取车牌类型 + /// + /// 车牌号 + /// 车牌类型 + public static PlateType GetPlateType(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return PlateType.Unknown; + } + + string normalized = Normalize(plateNumber)!; + + if (IsSmallNewEnergyPlate(normalized)) + { + return PlateType.NewEnergySmall; + } + + if (IsLargeNewEnergyPlate(normalized)) + { + return PlateType.NewEnergyLarge; + } + + if (IsNormalPlate(normalized)) + { + return PlateType.Normal; + } + + if (IsWJPlate(normalized)) + { + return PlateType.WJ; + } + + if (IsMilitaryPlate(normalized)) + { + return PlateType.Military; + } + + return PlateType.Unknown; + } + + /// + /// 验证是否为燃油车车牌(普通7位车牌,非新能源) + /// + /// 车牌号 + /// 是否为燃油车车牌 + public static bool IsFuelVehicle(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + + // 普通车牌(7位)且不是军队/武警车牌 = 燃油车 + return normalized.Length == 7 && IsNormalPlate(normalized); + } + + /// + /// 获取车辆燃料类型 + /// + /// 车牌号 + /// 燃料类型 + public static FuelType GetFuelType(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return FuelType.Unknown; + } + + string normalized = Normalize(plateNumber)!; + + // 燃油车(普通7位车牌) + if (IsFuelVehicle(normalized)) + { + return FuelType.Fuel; + } + + // 新能源车 + if (IsNewEnergyPlate(normalized)) + { + NewEnergyType? newEnergyType = GetNewEnergyType(normalized); + return newEnergyType switch + { + NewEnergyType.PureElectric => FuelType.PureElectric, + NewEnergyType.PluginHybrid => FuelType.PluginHybrid, + _ => FuelType.Unknown + }; + } + + return FuelType.Unknown; + } + + /// + /// 获取车辆燃料类型名称 + /// + /// 车牌号 + /// 燃料类型名称 + public static string? GetFuelTypeName(string? plateNumber) + { + return GetFuelType(plateNumber) switch + { + FuelType.Fuel => "燃油车", + FuelType.PureElectric => "纯电动", + FuelType.PluginHybrid => "插电混动", + _ => null + }; + } + + /// + /// 获取新能源车型类型 + /// + /// 车牌号 + /// 新能源类型,非新能源车牌返回null + public static NewEnergyType? GetNewEnergyType(string? plateNumber) + { + if (!IsNewEnergyPlate(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 小型新能源车牌:第3位 + // 大型新能源车牌:第8位 + char typeChar; + if (normalized.Length == 8) + { + if (normalized[2] == 'D' || normalized[2] == 'F') + { + typeChar = normalized[2]; + } + else + { + typeChar = normalized[7]; + } + } + else + { + return null; + } + + // D: 纯电动, F: 插电式混合动力 + return typeChar == 'D' ? NewEnergyType.PureElectric : NewEnergyType.PluginHybrid; + } + + #endregion + + #region 信息提取 + + /// + /// 获取省份名称 + /// + /// 车牌号 + /// 省份名称 + public static string? GetProvince(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + return "武警"; + } + + // 军队车牌特殊处理 + if (IsMilitaryPlate(normalized)) + { + return "军队"; + } + + string provinceCode = normalized.Substring(0, 1); + return ProvinceMap.TryGetValue(provinceCode, out string? province) ? province : null; + } + + /// + /// 获取城市名称 + /// + /// 车牌号 + /// 城市名称 + public static string? GetCity(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 武警或军队车牌无城市信息 + if (IsWJPlate(normalized) || IsMilitaryPlate(normalized)) + { + return null; + } + + string provinceCode = normalized.Substring(0, 1); + string cityCode = normalized.Substring(1, 1); + + if (CityMap.TryGetValue(provinceCode, out Dictionary? cities)) + { + if (cities.TryGetValue(cityCode, out string? city)) + { + return city; + } + } + + return null; + } + + /// + /// 获取车牌前缀(省份 + 字母) + /// + /// 车牌号 + /// 车牌前缀 + public static string? GetPrefix(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + if (normalized.Length < 2) + { + return null; + } + + // 普通车牌和新能源车牌:前2位 + // 武警车牌:前4位(WJ+数字) + if (IsWJPlate(normalized)) + { + return normalized.Length >= 4 ? normalized.Substring(0, 4) : null; + } + + return normalized.Substring(0, 2); + } + + /// + /// 获取号码部分(去除前缀) + /// + /// 车牌号 + /// 号码部分 + public static string? GetNumberPart(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 普通车牌:后5位 + // 新能源车牌:后6位(小型)/ 后6位(大型) + // 武警车牌:后5位 + + if (IsWJPlate(normalized)) + { + return normalized.Length >= 7 ? normalized.Substring(4) : null; + } + + if (normalized.Length == 8) + { + // 新能源车牌 + return normalized.Substring(2); + } + + if (normalized.Length == 7) + { + // 普通车牌或军队车牌 + return normalized.Substring(2); + } + + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化车牌号(转大写,去除特殊字符) + /// + /// 车牌号 + /// 格式化后的车牌号 + public static string? Normalize(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + string normalized = plateNumber.ToUpper().Trim(); + + // 保留汉字、字母、数字 + normalized = Regex.Replace(normalized, @"[^\u4e00-\u9fa5A-Z0-9]", ""); + + return normalized; + } + + /// + /// 格式化车牌号(带分隔符) + /// + /// 车牌号 + /// 分隔符(默认为空格) + /// 格式化后的车牌号 + public static string? Format(string? plateNumber, string separator = " ") + { + string? normalized = Normalize(plateNumber); + if (normalized == null) + { + return null; + } + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + if (normalized.Length == 7) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2, 2) + separator + normalized.Substring(4); + } + return normalized; + } + + // 普通车牌:2+5 + // 新能源车牌:2+6 + if (normalized.Length == 7) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2); + } + + if (normalized.Length == 8) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2); + } + + return normalized; + } + + /// + /// 车牌号脱敏:粤***123 + /// + /// 车牌号 + /// 脱敏后的车牌号 + public static string? Mask(string? plateNumber) + { + string? normalized = Normalize(plateNumber); + if (normalized == null) + { + return null; + } + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + if (normalized.Length >= 7) + { + return normalized.Substring(0, 4) + "***" + normalized.Substring(normalized.Length - 2); + } + return null; + } + + if (normalized.Length == 7) + { + // 普通车牌:保留省份 + 后2位 + return normalized.Substring(0, 1) + "***" + normalized.Substring(5); + } + + if (normalized.Length == 8) + { + // 新能源车牌:保留省份 + 后2位 + return normalized.Substring(0, 1) + "****" + normalized.Substring(6); + } + + return null; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机车牌号(仅供测试使用) + /// + /// 省份简称(可选,默认随机) + /// 是否为新能源车牌(可选,默认随机) + /// 车牌号 + public static string GenerateRandom(string? province = null, bool? isNewEnergy = null) + { + // 省份 + string[] provinces = { "京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "渝" }; + string prov = province ?? MathCategory.RandomUtil.GetRandomElement(provinces); + + // 字母 + const string letters = "ABCDEFGHJKLMNPQRSTUVWXYZ"; // 不包含I和O + string letter = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()).ToString(); + + bool newEnergy = isNewEnergy ?? MathCategory.RandomUtil.RandomBool(); + + if (newEnergy) + { + // 新能源车牌(8位) + char energyType = MathCategory.RandomUtil.RandomBool() ? 'D' : 'F'; + string numbers = GenerateRandomAlphanumeric(5); + return prov + letter + energyType + numbers; + } + else + { + // 普通车牌(7位) + string numbers = GenerateRandomAlphanumeric(5); + return prov + letter + numbers; + } + } + + #endregion + + #region 私有方法 + + /// + /// 生成随机字母数字组合 + /// + private static string GenerateRandomAlphanumeric(int length) + { + const string chars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; // 不包含I和O + string result = ""; + for (int i = 0; i < length; i++) + { + result += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/MACAddressUtil.cs b/EasyTool.Core/BusinessCategory/MACAddressUtil.cs new file mode 100644 index 0000000..0bd23a3 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/MACAddressUtil.cs @@ -0,0 +1,508 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// MAC地址工具类 + /// + public static class MACAddressUtil + { + #region 常量与私有字段 + + /// + /// MAC地址正则表达式(多种格式) + /// + private static readonly Regex[] MACRegexes = + { + // XX:XX:XX:XX:XX:XX + new(@"^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$", RegexOptions.Compiled), + // XX-XX-XX-XX-XX-XX + new(@"^([0-9A-Fa-f]{2}[-]){5}[0-9A-Fa-f]{2}$", RegexOptions.Compiled), + // XXXX.XXXX.XXXX (Cisco格式) + new(@"^([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$", RegexOptions.Compiled), + // XXXXXXXXXXXX (无分隔符) + new(@"^[0-9A-Fa-f]{12}$", RegexOptions.Compiled) + }; + + /// + /// OUI(组织唯一标识符)与厂商映射(部分) + /// + private static readonly (string Prefix, string Vendor)[] OuiPrefixMap = + { + // Apple + ("00:03:93", "Apple"), ("00:05:02", "Apple"), ("00:0A:27", "Apple"), + ("00:0A:95", "Apple"), ("00:0D:93", "Apple"), ("00:0E:B2", "Apple"), + ("00:11:24", "Apple"), ("00:14:51", "Apple"), ("00:16:CB", "Apple"), + ("00:17:F2", "Apple"), ("00:19:E3", "Apple"), ("00:1B:63", "Apple"), + ("00:1C:B3", "Apple"), ("00:1D:4F", "Apple"), ("00:1E:52", "Apple"), + ("00:1F:5B", "Apple"), ("00:1F:F3", "Apple"), ("00:22:41", "Apple"), + ("00:23:12", "Apple"), ("00:23:32", "Apple"), ("00:23:6C", "Apple"), + ("00:23:DF", "Apple"), ("00:24:36", "Apple"), ("00:25:00", "Apple"), + ("00:25:4B", "Apple"), ("00:25:BC", "Apple"), ("00:26:08", "Apple"), + ("00:26:4A", "Apple"), ("00:26:B0", "Apple"), ("00:26:BB", "Apple"), + ("00:26:DB", "Apple"), ("A4:83:E7", "Apple"), ("AC:87:A3", "Apple"), + ("DC:A9:04", "Apple"), ("F0:DB:E2", "Apple"), + + // Samsung + ("00:07:AB", "Samsung"), ("00:0D:E5", "Samsung"), ("00:12:47", "Samsung"), + ("00:13:77", "Samsung"), ("00:15:99", "Samsung"), ("00:16:6B", "Samsung"), + ("00:17:C9", "Samsung"), ("00:18:AF", "Samsung"), ("00:1A:8A", "Samsung"), + ("00:1B:59", "Samsung"), ("00:1D:2B", "Samsung"), ("00:1E:7D", "Samsung"), + ("00:1F:36", "Samsung"), ("00:22:43", "Samsung"), ("00:24:90", "Samsung"), + ("00:25:38", "Samsung"), ("08:EC:A9", "Samsung"), ("30:07:4D", "Samsung"), + ("34:23:87", "Samsung"), ("38:2D:D8", "Samsung"), ("40:4D:7F", "Samsung"), + ("54:88:0E", "Samsung"), ("5C:8D:4E", "Samsung"), ("64:5D:86", "Samsung"), + ("6C:5C:14", "Samsung"), ("7C:1E:52", "Samsung"), ("88:36:6C", "Samsung"), + ("8C:10:D4", "Samsung"), ("94:8B:C1", "Samsung"), ("98:F0:AB", "Samsung"), + ("A0:10:81", "Samsung"), ("B0:DF:3A", "Samsung"), ("CC:61:E5", "Samsung"), + ("D8:A2:5E", "Samsung"), ("E0:D5:5E", "Samsung"), ("E8:50:8B", "Samsung"), + ("F0:27:65", "Samsung"), + + // Huawei + ("00:0F:B5", "Huawei"), ("00:18:82", "Huawei"), ("00:1E:10", "Huawei"), + ("00:25:68", "Huawei"), ("08:57:00", "Huawei"), ("0C:96:BF", "Huawei"), + ("10:1D:59", "Huawei"), ("18:3E:0F", "Huawei"), ("28:ED:6A", "Huawei"), + ("2C:B0:5D", "Huawei"), ("34:A3:95", "Huawei"), ("38:F8:5B", "Huawei"), + ("40:4E:36", "Huawei"), ("44:8B:CE", "Huawei"), ("48:37:B9", "Huawei"), + ("4C:FB:9F", "Huawei"), ("50:01:BB", "Huawei"), ("54:BF:64", "Huawei"), + ("58:00:E3", "Huawei"), ("5C:FE:45", "Huawei"), ("64:9A:BE", "Huawei"), + ("68:DB:CA", "Huawei"), ("6C:5D:43", "Huawei"), ("70:19:0F", "Huawei"), + ("74:6A:D8", "Huawei"), ("78:44:76", "Huawei"), ("7C:1E:52", "Huawei"), + ("80:8D:3F", "Huawei"), ("84:10:0D", "Huawei"), ("88:43:E1", "Huawei"), + ("8C:71:F8", "Huawei"), ("90:2B:D2", "Huawei"), ("94:FE:22", "Huawei"), + ("98:6F:1A", "Huawei"), ("9C:2E:A1", "Huawei"), ("A0:8F:85", "Huawei"), + ("A4:4E:31", "Huawei"), ("B0:5B:48", "Huawei"), ("B4:69:21", "Huawei"), + ("C0:BD:D1", "Huawei"), ("C4:4E:AC", "Huawei"), ("C8:5B:76", "Huawei"), + ("CC:34:29", "Huawei"), ("D0:59:E4", "Huawei"), ("D4:7A:34", "Huawei"), + ("DC:72:9B", "Huawei"), ("E0:37:BF", "Huawei"), ("E4:0D:73", "Huawei"), + ("E8:4E:CE", "Huawei"), ("EC:9A:74", "Huawei"), ("F0:FE:6B", "Huawei"), + ("FC:2C:55", "Huawei"), + + // Xiaomi + ("00:BB:3E", "Xiaomi"), ("10:2A:B3", "Xiaomi"), ("18:59:36", "Xiaomi"), + ("20:82:C0", "Xiaomi"), ("24:F9:A3", "Xiaomi"), ("28:ED:E1", "Xiaomi"), + ("34:80:B3", "Xiaomi"), ("38:1A:21", "Xiaomi"), ("3C:BD:D8", "Xiaomi"), + ("40:31:3C", "Xiaomi"), ("44:6F:D1", "Xiaomi"), ("48:88:CA", "Xiaomi"), + ("4C:18:D6", "Xiaomi"), ("50:1E:2D", "Xiaomi"), ("50:EC:50", "Xiaomi"), + ("58:44:98", "Xiaomi"), ("64:90:C1", "Xiaomi"), ("6C:5C:14", "Xiaomi"), + ("6C:8D:C1", "Xiaomi"), ("74:A3:E4", "Xiaomi"), ("78:02:F8", "Xiaomi"), + ("7C:1D:D9", "Xiaomi"), ("7C:8B:CA", "Xiaomi"), ("88:0F:10", "Xiaomi"), + ("8C:4C:4B", "Xiaomi"), ("8C:F6:79", "Xiaomi"), ("90:82:37", "Xiaomi"), + ("94:87:E0", "Xiaomi"), ("98:0C:82", "Xiaomi"), ("9C:2E:A1", "Xiaomi"), + ("9C:99:A0", "Xiaomi"), ("A0:CB:FD", "Xiaomi"), ("A4:4E:31", "Xiaomi"), + ("AC:29:3A", "Xiaomi"), ("B0:E2:35", "Xiaomi"), ("B8:C1:11", "Xiaomi"), + ("C0:26:0D", "Xiaomi"), ("C0:EE:FB", "Xiaomi"), ("C4:0B:CB", "Xiaomi"), + ("C4:4C:CA", "Xiaomi"), ("C8:1E:E7", "Xiaomi"), ("C8:94:BB", "Xiaomi"), + ("CC:AF:78", "Xiaomi"), ("D0:D2:B0", "Xiaomi"), ("D4:5D:64", "Xiaomi"), + ("D8:1C:79", "Xiaomi"), ("D8:96:95", "Xiaomi"), ("DC:A6:32", "Xiaomi"), + ("E0:46:44", "Xiaomi"), ("E4:B2:1F", "Xiaomi"), ("EC:3A:FD", "Xiaomi"), + ("EC:41:18", "Xiaomi"), ("F0:B4:29", "Xiaomi"), ("F4:28:53", "Xiaomi"), + ("F8:A4:5F", "Xiaomi"), ("FC:6D:B3", "Xiaomi"), ("FC:A6:67", "Xiaomi"), + + // Intel + ("00:02:B3", "Intel"), ("00:03:47", "Intel"), ("00:04:23", "Intel"), + ("00:07:E9", "Intel"), ("00:0B:DB", "Intel"), ("00:0D:DA", "Intel"), + ("00:0E:0C", "Intel"), ("00:0E:35", "Intel"), ("00:0E:A6", "Intel"), + ("00:0F:B0", "Intel"), ("00:0F:EE", "Intel"), ("00:10:E0", "Intel"), + ("00:11:0A", "Intel"), ("00:11:11", "Intel"), ("00:11:43", "Intel"), + ("00:11:F5", "Intel"), ("00:12:3F", "Intel"), ("00:13:20", "Intel"), + ("00:13:CE", "Intel"), ("00:13:E8", "Intel"), ("00:14:22", "Intel"), + ("00:14:78", "Intel"), ("00:14:A5", "Intel"), ("00:15:17", "Intel"), + ("00:15:C5", "Intel"), ("00:16:76", "Intel"), ("00:16:B6", "Intel"), + ("00:17:08", "Intel"), ("00:17:9A", "Intel"), ("00:17:C2", "Intel"), + ("00:18:13", "Intel"), ("00:18:68", "Intel"), ("00:18:DE", "Intel"), + ("00:19:D1", "Intel"), ("00:1B:21", "Intel"), ("00:1C:BD", "Intel"), + ("00:1D:72", "Intel"), ("00:1E:64", "Intel"), ("00:1E:67", "Intel"), + ("00:1F:16", "Intel"), ("00:1F:29", "Intel"), ("00:21:5C", "Intel"), + ("00:21:CC", "Intel"), ("00:22:FA", "Intel"), ("00:23:14", "Intel"), + ("00:23:7E", "Intel"), ("00:23:AE", "Intel"), ("00:24:D7", "Intel"), + ("00:25:66", "Intel"), ("00:26:B7", "Intel"), ("00:26:C6", "Intel"), + ("00:26:C7", "Intel"), ("00:27:0E", "Intel"), ("00:30:1B", "Intel"), + + // Cisco + ("00:00:0C", "Cisco"), ("00:01:42", "Cisco"), ("00:01:43", "Cisco"), + ("00:01:63", "Cisco"), ("00:01:64", "Cisco"), ("00:01:96", "Cisco"), + ("00:01:97", "Cisco"), ("00:01:C7", "Cisco"), ("00:02:16", "Cisco"), + ("00:02:17", "Cisco"), ("00:02:4A", "Cisco"), ("00:02:7D", "Cisco"), + ("00:02:7E", "Cisco"), ("00:02:FD", "Cisco"), ("00:03:6B", "Cisco"), + ("00:03:6F", "Cisco"), ("00:03:E3", "Cisco"), ("00:04:27", "Cisco"), + ("00:04:C1", "Cisco"), ("00:05:30", "Cisco"), ("00:05:32", "Cisco"), + ("00:05:59", "Cisco"), ("00:05:85", "Cisco"), ("00:05:9A", "Cisco"), + ("00:05:DC", "Cisco"), ("00:06:28", "Cisco"), ("00:06:52", "Cisco"), + ("00:06:53", "Cisco"), ("00:07:0D", "Cisco"), ("00:07:0E", "Cisco"), + ("00:07:0F", "Cisco"), ("00:07:50", "Cisco"), ("00:07:EC", "Cisco"), + ("00:08:21", "Cisco"), ("00:08:22", "Cisco"), ("00:08:24", "Cisco"), + ("00:08:2C", "Cisco"), ("00:08:A3", "Cisco"), ("00:09:0C", "Cisco"), + ("00:09:0D", "Cisco"), ("00:09:41", "Cisco"), ("00:09:43", "Cisco"), + ("00:09:44", "Cisco"), ("00:09:7C", "Cisco"), ("00:09:B7", "Cisco"), + ("00:0A:B8", "Cisco"), ("00:0A:F4", "Cisco"), ("00:0B:5F", "Cisco"), + ("00:0B:BE", "Cisco"), ("00:0B:FD", "Cisco"), ("00:0C:0C", "Cisco"), + ("00:0C:30", "Cisco"), ("00:0C:31", "Cisco"), ("00:0C:CE", "Cisco"), + ("00:0D:28", "Cisco"), ("00:0D:29", "Cisco"), ("00:0D:62", "Cisco"), + ("00:0D:63", "Cisco"), ("00:0D:64", "Cisco"), ("00:0D:BD", "Cisco"), + ("00:0D:BE", "Cisco"), ("00:0D:BF", "Cisco"), ("00:0D:C0", "Cisco"), + ("00:0E:0C", "Cisco"), ("00:0E:38", "Cisco"), ("00:0E:39", "Cisco"), + ("00:0E:3A", "Cisco"), ("00:0E:3B", "Cisco"), ("00:0E:3C", "Cisco"), + ("00:0E:84", "Cisco"), ("00:0F:23", "Cisco"), ("00:0F:24", "Cisco"), + ("00:0F:34", "Cisco"), ("00:0F:35", "Cisco"), ("00:0F:F7", "Cisco"), + ("00:0F:F8", "Cisco"), ("00:10:0C", "Cisco"), ("00:10:0D", "Cisco"), + ("00:10:0E", "Cisco"), ("00:10:0F", "Cisco"), ("00:10:54", "Cisco"), + ("00:10:58", "Cisco"), ("00:10:7A", "Cisco"), ("00:10:7B", "Cisco"), + ("00:10:E8", "Cisco"), ("00:10:F3", "Cisco"), ("00:10:F6", "Cisco"), + ("00:11:1B", "Cisco"), ("00:11:20", "Cisco"), ("00:11:21", "Cisco"), + ("00:11:2F", "Cisco"), ("00:11:30", "Cisco"), ("00:11:90", "Cisco"), + ("00:11:91", "Cisco"), ("00:11:92", "Cisco"), ("00:11:93", "Cisco"), + ("00:11:BB", "Cisco"), ("00:11:BC", "Cisco"), ("00:11:BD", "Cisco"), + ("00:11:BE", "Cisco"), ("00:11:BF", "Cisco"), ("00:11:FA", "Cisco"), + ("00:11:FB", "Cisco"), ("00:11:FC", "Cisco"), ("00:11:FD", "Cisco"), + ("00:11:FE", "Cisco"), ("00:12:00", "Cisco"), ("00:12:01", "Cisco"), + ("00:12:17", "Cisco"), ("00:12:1C", "Cisco"), ("00:12:1D", "Cisco"), + ("00:12:40", "Cisco"), ("00:12:41", "Cisco"), ("00:12:43", "Cisco"), + ("00:12:7F", "Cisco"), ("00:12:80", "Cisco"), ("00:12:DA", "Cisco"), + ("00:12:DB", "Cisco"), ("00:12:DC", "Cisco"), ("00:12:F9", "Cisco"), + ("00:12:FA", "Cisco"), ("00:13:1A", "Cisco"), ("00:13:1B", "Cisco"), + ("00:13:1C", "Cisco"), ("00:13:19", "Cisco"), ("00:13:46", "Cisco"), + ("00:13:47", "Cisco"), ("00:13:48", "Cisco"), ("00:13:49", "Cisco"), + ("00:13:5F", "Cisco"), ("00:13:60", "Cisco"), ("00:13:61", "Cisco"), + ("00:13:7F", "Cisco"), ("00:13:80", "Cisco"), ("00:13:81", "Cisco"), + ("00:13:C3", "Cisco"), ("00:13:C4", "Cisco"), ("00:13:C5", "Cisco"), + ("00:13:E8", "Cisco"), ("00:13:F7", "Cisco"), ("00:14:1B", "Cisco"), + ("00:14:69", "Cisco"), ("00:14:6A", "Cisco"), ("00:14:6B", "Cisco"), + ("00:14:97", "Cisco"), ("00:14:9A", "Cisco"), ("00:14:A1", "Cisco"), + ("00:14:A2", "Cisco"), ("00:14:BF", "Cisco"), ("00:14:F1", "Cisco"), + ("00:14:F2", "Cisco"), ("00:15:0C", "Cisco"), ("00:15:17", "Cisco"), + ("00:15:1B", "Cisco"), ("00:15:1C", "Cisco"), ("00:15:2B", "Cisco"), + ("00:15:60", "Cisco"), ("00:15:61", "Cisco"), ("00:15:62", "Cisco"), + ("00:15:63", "Cisco"), ("00:15:FA", "Cisco"), ("00:15:FB", "Cisco"), + ("00:15:FC", "Cisco"), ("00:15:FD", "Cisco"), ("00:16:35", "Cisco"), + ("00:16:36", "Cisco"), ("00:16:37", "Cisco"), ("00:16:46", "Cisco"), + ("00:16:47", "Cisco"), ("00:16:48", "Cisco"), ("00:16:78", "Cisco"), + ("00:16:79", "Cisco"), ("00:16:9D", "Cisco"), ("00:16:9E", "Cisco"), + ("00:16:C6", "Cisco"), ("00:16:C7", "Cisco"), ("00:16:C8", "Cisco"), + ("00:17:0D", "Cisco"), ("00:17:0E", "Cisco"), ("00:17:0F", "Cisco"), + ("00:17:59", "Cisco"), ("00:17:5A", "Cisco"), ("00:17:5B", "Cisco"), + ("00:17:84", "Cisco"), ("00:17:85", "Cisco"), ("00:17:86", "Cisco"), + ("00:17:94", "Cisco"), ("00:17:95", "Cisco"), ("00:17:96", "Cisco"), + ("00:17:DF", "Cisco"), ("00:17:E0", "Cisco"), ("00:17:E1", "Cisco"), + ("00:18:71", "Cisco"), ("00:18:72", "Cisco"), ("00:18:73", "Cisco"), + ("00:18:81", "Cisco"), ("00:18:82", "Cisco"), ("00:18:83", "Cisco"), + ("00:18:AF", "Cisco"), ("00:18:B9", "Cisco"), ("00:18:BA", "Cisco"), + ("00:18:BB", "Cisco"), ("00:19:06", "Cisco"), ("00:19:07", "Cisco"), + ("00:19:2F", "Cisco"), ("00:19:30", "Cisco"), ("00:19:55", "Cisco"), + ("00:19:56", "Cisco"), ("00:19:57", "Cisco"), ("00:19:68", "Cisco"), + ("00:19:69", "Cisco"), ("00:19:6A", "Cisco"), ("00:19:85", "Cisco"), + ("00:19:86", "Cisco"), ("00:19:87", "Cisco"), ("00:19:A9", "Cisco"), + ("00:19:AA", "Cisco"), ("00:19:AB", "Cisco"), ("00:19:E7", "Cisco"), + ("00:19:E8", "Cisco"), ("00:19:E9", "Cisco"), ("00:1A:0D", "Cisco"), + ("00:1A:0E", "Cisco"), ("00:1A:0F", "Cisco"), ("00:1A:2F", "Cisco"), + ("00:1A:30", "Cisco"), ("00:1A:31", "Cisco"), ("00:1A:6B", "Cisco"), + ("00:1A:6C", "Cisco"), ("00:1A:6D", "Cisco"), ("00:1A:A0", "Cisco"), + ("00:1A:A1", "Cisco"), ("00:1A:A2", "Cisco"), ("00:1A:A3", "Cisco"), + ("00:1A:E1", "Cisco"), ("00:1A:E2", "Cisco"), ("00:1A:E3", "Cisco"), + ("00:1B:0D", "Cisco"), ("00:1B:0E", "Cisco"), ("00:1B:0F", "Cisco"), + ("00:1B:53", "Cisco"), ("00:1B:54", "Cisco"), ("00:1B:55", "Cisco"), + ("00:1B:8C", "Cisco"), ("00:1B:8D", "Cisco"), ("00:1B:8E", "Cisco"), + ("00:1B:D4", "Cisco"), ("00:1B:D5", "Cisco"), ("00:1B:D6", "Cisco"), + ("00:1C:0E", "Cisco"), ("00:1C:0F", "Cisco"), ("00:1C:10", "Cisco"), + ("00:1C:58", "Cisco"), ("00:1C:59", "Cisco"), ("00:1C:5A", "Cisco"), + ("00:1C:B0", "Cisco"), ("00:1C:B1", "Cisco"), ("00:1C:B2", "Cisco"), + ("00:1C:F0", "Cisco"), ("00:1C:F1", "Cisco"), ("00:1C:F2", "Cisco"), + ("00:1D:0F", "Cisco"), ("00:1D:10", "Cisco"), ("00:1D:11", "Cisco"), + ("00:1D:45", "Cisco"), ("00:1D:46", "Cisco"), ("00:1D:47", "Cisco"), + ("00:1D:9C", "Cisco"), ("00:1D:9D", "Cisco"), ("00:1D:9E", "Cisco"), + ("00:1D:E2", "Cisco"), ("00:1D:E3", "Cisco"), ("00:1D:E4", "Cisco"), + ("00:1E:13", "Cisco"), ("00:1E:14", "Cisco"), ("00:1E:15", "Cisco"), + ("00:1E:49", "Cisco"), ("00:1E:4A", "Cisco"), ("00:1E:4B", "Cisco"), + ("00:1E:79", "Cisco"), ("00:1E:7A", "Cisco"), ("00:1E:7B", "Cisco"), + ("00:1E:B4", "Cisco"), ("00:1E:B5", "Cisco"), ("00:1E:B6", "Cisco"), + ("00:1F:1D", "Cisco"), ("00:1F:1E", "Cisco"), ("00:1F:1F", "Cisco"), + ("00:1F:6C", "Cisco"), ("00:1F:6D", "Cisco"), ("00:1F:6E", "Cisco"), + ("00:1F:9D", "Cisco"), ("00:1F:9E", "Cisco"), ("00:1F:9F", "Cisco"), + ("00:1F:C8", "Cisco"), ("00:1F:C9", "Cisco"), ("00:1F:CA", "Cisco"), + ("00:21:0D", "Cisco"), ("00:21:0E", "Cisco"), ("00:21:0F", "Cisco"), + ("00:21:55", "Cisco"), ("00:21:56", "Cisco"), ("00:21:57", "Cisco"), + ("00:21:A0", "Cisco"), ("00:21:A1", "Cisco"), ("00:21:A2", "Cisco"), + ("00:21:D5", "Cisco"), ("00:21:D6", "Cisco"), ("00:21:D7", "Cisco"), + ("00:22:55", "Cisco"), ("00:22:56", "Cisco"), ("00:22:57", "Cisco"), + ("00:22:90", "Cisco"), ("00:22:91", "Cisco"), ("00:22:92", "Cisco"), + ("00:22:BD", "Cisco"), ("00:22:BE", "Cisco"), ("00:22:BF", "Cisco"), + ("00:23:04", "Cisco"), ("00:23:05", "Cisco"), ("00:23:06", "Cisco"), + ("00:23:33", "Cisco"), ("00:23:34", "Cisco"), ("00:23:35", "Cisco"), + ("00:23:5C", "Cisco"), ("00:23:5D", "Cisco"), ("00:23:5E", "Cisco"), + ("00:23:EB", "Cisco"), ("00:23:EC", "Cisco"), ("00:23:ED", "Cisco"), + ("00:24:13", "Cisco"), ("00:24:14", "Cisco"), ("00:24:15", "Cisco"), + ("00:24:50", "Cisco"), ("00:24:51", "Cisco"), ("00:24:52", "Cisco"), + ("00:24:97", "Cisco"), ("00:24:98", "Cisco"), ("00:24:99", "Cisco"), + ("00:24:B2", "Cisco"), ("00:24:B3", "Cisco"), ("00:24:B4", "Cisco"), + ("00:24:F7", "Cisco"), ("00:24:F8", "Cisco"), ("00:24:F9", "Cisco"), + ("00:25:1B", "Cisco"), ("00:25:1C", "Cisco"), ("00:25:1D", "Cisco"), + ("00:25:2A", "Cisco"), ("00:25:2B", "Cisco"), ("00:25:2C", "Cisco"), + ("00:25:61", "Cisco"), ("00:25:62", "Cisco"), ("00:25:63", "Cisco"), + ("00:25:84", "Cisco"), ("00:25:85", "Cisco"), ("00:25:86", "Cisco"), + ("00:25:B5", "Cisco"), ("00:25:B6", "Cisco"), ("00:25:B7", "Cisco"), + ("00:26:0B", "Cisco"), ("00:26:0C", "Cisco"), ("00:26:0D", "Cisco"), + ("00:26:51", "Cisco"), ("00:26:52", "Cisco"), ("00:26:53", "Cisco"), + ("00:26:88", "Cisco"), ("00:26:89", "Cisco"), ("00:26:8A", "Cisco"), + ("00:26:99", "Cisco"), ("00:26:9A", "Cisco"), ("00:26:9B", "Cisco"), + ("00:26:CA", "Cisco"), ("00:26:CB", "Cisco"), ("00:26:CC", "Cisco"), + ("00:50:56", "VMware"), ("00:0C:29", "VMware"), ("00:05:69", "VMware"), + ("00:1C:14", "VMware"), ("00:50:56", "VMware") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证MAC地址是否有效 + /// + /// MAC地址 + /// 是否有效 + public static bool IsValid(string? mac) + { + if (string.IsNullOrWhiteSpace(mac)) + { + return false; + } + + foreach (var regex in MACRegexes) + { + if (regex.IsMatch(mac)) + { + return true; + } + } + + return false; + } + + #endregion + + #region 信息提取 + + /// + /// 获取OUI(组织唯一标识符,前3字节) + /// + /// MAC地址 + /// OUI + public static string? GetOUI(string? mac) + { + if (!IsValid(mac)) + { + return null; + } + + string clean = Clean(mac)!; + return clean.Substring(0, 6).ToUpper(); + } + + /// + /// 获取设备标识符(后3字节) + /// + /// MAC地址 + /// 设备标识符 + public static string? GetDeviceId(string? mac) + { + if (!IsValid(mac)) + { + return null; + } + + string clean = Clean(mac)!; + return clean.Substring(6, 6).ToUpper(); + } + + /// + /// 获取厂商名称 + /// + /// MAC地址 + /// 厂商名称 + public static string? GetVendor(string? mac) + { + string? oui = GetOUI(mac); + if (oui == null) + { + return null; + } + + // 格式化为XX:XX:XX格式进行查找 + string formattedOui = $"{oui.Substring(0, 2)}:{oui.Substring(2, 2)}:{oui.Substring(4, 2)}".ToUpper(); + + foreach (var mapping in OuiPrefixMap) + { + if (mapping.Prefix.Equals(formattedOui, StringComparison.OrdinalIgnoreCase)) + { + return mapping.Vendor; + } + } + + return null; + } + + /// + /// 判断是否为组播地址 + /// + /// MAC地址 + /// 是否为组播地址 + public static bool IsMulticast(string? mac) + { + if (!IsValid(mac)) + { + return false; + } + + string clean = Clean(mac)!; + // 第一个字节的最低位为1表示组播 + int firstByte = Convert.ToInt32(clean.Substring(0, 2), 16); + return (firstByte & 0x01) == 1; + } + + /// + /// 判断是否为广播地址(FF:FF:FF:FF:FF:FF) + /// + /// MAC地址 + /// 是否为广播地址 + public static bool IsBroadcast(string? mac) + { + string? clean = Clean(mac); + return clean == "FFFFFFFFFFFF"; + } + + /// + /// 判断是否为本地管理地址 + /// + /// MAC地址 + /// 是否为本地管理地址 + public static bool IsLocallyAdministered(string? mac) + { + if (!IsValid(mac)) + { + return false; + } + + string clean = Clean(mac)!; + // 第一个字节的次低位为1表示本地管理 + int firstByte = Convert.ToInt32(clean.Substring(0, 2), 16); + return (firstByte & 0x02) == 2; + } + + #endregion + + #region 格式化方法 + + /// + /// 清理MAC地址(去除分隔符) + /// + /// MAC地址 + /// 12位十六进制字符串 + public static string? Clean(string? mac) + { + if (string.IsNullOrWhiteSpace(mac)) + { + return null; + } + + string cleaned = Regex.Replace(mac, @"[^\dA-Fa-f]", "").ToUpper(); + return cleaned.Length == 12 ? cleaned : null; + } + + /// + /// 格式化为标准格式(XX:XX:XX:XX:XX:XX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? Format(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:{clean.Substring(4, 2)}:" + + $"{clean.Substring(6, 2)}:{clean.Substring(8, 2)}:{clean.Substring(10, 2)}"; + } + + /// + /// 格式化为横线分隔(XX-XX-XX-XX-XX-XX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? FormatWithHyphens(string? mac) + { + return Format(mac)?.Replace(':', '-'); + } + + /// + /// 格式化为Cisco格式(XXXX.XXXX.XXXX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? FormatCisco(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 4)}.{clean.Substring(4, 4)}.{clean.Substring(8, 4)}"; + } + + /// + /// MAC地址脱敏:AA:BB:**:**:**:FF + /// + /// MAC地址 + /// 脱敏后的MAC地址 + public static string? Mask(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:**:**:**:{clean.Substring(10, 2)}"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机MAC地址(仅供测试使用) + /// + /// 厂商OUI(可选,默认随机生成) + /// MAC地址 + public static string GenerateRandom(string? vendor = null) + { + string oui; + string deviceId; + + if (!string.IsNullOrWhiteSpace(vendor) && vendor.Length >= 6) + { + oui = vendor.Substring(0, 6).ToUpper(); + } + else + { + // 随机生成OUI(设置本地管理位) + int firstByte = MathCategory.RandomUtil.RandomInt(0, 255) | 0x02; // 设置本地管理位 + oui = firstByte.ToString("X2") + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2"); + } + + // 随机生成设备ID + deviceId = MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2"); + + string clean = oui + deviceId; + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:{clean.Substring(4, 2)}:" + + $"{clean.Substring(6, 2)}:{clean.Substring(8, 2)}:{clean.Substring(10, 2)}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs b/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs new file mode 100644 index 0000000..ab168f2 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs @@ -0,0 +1,254 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 组织机构代码工具类 + /// + public static class OrgCodeUtil + { + #region 常量与私有字段 + + /// + /// 组织机构代码正则表达式(9位:8位数字/字母 + 1位校验码) + /// + private static readonly Regex OrgCodeRegex = new( + @"^[A-Z0-9]{8}-?[A-X0-9]$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 组织机构代码字符值映射 + /// + private static readonly int[] CharWeights = { 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表 + /// + private const string CheckCodes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + #endregion + + #region 验证方法 + + /// + /// 验证组织机构代码是否有效 + /// + /// 组织机构代码 + /// 是否有效 + public static bool IsValid(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return false; + } + + string code = orgCode.ToUpper().Replace("-", ""); + + if (code.Length != 9) + { + return false; + } + + if (!OrgCodeRegex.IsMatch(code)) + { + return false; + } + + // 计算校验码 + char? expectedCheck = CalculateCheckCode(code.Substring(0, 8)); + return expectedCheck.HasValue && expectedCheck.Value == code[8]; + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 组织机构代码 + /// 格式是否正确 + public static bool IsValidFormat(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return false; + } + + string code = orgCode.ToUpper().Replace("-", ""); + return code.Length == 9 && OrgCodeRegex.IsMatch(code); + } + + /// + /// 计算校验码 + /// + /// 不含校验位的8位代码 + /// 校验码,计算失败返回null + public static char? CalculateCheckCode(string? code8) + { + if (string.IsNullOrWhiteSpace(code8) || code8.Length != 8) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 8; i++) + { + char c = char.ToUpper(code8[i]); + int value; + + if (c >= '0' && c <= '9') + { + value = c - '0'; + } + else if (c >= 'A' && c <= 'Z') + { + value = c - 'A' + 10; + } + else + { + return null; + } + + sum += value * CharWeights[i]; + } + + int checkIndex = 11 - (sum % 11); + if (checkIndex == 11) + { + checkIndex = 0; + } + else if (checkIndex == 10) + { + return 'X'; // 10对应X + } + + return CheckCodes[checkIndex]; + } + + #endregion + + #region 信息提取 + + /// + /// 获取机构类型(第1位) + /// + /// 组织机构代码 + /// 机构类型 + public static string? GetOrganizationType(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + char typeCode = char.ToUpper(orgCode!.Replace("-", "")[0]); + return typeCode switch + { + '1' => "企业法人", + '2' => "企业非法人", + '3' => "事业法人", + '4' => "事业非法人", + '5' => "机关法人", + '6' => "机关非法人", + '7' => "社会团体法人", + '8' => "社会团体非法人", + '9' => "其他机构", + 'A' => "企业法人(外资)", + 'B' => "企业非法人(外资)", + _ => null + }; + } + + /// + /// 获取登记管理机关行政区划代码(第2-8位) + /// + /// 组织机构代码 + /// 行政区划代码 + public static string? GetAreaCode(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.Replace("-", ""); + return code.Substring(1, 7); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化组织机构代码(XXXXXXXX-X) + /// + /// 组织机构代码 + /// 格式化后的代码 + public static string? Format(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.ToUpper().Replace("-", ""); + return code.Substring(0, 8) + "-" + code[8]; + } + + /// + /// 清理组织机构代码(去除分隔符) + /// + /// 组织机构代码 + /// 清理后的代码 + public static string? Normalize(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return null; + } + + string code = orgCode.ToUpper().Replace("-", "").Trim(); + return code.Length == 9 && OrgCodeRegex.IsMatch(code) ? code : null; + } + + /// + /// 组织机构代码脱敏:123****9X + /// + /// 组织机构代码 + /// 脱敏后的代码 + public static string? Mask(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.Replace("-", ""); + return code.Substring(0, 3) + "*****" + code.Substring(8); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机组织机构代码(仅供测试使用) + /// + /// 9位组织机构代码 + public static string GenerateRandom() + { + const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + // 生成前8位 + string code8 = ""; + for (int i = 0; i < 8; i++) + { + code8 += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + // 计算校验码 + char? checkCode = CalculateCheckCode(code8); + return code8 + (checkCode ?? '0'); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PassportUtil.cs b/EasyTool.Core/BusinessCategory/PassportUtil.cs new file mode 100644 index 0000000..9ee432d --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PassportUtil.cs @@ -0,0 +1,375 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 护照类型枚举 + /// + public enum PassportType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 中国普通护照(E开头+8位数字) + /// + ChinaOrdinary = 1, + + /// + /// 中国公务护照(SE开头+7位数字) + /// + ChinaService = 2, + + /// + /// 中国外交护照(DE开头+7位数字) + /// + ChinaDiplomatic = 3, + + /// + /// 中国香港特区护照 + /// + HongKong = 4, + + /// + /// 中国澳门特区护照 + /// + Macau = 5, + + /// + /// 台湾护照 + /// + Taiwan = 6 + } + + /// + /// 护照号工具类 + /// + public static class PassportUtil + { + #region 常量与私有字段 + + /// + /// 中国普通护照正则(E+8位数字) + /// + private static readonly Regex ChinaOrdinaryRegex = new Regex(@"^[Ee]\d{8}$", RegexOptions.Compiled); + + /// + /// 中国公务护照正则(SE+7位数字) + /// + private static readonly Regex ChinaServiceRegex = new Regex(@"^[Ss][Ee]\d{7}$", RegexOptions.Compiled); + + /// + /// 中国外交护照正则(DE+7位数字) + /// + private static readonly Regex ChinaDiplomaticRegex = new Regex(@"^[Dd][Ee]\d{7}$", RegexOptions.Compiled); + + /// + /// 中国香港护照正则(K+8位数字 或 881/159开头+7位数字) + /// + private static readonly Regex HongKongRegex = new Regex(@"^([Kk]\d{8}|(881|159)\d{7})$", RegexOptions.Compiled); + + /// + /// 中国澳门护照正则(578开头+7位数字 或 1+7位数字) + /// + private static readonly Regex MacauRegex = new Regex(@"^(578\d{7}|[1]\d{7})$", RegexOptions.Compiled); + + /// + /// 台湾护照正则(数字+字母混合,9-10位) + /// + private static readonly Regex TaiwanRegex = new Regex(@"^\d{8,9}$|^[A-Za-z]\d{8,9}$", RegexOptions.Compiled); + + /// + /// 通用护照号正则(2-3位字母+6-9位数字,或纯数字8-9位) + /// + private static readonly Regex GeneralPassportRegex = new Regex( + @"^([A-Za-z]{1,3}\d{6,9}|\d{8,9})$", + RegexOptions.Compiled); + + #endregion + + #region 验证方法 + + /// + /// 验证护照号是否有效(自动识别类型) + /// + /// 护照号 + /// 是否有效 + public static bool IsValid(string? passportNumber) + { + return GetPassportType(passportNumber) != PassportType.Unknown; + } + + /// + /// 验证中国普通护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaOrdinary(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaOrdinaryRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国公务护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaService(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaServiceRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国外交护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaDiplomatic(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaDiplomaticRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国香港护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsHongKong(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return HongKongRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国澳门护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsMacau(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return MacauRegex.IsMatch(passportNumber); + } + + /// + /// 验证台湾护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsTaiwan(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return TaiwanRegex.IsMatch(passportNumber); + } + + /// + /// 验证是否为中国大陆护照(含普通、公务、外交) + /// + /// 护照号 + /// 是否为中国大陆护照 + public static bool IsChinaMainland(string? passportNumber) + { + return IsChinaOrdinary(passportNumber) || + IsChinaService(passportNumber) || + IsChinaDiplomatic(passportNumber); + } + + #endregion + + #region 类型识别 + + /// + /// 获取护照类型 + /// + /// 护照号 + /// 护照类型 + public static PassportType GetPassportType(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return PassportType.Unknown; + } + + string upper = passportNumber.ToUpper(); + + // 中国普通护照 + if (ChinaOrdinaryRegex.IsMatch(upper)) + { + return PassportType.ChinaOrdinary; + } + + // 中国公务护照 + if (ChinaServiceRegex.IsMatch(upper)) + { + return PassportType.ChinaService; + } + + // 中国外交护照 + if (ChinaDiplomaticRegex.IsMatch(upper)) + { + return PassportType.ChinaDiplomatic; + } + + // 香港护照 + if (HongKongRegex.IsMatch(upper)) + { + return PassportType.HongKong; + } + + // 澳门护照 + if (MacauRegex.IsMatch(upper)) + { + return PassportType.Macau; + } + + // 台湾护照 + if (TaiwanRegex.IsMatch(upper)) + { + return PassportType.Taiwan; + } + + return PassportType.Unknown; + } + + /// + /// 获取护照类型名称 + /// + /// 护照号 + /// 护照类型名称 + public static string? GetPassportTypeName(string? passportNumber) + { + PassportType type = GetPassportType(passportNumber); + return type switch + { + PassportType.ChinaOrdinary => "中国普通护照", + PassportType.ChinaService => "中国公务护照", + PassportType.ChinaDiplomatic => "中国外交护照", + PassportType.HongKong => "香港特区护照", + PassportType.Macau => "澳门特区护照", + PassportType.Taiwan => "台湾护照", + _ => null + }; + } + + /// + /// 获取护照类型描述 + /// + /// 护照类型 + /// 类型描述 + public static string GetTypeDescription(PassportType type) + { + return type switch + { + PassportType.ChinaOrdinary => "中国普通护照(E+8位数字)", + PassportType.ChinaService => "中国公务护照(SE+7位数字)", + PassportType.ChinaDiplomatic => "中国外交护照(DE+7位数字)", + PassportType.HongKong => "香港特区护照(K+8位数字)", + PassportType.Macau => "澳门特区护照(578开头+7位数字)", + PassportType.Taiwan => "台湾护照(8-9位数字)", + _ => "未知类型" + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化护照号(转大写,去除空格和特殊字符) + /// + /// 护照号 + /// 格式化后的护照号 + public static string? Normalize(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + string normalized = passportNumber.ToUpper().Trim(); + normalized = Regex.Replace(normalized, @"[^A-Z0-9]", ""); + + return normalized; + } + + /// + /// 护照号脱敏:E********(保留首字母) + /// + /// 护照号 + /// 脱敏后的护照号 + public static string? Mask(string? passportNumber) + { + string? normalized = Normalize(passportNumber); + if (normalized == null) + { + return null; + } + + // 保留首字符,其余用*代替 + if (normalized.Length <= 2) + { + return normalized[0] + "*"; + } + + // 保留前2位和后2位 + return normalized.Substring(0, 2) + new string('*', normalized.Length - 4) + normalized.Substring(normalized.Length - 2); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机护照号(仅供测试使用) + /// + /// 护照类型(可选,默认中国普通护照) + /// 护照号 + public static string GenerateRandom(PassportType type = PassportType.ChinaOrdinary) + { + return type switch + { + PassportType.ChinaOrdinary => "E" + MathCategory.RandomUtil.RandomDigitString(8), + PassportType.ChinaService => "SE" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.ChinaDiplomatic => "DE" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.HongKong => "K" + MathCategory.RandomUtil.RandomDigitString(8), + PassportType.Macau => "578" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.Taiwan => MathCategory.RandomUtil.RandomDigitString(9), + _ => "E" + MathCategory.RandomUtil.RandomDigitString(8) + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PasswordUtil.cs b/EasyTool.Core/BusinessCategory/PasswordUtil.cs new file mode 100644 index 0000000..c3e66ed --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PasswordUtil.cs @@ -0,0 +1,586 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 密码强度等级 + /// + public enum PasswordStrength + { + /// + /// 非常弱 + /// + VeryWeak = 0, + + /// + /// 弱 + /// + Weak = 1, + + /// + /// 中等 + /// + Medium = 2, + + /// + /// 强 + /// + Strong = 3, + + /// + /// 非常强 + /// + VeryStrong = 4 + } + + /// + /// 密码验证结果 + /// + public class PasswordValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 密码强度 + /// + public PasswordStrength Strength { get; set; } + + /// + /// 强度分数(0-100) + /// + public int Score { get; set; } + + /// + /// 错误信息列表 + /// + public List Errors { get; set; } = new List(); + + /// + /// 警告信息列表 + /// + public List Warnings { get; set; } = new List(); + } + + /// + /// 密码验证选项 + /// + public class PasswordValidationOptions + { + /// + /// 最小长度(默认8) + /// + public int MinLength { get; set; } = 8; + + /// + /// 最大长度(默认128) + /// + public int MaxLength { get; set; } = 128; + + /// + /// 是否要求包含小写字母(默认true) + /// + public bool RequireLowercase { get; set; } = true; + + /// + /// 是否要求包含大写字母(默认true) + /// + public bool RequireUppercase { get; set; } = true; + + /// + /// 是否要求包含数字(默认true) + /// + public bool RequireDigit { get; set; } = true; + + /// + /// 是否要求包含特殊字符(默认true) + /// + public bool RequireSpecialChar { get; set; } = true; + + /// + /// 允许的特殊字符(默认!@#$%^&*()_+-=[]{}|;:',.<>?) + /// + public string AllowedSpecialChars { get; set; } = "!@#$%^&*()_+-=[]{}|;:',.<>?"; + + /// + /// 最少不同字符类型数量(默认3) + /// + public int MinCharacterTypes { get; set; } = 3; + + /// + /// 是否禁止常见弱密码(默认true) + /// + public bool BlockCommonPasswords { get; set; } = true; + + /// + /// 是否禁止连续重复字符(默认true) + /// + public bool BlockRepeatingChars { get; set; } = true; + + /// + /// 是否禁止连续递增/递减字符(如123、abc)(默认true) + /// + public bool BlockSequentialChars { get; set; } = true; + } + + /// + /// 密码工具类 + /// + public static class PasswordUtil + { + #region 常量与私有字段 + + /// + /// 常见弱密码列表 + /// + private static readonly HashSet CommonPasswords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "password", "123456", "12345678", "123456789", "1234567890", + "qwerty", "abc123", "password123", "admin", "admin123", + "root", "root123", "111111", "000000", "123123", + "password1", "iloveyou", "monkey", "dragon", "master", + "letmein", "login", "welcome", "shadow", "sunshine", + "princess", "football", "baseball", "soccer", "hockey", + "batman", "superman", "trustno1", "passw0rd", "qazwsx", + "qwerty123", "123qwe", "654321", "888888", "666666" + }; + + /// + /// 键盘连续字符模式 + /// + private static readonly string[] KeyboardSequences = { + "qwertyuiop", "asdfghjkl", "zxcvbnm", + "qwertyuiop".ToUpper(), "asdfghjkl".ToUpper(), "zxcvbnm".ToUpper() + }; + + #endregion + + #region 验证方法 + + /// + /// 验证密码(使用默认选项) + /// + /// 密码 + /// 验证结果 + public static PasswordValidationResult Validate(string? password) + { + return Validate(password, new PasswordValidationOptions()); + } + + /// + /// 验证密码(使用自定义选项) + /// + /// 密码 + /// 验证选项 + /// 验证结果 + public static PasswordValidationResult Validate(string? password, PasswordValidationOptions options) + { + var result = new PasswordValidationResult(); + + // 空值检查 + if (string.IsNullOrEmpty(password)) + { + result.IsValid = false; + result.Errors.Add("密码不能为空"); + result.Score = 0; + result.Strength = PasswordStrength.VeryWeak; + return result; + } + + // 长度检查 + if (password.Length < options.MinLength) + { + result.Errors.Add($"密码长度不能少于{options.MinLength}位"); + } + + if (password.Length > options.MaxLength) + { + result.Errors.Add($"密码长度不能超过{options.MaxLength}位"); + } + + // 字符类型检查 + bool hasLowercase = Regex.IsMatch(password, @"[a-z]"); + bool hasUppercase = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + if (options.RequireLowercase && !hasLowercase) + { + result.Errors.Add("密码必须包含小写字母"); + } + + if (options.RequireUppercase && !hasUppercase) + { + result.Errors.Add("密码必须包含大写字母"); + } + + if (options.RequireDigit && !hasDigit) + { + result.Errors.Add("密码必须包含数字"); + } + + if (options.RequireSpecialChar && !hasSpecial) + { + result.Errors.Add("密码必须包含特殊字符"); + } + + // 统计字符类型数量 + int charTypeCount = (hasLowercase ? 1 : 0) + (hasUppercase ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0); + if (charTypeCount < options.MinCharacterTypes) + { + result.Errors.Add($"密码必须包含至少{options.MinCharacterTypes}种不同类型的字符"); + } + + // 检查非法字符 + if (!string.IsNullOrEmpty(options.AllowedSpecialChars)) + { + string allowedPattern = $@"^[a-zA-Z0-9{Regex.Escape(options.AllowedSpecialChars)}]+$"; + if (!Regex.IsMatch(password, allowedPattern)) + { + result.Errors.Add("密码包含非法字符"); + } + } + + // 检查常见弱密码 + if (options.BlockCommonPasswords && CommonPasswords.Contains(password)) + { + result.Errors.Add("密码过于简单,请使用更复杂的密码"); + } + + // 检查连续重复字符 + if (options.BlockRepeatingChars && HasRepeatingChars(password, 3)) + { + result.Warnings.Add("密码包含连续重复的字符"); + } + + // 检查连续递增/递减字符 + if (options.BlockSequentialChars && HasSequentialChars(password)) + { + result.Warnings.Add("密码包含连续的递增或递减字符"); + } + + // 计算强度分数 + int score = CalculateScore(password, hasLowercase, hasUppercase, hasDigit, hasSpecial); + result.Score = score; + result.Strength = GetStrengthFromScore(score); + + // 确定是否有效 + result.IsValid = result.Errors.Count == 0; + + return result; + } + + /// + /// 快速验证密码是否符合基本要求 + /// + /// 密码 + /// 最小长度(默认8) + /// 是否有效 + public static bool IsValid(string? password, int minLength = 8) + { + if (string.IsNullOrEmpty(password) || password.Length < minLength) + { + return false; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + + return hasLower && hasUpper && hasDigit; + } + + #endregion + + #region 强度评估 + + /// + /// 评估密码强度 + /// + /// 密码 + /// 密码强度 + public static PasswordStrength EvaluateStrength(string? password) + { + if (string.IsNullOrEmpty(password)) + { + return PasswordStrength.VeryWeak; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + int score = CalculateScore(password, hasLower, hasUpper, hasDigit, hasSpecial); + return GetStrengthFromScore(score); + } + + /// + /// 获取密码强度分数(0-100) + /// + /// 密码 + /// 强度分数 + public static int GetStrengthScore(string? password) + { + if (string.IsNullOrEmpty(password)) + { + return 0; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + return CalculateScore(password, hasLower, hasUpper, hasDigit, hasSpecial); + } + + /// + /// 获取密码强度描述 + /// + /// 密码强度 + /// 强度描述 + public static string GetStrengthDescription(PasswordStrength strength) + { + return strength switch + { + PasswordStrength.VeryWeak => "非常弱", + PasswordStrength.Weak => "弱", + PasswordStrength.Medium => "中等", + PasswordStrength.Strong => "强", + PasswordStrength.VeryStrong => "非常强", + _ => "未知" + }; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机密码 + /// + /// 密码长度(默认12) + /// 包含小写字母(默认true) + /// 包含大写字母(默认true) + /// 包含数字(默认true) + /// 包含特殊字符(默认true) + /// 随机密码 + public static string GenerateRandom( + int length = 12, + bool includeLowercase = true, + bool includeUppercase = true, + bool includeDigits = true, + bool includeSpecialChars = true) + { + const string lowercase = "abcdefghijklmnopqrstuvwxyz"; + const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + const string special = "!@#$%^&*()_+-=[]{}|;:',.<>?"; + + string charSet = ""; + if (includeLowercase) charSet += lowercase; + if (includeUppercase) charSet += uppercase; + if (includeDigits) charSet += digits; + if (includeSpecialChars) charSet += special; + + if (string.IsNullOrEmpty(charSet)) + { + charSet = lowercase + digits; + } + + string password = ""; + for (int i = 0; i < length; i++) + { + password += MathCategory.RandomUtil.GetRandomElement(charSet.ToCharArray()); + } + + return password; + } + + /// + /// 生成强密码(确保包含所有字符类型) + /// + /// 密码长度(默认16) + /// 强密码 + public static string GenerateStrong(int length = 16) + { + const string lowercase = "abcdefghijklmnopqrstuvwxyz"; + const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + const string special = "!@#$%^&*()_+-="; + + if (length < 4) + { + length = 4; + } + + // 确保每种字符类型至少有一个 + var password = new List(); + password.Add(MathCategory.RandomUtil.GetRandomElement(lowercase.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(uppercase.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(special.ToCharArray())); + + // 填充剩余字符 + string allChars = lowercase + uppercase + digits + special; + for (int i = 4; i < length; i++) + { + password.Add(MathCategory.RandomUtil.GetRandomElement(allChars.ToCharArray())); + } + + // 随机打乱顺序 + for (int i = password.Count - 1; i > 0; i--) + { + int j = MathCategory.RandomUtil.RandomInt(0, i + 1); + (password[i], password[j]) = (password[j], password[i]); + } + + return new string(password.ToArray()); + } + + #endregion + + #region 私有方法 + + /// + /// 计算密码强度分数 + /// + private static int CalculateScore(string password, bool hasLower, bool hasUpper, bool hasDigit, bool hasSpecial) + { + int score = 0; + + // 长度分数(最多40分) + score += Math.Min(password.Length * 4, 40); + + // 字符类型分数(每种类型10分,最多40分) + if (hasLower) score += 10; + if (hasUpper) score += 10; + if (hasDigit) score += 10; + if (hasSpecial) score += 10; + + // 混合奖励(最多10分) + int typeCount = (hasLower ? 1 : 0) + (hasUpper ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0); + if (typeCount >= 3) score += 5; + if (typeCount == 4) score += 5; + + // 惩罚 + // 常见弱密码 + if (CommonPasswords.Contains(password)) + { + score = Math.Max(0, score - 30); + } + + // 全部相同字符 + if (AllSameChar(password)) + { + score = Math.Max(0, score - 20); + } + + // 连续字符 + if (HasSequentialChars(password)) + { + score = Math.Max(0, score - 10); + } + + return Math.Min(100, Math.Max(0, score)); + } + + /// + /// 根据分数获取强度等级 + /// + private static PasswordStrength GetStrengthFromScore(int score) + { + if (score < 20) return PasswordStrength.VeryWeak; + if (score < 40) return PasswordStrength.Weak; + if (score < 60) return PasswordStrength.Medium; + if (score < 80) return PasswordStrength.Strong; + return PasswordStrength.VeryStrong; + } + + /// + /// 检查是否所有字符相同 + /// + private static bool AllSameChar(string str) + { + if (string.IsNullOrEmpty(str)) return true; + char first = str[0]; + foreach (char c in str) + { + if (c != first) return false; + } + return true; + } + + /// + /// 检查是否有连续重复字符 + /// + private static bool HasRepeatingChars(string str, int count) + { + if (string.IsNullOrEmpty(str) || str.Length < count) return false; + + for (int i = 0; i <= str.Length - count; i++) + { + bool allSame = true; + for (int j = 1; j < count; j++) + { + if (str[i + j] != str[i]) + { + allSame = false; + break; + } + } + if (allSame) return true; + } + return false; + } + + /// + /// 检查是否有连续递增/递减字符 + /// + private static bool HasSequentialChars(string str) + { + if (string.IsNullOrEmpty(str) || str.Length < 3) return false; + + string lower = str.ToLower(); + + // 检查字母和数字序列 + for (int i = 0; i <= lower.Length - 3; i++) + { + // 检查递增 + if (lower[i + 1] == lower[i] + 1 && lower[i + 2] == lower[i] + 2) + { + return true; + } + // 检查递减 + if (lower[i + 1] == lower[i] - 1 && lower[i + 2] == lower[i] - 2) + { + return true; + } + } + + // 检查键盘序列 + foreach (string seq in KeyboardSequences) + { + if (seq.Contains(lower.Substring(0, Math.Min(3, lower.Length)))) + { + for (int i = 0; i <= lower.Length - 3; i++) + { + if (seq.Contains(lower.Substring(i, 3))) + { + return true; + } + } + } + } + + return false; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs b/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs new file mode 100644 index 0000000..175383a --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs @@ -0,0 +1,350 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 手机运营商枚举 + /// + public enum Carrier + { + /// + /// 未知运营商 + /// + Unknown = 0, + + /// + /// 中国移动 + /// + ChinaMobile = 1, + + /// + /// 中国联通 + /// + ChinaUnicom = 2, + + /// + /// 中国电信 + /// + ChinaTelecom = 3, + + /// + /// 中国广电 + /// + ChinaBroadnet = 4 + } + + /// + /// 手机号工具类 + /// + public static class PhoneNumberUtil + { + #region 常量与私有字段 + + /// + /// 手机号正则表达式(11位,1开头) + /// + private static readonly Regex PhoneRegex = new Regex(@"^1[3-9]\d{9}$", RegexOptions.Compiled); + + /// + /// 中国移动号段(前3-4位) + /// + private static readonly HashSet ChinaMobilePrefixes = new HashSet + { + "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", + "157", "158", "159", "172", "178", "182", "183", "184", "187", "188", + "195", "197", "198" + }; + + /// + /// 中国联通号段(前3-4位) + /// + private static readonly HashSet ChinaUnicomPrefixes = new HashSet + { + "130", "131", "132", "145", "155", "156", "166", "167", "175", "176", + "185", "186", "196" + }; + + /// + /// 中国电信号段(前3-4位) + /// + private static readonly HashSet ChinaTelecomPrefixes = new HashSet + { + "133", "149", "153", "173", "174", "177", "180", "181", "189", "191", + "193", "199" + }; + + /// + /// 中国广电号段(前3-4位) + /// + private static readonly HashSet ChinaBroadnetPrefixes = new HashSet + { + "192" + }; + + #endregion + + #region 验证方法 + + /// + /// 验证手机号格式是否有效 + /// + /// 手机号 + /// 是否有效 + public static bool IsValid(string? phoneNumber) + { + if (string.IsNullOrWhiteSpace(phoneNumber)) + { + return false; + } + + return PhoneRegex.IsMatch(phoneNumber); + } + + /// + /// 格式化并验证手机号(去除非数字字符后验证) + /// + /// 手机号 + /// 格式化后的手机号,无效返回null + public static string? Normalize(string? phoneNumber) + { + if (string.IsNullOrWhiteSpace(phoneNumber)) + { + return null; + } + + // 去除所有非数字字符 + string normalized = Regex.Replace(phoneNumber, @"\D", ""); + + if (!IsValid(normalized)) + { + return null; + } + + return normalized; + } + + #endregion + + #region 运营商识别 + + /// + /// 获取运营商枚举 + /// + /// 手机号 + /// 运营商枚举 + public static Carrier GetCarrier(string? phoneNumber) + { + if (!IsValid(phoneNumber)) + { + return Carrier.Unknown; + } + + string prefix3 = phoneNumber!.Substring(0, 3); + + if (ChinaMobilePrefixes.Contains(prefix3)) + { + return Carrier.ChinaMobile; + } + + if (ChinaUnicomPrefixes.Contains(prefix3)) + { + return Carrier.ChinaUnicom; + } + + if (ChinaTelecomPrefixes.Contains(prefix3)) + { + return Carrier.ChinaTelecom; + } + + if (ChinaBroadnetPrefixes.Contains(prefix3)) + { + return Carrier.ChinaBroadnet; + } + + return Carrier.Unknown; + } + + /// + /// 获取运营商名称 + /// + /// 手机号 + /// 运营商名称 + public static string? GetCarrierName(string? phoneNumber) + { + Carrier carrier = GetCarrier(phoneNumber); + return carrier switch + { + Carrier.ChinaMobile => "中国移动", + Carrier.ChinaUnicom => "中国联通", + Carrier.ChinaTelecom => "中国电信", + Carrier.ChinaBroadnet => "中国广电", + _ => null + }; + } + + /// + /// 判断是否为中国移动号码 + /// + /// 手机号 + /// 是否为移动号码 + public static bool IsChinaMobile(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaMobile; + } + + /// + /// 判断是否为中国联通号码 + /// + /// 手机号 + /// 是否为联通号码 + public static bool IsChinaUnicom(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaUnicom; + } + + /// + /// 判断是否为中国电信号码 + /// + /// 手机号 + /// 是否为电信号码 + public static bool IsChinaTelecom(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaTelecom; + } + + /// + /// 判断是否为中国广电号码 + /// + /// 手机号 + /// 是否为广电号码 + public static bool IsChinaBroadnet(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaBroadnet; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化手机号(空格分隔):138 8888 8888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithSpaces(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)} {normalized.Substring(3, 4)} {normalized.Substring(7, 4)}"; + } + + /// + /// 格式化手机号(横线分隔):138-8888-8888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithHyphens(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 4)}-{normalized.Substring(7, 4)}"; + } + + /// + /// 格式化手机号(带国际区号):+86 13888888888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithCountryCode(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"+86 {normalized}"; + } + + /// + /// 手机号脱敏:138****8888 + /// + /// 手机号 + /// 脱敏后的手机号 + public static string? Mask(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)}****{normalized.Substring(7, 4)}"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机手机号(仅供测试使用) + /// + /// 运营商(可选,默认随机) + /// 11位手机号 + public static string GenerateRandom(Carrier? carrier = null) + { + string prefix; + + if (carrier.HasValue && carrier.Value != Carrier.Unknown) + { + prefix = carrier.Value switch + { + Carrier.ChinaMobile => MathCategory.RandomUtil.GetRandomElement(ChinaMobilePrefixes), + Carrier.ChinaUnicom => MathCategory.RandomUtil.GetRandomElement(ChinaUnicomPrefixes), + Carrier.ChinaTelecom => MathCategory.RandomUtil.GetRandomElement(ChinaTelecomPrefixes), + Carrier.ChinaBroadnet => MathCategory.RandomUtil.GetRandomElement(ChinaBroadnetPrefixes), + _ => GetRandomPrefix() + }; + } + else + { + prefix = GetRandomPrefix(); + } + + // 生成剩余8位数字 + string suffix = MathCategory.RandomUtil.RandomDigitString(8); + + return prefix + suffix; + } + + #endregion + + #region 私有方法 + + /// + /// 获取随机号段前缀 + /// + private static string GetRandomPrefix() + { + var allPrefixes = new List(); + allPrefixes.AddRange(ChinaMobilePrefixes); + allPrefixes.AddRange(ChinaUnicomPrefixes); + allPrefixes.AddRange(ChinaTelecomPrefixes); + allPrefixes.AddRange(ChinaBroadnetPrefixes); + + return MathCategory.RandomUtil.GetRandomElement(allPrefixes); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PhoneUtil.cs b/EasyTool.Core/BusinessCategory/PhoneUtil.cs new file mode 100644 index 0000000..3a2142f --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PhoneUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 固定电话工具类 + /// + public static class PhoneUtil + { + #region 常量与私有字段 + + /// + /// 中国大陆固定电话正则表达式(带区号) + /// + private static readonly Regex PhoneWithAreaCodeRegex = new( + @"^(0\d{2,3}[-\s]?)?\d{7,8}$", + RegexOptions.Compiled); + + /// + /// 中国大陆固定电话正则表达式(完整格式) + /// + private static readonly Regex PhoneFullRegex = new( + @"^0\d{2,3}[-\s]?\d{7,8}$", + RegexOptions.Compiled); + + /// + /// 400电话正则表达式 + /// + private static readonly Regex Phone400Regex = new( + @"^400[-\s]?\d{3}[-\s]?\d{4}$", + RegexOptions.Compiled); + + /// + /// 800电话正则表达式 + /// + private static readonly Regex Phone800Regex = new( + @"^800[-\s]?\d{3}[-\s]?\d{4}$", + RegexOptions.Compiled); + + /// + /// 区号与城市映射 + /// + private static readonly Dictionary AreaCodeMap = new() + { + // 直辖市 + { "010", "北京" }, { "021", "上海" }, { "022", "天津" }, { "023", "重庆" }, + + // 省会城市 + { "0311", "石家庄" }, { "0351", "太原" }, { "0471", "呼和浩特" }, + { "024", "沈阳" }, { "0431", "长春" }, { "0451", "哈尔滨" }, + { "025", "南京" }, { "0571", "杭州" }, { "0551", "合肥" }, + { "0591", "福州" }, { "0791", "南昌" }, { "0531", "济南" }, + { "0371", "郑州" }, { "027", "武汉" }, { "0731", "长沙" }, + { "020", "广州" }, { "0771", "南宁" }, { "0898", "海口" }, + { "028", "成都" }, { "0851", "贵阳" }, { "0871", "昆明" }, + { "0891", "拉萨" }, { "029", "西安" }, { "0931", "兰州" }, + { "0971", "西宁" }, { "0951", "银川" }, { "0991", "乌鲁木齐" }, + + // 重要城市 + { "0755", "深圳" }, { "0756", "珠海" }, { "0754", "汕头" }, + { "0757", "佛山" }, { "0769", "东莞" }, { "0760", "中山" }, + { "0512", "苏州" }, { "0510", "无锡" }, { "0574", "宁波" }, + { "0577", "温州" }, { "0532", "青岛" }, { "0411", "大连" }, + { "0592", "厦门" }, { "0514", "扬州" }, { "0519", "常州" }, + { "0573", "嘉兴" }, { "0575", "绍兴" }, { "0576", "台州" }, + { "0579", "金华" }, { "0752", "惠州" }, { "0753", "梅州" }, + { "0758", "肇庆" }, { "0759", "湛江" }, { "0762", "河源" }, + { "0763", "清远" }, { "0766", "云浮" }, { "0768", "潮州" }, + { "0773", "桂林" }, { "0774", "梧州" }, { "0775", "玉林" }, + { "0779", "北海" }, { "0772", "柳州" }, { "0778", "河池" }, + { "0733", "株洲" }, { "0734", "衡阳" }, { "0735", "郴州" }, + { "0737", "益阳" }, { "0738", "娄底" }, { "0739", "邵阳" }, + { "0792", "九江" }, { "0793", "上饶" }, { "0795", "宜春" }, + { "0796", "吉安" }, { "0797", "赣州" }, { "0799", "萍乡" }, + { "0533", "淄博" }, { "0534", "德州" }, { "0535", "烟台" }, + { "0536", "潍坊" }, { "0537", "济宁" }, { "0538", "泰安" }, + { "0539", "临沂" }, { "0543", "滨州" }, { "0546", "东营" }, + { "0379", "洛阳" }, { "0378", "开封" }, { "0372", "安阳" }, + { "0373", "新乡" }, { "0374", "许昌" }, { "0375", "平顶山" }, + { "0370", "商丘" }, { "0391", "焦作" }, { "0393", "濮阳" }, + { "0395", "漯河" }, { "0396", "驻马店" }, { "0398", "三门峡" }, + { "0376", "信阳" }, { "0377", "南阳" }, { "0392", "鹤壁" }, + { "027", "武汉" }, { "0710", "襄阳" }, { "0711", "鄂州" }, + { "0712", "孝感" }, { "0713", "黄冈" }, { "0714", "黄石" }, + { "0715", "咸宁" }, { "0716", "荆州" }, { "0717", "宜昌" }, + { "0718", "恩施" }, { "0719", "十堰" }, { "0722", "随州" }, + { "0724", "荆门" }, { "0728", "仙桃" }, { "0730", "岳阳" }, + + // 三位区号 + { "0310", "邯郸" }, { "0312", "保定" }, { "0313", "张家口" }, + { "0314", "承德" }, { "0315", "唐山" }, { "0316", "廊坊" }, + { "0317", "沧州" }, { "0318", "衡水" }, { "0319", "邢台" }, + { "0335", "秦皇岛" }, { "0349", "朔州" }, { "0350", "忻州" }, + { "0352", "大同" }, { "0353", "阳泉" }, { "0354", "晋中" }, + { "0355", "长治" }, { "0356", "晋城" }, { "0357", "临汾" }, + { "0358", "吕梁" }, { "0359", "运城" }, { "0410", "铁岭" }, + { "0412", "鞍山" }, { "0413", "抚顺" }, { "0414", "本溪" }, + { "0415", "丹东" }, { "0416", "锦州" }, { "0417", "营口" }, + { "0418", "阜新" }, { "0419", "辽阳" }, { "0421", "朝阳" }, + { "0427", "盘锦" }, { "0429", "葫芦岛" }, { "0432", "吉林市" }, + { "0433", "延边" }, { "0434", "四平" }, { "0435", "通化" }, + { "0436", "白城" }, { "0437", "辽源" }, { "0439", "白山" }, + { "0438", "松原" }, { "0452", "齐齐哈尔" }, { "0453", "牡丹江" }, + { "0454", "佳木斯" }, { "0455", "绥化" }, { "0456", "黑河" }, + { "0457", "大兴安岭" }, { "0458", "伊春" }, { "0459", "大庆" }, + { "0464", "七台河" }, { "0467", "鸡西" }, { "0468", "鹤岗" }, + { "0469", "双鸭山" }, { "0470", "呼伦贝尔" }, { "0472", "包头" }, + { "0473", "乌海" }, { "0474", "乌兰察布" }, { "0475", "通辽" }, + { "0476", "赤峰" }, { "0477", "鄂尔多斯" }, { "0478", "巴彦淖尔" }, + { "0479", "锡林郭勒" }, { "0482", "兴安盟" }, { "0483", "阿拉善" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证固定电话是否有效 + /// + /// 固定电话号码 + /// 是否有效 + public static bool IsValid(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return PhoneWithAreaCodeRegex.IsMatch(phone) || + Is400Phone(phone) || Is800Phone(phone); + } + + /// + /// 验证是否为带区号的固定电话 + /// + /// 电话号码 + /// 是否为固定电话 + public static bool IsLandline(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return PhoneFullRegex.IsMatch(phone); + } + + /// + /// 验证是否为400电话 + /// + /// 电话号码 + /// 是否为400电话 + public static bool Is400Phone(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return Phone400Regex.IsMatch(phone); + } + + /// + /// 验证是否为800电话 + /// + /// 电话号码 + /// 是否为800电话 + public static bool Is800Phone(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return Phone800Regex.IsMatch(phone); + } + + /// + /// 验证区号是否有效 + /// + /// 区号 + /// 是否有效 + public static bool IsValidAreaCode(string? areaCode) + { + if (string.IsNullOrWhiteSpace(areaCode)) + { + return false; + } + + string code = areaCode.TrimStart('0'); + return AreaCodeMap.ContainsKey("0" + code) || AreaCodeMap.ContainsKey(areaCode); + } + + #endregion + + #region 信息提取 + + /// + /// 获取区号 + /// + /// 电话号码 + /// 区号 + public static string? GetAreaCode(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + // 400/800电话无区号 + if (Is400Phone(phone) || Is800Phone(phone)) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + + // 三位区号(0开头) + if (cleaned.Length >= 10 && cleaned.StartsWith("0")) + { + string code3 = cleaned.Substring(0, 3); + if (AreaCodeMap.ContainsKey(code3)) + { + return code3; + } + } + + // 四位区号(0开头) + if (cleaned.Length >= 11 && cleaned.StartsWith("0")) + { + string code4 = cleaned.Substring(0, 4); + if (AreaCodeMap.ContainsKey(code4)) + { + return code4; + } + } + + // 尝试提取前3-4位作为区号 + if (cleaned.StartsWith("0")) + { + for (int len = Math.Min(4, cleaned.Length - 7); len >= 3; len--) + { + string code = cleaned.Substring(0, len); + if (AreaCodeMap.ContainsKey(code)) + { + return code; + } + } + } + + return null; + } + + /// + /// 获取城市名称 + /// + /// 电话号码 + /// 城市名称 + public static string? GetCity(string? phone) + { + string? areaCode = GetAreaCode(phone); + if (areaCode == null) + { + return null; + } + + return AreaCodeMap.TryGetValue(areaCode, out string? city) ? city : null; + } + + /// + /// 获取本地号码(不含区号) + /// + /// 电话号码 + /// 本地号码 + public static string? GetLocalNumber(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + // 400/800电话 + if (Is400Phone(phone) || Is800Phone(phone)) + { + string local = Regex.Replace(phone, @"[^\d]", ""); + return local.Length >= 10 ? local.Substring(3) : null; + } + + string? areaCode = GetAreaCode(phone); + if (areaCode == null) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + return cleaned.Substring(areaCode.Length); + } + + /// + /// 获取电话类型 + /// + /// 电话号码 + /// 电话类型描述 + public static string? GetPhoneType(string? phone) + { + if (Is400Phone(phone)) return "400企业热线"; + if (Is800Phone(phone)) return "800免费电话"; + if (IsLandline(phone)) return "固定电话"; + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化电话号码(去除非数字字符) + /// + /// 电话号码 + /// 格式化后的号码 + public static string? Normalize(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + return cleaned.Length >= 7 ? cleaned : null; + } + + /// + /// 格式化为标准格式(区号-本地号码) + /// + /// 电话号码 + /// 格式化后的号码 + public static string? Format(string? phone) + { + string? normalized = Normalize(phone); + if (normalized == null) + { + return null; + } + + // 400电话 + if (normalized.StartsWith("400") && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 3)}-{normalized.Substring(6)}"; + } + + // 800电话 + if (normalized.StartsWith("800") && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 3)}-{normalized.Substring(6)}"; + } + + // 带区号的固定电话 + string? areaCode = GetAreaCode(normalized); + if (areaCode != null) + { + string local = normalized.Substring(areaCode.Length); + return $"{areaCode}-{local}"; + } + + return normalized; + } + + /// + /// 电话号码脱敏:010-****1234 + /// + /// 电话号码 + /// 脱敏后的号码 + public static string? Mask(string? phone) + { + if (!IsValid(phone)) + { + return null; + } + + string? areaCode = GetAreaCode(phone); + string? local = GetLocalNumber(phone); + + if (areaCode != null && local != null && local.Length >= 4) + { + int visibleSuffix = 4; + int maskLen = local.Length - visibleSuffix; + return $"{areaCode}-{new string('*', maskLen)}{local.Substring(maskLen)}"; + } + + // 400/800电话 + string? normalized = Normalize(phone); + if (normalized != null && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-****{normalized.Substring(6)}"; + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PortUtil.cs b/EasyTool.Core/BusinessCategory/PortUtil.cs new file mode 100644 index 0000000..b2adf9b --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PortUtil.cs @@ -0,0 +1,447 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.BusinessCategory +{ + /// + /// 端口号工具类 + /// + public static class PortUtil + { + #region 常量与私有字段 + + /// + /// 知名端口号范围(0-1023) + /// + public const int WellKnownPortMin = 0; + + /// + /// 知名端口号范围上限 + /// + public const int WellKnownPortMax = 1023; + + /// + /// 注册端口号范围(1024-49151) + /// + public const int RegisteredPortMin = 1024; + + /// + /// 注册端口号范围上限 + /// + public const int RegisteredPortMax = 49151; + + /// + /// 动态/私有端口号范围(49152-65535) + /// + public const int DynamicPortMin = 49152; + + /// + /// 最大端口号 + /// + public const int MaxPort = 65535; + + /// + /// 常见端口与名称映射 + /// + private static readonly Dictionary CommonPorts = new() + { + // 文件传输 + { 20, new PortInfo("FTP Data", "文件传输协议数据端口", "FTP") }, + { 21, new PortInfo("FTP", "文件传输协议控制端口", "FTP") }, + + // 远程连接 + { 22, new PortInfo("SSH", "安全外壳协议", "SSH") }, + { 23, new PortInfo("Telnet", "远程终端协议", "Telnet") }, + { 3389, new PortInfo("RDP", "远程桌面协议", "RDP") }, + + // 邮件服务 + { 25, new PortInfo("SMTP", "简单邮件传输协议", "SMTP") }, + { 110, new PortInfo("POP3", "邮局协议第3版", "POP3") }, + { 143, new PortInfo("IMAP", "互联网消息访问协议", "IMAP") }, + { 465, new PortInfo("SMTPS", "SMTP安全协议", "SMTPS") }, + { 587, new PortInfo("SMTP(TLS)", "SMTP TLS协议", "SMTP") }, + { 993, new PortInfo("IMAPS", "IMAP安全协议", "IMAPS") }, + { 995, new PortInfo("POP3S", "POP3安全协议", "POP3S") }, + + // Web服务 + { 80, new PortInfo("HTTP", "超文本传输协议", "HTTP") }, + { 443, new PortInfo("HTTPS", "HTTP安全协议", "HTTPS") }, + { 8080, new PortInfo("HTTP-Proxy", "HTTP代理/备用端口", "HTTP") }, + { 8443, new PortInfo("HTTPS-Alt", "HTTPS备用端口", "HTTPS") }, + + // 域名服务 + { 53, new PortInfo("DNS", "域名系统", "DNS") }, + + // 数据库 + { 1433, new PortInfo("MSSQL", "Microsoft SQL Server", "MSSQL") }, + { 1521, new PortInfo("Oracle", "Oracle数据库", "Oracle") }, + { 3306, new PortInfo("MySQL", "MySQL数据库", "MySQL") }, + { 5432, new PortInfo("PostgreSQL", "PostgreSQL数据库", "PostgreSQL") }, + { 6379, new PortInfo("Redis", "Redis缓存", "Redis") }, + { 27017, new PortInfo("MongoDB", "MongoDB数据库", "MongoDB") }, + { 9200, new PortInfo("Elasticsearch", "Elasticsearch搜索", "Elasticsearch") }, + + // 消息队列 + { 5672, new PortInfo("RabbitMQ", "RabbitMQ消息队列", "RabbitMQ") }, + { 9092, new PortInfo("Kafka", "Kafka消息队列", "Kafka") }, + { 61616, new PortInfo("ActiveMQ", "ActiveMQ消息队列", "ActiveMQ") }, + + // 网络服务 + { 67, new PortInfo("DHCP Server", "DHCP服务器", "DHCP") }, + { 68, new PortInfo("DHCP Client", "DHCP客户端", "DHCP") }, + { 69, new PortInfo("TFTP", "简单文件传输协议", "TFTP") }, + { 123, new PortInfo("NTP", "网络时间协议", "NTP") }, + { 161, new PortInfo("SNMP", "简单网络管理协议", "SNMP") }, + { 162, new PortInfo("SNMP Trap", "SNMP陷阱", "SNMP") }, + { 514, new PortInfo("Syslog", "系统日志", "Syslog") }, + + // VPN + { 500, new PortInfo("IKE", "Internet密钥交换", "VPN") }, + { 1194, new PortInfo("OpenVPN", "OpenVPN", "VPN") }, + { 1723, new PortInfo("PPTP", "点对点隧道协议", "VPN") }, + + // 其他常用 + { 88, new PortInfo("Kerberos", "Kerberos认证", "Kerberos") }, + { 389, new PortInfo("LDAP", "轻量级目录访问协议", "LDAP") }, + { 636, new PortInfo("LDAPS", "LDAP安全协议", "LDAP") }, + { 4444, new PortInfo("Kerberos-Admin", "Kerberos管理", "Kerberos") }, + + // 即时通讯 + { 5222, new PortInfo("XMPP", "XMPP客户端连接", "XMPP") }, + { 5269, new PortInfo("XMPP-Server", "XMPP服务器连接", "XMPP") }, + + // 游戏服务 + { 25565, new PortInfo("Minecraft", "Minecraft服务器", "Minecraft") }, + { 27015, new PortInfo("Steam", "Steam游戏服务", "Steam") }, + + // 文件共享 + { 139, new PortInfo("NetBIOS-SSN", "NetBIOS会话服务", "NetBIOS") }, + { 445, new PortInfo("SMB", "Server Message Block", "SMB") }, + { 2049, new PortInfo("NFS", "网络文件系统", "NFS") }, + + // 代理服务 + { 1080, new PortInfo("SOCKS", "SOCKS代理", "SOCKS") }, + { 3128, new PortInfo("Squid", "Squid代理", "Squid") }, + + // Java相关 + { 1099, new PortInfo("RMI", "Java RMI注册", "RMI") }, + { 8009, new PortInfo("AJP", "Apache JServ协议", "AJP") }, + + // 监控 + { 9090, new PortInfo("Prometheus", "Prometheus监控", "Prometheus") }, + { 3000, new PortInfo("Grafana", "Grafana监控", "Grafana") }, + { 8500, new PortInfo("Consul", "Consul服务发现", "Consul") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证端口号是否有效 + /// + /// 端口号 + /// 是否有效 + public static bool IsValid(int port) + { + return port >= WellKnownPortMin && port <= MaxPort; + } + + /// + /// 验证端口号字符串是否有效 + /// + /// 端口号字符串 + /// 是否有效 + public static bool IsValid(string? port) + { + if (string.IsNullOrWhiteSpace(port)) + { + return false; + } + + if (!int.TryParse(port, out int portNum)) + { + return false; + } + + return IsValid(portNum); + } + + #endregion + + #region 端口类型判断 + + /// + /// 判断是否为知名端口(0-1023) + /// + /// 端口号 + /// 是否为知名端口 + public static bool IsWellKnownPort(int port) + { + return port >= WellKnownPortMin && port <= WellKnownPortMax; + } + + /// + /// 判断是否为注册端口(1024-49151) + /// + /// 端口号 + /// 是否为注册端口 + public static bool IsRegisteredPort(int port) + { + return port >= RegisteredPortMin && port <= RegisteredPortMax; + } + + /// + /// 判断是否为动态/私有端口(49152-65535) + /// + /// 端口号 + /// 是否为动态端口 + public static bool IsDynamicPort(int port) + { + return port >= DynamicPortMin && port <= MaxPort; + } + + /// + /// 获取端口类型 + /// + /// 端口号 + /// 端口类型 + public static PortType GetPortType(int port) + { + if (IsWellKnownPort(port)) + { + return PortType.WellKnown; + } + else if (IsRegisteredPort(port)) + { + return PortType.Registered; + } + else if (IsDynamicPort(port)) + { + return PortType.Dynamic; + } + else + { + return PortType.Invalid; + } + } + + /// + /// 获取端口类型名称 + /// + /// 端口号 + /// 端口类型名称 + public static string? GetPortTypeName(int port) + { + return GetPortType(port) switch + { + PortType.WellKnown => "知名端口", + PortType.Registered => "注册端口", + PortType.Dynamic => "动态/私有端口", + _ => null + }; + } + + #endregion + + #region 端口信息 + + /// + /// 获取端口信息 + /// + /// 端口号 + /// 端口信息 + public static PortInfo? GetPortInfo(int port) + { + if (!IsValid(port)) + { + return null; + } + + return CommonPorts.TryGetValue(port, out PortInfo? info) ? info : null; + } + + /// + /// 获取端口名称 + /// + /// 端口号 + /// 端口名称 + public static string? GetPortName(int port) + { + return GetPortInfo(port)?.Name; + } + + /// + /// 获取端口描述 + /// + /// 端口号 + /// 端口描述 + public static string? GetPortDescription(int port) + { + return GetPortInfo(port)?.Description; + } + + /// + /// 获取端口所属服务类别 + /// + /// 端口号 + /// 服务类别 + public static string? GetPortCategory(int port) + { + return GetPortInfo(port)?.Category; + } + + /// + /// 判断是否为常见端口 + /// + /// 端口号 + /// 是否为常见端口 + public static bool IsCommonPort(int port) + { + return CommonPorts.ContainsKey(port); + } + + #endregion + + #region 范围操作 + + /// + /// 获取指定范围内的所有端口 + /// + /// 起始端口 + /// 结束端口 + /// 端口列表 + public static int[] GetPortRange(int start, int end) + { + if (start < WellKnownPortMin || end > MaxPort || start > end) + { + return Array.Empty(); + } + + int[] ports = new int[end - start + 1]; + for (int i = 0; i < ports.Length; i++) + { + ports[i] = start + i; + } + return ports; + } + + /// + /// 获取所有知名端口 + /// + /// 知名端口数组 + public static int[] GetWellKnownPorts() + { + return GetPortRange(WellKnownPortMin, WellKnownPortMax); + } + + /// + /// 获取所有动态端口范围 + /// + /// 动态端口数组 + public static int[] GetDynamicPorts() + { + return GetPortRange(DynamicPortMin, MaxPort); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化端口号为字符串 + /// + /// 端口号 + /// 端口号字符串 + public static string? Format(int port) + { + if (!IsValid(port)) + { + return null; + } + + return port.ToString(); + } + + /// + /// 格式化端口信息 + /// + /// 端口号 + /// 格式化的端口信息 + public static string? FormatWithInfo(int port) + { + if (!IsValid(port)) + { + return null; + } + + PortInfo? info = GetPortInfo(port); + if (info != null) + { + return $"{port} ({info.Name})"; + } + + string? typeName = GetPortTypeName(port); + return $"{port} ({typeName})"; + } + + #endregion + } + + /// + /// 端口类型枚举 + /// + public enum PortType + { + /// + /// 无效端口 + /// + Invalid = 0, + + /// + /// 知名端口(0-1023) + /// + WellKnown = 1, + + /// + /// 注册端口(1024-49151) + /// + Registered = 2, + + /// + /// 动态/私有端口(49152-65535) + /// + Dynamic = 3 + } + + /// + /// 端口信息类 + /// + public class PortInfo + { + /// + /// 端口名称 + /// + public string Name { get; set; } + + /// + /// 端口描述 + /// + public string Description { get; set; } + + /// + /// 服务类别 + /// + public string Category { get; set; } + + /// + /// 构造函数 + /// + public PortInfo(string name, string description, string category) + { + Name = name; + Description = description; + Category = category; + } + } +} diff --git a/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs b/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs new file mode 100644 index 0000000..b4112de --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs @@ -0,0 +1,437 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 邮政编码工具类 + /// + public static class PostalCodeUtil + { + #region 常量与私有字段 + + /// + /// 中国邮政编码正则表达式(6位数字) + /// + private static readonly Regex PostalCodeRegex = new Regex(@"^\d{6}$", RegexOptions.Compiled); + + /// + /// 省份编码前缀与名称映射(邮政编码前2位) + /// + private static readonly Dictionary ProvincePrefixMap = new Dictionary + { + { "10", "北京市" }, { "11", "北京市" }, { "12", "天津市" }, + { "01", "上海市" }, { "02", "上海市" }, { "03", "上海市" }, { "20", "上海市" }, + { "05", "河北省" }, { "06", "河北省" }, { "07", "河北省" }, + { "03", "山西省" }, { "04", "山西省" }, { "03", "内蒙古自治区" }, { "01", "内蒙古自治区" }, { "02", "内蒙古自治区" }, + { "11", "辽宁省" }, { "12", "辽宁省" }, + { "13", "吉林省" }, { "10", "吉林省" }, + { "15", "黑龙江省" }, { "16", "黑龙江省" }, + { "21", "江苏省" }, { "22", "江苏省" }, + { "31", "浙江省" }, { "32", "浙江省" }, + { "23", "安徽省" }, { "24", "安徽省" }, + { "35", "福建省" }, { "36", "福建省" }, + { "33", "江西省" }, { "34", "江西省" }, + { "25", "山东省" }, { "26", "山东省" }, { "27", "山东省" }, + { "45", "河南省" }, { "46", "河南省" }, { "47", "河南省" }, + { "41", "湖北省" }, { "42", "湖北省" }, { "43", "湖北省" }, { "44", "湖北省" }, + { "41", "湖南省" }, { "42", "湖南省" }, { "43", "湖南省" }, + { "51", "广东省" }, { "52", "广东省" }, { "53", "广东省" }, + { "54", "广西壮族自治区" }, { "55", "广西壮族自治区" }, + { "57", "海南省" }, { "58", "海南省" }, + { "40", "重庆市" }, + { "61", "四川省" }, { "62", "四川省" }, { "63", "四川省" }, { "64", "四川省" }, + { "55", "贵州省" }, { "56", "贵州省" }, + { "65", "云南省" }, { "66", "云南省" }, { "67", "云南省" }, + { "85", "西藏自治区" }, { "86", "西藏自治区" }, + { "71", "陕西省" }, { "72", "陕西省" }, { "73", "陕西省" }, + { "73", "甘肃省" }, { "74", "甘肃省" }, + { "81", "青海省" }, { "82", "青海省" }, { "83", "青海省" }, + { "75", "宁夏回族自治区" }, + { "83", "新疆维吾尔自治区" }, { "84", "新疆维吾尔自治区" } + }; + + /// + /// 城市邮政编码范围映射(部分主要城市) + /// + private static readonly Dictionary CityCodeRanges = new Dictionary + { + // 直辖市 + { "北京", ("100000", "102999", "北京市") }, + { "上海", ("200000", "202999", "上海市") }, + { "天津", ("300000", "302999", "天津市") }, + { "重庆", ("400000", "409999", "重庆市") }, + + // 省会城市 + { "石家庄", ("050000", "052999", "石家庄市") }, + { "太原", ("030000", "032999", "太原市") }, + { "呼和浩特", ("010000", "012999", "呼和浩特市") }, + { "沈阳", ("110000", "112999", "沈阳市") }, + { "长春", ("130000", "132999", "长春市") }, + { "哈尔滨", ("150000", "152999", "哈尔滨市") }, + { "南京", ("210000", "212999", "南京市") }, + { "杭州", ("310000", "312999", "杭州市") }, + { "合肥", ("230000", "232999", "合肥市") }, + { "福州", ("350000", "352999", "福州市") }, + { "南昌", ("330000", "332999", "南昌市") }, + { "济南", ("250000", "252999", "济南市") }, + { "郑州", ("450000", "452999", "郑州市") }, + { "武汉", ("430000", "432999", "武汉市") }, + { "长沙", ("410000", "412999", "长沙市") }, + { "广州", ("510000", "512999", "广州市") }, + { "南宁", ("530000", "532999", "南宁市") }, + { "海口", ("570000", "572999", "海口市") }, + { "成都", ("610000", "612999", "成都市") }, + { "贵阳", ("550000", "552999", "贵阳市") }, + { "昆明", ("650000", "652999", "昆明市") }, + { "拉萨", ("850000", "852999", "拉萨市") }, + { "西安", ("710000", "712999", "西安市") }, + { "兰州", ("730000", "732999", "兰州市") }, + { "西宁", ("810000", "812999", "西宁市") }, + { "银川", ("750000", "752999", "银川市") }, + { "乌鲁木齐", ("830000", "832999", "乌鲁木齐市") }, + + // 重要城市 + { "深圳", ("518000", "518999", "深圳市") }, + { "珠海", ("519000", "519999", "珠海市") }, + { "汕头", ("515000", "515999", "汕头市") }, + { "佛山", ("528000", "528999", "佛山市") }, + { "东莞", ("523000", "523999", "东莞市") }, + { "中山", ("528400", "528499", "中山市") }, + { "苏州", ("215000", "215999", "苏州市") }, + { "无锡", ("214000", "214999", "无锡市") }, + { "宁波", ("315000", "315999", "宁波市") }, + { "温州", ("325000", "325999", "温州市") }, + { "青岛", ("266000", "266999", "青岛市") }, + { "大连", ("116000", "116999", "大连市") }, + { "厦门", ("361000", "361999", "厦门市") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证邮政编码格式是否有效 + /// + /// 邮政编码 + /// 是否有效 + public static bool IsValid(string? postalCode) + { + if (string.IsNullOrWhiteSpace(postalCode)) + { + return false; + } + + return PostalCodeRegex.IsMatch(postalCode); + } + + /// + /// 验证邮政编码是否有效且存在对应的省份 + /// + /// 邮政编码 + /// 是否为有效且存在的邮政编码 + public static bool IsValidAndExists(string? postalCode) + { + if (!IsValid(postalCode)) + { + return false; + } + + return GetProvince(postalCode) != null; + } + + #endregion + + #region 信息查询 + + /// + /// 获取省份名称 + /// + /// 邮政编码 + /// 省份名称 + public static string? GetProvince(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + string prefix = postalCode!.Substring(0, 2); + + // 特殊处理直辖市 + if (prefix == "10" || prefix == "11") + { + return "北京市"; + } + if (prefix == "12") + { + return "天津市"; + } + if (prefix == "20" || prefix == "01" || prefix == "02") + { + return "上海市"; + } + if (prefix == "40") + { + return "重庆市"; + } + + // 根据前2位判断省份 + return prefix switch + { + "05" or "06" or "07" => "河北省", + "03" or "04" => "山西省", + "01" or "02" => CheckInnerMongolia(postalCode) ? "内蒙古自治区" : null, + "11" or "12" => "辽宁省", + "13" => "吉林省", + "15" or "16" => "黑龙江省", + "21" or "22" => "江苏省", + "31" or "32" => "浙江省", + "23" or "24" => "安徽省", + "35" or "36" => "福建省", + "33" or "34" => "江西省", + "25" or "26" or "27" => "山东省", + "45" or "46" or "47" => "河南省", + "43" or "44" => "湖北省", + "41" or "42" => "湖南省", + "51" or "52" or "53" => "广东省", + "54" or "55" => "广西壮族自治区", + "57" or "58" => "海南省", + "61" or "62" or "63" or "64" => "四川省", + "55" or "56" => "贵州省", + "65" or "66" or "67" => "云南省", + "85" or "86" => "西藏自治区", + "71" or "72" or "73" => "陕西省", + "73" or "74" => "甘肃省", + "81" or "82" => "青海省", + "75" => "宁夏回族自治区", + "83" or "84" => "新疆维吾尔自治区", + _ => null + }; + } + + /// + /// 获取城市名称(部分城市支持) + /// + /// 邮政编码 + /// 城市名称 + public static string? GetCity(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + string code = postalCode!; + + // 遍历城市编码范围 + foreach (var kvp in CityCodeRanges) + { + if (string.Compare(code, kvp.Value.Min) >= 0 && string.Compare(code, kvp.Value.Max) <= 0) + { + return kvp.Value.City; + } + } + + return null; + } + + /// + /// 根据城市名称查询邮政编码(返回主要邮编) + /// + /// 城市名称 + /// 邮政编码,未找到返回null + public static string? GetPostalCodeByCity(string? cityName) + { + if (string.IsNullOrWhiteSpace(cityName)) + { + return null; + } + + // 处理常见城市名称变体 + string normalizedCity = cityName.Replace("市", "").Trim(); + + foreach (var kvp in CityCodeRanges) + { + if (kvp.Key.Contains(normalizedCity) || normalizedCity.Contains(kvp.Key)) + { + return kvp.Value.Min; + } + } + + return null; + } + + /// + /// 获取邮政编码前缀(前2位) + /// + /// 邮政编码 + /// 前缀 + public static string? GetPrefix(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(0, 2); + } + + /// + /// 获取邮政编码后缀(后4位) + /// + /// 邮政编码 + /// 后缀 + public static string? GetSuffix(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(2, 4); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化邮政编码(去除非数字字符) + /// + /// 邮政编码 + /// 格式化后的邮政编码 + public static string? Normalize(string? postalCode) + { + if (string.IsNullOrWhiteSpace(postalCode)) + { + return null; + } + + // 去除所有非数字字符 + string normalized = Regex.Replace(postalCode, @"\D", ""); + + if (normalized.Length != 6) + { + return null; + } + + return normalized; + } + + /// + /// 邮政编码脱敏:100*** + /// + /// 邮政编码 + /// 脱敏后的邮政编码 + public static string? Mask(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(0, 3) + "***"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机邮政编码(仅供测试使用) + /// + /// 省份名称(可选,默认随机) + /// 6位邮政编码 + public static string GenerateRandom(string? province = null) + { + if (!string.IsNullOrWhiteSpace(province)) + { + // 根据省份生成 + string prefix = GetProvincePrefix(province); + if (!string.IsNullOrEmpty(prefix)) + { + return prefix + MathCategory.RandomUtil.RandomDigitString(4); + } + } + + // 随机生成有效前缀 + string[] validPrefixes = { + "10", "11", "12", "20", "30", "40", + "05", "06", "07", "03", "04", "01", "02", + "11", "12", "13", "15", "16", + "21", "22", "31", "32", "23", "24", + "35", "36", "33", "34", "25", "26", "27", + "45", "46", "47", "43", "44", "41", "42", + "51", "52", "53", "54", "55", "57", "58", + "40", "61", "62", "63", "64", "65", "66", "67", + "85", "86", "71", "72", "73", "74", "75", "81", "82", "83", "84" + }; + + string randomPrefix = MathCategory.RandomUtil.GetRandomElement(validPrefixes); + return randomPrefix + MathCategory.RandomUtil.RandomDigitString(4); + } + + #endregion + + #region 私有方法 + + /// + /// 检查是否为内蒙古邮编 + /// + private static bool CheckInnerMongolia(string postalCode) + { + // 内蒙古邮编范围:010000-029999 + string prefix = postalCode.Substring(0, 2); + return prefix == "01" || prefix == "02"; + } + + /// + /// 根据省份名称获取邮编前缀 + /// + private static string? GetProvincePrefix(string province) + { + string normalized = province.Replace("省", "").Replace("市", "").Replace("自治区", "").Trim(); + + return normalized switch + { + "北京" => "10", + "上海" => "20", + "天津" => "30", + "重庆" => "40", + "河北" => "05", + "山西" => "03", + "内蒙古" => "01", + "辽宁" => "11", + "吉林" => "13", + "黑龙江" => "15", + "江苏" => "21", + "浙江" => "31", + "安徽" => "23", + "福建" => "35", + "江西" => "33", + "山东" => "25", + "河南" => "45", + "湖北" => "43", + "湖南" => "41", + "广东" => "51", + "广西" => "54", + "海南" => "57", + "四川" => "61", + "贵州" => "55", + "云南" => "65", + "西藏" => "85", + "陕西" => "71", + "甘肃" => "73", + "青海" => "81", + "宁夏" => "75", + "新疆" => "83", + _ => null + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ProvinceUtil.cs b/EasyTool.Core/BusinessCategory/ProvinceUtil.cs new file mode 100644 index 0000000..acdfd75 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ProvinceUtil.cs @@ -0,0 +1,573 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.BusinessCategory +{ + /// + /// 中国省份城市工具类 + /// 提供省份、城市查询和验证功能 + /// + public static class ProvinceUtil + { + /// + /// 省份数据 + /// + private static readonly Dictionary Provinces = new() + { + { "110000", new ProvinceInfo { Code = "110000", Name = "北京市", ShortName = "北京", Cities = new List { + new CityInfo { Code = "110100", Name = "北京市" } + }}}, + { "120000", new ProvinceInfo { Code = "120000", Name = "天津市", ShortName = "天津", Cities = new List { + new CityInfo { Code = "120100", Name = "天津市" } + }}}, + { "130000", new ProvinceInfo { Code = "130000", Name = "河北省", ShortName = "河北", Cities = new List { + new CityInfo { Code = "130100", Name = "石家庄市" }, + new CityInfo { Code = "130200", Name = "唐山市" }, + new CityInfo { Code = "130300", Name = "秦皇岛市" }, + new CityInfo { Code = "130400", Name = "邯郸市" }, + new CityInfo { Code = "130500", Name = "邢台市" }, + new CityInfo { Code = "130600", Name = "保定市" }, + new CityInfo { Code = "130700", Name = "张家口市" }, + new CityInfo { Code = "130800", Name = "承德市" }, + new CityInfo { Code = "130900", Name = "沧州市" }, + new CityInfo { Code = "131000", Name = "廊坊市" }, + new CityInfo { Code = "131100", Name = "衡水市" } + }}}, + { "140000", new ProvinceInfo { Code = "140000", Name = "山西省", ShortName = "山西", Cities = new List { + new CityInfo { Code = "140100", Name = "太原市" }, + new CityInfo { Code = "140200", Name = "大同市" }, + new CityInfo { Code = "140300", Name = "阳泉市" }, + new CityInfo { Code = "140400", Name = "长治市" }, + new CityInfo { Code = "140500", Name = "晋城市" }, + new CityInfo { Code = "140600", Name = "朔州市" }, + new CityInfo { Code = "140700", Name = "晋中市" }, + new CityInfo { Code = "140800", Name = "运城市" }, + new CityInfo { Code = "140900", Name = "忻州市" }, + new CityInfo { Code = "141000", Name = "临汾市" }, + new CityInfo { Code = "141100", Name = "吕梁市" } + }}}, + { "150000", new ProvinceInfo { Code = "150000", Name = "内蒙古自治区", ShortName = "内蒙古", Cities = new List { + new CityInfo { Code = "150100", Name = "呼和浩特市" }, + new CityInfo { Code = "150200", Name = "包头市" }, + new CityInfo { Code = "150300", Name = "乌海市" }, + new CityInfo { Code = "150400", Name = "赤峰市" }, + new CityInfo { Code = "150500", Name = "通辽市" }, + new CityInfo { Code = "150600", Name = "鄂尔多斯市" }, + new CityInfo { Code = "150700", Name = "呼伦贝尔市" }, + new CityInfo { Code = "150800", Name = "巴彦淖尔市" }, + new CityInfo { Code = "150900", Name = "乌兰察布市" } + }}}, + { "210000", new ProvinceInfo { Code = "210000", Name = "辽宁省", ShortName = "辽宁", Cities = new List { + new CityInfo { Code = "210100", Name = "沈阳市" }, + new CityInfo { Code = "210200", Name = "大连市" }, + new CityInfo { Code = "210300", Name = "鞍山市" }, + new CityInfo { Code = "210400", Name = "抚顺市" }, + new CityInfo { Code = "210500", Name = "本溪市" }, + new CityInfo { Code = "210600", Name = "丹东市" }, + new CityInfo { Code = "210700", Name = "锦州市" }, + new CityInfo { Code = "210800", Name = "营口市" }, + new CityInfo { Code = "210900", Name = "阜新市" }, + new CityInfo { Code = "211000", Name = "辽阳市" }, + new CityInfo { Code = "211100", Name = "盘锦市" }, + new CityInfo { Code = "211200", Name = "铁岭市" }, + new CityInfo { Code = "211300", Name = "朝阳市" }, + new CityInfo { Code = "211400", Name = "葫芦岛市" } + }}}, + { "220000", new ProvinceInfo { Code = "220000", Name = "吉林省", ShortName = "吉林", Cities = new List { + new CityInfo { Code = "220100", Name = "长春市" }, + new CityInfo { Code = "220200", Name = "吉林市" }, + new CityInfo { Code = "220300", Name = "四平市" }, + new CityInfo { Code = "220400", Name = "辽源市" }, + new CityInfo { Code = "220500", Name = "通化市" }, + new CityInfo { Code = "220600", Name = "白山市" }, + new CityInfo { Code = "220700", Name = "松原市" }, + new CityInfo { Code = "220800", Name = "白城市" } + }}}, + { "230000", new ProvinceInfo { Code = "230000", Name = "黑龙江省", ShortName = "黑龙江", Cities = new List { + new CityInfo { Code = "230100", Name = "哈尔滨市" }, + new CityInfo { Code = "230200", Name = "齐齐哈尔市" }, + new CityInfo { Code = "230300", Name = "鸡西市" }, + new CityInfo { Code = "230400", Name = "鹤岗市" }, + new CityInfo { Code = "230500", Name = "双鸭山市" }, + new CityInfo { Code = "230600", Name = "大庆市" }, + new CityInfo { Code = "230700", Name = "伊春市" }, + new CityInfo { Code = "230800", Name = "佳木斯市" }, + new CityInfo { Code = "230900", Name = "七台河市" }, + new CityInfo { Code = "231000", Name = "牡丹江市" }, + new CityInfo { Code = "231100", Name = "黑河市" }, + new CityInfo { Code = "231200", Name = "绥化市" } + }}}, + { "310000", new ProvinceInfo { Code = "310000", Name = "上海市", ShortName = "上海", Cities = new List { + new CityInfo { Code = "310100", Name = "上海市" } + }}}, + { "320000", new ProvinceInfo { Code = "320000", Name = "江苏省", ShortName = "江苏", Cities = new List { + new CityInfo { Code = "320100", Name = "南京市" }, + new CityInfo { Code = "320200", Name = "无锡市" }, + new CityInfo { Code = "320300", Name = "徐州市" }, + new CityInfo { Code = "320400", Name = "常州市" }, + new CityInfo { Code = "320500", Name = "苏州市" }, + new CityInfo { Code = "320600", Name = "南通市" }, + new CityInfo { Code = "320700", Name = "连云港市" }, + new CityInfo { Code = "320800", Name = "淮安市" }, + new CityInfo { Code = "320900", Name = "盐城市" }, + new CityInfo { Code = "321000", Name = "扬州市" }, + new CityInfo { Code = "321100", Name = "镇江市" }, + new CityInfo { Code = "321200", Name = "泰州市" }, + new CityInfo { Code = "321300", Name = "宿迁市" } + }}}, + { "330000", new ProvinceInfo { Code = "330000", Name = "浙江省", ShortName = "浙江", Cities = new List { + new CityInfo { Code = "330100", Name = "杭州市" }, + new CityInfo { Code = "330200", Name = "宁波市" }, + new CityInfo { Code = "330300", Name = "温州市" }, + new CityInfo { Code = "330400", Name = "嘉兴市" }, + new CityInfo { Code = "330500", Name = "湖州市" }, + new CityInfo { Code = "330600", Name = "绍兴市" }, + new CityInfo { Code = "330700", Name = "金华市" }, + new CityInfo { Code = "330800", Name = "衢州市" }, + new CityInfo { Code = "330900", Name = "舟山市" }, + new CityInfo { Code = "331000", Name = "台州市" }, + new CityInfo { Code = "331100", Name = "丽水市" } + }}}, + { "340000", new ProvinceInfo { Code = "340000", Name = "安徽省", ShortName = "安徽", Cities = new List { + new CityInfo { Code = "340100", Name = "合肥市" }, + new CityInfo { Code = "340200", Name = "芜湖市" }, + new CityInfo { Code = "340300", Name = "蚌埠市" }, + new CityInfo { Code = "340400", Name = "淮南市" }, + new CityInfo { Code = "340500", Name = "马鞍山市" }, + new CityInfo { Code = "340600", Name = "淮北市" }, + new CityInfo { Code = "340700", Name = "铜陵市" }, + new CityInfo { Code = "340800", Name = "安庆市" }, + new CityInfo { Code = "341000", Name = "黄山市" }, + new CityInfo { Code = "341100", Name = "滁州市" }, + new CityInfo { Code = "341200", Name = "阜阳市" }, + new CityInfo { Code = "341300", Name = "宿州市" }, + new CityInfo { Code = "341500", Name = "六安市" }, + new CityInfo { Code = "341600", Name = "亳州市" }, + new CityInfo { Code = "341700", Name = "池州市" }, + new CityInfo { Code = "341800", Name = "宣城市" } + }}}, + { "350000", new ProvinceInfo { Code = "350000", Name = "福建省", ShortName = "福建", Cities = new List { + new CityInfo { Code = "350100", Name = "福州市" }, + new CityInfo { Code = "350200", Name = "厦门市" }, + new CityInfo { Code = "350300", Name = "莆田市" }, + new CityInfo { Code = "350400", Name = "三明市" }, + new CityInfo { Code = "350500", Name = "泉州市" }, + new CityInfo { Code = "350600", Name = "漳州市" }, + new CityInfo { Code = "350700", Name = "南平市" }, + new CityInfo { Code = "350800", Name = "龙岩市" }, + new CityInfo { Code = "350900", Name = "宁德市" } + }}}, + { "360000", new ProvinceInfo { Code = "360000", Name = "江西省", ShortName = "江西", Cities = new List { + new CityInfo { Code = "360100", Name = "南昌市" }, + new CityInfo { Code = "360200", Name = "景德镇市" }, + new CityInfo { Code = "360300", Name = "萍乡市" }, + new CityInfo { Code = "360400", Name = "九江市" }, + new CityInfo { Code = "360500", Name = "新余市" }, + new CityInfo { Code = "360600", Name = "鹰潭市" }, + new CityInfo { Code = "360700", Name = "赣州市" }, + new CityInfo { Code = "360800", Name = "吉安市" }, + new CityInfo { Code = "360900", Name = "宜春市" }, + new CityInfo { Code = "361000", Name = "抚州市" }, + new CityInfo { Code = "361100", Name = "上饶市" } + }}}, + { "370000", new ProvinceInfo { Code = "370000", Name = "山东省", ShortName = "山东", Cities = new List { + new CityInfo { Code = "370100", Name = "济南市" }, + new CityInfo { Code = "370200", Name = "青岛市" }, + new CityInfo { Code = "370300", Name = "淄博市" }, + new CityInfo { Code = "370400", Name = "枣庄市" }, + new CityInfo { Code = "370500", Name = "东营市" }, + new CityInfo { Code = "370600", Name = "烟台市" }, + new CityInfo { Code = "370700", Name = "潍坊市" }, + new CityInfo { Code = "370800", Name = "济宁市" }, + new CityInfo { Code = "370900", Name = "泰安市" }, + new CityInfo { Code = "371000", Name = "威海市" }, + new CityInfo { Code = "371100", Name = "日照市" }, + new CityInfo { Code = "371300", Name = "临沂市" }, + new CityInfo { Code = "371400", Name = "德州市" }, + new CityInfo { Code = "371500", Name = "聊城市" }, + new CityInfo { Code = "371600", Name = "滨州市" }, + new CityInfo { Code = "371700", Name = "菏泽市" } + }}}, + { "410000", new ProvinceInfo { Code = "410000", Name = "河南省", ShortName = "河南", Cities = new List { + new CityInfo { Code = "410100", Name = "郑州市" }, + new CityInfo { Code = "410200", Name = "开封市" }, + new CityInfo { Code = "410300", Name = "洛阳市" }, + new CityInfo { Code = "410400", Name = "平顶山市" }, + new CityInfo { Code = "410500", Name = "安阳市" }, + new CityInfo { Code = "410600", Name = "鹤壁市" }, + new CityInfo { Code = "410700", Name = "新乡市" }, + new CityInfo { Code = "410800", Name = "焦作市" }, + new CityInfo { Code = "410900", Name = "濮阳市" }, + new CityInfo { Code = "411000", Name = "许昌市" }, + new CityInfo { Code = "411100", Name = "漯河市" }, + new CityInfo { Code = "411200", Name = "三门峡市" }, + new CityInfo { Code = "411300", Name = "南阳市" }, + new CityInfo { Code = "411400", Name = "商丘市" }, + new CityInfo { Code = "411500", Name = "信阳市" }, + new CityInfo { Code = "411600", Name = "周口市" }, + new CityInfo { Code = "411700", Name = "驻马店市" } + }}}, + { "420000", new ProvinceInfo { Code = "420000", Name = "湖北省", ShortName = "湖北", Cities = new List { + new CityInfo { Code = "420100", Name = "武汉市" }, + new CityInfo { Code = "420200", Name = "黄石市" }, + new CityInfo { Code = "420300", Name = "十堰市" }, + new CityInfo { Code = "420500", Name = "宜昌市" }, + new CityInfo { Code = "420600", Name = "襄阳市" }, + new CityInfo { Code = "420700", Name = "鄂州市" }, + new CityInfo { Code = "420800", Name = "荆门市" }, + new CityInfo { Code = "420900", Name = "孝感市" }, + new CityInfo { Code = "421000", Name = "荆州市" }, + new CityInfo { Code = "421100", Name = "黄冈市" }, + new CityInfo { Code = "421200", Name = "咸宁市" }, + new CityInfo { Code = "421300", Name = "随州市" } + }}}, + { "430000", new ProvinceInfo { Code = "430000", Name = "湖南省", ShortName = "湖南", Cities = new List { + new CityInfo { Code = "430100", Name = "长沙市" }, + new CityInfo { Code = "430200", Name = "株洲市" }, + new CityInfo { Code = "430300", Name = "湘潭市" }, + new CityInfo { Code = "430400", Name = "衡阳市" }, + new CityInfo { Code = "430500", Name = "邵阳市" }, + new CityInfo { Code = "430600", Name = "岳阳市" }, + new CityInfo { Code = "430700", Name = "常德市" }, + new CityInfo { Code = "430800", Name = "张家界市" }, + new CityInfo { Code = "430900", Name = "益阳市" }, + new CityInfo { Code = "431000", Name = "郴州市" }, + new CityInfo { Code = "431100", Name = "永州市" }, + new CityInfo { Code = "431200", Name = "怀化市" }, + new CityInfo { Code = "431300", Name = "娄底市" } + }}}, + { "440000", new ProvinceInfo { Code = "440000", Name = "广东省", ShortName = "广东", Cities = new List { + new CityInfo { Code = "440100", Name = "广州市" }, + new CityInfo { Code = "440200", Name = "韶关市" }, + new CityInfo { Code = "440300", Name = "深圳市" }, + new CityInfo { Code = "440400", Name = "珠海市" }, + new CityInfo { Code = "440500", Name = "汕头市" }, + new CityInfo { Code = "440600", Name = "佛山市" }, + new CityInfo { Code = "440700", Name = "江门市" }, + new CityInfo { Code = "440800", Name = "湛江市" }, + new CityInfo { Code = "440900", Name = "茂名市" }, + new CityInfo { Code = "441200", Name = "肇庆市" }, + new CityInfo { Code = "441300", Name = "惠州市" }, + new CityInfo { Code = "441400", Name = "梅州市" }, + new CityInfo { Code = "441500", Name = "汕尾市" }, + new CityInfo { Code = "441600", Name = "河源市" }, + new CityInfo { Code = "441700", Name = "阳江市" }, + new CityInfo { Code = "441800", Name = "清远市" }, + new CityInfo { Code = "441900", Name = "东莞市" }, + new CityInfo { Code = "442000", Name = "中山市" }, + new CityInfo { Code = "445100", Name = "潮州市" }, + new CityInfo { Code = "445200", Name = "揭阳市" }, + new CityInfo { Code = "445300", Name = "云浮市" } + }}}, + { "450000", new ProvinceInfo { Code = "450000", Name = "广西壮族自治区", ShortName = "广西", Cities = new List { + new CityInfo { Code = "450100", Name = "南宁市" }, + new CityInfo { Code = "450200", Name = "柳州市" }, + new CityInfo { Code = "450300", Name = "桂林市" }, + new CityInfo { Code = "450400", Name = "梧州市" }, + new CityInfo { Code = "450500", Name = "北海市" }, + new CityInfo { Code = "450600", Name = "防城港市" }, + new CityInfo { Code = "450700", Name = "钦州市" }, + new CityInfo { Code = "450800", Name = "贵港市" }, + new CityInfo { Code = "450900", Name = "玉林市" }, + new CityInfo { Code = "451000", Name = "百色市" }, + new CityInfo { Code = "451100", Name = "贺州市" }, + new CityInfo { Code = "451200", Name = "河池市" }, + new CityInfo { Code = "451300", Name = "来宾市" }, + new CityInfo { Code = "451400", Name = "崇左市" } + }}}, + { "460000", new ProvinceInfo { Code = "460000", Name = "海南省", ShortName = "海南", Cities = new List { + new CityInfo { Code = "460100", Name = "海口市" }, + new CityInfo { Code = "460200", Name = "三亚市" }, + new CityInfo { Code = "460300", Name = "三沙市" }, + new CityInfo { Code = "460400", Name = "儋州市" } + }}}, + { "500000", new ProvinceInfo { Code = "500000", Name = "重庆市", ShortName = "重庆", Cities = new List { + new CityInfo { Code = "500100", Name = "重庆市" } + }}}, + { "510000", new ProvinceInfo { Code = "510000", Name = "四川省", ShortName = "四川", Cities = new List { + new CityInfo { Code = "510100", Name = "成都市" }, + new CityInfo { Code = "510300", Name = "自贡市" }, + new CityInfo { Code = "510400", Name = "攀枝花市" }, + new CityInfo { Code = "510500", Name = "泸州市" }, + new CityInfo { Code = "510600", Name = "德阳市" }, + new CityInfo { Code = "510700", Name = "绵阳市" }, + new CityInfo { Code = "510800", Name = "广元市" }, + new CityInfo { Code = "510900", Name = "遂宁市" }, + new CityInfo { Code = "511000", Name = "内江市" }, + new CityInfo { Code = "511100", Name = "乐山市" }, + new CityInfo { Code = "511300", Name = "南充市" }, + new CityInfo { Code = "511400", Name = "眉山市" }, + new CityInfo { Code = "511500", Name = "宜宾市" }, + new CityInfo { Code = "511600", Name = "广安市" }, + new CityInfo { Code = "511700", Name = "达州市" }, + new CityInfo { Code = "511800", Name = "雅安市" }, + new CityInfo { Code = "511900", Name = "巴中市" }, + new CityInfo { Code = "512000", Name = "资阳市" } + }}}, + { "520000", new ProvinceInfo { Code = "520000", Name = "贵州省", ShortName = "贵州", Cities = new List { + new CityInfo { Code = "520100", Name = "贵阳市" }, + new CityInfo { Code = "520200", Name = "六盘水市" }, + new CityInfo { Code = "520300", Name = "遵义市" }, + new CityInfo { Code = "520400", Name = "安顺市" }, + new CityInfo { Code = "520500", Name = "毕节市" }, + new CityInfo { Code = "520600", Name = "铜仁市" } + }}}, + { "530000", new ProvinceInfo { Code = "530000", Name = "云南省", ShortName = "云南", Cities = new List { + new CityInfo { Code = "530100", Name = "昆明市" }, + new CityInfo { Code = "530300", Name = "曲靖市" }, + new CityInfo { Code = "530400", Name = "玉溪市" }, + new CityInfo { Code = "530500", Name = "保山市" }, + new CityInfo { Code = "530600", Name = "昭通市" }, + new CityInfo { Code = "530700", Name = "丽江市" }, + new CityInfo { Code = "530800", Name = "普洱市" }, + new CityInfo { Code = "530900", Name = "临沧市" } + }}}, + { "540000", new ProvinceInfo { Code = "540000", Name = "西藏自治区", ShortName = "西藏", Cities = new List { + new CityInfo { Code = "540100", Name = "拉萨市" }, + new CityInfo { Code = "540200", Name = "日喀则市" }, + new CityInfo { Code = "540300", Name = "昌都市" }, + new CityInfo { Code = "540400", Name = "林芝市" }, + new CityInfo { Code = "540500", Name = "山南市" }, + new CityInfo { Code = "540600", Name = "那曲市" } + }}}, + { "610000", new ProvinceInfo { Code = "610000", Name = "陕西省", ShortName = "陕西", Cities = new List { + new CityInfo { Code = "610100", Name = "西安市" }, + new CityInfo { Code = "610200", Name = "铜川市" }, + new CityInfo { Code = "610300", Name = "宝鸡市" }, + new CityInfo { Code = "610400", Name = "咸阳市" }, + new CityInfo { Code = "610500", Name = "渭南市" }, + new CityInfo { Code = "610600", Name = "延安市" }, + new CityInfo { Code = "610700", Name = "汉中市" }, + new CityInfo { Code = "610800", Name = "榆林市" }, + new CityInfo { Code = "610900", Name = "安康市" }, + new CityInfo { Code = "611000", Name = "商洛市" } + }}}, + { "620000", new ProvinceInfo { Code = "620000", Name = "甘肃省", ShortName = "甘肃", Cities = new List { + new CityInfo { Code = "620100", Name = "兰州市" }, + new CityInfo { Code = "620200", Name = "嘉峪关市" }, + new CityInfo { Code = "620300", Name = "金昌市" }, + new CityInfo { Code = "620400", Name = "白银市" }, + new CityInfo { Code = "620500", Name = "天水市" }, + new CityInfo { Code = "620600", Name = "武威市" }, + new CityInfo { Code = "620700", Name = "张掖市" }, + new CityInfo { Code = "620800", Name = "平凉市" }, + new CityInfo { Code = "620900", Name = "酒泉市" }, + new CityInfo { Code = "621000", Name = "庆阳市" }, + new CityInfo { Code = "621100", Name = "定西市" }, + new CityInfo { Code = "621200", Name = "陇南市" } + }}}, + { "630000", new ProvinceInfo { Code = "630000", Name = "青海省", ShortName = "青海", Cities = new List { + new CityInfo { Code = "630100", Name = "西宁市" }, + new CityInfo { Code = "630200", Name = "海东市" } + }}}, + { "640000", new ProvinceInfo { Code = "640000", Name = "宁夏回族自治区", ShortName = "宁夏", Cities = new List { + new CityInfo { Code = "640100", Name = "银川市" }, + new CityInfo { Code = "640200", Name = "石嘴山市" }, + new CityInfo { Code = "640300", Name = "吴忠市" }, + new CityInfo { Code = "640400", Name = "固原市" }, + new CityInfo { Code = "640500", Name = "中卫市" } + }}}, + { "650000", new ProvinceInfo { Code = "650000", Name = "新疆维吾尔自治区", ShortName = "新疆", Cities = new List { + new CityInfo { Code = "650100", Name = "乌鲁木齐市" }, + new CityInfo { Code = "650200", Name = "克拉玛依市" } + }}}, + { "710000", new ProvinceInfo { Code = "710000", Name = "台湾省", ShortName = "台湾", Cities = new List() }}, + { "810000", new ProvinceInfo { Code = "810000", Name = "香港特别行政区", ShortName = "香港", Cities = new List() }}, + { "820000", new ProvinceInfo { Code = "820000", Name = "澳门特别行政区", ShortName = "澳门", Cities = new List() }} + }; + + /// + /// 根据省份代码获取省份信息 + /// + public static ProvinceInfo? GetProvinceByCode(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length < 2) + return null; + + var provinceCode = code.Substring(0, 2) + "0000"; + return Provinces.TryGetValue(provinceCode, out var province) ? province : null; + } + + /// + /// 根据省份名称获取省份信息 + /// + public static ProvinceInfo? GetProvinceByName(string? name) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + + foreach (var province in Provinces.Values) + { + if (province.Name == name || province.ShortName == name || + province.Name.Contains(name) || name.Contains(province.ShortName)) + { + return province; + } + } + + return null; + } + + /// + /// 根据城市代码获取城市信息 + /// + public static CityInfo? GetCityByCode(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length < 4) + return null; + + var provinceCode = code.Substring(0, 2) + "0000"; + if (!Provinces.TryGetValue(provinceCode, out var province)) + return null; + + foreach (var city in province.Cities) + { + if (city.Code == code) + return city; + } + + return null; + } + + /// + /// 根据城市名称获取城市信息 + /// + public static CityInfo? GetCityByName(string? name, string? provinceName = null) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + + foreach (var province in Provinces.Values) + { + if (!string.IsNullOrEmpty(provinceName) && + province.Name != provinceName && + province.ShortName != provinceName) + continue; + + foreach (var city in province.Cities) + { + if (city.Name == name || city.Name.Contains(name) || name.Contains(city.Name.Replace("市", ""))) + { + return city; + } + } + } + + return null; + } + + /// + /// 获取所有省份 + /// + public static IEnumerable GetAllProvinces() + { + return Provinces.Values; + } + + /// + /// 获取省份下的所有城市 + /// + public static IEnumerable GetCitiesByProvinceCode(string? provinceCode) + { + var province = GetProvinceByCode(provinceCode); + return province?.Cities ?? Enumerable.Empty(); + } + + /// + /// 验证行政区划代码是否有效 + /// + public static bool IsValidCode(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + return false; + + code = code.Trim(); + if (code.Length != 6) + return false; + + foreach (var c in code) + { + if (!char.IsDigit(c)) + return false; + } + + var provinceCode = code.Substring(0, 2) + "0000"; + return Provinces.ContainsKey(provinceCode); + } + + /// + /// 根据身份证号前6位获取籍贯 + /// + public static string? GetNativePlace(string? idCardPrefix) + { + if (string.IsNullOrWhiteSpace(idCardPrefix) || idCardPrefix.Length < 6) + return null; + + var provinceCode = idCardPrefix.Substring(0, 2) + "0000"; + if (!Provinces.TryGetValue(provinceCode, out var province)) + return null; + + var cityCode = idCardPrefix.Substring(0, 4) + "00"; + foreach (var city in province.Cities) + { + if (city.Code == cityCode) + { + return $"{province.Name}{city.Name}"; + } + } + + return province.Name; + } + } + + #region 数据类 + + /// + /// 省份信息 + /// + public class ProvinceInfo + { + /// + /// 省份代码(6位) + /// + public string Code { get; set; } = string.Empty; + + /// + /// 省份名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 简称 + /// + public string ShortName { get; set; } = string.Empty; + + /// + /// 城市列表 + /// + public List Cities { get; set; } = new(); + + public override string ToString() => Name; + } + + /// + /// 城市信息 + /// + public class CityInfo + { + /// + /// 城市代码(6位) + /// + public string Code { get; set; } = string.Empty; + + /// + /// 城市名称 + /// + public string Name { get; set; } = string.Empty; + + public override string ToString() => Name; + } + + #endregion +} diff --git a/EasyTool.Core/BusinessCategory/QQUtil.cs b/EasyTool.Core/BusinessCategory/QQUtil.cs new file mode 100644 index 0000000..1357b92 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/QQUtil.cs @@ -0,0 +1,179 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// QQ号工具类 + /// + public static class QQUtil + { + #region 常量与私有字段 + + /// + /// QQ号正则表达式(5-11位数字,不以0开头) + /// + private static readonly Regex QQRegex = new( + @"^[1-9]\d{4,10}$", + RegexOptions.Compiled); + + /// + /// QQ邮箱正则表达式 + /// + private static readonly Regex QQEmailRegex = new( + @"^[1-9]\d{4,10}@qq\.com$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + #endregion + + #region 验证方法 + + /// + /// 验证QQ号是否有效 + /// + /// QQ号 + /// 是否有效 + public static bool IsValid(string? qq) + { + if (string.IsNullOrWhiteSpace(qq)) + { + return false; + } + + return QQRegex.IsMatch(qq); + } + + /// + /// 验证QQ邮箱是否有效 + /// + /// QQ邮箱 + /// 是否有效 + public static bool IsValidQQEmail(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + return QQEmailRegex.IsMatch(email); + } + + /// + /// 验证QQ号格式(仅检查格式,不验证是否存在) + /// + /// QQ号 + /// 格式是否正确 + public static bool IsValidFormat(string? qq) + { + return IsValid(qq); + } + + #endregion + + #region 转换方法 + + /// + /// 从QQ邮箱提取QQ号 + /// + /// QQ邮箱 + /// QQ号,提取失败返回null + public static string? ExtractFromEmail(string? email) + { + if (!IsValidQQEmail(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(0, atIndex); + } + + /// + /// 将QQ号转换为QQ邮箱 + /// + /// QQ号 + /// QQ邮箱 + public static string? ToEmail(string? qq) + { + if (!IsValid(qq)) + { + return null; + } + + return qq + "@qq.com"; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化QQ号(去除非数字字符) + /// + /// QQ号 + /// 格式化后的QQ号 + public static string? Normalize(string? qq) + { + if (string.IsNullOrWhiteSpace(qq)) + { + return null; + } + + string cleaned = Regex.Replace(qq, @"\D", ""); + return IsValid(cleaned) ? cleaned : null; + } + + /// + /// QQ号脱敏:123****890 + /// + /// QQ号 + /// 脱敏后的QQ号 + public static string? Mask(string? qq) + { + if (!IsValid(qq)) + { + return null; + } + + string code = qq!; + if (code.Length <= 4) + { + return code[0] + new string('*', code.Length - 1); + } + + // 保留前3位和后3位 + int prefixLen = 3; + int suffixLen = 3; + int maskLen = code.Length - prefixLen - suffixLen; + + return code.Substring(0, prefixLen) + new string('*', maskLen) + code.Substring(code.Length - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机QQ号(仅供测试使用) + /// + /// 随机QQ号 + public static string GenerateRandom() + { + // QQ号长度5-11位 + int length = MathCategory.RandomUtil.RandomInt(5, 12); + + // 第一位不能为0 + string result = MathCategory.RandomUtil.RandomInt(1, 10).ToString(); + + // 剩余位数 + for (int i = 1; i < length; i++) + { + result += MathCategory.RandomUtil.RandomInt(0, 10).ToString(); + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs b/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs new file mode 100644 index 0000000..c033fbd --- /dev/null +++ b/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs @@ -0,0 +1,293 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 社保号工具类 + /// + public static class SocialSecurityUtil + { + #region 常量与私有字段 + + /// + /// 社保号正则表达式(18位,与身份证号格式相同) + /// + private static readonly Regex SSN18Regex = new( + @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 社保号正则表达式(部分省市为15位或16位) + /// + private static readonly Regex SSN15Regex = new(@"^\d{15,16}$", RegexOptions.Compiled); + + /// + /// 社会保障卡号正则(带字母) + /// + private static readonly Regex SSNCardRegex = new( + @"^[A-Za-z]\d{17}$", + RegexOptions.Compiled); + + /// + /// 校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 省份社保号规则(简化版) + /// + private static readonly Dictionary ProvinceLengthMap = new() + { + { "北京", 18 }, { "上海", 18 }, { "天津", 18 }, { "重庆", 18 }, + { "广东", 18 }, { "浙江", 18 }, { "江苏", 18 }, { "山东", 18 }, + { "四川", 18 }, { "湖北", 18 }, { "河南", 18 }, { "河北", 18 }, + { "福建", 18 }, { "安徽", 18 }, { "辽宁", 18 }, { "陕西", 18 }, + { "湖南", 18 }, { "江西", 18 }, { "云南", 18 }, { "贵州", 18 }, + { "甘肃", 18 }, { "青海", 18 }, { "宁夏", 18 }, { "新疆", 18 }, + { "西藏", 18 }, { "内蒙古", 18 }, { "广西", 18 }, { "黑龙江", 18 }, + { "吉林", 18 }, { "山西", 18 }, { "海南", 18 } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证社保号是否有效 + /// + /// 社保号 + /// 是否有效 + public static bool IsValid(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + + // 18位(身份证号格式) + if (cleaned.Length == 18 && SSN18Regex.IsMatch(cleaned)) + { + return ValidateCheckDigit(cleaned); + } + + // 15-16位纯数字 + if (SSN15Regex.IsMatch(cleaned)) + { + return true; + } + + // 带字母的卡号 + if (SSNCardRegex.IsMatch(cleaned)) + { + return true; + } + + return false; + } + + /// + /// 验证是否为18位社保号(身份证号格式) + /// + /// 社保号 + /// 是否为18位 + public static bool Is18Digit(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + return cleaned.Length == 18 && SSN18Regex.IsMatch(cleaned) && ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 社保号 + /// 格式是否正确 + public static bool IsValidFormat(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + return SSN18Regex.IsMatch(cleaned) || SSN15Regex.IsMatch(cleaned) || SSNCardRegex.IsMatch(cleaned); + } + + /// + /// 验证校验位 + /// + private static bool ValidateCheckDigit(string ssn) + { + if (ssn.Length != 18) return false; + + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (ssn[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + return char.ToUpper(ssn[17]) == expectedCheckCode; + } + + #endregion + + #region 信息提取 + + /// + /// 获取出生日期(仅18位格式) + /// + /// 社保号 + /// 出生日期 + public static DateTime? GetBirthday(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + string cleaned = ssn!.Trim(); + int year = int.Parse(cleaned.Substring(6, 4)); + int month = int.Parse(cleaned.Substring(10, 2)); + int day = int.Parse(cleaned.Substring(12, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取性别(仅18位格式) + /// + /// 社保号 + /// 性别(1男2女) + public static int? GetGender(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + int genderDigit = ssn![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 社保号 + /// 性别 + public static string? GetGenderString(string? ssn) + { + int? gender = GetGender(ssn); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取行政区划代码(仅18位格式) + /// + /// 社保号 + /// 行政区划代码 + public static string? GetAreaCode(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + return ssn!.Substring(0, 6); + } + + /// + /// 获取年龄(仅18位格式) + /// + /// 社保号 + /// 年龄 + public static int? GetAge(string? ssn) + { + DateTime? birthday = GetBirthday(ssn); + if (!birthday.HasValue) + { + return null; + } + + DateTime today = DateTime.Today; + int age = today.Year - birthday.Value.Year; + if (today < birthday.Value.AddYears(age)) + { + age--; + } + + return age; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化社保号 + /// + /// 社保号 + /// 格式化后的社保号 + public static string? Normalize(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return null; + } + + string cleaned = ssn.Trim().ToUpper(); + return IsValidFormat(cleaned) ? cleaned : null; + } + + /// + /// 社保号脱敏:110***********1234 + /// + /// 社保号 + /// 脱敏后的社保号 + public static string? Mask(string? ssn) + { + if (!IsValid(ssn)) + { + return null; + } + + string cleaned = ssn!.Trim().ToUpper(); + + if (cleaned.Length == 18) + { + return cleaned.Substring(0, 3) + "***********" + cleaned.Substring(14); + } + + if (cleaned.Length >= 15) + { + int prefixLen = 3; + int suffixLen = 4; + return cleaned.Substring(0, prefixLen) + + new string('*', cleaned.Length - prefixLen - suffixLen) + + cleaned.Substring(cleaned.Length - suffixLen); + } + + return cleaned[0] + new string('*', cleaned.Length - 2) + cleaned[^1]; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/StockCodeUtil.cs b/EasyTool.Core/BusinessCategory/StockCodeUtil.cs new file mode 100644 index 0000000..a2d7122 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/StockCodeUtil.cs @@ -0,0 +1,529 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 股票市场枚举 + /// + public enum StockMarket + { + /// + /// 未知 + /// + Unknown = 0, + + /// + /// 上海证券交易所 + /// + SHSE = 1, + + /// + /// 深圳证券交易所 + /// + SZSE = 2, + + /// + /// 北京证券交易所 + /// + BSE = 3, + + /// + /// 香港交易所 + /// + HKEX = 4, + + /// + /// 纽约证券交易所 + /// + NYSE = 5, + + /// + /// 纳斯达克 + /// + NASDAQ = 6 + } + + /// + /// 股票类型枚举 + /// + public enum StockType + { + /// + /// 未知 + /// + Unknown = 0, + + /// + /// A股 + /// + AShare = 1, + + /// + /// B股 + /// + BShare = 2, + + /// + /// 创业板 + /// + ChiNext = 3, + + /// + /// 科创板 + /// + STAR = 4, + + /// + /// 北交所 + /// + BSEShare = 5, + + /// + /// 港股 + /// + HKStock = 6, + + /// + /// 美股 + /// + USStock = 7 + } + + /// + /// 股票代码工具类 + /// + public static class StockCodeUtil + { + #region 常量与私有字段 + + /// + /// A股代码正则表达式(6位数字) + /// + private static readonly Regex AShareRegex = new(@"^[036]\d{5}$", RegexOptions.Compiled); + + /// + /// B股代码正则表达式 + /// + private static readonly Regex BShareRegex = new(@"^[29]\d{5}$", RegexOptions.Compiled); + + /// + /// 创业板代码正则表达式(30开头) + /// + private static readonly Regex ChiNextRegex = new(@"^30\d{4}$", RegexOptions.Compiled); + + /// + /// 科创板代码正则表达式(688开头) + /// + private static readonly Regex STARRegex = new(@"^688\d{3}$", RegexOptions.Compiled); + + /// + /// 北交所代码正则表达式(8开头,4位或6位) + /// + private static readonly Regex BSERegex = new(@"^(8[34]\d{4}|4[38]\d{4})$", RegexOptions.Compiled); + + /// + /// 港股代码正则表达式(1-5位数字) + /// + private static readonly Regex HKStockRegex = new(@"^\d{4,5}$", RegexOptions.Compiled); + + /// + /// 美股代码正则表达式(1-5位大写字母) + /// + private static readonly Regex USStockRegex = new(@"^[A-Z]{1,5}$", RegexOptions.Compiled); + + /// + /// 常见A股股票代码映射(部分示例) + /// + private static readonly Dictionary StockCodeMap = new() + { + // 上证A股 + { "600000", ("浦发银行", "上海") }, { "600036", ("招商银行", "上海") }, + { "600519", ("贵州茅台", "上海") }, { "600887", ("伊利股份", "上海") }, + { "601318", ("中国平安", "上海") }, { "601398", ("工商银行", "上海") }, + { "601939", ("建设银行", "上海") }, { "601988", ("中国银行", "上海") }, + { "601288", ("农业银行", "上海") }, { "601857", ("中国石油", "上海") }, + { "601668", ("中国建筑", "上海") }, { "600276", ("恒瑞医药", "上海") }, + { "600309", ("万华化学", "上海") }, { "600900", ("长江电力", "上海") }, + { "601012", ("隆基绿能", "上海") }, { "603259", ("药明康德", "上海") }, + + // 深证A股 + { "000001", ("平安银行", "深圳") }, { "000002", ("万科A", "深圳") }, + { "000333", ("美的集团", "深圳") }, { "000651", ("格力电器", "深圳") }, + { "000858", ("五粮液", "深圳") }, { "002594", ("比亚迪", "深圳") }, + { "000063", ("中兴通讯", "深圳") }, { "002475", ("立讯精密", "深圳") }, + { "002415", ("海康威视", "深圳") }, { "002352", ("顺丰控股", "深圳") }, + { "000568", ("泸州老窖", "深圳") }, { "002714", ("牧原股份", "深圳") }, + + // 创业板 + { "300750", ("宁德时代", "深圳") }, { "300059", ("东方财富", "深圳") }, + { "300015", ("爱尔眼科", "深圳") }, { "300347", ("泰格医药", "深圳") }, + { "300760", ("迈瑞医疗", "深圳") }, { "300124", ("汇川技术", "深圳") }, + + // 科创板 + { "688981", ("中芯国际", "上海") }, { "688111", ("金山办公", "上海") }, + { "688012", ("中微公司", "上海") }, { "688256", ("寒武纪", "上海") }, + + // 港股 + { "00700", ("腾讯控股", "香港") }, { "09988", ("阿里巴巴-SW", "香港") }, + { "03690", ("美团-W", "香港") }, { "09999", ("网易-S", "香港") }, + { "01024", ("快手-W", "香港") }, { "01810", ("小米集团-W", "香港") }, + { "09618", ("京东集团-SW", "香港") }, { "02318", ("中国平安", "香港") }, + { "00005", ("汇丰控股", "香港") }, { "00941", ("中国移动", "香港") }, + { "03988", ("中国银行", "香港") }, { "01398", ("工商银行", "香港") }, + + // 美股 + { "AAPL", ("苹果", "纳斯达克") }, { "MSFT", ("微软", "纳斯达克") }, + { "GOOGL", ("谷歌", "纳斯达克") }, { "AMZN", ("亚马逊", "纳斯达克") }, + { "META", ("Meta", "纳斯达克") }, { "NVDA", ("英伟达", "纳斯达克") }, + { "TSLA", ("特斯拉", "纳斯达克") }, { "NFLX", ("奈飞", "纳斯达克") }, + { "BABA", ("阿里巴巴", "纽约") }, { "JD", ("京东", "纳斯达克") }, + { "PDD", ("拼多多", "纳斯达克") }, { "BIDU", ("百度", "纳斯达克") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证股票代码是否有效(支持A股、港股、美股) + /// + /// 股票代码 + /// 市场类型(可选,默认自动识别) + /// 是否有效 + public static bool IsValid(string? code, StockMarket? market = null) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + if (market.HasValue) + { + return market.Value switch + { + StockMarket.SHSE or StockMarket.SZSE => IsValidAShare(code), + StockMarket.BSE => IsValidBSE(code), + StockMarket.HKEX => IsValidHKStock(code), + StockMarket.NYSE or StockMarket.NASDAQ => IsValidUSStock(code), + _ => false + }; + } + + return IsValidAShare(code) || IsValidBSE(code) || IsValidHKStock(code) || IsValidUSStock(code); + } + + /// + /// 验证A股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidAShare(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length != 6) + { + return false; + } + + // A股(60、00、30、688开头)和B股(20、900开头) + return AShareRegex.IsMatch(code) || BShareRegex.IsMatch(code) || + ChiNextRegex.IsMatch(code) || STARRegex.IsMatch(code); + } + + /// + /// 验证北交所代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidBSE(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length != 6) + { + return false; + } + + return BSERegex.IsMatch(code); + } + + /// + /// 验证港股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidHKStock(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + return HKStockRegex.IsMatch(code); + } + + /// + /// 验证美股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidUSStock(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + return USStockRegex.IsMatch(code.ToUpper()); + } + + #endregion + + #region 市场识别 + + /// + /// 获取股票市场 + /// + /// 股票代码 + /// 股票市场 + public static StockMarket GetMarket(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return StockMarket.Unknown; + } + + string upper = code.ToUpper(); + + // 美股(字母代码) + if (USStockRegex.IsMatch(upper)) + { + return StockMarket.NASDAQ; // 简化处理 + } + + // 港股(4-5位数字) + if (HKStockRegex.IsMatch(code)) + { + return StockMarket.HKEX; + } + + // A股(6位数字) + if (code.Length == 6) + { + if (code.StartsWith("60") || code.StartsWith("68")) + { + return StockMarket.SHSE; + } + if (code.StartsWith("00") || code.StartsWith("30")) + { + return StockMarket.SZSE; + } + if (code.StartsWith("83") || code.StartsWith("87") || code.StartsWith("43") || code.StartsWith("83")) + { + return StockMarket.BSE; + } + // B股 + if (code.StartsWith("900")) + { + return StockMarket.SHSE; + } + if (code.StartsWith("200")) + { + return StockMarket.SZSE; + } + } + + return StockMarket.Unknown; + } + + /// + /// 获取股票市场名称 + /// + /// 股票市场 + /// 市场名称 + public static string GetMarketName(StockMarket market) + { + return market switch + { + StockMarket.SHSE => "上海证券交易所", + StockMarket.SZSE => "深圳证券交易所", + StockMarket.BSE => "北京证券交易所", + StockMarket.HKEX => "香港交易所", + StockMarket.NYSE => "纽约证券交易所", + StockMarket.NASDAQ => "纳斯达克", + _ => "未知" + }; + } + + #endregion + + #region 类型识别 + + /// + /// 获取股票类型 + /// + /// 股票代码 + /// 股票类型 + public static StockType GetStockType(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return StockType.Unknown; + } + + string upper = code.ToUpper(); + + // 美股 + if (USStockRegex.IsMatch(upper)) + { + return StockType.USStock; + } + + // 港股 + if (HKStockRegex.IsMatch(code)) + { + return StockType.HKStock; + } + + // A股细分 + if (code.Length == 6) + { + if (STARRegex.IsMatch(code)) return StockType.STAR; + if (ChiNextRegex.IsMatch(code)) return StockType.ChiNext; + if (BSERegex.IsMatch(code)) return StockType.BSEShare; + if (code.StartsWith("60") || code.StartsWith("00")) return StockType.AShare; + if (code.StartsWith("900") || code.StartsWith("200")) return StockType.BShare; + } + + return StockType.Unknown; + } + + /// + /// 获取股票类型名称 + /// + /// 股票类型 + /// 类型名称 + public static string GetStockTypeName(StockType type) + { + return type switch + { + StockType.AShare => "A股", + StockType.BShare => "B股", + StockType.ChiNext => "创业板", + StockType.STAR => "科创板", + StockType.BSEShare => "北交所", + StockType.HKStock => "港股", + StockType.USStock => "美股", + _ => "未知" + }; + } + + #endregion + + #region 信息查询 + + /// + /// 获取股票名称 + /// + /// 股票代码 + /// 股票名称 + public static string? GetName(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + string key = code.ToUpper().PadLeft(6, '0'); + if (StockCodeMap.TryGetValue(key, out var info)) + { + return info.Name; + } + + // 尝试原始格式 + if (StockCodeMap.TryGetValue(code.ToUpper(), out info)) + { + return info.Name; + } + + return null; + } + + /// + /// 获取完整股票代码(带市场前缀) + /// + /// 股票代码 + /// 完整代码(如sh600519、sz000001、hk00700) + public static string? GetFullCode(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + StockMarket market = GetMarket(code); + return market switch + { + StockMarket.SHSE => "sh" + code, + StockMarket.SZSE => "sz" + code, + StockMarket.BSE => "bj" + code, + StockMarket.HKEX => "hk" + code.PadLeft(5, '0'), + StockMarket.NYSE => "nyse:" + code.ToUpper(), + StockMarket.NASDAQ => "nasdaq:" + code.ToUpper(), + _ => null + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化股票代码 + /// + /// 股票代码 + /// 格式化后的代码 + public static string? Normalize(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + // 去除市场前缀 + string cleaned = code.ToLower() + .Replace("sh", "").Replace("sz", "").Replace("bj", "") + .Replace("hk", "").Replace("nyse:", "").Replace("nasdaq:", ""); + + // 港股补零 + if (HKStockRegex.IsMatch(cleaned) && cleaned.Length < 5) + { + cleaned = cleaned.PadLeft(5, '0'); + } + + return IsValid(cleaned) ? cleaned.ToUpper() : null; + } + + /// + /// 股票代码脱敏:60****9 + /// + /// 股票代码 + /// 脱敏后的代码 + public static string? Mask(string? code) + { + string? normalized = Normalize(code); + if (normalized == null) + { + return null; + } + + if (normalized.Length <= 2) + { + return normalized[0] + "*"; + } + + return normalized[0] + new string('*', normalized.Length - 2) + normalized[^1]; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs b/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs new file mode 100644 index 0000000..3709510 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs @@ -0,0 +1,438 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// SWIFT银行代码工具类 + /// + public static class SwiftCodeUtil + { + #region 常量与私有字段 + + /// + /// SWIFT代码正则表达式(8位或11位) + /// + private static readonly Regex SwiftRegex = new( + @"^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 中国主要银行SWIFT代码映射 + /// + private static readonly Dictionary ChinaBankSwiftMap = new() + { + // 工商银行 + { "ICBKCNBJ", ("中国工商银行", "北京") }, + { "ICBKCNBJBJN", ("中国工商银行", "济南") }, + { "ICBKCNBJCQX", ("中国工商银行", "重庆") }, + { "ICBKCNBJSHI", ("中国工商银行", "上海") }, + { "ICBKCNBJSZN", ("中国工商银行", "深圳") }, + { "ICBKCNBJGZU", ("中国工商银行", "广州") }, + { "ICBKCNBJNJA", ("中国工商银行", "南京") }, + { "ICBKCNBJHBR", ("中国工商银行", "哈尔滨") }, + { "ICBKCNBJTJN", ("中国工商银行", "天津") }, + { "ICBKCNBJCDU", ("中国工商银行", "成都") }, + { "ICBKCNBJWUH", ("中国工商银行", "武汉") }, + { "ICBKCNBJHAN", ("中国工商银行", "杭州") }, + { "ICBKCNBJXIM", ("中国工商银行", "厦门") }, + { "ICBKCNBJDLC", ("中国工商银行", "大连") }, + { "ICBKCNBJSYN", ("中国工商银行", "沈阳") }, + { "ICBKCNBJJIX", ("中国工商银行", "吉林") }, + { "ICBKCNBJSWA", ("中国工商银行", "汕头") }, + { "ICBKCNBJZHO", ("中国工商银行", "珠海") }, + { "ICBKCNBJFZH", ("中国工商银行", "福州") }, + { "ICBKCNBJKUN", ("中国工商银行", "昆明") }, + + // 农业银行 + { "ABOCCNBJ", ("中国农业银行", "北京") }, + { "ABOCCNBJ070", ("中国农业银行", "哈尔滨") }, + { "ABOCCNBJ080", ("中国农业银行", "上海") }, + { "ABOCCNBJ100", ("中国农业银行", "广州") }, + { "ABOCCNBJ110", ("中国农业银行", "深圳") }, + { "ABOCCNBJ120", ("中国农业银行", "天津") }, + { "ABOCCNBJ130", ("中国农业银行", "重庆") }, + { "ABOCCNBJ140", ("中国农业银行", "南京") }, + { "ABOCCNBJ150", ("中国农业银行", "成都") }, + { "ABOCCNBJ160", ("中国农业银行", "武汉") }, + { "ABOCCNBJ170", ("中国农业银行", "杭州") }, + { "ABOCCNBJ180", ("中国农业银行", "济南") }, + { "ABOCCNBJ190", ("中国农业银行", "西安") }, + { "ABOCCNBJ200", ("中国农业银行", "沈阳") }, + + // 中国银行 + { "BKCHCNBJ", ("中国银行", "北京") }, + { "BKCHCNBJ300", ("中国银行", "上海") }, + { "BKCHCNBJ400", ("中国银行", "广州") }, + { "BKCHCNBJ500", ("中国银行", "深圳") }, + { "BKCHCNBJ600", ("中国银行", "天津") }, + { "BKCHCNBJ700", ("中国银行", "重庆") }, + { "BKCHCNBJ800", ("中国银行", "南京") }, + { "BKCHCNBJ900", ("中国银行", "成都") }, + { "BKCHCNBJ910", ("中国银行", "武汉") }, + { "BKCHCNBJ920", ("中国银行", "杭州") }, + { "BKCHCNBJ930", ("中国银行", "济南") }, + { "BKCHCNBJ940", ("中国银行", "西安") }, + { "BKCHCNBJ950", ("中国银行", "沈阳") }, + { "BKCHCNBJ960", ("中国银行", "大连") }, + { "BKCHCNBJ970", ("中国银行", "青岛") }, + { "BKCHCNBJ980", ("中国银行", "厦门") }, + { "BKCHCNBJ990", ("中国银行", "福州") }, + + // 建设银行 + { "PCBCCNBJ", ("中国建设银行", "北京") }, + { "PCBCCNBJBJX", ("中国建设银行", "北京") }, + { "PCBCCNBJSHX", ("中国建设银行", "上海") }, + { "PCBCCNBJGZX", ("中国建设银行", "广州") }, + { "PCBCCNBJSZX", ("中国建设银行", "深圳") }, + { "PCBCCNBJTJX", ("中国建设银行", "天津") }, + { "PCBCCNBJCQX", ("中国建设银行", "重庆") }, + { "PCBCCNBJNJX", ("中国建设银行", "南京") }, + { "PCBCCNBJCDX", ("中国建设银行", "成都") }, + { "PCBCCNBJWHX", ("中国建设银行", "武汉") }, + { "PCBCCNBJHZX", ("中国建设银行", "杭州") }, + { "PCBCCNBJJNX", ("中国建设银行", "济南") }, + { "PCBCCNBJXAX", ("中国建设银行", "西安") }, + { "PCBCCNBJSYX", ("中国建设银行", "沈阳") }, + { "PCBCCNBJDLX", ("中国建设银行", "大连") }, + { "PCBCCNBJQDX", ("中国建设银行", "青岛") }, + + // 交通银行 + { "COMMCNSh", ("交通银行", "上海") }, + { "COMMCNShKUN", ("交通银行", "昆明") }, + { "COMMCNShGZH", ("交通银行", "广州") }, + + // 招商银行 + { "CMBCCNBS", ("招商银行", "上海") }, + { "CMBCCNBS001", ("招商银行", "上海") }, + { "CMBCCNBS002", ("招商银行", "北京") }, + { "CMBCCNBS003", ("招商银行", "深圳") }, + { "CMBCCNBS004", ("招商银行", "广州") }, + + // 中信银行 + { "CIBKCNBJ", ("中信银行", "北京") }, + { "CIBKCNBJSHI", ("中信银行", "上海") }, + { "CIBKCNBJGZU", ("中信银行", "广州") }, + { "CIBKCNBJSZN", ("中信银行", "深圳") }, + + // 浦发银行 + { "SPDBCNSH", ("浦发银行", "上海") }, + { "SPDBCNSHBJG", ("浦发银行", "北京") }, + { "SPDBCNSHGXG", ("浦发银行", "广州") }, + { "SPDBCNSHSZN", ("浦发银行", "深圳") }, + + // 民生银行 + { "MSBCCNBJ", ("民生银行", "北京") }, + { "MSBCCNBJ001", ("民生银行", "上海") }, + { "MSBCCNBJ002", ("民生银行", "广州") }, + + // 光大银行 + { "EVERCNBJ", ("光大银行", "北京") }, + { "EVERCNBJ1BJ", ("光大银行", "北京") }, + { "EVERCNBJ1SH", ("光大银行", "上海") }, + + // 华夏银行 + { "HXBKCNBJ", ("华夏银行", "北京") }, + { "HXBKCNBJ070", ("华夏银行", "上海") }, + + // 兴业银行 + { "FJIBCNBA", ("兴业银行", "福州") }, + { "FJIBCNBA001", ("兴业银行", "北京") }, + { "FJIBCNBA002", ("兴业银行", "上海") }, + + // 平安银行 + { "SZDBCNBS", ("平安银行", "深圳") }, + { "SZDBCNBS001", ("平安银行", "北京") }, + { "SZDBCNBS002", ("平安银行", "上海") }, + + // 广发银行 + { "GDBKCN22", ("广发银行", "广州") }, + { "GDBKCN22001", ("广发银行", "北京") }, + { "GDBKCN22002", ("广发银行", "上海") }, + + // 邮储银行 + { "PSBCCNBJ", ("邮储银行", "北京") }, + { "PSBCCNBJ001", ("邮储银行", "上海") }, + { "PSBCCNBJ002", ("邮储银行", "广州") }, + + // 汇丰银行(中国) + { "HSBCCNSH", ("汇丰银行(中国)", "上海") }, + { "HSBCCNSH001", ("汇丰银行(中国)", "北京") }, + { "HSBCCNSH002", ("汇丰银行(中国)", "广州") }, + + // 渣打银行(中国) + { "SCBLCNSX", ("渣打银行(中国)", "上海") }, + { "SCBLCNSX001", ("渣打银行(中国)", "北京") }, + + // 花旗银行(中国) + { "CITICNSX", ("花旗银行(中国)", "上海") }, + { "CITICNSX001", ("花旗银行(中国)", "北京") } + }; + + /// + /// 国家代码与名称映射(部分) + /// + private static readonly Dictionary CountryCodeMap = new() + { + { "CN", "中国" }, { "HK", "香港" }, { "TW", "台湾" }, { "JP", "日本" }, + { "KR", "韩国" }, { "SG", "新加坡" }, { "MY", "马来西亚" }, { "TH", "泰国" }, + { "AU", "澳大利亚" }, { "NZ", "新西兰" }, { "US", "美国" }, { "CA", "加拿大" }, + { "GB", "英国" }, { "DE", "德国" }, { "FR", "法国" }, { "IT", "意大利" }, + { "ES", "西班牙" }, { "NL", "荷兰" }, { "BE", "比利时" }, { "CH", "瑞士" }, + { "AT", "奥地利" }, { "SE", "瑞典" }, { "NO", "挪威" }, { "DK", "丹麦" }, + { "FI", "芬兰" }, { "RU", "俄罗斯" }, { "BR", "巴西" }, { "MX", "墨西哥" }, + { "AR", "阿根廷" }, { "ZA", "南非" }, { "AE", "阿联酋" }, { "SA", "沙特" }, + { "IN", "印度" }, { "PK", "巴基斯坦" }, { "ID", "印度尼西亚" }, { "PH", "菲律宾" }, + { "VN", "越南" }, { "MM", "缅甸" }, { "LU", "卢森堡" }, { "IE", "爱尔兰" }, + { "PT", "葡萄牙" }, { "GR", "希腊" }, { "PL", "波兰" }, { "CZ", "捷克" }, + { "HU", "匈牙利" }, { "TR", "土耳其" }, { "IL", "以色列" }, { "EG", "埃及" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证SWIFT代码是否有效 + /// + /// SWIFT代码 + /// 是否有效 + public static bool IsValid(string? swiftCode) + { + if (string.IsNullOrWhiteSpace(swiftCode)) + { + return false; + } + + return SwiftRegex.IsMatch(swiftCode); + } + + /// + /// 验证是否为8位SWIFT代码(不含分行代码) + /// + /// SWIFT代码 + /// 是否为8位 + public static bool Is8Digit(string? swiftCode) + { + return swiftCode?.Length == 8 && IsValid(swiftCode); + } + + /// + /// 验证是否为11位SWIFT代码(含分行代码) + /// + /// SWIFT代码 + /// 是否为11位 + public static bool Is11Digit(string? swiftCode) + { + return swiftCode?.Length == 11 && IsValid(swiftCode); + } + + #endregion + + #region 信息提取 + + /// + /// 获取银行代码(前4位) + /// + /// SWIFT代码 + /// 银行代码 + public static string? GetBankCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(0, 4).ToUpper(); + } + + /// + /// 获取国家代码(第5-6位) + /// + /// SWIFT代码 + /// 国家代码 + public static string? GetCountryCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(4, 2).ToUpper(); + } + + /// + /// 获取国家名称 + /// + /// SWIFT代码 + /// 国家名称 + public static string? GetCountryName(string? swiftCode) + { + string? countryCode = GetCountryCode(swiftCode); + if (countryCode == null) + { + return null; + } + + return CountryCodeMap.TryGetValue(countryCode, out string? name) ? name : null; + } + + /// + /// 获取位置代码(第7-8位) + /// + /// SWIFT代码 + /// 位置代码 + public static string? GetLocationCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(6, 2).ToUpper(); + } + + /// + /// 获取分行代码(第9-11位,11位代码才有) + /// + /// SWIFT代码 + /// 分行代码 + public static string? GetBranchCode(string? swiftCode) + { + if (!Is11Digit(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(8, 3).ToUpper(); + } + + /// + /// 判断是否为总行代码(第7-8位为XX或位置代码首位为0) + /// + /// SWIFT代码 + /// 是否为总行 + public static bool IsHeadOffice(string? swiftCode) + { + string? locationCode = GetLocationCode(swiftCode); + if (locationCode == null) + { + return false; + } + + // 位置代码为"XX"或首位为0表示总行 + return locationCode == "XX" || locationCode[0] == '0'; + } + + /// + /// 获取银行信息(仅限中国主要银行) + /// + /// SWIFT代码 + /// 银行和城市信息 + public static (string Bank, string City)? GetBankInfo(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + string upper = swiftCode!.ToUpper(); + + // 先尝试完整匹配 + if (ChinaBankSwiftMap.TryGetValue(upper, out var info)) + { + return info; + } + + // 再尝试8位匹配 + string code8 = upper.Substring(0, 8); + if (ChinaBankSwiftMap.TryGetValue(code8, out info)) + { + return info; + } + + return null; + } + + /// + /// 获取银行名称 + /// + /// SWIFT代码 + /// 银行名称 + public static string? GetBankName(string? swiftCode) + { + return GetBankInfo(swiftCode)?.Bank; + } + + /// + /// 获取城市名称 + /// + /// SWIFT代码 + /// 城市名称 + public static string? GetCityName(string? swiftCode) + { + return GetBankInfo(swiftCode)?.City; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化SWIFT代码(转大写) + /// + /// SWIFT代码 + /// 格式化后的SWIFT代码 + public static string? Normalize(string? swiftCode) + { + if (string.IsNullOrWhiteSpace(swiftCode)) + { + return null; + } + + string upper = swiftCode.ToUpper().Trim(); + return IsValid(upper) ? upper : null; + } + + /// + /// SWIFT代码脱敏:ICBK****BJ + /// + /// SWIFT代码 + /// 脱敏后的SWIFT代码 + public static string? Mask(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + string upper = swiftCode!.ToUpper(); + if (upper.Length == 8) + { + return upper.Substring(0, 4) + "****"; + } + else + { + return upper.Substring(0, 4) + "*******"; + } + } + + /// + /// 转换为8位SWIFT代码(去除分行代码) + /// + /// SWIFT代码 + /// 8位SWIFT代码 + public static string? To8Digit(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(0, 8).ToUpper(); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs b/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs new file mode 100644 index 0000000..4ffa424 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs @@ -0,0 +1,557 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 税号类型枚举 + /// + public enum TaxNumberType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 统一社会信用代码(18位) + /// + CreditCode = 1, + + /// + /// 旧税号(15位) + /// + OldTaxCode = 2, + + /// + /// 税务登记号(20位) + /// + TaxRegistration = 3 + } + + /// + /// 企业税号工具类 + /// + public static class TaxNumberUtil + { + #region 常量与私有字段 + + /// + /// 统一社会信用代码字符集(31个字符) + /// + private const string BaseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + + /// + /// 统一社会信用代码权重 + /// + private static readonly int[] Weights = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 }; + + /// + /// 18位统一社会信用代码正则表达式 + /// + private static readonly Regex CreditCodeRegex = new Regex( + @"^[0-9A-HJ-NPQRTUWXY]{2}[0-9]{6}[0-9A-HJ-NPQRTUWXY]{10}$", + RegexOptions.Compiled); + + /// + /// 15位旧税号正则表达式(6位区域码 + 9位组织机构代码) + /// + private static readonly Regex OldTaxCodeRegex = new Regex( + @"^[0-9]{15}$", + RegexOptions.Compiled); + + /// + /// 20位税务登记号正则表达式 + /// + private static readonly Regex TaxRegistrationRegex = new Regex( + @"^[0-9]{20}$", + RegexOptions.Compiled); + + /// + /// 登记管理部门代码映射 + /// + private static readonly Dictionary DepartmentMap = new Dictionary + { + { "11", "机构编制" }, { "12", "外交" }, { "13", "教育" }, { "14", "公安" }, + { "15", "民政" }, { "16", "司法" }, { "17", "交通运输" }, { "18", "文化和旅游" }, + { "19", "市场监管" }, { "21", "农业" }, { "22", "林业和草原" }, { "23", "卫生健康" }, + { "24", "中医药" }, { "25", "退役军人" }, { "26", "应急管理" }, { "27", "国有资产" }, + { "28", "海关" }, { "29", "税务" }, { "31", "人民银行" }, { "32", "外汇" }, + { "33", "知识产权" }, { "34", "粮食和储备" }, { "35", "能源" }, { "36", "国防科工" }, + { "37", "烟草" }, { "41", "中央军委" }, { "51", "全国总工会" }, { "52", "全国妇联" }, + { "53", "全国工商联" }, { "54", "全国青联" }, { "55", "中国残联" }, + { "91", "工商" }, { "92", "中央及地方编办" }, { "93", "民政" }, { "99", "其他" } + }; + + /// + /// 机构类型代码映射(与登记管理部门组合使用) + /// + private static readonly Dictionary OrganizationTypeMap = new Dictionary + { + { '1', "企业" }, { '2', "个体工商户" }, { '3', "农民专业合作社" }, + { '4', "机关" }, { '5', "事业单位" }, { '6', "社会团体" }, + { '7', "民办非企业单位" }, { '8', "基金会" }, { '9', "其他" } + }; + + /// + /// 行业代码映射(GB/T 4754-2017 国民经济行业分类,部分常用) + /// + private static readonly Dictionary IndustryCodeMap = new Dictionary + { + { "01", "农业" }, { "02", "林业" }, { "03", "畜牧业" }, { "04", "渔业" }, + { "06", "煤炭开采和洗选业" }, { "07", "石油和天然气开采业" }, + { "08", "黑色金属矿采选业" }, { "09", "有色金属矿采选业" }, + { "10", "非金属矿采选业" }, { "13", "农副食品加工业" }, + { "14", "食品制造业" }, { "15", "酒、饮料和精制茶制造业" }, + { "17", "纺织业" }, { "18", "纺织服装、服饰业" }, + { "19", "皮革、毛皮、羽毛及其制品和制鞋业" }, { "20", "木材加工和木、竹、藤、棕、草制品业" }, + { "21", "家具制造业" }, { "22", "造纸和纸制品业" }, + { "23", "印刷和记录媒介复制业" }, { "24", "文教、工美、体育和娱乐用品制造业" }, + { "25", "石油、煤炭及其他燃料加工业" }, { "26", "化学原料和化学制品制造业" }, + { "27", "医药制造业" }, { "28", "化学纤维制造业" }, + { "29", "橡胶和塑料制品业" }, { "30", "非金属矿物制品业" }, + { "31", "黑色金属冶炼和压延加工业" }, { "32", "有色金属冶炼和压延加工业" }, + { "33", "金属制品业" }, { "34", "通用设备制造业" }, + { "35", "专用设备制造业" }, { "36", "汽车制造业" }, + { "37", "铁路、船舶、航空航天和其他运输设备制造业" }, + { "38", "电气机械和器材制造业" }, { "39", "计算机、通信和其他电子设备制造业" }, + { "40", "仪器仪表制造业" }, { "41", "其他制造业" }, + { "42", "废弃资源综合利用业" }, { "43", "金属制品、机械和设备修理业" }, + { "44", "电力、热力生产和供应业" }, { "45", "燃气生产和供应业" }, + { "46", "水的生产和供应业" }, { "47", "房屋建筑业" }, + { "48", "土木工程建筑业" }, { "49", "建筑安装业" }, + { "50", "建筑装饰、装修和其他建筑业" }, { "51", "批发业" }, + { "52", "零售业" }, { "53", "铁路运输业" }, + { "54", "道路运输业" }, { "55", "水上运输业" }, + { "56", "航空运输业" }, { "57", "管道运输业" }, + { "58", "多式联运和运输代理业" }, { "59", "装卸搬运和仓储业" }, + { "60", "邮政业" }, { "61", "住宿业" }, + { "62", "餐饮业" }, { "63", "电信、广播电视和卫星传输服务" }, + { "64", "互联网和相关服务" }, { "65", "软件和信息技术服务业" }, + { "66", "货币金融服务" }, { "67", "资本市场服务" }, + { "68", "保险业" }, { "69", "其他金融业" }, + { "70", "房地产业" }, { "71", "租赁业" }, + { "72", "商务服务业" }, { "73", "研究和试验发展" }, + { "74", "专业技术服务业" }, { "75", "科技推广和应用服务业" }, + { "76", "水利管理业" }, { "77", "生态保护和环境治理业" }, + { "78", "公共设施管理业" }, { "79", "居民服务业" }, + { "80", "机动车、电子产品和日用产品修理业" }, { "81", "其他服务业" }, + { "82", "教育" }, { "83", "卫生" }, + { "84", "社会工作" }, { "85", "新闻和出版业" }, + { "86", "广播、电视、电影和录音制作业" }, { "87", "文化艺术业" }, + { "88", "体育" }, { "89", "娱乐业" }, + { "90", "中国共产党机关" }, { "91", "国家机构" }, + { "92", "人民政协、民主党派" }, { "93", "社会保障" }, + { "94", "群众团体、社会团体和其他成员组织" }, { "95", "基层群众自治组织" }, + { "96", "国际组织" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证税号是否有效(支持15/18/20位) + /// + /// 税号 + /// 是否有效 + public static bool IsValid(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return false; + } + + return IsValid18(taxNumber) || IsValid15(taxNumber) || IsValid20(taxNumber); + } + + /// + /// 验证18位统一社会信用代码是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid18(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 18) + { + return false; + } + + string normalized = taxNumber.ToUpper(); + + // 验证格式 + if (!CreditCodeRegex.IsMatch(normalized)) + { + return false; + } + + // 验证校验码 + char? expectedCheckCode = CalculateCheckCode(normalized.Substring(0, 17)); + return expectedCheckCode.HasValue && expectedCheckCode.Value == normalized[17]; + } + + /// + /// 验证15位旧税号是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid15(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 15) + { + return false; + } + + return OldTaxCodeRegex.IsMatch(taxNumber); + } + + /// + /// 验证20位税务登记号是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid20(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 20) + { + return false; + } + + return TaxRegistrationRegex.IsMatch(taxNumber); + } + + /// + /// 判断是否为统一社会信用代码(18位) + /// + /// 税号 + /// 是否为统一社会信用代码 + public static bool IsCreditCode(string? taxNumber) + { + return IsValid18(taxNumber); + } + + /// + /// 计算统一社会信用代码校验码 + /// + /// 不含校验码的17位代码 + /// 校验码,计算失败返回null + public static char? CalculateCheckCode(string? codeWithoutCheck) + { + if (string.IsNullOrWhiteSpace(codeWithoutCheck) || codeWithoutCheck.Length != 17) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 17; i++) + { + int value = BaseCode.IndexOf(char.ToUpper(codeWithoutCheck[i])); + if (value < 0) + { + return null; + } + sum += value * Weights[i]; + } + + int checkValue = 31 - (sum % 31); + if (checkValue == 31) + { + checkValue = 0; + } + + return BaseCode[checkValue]; + } + + #endregion + + #region 类型识别 + + /// + /// 获取税号类型 + /// + /// 税号 + /// 税号类型 + public static TaxNumberType GetTaxNumberType(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return TaxNumberType.Unknown; + } + + if (IsValid18(taxNumber)) + { + return TaxNumberType.CreditCode; + } + + if (IsValid15(taxNumber)) + { + return TaxNumberType.OldTaxCode; + } + + if (IsValid20(taxNumber)) + { + return TaxNumberType.TaxRegistration; + } + + return TaxNumberType.Unknown; + } + + #endregion + + #region 信息提取 + + /// + /// 获取登记管理部门(仅18位统一社会信用代码) + /// + /// 税号 + /// 登记管理部门名称 + public static string? GetDepartment(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + string normalized = taxNumber!.ToUpper(); + string deptCode = normalized.Substring(0, 2); + + return DepartmentMap.TryGetValue(deptCode, out string? dept) ? dept : null; + } + + /// + /// 获取机构类型(仅18位统一社会信用代码) + /// + /// 税号 + /// 机构类型名称 + public static string? GetOrganizationType(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + string normalized = taxNumber!.ToUpper(); + char typeCode = normalized[2]; + + return OrganizationTypeMap.TryGetValue(typeCode, out string? type) ? type : null; + } + + /// + /// 获取行政区划代码(仅18位统一社会信用代码) + /// + /// 税号 + /// 行政区划代码 + public static string? GetAreaCode(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(3, 6); + } + + /// + /// 获取行业代码(仅18位统一社会信用代码) + /// + /// 税号 + /// 行业代码 + public static string? GetIndustryCode(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(9, 2); + } + + /// + /// 获取行业名称(仅18位统一社会信用代码) + /// + /// 税号 + /// 行业名称 + public static string? GetIndustryName(string? taxNumber) + { + string? code = GetIndustryCode(taxNumber); + if (code == null) + { + return null; + } + + return IndustryCodeMap.TryGetValue(code, out string? name) ? name : null; + } + + /// + /// 获取主体标识码(仅18位统一社会信用代码) + /// + /// 税号 + /// 主体标识码 + public static string? GetSubjectIdentifier(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(11, 6); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化税号(转大写,去除特殊字符) + /// + /// 税号 + /// 格式化后的税号 + public static string? Normalize(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + return taxNumber.ToUpper().Trim(); + } + + /// + /// 税号脱敏:911010****001Q + /// + /// 税号 + /// 脱敏后的税号 + public static string? Mask(string? taxNumber) + { + string? normalized = Normalize(taxNumber); + if (normalized == null) + { + return null; + } + + if (normalized.Length == 18) + { + // 保留前5位 + 后3位 + return normalized.Substring(0, 5) + "**********" + normalized.Substring(15); + } + + if (normalized.Length == 15) + { + // 保留前4位 + 后3位 + return normalized.Substring(0, 4) + "********" + normalized.Substring(12); + } + + if (normalized.Length == 20) + { + // 保留前5位 + 后3位 + return normalized.Substring(0, 5) + "************" + normalized.Substring(17); + } + + return null; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机统一社会信用代码(仅供测试使用) + /// + /// 登记管理部门代码(可选,默认91-工商) + /// 机构类型代码(可选,默认1-企业) + /// 行政区划代码(可选,默认110101-北京市东城区) + /// 18位统一社会信用代码 + public static string GenerateRandom( + string? departmentCode = null, + char? organizationType = null, + string? areaCode = null) + { + // 登记管理部门代码(2位) + string deptCode = departmentCode ?? "91"; + + // 机构类型(1位) + char orgType = organizationType ?? '1'; + + // 行政区划代码(6位) + string area = areaCode ?? "110101"; + + // 行业代码(2位) + string[] industries = { "51", "52", "63", "64", "65", "70", "72" }; + string industry = MathCategory.RandomUtil.GetRandomElement(industries); + + // 主体标识码(6位) + string subject = GenerateRandomCode(6); + + // 前17位 + string code17 = deptCode + orgType + area + industry + subject; + + // 计算校验码 + char? checkCode = CalculateCheckCode(code17); + if (!checkCode.HasValue) + { + throw new InvalidOperationException("Failed to calculate check code"); + } + + return code17 + checkCode.Value; + } + + /// + /// 将15位旧税号转换为18位统一社会信用代码 + /// 注意:这是一个近似转换,实际转换需要根据具体情况补充信息 + /// + /// 15位旧税号 + /// 机构类型代码(默认1-企业) + /// 18位统一社会信用代码,转换失败返回null + public static string? Convert15To18(string? taxNumber15, char organizationType = '1') + { + if (!IsValid15(taxNumber15)) + { + return null; + } + + // 15位旧税号结构:6位区域码 + 9位组织机构代码 + // 18位统一社会信用代码结构: + // - 登记管理部门(2位):默认91(工商) + // - 机构类型(1位) + // - 行政区划(6位):取旧税号前6位 + // - 主体标识码(9位):取旧税号后9位 + // - 校验码(1位) + + string areaCode = taxNumber15!.Substring(0, 6); + string subjectCode = taxNumber15.Substring(6, 9); + + // 前17位:91 + 机构类型 + 区域码 + 主体标识码 + string code17 = "91" + organizationType + areaCode + subjectCode; + + // 计算校验码 + char? checkCode = CalculateCheckCode(code17); + if (!checkCode.HasValue) + { + return null; + } + + return code17 + checkCode.Value; + } + + #endregion + + #region 私有方法 + + /// + /// 生成随机代码(使用BaseCode字符集) + /// + private static string GenerateRandomCode(int length) + { + string result = ""; + for (int i = 0; i < length; i++) + { + result += BaseCode[MathCategory.RandomUtil.RandomInt(0, BaseCode.Length)]; + } + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs b/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs new file mode 100644 index 0000000..44a17bd --- /dev/null +++ b/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs @@ -0,0 +1,357 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 台湾身份证工具类 + /// + public static class TwIdCardUtil + { + #region 常量与私有字段 + + /// + /// 台湾身份证正则表达式 + /// 格式:1个英文字母(县市代码)+ 1位数字(性别)+ 7位数字 + 1位校验码 + /// 例如:A123456789 + /// + private static readonly Regex TwIdCardRegex = new( + @"^[A-Z]\d{9}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 首字母对应数值(台湾身份证特殊编码) + /// + private static readonly Dictionary LetterValues = new() + { + { 'A', (10, 0) }, { 'B', (11, 1) }, { 'C', (12, 2) }, { 'D', (13, 3) }, + { 'E', (14, 4) }, { 'F', (15, 5) }, { 'G', (16, 6) }, { 'H', (17, 7) }, + { 'I', (34, 4) }, { 'J', (18, 8) }, { 'K', (19, 9) }, { 'L', (20, 0) }, + { 'M', (21, 1) }, { 'N', (22, 2) }, { 'O', (35, 5) }, { 'P', (23, 3) }, + { 'Q', (24, 4) }, { 'R', (25, 5) }, { 'S', (26, 6) }, { 'T', (27, 7) }, + { 'U', (28, 8) }, { 'V', (29, 9) }, { 'W', (32, 2) }, { 'X', (30, 0) }, + { 'Y', (31, 1) }, { 'Z', (33, 3) } + }; + + /// + /// 县市代码与名称映射 + /// + private static readonly Dictionary CountyMap = new() + { + { 'A', "台北市" }, { 'B', "台中市" }, { 'C', "基隆市" }, { 'D', "台南市" }, + { 'E', "高雄市" }, { 'F', "台北县" }, { 'G', "宜兰县" }, { 'H', "桃园县" }, + { 'I', "嘉义市" }, { 'J', "新竹县" }, { 'K', "苗栗县" }, { 'L', "台中县" }, + { 'M', "南投县" }, { 'N', "彰化县" }, { 'O', "新竹市" }, { 'P', "云林县" }, + { 'Q', "嘉义县" }, { 'R', "台南县" }, { 'S', "高雄县" }, { 'T', "屏东县" }, + { 'U', "花莲县" }, { 'V', "台东县" }, { 'W', "金门县" }, { 'X', "澎湖县" }, + { 'Y', "阳明山" }, { 'Z', "连江县" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证台湾身份证是否有效 + /// + /// 台湾身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 检查格式 + if (!TwIdCardRegex.IsMatch(cleaned)) + { + return false; + } + + // 检查字母是否有效 + if (!LetterValues.ContainsKey(cleaned[0])) + { + return false; + } + + // 验证校验码 + return ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 台湾身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return TwIdCardRegex.IsMatch(cleaned) && LetterValues.ContainsKey(cleaned[0]); + } + + /// + /// 验证校验码 + /// + private static bool ValidateCheckDigit(string idCard) + { + if (idCard.Length != 10) + { + return false; + } + + char letter = char.ToUpper(idCard[0]); + if (!LetterValues.TryGetValue(letter, out var values)) + { + return false; + } + + // 计算加权和 + int sum = values.Value1; + + // 第2-9位权重为9到1 + int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 }; + for (int i = 0; i < 8; i++) + { + sum += (idCard[i + 1] - '0') * weights[i]; + } + + // 计算校验码 + int remainder = sum % 10; + int expectedCheck = remainder == 0 ? 0 : 10 - remainder; + + int actualCheck = idCard[9] - '0'; + + return expectedCheck == actualCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取县市名称 + /// + /// 台湾身份证号 + /// 县市名称 + public static string? GetCounty(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + char letter = char.ToUpper(idCard![0]); + return CountyMap.TryGetValue(letter, out string? county) ? county : null; + } + + /// + /// 获取县市代码(首字母) + /// + /// 台湾身份证号 + /// 县市代码 + public static char? GetCountyCode(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return char.ToUpper(idCard![0]); + } + + /// + /// 获取性别 + /// + /// 台湾身份证号 + /// 性别(1男2女) + public static int? GetGender(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + int genderDigit = idCard![1] - '0'; + // 1为男性,2为女性 + return genderDigit == 1 ? 1 : (genderDigit == 2 ? 2 : null); + } + + /// + /// 获取性别字符串 + /// + /// 台湾身份证号 + /// 性别 + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取数字部分(后9位) + /// + /// 台湾身份证号 + /// 数字部分 + public static string? GetDigitPart(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.Substring(1); + } + + /// + /// 获取校验码(最后一位) + /// + /// 台湾身份证号 + /// 校验码 + public static int? GetCheckDigit(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard![9] - '0'; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化台湾身份证(统一大写) + /// + /// 台湾身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 台湾身份证脱敏:A123****89 + /// + /// 台湾身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + // 保留前4位和后2位 + return cleaned.Substring(0, 4) + "****" + cleaned.Substring(8); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机台湾身份证号(仅供测试使用) + /// + /// 县市代码(可选,默认随机) + /// 性别(1男2女,可选) + /// 台湾身份证号 + public static string GenerateRandom(char? countyCode = null, int? gender = null) + { + const string letters = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; + const string digits = "0123456789"; + + // 县市代码 + char letter; + if (countyCode.HasValue && LetterValues.ContainsKey(char.ToUpper(countyCode.Value))) + { + letter = char.ToUpper(countyCode.Value); + } + else + { + letter = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()); + } + + // 性别(第2位) + int genderDigit; + if (gender == 1) + { + genderDigit = 1; + } + else if (gender == 2) + { + genderDigit = 2; + } + else + { + genderDigit = MathCategory.RandomUtil.RandomInt(1, 3); + } + + // 第3-9位随机数字 + string middleDigits = ""; + for (int i = 0; i < 7; i++) + { + middleDigits += MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray()); + } + + // 计算校验码 + string tempId = letter + genderDigit.ToString() + middleDigits + "0"; + char? checkDigit = CalculateCheckDigit(tempId); + + return $"{letter}{genderDigit}{middleDigits}{checkDigit ?? '0'}"; + } + + /// + /// 计算校验码 + /// + private static char CalculateCheckDigit(string idCard) + { + if (idCard.Length < 10) + { + return '0'; + } + + char letter = char.ToUpper(idCard[0]); + if (!LetterValues.TryGetValue(letter, out var values)) + { + return '0'; + } + + int sum = values.Value1; + int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 }; + + for (int i = 0; i < 8; i++) + { + sum += (idCard[i + 1] - '0') * weights[i]; + } + + int remainder = sum % 10; + int checkValue = remainder == 0 ? 0 : 10 - remainder; + + return (char)('0' + checkValue); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/VINUtil.cs b/EasyTool.Core/BusinessCategory/VINUtil.cs new file mode 100644 index 0000000..bfec174 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/VINUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// VIN(车辆识别代号)工具类 + /// + public static class VINUtil + { + #region 常量与私有字段 + + /// + /// VIN正则表达式(17位,不含I、O、Q) + /// + private static readonly Regex VINRegex = new( + @"^[A-HJ-NPR-Z0-9]{17}$", + RegexOptions.Compiled); + + /// + /// VIN字符值映射表(不含I、O、Q) + /// + private static readonly Dictionary CharValueMap = new() + { + {'A', 1}, {'B', 2}, {'C', 3}, {'D', 4}, {'E', 5}, {'F', 6}, {'G', 7}, {'H', 8}, + {'J', 1}, {'K', 2}, {'L', 3}, {'M', 4}, {'N', 5}, {'P', 7}, {'R', 9}, + {'S', 2}, {'T', 3}, {'U', 4}, {'V', 5}, {'W', 6}, {'X', 7}, {'Y', 8}, {'Z', 9}, + {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9} + }; + + /// + /// VIN位置权重 + /// + private static readonly int[] Weights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 }; + + /// + /// WMI(世界制造商识别码)映射(部分) + /// + private static readonly (string Code, string Manufacturer)[] WmiMap = + { + // 中国 + ("LSV", "上海大众"), ("LSJ", "上海通用"), ("LSG", "上海通用五菱"), + ("LDC", "神龙富康"), ("LEN", "北京吉普"), ("LHB", "华晨宝马"), + ("LBV", "宝马"), ("LJC", "捷豹路虎"), ("LTV", "天津丰田"), + ("LFV", "一汽大众"), ("LFP", "一汽轿车"), ("LFW", "一汽夏利"), + ("LKG", "长安铃木"), ("LKL", "长安福特"), ("LLV", "长安汽车"), + ("LVF", "东风日产"), ("LUG", "东风本田"), ("LVH", "东风本田"), + ("LZW", "柳州五菱"), ("LJD", "江淮汽车"), ("LKY", "奇瑞汽车"), + ("LVS", "长安马自达"), ("LZY", "众泰汽车"), ("LVSH", "福特中国"), + + // 德国 + ("WBA", "宝马"), ("WBS", "宝马M"), ("WBW", "宝马"), + ("WAU", "奥迪"), ("WA1", "奥迪SUV"), + ("WDB", "奔驰"), ("WDC", "奔驰"), ("WDD", "奔驰"), + ("WVW", "大众"), ("WV2", "大众商用车"), ("WVG", "大众SUV"), + ("WPO", "保时捷"), + + // 日本 + ("JTD", "丰田"), ("JTM", "丰田"), ("JTK", "丰田"), + ("JHM", "本田"), ("JHG", "本田"), ("JHL", "本田"), + ("JN1", "日产"), ("JN8", "日产"), ("JN3", "日产"), + ("JM1", "马自达"), ("JMZ", "马自达"), + ("JS1", "铃木"), ("JS2", "铃木"), ("JS3", "铃木"), + ("KL1", "大宇"), ("KL2", "大宇"), + + // 美国 + ("1G1", "雪佛兰"), ("1G2", "庞蒂亚克"), ("1G3", "奥兹莫比尔"), + ("1G4", "别克"), ("1G6", "凯迪拉克"), ("1G8", "萨博"), + ("1GM", "通用"), ("1HG", "本田美国"), ("1J4", "Jeep"), + ("1F1", "福特"), ("1F2", "福特"), ("1FA", "福特"), ("1FB", "福特"), + ("1C3", "克莱斯勒"), ("1C4", "克莱斯勒"), ("1C6", "克莱斯勒"), + ("2G1", "雪佛兰加拿大"), ("2G2", "庞蒂亚克加拿大"), + ("2HM", "现代加拿大"), ("2HG", "本田加拿大"), + + // 韩国 + ("KMH", "现代"), ("KMB", "现代"), ("KNA", "起亚"), ("KND", "起亚"), + + // 英国 + ("SAJ", "捷豹"), ("SAL", "路虎"), ("SCC", "迈凯伦"), + + // 意大利 + ("ZAM", "玛莎拉蒂"), ("ZAR", "阿尔法罗密欧"), + ("ZDF", "法拉利"), ("ZFF", "法拉利"), + ("ZHW", "兰博基尼"), + + // 法国 + ("VF1", "雷诺"), ("VF3", "标致"), ("VF7", "雪铁龙"), + + // 瑞典 + ("YV1", "沃尔沃"), ("YV4", "沃尔沃"), ("YV2", "沃尔沃货车") + }; + + /// + /// VDS车辆特征码映射(简化版) + /// + private static readonly Dictionary VehicleTypeMap = new() + { + {"A", "轿车"}, {"B", "客车"}, {"C", "跑车"}, {"S", "SUV/跨界车"}, + {"T", "卡车"}, {"V", "MPV/厢式车"}, {"W", "旅行车"}, {"X", "特种车"} + }; + + /// + /// 年份代码映射 + /// + private static readonly Dictionary YearCodeMap = new() + { + {'A', 2010}, {'B', 2011}, {'C', 2012}, {'D', 2013}, {'E', 2014}, + {'F', 2015}, {'G', 2016}, {'H', 2017}, {'J', 2018}, {'K', 2019}, + {'L', 2020}, {'M', 2021}, {'N', 2022}, {'P', 2023}, {'R', 2024}, + {'S', 2025}, {'T', 2026}, {'V', 2027}, {'W', 2028}, {'X', 2029}, + {'Y', 2030}, + {'1', 2001}, {'2', 2002}, {'3', 2003}, {'4', 2004}, {'5', 2005}, + {'6', 2006}, {'7', 2007}, {'8', 2008}, {'9', 2009} + }; + + #endregion + + #region 验证方法 + + /// + /// 验证VIN是否有效(格式+校验位) + /// + /// VIN码 + /// 是否有效 + public static bool IsValid(string? vin) + { + if (!IsValidFormat(vin)) + { + return false; + } + + return ValidateCheckDigit(vin!); + } + + /// + /// 仅验证VIN格式(不校验) + /// + /// VIN码 + /// 格式是否正确 + public static bool IsValidFormat(string? vin) + { + if (string.IsNullOrWhiteSpace(vin)) + { + return false; + } + + return VINRegex.IsMatch(vin.ToUpper()); + } + + /// + /// 验证VIN校验位 + /// + /// VIN码 + /// 校验位是否正确 + public static bool ValidateCheckDigit(string? vin) + { + if (!IsValidFormat(vin)) + { + return false; + } + + string upper = vin!.ToUpper(); + int sum = 0; + + for (int i = 0; i < 17; i++) + { + if (!CharValueMap.TryGetValue(upper[i], out int value)) + { + return false; + } + sum += value * Weights[i]; + } + + char expectedCheck = (sum % 11) switch + { + 10 => 'X', + _ => (char)('0' + (sum % 11)) + }; + + return upper[8] == expectedCheck; + } + + /// + /// 计算VIN校验位 + /// + /// 不含校验位的16位VIN + /// 校验位(0-9或X),计算失败返回null + public static char? CalculateCheckDigit(string? vin16) + { + if (string.IsNullOrWhiteSpace(vin16) || vin16.Length != 16) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 16; i++) + { + char c = char.ToUpper(vin16[i]); + if (!CharValueMap.TryGetValue(c, out int value)) + { + return null; + } + // 权重需要跳过第9位(校验位位置) + int weight = i >= 8 ? Weights[i + 1] : Weights[i]; + sum += value * weight; + } + + return (sum % 11) switch + { + 10 => 'X', + _ => (char)('0' + (sum % 11)) + }; + } + + #endregion + + #region 信息提取 + + /// + /// 获取WMI(世界制造商识别码,前3位) + /// + /// VIN码 + /// WMI码 + public static string? GetWMI(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(0, 3).ToUpper(); + } + + /// + /// 获取制造商 + /// + /// VIN码 + /// 制造商名称 + public static string? GetManufacturer(string? vin) + { + string? wmi = GetWMI(vin); + if (wmi == null) + { + return null; + } + + foreach (var mapping in WmiMap) + { + if (wmi.StartsWith(mapping.Code)) + { + return mapping.Manufacturer; + } + } + + return null; + } + + /// + /// 获取生产地区(根据WMI判断) + /// + /// VIN码 + /// 生产地区 + public static string? GetRegion(string? vin) + { + string? wmi = GetWMI(vin); + if (wmi == null) + { + return null; + } + + char first = wmi[0]; + return first switch + { + 'L' => "中国", + 'W' => "德国", + 'J' => "日本", + 'K' => "韩国", + '1' or '2' or '3' or '4' or '5' => "美国/加拿大", + 'S' => "英国", + 'Z' => "意大利", + 'V' => "法国", + 'Y' => "瑞典", + '6' or '7' => "大洋洲", + '8' or '9' => "南美洲", + _ => null + }; + } + + /// + /// 获取VDS(车辆特征码,第4-9位) + /// + /// VIN码 + /// VDS码 + public static string? GetVDS(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(3, 6).ToUpper(); + } + + /// + /// 获取VIS(车辆指示码,第10-17位) + /// + /// VIN码 + /// VIS码 + public static string? GetVIS(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(9, 8).ToUpper(); + } + + /// + /// 获取车型年份 + /// + /// VIN码 + /// 车型年份 + public static int? GetModelYear(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + char yearCode = char.ToUpper(vin![9]); + return YearCodeMap.TryGetValue(yearCode, out int year) ? year : null; + } + + /// + /// 获取装配厂代码(第11位) + /// + /// VIN码 + /// 装配厂代码 + public static char? GetPlantCode(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return char.ToUpper(vin![10]); + } + + /// + /// 获取生产序列号(第12-17位) + /// + /// VIN码 + /// 序列号 + public static string? GetSequenceNumber(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(11, 6).ToUpper(); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化VIN(转大写) + /// + /// VIN码 + /// 格式化后的VIN + public static string? Normalize(string? vin) + { + if (string.IsNullOrWhiteSpace(vin)) + { + return null; + } + + string upper = vin.ToUpper().Trim(); + return upper.Length == 17 && VINRegex.IsMatch(upper) ? upper : null; + } + + /// + /// VIN脱敏:LSV***********X + /// + /// VIN码 + /// 脱敏后的VIN + public static string? Mask(string? vin) + { + string? normalized = Normalize(vin); + if (normalized == null) + { + return null; + } + + return normalized.Substring(0, 3) + "*********" + normalized.Substring(14, 3); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机VIN(仅供测试使用) + /// + /// WMI码(可选,默认LSV-上海大众) + /// 车型年份(可选,默认2023) + /// 17位VIN + public static string GenerateRandom(string? wmi = null, int? modelYear = null) + { + // WMI(3位) + string wmiCode = wmi ?? "LSV"; + + // VDS(5位随机) + const string vdsChars = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"; + string vds = ""; + for (int i = 0; i < 5; i++) + { + vds += MathCategory.RandomUtil.GetRandomElement(vdsChars.ToCharArray()); + } + + // 年份代码 + int year = modelYear ?? 2023; + char yearCode = GetYearCode(year); + + // 装配厂代码(1位) + char plantCode = MathCategory.RandomUtil.GetRandomElement(vdsChars.ToCharArray()); + + // 序列号(5位) + string sequence = MathCategory.RandomUtil.RandomDigitString(5); + + // 组合16位,计算校验位 + string vin16 = wmiCode + vds + yearCode + plantCode + sequence; + char? checkDigit = CalculateCheckDigit(vin16); + + return vin16.Substring(0, 8) + (checkDigit ?? '0') + vin16.Substring(8); + } + + #endregion + + #region 私有方法 + + /// + /// 根据年份获取年份代码 + /// + private static char GetYearCode(int year) + { + foreach (var kvp in YearCodeMap) + { + if (kvp.Value == year) + { + return kvp.Key; + } + } + return 'P'; // 默认2023 + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/WeChatUtil.cs b/EasyTool.Core/BusinessCategory/WeChatUtil.cs new file mode 100644 index 0000000..6a9b03b --- /dev/null +++ b/EasyTool.Core/BusinessCategory/WeChatUtil.cs @@ -0,0 +1,222 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 微信号工具类 + /// + public static class WeChatUtil + { + #region 常量与私有字段 + + /// + /// 微信号正则表达式(6-20位,字母开头,字母数字下划线减号) + /// + private static readonly Regex WeChatIdRegex = new( + @"^[a-zA-Z][a-zA-Z0-9_-]{5,19}$", + RegexOptions.Compiled); + + /// + /// 微信原始ID正则表达式(gh_开头) + /// + private static readonly Regex WeChatOriginalIdRegex = new( + @"^gh_[a-zA-Z0-9]{11,12}$", + RegexOptions.Compiled); + + /// + /// 微信开放平台UnionID正则表达式 + /// + private static readonly Regex WeChatUnionIdRegex = new( + @"^[a-zA-Z0-9_-]{28,32}$", + RegexOptions.Compiled); + + /// + /// 微信小程序AppID正则表达式 + /// + private static readonly Regex WeChatAppIdRegex = new( + @"^wx[a-f0-9]{16}$", + RegexOptions.Compiled); + + #endregion + + #region 验证方法 + + /// + /// 验证微信号是否有效 + /// + /// 微信号 + /// 是否有效 + public static bool IsValid(string? wechatId) + { + if (string.IsNullOrWhiteSpace(wechatId)) + { + return false; + } + + return WeChatIdRegex.IsMatch(wechatId); + } + + /// + /// 验证微信原始ID是否有效(公众号/小程序) + /// + /// 原始ID(gh_开头) + /// 是否有效 + public static bool IsValidOriginalId(string? originalId) + { + if (string.IsNullOrWhiteSpace(originalId)) + { + return false; + } + + return WeChatOriginalIdRegex.IsMatch(originalId); + } + + /// + /// 验证微信UnionID是否有效 + /// + /// UnionID + /// 是否有效 + public static bool IsValidUnionId(string? unionId) + { + if (string.IsNullOrWhiteSpace(unionId)) + { + return false; + } + + return WeChatUnionIdRegex.IsMatch(unionId); + } + + /// + /// 验证微信小程序AppID是否有效 + /// + /// AppID + /// 是否有效 + public static bool IsValidAppId(string? appId) + { + if (string.IsNullOrWhiteSpace(appId)) + { + return false; + } + + return WeChatAppIdRegex.IsMatch(appId.ToLower()); + } + + /// + /// 验证格式是否正确(仅格式检查) + /// + /// 微信号 + /// 格式是否正确 + public static bool IsValidFormat(string? wechatId) + { + return IsValid(wechatId); + } + + #endregion + + #region 类型识别 + + /// + /// 获取微信ID类型 + /// + /// 微信相关ID + /// ID类型描述 + public static string? GetIdType(string? id) + { + if (IsValid(id)) return "微信号"; + if (IsValidOriginalId(id)) return "微信原始ID"; + if (IsValidUnionId(id)) return "UnionID"; + if (IsValidAppId(id)) return "小程序AppID"; + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化微信号(转小写) + /// + /// 微信号 + /// 格式化后的微信号 + public static string? Normalize(string? wechatId) + { + if (string.IsNullOrWhiteSpace(wechatId)) + { + return null; + } + + string normalized = wechatId.ToLower().Trim(); + return IsValid(normalized) ? normalized : null; + } + + /// + /// 微信号脱敏:abc***xyz + /// + /// 微信号 + /// 脱敏后的微信号 + public static string? Mask(string? wechatId) + { + if (!IsValid(wechatId)) + { + return null; + } + + string id = wechatId!; + if (id.Length <= 4) + { + return id[0] + new string('*', id.Length - 1); + } + + // 保留前3位和后3位 + int prefixLen = 3; + int suffixLen = 3; + int maskLen = id.Length - prefixLen - suffixLen; + + return id.Substring(0, prefixLen) + new string('*', maskLen) + id.Substring(id.Length - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机微信号(仅供测试使用) + /// + /// 随机微信号 + public static string GenerateRandom() + { + // 微信号长度6-20位 + int length = MathCategory.RandomUtil.RandomInt(6, 21); + + // 第一位为字母 + const string letters = "abcdefghijklmnopqrstuvwxyz"; + string result = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()).ToString(); + + // 剩余位为字母数字下划线减号 + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789_-"; + for (int i = 1; i < length; i++) + { + result += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + return result; + } + + /// + /// 生成随机小程序AppID(仅供测试使用) + /// + /// 随机AppID + public static string GenerateRandomAppId() + { + string hex = ""; + for (int i = 0; i < 16; i++) + { + hex += "0123456789abcdef"[MathCategory.RandomUtil.RandomInt(0, 16)]; + } + return "wx" + hex; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Adler32Util.cs b/EasyTool.Core/CodeCategory/Adler32Util.cs new file mode 100644 index 0000000..8edd799 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Adler32Util.cs @@ -0,0 +1,224 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Adler-32 校验和工具类 + /// Adler-32 是一种快速校验和算法,由 Mark Adler 发明 + /// 用于 zlib 压缩格式,比 CRC32 更快但可靠性略低 + /// + public static class Adler32Util + { + private const uint ModAdler = 65521; + + /// + /// 计算 Adler-32 校验和 + /// + /// 输入数据 + /// 32位校验和 + public static uint Compute(byte[] data) + { + if (data == null || data.Length == 0) + return 1; + + return Compute(data, 0, data.Length); + } + + /// + /// 计算 Adler-32 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 32位校验和 + public static uint Compute(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint a = 1; + uint b = 0; + + for (int i = offset; i < offset + length; i++) + { + a = (a + data[i]) % ModAdler; + b = (b + a) % ModAdler; + } + + return (b << 16) | a; + } + + /// + /// 继续计算 Adler-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 更新后的校验和 + public static uint Continue(uint previousChecksum, byte[] data) + { + if (data == null || data.Length == 0) + return previousChecksum; + + return Continue(previousChecksum, data, 0, data.Length); + } + + /// + /// 继续计算 Adler-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 起始偏移 + /// 数据长度 + /// 更新后的校验和 + public static uint Continue(uint previousChecksum, byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + // 从之前的校验和中提取 a 和 b + uint a = previousChecksum & 0xFFFF; + uint b = (previousChecksum >> 16) & 0xFFFF; + + for (int i = offset; i < offset + length; i++) + { + a = (a + data[i]) % ModAdler; + b = (b + a) % ModAdler; + } + + return (b << 16) | a; + } + + /// + /// 计算字符串的 Adler-32 校验和 + /// + /// 文本 + /// 32位校验和 + public static uint ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return 1; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute(data); + } + + /// + /// 获取 Adler-32 校验和的十六进制表示 + /// + /// 输入数据 + /// 8字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + uint checksum = Compute(data); + return checksum.ToString("x8"); + } + + /// + /// 验证数据的 Adler-32 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify(byte[] data, uint expectedChecksum) + { + return Compute(data) == expectedChecksum; + } + + /// + /// 验证数据的 Adler-32 校验和(十六进制格式) + /// + /// 输入数据 + /// 期望的十六进制校验和 + /// 是否匹配 + public static bool VerifyHex(byte[] data, string expectedHex) + { + if (string.IsNullOrEmpty(expectedHex)) + return false; + + string actual = ComputeHex(data); + return string.Equals(actual, expectedHex, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 合并两个 Adler-32 校验和 + /// + /// 第一个校验和 + /// 第一个数据块的长度 + /// 第二个校验和 + /// 第二个数据块的长度 + /// 合并后的校验和 + public static uint Combine(uint checksum1, long length1, uint checksum2, long length2) + { + // 从校验和中提取 a 和 b + uint a1 = checksum1 & 0xFFFF; + uint b1 = (checksum1 >> 16) & 0xFFFF; + uint a2 = checksum2 & 0xFFFF; + uint b2 = (checksum2 >> 16) & 0xFFFF; + + // 计算合并后的 a 和 b + uint a = (uint)((a1 + a2 * (ulong)LengthPower(length1)) % ModAdler); + uint b = (uint)((b1 + b2 + a2 * (ulong)LengthSum(length1)) % ModAdler); + + return (b << 16) | a; + } + + /// + /// 获取初始校验和值 + /// + /// 初始值(1) + public static uint InitialValue() + { + return 1; + } + + #region 私有方法 + + private static ulong LengthPower(long length) + { + // 计算 65521^length mod (2^32) + ulong result = 1; + ulong baseVal = ModAdler; + + while (length > 0) + { + if ((length & 1) == 1) + { + result = (result * baseVal) % 0x100000000; + } + baseVal = (baseVal * baseVal) % 0x100000000; + length >>= 1; + } + + return result; + } + + private static ulong LengthSum(long length) + { + // 计算 sum(65521^i) for i from 0 to length-1 + // 使用等比数列求和公式 + if (length == 0) + return 0; + + ulong sum = 0; + ulong power = 1; + + for (long i = 0; i < length; i++) + { + sum = (sum + power) % 0x100000000; + power = (power * ModAdler) % 0x100000000; + } + + return sum; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/AesUtil.cs b/EasyTool.Core/CodeCategory/AesUtil.cs index c7c8142..50befb3 100644 --- a/EasyTool.Core/CodeCategory/AesUtil.cs +++ b/EasyTool.Core/CodeCategory/AesUtil.cs @@ -15,9 +15,12 @@ public static class AesUtil /// /// Aes 加密 /// - /// - /// - /// + /// 需要加密的字符串 + /// 加密密钥(16、24或32位) + /// 加密模式,默认ECB + /// 填充模式,默认PKCS7 + /// 编码格式,默认UTF-8 + /// Base64编码的加密结果 public static string Encrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrEmpty(str)) return string.Empty; @@ -25,7 +28,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] key = encoding.GetBytes(sk).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var aes = Aes.Create(); + using var aes = Aes.Create(); aes.Key = key; aes.Mode = cipher; aes.Padding = padding; @@ -37,9 +40,12 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo /// /// Aes 解密 /// - /// - /// - /// + /// Base64编码的加密字符串 + /// 解密密钥(16、24或32位) + /// 加密模式,默认ECB + /// 填充模式,默认PKCS7 + /// 编码格式,默认UTF-8 + /// 解密后的原始字符串 public static string Decrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrEmpty(str)) return string.Empty; @@ -47,7 +53,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] key = encoding.GetBytes(sk).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var aes = Aes.Create(); + using var aes = Aes.Create(); aes.Key = key; aes.Mode = cipher; aes.Padding = padding; @@ -248,8 +254,8 @@ private static bool KeyIsLegalSizeBytes(byte[] key) /// 向量iv /// 默认CBC /// 默认PKCS7 - /// 加密流 - public static CryptoStream CreateEncryptingStream(Stream outputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + /// 加密流包装器,使用后需要调用 Dispose 释放资源 + public static AesCryptoStream CreateEncryptingStream(Stream outputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) { if (outputStream == null) throw new ArgumentNullException(nameof(outputStream)); @@ -264,7 +270,8 @@ public static CryptoStream CreateEncryptingStream(Stream outputStream, byte[] ke aes.Mode = cipher; aes.Padding = padding; var encryptor = aes.CreateEncryptor(key, iv); - return new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write); + var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write); + return new AesCryptoStream(aes, cryptoStream); } /// @@ -275,8 +282,8 @@ public static CryptoStream CreateEncryptingStream(Stream outputStream, byte[] ke /// 向量iv /// 默认CBC /// 默认PKCS7 - /// 解密流 - public static CryptoStream CreateDecryptingStream(Stream inputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + /// 解密流包装器,使用后需要调用 Dispose 释放资源 + public static AesCryptoStream CreateDecryptingStream(Stream inputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) { if (inputStream == null) throw new ArgumentNullException(nameof(inputStream)); @@ -291,7 +298,56 @@ public static CryptoStream CreateDecryptingStream(Stream inputStream, byte[] key aes.Mode = cipher; aes.Padding = padding; var decryptor = aes.CreateDecryptor(key, iv); - return new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read); + var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read); + return new AesCryptoStream(aes, cryptoStream); + } + } + + /// + /// AES 加密流包装器,用于正确管理 Aes 和 CryptoStream 的生命周期 + /// + public class AesCryptoStream : Stream, IDisposable + { + private readonly Aes _aes; + private readonly CryptoStream _cryptoStream; + private bool _disposed = false; + + internal AesCryptoStream(Aes aes, CryptoStream cryptoStream) + { + _aes = aes ?? throw new ArgumentNullException(nameof(aes)); + _cryptoStream = cryptoStream ?? throw new ArgumentNullException(nameof(cryptoStream)); + } + + public override bool CanRead => _cryptoStream.CanRead; + public override bool CanSeek => _cryptoStream.CanSeek; + public override bool CanWrite => _cryptoStream.CanWrite; + public override long Length => _cryptoStream.Length; + public override long Position { get => _cryptoStream.Position; set => _cryptoStream.Position = value; } + + public override void Flush() => _cryptoStream.Flush(); + public override int Read(byte[] buffer, int offset, int count) => _cryptoStream.Read(buffer, offset, count); + public override long Seek(long offset, SeekOrigin origin) => _cryptoStream.Seek(offset, origin); + public override void SetLength(long value) => _cryptoStream.SetLength(value); + public override void Write(byte[] buffer, int offset, int count) => _cryptoStream.Write(buffer, offset, count); + + public new void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected override void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing) + { + _cryptoStream?.Dispose(); + _aes?.Dispose(); + } + _disposed = true; + } + base.Dispose(disposing); } } } diff --git a/EasyTool.Core/CodeCategory/Argon2Util.cs b/EasyTool.Core/CodeCategory/Argon2Util.cs new file mode 100644 index 0000000..652e5f7 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Argon2Util.cs @@ -0,0 +1,390 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Argon2 密码哈希工具类 + /// Argon2 是2015年密码哈希竞赛的获胜者,是目前最安全的密码哈希算法 + /// 分为 Argon2d(抗GPU)、Argon2i(抗侧信道)、Argon2id(混合) + /// + public static class Argon2Util + { + // 默认参数 + private const int DefaultMemorySize = 65536; // 64 MB + private const int DefaultIterations = 3; + private const int DefaultParallelism = 4; + private const int DefaultHashLength = 32; + private const int DefaultSaltLength = 16; + + /// + /// 使用 Argon2id 哈希密码 + /// + /// 密码 + /// 盐值(可选,默认自动生成) + /// 内存大小(KB,默认65536) + /// 迭代次数(默认3) + /// 并行度(默认4) + /// 哈希长度(默认32) + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt = null, int memorySize = DefaultMemorySize, + int iterations = DefaultIterations, int parallelism = DefaultParallelism, int hashLength = DefaultHashLength) + { + return Hash(password, salt, Argon2Type.Argon2id, memorySize, iterations, parallelism, hashLength); + } + + /// + /// 使用指定类型的 Argon2 哈希密码 + /// + /// 密码 + /// 盐值 + /// Argon2 类型 + /// 内存大小(KB) + /// 迭代次数 + /// 并行度 + /// 哈希长度 + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt, Argon2Type type, int memorySize, + int iterations, int parallelism, int hashLength) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + ValidateParameters(memorySize, iterations, parallelism, hashLength); + + salt ??= GenerateSalt(); + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] hash = DeriveKey(passwordBytes, salt, type, memorySize, iterations, parallelism, hashLength); + + // 格式:$argon2$v=19$m=,t=,p=$$ + string typeStr = type switch + { + Argon2Type.Argon2d => "d", + Argon2Type.Argon2i => "i", + Argon2Type.Argon2id => "id", + _ => "id" + }; + + return $"$argon2{typeStr}$v=19$m={memorySize},t={iterations},p={parallelism}" + + $"${Base64Encode(salt)}${Base64Encode(hash)}"; + } + + /// + /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + var (type, memorySize, iterations, parallelism, salt, expectedHash) = ParseHash(hash); + if (salt == null || expectedHash == null) + return false; + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] computedHash = DeriveKey(passwordBytes, salt, type, memorySize, iterations, parallelism, expectedHash.Length); + + return ConstantTimeEquals(computedHash, expectedHash); + } + catch + { + return false; + } + } + + /// + /// 使用 Argon2 派生密钥 + /// + /// 密码 + /// 盐值 + /// Argon2 类型 + /// 内存大小(KB) + /// 迭代次数 + /// 并行度 + /// 哈希长度 + /// 派生密钥 + public static byte[] DeriveKey(byte[] password, byte[] salt, Argon2Type type = Argon2Type.Argon2id, + int memorySize = DefaultMemorySize, int iterations = DefaultIterations, + int parallelism = DefaultParallelism, int hashLength = DefaultHashLength) + { + if (password == null || password.Length == 0) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length < 8) + throw new ArgumentException("Salt must be at least 8 bytes", nameof(salt)); + + ValidateParameters(memorySize, iterations, parallelism, hashLength); + + // 简化版 Argon2 实现 + return SimplifiedArgon2(password, salt, type, memorySize, iterations, parallelism, hashLength); + } + + /// + /// 生成随机盐值 + /// + /// 盐值长度 + /// 盐值 + public static byte[] GenerateSalt(int length = DefaultSaltLength) + { + byte[] salt = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return salt; + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的内存大小 + /// 新的迭代次数 + /// 新的并行度 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int memorySize = DefaultMemorySize, + int iterations = DefaultIterations, int parallelism = DefaultParallelism) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (_, oldMemory, oldIterations, oldParallelism, _, _) = ParseHash(hash); + return oldMemory != memorySize || oldIterations != iterations || oldParallelism != parallelism; + } + catch + { + return true; + } + } + + #region 私有方法 + + private static void ValidateParameters(int memorySize, int iterations, int parallelism, int hashLength) + { + if (memorySize < 8) + throw new ArgumentException("Memory size must be at least 8 KB", nameof(memorySize)); + + if (iterations < 1) + throw new ArgumentException("Iterations must be at least 1", nameof(iterations)); + + if (parallelism < 1) + throw new ArgumentException("Parallelism must be at least 1", nameof(parallelism)); + + if (hashLength < 4) + throw new ArgumentException("Hash length must be at least 4 bytes", nameof(hashLength)); + } + + private static (Argon2Type type, int memory, int iterations, int parallelism, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (!hash.StartsWith("$argon2")) + throw new FormatException("Invalid Argon2 hash format"); + + string[] parts = hash.Split('$'); + if (parts.Length < 6) + throw new FormatException("Invalid Argon2 hash format"); + + // 解析类型 + Argon2Type type = parts[1] switch + { + "argon2d" => Argon2Type.Argon2d, + "argon2i" => Argon2Type.Argon2i, + "argon2id" => Argon2Type.Argon2id, + _ => throw new FormatException("Unknown Argon2 type") + }; + + // 解析版本(跳过 v=19) + // 解析参数 + int memory = 0, iterations = 0, parallelism = 0; + + string[] parameters = parts[3].Split(','); + foreach (string param in parameters) + { + string[] kv = param.Split('='); + if (kv.Length != 2) + continue; + + switch (kv[0]) + { + case "m": + memory = int.Parse(kv[1]); + break; + case "t": + iterations = int.Parse(kv[1]); + break; + case "p": + parallelism = int.Parse(kv[1]); + break; + } + } + + byte[] salt = Base64Decode(parts[4]); + byte[] expectedHash = Base64Decode(parts[5]); + + return (type, memory, iterations, parallelism, salt, expectedHash); + } + + private static byte[] SimplifiedArgon2(byte[] password, byte[] salt, Argon2Type type, + int memorySize, int iterations, int parallelism, int hashLength) + { + // 简化版 Argon2 实现 - 使用 HMAC-SHA256 作为核心 + // 注:这是一个简化的实现,不是完整的 Argon2 规范 + + int segmentLength = memorySize * 1024 / (4 * parallelism); + int blockCount = 4 * parallelism * segmentLength / 64; + + // 初始化内存块 + byte[][] memory = new byte[blockCount][]; + for (int i = 0; i < blockCount; i++) + { + memory[i] = new byte[64]; + } + + // 使用 HMAC-SHA256 生成初始块 + using var hmac = new HMACSHA256(password); + + // 填充第一个块 + byte[] initialInput = new byte[salt.Length + 8]; + Array.Copy(salt, initialInput, salt.Length); + BitConverter.GetBytes((long)0).CopyTo(initialInput, salt.Length); + byte[] hash1 = hmac.ComputeHash(initialInput); + Array.Copy(hash1, memory[0], 32); + + BitConverter.GetBytes((long)1).CopyTo(initialInput, salt.Length); + byte[] hash2 = hmac.ComputeHash(initialInput); + Array.Copy(hash2, 0, memory[0], 32, 32); + + // 填充其余块 + for (int i = 1; i < blockCount; i++) + { + byte[] input = new byte[64 + 8]; + Array.Copy(memory[i - 1], input, 64); + BitConverter.GetBytes((long)i).CopyTo(input, 64); + + byte[] h1 = hmac.ComputeHash(input); + byte[] h2 = hmac.ComputeHash(h1); + + Array.Copy(h1, memory[i], 32); + Array.Copy(h2, 0, memory[i], 32, 32); + } + + // 执行迭代 + for (int iter = 0; iter < iterations; iter++) + { + for (int i = 0; i < blockCount; i++) + { + // 根据类型选择引用块 + int refIndex = type switch + { + Argon2Type.Argon2d => (int)(BitConverter.ToUInt32(memory[i], 0) % (uint)i), + Argon2Type.Argon2i => (iter * blockCount + i) % (i + 1), + Argon2Type.Argon2id => i % 2 == 0 ? (int)(BitConverter.ToUInt32(memory[i], 0) % (uint)(i + 1)) : (iter * blockCount + i) % (i + 1), + _ => i > 0 ? i - 1 : 0 + }; + + if (refIndex < 0) refIndex = 0; + if (refIndex >= blockCount) refIndex = blockCount - 1; + + // XOR 操作 + for (int j = 0; j < 64; j++) + { + memory[i][j] ^= memory[refIndex][j]; + } + + // 压缩 + byte[] compressed = hmac.ComputeHash(memory[i]); + Array.Copy(compressed, memory[i], 32); + byte[] compressed2 = hmac.ComputeHash(compressed); + Array.Copy(compressed2, 0, memory[i], 32, 32); + } + } + + // 生成最终哈希 + byte[] result = new byte[hashLength]; + byte[] finalBlock = memory[blockCount - 1]; + + for (int i = 0; i < hashLength; i++) + { + result[i] = finalBlock[i % 64]; + } + + // 额外的混合 + for (int i = 0; i < hashLength; i++) + { + byte[] input = new byte[64 + 4]; + Array.Copy(finalBlock, input, 64); + BitConverter.GetBytes(i).CopyTo(input, 64); + byte[] mixed = hmac.ComputeHash(input); + result[i] = mixed[i % 32]; + } + + return result; + } + + private static string Base64Encode(byte[] data) + { + return Convert.ToBase64String(data) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + private static byte[] Base64Decode(string data) + { + string output = data + .Replace('-', '+') + .Replace('_', '/'); + + switch (output.Length % 4) + { + case 2: output += "=="; break; + case 3: output += "="; break; + } + + return Convert.FromBase64String(output); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } + + /// + /// Argon2 类型 + /// + public enum Argon2Type + { + /// + /// Argon2d - 抗 GPU 攻击 + /// + Argon2d, + + /// + /// Argon2i - 抗侧信道攻击 + /// + Argon2i, + + /// + /// Argon2id - 混合模式(推荐) + /// + Argon2id + } +} diff --git a/EasyTool.Core/CodeCategory/Base32Util.cs b/EasyTool.Core/CodeCategory/Base32Util.cs new file mode 100644 index 0000000..5ee0520 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base32Util.cs @@ -0,0 +1,305 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base32 编码工具类 + /// Base32 使用 32 个可打印字符(A-Z 和 2-7)编码二进制数据 + /// 常用于双因素认证密钥、文件名安全编码等场景 + /// 支持 RFC 4648 标准和 Crockford 编码 + /// + public static class Base32Util + { + // RFC 4648 标准字符集 + private const string Rfc4648Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + + // Crockford 字符集(更友好,避免混淆字符) + private const string CrockfordChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + + // 解码映射 + private static readonly int[] Rfc4648DecodeMap; + private static readonly int[] CrockfordDecodeMap; + + static Base32Util() + { + Rfc4648DecodeMap = CreateDecodeMap(Rfc4648Chars); + CrockfordDecodeMap = CreateDecodeMap(CrockfordChars); + } + + private static int[] CreateDecodeMap(string chars) + { + var map = new int[128]; + for (int i = 0; i < 128; i++) map[i] = -1; + + for (int i = 0; i < chars.Length; i++) + { + map[chars[i]] = i; + if (chars[i] >= 'A' && chars[i] <= 'Z') + map[chars[i] + 32] = i; // 小写映射 + } + + // Crockford 特殊映射 + if (chars == CrockfordChars) + { + map['O'] = map['o'] = 0; + map['I'] = map['i'] = 1; + map['L'] = map['l'] = 1; + } + + return map; + } + + #region RFC 4648 编码 + + /// + /// 使用 RFC 4648 标准编码为 Base32 + /// + /// 要编码的数据 + /// Base32 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, Base32Format.Rfc4648); + } + + /// + /// 使用指定格式编码为 Base32 + /// + /// 要编码的数据 + /// 编码格式 + /// Base32 编码字符串 + public static string Encode(byte[] data, Base32Format format) + { + if (data == null || data.Length == 0) + return string.Empty; + + string chars = format == Base32Format.Crockford ? CrockfordChars : Rfc4648Chars; + + var result = new StringBuilder((data.Length * 8 + 4) / 5); + int bits = 0; + int value = 0; + + foreach (byte b in data) + { + value = (value << 8) | b; + bits += 8; + + while (bits >= 5) + { + result.Append(chars[(value >> (bits - 5)) & 0x1F]); + bits -= 5; + } + } + + if (bits > 0) + { + result.Append(chars[(value << (5 - bits)) & 0x1F]); + } + + // RFC 4648 添加填充 + if (format == Base32Format.Rfc4648) + { + int padding = (8 - (result.Length % 8)) % 8; + for (int i = 0; i < padding; i++) + { + result.Append('='); + } + } + + return result.ToString(); + } + + /// + /// 解码 Base32 字符串 + /// + /// Base32 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + return Decode(encoded, Base32Format.Rfc4648); + } + + /// + /// 使用指定格式解码 Base32 字符串 + /// + /// Base32 编码字符串 + /// 编码格式 + /// 解码后的字节数组 + public static byte[] Decode(string encoded, Base32Format format) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + int[] decodeMap = format == Base32Format.Crockford ? CrockfordDecodeMap : Rfc4648DecodeMap; + + // 移除填充字符和空白 + encoded = encoded.TrimEnd('=').Replace(" ", "").Replace("-", ""); + + var result = new System.Collections.Generic.List(); + int bits = 0; + int value = 0; + + foreach (char c in encoded) + { + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid Base32 character: {c}", nameof(encoded)); + + value = (value << 5) | decodeMap[c]; + bits += 5; + + while (bits >= 8) + { + result.Add((byte)((value >> (bits - 8)) & 0xFF)); + bits -= 8; + } + } + + return result.ToArray(); + } + + #endregion + + #region 字符串编码 + + /// + /// 将字符串编码为 Base32(使用 UTF-8) + /// + /// 文本 + /// 编码格式 + /// Base32 编码字符串 + public static string EncodeString(string text, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes, format); + } + + /// + /// 将 Base32 字符串解码为文本(使用 UTF-8) + /// + /// Base32 编码字符串 + /// 编码格式 + /// 解码后的文本 + public static string DecodeToString(string encoded, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded, format); + return Encoding.UTF8.GetString(bytes); + } + + #endregion + + #region 验证 + + /// + /// 验证 Base32 字符串是否有效 + /// + /// Base32 编码字符串 + /// 编码格式 + /// 是否有效 + public static bool IsValid(string encoded, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + int[] decodeMap = format == Base32Format.Crockford ? CrockfordDecodeMap : Rfc4648DecodeMap; + string validChars = format == Base32Format.Crockford ? CrockfordChars : Rfc4648Chars + "="; + + foreach (char c in encoded) + { + if (c == '=' || c == ' ' || c == '-') + continue; + + if (c >= 128 || decodeMap[c] < 0) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base32 字符串 + /// + /// Base32 编码字符串 + /// 解码后的字节数组 + /// 编码格式 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result, Base32Format format = Base32Format.Rfc4648) + { + result = null; + + if (!IsValid(encoded, format)) + return false; + + try + { + result = Decode(encoded, format); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 计算长度 + + /// + /// 计算 Base32 编码后的预计长度 + /// + /// 输入数据长度 + /// 是否包含填充 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength, bool includePadding = true) + { + if (inputLength == 0) + return 0; + + int length = (inputLength * 8 + 4) / 5; + + if (includePadding) + { + length = ((length + 7) / 8) * 8; + } + + return length; + } + + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return encodedLength * 5 / 8; + } + + #endregion + } + + /// + /// Base32 编码格式 + /// + public enum Base32Format + { + /// + /// RFC 4648 标准格式(使用 = 填充) + /// + Rfc4648, + + /// + /// Crockford 格式(无填充,支持校验位) + /// + Crockford + } +} diff --git a/EasyTool.Core/CodeCategory/Base45Util.cs b/EasyTool.Core/CodeCategory/Base45Util.cs new file mode 100644 index 0000000..227ae59 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base45Util.cs @@ -0,0 +1,234 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base45 编码工具类 + /// Base45 是一种使用 45 个字符的二进制到文本编码方案 + /// 用于 QR 码、疫苗证书(EU Digital COVID Certificate)等场景 + /// RFC 9285 标准 + /// + public static class Base45Util + { + // Base45 字符集(RFC 9285) + private const string Base45Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; + + // 解码映射 + private static readonly int[] DecodeMap; + + static Base45Util() + { + DecodeMap = new int[128]; + for (int i = 0; i < 128; i++) + DecodeMap[i] = -1; + + for (int i = 0; i < Base45Chars.Length; i++) + { + DecodeMap[Base45Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base45 字符串 + /// + /// 要编码的数据 + /// Base45 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + for (int i = 0; i < data.Length; i += 2) + { + if (i + 1 < data.Length) + { + // 2 字节 -> 3 字符 + uint value = (uint)((data[i] << 8) | data[i + 1]); + result.Append(Base45Chars[(int)(value % 45)]); + result.Append(Base45Chars[(int)((value / 45) % 45)]); + result.Append(Base45Chars[(int)((value / 2025) % 45)]); + } + else + { + // 1 字节 -> 2 字符 + uint value = data[i]; + result.Append(Base45Chars[(int)(value % 45)]); + result.Append(Base45Chars[(int)((value / 45) % 45)]); + } + } + + return result.ToString(); + } + + /// + /// 将 Base45 字符串解码为字节数组 + /// + /// Base45 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + // 验证长度 + if (encoded.Length % 3 == 1) + throw new ArgumentException("Invalid Base45 string length", nameof(encoded)); + + var result = new System.Collections.Generic.List(); + + for (int i = 0; i < encoded.Length; i += 3) + { + if (i + 2 < encoded.Length) + { + // 3 字符 -> 2 字节 + int c0 = DecodeChar(encoded[i]); + int c1 = DecodeChar(encoded[i + 1]); + int c2 = DecodeChar(encoded[i + 2]); + + uint value = (uint)(c0 + 45 * c1 + 2025 * c2); + + if (value > 65535) + throw new ArgumentException("Invalid Base45 encoding", nameof(encoded)); + + result.Add((byte)(value >> 8)); + result.Add((byte)(value & 0xFF)); + } + else + { + // 2 字符 -> 1 字节 + int c0 = DecodeChar(encoded[i]); + int c1 = DecodeChar(encoded[i + 1]); + + uint value = (uint)(c0 + 45 * c1); + + if (value > 255) + throw new ArgumentException("Invalid Base45 encoding", nameof(encoded)); + + result.Add((byte)value); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base45(使用 UTF-8) + /// + /// 文本 + /// Base45 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base45 字符串解码为文本(使用 UTF-8) + /// + /// Base45 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base45 字符串是否有效 + /// + /// Base45 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + // 长度检查 + if (encoded.Length % 3 == 1) + return false; + + foreach (char c in encoded) + { + if (c >= 128 || DecodeMap[c] < 0) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base45 字符串 + /// + /// Base45 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base45 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // 每 2 字节编码为 3 字符,最后可能剩 1 字节编码为 2 字符 + int fullGroups = inputLength / 2; + int remaining = inputLength % 2; + + return fullGroups * 3 + remaining * 2; + } + + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + // 每 3 字符解码为 2 字节 + int fullGroups = encodedLength / 3; + int remaining = encodedLength % 3; + + return fullGroups * 2 + (remaining == 2 ? 1 : 0); + } + + private static int DecodeChar(char c) + { + if (c >= 128 || DecodeMap[c] < 0) + throw new ArgumentException($"Invalid Base45 character: {c}", "encoded"); + + return DecodeMap[c]; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base58Util.cs b/EasyTool.Core/CodeCategory/Base58Util.cs new file mode 100644 index 0000000..b8c9d64 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base58Util.cs @@ -0,0 +1,292 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base58 编解码工具类 + /// Base58 是一种用于 Bitcoin 地址的编码方式,排除了容易混淆的字符 0, O, I, l + /// + public static class Base58Util + { + // Base58 字符集(Bitcoin) + private const string BitcoinAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + + // Base58 字符集(Ripple) + private const string RippleAlphabet = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"; + + // Base58 字符集(Flickr) + private const string FlickrAlphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; + + /// + /// 将字节数组编码为 Base58 字符串 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, BitcoinAlphabet); + } + + /// + /// 将字节数组编码为 Base58 字符串(指定字符集) + /// + /// 要编码的字节数组 + /// 字符集 + /// Base58 编码字符串 + public static string Encode(byte[] data, string alphabet) + { + if (data == null || data.Length == 0) + return string.Empty; + + // 统计前导零 + int leadingZeros = 0; + foreach (byte b in data) + { + if (b == 0) + leadingZeros++; + else + break; + } + + // 转换为 BigInteger 进行计算 + BigInteger value = new BigInteger(data, isBigEndian: true); + + var result = new StringBuilder(); + while (value > 0) + { + value = BigInteger.DivRem(value, 58, out BigInteger remainder); + result.Insert(0, alphabet[(int)remainder]); + } + + // 添加前导 '1'(对应前导零) + for (int i = 0; i < leadingZeros; i++) + { + result.Insert(0, alphabet[0]); + } + + return result.ToString(); + } + + /// < /// 将 Base58 字符串解码为字节数组 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string value) + { + return Decode(value, BitcoinAlphabet); + } + + /// + /// 将 Base58 字符串解码为字节数组(指定字符集) + /// + /// Base58 编码字符串 + /// 字符集 + /// 解码后的字节数组 + public static byte[] Decode(string value, string alphabet) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + // 构建解码映射 + var decodeMap = new Dictionary(); + for (int i = 0; i < alphabet.Length; i++) + { + decodeMap[alphabet[i]] = i; + } + + // 统计前导字符(对应前导零) + int leadingOnes = 0; + foreach (char c in value) + { + if (c == alphabet[0]) + leadingOnes++; + else + break; + } + + // 转换为 BigInteger + BigInteger result = BigInteger.Zero; + BigInteger baseMultiplier = BigInteger.One; + + for (int i = value.Length - 1; i >= 0; i--) + { + char c = value[i]; + if (!decodeMap.TryGetValue(c, out int index)) + { + throw new ArgumentException($"Invalid Base58 character: {c}", nameof(value)); + } + + result += index * baseMultiplier; + baseMultiplier *= 58; + } + + // 转换为字节数组 + byte[] bytes = result.ToByteArray(isUnsigned: true, isBigEndian: true); + + // 添加前导零 + if (leadingOnes > 0) + { + byte[] newBytes = new byte[leadingOnes + bytes.Length]; + Array.Copy(bytes, 0, newBytes, leadingOnes, bytes.Length); + bytes = newBytes; + } + + return bytes; + } + + /// + /// 使用 Ripple 字符集编码 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string EncodeRipple(byte[] data) + { + return Encode(data, RippleAlphabet); + } + + /// + /// 使用 Ripple 字符集解码 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] DecodeRipple(string value) + { + return Decode(value, RippleAlphabet); + } + + /// + /// 使用 Flickr 字符集编码 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string EncodeFlickr(byte[] data) + { + return Encode(data, FlickrAlphabet); + } + + /// + /// 使用 Flickr 字符集解码 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] DecodeFlickr(string value) + { + return Decode(value, FlickrAlphabet); + } + + /// + /// 将字符串编码为 Base58 + /// + /// 要编码的字符串 + /// 编码方式(默认 UTF-8) + /// Base58 编码字符串 + public static string EncodeString(string text, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return Encode(encoding.GetBytes(text)); + } + + /// + /// 将 Base58 字符串解码为原始字符串 + /// + /// Base58 编码字符串 + /// 编码方式(默认 UTF-8) + /// 解码后的字符串 + public static string DecodeString(string value, Encoding encoding = null) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = Decode(value); + return encoding.GetString(bytes); + } + + /// + /// 验证是否是有效的 Base58 字符串 + /// + /// 要验证的字符串 + /// 是否有效 + public static bool IsValid(string value) + { + return IsValid(value, BitcoinAlphabet); + } + + /// + /// 验证是否是有效的 Base58 字符串(指定字符集) + /// + /// 要验证的字符串 + /// 字符集 + /// 是否有效 + public static bool IsValid(string value, string alphabet) + { + if (string.IsNullOrEmpty(value)) + return false; + + var validChars = new HashSet(alphabet); + foreach (char c in value) + { + if (!validChars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base58 字符串 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string value, out byte[] bytes) + { + bytes = null; + if (!IsValid(value)) + return false; + + try + { + bytes = Decode(value); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算编码后的长度 + /// + /// 原始数据长度 + /// 编码后长度 + public static int GetEncodedLength(int dataLength) + { + if (dataLength == 0) + return 0; + + // Base58 编码后长度约为原始长度的 1.37 倍 + return (int)Math.Ceiling(dataLength * 137.0 / 100); + } + + /// + /// 计算解码后的最大长度 + /// + /// 编码后长度 + /// 解码后最大长度 + public static int GetMaxDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return (int)Math.Ceiling(encodedLength * 733.0 / 1000); + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base64UrlUtil.cs b/EasyTool.Core/CodeCategory/Base64UrlUtil.cs new file mode 100644 index 0000000..25d02ee --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base64UrlUtil.cs @@ -0,0 +1,294 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base64URL 编码工具类 + /// Base64URL 是 URL 和文件名安全的 Base64 编码变体 + /// 使用 - 代替 +,_ 代替 /,通常省略填充 + /// 用于 JWT、URL 参数等场景 + /// RFC 4648 标准 + /// + public static class Base64UrlUtil + { + private const string Base64UrlChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + private const string Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + /// + /// 将字节数组编码为 Base64URL 字符串 + /// + /// 要编码的数据 + /// 是否添加填充 + /// Base64URL 编码字符串 + public static string Encode(byte[] data, bool padding = false) + { + if (data == null || data.Length == 0) + return string.Empty; + + // 先使用标准 Base64 编码 + string base64 = Convert.ToBase64String(data); + + // 转换为 Base64URL + var result = new StringBuilder(base64.Length); + foreach (char c in base64) + { + if (c == '+') + result.Append('-'); + else if (c == '/') + result.Append('_'); + else if (c == '=') + { + if (padding) + result.Append(c); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 将 Base64URL 字符串解码为字节数组 + /// + /// Base64URL 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + // 转换回标准 Base64 + var base64 = new StringBuilder(encoded.Length); + + foreach (char c in encoded) + { + if (c == '-') + base64.Append('+'); + else if (c == '_') + base64.Append('/'); + else + base64.Append(c); + } + + // 添加填充 + int padding = base64.Length % 4; + if (padding > 0) + { + base64.Append(new string('=', 4 - padding)); + } + + return Convert.FromBase64String(base64.ToString()); + } + + /// + /// 将字符串编码为 Base64URL(使用 UTF-8) + /// + /// 文本 + /// 是否添加填充 + /// Base64URL 编码字符串 + public static string EncodeString(string text, bool padding = false) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes, padding); + } + + /// + /// 将 Base64URL 字符串解码为文本(使用 UTF-8) + /// + /// Base64URL 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base64URL 字符串是否有效 + /// + /// Base64URL 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + // 检查长度 + if (encoded.Length % 4 == 1) + return false; + + // 检查字符 + foreach (char c in encoded) + { + if (c >= 'A' && c <= 'Z') continue; + if (c >= 'a' && c <= 'z') continue; + if (c >= '0' && c <= '9') continue; + if (c == '-' || c == '_') continue; + if (c == '=') + { + // 填充只能在末尾 + int index = encoded.IndexOf('='); + if (index < encoded.Length - 2) + return false; + continue; + } + + return false; + } + + return true; + } + + /// + /// 尝试解码 Base64URL 字符串 + /// + /// Base64URL 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 从标准 Base64 转换为 Base64URL + /// + /// 标准 Base64 字符串 + /// 是否移除填充 + /// Base64URL 字符串 + public static string FromBase64(string base64, bool removePadding = true) + { + if (string.IsNullOrEmpty(base64)) + return string.Empty; + + var result = new StringBuilder(base64.Length); + + foreach (char c in base64) + { + if (c == '+') + result.Append('-'); + else if (c == '/') + result.Append('_'); + else if (c == '=' && removePadding) + continue; + else + result.Append(c); + } + + return result.ToString(); + } + + /// + /// 从 Base64URL 转换为标准 Base64 + /// + /// Base64URL 字符串 + /// 标准 Base64 字符串 + public static string ToBase64(string base64Url) + { + if (string.IsNullOrEmpty(base64Url)) + return string.Empty; + + var result = new StringBuilder(base64Url); + + for (int i = 0; i < result.Length; i++) + { + if (result[i] == '-') + result[i] = '+'; + else if (result[i] == '_') + result[i] = '/'; + } + + // 添加填充 + int padding = result.Length % 4; + if (padding > 0) + { + result.Append(new string('=', 4 - padding)); + } + + return result.ToString(); + } + + /// + /// 计算 Base64URL 编码后的预计长度 + /// + /// 输入数据长度 + /// 是否包含填充 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength, bool includePadding = false) + { + if (inputLength == 0) + return 0; + + int length = (inputLength + 2) / 3 * 4; + + if (!includePadding) + { + // 移除填充 + int remainder = inputLength % 3; + if (remainder > 0) + length -= 3 - remainder; + } + + return length; + } + + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return encodedLength * 3 / 4; + } + + /// + /// 比较两个 Base64URL 字符串是否相等(常量时间) + /// + /// 第一个字符串 + /// 第二个字符串 + /// 是否相等 + public static bool ConstantTimeEquals(string a, string b) + { + if (a == null || b == null) + return a == b; + + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base85Util.cs b/EasyTool.Core/CodeCategory/Base85Util.cs new file mode 100644 index 0000000..fcdefff --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base85Util.cs @@ -0,0 +1,437 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base85(Ascii85)编解码工具类 + /// Base85 是一种将二进制数据编码为ASCII字符的编码方式 + /// 每4个字节编码为5个字符,比Base64更高效 + /// + public static class Base85Util + { + // Ascii85 字符集 + private const string Ascii85Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; + + // Z85 字符集(ZeroMQ) + private const string Z85Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"; + + // RFC1924 字符集(IPv6地址) + private const string Rfc1924Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; + + // Adobe 分隔符 + private const string AdobePrefix = "<~"; + private const string AdobeSuffix = "~>"; + + #region Ascii85 编解码 + + /// + /// 将字节数组编码为 Ascii85 字符串 + /// + /// 要编码的字节数组 + /// Ascii85 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, false); + } + + /// + /// 将字节数组编码为 Ascii85 字符串 + /// + /// 要编码的字节数组 + /// 是否使用Adobe格式(添加 <~ 和 ~>) + /// Ascii85 编码字符串 + public static string Encode(byte[] data, bool useAdobeFormat) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + // 处理完整的4字节块 + int fullBlocks = data.Length / 4; + int remainder = data.Length % 4; + + for (int i = 0; i < fullBlocks; i++) + { + uint value = (uint)((data[i * 4] << 24) | + (data[i * 4 + 1] << 16) | + (data[i * 4 + 2] << 8) | + data[i * 4 + 3]); + + if (value == 0 && useAdobeFormat) + { + result.Append('z'); // 特殊压缩:4个零字节编码为 'z' + } + else + { + EncodeBlock(result, value, 5); + } + } + + // 处理剩余字节 + if (remainder > 0) + { + uint value = 0; + int padding = 4 - remainder; + + for (int i = 0; i < remainder; i++) + { + value = (value << 8) | data[fullBlocks * 4 + i]; + } + value <<= (padding * 8); + + EncodeBlock(result, value, remainder + 1); + } + + if (useAdobeFormat) + { + return AdobePrefix + result.ToString() + AdobeSuffix; + } + + return result.ToString(); + } + + /// + /// 将 Ascii85 字符串解码为字节数组 + /// + /// Ascii85 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string value) + { + return Decode(value, false); + } + + /// + /// 将 Ascii85 字符串解码为字节数组 + /// + /// Ascii85 编码字符串 + /// 是否为Adobe格式 + /// 解码后的字节数组 + public static byte[] Decode(string value, bool adobeFormat) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + // 移除 Adobe 格式的分隔符 + if (adobeFormat) + { + if (value.StartsWith(AdobePrefix)) + value = value.Substring(AdobePrefix.Length); + if (value.EndsWith(AdobeSuffix)) + value = value.Substring(0, value.Length - AdobeSuffix.Length); + } + + // 移除空白字符 + value = RemoveWhitespace(value); + + var result = new byte[CalculateDecodedLength(value)]; + int resultIndex = 0; + + int i = 0; + while (i < value.Length) + { + if (value[i] == 'z') + { + // 'z' 表示4个零字节 + result[resultIndex++] = 0; + result[resultIndex++] = 0; + result[resultIndex++] = 0; + result[resultIndex++] = 0; + i++; + } + else if (value[i] == 'y') + { + // 'y' 表示4个空格字节(某些变体) + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + i++; + } + else + { + // 处理5字符块 + int blockLength = Math.Min(5, value.Length - i); + uint decoded = DecodeBlock(value, i, blockLength); + + int bytesToWrite = blockLength - 1; + for (int j = 3; j >= 4 - bytesToWrite; j--) + { + result[resultIndex++] = (byte)((decoded >> (j * 8)) & 0xFF); + } + + i += blockLength; + } + } + + // 调整数组大小 + if (resultIndex < result.Length) + { + Array.Resize(ref result, resultIndex); + } + + return result; + } + + private static void EncodeBlock(StringBuilder result, uint value, int chars) + { + for (int i = chars - 1; i >= 0; i--) + { + result.Append((char)(value % 85 + 33)); + value /= 85; + } + } + + private static uint DecodeBlock(string value, int offset, int length) + { + uint result = 0; + for (int i = 0; i < length; i++) + { + char c = value[offset + i]; + if (c < 33 || c > 117) + { + throw new ArgumentException($"Invalid Ascii85 character: {c}", nameof(value)); + } + result = result * 85 + (uint)(c - 33); + } + + // 填充剩余字符的影响 + for (int i = length; i < 5; i++) + { + result = result * 85 + 84; // 'u' - 33 = 84 + } + + return result; + } + + private static int CalculateDecodedLength(string value) + { + int length = 0; + for (int i = 0; i < value.Length; i++) + { + if (value[i] == 'z' || value[i] == 'y') + { + length += 4; + } + else if (value[i] >= 33 && value[i] <= 117) + { + length++; + } + } + return (length / 5) * 4 + 4; // 估算,后续会调整 + } + + private static string RemoveWhitespace(string value) + { + var result = new StringBuilder(value.Length); + foreach (char c in value) + { + if (!char.IsWhiteSpace(c)) + { + result.Append(c); + } + } + return result.ToString(); + } + + #endregion + + #region Z85 编解码 + + private static readonly int[] Z85DecodeMap = BuildZ85DecodeMap(); + + private static int[] BuildZ85DecodeMap() + { + var map = new int[256]; + for (int i = 0; i < 256; i++) + map[i] = -1; + for (int i = 0; i < Z85Chars.Length; i++) + map[Z85Chars[i]] = i; + return map; + } + + /// + /// 将字节数组编码为 Z85 字符串 + /// + /// 要编码的字节数组(长度必须是4的倍数) + /// Z85 编码字符串 + public static string EncodeZ85(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + if (data.Length % 4 != 0) + throw new ArgumentException("Data length must be a multiple of 4 for Z85 encoding", nameof(data)); + + var result = new StringBuilder(data.Length * 5 / 4); + + for (int i = 0; i < data.Length; i += 4) + { + uint value = (uint)((data[i] << 24) | + (data[i + 1] << 16) | + (data[i + 2] << 8) | + data[i + 3]); + + char[] block = new char[5]; + for (int j = 4; j >= 0; j--) + { + block[j] = Z85Chars[(int)(value % 85)]; + value /= 85; + } + result.Append(block); + } + + return result.ToString(); + } + + /// + /// 将 Z85 字符串解码为字节数组 + /// + /// Z85 编码字符串(长度必须是5的倍数) + /// 解码后的字节数组 + public static byte[] DecodeZ85(string value) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + if (value.Length % 5 != 0) + throw new ArgumentException("String length must be a multiple of 5 for Z85 decoding", nameof(value)); + + var result = new byte[value.Length * 4 / 5]; + int resultIndex = 0; + + for (int i = 0; i < value.Length; i += 5) + { + uint decoded = 0; + for (int j = 0; j < 5; j++) + { + int index = Z85DecodeMap[value[i + j]]; + if (index < 0) + throw new ArgumentException($"Invalid Z85 character: {value[i + j]}", nameof(value)); + decoded = decoded * 85 + (uint)index; + } + + result[resultIndex++] = (byte)((decoded >> 24) & 0xFF); + result[resultIndex++] = (byte)((decoded >> 16) & 0xFF); + result[resultIndex++] = (byte)((decoded >> 8) & 0xFF); + result[resultIndex++] = (byte)(decoded & 0xFF); + } + + return result; + } + + #endregion + + #region 通用方法 + + /// + /// 将字符串编码为 Ascii85 + /// + /// 要编码的字符串 + /// 编码方式(默认UTF-8) + /// Ascii85 编码字符串 + public static string EncodeString(string text, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return Encode(encoding.GetBytes(text)); + } + + /// + /// 将 Ascii85 字符串解码为原始字符串 + /// + /// Ascii85 编码字符串 + /// 编码方式(默认UTF-8) + /// 解码后的字符串 + public static string DecodeString(string value, Encoding encoding = null) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = Decode(value); + return encoding.GetString(bytes); + } + + /// + /// 验证是否是有效的 Ascii85 字符串 + /// + /// 要验证的字符串 + /// 是否有效 + public static bool IsValid(string value) + { + if (string.IsNullOrEmpty(value)) + return false; + + // 移除 Adobe 格式的分隔符 + if (value.StartsWith(AdobePrefix)) + value = value.Substring(AdobePrefix.Length); + if (value.EndsWith(AdobeSuffix)) + value = value.Substring(0, value.Length - AdobeSuffix.Length); + + value = RemoveWhitespace(value); + + foreach (char c in value) + { + if (c != 'z' && c != 'y' && (c < 33 || c > 117)) + return false; + } + + return true; + } + + /// + /// 尝试解码 Ascii85 字符串 + /// + /// Ascii85 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string value, out byte[] bytes) + { + bytes = null; + if (!IsValid(value)) + return false; + + try + { + bytes = Decode(value); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算编码后的长度 + /// + /// 原始数据长度 + /// 编码后长度 + public static int GetEncodedLength(int dataLength) + { + if (dataLength == 0) + return 0; + + return (dataLength + 3) / 4 * 5; + } + + /// + /// 计算解码后的最大长度 + /// + /// 编码后长度 + /// 解码后最大长度 + public static int GetMaxDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return encodedLength / 5 * 4 + 4; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Base91Util.cs b/EasyTool.Core/CodeCategory/Base91Util.cs new file mode 100644 index 0000000..b1fbfa1 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base91Util.cs @@ -0,0 +1,216 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base91 编码工具类 + /// Base91 是一种二进制到文本的编码方案,比 Base64 更高效 + /// 编码效率:约 23%(比 Base64 的 33% 更低开销) + /// + public static class Base91Util + { + // Base91 字符集 + private const string Base91Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~\""; + + // 解码映射表 + private static readonly int[] DecodeMap; + + static Base91Util() + { + DecodeMap = new int[256]; + for (int i = 0; i < 256; i++) + { + DecodeMap[i] = -1; + } + for (int i = 0; i < Base91Chars.Length; i++) + { + DecodeMap[Base91Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base91 字符串 + /// + /// 要编码的数据 + /// Base91 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + int b = 0; + int n = 0; + + foreach (byte c in data) + { + b |= c << n; + n += 8; + + if (n > 13) + { + int v = b & 8191; + if (v > 88) + { + b >>= 13; + n -= 13; + } + else + { + v = b & 16383; + b >>= 14; + n -= 14; + } + result.Append(Base91Chars[v % 91]); + result.Append(Base91Chars[v / 91]); + } + } + + if (n > 0) + { + result.Append(Base91Chars[b % 91]); + if (n > 7 || b > 90) + { + result.Append(Base91Chars[b / 91]); + } + } + + return result.ToString(); + } + + /// + /// 将 Base91 字符串解码为字节数组 + /// + /// Base91 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + int b = 0; + int n = 0; + int v = -1; + + foreach (char c in encoded) + { + if (c >= 256 || DecodeMap[c] == -1) + continue; + + if (v == -1) + { + v = DecodeMap[c]; + } + else + { + v += DecodeMap[c] * 91; + b |= v << n; + n += (v & 8191) > 88 ? 13 : 14; + + while (n > 7) + { + result.Add((byte)(b & 255)); + b >>= 8; + n -= 8; + } + + v = -1; + } + } + + if (v != -1) + { + result.Add((byte)((b | v << n) & 255)); + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base91(使用 UTF-8) + /// + /// 要编码的文本 + /// Base91 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base91 字符串解码为文本(使用 UTF-8) + /// + /// Base91 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base91 字符串是否有效 + /// + /// Base91 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + foreach (char c in encoded) + { + if (c >= 256 || DecodeMap[c] == -1) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base91 字符串 + /// + /// Base91 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base91 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // Base91 编码效率约为 23% + return (int)Math.Ceiling(inputLength * 1.23); + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base92Util.cs b/EasyTool.Core/CodeCategory/Base92Util.cs new file mode 100644 index 0000000..8c1e724 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base92Util.cs @@ -0,0 +1,263 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base92 编码工具类 + /// Base92 是一种二进制到文本的编码方案,比 Base85 更高效 + /// 使用 92 个可打印 ASCII 字符 + /// + public static class Base92Util + { + // Base92 字符集 + private const string Base92Chars = "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~\""; + + // 解码映射表 + private static readonly int[] DecodeMap; + + static Base92Util() + { + DecodeMap = new int[256]; + for (int i = 0; i < 256; i++) + { + DecodeMap[i] = -1; + } + for (int i = 0; i < Base92Chars.Length; i++) + { + DecodeMap[Base92Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base92 字符串 + /// + /// 要编码的数据 + /// Base92 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return "~"; + + var result = new StringBuilder(); + int bitBuffer = 0; + int bitsInBuffer = 0; + + foreach (byte b in data) + { + bitBuffer = (bitBuffer << 8) | b; + bitsInBuffer += 8; + + while (bitsInBuffer >= 13) + { + int value = (bitBuffer >> (bitsInBuffer - 13)) & 0x1FFF; + + if (value < 91) + { + result.Append(Base92Chars[value]); + bitsInBuffer -= 13; + } + else + { + value -= 91; + result.Append(Base92Chars[value / 91 + 91]); + result.Append(Base92Chars[value % 91]); + bitsInBuffer -= 14; + } + } + } + + // 处理剩余位 + if (bitsInBuffer > 0) + { + int value = (bitBuffer << (13 - bitsInBuffer)) & 0x1FFF; + + if (value < 91) + { + result.Append(Base92Chars[value]); + } + else + { + value -= 91; + result.Append(Base92Chars[value / 91 + 91]); + result.Append(Base92Chars[value % 91]); + } + } + + result.Append('~'); + + return result.ToString(); + } + + /// + /// 将 Base92 字符串解码为字节数组 + /// + /// Base92 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + if (!encoded.EndsWith("~")) + throw new ArgumentException("Invalid Base92 string: must end with ~", nameof(encoded)); + + string data = encoded.TrimEnd('~'); + if (string.IsNullOrEmpty(data)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + int bitBuffer = 0; + int bitsInBuffer = 0; + + int i = 0; + while (i < data.Length) + { + int value; + + int c1 = data[i] < 256 ? DecodeMap[data[i]] : -1; + if (c1 < 0) + throw new ArgumentException($"Invalid Base92 character: {data[i]}", nameof(encoded)); + + if (c1 < 91) + { + value = c1; + i++; + } + else + { + if (i + 1 >= data.Length) + throw new ArgumentException("Invalid Base92 string: unexpected end", nameof(encoded)); + + int c2 = data[i + 1] < 256 ? DecodeMap[data[i + 1]] : -1; + if (c2 < 0) + throw new ArgumentException($"Invalid Base92 character: {data[i + 1]}", nameof(encoded)); + + value = (c1 - 91) * 91 + c2 + 91; + i += 2; + } + + bitBuffer = (bitBuffer << 13) | value; + bitsInBuffer += 13; + + while (bitsInBuffer >= 8) + { + bitsInBuffer -= 8; + result.Add((byte)((bitBuffer >> bitsInBuffer) & 0xFF)); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base92(使用 UTF-8) + /// + /// 要编码的文本 + /// Base92 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return "~"; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base92 字符串解码为文本(使用 UTF-8) + /// + /// Base92 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded) || encoded == "~") + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base92 字符串是否有效 + /// + /// Base92 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + if (!encoded.EndsWith("~")) + return false; + + string data = encoded.TrimEnd('~'); + if (string.IsNullOrEmpty(data)) + return true; + + int i = 0; + while (i < data.Length) + { + int c1 = data[i] < 256 ? DecodeMap[data[i]] : -1; + if (c1 < 0) + return false; + + if (c1 < 91) + { + i++; + } + else + { + if (i + 1 >= data.Length) + return false; + + int c2 = data[i + 1] < 256 ? DecodeMap[data[i + 1]] : -1; + if (c2 < 0) + return false; + + i += 2; + } + } + + return true; + } + + /// + /// 尝试解码 Base92 字符串 + /// + /// Base92 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base92 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 1; + + // Base92 编码效率约为 16/13 + return (int)Math.Ceiling(inputLength * 16.0 / 13.0) + 1; + } + } +} diff --git a/EasyTool.Core/CodeCategory/BaudotUtil.cs b/EasyTool.Core/CodeCategory/BaudotUtil.cs new file mode 100644 index 0000000..7d3ecdd --- /dev/null +++ b/EasyTool.Core/CodeCategory/BaudotUtil.cs @@ -0,0 +1,315 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Baudot 编码工具类 + /// Baudot 码(也称为 Murrey 码或 ITA2)是一种5位字符编码 + /// 用于电报和 TTY 通信,支持字母和数字两个字符集 + /// + public static class BaudotUtil + { + // 字母表模式(LTRS - Letters) + private static readonly char[] LettersMode = new char[] + { + '\0', 'E', '\n', 'A', ' ', 'S', 'I', 'U', + '\r', 'D', 'R', 'J', 'N', 'F', 'C', 'K', + 'T', 'Z', 'L', 'W', 'H', 'Y', 'P', 'Q', + 'O', 'B', 'G', '\0', 'M', 'X', 'V', '\0' + }; + + // 数字/符号模式(FIGS - Figures) + private static readonly char[] FiguresMode = new char[] + { + '\0', '3', '\n', '-', ' ', '\'', '8', '7', + '\r', '$', '4', '\a', ',', '!', ':', '(', + '5', '+', ')', '2', '$', '6', '0', '1', + '9', '?', '=', '\0', '.', '/', '=', '\0' + }; + + // 切换到字母表的代码 + private const byte LTRS = 0x1F; + + // 切换到数字表的代码 + private const byte FIGS = 0x1B; + + /// + /// 将文本编码为 Baudot 码字节数组 + /// + /// 要编码的文本 + /// Baudot 码字节数组 + public static byte[] Encode(string text) + { + if (string.IsNullOrEmpty(text)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + bool inLettersMode = true; + + foreach (char c in text.ToUpperInvariant()) + { + // 在字母表中查找 + int lettersIndex = Array.IndexOf(LettersMode, c); + int figuresIndex = Array.IndexOf(FiguresMode, c); + + if (lettersIndex >= 0 && figuresIndex >= 0) + { + // 字符在两个表中都存在,保持当前模式 + result.Add((byte)lettersIndex); + } + else if (lettersIndex >= 0) + { + // 只在字母表中 + if (!inLettersMode) + { + result.Add(LTRS); + inLettersMode = true; + } + result.Add((byte)lettersIndex); + } + else if (figuresIndex >= 0) + { + // 只在数字表中 + if (inLettersMode) + { + result.Add(FIGS); + inLettersMode = false; + } + result.Add((byte)figuresIndex); + } + // 忽略不支持的字符 + } + + return result.ToArray(); + } + + /// + /// 将 Baudot 码字节数组解码为文本 + /// + /// Baudot 码字节数组 + /// 解码后的文本 + public static string Decode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + bool inLettersMode = true; + + foreach (byte b in data) + { + if (b == LTRS) + { + inLettersMode = true; + } + else if (b == FIGS) + { + inLettersMode = false; + } + else + { + char[] table = inLettersMode ? LettersMode : FiguresMode; + if (b < table.Length) + { + char c = table[b]; + if (c != '\0') + { + result.Append(c); + } + } + } + } + + return result.ToString(); + } + + /// + /// 将 Baudot 码编码为二进制字符串表示 + /// + /// Baudot 码字节数组 + /// 二进制字符串数组 + public static string[] ToBinaryStrings(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + var result = new string[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = Convert.ToString(data[i] & 0x1F, 2).PadLeft(5, '0'); + } + + return result; + } + + /// + /// 从二进制字符串创建 Baudot 码 + /// + /// 二进制字符串数组 + /// Baudot 码字节数组 + public static byte[] FromBinaryStrings(string[] binaryStrings) + { + if (binaryStrings == null || binaryStrings.Length == 0) + return Array.Empty(); + + var result = new byte[binaryStrings.Length]; + for (int i = 0; i < binaryStrings.Length; i++) + { + result[i] = (byte)Convert.ToByte(binaryStrings[i], 2); + } + + return result; + } + + /// + /// 将 Baudot 码编码为十六进制字符串 + /// + /// Baudot 码字节数组 + /// 十六进制字符串 + public static string ToHexString(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + foreach (byte b in data) + { + result.Append((b & 0x1F).ToString("X2")); + } + + return result.ToString(); + } + + /// + /// 从十六进制字符串解码 Baudot 码 + /// + /// 十六进制字符串 + /// Baudot 码字节数组 + public static byte[] FromHexString(string hex) + { + if (string.IsNullOrEmpty(hex) + || hex.Length % 2 != 0) + return Array.Empty(); + + var result = new byte[hex.Length / 2]; + for (int i = 0; i < result.Length; i++) + { + result[i] = (byte)(Convert.ToByte(hex.Substring(i * 2, 2), 16) & 0x1F); + } + + return result; + } + + /// + /// 验证文本是否可以用 Baudot 码表示 + /// + /// 文本 + /// 是否可以编码 + public static bool CanEncode(string text) + { + if (string.IsNullOrEmpty(text)) + return true; + + foreach (char c in text.ToUpperInvariant()) + { + if (Array.IndexOf(LettersMode, c) < 0 && + Array.IndexOf(FiguresMode, c) < 0) + { + return false; + } + } + + return true; + } + + /// + /// 获取不支持的字符 + /// + /// 文本 + /// 不支持的字符数组 + public static char[] GetUnsupportedChars(string text) + { + if (string.IsNullOrEmpty(text)) + return Array.Empty(); + + var unsupported = new System.Collections.Generic.List(); + + foreach (char c in text.ToUpperInvariant()) + { + if (Array.IndexOf(LettersMode, c) < 0 && + Array.IndexOf(FiguresMode, c) < 0 && + !unsupported.Contains(c)) + { + unsupported.Add(c); + } + } + + return unsupported.ToArray(); + } + + /// + /// 获取字母表模式字符 + /// + /// Baudot 码(0-31) + /// 字符,如果无效则返回 '\0' + public static char GetLettersChar(byte code) + { + if (code > 31) + return '\0'; + + return LettersMode[code]; + } + + /// + /// 获取数字模式字符 + /// + /// Baudot 码(0-31) + /// 字符,如果无效则返回 '\0' + public static char GetFiguresChar(byte code) + { + if (code > 31) + return '\0'; + + return FiguresMode[code]; + } + + /// + /// 获取字符的 Baudot 码(字母模式) + /// + /// 字符 + /// Baudot 码,如果不存在则返回 -1 + public static int GetLettersCode(char c) + { + return Array.IndexOf(LettersMode, char.ToUpperInvariant(c)); + } + + /// + /// 获取字符的 Baudot 码(数字模式) + /// + /// 字符 + /// Baudot 码,如果不存在则返回 -1 + public static int GetFiguresCode(char c) + { + return Array.IndexOf(FiguresMode, c); + } + + /// + /// 获取完整的字母表 + /// + /// 字母表字符数组 + public static char[] GetLettersTable() + { + return (char[])LettersMode.Clone(); + } + + /// + /// 获取完整的数字表 + /// + /// 数字表字符数组 + public static char[] GetFiguresTable() + { + return (char[])FiguresMode.Clone(); + } + } +} diff --git a/EasyTool.Core/CodeCategory/BcryptUtil.cs b/EasyTool.Core/CodeCategory/BcryptUtil.cs new file mode 100644 index 0000000..e65ff1a --- /dev/null +++ b/EasyTool.Core/CodeCategory/BcryptUtil.cs @@ -0,0 +1,617 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Bcrypt 密码哈希工具类 + /// Bcrypt 是一种专为密码存储设计的哈希算法,内置盐值和工作因子 + /// + public static class BcryptUtil + { + // Bcrypt Base64 字符集 + private const string Base64Chars = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + // 默认工作因子 + private const int DefaultCost = 12; + + // 盐值长度(16字节) + private const int SaltLength = 16; + + // 密码最大长度(72字节) + private const int MaxPasswordLength = 72; + + // Blowfish 初始 P 数组 + private static readonly uint[] InitialP = new uint[] + { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, + 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, + 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, + 0x9216d5d9, 0x8979fb1b + }; + + // Blowfish 初始 S 盒 + private static readonly uint[] InitialS = new uint[] + { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, + 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, + 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, + 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, + 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, + 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, + 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, + 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, + 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, + 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, + 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, + 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, + 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, + 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, + 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, + 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, + 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, + 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, + 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, + 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, + 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, + 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, + 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, + 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, + 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, + 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, + 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, + 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, + 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, + 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, + 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, + 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, + 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, + 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, + 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, + 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, + 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, + 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, + 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, + 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, + 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, + 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, + 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, + 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, + 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, + 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, + 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, + 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, + 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, + 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, + 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, + 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, + 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, + 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, + 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, + 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, + 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, + 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, + 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, + 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, + 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, + 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, + 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, + 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, + 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, + 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, + 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, + 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, + 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, + 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, + 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, + 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, + 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, + 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, + 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, + 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, + 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, + 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, + 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, + 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, + 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, + 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, + 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, + 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, + 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, + 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, + 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, + 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, + 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, + 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, + 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, + 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, + 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, + 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, + 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, + 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, + 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, + 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, + 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, + 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, + 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, + 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, + 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, + 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, + 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, + 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, + 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, + 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, + 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, + 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, + 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, + 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, + 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, + 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, + 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, + 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, + 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, + 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, + 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, + 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, + 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, + 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, + 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, + 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, + 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, + 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, + 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, + 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, + 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, + 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, + 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, + 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, + 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, + 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, + 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, + 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, + 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, + 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, + 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, + 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, + 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, + 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, + 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, + 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, + 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, + 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, + 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, + 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, + 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, + 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, + 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, + 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, + 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, + 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, + 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, + 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, + 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, + 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, + 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, + 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, + 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, + 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, + 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, + 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, + 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, + 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, + 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, + 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, + 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 + }; + + /// + /// 使用 Bcrypt 哈希密码 + /// + /// 密码 + /// 工作因子(4-31,默认12) + /// 哈希后的密码字符串 + public static string Hash(string password, int cost = DefaultCost) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (cost < 4 || cost > 31) + throw new ArgumentException("Cost must be between 4 and 31", nameof(cost)); + + // 生成随机盐值 + byte[] salt = new byte[SaltLength]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + + return HashInternal(password, salt, cost); + } + + /// + /// 使用 Bcrypt 哈希密码(使用指定盐值) + /// + /// 密码 + /// 盐值(16字节) + /// 工作因子 + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt, int cost = DefaultCost) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length != SaltLength) + throw new ArgumentException($"Salt must be {SaltLength} bytes", nameof(salt)); + + if (cost < 4 || cost > 31) + throw new ArgumentException("Cost must be between 4 and 31", nameof(cost)); + + return HashInternal(password, salt, cost); + } + + /// + /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + // 解析哈希字符串 + var (cost, salt, expectedHash) = ParseHash(hash); + if (cost == 0 || salt == null || expectedHash == null) + return false; + + // 计算新哈希 + string computedHash = HashInternal(password, salt, cost); + + // 常量时间比较 + return ConstantTimeEquals(hash, computedHash); + } + catch + { + return false; + } + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的工作因子 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int newCost = DefaultCost) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (cost, _, _) = ParseHash(hash); + return cost != newCost; + } + catch + { + return true; + } + } + + /// + /// 获取哈希的工作因子 + /// + /// 哈希字符串 + /// 工作因子 + public static int GetCost(string hash) + { + if (string.IsNullOrEmpty(hash)) + throw new ArgumentException("Hash cannot be null or empty", nameof(hash)); + + var (cost, _, _) = ParseHash(hash); + return cost; + } + + #region 私有方法 + + private static string HashInternal(string password, byte[] salt, int cost) + { + // 限制密码长度 + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + if (passwordBytes.Length > MaxPasswordLength) + { + Array.Resize(ref passwordBytes, MaxPasswordLength); + } + + // Eksblowfish 密钥调度 + uint[] P = new uint[18]; + uint[] S = new uint[1024]; + Array.Copy(InitialP, P, 18); + Array.Copy(InitialS, S, 1024); + + // 使用密码扩展密钥 + byte[] key = new byte[passwordBytes.Length + 1]; + Array.Copy(passwordBytes, key, passwordBytes.Length); + key[passwordBytes.Length] = 0; + + P = ExpandKey(P, S, key); + + // 使用盐值扩展密钥 + P = ExpandKey(P, S, salt); + + // Expensive key setup + for (int i = 0; i < (1 << cost); i++) + { + P = ExpandKey(P, S, key); + P = ExpandKey(P, S, salt); + } + + // 加密 "OrpheanBeholderScryDoubt" 64次 + byte[] magic = Encoding.ASCII.GetBytes("OrpheanBeholderScryDoubt"); + byte[] hash = EncryptECB(P, S, magic, 64); + + // 构建输出字符串 + string saltBase64 = EncodeBase64(salt); + string hashBase64 = EncodeBase64(hash, 23); // 只使用23字节 + + return $"$2a${cost:D2}${saltBase64}{hashBase64}"; + } + + private static uint[] ExpandKey(uint[] P, uint[] S, byte[] data) + { + int offset = 0; + + for (int i = 0; i < 18; i++) + { + uint d = ((uint)data[offset % data.Length] << 24) | + ((uint)data[(offset + 1) % data.Length] << 16) | + ((uint)data[(offset + 2) % data.Length] << 8) | + data[(offset + 3) % data.Length]; + P[i] ^= d; + offset += 4; + } + + uint L = 0, R = 0; + + for (int i = 0; i < 18; i += 2) + { + EncryptBlock(ref L, ref R, P, S); + P[i] = L; + P[i + 1] = R; + } + + for (int i = 0; i < 1024; i += 2) + { + EncryptBlock(ref L, ref R, P, S); + S[i] = L; + S[i + 1] = R; + } + + return P; + } + + private static void EncryptBlock(ref uint L, ref uint R, uint[] P, uint[] S) + { + L ^= P[0]; + for (int i = 1; i <= 16; i++) + { + R ^= ((S[(L >> 24) & 0xFF] + S[256 + ((L >> 16) & 0xFF)]) ^ + S[512 + ((L >> 8) & 0xFF)]) + S[768 + (L & 0xFF)]; + R ^= P[i]; + + if (i < 16) + { + uint temp = L; + L = R; + R = temp; + } + } + R ^= P[17]; + } + + private static byte[] EncryptECB(uint[] P, uint[] S, byte[] data, int rounds) + { + byte[] result = new byte[data.Length]; + Array.Copy(data, result, data.Length); + + for (int r = 0; r < rounds; r++) + { + for (int i = 0; i < result.Length; i += 8) + { + uint L = ((uint)result[i] << 24) | ((uint)result[i + 1] << 16) | + ((uint)result[i + 2] << 8) | result[i + 3]; + uint R = ((uint)result[i + 4] << 24) | ((uint)result[i + 5] << 16) | + ((uint)result[i + 6] << 8) | result[i + 7]; + + EncryptBlock(ref L, ref R, P, S); + + result[i] = (byte)(L >> 24); + result[i + 1] = (byte)(L >> 16); + result[i + 2] = (byte)(L >> 8); + result[i + 3] = (byte)L; + result[i + 4] = (byte)(R >> 24); + result[i + 5] = (byte)(R >> 16); + result[i + 6] = (byte)(R >> 8); + result[i + 7] = (byte)R; + } + } + + return result; + } + + private static (int cost, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (string.IsNullOrEmpty(hash) || !hash.StartsWith("$2")) + return (0, null, null); + + string[] parts = hash.Split('$'); + if (parts.Length < 4) + return (0, null, null); + + // 解析版本和成本 + string version = parts[1]; + if (!int.TryParse(parts[2], out int cost)) + return (0, null, null); + + // 解析盐值和哈希 + string saltAndHash = parts[3]; + if (saltAndHash.Length < 31) + return (0, null, null); + + byte[] salt = DecodeBase64(saltAndHash.Substring(0, 22)); + byte[] expectedHash = DecodeBase64(saltAndHash.Substring(22)); + + return (cost, salt, expectedHash); + } + + private static string EncodeBase64(byte[] data, int length = -1) + { + if (length == -1) + length = data.Length; + + var result = new StringBuilder(); + int i = 0; + + while (i < length) + { + uint b1 = i < length ? (uint)data[i++] : 0; + uint b2 = i < length ? (uint)data[i++] : 0; + uint b3 = i < length ? (uint)data[i++] : 0; + + result.Append(Base64Chars[(int)(b1 >> 2)]); + result.Append(Base64Chars[(int)(((b1 & 0x03) << 4) | (b2 >> 4))]); + result.Append(Base64Chars[(int)(((b2 & 0x0f) << 2) | (b3 >> 6))]); + result.Append(Base64Chars[(int)(b3 & 0x3f)]); + } + + return result.ToString(); + } + + private static byte[] DecodeBase64(string data) + { + var result = new List(); + int i = 0; + + while (i < data.Length) + { + uint c1 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c2 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c3 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c4 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + + result.Add((byte)((c1 << 2) | (c2 >> 4))); + result.Add((byte)(((c2 & 0x0f) << 4) | (c3 >> 2))); + result.Add((byte)(((c3 & 0x03) << 6) | c4)); + } + + return result.ToArray(); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Blake2Util.cs b/EasyTool.Core/CodeCategory/Blake2Util.cs new file mode 100644 index 0000000..ddb823e --- /dev/null +++ b/EasyTool.Core/CodeCategory/Blake2Util.cs @@ -0,0 +1,569 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// BLAKE2 哈希工具类 + /// BLAKE2 是一种快速、安全的加密哈希函数 + /// 比 MD5、SHA-1、SHA-2 更快,同时提供更高的安全性 + /// 包含 BLAKE2b(64位优化)和 BLAKE2s(32位优化)两个版本 + /// + public static class Blake2Util + { + #region BLAKE2b + + /// + /// 计算 BLAKE2b-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeBlake2b256(byte[] data) + { + return ComputeBlake2b(data, 32); + } + + /// + /// 计算 BLAKE2b-384 哈希值 + /// + /// 输入数据 + /// 48字节哈希值 + public static byte[] ComputeBlake2b384(byte[] data) + { + return ComputeBlake2b(data, 48); + } + + /// + /// 计算 BLAKE2b-512 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] ComputeBlake2b512(byte[] data) + { + return ComputeBlake2b(data, 64); + } + + /// + /// 计算指定长度的 BLAKE2b 哈希值 + /// + /// 输入数据 + /// 哈希长度(1-64字节) + /// 指定长度的哈希值 + public static byte[] ComputeBlake2b(byte[] data, int hashLength) + { + return ComputeBlake2b(data, 0, data?.Length ?? 0, null, null, hashLength); + } + + /// + /// 使用密钥计算 BLAKE2b 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(最多64字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeBlake2bWithKey(byte[] data, byte[] key, int hashLength = 64) + { + return ComputeBlake2b(data, 0, data?.Length ?? 0, key, null, hashLength); + } + + /// + /// 计算 BLAKE2b 哈希值(完整参数) + /// + public static byte[] ComputeBlake2b(byte[] data, int offset, int length, byte[] key, byte[] salt, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1 || hashLength > 64) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be between 1 and 64 bytes"); + if (key != null && key.Length > 64) + throw new ArgumentException("Key must be at most 64 bytes", nameof(key)); + + var hasher = new Blake2bHasher(key, salt, hashLength); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 BLAKE2b-256 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeBlake2b256String(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeBlake2b256(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeBlake2b256(data); + } + + /// + /// 获取 BLAKE2b-512 哈希的十六进制表示 + /// + /// 输入数据 + /// 128字符的十六进制字符串 + public static string ComputeBlake2b512Hex(byte[] data) + { + byte[] hash = ComputeBlake2b512(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + #endregion + + #region BLAKE2s + + /// + /// 计算 BLAKE2s-128 哈希值 + /// + /// 输入数据 + /// 16字节哈希值 + public static byte[] ComputeBlake2s128(byte[] data) + { + return ComputeBlake2s(data, 16); + } + + /// + /// 计算 BLAKE2s-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeBlake2s256(byte[] data) + { + return ComputeBlake2s(data, 32); + } + + /// + /// 计算指定长度的 BLAKE2s 哈希值 + /// + /// 输入数据 + /// 哈希长度(1-32字节) + /// 指定长度的哈希值 + public static byte[] ComputeBlake2s(byte[] data, int hashLength) + { + return ComputeBlake2s(data, 0, data?.Length ?? 0, null, null, hashLength); + } + + /// + /// 使用密钥计算 BLAKE2s 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(最多32字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeBlake2sWithKey(byte[] data, byte[] key, int hashLength = 32) + { + return ComputeBlake2s(data, 0, data?.Length ?? 0, key, null, hashLength); + } + + /// + /// 计算 BLAKE2s 哈希值(完整参数) + /// + public static byte[] ComputeBlake2s(byte[] data, int offset, int length, byte[] key, byte[] salt, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1 || hashLength > 32) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be between 1 and 32 bytes"); + if (key != null && key.Length > 32) + throw new ArgumentException("Key must be at most 32 bytes", nameof(key)); + + var hasher = new Blake2sHasher(key, salt, hashLength); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 BLAKE2s-256 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeBlake2s256String(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeBlake2s256(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeBlake2s256(data); + } + + /// + /// 获取 BLAKE2s-256 哈希的十六进制表示 + /// + /// 输入数据 + /// 64字符的十六进制字符串 + public static string ComputeBlake2s256Hex(byte[] data) + { + byte[] hash = ComputeBlake2s256(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成适合 BLAKE2b 的随机密钥 + /// + /// 密钥长度(最多64字节) + /// 随机密钥 + public static byte[] GenerateKey(int length = 32) + { + if (length < 1 || length > 64) + throw new ArgumentOutOfRangeException(nameof(length), "Key length must be between 1 and 64 bytes"); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成密钥并返回十六进制 + /// + /// 密钥长度 + /// 十六进制密钥字符串 + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #endregion + } + + #region BLAKE2b 实现类 + + internal class Blake2bHasher + { + private static readonly ulong[] IV = new ulong[] + { + 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, + 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 + }; + + private static readonly int[] Sigma = new int[] + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, + 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, + 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, + 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, + 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, + 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, + 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10, + 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5, + 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 + }; + + private ulong[] h = new ulong[8]; + private ulong[] m = new ulong[16]; + private byte[] buffer = new byte[128]; + private int bufferLength; + private ulong totalLength; + private readonly int hashLength; + + public Blake2bHasher(byte[] key, byte[] salt, int hashLength) + { + this.hashLength = hashLength; + + // 初始化 + for (int i = 0; i < 8; i++) + h[i] = IV[i]; + + // 参数块 + h[0] ^= 0x01010000UL ^ ((ulong)(key?.Length ?? 0) << 8) ^ (ulong)hashLength; + + // 盐 + if (salt != null && salt.Length >= 8) + { + h[4] ^= BitConverter.ToUInt64(salt, 0); + h[5] ^= BitConverter.ToUInt64(salt, 8); + } + + // 处理密钥 + if (key != null && key.Length > 0) + { + Array.Copy(key, buffer, key.Length); + bufferLength = 128; + } + else + { + bufferLength = 0; + } + + totalLength = 0; + } + + public void Update(byte[] data, int offset, int length) + { + totalLength += (ulong)length; + + int pos = 0; + if (bufferLength > 0) + { + int copy = Math.Min(128 - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == 128) + { + Compress(buffer, 0); + bufferLength = 0; + } + } + + while (pos + 128 <= length) + { + Compress(data, offset + pos); + pos += 128; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + public byte[] Final() + { + // 填充 + for (int i = bufferLength; i < 128; i++) + buffer[i] = 0; + + // 最后一轮 + h[6] ^= ~0UL; + + Compress(buffer, 0, true); + + byte[] result = new byte[hashLength]; + for (int i = 0; i < hashLength; i++) + { + result[i] = (byte)(h[i / 8] >> ((i % 8) * 8)); + } + + return result; + } + + private void Compress(byte[] data, int offset, bool isLast = false) + { + ulong[] v = new ulong[16]; + for (int i = 0; i < 8; i++) + { + v[i] = h[i]; + v[i + 8] = IV[i]; + } + + for (int i = 0; i < 16; i++) + { + v[i] ^= BitConverter.ToUInt64(data, offset + i * 8); + } + + ulong counter = totalLength; + v[12] ^= counter; + v[13] ^= counter >> 56; + if (isLast) v[14] = ~0UL; + + for (int round = 0; round < 12; round++) + { + int s = round * 16; + Mix(v, 0, 4, 8, 12, Sigma[s], Sigma[s + 1]); + Mix(v, 1, 5, 9, 13, Sigma[s + 2], Sigma[s + 3]); + Mix(v, 2, 6, 10, 14, Sigma[s + 4], Sigma[s + 5]); + Mix(v, 3, 7, 11, 15, Sigma[s + 6], Sigma[s + 7]); + + Mix(v, 0, 5, 10, 15, Sigma[s + 8], Sigma[s + 9]); + Mix(v, 1, 6, 11, 12, Sigma[s + 10], Sigma[s + 11]); + Mix(v, 2, 7, 8, 13, Sigma[s + 12], Sigma[s + 13]); + Mix(v, 3, 4, 9, 14, Sigma[s + 14], Sigma[s + 15]); + } + + for (int i = 0; i < 8; i++) + h[i] ^= v[i] ^ v[i + 8]; + } + + private static void Mix(ulong[] v, int a, int b, int c, int d, int x, int y) + { + v[a] += v[b] + (ulong)x; + v[d] = RotateRight(v[d] ^ v[a], 32); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 24); + v[a] += v[b] + (ulong)y; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 63); + } + + private static ulong RotateRight(ulong x, int n) => (x >> n) | (x << (64 - n)); + } + + #endregion + + #region BLAKE2s 实现类 + + internal class Blake2sHasher + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private static readonly int[] Sigma = new int[] + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, + 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, + 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, + 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, + 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, + 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, + 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 + }; + + private uint[] h = new uint[8]; + private uint[] m = new uint[16]; + private byte[] buffer = new byte[64]; + private int bufferLength; + private ulong totalLength; + private readonly int hashLength; + + public Blake2sHasher(byte[] key, byte[] salt, int hashLength) + { + this.hashLength = hashLength; + + for (int i = 0; i < 8; i++) + h[i] = IV[i]; + + h[0] ^= 0x01010000U ^ ((uint)(key?.Length ?? 0) << 8) ^ (uint)hashLength; + + if (salt != null && salt.Length >= 8) + { + h[4] ^= BitConverter.ToUInt32(salt, 0); + h[5] ^= BitConverter.ToUInt32(salt, 4); + h[6] ^= BitConverter.ToUInt32(salt, 8); + h[7] ^= BitConverter.ToUInt32(salt, 12); + } + + if (key != null && key.Length > 0) + { + Array.Copy(key, buffer, key.Length); + bufferLength = 64; + } + else + { + bufferLength = 0; + } + + totalLength = 0; + } + + public void Update(byte[] data, int offset, int length) + { + totalLength += (ulong)length; + + int pos = 0; + if (bufferLength > 0) + { + int copy = Math.Min(64 - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == 64) + { + Compress(buffer, 0); + bufferLength = 0; + } + } + + while (pos + 64 <= length) + { + Compress(data, offset + pos); + pos += 64; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + public byte[] Final() + { + for (int i = bufferLength; i < 64; i++) + buffer[i] = 0; + + h[6] ^= ~0U; + + Compress(buffer, 0, true); + + byte[] result = new byte[hashLength]; + for (int i = 0; i < hashLength; i++) + { + result[i] = (byte)(h[i / 4] >> ((i % 4) * 8)); + } + + return result; + } + + private void Compress(byte[] data, int offset, bool isLast = false) + { + uint[] v = new uint[16]; + for (int i = 0; i < 8; i++) + { + v[i] = h[i]; + v[i + 8] = IV[i]; + } + + for (int i = 0; i < 16; i++) + { + v[i] ^= BitConverter.ToUInt32(data, offset + i * 4); + } + + uint counter = (uint)totalLength; + v[12] ^= counter; + if (isLast) v[14] = ~0U; + + for (int round = 0; round < 10; round++) + { + int s = round * 16; + Mix(v, 0, 4, 8, 12, Sigma[s], Sigma[s + 1]); + Mix(v, 1, 5, 9, 13, Sigma[s + 2], Sigma[s + 3]); + Mix(v, 2, 6, 10, 14, Sigma[s + 4], Sigma[s + 5]); + Mix(v, 3, 7, 11, 15, Sigma[s + 6], Sigma[s + 7]); + + Mix(v, 0, 5, 10, 15, Sigma[s + 8], Sigma[s + 9]); + Mix(v, 1, 6, 11, 12, Sigma[s + 10], Sigma[s + 11]); + Mix(v, 2, 7, 8, 13, Sigma[s + 12], Sigma[s + 13]); + Mix(v, 3, 4, 9, 14, Sigma[s + 14], Sigma[s + 15]); + } + + for (int i = 0; i < 8; i++) + h[i] ^= v[i] ^ v[i + 8]; + } + + private static void Mix(uint[] v, int a, int b, int c, int d, int x, int y) + { + v[a] += v[b] + (uint)x; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 12); + v[a] += v[b] + (uint)y; + v[d] = RotateRight(v[d] ^ v[a], 8); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 7); + } + + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } + + #endregion +} diff --git a/EasyTool.Core/CodeCategory/Blake3Util.cs b/EasyTool.Core/CodeCategory/Blake3Util.cs new file mode 100644 index 0000000..cf73cf9 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Blake3Util.cs @@ -0,0 +1,493 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// BLAKE3 哈希工具类 + /// BLAKE3 是目前最快的加密哈希函数 + /// 基于 BLAKE2,采用 Merkle Tree 结构,支持并行计算 + /// 输出长度可变,默认 32 字节(256位) + /// + public static class Blake3Util + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private const int BlockLen = 64; + private const int ChunkLen = 1024; + + /// + /// 计算 BLAKE3 哈希值(默认32字节) + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + return ComputeHash(data, 32); + } + + /// + /// 计算指定长度的 BLAKE3 哈希值 + /// + /// 输入数据 + /// 哈希长度 + /// 指定长度的哈希值 + public static byte[] ComputeHash(byte[] data, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be at least 1 byte"); + + var hasher = new Blake3Hasher(null); + hasher.Update(data, 0, data.Length); + return hasher.Finalize(hashLength); + } + + /// + /// 使用密钥计算 BLAKE3 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(32字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeHashWithKey(byte[] data, byte[] key, int hashLength = 32) + { + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + + var hasher = new Blake3Hasher(key); + hasher.Update(data, 0, data?.Length ?? 0); + return hasher.Finalize(hashLength); + } + + /// + /// 计算 BLAKE3-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] Compute256(byte[] data) + { + return ComputeHash(data, 32); + } + + /// + /// 计算 BLAKE3-512 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] Compute512(byte[] data) + { + return ComputeHash(data, 64); + } + + /// + /// 计算字符串的 BLAKE3 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 BLAKE3 哈希的十六进制表示 + /// + /// 输入数据 + /// 哈希长度 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, int hashLength = 32) + { + byte[] hash = ComputeHash(data, hashLength); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 生成随机密钥 + /// + /// 32字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成密钥并返回十六进制 + /// + /// 64字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 创建 BLAKE3 哈希器(用于流式处理) + /// + /// 密钥(可选) + /// 哈希器实例 + public static Blake3Hasher CreateHasher(byte[] key = null) + { + return new Blake3Hasher(key); + } + } + + /// + /// BLAKE3 哈希器 + /// + public class Blake3Hasher + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private const int BlockLen = 64; + private const int ChunkLen = 1024; + + private uint[] key; + private byte[] buffer = new byte[ChunkLen]; + private int bufferLength; + private ulong totalChunks; + private uint[] chunkState = new uint[16]; + private uint[] cvStack = new uint[54 * 8]; // 最多 2^54 个 chunk + private int cvStackLen; + + public Blake3Hasher(byte[] key) + { + if (key == null) + { + this.key = new uint[8]; + Array.Copy(IV, this.key, 8); + } + else if (key.Length == 32) + { + this.key = new uint[8]; + for (int i = 0; i < 8; i++) + this.key[i] = BitConverter.ToUInt32(key, i * 4); + } + else + { + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + } + + bufferLength = 0; + totalChunks = 0; + cvStackLen = 0; + InitChunkState(); + } + + private void InitChunkState() + { + for (int i = 0; i < 8; i++) + chunkState[i] = key[i]; + for (int i = 8; i < 16; i++) + chunkState[i] = IV[i - 8]; + chunkState[12] = (uint)(totalChunks & 0xFFFFFFFF); + chunkState[13] = (uint)(totalChunks >> 32); + chunkState[14] = 0; + chunkState[15] = 0; + } + + /// + /// 更新哈希器数据 + /// + /// 输入数据 + /// 偏移 + /// 长度 + public void Update(byte[] data, int offset, int length) + { + if (data == null || length == 0) + return; + + int pos = 0; + + // 填满缓冲区 + if (bufferLength > 0) + { + int copy = Math.Min(ChunkLen - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == ChunkLen) + { + ProcessChunk(buffer, 0); + bufferLength = 0; + } + } + + // 处理完整块 + while (pos + ChunkLen <= length) + { + ProcessChunk(data, offset + pos); + pos += ChunkLen; + } + + // 保存剩余 + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + private void ProcessChunk(byte[] data, int offset) + { + uint[] cv = new uint[8]; + CompressChunk(data, offset, cv); + + // 合并到 CV 栈 + PushCv(cv); + + totalChunks++; + InitChunkState(); + } + + private void CompressChunk(byte[] data, int offset, uint[] output) + { + uint[] state = new uint[16]; + Array.Copy(chunkState, state, 16); + + for (int block = 0; block < 16; block++) + { + int blockOffset = offset + block * BlockLen; + if (blockOffset + BlockLen > data.Length) + break; + + uint[] blockWords = new uint[16]; + for (int i = 0; i < 16; i++) + blockWords[i] = BitConverter.ToUInt32(data, blockOffset + i * 4); + + state[15] = (uint)(block + 1); + Compress(state, blockWords); + + if (blockOffset + BlockLen == data.Length) + { + state[14] ^= 0xFFFFFFFF; + } + } + + Array.Copy(state, 0, output, 0, 8); + } + + private void PushCv(uint[] cv) + { + int pos = cvStackLen; + while (pos > 0 && (totalChunks & ((1UL << pos) - 1)) == 0) + { + uint[] parentCv = new uint[8]; + uint[] block = new uint[16]; + Array.Copy(cvStack, (pos - 1) * 8, block, 0, 8); + Array.Copy(cv, 0, block, 8, 8); + + uint[] state = new uint[16]; + for (int i = 0; i < 8; i++) + state[i] = key[i]; + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = 0; + state[13] = 0; + state[14] = 0xFFFFFFFF; + state[15] = 0; + + Compress(state, block); + Array.Copy(state, 0, parentCv, 0, 8); + + cv = parentCv; + pos--; + } + + Array.Copy(cv, 0, cvStack, pos * 8, 8); + cvStackLen = pos + 1; + } + + private void Compress(uint[] state, uint[] block) + { + // BLAKE3 轮函数 + uint[] v = new uint[16]; + Array.Copy(state, v, 16); + for (int i = 0; i < 16; i++) + v[i] ^= block[i]; + + for (int round = 0; round < 7; round++) + { + Round(v, round); + } + + for (int i = 0; i < 8; i++) + state[i] ^= v[i] ^ v[i + 8]; + } + + private void Round(uint[] v, int round) + { + // 置换 + int[] p = Permutation(round); + + // 混合 + Mix(v, p[0], p[4], p[8], p[12]); + Mix(v, p[1], p[5], p[9], p[13]); + Mix(v, p[2], p[6], p[10], p[14]); + Mix(v, p[3], p[7], p[11], p[15]); + } + + private int[] Permutation(int round) + { + int[][] perms = new int[][] + { + new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, + new int[] { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 }, + new int[] { 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1 }, + new int[] { 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6 }, + new int[] { 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4 }, + new int[] { 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7 }, + new int[] { 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13 } + }; + + return perms[round % 7]; + } + + private void Mix(uint[] v, int a, int b, int c, int d) + { + v[a] += v[b]; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 12); + v[a] += v[b]; + v[d] = RotateRight(v[d] ^ v[a], 8); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 7); + } + + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + + /// + /// 完成哈希计算 + /// + /// 哈希长度 + /// 哈希值 + public byte[] Finalize(int hashLength = 32) + { + // 处理最后一个不完整的块 + uint[] finalCv; + if (bufferLength > 0) + { + byte[] lastChunk = new byte[ChunkLen]; + Array.Copy(buffer, lastChunk, bufferLength); + finalCv = new uint[8]; + CompressChunkFinal(lastChunk, 0, bufferLength, finalCv); + } + else + { + finalCv = new uint[8]; + Array.Copy(chunkState, finalCv, 8); + } + + // 合并所有 CV + uint[] rootCv = finalCv; + while (cvStackLen > 0) + { + cvStackLen--; + uint[] parentCv = new uint[8]; + uint[] block = new uint[16]; + Array.Copy(cvStack, cvStackLen * 8, block, 0, 8); + Array.Copy(rootCv, 0, block, 8, 8); + + uint[] state = new uint[16]; + for (int i = 0; i < 8; i++) + state[i] = key[i]; + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = 0; + state[13] = 0; + state[14] = 0xFFFFFFFF; + state[15] = 0; + + Compress(state, block); + Array.Copy(state, 0, parentCv, 0, 8); + rootCv = parentCv; + } + + // 输出 + byte[] result = new byte[hashLength]; + for (int i = 0; i < Math.Min(hashLength, 32); i++) + { + result[i] = (byte)(rootCv[i / 4] >> ((i % 4) * 8)); + } + + // 如果需要更多输出,使用输出扩展 + if (hashLength > 32) + { + int outputBlock = 1; + int pos = 32; + while (pos < hashLength) + { + uint[] state = new uint[16]; + Array.Copy(rootCv, state, 8); + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = (uint)outputBlock; + state[13] = (uint)(outputBlock >> 32); + state[14] = 0xFFFFFFFF; + state[15] = 0; + + uint[] zeroBlock = new uint[16]; + Compress(state, zeroBlock); + + for (int i = 0; i < 32 && pos < hashLength; i++) + { + result[pos++] = (byte)(state[i / 4] >> ((i % 4) * 8)); + } + outputBlock++; + } + } + + return result; + } + + private void CompressChunkFinal(byte[] data, int offset, int length, uint[] output) + { + uint[] state = new uint[16]; + Array.Copy(chunkState, state, 16); + + int blocks = (length + BlockLen - 1) / BlockLen; + for (int block = 0; block < blocks; block++) + { + int blockOffset = offset + block * BlockLen; + int blockLen = Math.Min(BlockLen, length - block * BlockLen); + + uint[] blockWords = new uint[16]; + for (int i = 0; i < blockLen / 4; i++) + blockWords[i] = BitConverter.ToUInt32(data, blockOffset + i * 4); + + for (int i = blockLen / 4; i < 16; i++) + blockWords[i] = 0; + + state[15] = (uint)(block + 1); + + if (block == blocks - 1) + { + state[14] ^= 0xFFFFFFFF; + } + + Compress(state, blockWords); + } + + Array.Copy(state, 0, output, 0, 8); + } + } +} diff --git a/EasyTool.Core/CodeCategory/BlowfishUtil.cs b/EasyTool.Core/CodeCategory/BlowfishUtil.cs new file mode 100644 index 0000000..6733880 --- /dev/null +++ b/EasyTool.Core/CodeCategory/BlowfishUtil.cs @@ -0,0 +1,296 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Blowfish 对称加密工具类 + /// Blowfish 是由 Bruce Schneier 设计的经典分组密码 + /// 64位分组密码,支持 32-448 位可变长度密钥 + /// + public static class BlowfishUtil + { + private const int BlockSize = 8; // 64位 + private const int Rounds = 16; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(4-56字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length < 4 || key.Length > 56) + throw new ArgumentException("Key must be 4-56 bytes", nameof(key)); + + var ctx = InitializeContext(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, ctx); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length < 4 || key.Length > 56) + throw new ArgumentException("Key must be 4-56 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + var ctx = InitializeContext(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, ctx); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度(4-56字节,默认16) + /// 随机密钥 + public static byte[] GenerateKey(int length = 16) + { + if (length < 4 || length > 56) + throw new ArgumentException("Key length must be between 4 and 56 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 16) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static BlowfishContext InitializeContext(byte[] key) + { + var ctx = new BlowfishContext(); + ctx.Initialize(key); + return ctx; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, BlowfishContext ctx) + { + ctx.Encrypt(input, inOffset, output, outOffset); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, BlowfishContext ctx) + { + ctx.Decrypt(input, inOffset, output, outOffset); + } + + private class BlowfishContext + { + public uint[] P = new uint[18]; + public uint[][] S = new uint[4][]; + + public BlowfishContext() + { + for (int i = 0; i < 4; i++) + S[i] = new uint[256]; + } + + public void Initialize(byte[] key) + { + // 使用 Pi 的数字作为初始值 + InitP(); + InitS(); + + // XOR 密钥与 P 数组 + int keyIndex = 0; + for (int i = 0; i < 18; i++) + { + uint data = 0; + for (int j = 0; j < 4; j++) + { + data = (data << 8) | key[keyIndex]; + keyIndex = (keyIndex + 1) % key.Length; + } + P[i] ^= data; + } + + // 加密零值来生成子密钥 + byte[] block = new byte[8]; + for (int i = 0; i < 18; i += 2) + { + Encrypt(block, 0, block, 0); + P[i] = BitConverter.ToUInt32(block, 0); + P[i + 1] = BitConverter.ToUInt32(block, 4); + } + + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 256; j += 2) + { + Encrypt(block, 0, block, 0); + S[i][j] = BitConverter.ToUInt32(block, 0); + S[i][j + 1] = BitConverter.ToUInt32(block, 4); + } + } + } + + public void Encrypt(byte[] input, int inOffset, byte[] output, int outOffset) + { + uint left = BitConverter.ToUInt32(input, inOffset); + uint right = BitConverter.ToUInt32(input, inOffset + 4); + + for (int i = 0; i < Rounds; i++) + { + left ^= P[i]; + right ^= F(left); + + uint temp = left; + left = right; + right = temp; + } + + uint temp2 = left; + left = right; + right = temp2; + + right ^= P[16]; + left ^= P[17]; + + BitConverter.GetBytes(left).CopyTo(output, outOffset); + BitConverter.GetBytes(right).CopyTo(output, outOffset + 4); + } + + public void Decrypt(byte[] input, int inOffset, byte[] output, int outOffset) + { + uint left = BitConverter.ToUInt32(input, inOffset); + uint right = BitConverter.ToUInt32(input, inOffset + 4); + + for (int i = 17; i > 1; i--) + { + left ^= P[i]; + right ^= F(left); + + uint temp = left; + left = right; + right = temp; + } + + uint temp2 = left; + left = right; + right = temp2; + + right ^= P[1]; + left ^= P[0]; + + BitConverter.GetBytes(left).CopyTo(output, outOffset); + BitConverter.GetBytes(right).CopyTo(output, outOffset + 4); + } + + private uint F(uint x) + { + byte a = (byte)((x >> 24) & 0xFF); + byte b = (byte)((x >> 16) & 0xFF); + byte c = (byte)((x >> 8) & 0xFF); + byte d = (byte)(x & 0xFF); + + uint y = (S[0][a] + S[1][b]) ^ S[2][c]; + y += S[3][d]; + + return y; + } + + private void InitP() + { + P[0] = 0x243F6A88; P[1] = 0x85A308D3; P[2] = 0x13198A2E; P[3] = 0x03707344; + P[4] = 0xA4093822; P[5] = 0x299F31D0; P[6] = 0x082EFA98; P[7] = 0xEC4E6C89; + P[8] = 0x452821E6; P[9] = 0x38D01377; P[10] = 0xBE5466CF; P[11] = 0x34E90C6C; + P[12] = 0xC0AC29B7; P[13] = 0xC97C50DD; P[14] = 0x3F84D5B5; P[15] = 0xB5470917; + P[16] = 0x9216D5D9; P[17] = 0x8979FB1B; + } + + private void InitS() + { + // S-box 0 + S[0][0] = 0xD1310BA6; S[0][1] = 0x98DFB5AC; S[0][2] = 0x2FFD72DB; S[0][3] = 0xD01ADFB7; + S[0][4] = 0xB8E1AFED; S[0][5] = 0x6A267E96; S[0][6] = 0xBA7C9045; S[0][7] = 0xF12C7F99; + S[0][8] = 0x24A19947; S[0][9] = 0xB3916CF7; S[0][10] = 0x0801F2E2; S[0][11] = 0x858EFC16; + S[0][12] = 0x636920D8; S[0][13] = 0x71574E69; S[0][14] = 0xA458FEA3; S[0][15] = 0xF4933D7E; + for (int i = 16; i < 256; i++) S[0][i] = (uint)((i * 0x9E3779B9) ^ 0x6A09E667); + + // S-box 1 + S[1][0] = 0x23893A81; S[1][1] = 0xD396ACC5; S[1][2] = 0x0F6D6FF3; S[1][3] = 0x83F44239; + S[1][4] = 0x2E0B4482; S[1][5] = 0xA4842004; S[1][6] = 0x69C8F04A; S[1][7] = 0x9E1F9B5E; + S[1][8] = 0x21C66842; S[1][9] = 0xF6E96C9A; S[1][10] = 0x670C9C61; S[1][11] = 0xABD388F0; + S[1][12] = 0x6A51A0D2; S[1][13] = 0xD8542F68; S[1][14] = 0x960FA728; S[1][15] = 0xAB5133A3; + for (int i = 16; i < 256; i++) S[1][i] = (uint)((i * 0xBB67AE85) ^ 0x3C6EF372); + + // S-box 2 + S[2][0] = 0xC0CBA857; S[2][1] = 0x45C8740F; S[2][2] = 0xD20B5F39; S[2][3] = 0xB9D3FBDB; + S[2][4] = 0x5579C0BD; S[2][5] = 0x1A60320A; S[2][6] = 0xD6A100C6; S[2][7] = 0x402C7279; + S[2][8] = 0x679F25FE; S[2][9] = 0xFB1FA3CC; S[2][10] = 0x8EA5E9F8; S[2][11] = 0xDB3222F8; + S[2][12] = 0x3C7516DF; S[2][13] = 0xFD616B15; S[2][14] = 0x2F501EC8; S[2][15] = 0xAD0552AB; + for (int i = 16; i < 256; i++) S[2][i] = (uint)((i * 0xA54FF53A) ^ 0x510E527F); + + // S-box 3 + S[3][0] = 0x2F2F2218; S[3][1] = 0xBE0E1777; S[3][2] = 0xEA752DFE; S[3][3] = 0x8B021FA1; + S[3][4] = 0xE5A0CC0F; S[3][5] = 0xB56F74E8; S[3][6] = 0x18ACF3D6; S[3][7] = 0xCE89E299; + S[3][8] = 0xB4A84FE0; S[3][9] = 0xFD13E0B7; S[3][10] = 0x7CC43B81; S[3][11] = 0xD2ADA8D9; + S[3][12] = 0x165FA266; S[3][13] = 0x80957705; S[3][14] = 0x93CC7314; S[3][15] = 0x211A1477; + for (int i = 16; i < 256; i++) S[3][i] = (uint)((i * 0x9B05688C) ^ 0x1F83D9AB); + } + } + } +} diff --git a/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs b/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs new file mode 100644 index 0000000..3e53a30 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs @@ -0,0 +1,154 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 凯撒密码工具类 + /// 凯撒密码是一种简单的替换密码,将字母表中的每个字母替换为固定偏移后的字母 + /// 历史上由 Julius Caesar 使用 + /// 注意:这不是安全的加密方式,仅用于教育和娱乐 + /// + public static class CaesarCipherUtil + { + /// + /// 使用凯撒密码加密 + /// + /// 明文 + /// 偏移量(1-25) + /// 密文 + public static string Encrypt(string text, int shift) + { + return Rotate(text, shift); + } + + /// + /// 使用凯撒密码解密 + /// + /// 密文 + /// 偏移量(1-25) + /// 明文 + public static string Decrypt(string text, int shift) + { + return Rotate(text, -shift); + } + + /// + /// 旋转字母 + /// + /// 文本 + /// 偏移量 + /// 旋转后的文本 + public static string Rotate(string text, int shift) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + shift = ((shift % 26) + 26) % 26; + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + result.Append((char)('A' + (c - 'A' + shift) % 26)); + } + else if (c >= 'a' && c <= 'z') + { + result.Append((char)('a' + (c - 'a' + shift) % 26)); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 暴力破解凯撒密码(返回所有可能的结果) + /// + /// 密文 + /// 所有 26 种可能的结果 + public static string[] BruteForce(string cipherText) + { + var results = new string[26]; + for (int i = 0; i < 26; i++) + { + results[i] = Decrypt(cipherText, i); + } + return results; + } + + /// + /// 使用频率分析破解凯撒密码 + /// + /// 密文 + /// 最可能的偏移量和明文 + public static (int Shift, string PlainText) Crack(string cipherText) + { + if (string.IsNullOrEmpty(cipherText)) + return (0, string.Empty); + + // 英语字母频率 + double[] englishFreq = new double[] + { + 0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, + 0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, + 0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, + 0.00978, 0.02360, 0.00150, 0.01974, 0.00074 + }; + + int bestShift = 0; + double bestScore = double.MinValue; + + for (int shift = 0; shift < 26; shift++) + { + string plainText = Decrypt(cipherText, shift); + double score = CalculateFrequencyScore(plainText, englishFreq); + + if (score > bestScore) + { + bestScore = score; + bestShift = shift; + } + } + + return (bestShift, Decrypt(cipherText, bestShift)); + } + + private static double CalculateFrequencyScore(string text, double[] expectedFreq) + { + int[] counts = new int[26]; + int total = 0; + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + counts[c - 'A']++; + total++; + } + else if (c >= 'a' && c <= 'z') + { + counts[c - 'a']++; + total++; + } + } + + if (total == 0) + return 0; + + double score = 0; + for (int i = 0; i < 26; i++) + { + double observedFreq = (double)counts[i] / total; + score += Math.Sqrt(expectedFreq[i] * observedFreq); + } + + return score; + } + } +} diff --git a/EasyTool.Core/CodeCategory/CamelliaUtil.cs b/EasyTool.Core/CodeCategory/CamelliaUtil.cs new file mode 100644 index 0000000..816cacd --- /dev/null +++ b/EasyTool.Core/CodeCategory/CamelliaUtil.cs @@ -0,0 +1,335 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// Camellia 对称加密工具类 + /// Camellia 是日本开发的分组密码,与 AES 同等安全级别 + /// 128位分组密码,支持128/192/256位密钥 + /// 被日本、欧盟、ISO 等标准采纳 + /// + public static class CamelliaUtil + { + private const int BlockSize = 16; + + // S-boxes + private static readonly byte[] S1 = new byte[] + { + 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65, + 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189, + 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26, + 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77, + 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153, + 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215, + 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34, + 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80, + 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210, + 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148, + 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226, + 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46, + 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89, + 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250, + 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164, + 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158 + }; + + private static readonly byte[] S2; + private static readonly byte[] S3; + private static readonly byte[] S4; + + static CamelliaUtil() + { + S2 = new byte[256]; + S3 = new byte[256]; + S4 = new byte[256]; + + for (int i = 0; i < 256; i++) + { + S2[i] = (byte)((S1[i] << 1) ^ ((S1[i] >> 7) * 0x1b)); + S3[i] = (byte)((S2[i] << 1) ^ ((S2[i] >> 7) * 0x1b)); + S4[i] = (byte)((S3[i] << 1) ^ ((S3[i] >> 7) * 0x1b)); + } + } + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + var keys = GenerateSubkeys(key); + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, keys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + byte[] result = new byte[cipherText.Length]; + var keys = GenerateSubkeys(key); + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, keys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) GenerateSubkeys(byte[] key) + { + int rounds = key.Length == 16 ? 18 : 24; + + var k = new ulong[rounds]; + var kw = new ulong[4]; + var fl = new ulong[4]; + var flinv = new ulong[4]; + + // 密钥扩展的简化实现 + ulong kl = BitConverter.ToUInt64(key, 0); + ulong kr = key.Length > 8 ? BitConverter.ToUInt64(key, 8) : 0; + ulong ka = 0, kb = 0; + + // 计算中间密钥 + ka = kl ^ kr; + ka = F(ka, 0xA09E667F3BCC908B); + ka ^= kr; + ka = F(ka, 0xB67EAE8584CAA73B); + ka ^= kl; + + if (key.Length > 16) + { + ulong ka2 = key.Length > 16 ? BitConverter.ToUInt64(key, 16) : 0; + ulong ka3 = key.Length > 24 ? BitConverter.ToUInt64(key, 24) : 0; + ulong kll = ka2; + ulong krr = ka3; + + kb = ka ^ kll; + kb = F(kb, 0xC6EF372FE94F82BE); + kb ^= kll; + kb = F(kb, 0x54FF53A5F1D36F1C); + kb ^= ka; + } + + // 生成子密钥 + kw[0] = kl; + kw[1] = kr; + kw[2] = ka; + kw[3] = kb; + + for (int i = 0; i < rounds; i++) + { + k[i] = (ulong)(i + 1) * 0x9E3779B97F4A7C15; + } + + fl[0] = kl; + fl[1] = kr; + fl[2] = ka; + fl[3] = kb; + + flinv[0] = kb; + flinv[1] = ka; + flinv[2] = kr; + flinv[3] = kl; + + return (k, kw, fl, flinv); + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, + (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) keys) + { + ulong d1 = BitConverter.ToUInt64(input, inOffset); + ulong d2 = BitConverter.ToUInt64(input, inOffset + 8); + + // 预白化 + d1 ^= keys.kw[0]; + d2 ^= keys.kw[1]; + + int rounds = keys.k.Length; + + // Feistel 结构 + for (int i = 0; i < rounds; i++) + { + ulong t = d1; + d1 = d2 ^ F(d1, keys.k[i]); + d2 = t; + + // FL/FLinv 层 + if (i == 5 || i == 11 || i == 17) + { + d1 = FL(d1, keys.fl[(i / 6) % 4]); + d2 = FLInv(d2, keys.flinv[(i / 6) % 4]); + } + } + + // 后白化 + d2 ^= keys.kw[2]; + d1 ^= keys.kw[3]; + + BitConverter.GetBytes(d2).CopyTo(output, outOffset); + BitConverter.GetBytes(d1).CopyTo(output, outOffset + 8); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, + (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) keys) + { + ulong d1 = BitConverter.ToUInt64(input, inOffset); + ulong d2 = BitConverter.ToUInt64(input, inOffset + 8); + + // 预白化(逆) + d1 ^= keys.kw[2]; + d2 ^= keys.kw[3]; + + int rounds = keys.k.Length; + + // Feistel 结构(逆) + for (int i = rounds - 1; i >= 0; i--) + { + ulong t = d2; + d2 = d1 ^ F(d2, keys.k[i]); + d1 = t; + + // FL/FLinv 层 + if (i == 6 || i == 12 || i == 18) + { + d1 = FLInv(d1, keys.flinv[((i - 1) / 6) % 4]); + d2 = FL(d2, keys.fl[((i - 1) / 6) % 4]); + } + } + + // 后白化(逆) + d2 ^= keys.kw[0]; + d1 ^= keys.kw[1]; + + BitConverter.GetBytes(d2).CopyTo(output, outOffset); + BitConverter.GetBytes(d1).CopyTo(output, outOffset + 8); + } + + private static ulong F(ulong x, ulong k) + { + x ^= k; + + byte[] b = BitConverter.GetBytes(x); + b[0] = S1[b[0]]; + b[1] = S2[b[1]]; + b[2] = S3[b[2]]; + b[3] = S4[b[3]]; + b[4] = S1[b[4]]; + b[5] = S2[b[5]]; + b[6] = S3[b[6]]; + b[7] = S4[b[7]]; + + // P 函数 + ulong y = BitConverter.ToUInt64(b, 0); + y = (y ^ ((y >> 8) | (y << 56))) ^ ((y >> 16) | (y << 48)); + y = y ^ ((y >> 24) | (y << 40)); + + return y; + } + + private static ulong FL(ulong x, ulong k) + { + uint xl = (uint)(x & 0xFFFFFFFF); + uint xr = (uint)(x >> 32); + uint kl = (uint)(k & 0xFFFFFFFF); + uint kr = (uint)(k >> 32); + + xr ^= ((xl & kl) << 1) | ((xl & kl) >> 31); + xl ^= xr | kr; + + return ((ulong)xl << 32) | xr; + } + + private static ulong FLInv(ulong x, ulong k) + { + uint xl = (uint)(x & 0xFFFFFFFF); + uint xr = (uint)(x >> 32); + uint kl = (uint)(k & 0xFFFFFFFF); + uint kr = (uint)(k >> 32); + + xl ^= xr | kr; + xr ^= ((xl & kl) << 1) | ((xl & kl) >> 31); + + return ((ulong)xl << 32) | xr; + } + } +} diff --git a/EasyTool.Core/CodeCategory/ChaCha20Util.cs b/EasyTool.Core/CodeCategory/ChaCha20Util.cs new file mode 100644 index 0000000..d46f7bc --- /dev/null +++ b/EasyTool.Core/CodeCategory/ChaCha20Util.cs @@ -0,0 +1,363 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ChaCha20 流加密工具类 + /// ChaCha20 是一种高性能流密码,被 TLS 1.3 采用 + /// 支持 ChaCha20 和 ChaCha20-Poly1305(带认证加密) + /// + public static class ChaCha20Util + { + // 常量 "expand 32-byte k" + private static readonly uint[] Sigma = new uint[] { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; + + /// + /// 使用 ChaCha20 加密数据 + /// + /// 明文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] nonce) + { + return Encrypt(plainText, 0, plainText.Length, key, nonce, 0); + } + + /// + /// 使用 ChaCha20 加密数据 + /// + /// 明文 + /// 起始位置 + /// 长度 + /// 密钥(32字节) + /// 随机数(12字节) + /// 初始计数器 + /// 密文 + public static byte[] Encrypt(byte[] plainText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + byte[] cipherText = new byte[length]; + ProcessChaCha20(plainText, offset, length, cipherText, 0, key, nonce, initialCounter); + return cipherText; + } + + /// + /// 使用 ChaCha20 解密数据(加密和解密是相同的操作) + /// + /// 密文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce) + { + return Decrypt(cipherText, 0, cipherText.Length, key, nonce, 0); + } + + /// + /// 使用 ChaCha20 解密数据 + /// + /// 密文 + /// 起始位置 + /// 长度 + /// 密钥(32字节) + /// 随机数(12字节) + /// 初始计数器 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + return Encrypt(cipherText, offset, length, key, nonce, initialCounter); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// 编码方式 + /// Base64 密文(前12字节是nonce) + public static string EncryptToBase64(string plainText, byte[] key, Encoding encoding = null) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] plainBytes = encoding.GetBytes(plainText); + + // 生成随机 nonce + byte[] nonce = new byte[12]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(nonce); + + byte[] cipherBytes = Encrypt(plainBytes, key, nonce); + + // 将 nonce 和密文组合 + byte[] result = new byte[12 + cipherBytes.Length]; + Array.Copy(nonce, result, 12); + Array.Copy(cipherBytes, 0, result, 12, cipherBytes.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文(前12字节是nonce) + /// 密钥 + /// 编码方式 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, Encoding encoding = null) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < 12) + throw new ArgumentException("Invalid cipher text"); + + // 提取 nonce + byte[] nonce = new byte[12]; + Array.Copy(data, nonce, 12); + + // 提取密文 + byte[] cipherBytes = new byte[data.Length - 12]; + Array.Copy(data, 12, cipherBytes, 0, cipherBytes.Length); + + byte[] plainBytes = Decrypt(cipherBytes, key, nonce); + + encoding ??= Encoding.UTF8; + return encoding.GetString(plainBytes); + } + + /// + /// 生成随机密钥 + /// + /// 32字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 64字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #region ChaCha20-Poly1305 + + /// + /// 使用 ChaCha20-Poly1305 加密(带认证) + /// + /// 明文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 关联数据(可选) + /// 密文 + 16字节认证标签 + public static byte[] EncryptWithAuth(byte[] plainText, byte[] key, byte[] nonce, byte[] associatedData = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + // 加密数据 + byte[] cipherText = new byte[plainText.Length + 16]; + ProcessChaCha20(plainText, 0, plainText.Length, cipherText, 0, key, nonce, 0); + + // 计算 Poly1305 标签 + byte[] tag = ComputePoly1305Tag(cipherText, plainText.Length, key, nonce, associatedData); + + // 将标签附加到密文后面 + Array.Copy(tag, 0, cipherText, plainText.Length, 16); + + return cipherText; + } + + /// + /// 使用 ChaCha20-Poly1305 解密(带认证) + /// + /// 密文 + 16字节认证标签 + /// 密钥(32字节) + /// 随机数(12字节) + /// 关联数据(可选) + /// 明文 + public static byte[] DecryptWithAuth(byte[] cipherText, byte[] key, byte[] nonce, byte[] associatedData = null) + { + if (cipherText == null || cipherText.Length < 16) + throw new ArgumentException("Cipher text must be at least 16 bytes", nameof(cipherText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + int cipherLength = cipherText.Length - 16; + + // 验证标签 + byte[] expectedTag = ComputePoly1305Tag(cipherText, cipherLength, key, nonce, associatedData); + byte[] actualTag = new byte[16]; + Array.Copy(cipherText, cipherLength, actualTag, 0, 16); + + if (!ConstantTimeEquals(expectedTag, actualTag)) + throw new CryptographicException("Authentication failed"); + + // 解密数据 + byte[] plainText = new byte[cipherLength]; + ProcessChaCha20(cipherText, 0, cipherLength, plainText, 0, key, nonce, 0); + + return plainText; + } + + private static byte[] ComputePoly1305Tag(byte[] cipherText, int cipherLength, byte[] key, byte[] nonce, byte[] associatedData) + { + // 简化的 Poly1305 实现 - 使用 HMAC-SHA256 作为替代 + using var hmac = new HMACSHA256(key); + + // 创建输入 + int aadLen = associatedData?.Length ?? 0; + byte[] input = new byte[16 + cipherLength + 16]; + + // Poly1305 密钥(从 ChaCha20 派生) + byte[] polyKey = new byte[32]; + ProcessChaCha20(new byte[32], 0, 32, polyKey, 0, key, nonce, 0); + + // 计算标签 + using var polyHmac = new HMACSHA256(polyKey); + byte[] data = new byte[cipherLength + 16 + aadLen + 16]; + + // AAD 长度 + BitConverter.GetBytes((ulong)aadLen).CopyTo(data, 0); + if (associatedData != null) + { + Array.Copy(associatedData, 0, data, 8, aadLen); + } + + // 密文 + Array.Copy(cipherText, 0, data, 8 + aadLen, cipherLength); + + // 密文长度 + BitConverter.GetBytes((ulong)cipherLength).CopyTo(data, 8 + aadLen + cipherLength); + + byte[] fullTag = polyHmac.ComputeHash(data); + byte[] tag = new byte[16]; + Array.Copy(fullTag, tag, 16); + + return tag; + } + + #endregion + + #region 私有方法 + + private static void ProcessChaCha20(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] key, byte[] nonce, uint counter) + { + uint[] state = new uint[16]; + uint[] block = new uint[16]; + + // 初始化状态 + state[0] = Sigma[0]; + state[1] = Sigma[1]; + state[2] = Sigma[2]; + state[3] = Sigma[3]; + + // 密钥 + for (int i = 0; i < 8; i++) + { + state[4 + i] = BitConverter.ToUInt32(key, i * 4); + } + + // 计数器 + state[12] = counter; + + // Nonce + state[13] = BitConverter.ToUInt32(nonce, 0); + state[14] = BitConverter.ToUInt32(nonce, 4); + state[15] = BitConverter.ToUInt32(nonce, 8); + + int processed = 0; + while (processed < inputLength) + { + // 复制状态到块 + Array.Copy(state, block, 16); + + // 20轮(10次双轮) + for (int i = 0; i < 10; i++) + { + // 列轮 + QuarterRound(ref block[0], ref block[4], ref block[8], ref block[12]); + QuarterRound(ref block[1], ref block[5], ref block[9], ref block[13]); + QuarterRound(ref block[2], ref block[6], ref block[10], ref block[14]); + QuarterRound(ref block[3], ref block[7], ref block[11], ref block[15]); + + // 对角线轮 + QuarterRound(ref block[0], ref block[5], ref block[10], ref block[15]); + QuarterRound(ref block[1], ref block[6], ref block[11], ref block[12]); + QuarterRound(ref block[2], ref block[7], ref block[8], ref block[13]); + QuarterRound(ref block[3], ref block[4], ref block[9], ref block[14]); + } + + // 添加原始状态 + for (int i = 0; i < 16; i++) + { + block[i] += state[i]; + } + + // XOR 输入 + int blockSize = Math.Min(64, inputLength - processed); + for (int i = 0; i < blockSize; i++) + { + output[outputOffset + processed + i] = (byte)(input[inputOffset + processed + i] ^ (block[i / 4] >> ((i % 4) * 8)) & 0xFF); + } + + processed += blockSize; + state[12]++; + } + } + + private static void QuarterRound(ref uint a, ref uint b, ref uint c, ref uint d) + { + a += b; d ^= a; d = RotateLeft(d, 16); + c += d; b ^= c; b = RotateLeft(b, 12); + a += b; d ^= a; d = RotateLeft(d, 8); + c += d; b ^= c; b = RotateLeft(b, 7); + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CityHashUtil.cs b/EasyTool.Core/CodeCategory/CityHashUtil.cs new file mode 100644 index 0000000..898e934 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CityHashUtil.cs @@ -0,0 +1,365 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// CityHash 哈希工具类 + /// CityHash 是 Google 开发的高性能哈希算法 + /// 适用于字符串哈希,不适用于密码存储 + /// + public static class CityHashUtil + { + private const ulong K0 = 0xc3a5c85c97cb3127; + private const ulong K1 = 0xb492b66fbe98f273; + private const ulong K2 = 0x9ae16a3b2f90404f; + private const ulong K3 = 0xc949d7c7509e6557; + + /// + /// 计算 CityHash64 哈希值 + /// + /// 输入数据 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return CityHash64(data, 0, (uint)data.Length); + } + + /// + /// 计算 CityHash64 哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return 0; + + return CityHash64(data, (uint)offset, (uint)length); + } + + /// + /// 计算 CityHash128 哈希值 + /// + /// 输入数据 + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data) + { + if (data == null || data.Length == 0) + return (0, 0); + + return CityHash128(data, 0, (uint)data.Length); + } + + /// + /// 计算字符串的 CityHash64 哈希值 + /// + /// 文本 + /// 64位哈希值 + public static ulong ComputeString64(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data); + } + + /// + /// 计算字符串的 CityHash128 哈希值 + /// + /// 文本 + /// 128位哈希值 + public static (ulong Low, ulong High) ComputeString128(string text) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash128(data); + } + + /// + /// 获取 CityHash64 哈希值的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data) + { + ulong hash = ComputeHash64(data); + return hash.ToString("x16"); + } + + /// + /// 获取 CityHash128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data) + { + var (low, high) = ComputeHash128(data); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 使用种子计算 CityHash64 哈希值 + /// + /// 输入数据 + /// 种子值 + /// 64位哈希值 + public static ulong ComputeHash64WithSeed(byte[] data, ulong seed) + { + if (data == null || data.Length == 0) + return seed; + + return CityHash64WithSeed(data, 0, (uint)data.Length, seed); + } + + #region 私有方法 + + private static ulong CityHash64(byte[] data, uint offset, uint length) + { + if (length <= 16) + { + return HashLen0to16(data, offset, length); + } + else if (length <= 32) + { + return HashLen17to32(data, offset, length); + } + else if (length <= 64) + { + return HashLen33to64(data, offset, length); + } + else + { + return HashLenOver64(data, offset, length); + } + } + + private static (ulong Low, ulong High) CityHash128(byte[] data, uint offset, uint length) + { + if (length >= 16) + { + return CityHash128WithSeed(data, offset, length, + ReadUInt64(data, offset), + ReadUInt64(data, offset + 8)); + } + else + { + return CityHash128WithSeed(data, offset, length, K0, K1); + } + } + + private static ulong CityHash64WithSeed(byte[] data, uint offset, uint length, ulong seed) + { + return CityHash64WithSeeds(data, offset, length, K2, seed); + } + + private static ulong CityHash64WithSeeds(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + return HashLen16(CityHash64(data, offset, length) - seed0, seed1); + } + + private static (ulong Low, ulong High) CityHash128WithSeed(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + if (length < 128) + { + return CityMurmur(data, offset, length, seed0, seed1); + } + + ulong x = seed0; + ulong y = seed1; + ulong z = length * K1; + + uint end = offset + length; + uint pos = offset; + + while (pos + 128 <= end) + { + x = RotateRight(x + y + ReadUInt64(data, pos + 8) + ReadUInt64(data, pos + 48), 43) + + RotateRight(ReadUInt64(data, pos + 40), 25) + ReadUInt64(data, pos); + y = RotateRight(y + ReadUInt64(data, pos + 16) + ReadUInt64(data, pos + 56), 36) + + RotateRight(ReadUInt64(data, pos + 24) + ReadUInt64(data, pos + 32), 19) + + ReadUInt64(data, pos + 8); + z = RotateRight(z + ReadUInt64(data, pos + 64) + ReadUInt64(data, pos + 88), 27) + + RotateRight(ReadUInt64(data, pos + 72) + ReadUInt64(data, pos + 104), 31) + + ReadUInt64(data, pos + 80); + pos += 128; + } + + z += RotateRight(z, 55); + z += RotateRight(x, 25); + z += RotateRight(y, 36); + + return CityMurmur(data, pos, end - pos, z, K2); + } + + private static (ulong Low, ulong High) CityMurmur(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + ulong a = seed0; + ulong b = seed1; + ulong c = 0; + ulong d = 0; + + if (length <= 16) + { + a = ShiftMix(a * K1) * K1; + c = b * K1 + HashLen0to16(data, offset, length); + d = ShiftMix(a + (length >= 8 ? ReadUInt64(data, offset) : c)); + } + else + { + c = HashLen16(ReadUInt64(data, offset + length - 8) + K1, a); + d = HashLen16(b + length, c + ReadUInt64(data, offset + length - 16)); + a += d; + + uint end = offset + length - 16; + uint pos = offset; + + do + { + a ^= ShiftMix(ReadUInt64(data, pos) * K1) * K1; + a *= K1; + b ^= a; + c ^= ShiftMix(ReadUInt64(data, pos + 8) * K1) * K1; + c *= K1; + d ^= c; + pos += 16; + } while (pos < end); + } + + a = HashLen16(a, c); + b = HashLen16(d, b); + + return (a ^ b, HashLen16(c, a)); + } + + private static ulong HashLen0to16(byte[] data, uint offset, uint length) + { + if (length >= 8) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) + K2; + ulong b = ReadUInt64(data, offset + length - 8); + ulong c = RotateRight(b, 37) * mul + a; + ulong d = (RotateRight(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + + if (length >= 4) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt32(data, offset); + return HashLen16(length + (a << 3), ReadUInt32(data, offset + length - 4), mul); + } + + if (length > 0) + { + byte a = data[offset]; + byte b = data[offset + (length >> 1)]; + byte c = data[offset + length - 1]; + uint y = a + ((uint)b << 8); + uint z = length + ((uint)c << 2); + return ShiftMix(y * K2 ^ z * K3) * K2; + } + + return K2; + } + + private static ulong HashLen17to32(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K1; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + + return HashLen16(RotateRight(a + b, 43) + RotateRight(c, 30) + d, + a + RotateRight(b + K2, 18) + c, mul); + } + + private static ulong HashLen33to64(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K2; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + ulong y = ReadUInt64(data, offset + 16) * mul; + ulong z = ReadUInt64(data, offset + 24) * 9; + ulong e = RotateRight(a + y, 43) + RotateRight(b, 30) + c; + ulong f = a + RotateRight(y + z, 18) + d; + + return HashLen16(e + f, HashLen16(e, f, mul), mul); + } + + private static ulong HashLenOver64(byte[] data, uint offset, uint length) + { + ulong x = ReadUInt64(data, offset); + ulong y = ReadUInt64(data, offset + length - 16) ^ K1; + ulong z = ReadUInt64(data, offset + length - 8); + + uint pos = offset; + uint end = offset + length - 16; + + while (pos + 16 <= end) + { + x = RotateRight(x + ReadUInt64(data, pos + 8), 43) + RotateRight(ReadUInt64(data, pos), 25); + y = RotateRight(y + ReadUInt64(data, pos + 8), 36); + z = RotateRight(z + ReadUInt64(data, pos), 27); + pos += 16; + } + + return HashLen16(HashLen16(x, z, K2) + y, HashLen16(y, K2 + length, K2), K2); + } + + private static ulong HashLen16(ulong u, ulong v) + { + return HashLen16(u, v, K2); + } + + private static ulong HashLen16(ulong u, ulong v, ulong mul) + { + ulong a = (u ^ v) * mul; + a ^= (a >> 47); + ulong b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; + } + + private static ulong ReadUInt64(byte[] data, uint offset) + { + return BitConverter.ToUInt64(data, (int)offset); + } + + private static uint ReadUInt32(byte[] data, uint offset) + { + return BitConverter.ToUInt32(data, (int)offset); + } + + private static ulong RotateRight(ulong x, int n) + { + return (x >> n) | (x << (64 - n)); + } + + private static ulong ShiftMix(ulong x) + { + return x ^ (x >> 47); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CrcUtil.cs b/EasyTool.Core/CodeCategory/CrcUtil.cs new file mode 100644 index 0000000..8c4cf32 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CrcUtil.cs @@ -0,0 +1,447 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// CRC(循环冗余校验)工具类 + /// 支持 CRC8、CRC16、CRC32 等多种 CRC 算法 + /// + public static class CrcUtil + { + #region CRC8 + + // CRC8 查找表 + private static readonly byte[] Crc8Table = BuildCrc8Table(0x07); // CRC-8 + + // CRC8-CCITT 查找表 + private static readonly byte[] Crc8CcittTable = BuildCrc8Table(0x07); + + // CRC8-MAXIM 查找表 + private static readonly byte[] Crc8MaximTable = BuildCrc8Table(0x31); + + // CRC8-ROHC 查找表 + private static readonly byte[] Crc8RohcTable = BuildCrc8Table(0x07); + + /// + /// 计算 CRC8 校验值 + /// + /// 数据 + /// CRC8 值 + public static byte Crc8(byte[] data) + { + return Crc8(data, 0, data.Length); + } + + /// + /// 计算 CRC8 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC8 值 + public static byte Crc8(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset >= data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte crc = 0; + for (int i = offset; i < offset + length; i++) + { + crc = Crc8Table[crc ^ data[i]]; + } + return crc; + } + + /// + /// 计算 CRC8-CCITT 校验值 + /// + /// 数据 + /// CRC8-CCITT 值 + public static byte Crc8Ccitt(byte[] data) + { + byte crc = 0; + foreach (byte b in data) + { + crc = Crc8CcittTable[crc ^ b]; + } + return crc; + } + + /// + /// 计算 CRC8-MAXIM 校验值 + /// + /// 数据 + /// CRC8-MAXIM 值 + public static byte Crc8Maxim(byte[] data) + { + byte crc = 0; + foreach (byte b in data) + { + crc = Crc8MaximTable[crc ^ b]; + } + return crc; + } + + private static byte[] BuildCrc8Table(byte polynomial) + { + var table = new byte[256]; + for (int i = 0; i < 256; i++) + { + byte crc = (byte)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 0x80) != 0 ? (byte)((crc << 1) ^ polynomial) : (byte)(crc << 1); + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC16 + + // CRC16-CCITT 查找表 + private static readonly ushort[] Crc16CcittTable = BuildCrc16Table(0x1021); + + // CRC16-MODBUS 查找表 + private static readonly ushort[] Crc16ModbusTable = BuildCrc16Table(0xA001); + + // CRC16-IBM 查找表 + private static readonly ushort[] Crc16IbmTable = BuildCrc16Table(0x8005); + + // CRC16-USB 查找表 + private static readonly ushort[] Crc16UsbTable = BuildCrc16Table(0xA001); + + /// + /// 计算 CRC16-CCITT 校验值 + /// + /// 数据 + /// CRC16-CCITT 值 + public static ushort Crc16Ccitt(byte[] data) + { + return Crc16Ccitt(data, 0, data.Length); + } + + /// + /// 计算 CRC16-CCITT 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC16-CCITT 值 + public static ushort Crc16Ccitt(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ushort crc = 0xFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (ushort)((crc << 8) ^ Crc16CcittTable[(crc >> 8) ^ data[i]]); + } + return crc; + } + + /// + /// 计算 CRC16-MODBUS 校验值 + /// + /// 数据 + /// CRC16-MODBUS 值 + public static ushort Crc16Modbus(byte[] data) + { + return Crc16Modbus(data, 0, data.Length); + } + + /// + /// 计算 CRC16-MODBUS 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC16-MODBUS 值 + public static ushort Crc16Modbus(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ushort crc = 0xFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (ushort)((crc >> 8) ^ Crc16ModbusTable[(crc ^ data[i]) & 0xFF]); + } + return crc; + } + + /// + /// 计算 CRC16-IBM 校验值 + /// + /// 数据 + /// CRC16-IBM 值 + public static ushort Crc16Ibm(byte[] data) + { + ushort crc = 0; + foreach (byte b in data) + { + crc = (ushort)((crc >> 8) ^ Crc16IbmTable[(crc ^ b) & 0xFF]); + } + return crc; + } + + /// + /// 计算 CRC16-USB 校验值 + /// + /// 数据 + /// CRC16-USB 值 + public static ushort Crc16Usb(byte[] data) + { + ushort crc = 0xFFFF; + foreach (byte b in data) + { + crc = (ushort)((crc >> 8) ^ Crc16UsbTable[(crc ^ b) & 0xFF]); + } + return (ushort)~crc; + } + + private static ushort[] BuildCrc16Table(ushort polynomial) + { + var table = new ushort[256]; + for (int i = 0; i < 256; i++) + { + ushort crc = (ushort)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ polynomial) : (ushort)(crc >> 1); + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC32 + + // CRC32 查找表(IEEE 802.3) + private static readonly uint[] Crc32Table = BuildCrc32Table(0xEDB88320); + + // CRC32-MPEG2 查找表 + private static readonly uint[] Crc32Mpeg2Table = BuildCrc32Table(0x04C11DB7); + + // CRC32C (Castagnoli) 查找表 + private static readonly uint[] Crc32CTable = BuildCrc32Table(0x82F63B78); + + /// + /// 计算 CRC32 校验值(IEEE 802.3) + /// + /// 数据 + /// CRC32 值 + public static uint Crc32(byte[] data) + { + return Crc32(data, 0, data.Length); + } + + /// + /// 计算 CRC32 校验值(IEEE 802.3) + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC32 值 + public static uint Crc32(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + uint crc = 0xFFFFFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (crc >> 8) ^ Crc32Table[(crc ^ data[i]) & 0xFF]; + } + return crc ^ 0xFFFFFFFF; + } + + /// + /// 计算 CRC32-MPEG2 校验值 + /// + /// 数据 + /// CRC32-MPEG2 值 + public static uint Crc32Mpeg2(byte[] data) + { + uint crc = 0xFFFFFFFF; + foreach (byte b in data) + { + crc = (crc << 8) ^ Crc32Mpeg2Table[((crc >> 24) ^ b) & 0xFF]; + } + return crc; + } + + /// + /// 计算 CRC32C (Castagnoli) 校验值 + /// + /// 数据 + /// CRC32C 值 + public static uint Crc32C(byte[] data) + { + uint crc = 0xFFFFFFFF; + foreach (byte b in data) + { + crc = (crc >> 8) ^ Crc32CTable[(crc ^ b) & 0xFF]; + } + return crc ^ 0xFFFFFFFF; + } + + private static uint[] BuildCrc32Table(uint polynomial) + { + var table = new uint[256]; + for (int i = 0; i < 256; i++) + { + uint crc = (uint)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1; + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC64 + + // CRC64-ECMA 查找表 + private static readonly ulong[] Crc64Table = BuildCrc64Table(0xC96C5795D7870F42); + + /// + /// 计算 CRC64-ECMA 校验值 + /// + /// 数据 + /// CRC64 值 + public static ulong Crc64(byte[] data) + { + return Crc64(data, 0, data.Length); + } + + /// + /// 计算 CRC64-ECMA 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC64 值 + public static ulong Crc64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ulong crc = 0; + for (int i = offset; i < offset + length; i++) + { + crc = Crc64Table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); + } + return crc; + } + + private static ulong[] BuildCrc64Table(ulong polynomial) + { + var table = new ulong[256]; + for (int i = 0; i < 256; i++) + { + ulong crc = (ulong)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1; + } + table[i] = crc; + } + return table; + } + + #endregion + + #region 通用方法 + + /// + /// 计算校验值并返回十六进制字符串 + /// + /// 数据 + /// 算法:CRC8, CRC16, CRC32, CRC64 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, string algorithm = "CRC32") + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + switch (algorithm.ToUpperInvariant()) + { + case "CRC8": + return Crc8(data).ToString("X2"); + case "CRC8-CCITT": + return Crc8Ccitt(data).ToString("X2"); + case "CRC8-MAXIM": + return Crc8Maxim(data).ToString("X2"); + case "CRC16": + case "CRC16-CCITT": + return Crc16Ccitt(data).ToString("X4"); + case "CRC16-MODBUS": + return Crc16Modbus(data).ToString("X4"); + case "CRC16-IBM": + return Crc16Ibm(data).ToString("X4"); + case "CRC16-USB": + return Crc16Usb(data).ToString("X4"); + case "CRC32": + return Crc32(data).ToString("X8"); + case "CRC32-MPEG2": + return Crc32Mpeg2(data).ToString("X8"); + case "CRC32C": + return Crc32C(data).ToString("X8"); + case "CRC64": + return Crc64(data).ToString("X16"); + default: + throw new ArgumentException($"Unknown CRC algorithm: {algorithm}", nameof(algorithm)); + } + } + + /// + /// 验证数据校验值 + /// + /// 数据 + /// 预期的 CRC 值(十六进制字符串) + /// 算法 + /// 是否匹配 + public static bool Verify(byte[] data, string expectedCrc, string algorithm = "CRC32") + { + string computed = ComputeHex(data, algorithm); + return string.Equals(computed, expectedCrc, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 验证 CRC32 校验值 + /// + /// 数据 + /// 预期的 CRC32 值 + /// 是否匹配 + public static bool VerifyCrc32(byte[] data, uint expectedCrc) + { + return Crc32(data) == expectedCrc; + } + + /// + /// 验证 CRC16 校验值 + /// + /// 数据 + /// 预期的 CRC16 值 + /// 是否匹配 + public static bool VerifyCrc16(byte[] data, ushort expectedCrc) + { + return Crc16Ccitt(data) == expectedCrc; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CuidUtil.cs b/EasyTool.Core/CodeCategory/CuidUtil.cs new file mode 100644 index 0000000..ea49765 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CuidUtil.cs @@ -0,0 +1,399 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// CUID/CUID2 碰撞抵抗ID工具类 + /// CUID 是一种水平可扩展、碰撞抵抗的ID生成方案 + /// CUID2 是更新版本,更安全、更符合标准 + /// + public static class CuidUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly object _lock = new object(); + private static int _counter = 0; + private static string _fingerprint = null; + + // Base36 字符集 + private const string Base36Chars = "0123456789abcdefghijklmnopqrstuvwxyz"; + private const string Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + #region CUID(原始版本) + + /// + /// 生成 CUID + /// + /// 25字符的 CUID 字符串 + public static string GenerateCuid() + { + var sb = new StringBuilder(25); + + // 1. 以 'c' 开头 + sb.Append('c'); + + // 2. 时间戳(Base36) + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + sb.Append(ToBase36(timestamp)); + + // 3. 计数器(Base36,4字符) + int counter; + lock (_lock) + { + counter = _counter++; + if (_counter > 1679615) _counter = 0; // 36^4 - 1 + } + sb.Append(ToBase36(counter, 4)); + + // 4. 指纹(8字符) + sb.Append(GetFingerprint()); + + // 5. 随机字符(4字符) + sb.Append(RandomBase36(4)); + + // 6. 随机字符(4字符) + sb.Append(RandomBase36(4)); + + return sb.ToString(); + } + + /// + /// 生成带前缀的 CUID(用于分布式系统) + /// + /// 前缀(1-4字符) + /// 带前缀的 CUID + public static string GenerateCuid(string prefix) + { + if (string.IsNullOrEmpty(prefix)) + return GenerateCuid(); + + if (prefix.Length > 4) + prefix = prefix.Substring(0, 4); + + return prefix + "_" + GenerateCuid().Substring(1); + } + + /// + /// 验证 CUID 是否有效 + /// + /// CUID 字符串 + /// 是否有效 + public static bool IsValidCuid(string cuid) + { + if (string.IsNullOrEmpty(cuid) || cuid.Length < 25) + return false; + + // 检查是否以 'c' 开头或带前缀 + if (cuid[0] != 'c' && !cuid.Contains("_")) + return false; + + // 检查字符是否有效 + foreach (char c in cuid) + { + if (c == '_') continue; + if (!Base36Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + #endregion + + #region CUID2 + + /// + /// 生成 CUID2(更安全) + /// + /// 24字符的 CUID2 + public static string GenerateCuid2() + { + return GenerateCuid2(24); + } + + /// + /// 生成指定长度的 CUID2 + /// + /// 长度(2-32) + /// CUID2 字符串 + public static string GenerateCuid2(int length) + { + if (length < 2 || length > 32) + throw new ArgumentException("Length must be between 2 and 32", nameof(length)); + + // 第一部分:时间戳 + 计数器 + 指纹的哈希 + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + + int counter; + lock (_lock) + { + counter = _counter++; + } + + string entropy = RandomBase36(32); + string fingerprint = GetFingerprint(); + + string input = $"{timestamp}{counter}{fingerprint}{entropy}"; + + // 使用 SHA3 或 SHA256 哈希 + byte[] hash; + using (var sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); + } + + // 转换为 Base62 + string result = ToBase62(hash).Substring(0, length); + + return result; + } + + /// + /// 生成带熵的 CUID2 + /// + /// 长度 + /// 额外熵值 + /// CUID2 字符串 + public static string GenerateCuid2(int length, string entropy) + { + if (length < 2 || length > 32) + throw new ArgumentException("Length must be between 2 and 32", nameof(length)); + + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + + int counter; + lock (_lock) + { + counter = _counter++; + } + + string fingerprint = GetFingerprint(); + string randomEntropy = RandomBase36(32); + + string input = $"{timestamp}{counter}{fingerprint}{entropy}{randomEntropy}"; + + byte[] hash; + using (var sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); + } + + string result = ToBase62(hash).Substring(0, length); + + return result; + } + + /// + /// 验证 CUID2 是否有效 + /// + /// CUID2 字符串 + /// 是否有效 + public static bool IsValidCuid2(string cuid2) + { + if (string.IsNullOrEmpty(cuid2) || cuid2.Length < 2 || cuid2.Length > 32) + return false; + + foreach (char c in cuid2) + { + if (!Base62Chars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 判断是 CUID 还是 CUID2 + /// + /// CUID 字符串 + /// CUID 类型 + public static CuidType GetCuidType(string cuid) + { + if (string.IsNullOrEmpty(cuid)) + return CuidType.Invalid; + + if (cuid.Length == 24 && IsValidCuid2(cuid)) + return CuidType.CUID2; + + if (cuid.Length >= 25 && (cuid[0] == 'c' || cuid.Contains("_"))) + return CuidType.CUID; + + return CuidType.Invalid; + } + + #endregion + + #region Slug(短版本) + + /// + /// 生成 Slug ID(更短的唯一ID) + /// + /// 7-10字符的 Slug + public static string GenerateSlug() + { + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + string random = RandomBase36(4); + string counter = ToBase36(Environment.CurrentManagedThreadId % 1296, 2); + + return ToBase36(timestamp).Substring(5) + counter + random; + } + + /// + /// 验证 Slug 是否有效 + /// + /// Slug 字符串 + /// 是否有效 + public static bool IsValidSlug(string slug) + { + if (string.IsNullOrEmpty(slug) || slug.Length < 7 || slug.Length > 10) + return false; + + foreach (char c in slug) + { + if (!Base36Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + #endregion + + #region 批量生成 + + /// + /// 批量生成 CUID + /// + /// 数量 + /// CUID 数组 + public static string[] GenerateCuidBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateCuid(); + } + return result; + } + + /// + /// 批量生成 CUID2 + /// + /// 数量 + /// 每个 CUID2 的长度 + /// CUID2 数组 + public static string[] GenerateCuid2Batch(int count, int length = 24) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateCuid2(length); + } + return result; + } + + #endregion + + #region 私有方法 + + private static string GetFingerprint() + { + if (_fingerprint != null) + return _fingerprint; + + var sb = new StringBuilder(); + + // 机器标识 + sb.Append(Environment.MachineName.GetHashCode()); + + // 进程ID + sb.Append(Environment.CurrentManagedThreadId); + + // 随机部分 + sb.Append(new Random().Next(1000)); + + _fingerprint = ToBase36(Math.Abs(sb.ToString().GetHashCode()), 8); + + return _fingerprint; + } + + private static string ToBase36(long value, int padLength = 0) + { + if (value < 0) value = -value; + + var result = new StringBuilder(); + while (value > 0) + { + result.Insert(0, Base36Chars[(int)(value % 36)]); + value /= 36; + } + + if (padLength > 0 && result.Length < padLength) + { + result.Insert(0, new string('0', padLength - result.Length)); + } + + return result.ToString(); + } + + private static string ToBase62(byte[] bytes) + { + // 将字节数组转换为大整数,然后转换为 Base62 + var result = new StringBuilder(); + + // 简化处理:直接使用字节的值 + for (int i = 0; i < bytes.Length; i++) + { + int value = bytes[i] % 62; + result.Append(Base62Chars[value]); + } + + return result.ToString(); + } + + private static string RandomBase36(int length) + { + var result = new StringBuilder(length); + byte[] randomBytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(randomBytes); + + foreach (byte b in randomBytes) + { + result.Append(Base36Chars[b % 36]); + } + + return result.ToString(); + } + + #endregion + } + + /// + /// CUID 类型 + /// + public enum CuidType + { + /// + /// 无效 + /// + Invalid, + + /// + /// 原始 CUID + /// + CUID, + + /// + /// CUID2 + /// + CUID2 + } +} diff --git a/EasyTool.Core/CodeCategory/DammUtil.cs b/EasyTool.Core/CodeCategory/DammUtil.cs new file mode 100644 index 0000000..6e7859a --- /dev/null +++ b/EasyTool.Core/CodeCategory/DammUtil.cs @@ -0,0 +1,235 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Damm 算法校验和工具类 + /// Damm 算法是一种检测单个数字错误的校验和算法 + /// 由 H. Michael Damm 发明,类似于 Verhoeff 算法 + /// 使用弱完全反对称群(quasigroup) + /// + public static class DammUtil + { + // Damm 算法的运算表(10x10 弱完全反对称群) + private static readonly byte[,] Matrix = new byte[,] + { + {0, 3, 1, 7, 5, 9, 8, 6, 4, 2}, + {7, 0, 9, 2, 1, 5, 4, 8, 6, 3}, + {4, 2, 0, 6, 8, 7, 1, 3, 5, 9}, + {1, 7, 5, 0, 9, 8, 3, 4, 2, 6}, + {6, 1, 2, 3, 0, 4, 5, 9, 7, 8}, + {3, 6, 7, 4, 2, 0, 9, 5, 8, 1}, + {5, 8, 6, 9, 7, 2, 0, 1, 3, 4}, + {8, 9, 4, 5, 3, 6, 2, 0, 1, 7}, + {9, 4, 3, 8, 6, 1, 7, 2, 0, 5}, + {2, 5, 8, 1, 4, 3, 6, 7, 9, 0} + }; + + /// + /// 计算数字字符串的 Damm 校验位 + /// + /// 数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + return CalculateCheckDigit(GetDigits(number)); + } + + /// + /// 计算数字数组的 Damm 校验位 + /// + /// 数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be empty", nameof(digits)); + + int interim = 0; + + foreach (int digit in digits) + { + if (digit < 0 || digit > 9) + throw new ArgumentException($"Invalid digit: {digit}", nameof(digits)); + + interim = Matrix[interim, digit]; + } + + return interim; + } + + /// + /// 生成带校验位的数字字符串 + /// + /// 原始数字字符串 + /// 带校验位的数字字符串 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 验证带校验位的数字字符串是否有效 + /// + /// 带校验位的数字字符串 + /// 是否有效 + public static bool Validate(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return false; + + return Validate(GetDigits(numberWithCheckDigit)); + } + + /// + /// 验证带校验位的数字数组是否有效 + /// + /// 带校验位的数字数组 + /// 是否有效 + public static bool Validate(int[] digitsWithCheckDigit) + { + if (digitsWithCheckDigit == null || digitsWithCheckDigit.Length < 2) + return false; + + int interim = 0; + + foreach (int digit in digitsWithCheckDigit) + { + if (digit < 0 || digit > 9) + return false; + + interim = Matrix[interim, digit]; + } + + return interim == 0; + } + + /// + /// 从带校验位的字符串中提取原始数字 + /// + /// 带校验位的数字字符串 + /// 原始数字字符串,如果无效则返回 null + public static string ExtractNumber(string numberWithCheckDigit) + { + if (!Validate(numberWithCheckDigit)) + return null; + + return numberWithCheckDigit.Substring(0, numberWithCheckDigit.Length - 1); + } + + /// + /// 获取校验位 + /// + /// 带校验位的数字字符串 + /// 校验位,如果格式无效则返回 -1 + public static int GetCheckDigit(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return -1; + + if (!int.TryParse(numberWithCheckDigit[numberWithCheckDigit.Length - 1].ToString(), out int digit)) + return -1; + + return digit; + } + + /// + /// 生成随机数字序列并添加校验位 + /// + /// 数字序列长度(不含校验位) + /// 带校验位的随机数字字符串 + public static string GenerateRandom(int length) + { + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + var random = new Random(); + var digits = new int[length]; + + for (int i = 0; i < length; i++) + { + digits[i] = random.Next(10); + } + + int checkDigit = CalculateCheckDigit(digits); + + var result = new System.Text.StringBuilder(length + 1); + foreach (int digit in digits) + { + result.Append(digit); + } + result.Append(checkDigit); + + return result.ToString(); + } + + /// + /// 批量验证多个数字字符串 + /// + /// 数字字符串数组 + /// 验证结果数组 + public static bool[] ValidateBatch(string[] numbers) + { + if (numbers == null) + throw new ArgumentNullException(nameof(numbers)); + + var results = new bool[numbers.Length]; + for (int i = 0; i < numbers.Length; i++) + { + results[i] = Validate(numbers[i]); + } + return results; + } + + /// + /// 检测并纠正单个数字错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrect(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试纠正每个位置的错误 + for (int pos = 0; pos < numberWithCheckDigit.Length; pos++) + { + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + corrected[pos] = (char)('0' + newDigit); + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + } + + return null; + } + + private static int[] GetDigits(string number) + { + var digits = new int[number.Length]; + for (int i = 0; i < number.Length; i++) + { + if (!char.IsDigit(number[i])) + throw new ArgumentException($"Invalid character: {number[i]}", nameof(number)); + + digits[i] = number[i] - '0'; + } + return digits; + } + } +} diff --git a/EasyTool.Core/CodeCategory/DesUtil.cs b/EasyTool.Core/CodeCategory/DesUtil.cs index 1fad310..5f2058c 100644 --- a/EasyTool.Core/CodeCategory/DesUtil.cs +++ b/EasyTool.Core/CodeCategory/DesUtil.cs @@ -28,7 +28,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -55,7 +55,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -87,7 +87,7 @@ public static string Encrypt(string str, string sk,string iv, CipherMode cipher byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -118,7 +118,7 @@ public static string Decrypt(string str, string sk, string iv, CipherMode cipher byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -155,7 +155,7 @@ public static byte[] Encrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = nul if (ivBytes != null && ivBytes.Length != 8) throw new ArgumentException("不合规的IV,请确认IV为8位"); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -187,7 +187,7 @@ public static byte[] Decrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = nul if (ivBytes != null && ivBytes.Length != 8) throw new ArgumentException("不合规的IV,请确认IV为8位"); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; diff --git a/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs b/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs new file mode 100644 index 0000000..1ab1c31 --- /dev/null +++ b/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Diffie-Hellman 密钥交换工具类 + /// DH 是一种安全地在公共信道上共享密钥的方法 + /// 基于离散对数问题的数学难题 + /// + public static class DiffieHellmanUtil + { + private const int DefaultKeySize = 2048; + + // 常用安全素数参数(RFC 3526) + private static readonly string[] KnownPrimes = new string[] + { + // 2048位 MODP Group (RFC 3526, Group 14) + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF" + }; + + private static readonly string[] KnownGenerators = new string[] + { + "2" + }; + + /// + /// 生成密钥对 + /// + /// 密钥长度(位) + /// 密钥对 + public static DHKeyPair GenerateKeyPair(int keySize = DefaultKeySize) + { + if (keySize < 512) + throw new ArgumentException("Key size must be at least 512 bits", nameof(keySize)); + + using var rng = RandomNumberGenerator.Create(); + + // 使用预定义的素数参数 + int primeIndex = 0; + System.Numerics.BigInteger p, g; + + if (keySize <= 2048) + { + p = System.Numerics.BigInteger.Parse(KnownPrimes[primeIndex], System.Globalization.NumberStyles.HexNumber); + g = System.Numerics.BigInteger.Parse(KnownGenerators[primeIndex], System.Globalization.NumberStyles.HexNumber); + } + else + { + // 生成自定义参数(较慢) + (p, g) = GenerateParametersInternal(keySize, rng); + } + + // 生成私钥 + byte[] xBytes = new byte[keySize / 8]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + // 计算公钥 y = g^x mod p + var y = ModPow(g, x, p); + + return new DHKeyPair(new DHParameters(p, g), x, y); + } + + /// + /// 使用指定参数生成密钥对 + /// + /// DH 参数 + /// 密钥对 + public static DHKeyPair GenerateKeyPair(DHParameters parameters) + { + using var rng = RandomNumberGenerator.Create(); + + var p = parameters.P; + var g = parameters.G; + + int keySize = p.ToByteArray().Length * 8; + + byte[] xBytes = new byte[keySize / 8]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + var y = ModPow(g, x, p); + + return new DHKeyPair(parameters, x, y); + } + + /// + /// 计算共享密钥 + /// + /// 对方的公钥 + /// 自己的私钥 + /// DH 参数 + /// 共享密钥 + public static byte[] ComputeSharedSecret(System.Numerics.BigInteger otherPublicKey, System.Numerics.BigInteger privateKey, DHParameters parameters) + { + var sharedSecret = ModPow(otherPublicKey, privateKey, parameters.P); + return sharedSecret.ToByteArray(); + } + + /// + /// 计算共享密钥并派生为指定长度的对称密钥 + /// + /// 对方的公钥 + /// 自己的私钥 + /// DH 参数 + /// 派生密钥长度(字节) + /// 盐值(可选) + /// 派生的对称密钥 + public static byte[] DeriveKey(System.Numerics.BigInteger otherPublicKey, System.Numerics.BigInteger privateKey, DHParameters parameters, int keyLength, byte[] salt = null) + { + byte[] sharedSecret = ComputeSharedSecret(otherPublicKey, privateKey, parameters); + + using var kdf = new Rfc2898DeriveBytes(sharedSecret, salt ?? new byte[16], 10000, HashAlgorithmName.SHA256); + return kdf.GetBytes(keyLength); + } + + /// + /// 验证公钥是否有效 + /// + /// 公钥 + /// DH 参数 + /// 是否有效 + public static bool ValidatePublicKey(System.Numerics.BigInteger publicKey, DHParameters parameters) + { + var p = parameters.P; + var g = parameters.G; + + // 公钥必须在 [2, p-1] 范围内 + if (publicKey < 2 || publicKey >= p) + return false; + + // 公钥不能是 p-1 的因子 + if (publicKey == p - 1) + return false; + + return true; + } + + /// + /// 生成 DH 参数 + /// + /// 密钥长度 + /// DH 参数 + public static DHParameters GenerateParameters(int keySize) + { + using var rng = RandomNumberGenerator.Create(); + var (p, g) = GenerateParametersInternal(keySize, rng); + return new DHParameters(p, g); + } + + private static (System.Numerics.BigInteger p, System.Numerics.BigInteger g) GenerateParametersInternal(int keySize, RandomNumberGenerator rng) + { + // 生成安全素数 p = 2q + 1,其中 q 也是素数 + byte[] pBytes = new byte[keySize / 8]; + System.Numerics.BigInteger p, q; + + do + { + rng.GetBytes(pBytes); + pBytes[pBytes.Length - 1] |= 0x01; // 奇数 + pBytes[0] |= 0x80; // 高位为1 + + p = new System.Numerics.BigInteger(pBytes); + p = System.Numerics.BigInteger.Abs(p); + + q = (p - 1) / 2; + + } while (!IsProbablyPrime(q) || !IsProbablyPrime(p)); + + // 找生成元 g + System.Numerics.BigInteger g = 2; + while (ModPow(g, 2, p) == 1 || ModPow(g, q, p) == 1) + { + g++; + } + + return (p, g); + } + + private static System.Numerics.BigInteger ModPow(System.Numerics.BigInteger b, System.Numerics.BigInteger e, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(b, e, m); + } + + private static bool IsProbablyPrime(System.Numerics.BigInteger n, int k = 10) + { + if (n < 2) return false; + if (n == 2 || n == 3) return true; + if (n % 2 == 0) return false; + + var d = n - 1; + int r = 0; + while (d % 2 == 0) + { + d /= 2; + r++; + } + + using var rng = RandomNumberGenerator.Create(); + byte[] bytes = n.ToByteArray(); + + for (int i = 0; i < k; i++) + { + byte[] aBytes = new byte[bytes.Length]; + rng.GetBytes(aBytes); + var a = new System.Numerics.BigInteger(aBytes); + a = System.Numerics.BigInteger.Abs(a) % (n - 3) + 2; + + var x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + for (int j = 0; j < r - 1; j++) + { + x = (x * x) % n; + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + } + + /// + /// DH 参数 + /// + public class DHParameters + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + + public DHParameters(System.Numerics.BigInteger p, System.Numerics.BigInteger g) + { + P = p; + G = g; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + + byte[] result = new byte[8 + pBytes.Length + gBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + + return result; + } + + public static DHParameters FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + + return new DHParameters( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes) + ); + } + + public string ToBase64() + { + return Convert.ToBase64String(ToByteArray()); + } + + public static DHParameters FromBase64(string base64) + { + return FromByteArray(Convert.FromBase64String(base64)); + } + } + + /// + /// DH 密钥对 + /// + public class DHKeyPair + { + public DHParameters Parameters { get; } + public System.Numerics.BigInteger PrivateKey { get; } + public System.Numerics.BigInteger PublicKey { get; } + + public DHKeyPair(DHParameters parameters, System.Numerics.BigInteger privateKey, System.Numerics.BigInteger publicKey) + { + Parameters = parameters; + PrivateKey = privateKey; + PublicKey = publicKey; + } + + /// + /// 计算与对方公钥的共享密钥 + /// + public byte[] ComputeSharedSecret(System.Numerics.BigInteger otherPublicKey) + { + return DiffieHellmanUtil.ComputeSharedSecret(otherPublicKey, PrivateKey, Parameters); + } + + /// + /// 派生对称密钥 + /// + public byte[] DeriveKey(System.Numerics.BigInteger otherPublicKey, int keyLength, byte[] salt = null) + { + return DiffieHellmanUtil.DeriveKey(otherPublicKey, PrivateKey, Parameters, keyLength, salt); + } + + /// + /// 导出公钥 + /// + public byte[] ExportPublicKey() + { + return PublicKey.ToByteArray(); + } + + /// + /// 导出公钥为 Base64 + /// + public string ExportPublicKeyBase64() + { + return Convert.ToBase64String(ExportPublicKey()); + } + + /// + /// 导出私钥(谨慎使用) + /// + public byte[] ExportPrivateKey() + { + byte[] paramBytes = Parameters.ToByteArray(); + byte[] keyBytes = PrivateKey.ToByteArray(); + + byte[] result = new byte[4 + paramBytes.Length + keyBytes.Length]; + BitConverter.GetBytes(paramBytes.Length).CopyTo(result, 0); + paramBytes.CopyTo(result, 4); + keyBytes.CopyTo(result, 4 + paramBytes.Length); + + return result; + } + + /// + /// 导入私钥 + /// + public static DHKeyPair ImportPrivateKey(byte[] data) + { + int paramLength = BitConverter.ToInt32(data, 0); + byte[] paramBytes = new byte[paramLength]; + byte[] keyBytes = new byte[data.Length - 4 - paramLength]; + + Array.Copy(data, 4, paramBytes, 0, paramLength); + Array.Copy(data, 4 + paramLength, keyBytes, 0, keyBytes.Length); + + var parameters = DHParameters.FromByteArray(paramBytes); + var privateKey = new System.Numerics.BigInteger(keyBytes); + var publicKey = System.Numerics.BigInteger.ModPow(parameters.G, privateKey, parameters.P); + + return new DHKeyPair(parameters, privateKey, publicKey); + } + } +} diff --git a/EasyTool.Core/CodeCategory/ElGamalUtil.cs b/EasyTool.Core/CodeCategory/ElGamalUtil.cs new file mode 100644 index 0000000..38d89fa --- /dev/null +++ b/EasyTool.Core/CodeCategory/ElGamalUtil.cs @@ -0,0 +1,460 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ElGamal 公钥加密工具类 + /// ElGamal 是基于离散对数问题的非对称加密算法 + /// 可用于加密和数字签名 + /// + public static class ElGamalUtil + { + private const int DefaultKeySize = 2048; + + /// + /// 生成密钥对 + /// + /// 密钥长度(位) + /// 公钥和私钥 + public static (ElGamalPublicKey PublicKey, ElGamalPrivateKey PrivateKey) GenerateKeyPair(int keySize = DefaultKeySize) + { + if (keySize < 512) + throw new ArgumentException("Key size must be at least 512 bits", nameof(keySize)); + + using var rng = RandomNumberGenerator.Create(); + + // 生成大素数 p + byte[] pBytes = new byte[keySize / 8]; + rng.GetBytes(pBytes); + pBytes[pBytes.Length - 1] |= 0x01; // 确保是奇数 + pBytes[0] |= 0x80; // 确保高位为1 + + var p = new System.Numerics.BigInteger(pBytes); + p = System.Numerics.BigInteger.Abs(p); + + // 找到下一个素数 + while (!IsProbablyPrime(p)) + { + p += 2; + } + + // 生成生成元 g(简化:使用小素数) + var g = FindGenerator(p, keySize, rng); + + // 生成私钥 x + byte[] xBytes = new byte[keySize / 8 - 1]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + // 计算公钥 y = g^x mod p + var y = ModPow(g, x, p); + + return ( + new ElGamalPublicKey(p, g, y), + new ElGamalPrivateKey(p, g, x) + ); + } + + /// + /// 加密数据 + /// + /// 明文 + /// 公钥 + /// 密文(C1 + C2) + public static byte[] Encrypt(byte[] plainText, ElGamalPublicKey publicKey) + { + if (plainText == null || plainText.Length == 0) + return Array.Empty(); + + using var rng = RandomNumberGenerator.Create(); + + var p = publicKey.P; + var g = publicKey.G; + var y = publicKey.Y; + + int keySize = p.ToByteArray().Length; + + // 将明文转换为数字 + byte[] paddedPlain = new byte[plainText.Length + 2]; + Array.Copy(plainText, paddedPlain, plainText.Length); + var m = new System.Numerics.BigInteger(paddedPlain); + + if (m >= p) + throw new ArgumentException("Message too long for key size", nameof(plainText)); + + // 生成随机数 k + byte[] kBytes = new byte[keySize - 1]; + rng.GetBytes(kBytes); + var k = new System.Numerics.BigInteger(kBytes); + k = System.Numerics.BigInteger.Abs(k) % (p - 2) + 1; + + // 计算 C1 = g^k mod p + var c1 = ModPow(g, k, p); + + // 计算 C2 = m * y^k mod p + var c2 = (m * ModPow(y, k, p)) % p; + + // 序列化 C1 和 C2 + byte[] c1Bytes = c1.ToByteArray(); + byte[] c2Bytes = c2.ToByteArray(); + + byte[] result = new byte[4 + c1Bytes.Length + 4 + c2Bytes.Length]; + BitConverter.GetBytes(c1Bytes.Length).CopyTo(result, 0); + c1Bytes.CopyTo(result, 4); + BitConverter.GetBytes(c2Bytes.Length).CopyTo(result, 4 + c1Bytes.Length); + c2Bytes.CopyTo(result, 8 + c1Bytes.Length); + + return result; + } + + /// + /// 解密数据 + /// + /// 密文 + /// 私钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, ElGamalPrivateKey privateKey) + { + if (cipherText == null || cipherText.Length < 8) + return Array.Empty(); + + var p = privateKey.P; + var x = privateKey.X; + + // 解析 C1 和 C2 + int c1Length = BitConverter.ToInt32(cipherText, 0); + int c2Length = BitConverter.ToInt32(cipherText, 4 + c1Length); + + byte[] c1Bytes = new byte[c1Length]; + byte[] c2Bytes = new byte[c2Length]; + Array.Copy(cipherText, 4, c1Bytes, 0, c1Length); + Array.Copy(cipherText, 8 + c1Length, c2Bytes, 0, c2Length); + + var c1 = new System.Numerics.BigInteger(c1Bytes); + var c2 = new System.Numerics.BigInteger(c2Bytes); + + // 计算 s = C1^x mod p + var s = ModPow(c1, x, p); + + // 计算逆元 s^-1 + var sInv = ModInverse(s, p); + + // 计算 m = C2 * s^-1 mod p + var m = (c2 * sInv) % p; + + // 转换为字节数组 + byte[] result = m.ToByteArray(); + Array.Resize(ref result, result.Length > 2 ? result.Length - 2 : 0); + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, ElGamalPublicKey publicKey) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, publicKey); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, ElGamalPrivateKey privateKey) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, privateKey); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 签名数据 + /// + public static byte[] Sign(byte[] data, ElGamalPrivateKey privateKey, System.Security.Cryptography.HashAlgorithm hashAlgorithm = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + bool shouldDisposeHash = hashAlgorithm == null; + hashAlgorithm ??= SHA256.Create(); + try + { + byte[] hash = hashAlgorithm.ComputeHash(data); + var h = new System.Numerics.BigInteger(hash); + + var p = privateKey.P; + var g = privateKey.G; + var x = privateKey.X; + + using var rng = RandomNumberGenerator.Create(); + int keySize = p.ToByteArray().Length; + + byte[] kBytes = new byte[keySize - 1]; + System.Numerics.BigInteger k; + System.Numerics.BigInteger kInv; + + do + { + rng.GetBytes(kBytes); + k = new System.Numerics.BigInteger(kBytes); + k = System.Numerics.BigInteger.Abs(k) % (p - 2) + 1; + kInv = ModInverse(k, p - 1); + } while (kInv == 0); + + var r = ModPow(g, k, p); + var s = ((h - x * r) * kInv) % (p - 1); + if (s < 0) s += p - 1; + + byte[] rBytes = r.ToByteArray(); + byte[] sBytes = s.ToByteArray(); + + byte[] result = new byte[4 + rBytes.Length + 4 + sBytes.Length]; + BitConverter.GetBytes(rBytes.Length).CopyTo(result, 0); + rBytes.CopyTo(result, 4); + BitConverter.GetBytes(sBytes.Length).CopyTo(result, 4 + rBytes.Length); + sBytes.CopyTo(result, 8 + rBytes.Length); + + return result; + } + finally + { + if (shouldDisposeHash) + hashAlgorithm.Dispose(); + } + } + + /// + /// 验证签名 + /// + public static bool Verify(byte[] data, byte[] signature, ElGamalPublicKey publicKey, System.Security.Cryptography.HashAlgorithm hashAlgorithm = null) + { + if (data == null || signature == null || signature.Length < 8) + return false; + + bool shouldDisposeHash = hashAlgorithm == null; + hashAlgorithm ??= SHA256.Create(); + try + { + byte[] hash = hashAlgorithm.ComputeHash(data); + var h = new System.Numerics.BigInteger(hash); + + var p = publicKey.P; + var g = publicKey.G; + var y = publicKey.Y; + + int rLength = BitConverter.ToInt32(signature, 0); + int sLength = BitConverter.ToInt32(signature, 4 + rLength); + + byte[] rBytes = new byte[rLength]; + byte[] sBytes = new byte[sLength]; + Array.Copy(signature, 4, rBytes, 0, rLength); + Array.Copy(signature, 8 + rLength, sBytes, 0, sLength); + + var r = new System.Numerics.BigInteger(rBytes); + var s = new System.Numerics.BigInteger(sBytes); + + // 验证: g^h ≡ y^r * r^s (mod p) + var left = ModPow(g, h, p); + var right = (ModPow(y, r, p) * ModPow(r, s, p)) % p; + + return left == right; + } + finally + { + if (shouldDisposeHash) + hashAlgorithm.Dispose(); + } + } + + private static System.Numerics.BigInteger ModPow(System.Numerics.BigInteger b, System.Numerics.BigInteger e, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(b, e, m); + } + + private static System.Numerics.BigInteger ModInverse(System.Numerics.BigInteger a, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(a, m - 2, m); + } + + private static System.Numerics.BigInteger FindGenerator(System.Numerics.BigInteger p, int keySize, RandomNumberGenerator rng) + { + // 简化:使用小生成元 + for (int g = 2; g < 100; g++) + { + if (ModPow(g, (p - 1) / 2, p) != 1) + { + return g; + } + } + return 2; + } + + private static bool IsProbablyPrime(System.Numerics.BigInteger n, int k = 10) + { + if (n < 2) return false; + if (n == 2 || n == 3) return true; + if (n % 2 == 0) return false; + + var d = n - 1; + int r = 0; + while (d % 2 == 0) + { + d /= 2; + r++; + } + + using var rng = RandomNumberGenerator.Create(); + int byteLength = n.ToByteArray().Length; + + for (int i = 0; i < k; i++) + { + byte[] aBytes = new byte[byteLength]; + rng.GetBytes(aBytes); + var a = new System.Numerics.BigInteger(aBytes); + a = System.Numerics.BigInteger.Abs(a) % (n - 3) + 2; + + var x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + for (int j = 0; j < r - 1; j++) + { + x = (x * x) % n; + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + } + + /// + /// ElGamal 公钥 + /// + public class ElGamalPublicKey + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + public System.Numerics.BigInteger Y { get; } + + public ElGamalPublicKey(System.Numerics.BigInteger p, System.Numerics.BigInteger g, System.Numerics.BigInteger y) + { + P = p; + G = g; + Y = y; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + byte[] yBytes = Y.ToByteArray(); + + byte[] result = new byte[12 + pBytes.Length + gBytes.Length + yBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + BitConverter.GetBytes(yBytes.Length).CopyTo(result, 8 + pBytes.Length + gBytes.Length); + yBytes.CopyTo(result, 12 + pBytes.Length + gBytes.Length); + + return result; + } + + public static ElGamalPublicKey FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + int yLength = BitConverter.ToInt32(data, 8 + pLength + gLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + byte[] yBytes = new byte[yLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + Array.Copy(data, 12 + pLength + gLength, yBytes, 0, yLength); + + return new ElGamalPublicKey( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes), + new System.Numerics.BigInteger(yBytes) + ); + } + } + + /// + /// ElGamal 私钥 + /// + public class ElGamalPrivateKey + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + public System.Numerics.BigInteger X { get; } + + public ElGamalPrivateKey(System.Numerics.BigInteger p, System.Numerics.BigInteger g, System.Numerics.BigInteger x) + { + P = p; + G = g; + X = x; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + byte[] xBytes = X.ToByteArray(); + + byte[] result = new byte[12 + pBytes.Length + gBytes.Length + xBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + BitConverter.GetBytes(xBytes.Length).CopyTo(result, 8 + pBytes.Length + gBytes.Length); + xBytes.CopyTo(result, 12 + pBytes.Length + gBytes.Length); + + return result; + } + + public static ElGamalPrivateKey FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + int xLength = BitConverter.ToInt32(data, 8 + pLength + gLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + byte[] xBytes = new byte[xLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + Array.Copy(data, 12 + pLength + gLength, xBytes, 0, xLength); + + return new ElGamalPrivateKey( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes), + new System.Numerics.BigInteger(xBytes) + ); + } + } +} diff --git a/EasyTool.Core/CodeCategory/FarmHashUtil.cs b/EasyTool.Core/CodeCategory/FarmHashUtil.cs new file mode 100644 index 0000000..a172e54 --- /dev/null +++ b/EasyTool.Core/CodeCategory/FarmHashUtil.cs @@ -0,0 +1,368 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// FarmHash 哈希工具类 + /// FarmHash 是 Google 开发的高性能哈希算法,是 CityHash 的继任者 + /// 专为哈希表设计,性能优异 + /// + public static class FarmHashUtil + { + private const ulong K0 = 0xc3a5c85c97cb3127; + private const ulong K1 = 0xb492b66fbe98f273; + private const ulong K2 = 0x9ae16a3b2f90404f; + private const ulong K3 = 0xc949d7c7509e6557; + + /// + /// 计算 FarmHash64 哈希值 + /// + /// 输入数据 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return FarmHash64(data, 0, (uint)data.Length); + } + + /// + /// 计算 FarmHash64 哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return 0; + + return FarmHash64(data, (uint)offset, (uint)length); + } + + /// + /// 计算 FarmHash128 哈希值 + /// + /// 输入数据 + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data) + { + if (data == null || data.Length == 0) + return (0, 0); + + return FarmHash128(data, 0, (uint)data.Length); + } + + /// + /// 计算字符串的 FarmHash64 哈希值 + /// + /// 文本 + /// 64位哈希值 + public static ulong ComputeString64(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data); + } + + /// + /// 计算字符串的 FarmHash128 哈希值 + /// + /// 文本 + /// 128位哈希值 + public static (ulong Low, ulong High) ComputeString128(string text) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash128(data); + } + + /// + /// 获取 FarmHash64 哈希值的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data) + { + ulong hash = ComputeHash64(data); + return hash.ToString("x16"); + } + + /// + /// 获取 FarmHash128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data) + { + var (low, high) = ComputeHash128(data); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 使用种子计算 FarmHash64 哈希值 + /// + /// 输入数据 + /// 种子值 + /// 64位哈希值 + public static ulong ComputeHash64WithSeed(byte[] data, ulong seed) + { + if (data == null || data.Length == 0) + return seed; + + return FarmHash64WithSeed(data, 0, (uint)data.Length, seed); + } + + #region 私有方法 + + private static ulong FarmHash64(byte[] data, uint offset, uint length) + { + if (length <= 16) + { + return HashLen0to16(data, offset, length); + } + else if (length <= 32) + { + return HashLen17to32(data, offset, length); + } + else if (length <= 64) + { + return HashLen33to64(data, offset, length); + } + else + { + return HashLenOver64(data, offset, length); + } + } + + private static (ulong Low, ulong High) FarmHash128(byte[] data, uint offset, uint length) + { + if (length < 128) + { + return FarmHash128WithSeed(data, offset, length, 0, 0); + } + + ulong h1 = length; + ulong h2 = 0; + ulong h3 = 0; + ulong h4 = 0; + + uint pos = offset; + uint end = offset + length; + + while (pos + 128 <= end) + { + h1 += ReadUInt64(data, pos); + h2 += ReadUInt64(data, pos + 8); + h3 += ReadUInt64(data, pos + 16); + h4 += ReadUInt64(data, pos + 24); + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + h3 = ShiftMix(h3) * K3; + h4 = ShiftMix(h4) * K1; + pos += 32; + } + + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + h3 = ShiftMix(h3) * K3; + h4 = ShiftMix(h4) * K1; + + return (h1 ^ h2 ^ h3 ^ h4, (h1 + h2 + h3 + h4) * K1); + } + + private static (ulong Low, ulong High) FarmHash128WithSeed(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + if (length == 0) + return (seed0, seed1); + + ulong h1 = seed0; + ulong h2 = seed1; + ulong h3 = length * K1; + ulong h4 = length * K2; + + uint pos = offset; + uint end = offset + length; + + while (pos + 16 <= end) + { + h1 += ReadUInt64(data, pos); + h2 += ReadUInt64(data, pos + 8); + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + pos += 16; + } + + if (pos < end) + { + ulong remaining = 0; + for (int i = 0; pos + i < end; i++) + { + remaining |= ((ulong)data[pos + i]) << (i * 8); + } + h3 += remaining * K3; + h4 = ShiftMix(h4 + remaining) * K2; + } + + h1 = ShiftMix(h1 + h3) * K1; + h2 = ShiftMix(h2 + h4) * K2; + + return (h1 ^ h2, ShiftMix(h1 + h2) * K1); + } + + private static ulong FarmHash64WithSeed(byte[] data, uint offset, uint length, ulong seed) + { + return HashLen16(FarmHash64(data, offset, length) - K2, seed); + } + + private static ulong HashLen0to16(byte[] data, uint offset, uint length) + { + if (length >= 8) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) + K2; + ulong b = ReadUInt64(data, offset + length - 8); + ulong c = RotateRight(b, 37) * mul + a; + ulong d = (RotateRight(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + + if (length >= 4) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt32(data, offset); + return HashLen16(length + (a << 3), ReadUInt32(data, offset + length - 4), mul); + } + + if (length > 0) + { + byte a = data[offset]; + byte b = data[offset + (length >> 1)]; + byte c = data[offset + length - 1]; + uint y = a + ((uint)b << 8); + uint z = length + ((uint)c << 2); + return ShiftMix(y * K2 ^ z * K3) * K2; + } + + return K2; + } + + private static ulong HashLen17to32(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K1; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + + return HashLen16(RotateRight(a + b, 43) + RotateRight(c, 30) + d, + a + RotateRight(b + K2, 18) + c, mul); + } + + private static ulong HashLen33to64(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K2; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + ulong y = ReadUInt64(data, offset + 16) * mul; + ulong z = ReadUInt64(data, offset + 24) * 9; + ulong e = RotateRight(a + y, 43) + RotateRight(b, 30) + c; + ulong f = a + RotateRight(y + z, 18) + d; + + return HashLen16(e + f, HashLen16(e, f, mul), mul); + } + + private static ulong HashLenOver64(byte[] data, uint offset, uint length) + { + ulong h = length; + ulong g = K1 * length; + ulong f = g; + + uint pos = offset; + uint end = offset + length; + + while (pos + 32 <= end) + { + ulong a = ReadUInt64(data, pos); + ulong b = ReadUInt64(data, pos + 8); + ulong c = ReadUInt64(data, pos + 16); + ulong d = ReadUInt64(data, pos + 24); + + h += a; + g += b; + f += c; + h = ShiftMix(h) * K1; + g = ShiftMix(g) * K2; + f = ShiftMix(f) * K3; + pos += 32; + } + + h = ShiftMix(h + f) * K1; + g = ShiftMix(g) * K2; + + if (pos < end) + { + ulong remaining = 0; + for (int i = 0; pos + i < end; i++) + { + remaining |= ((ulong)data[pos + i]) << (i * 8); + } + h += remaining * K3; + } + + return HashLen16(h, g); + } + + private static ulong HashLen16(ulong u, ulong v) + { + return HashLen16(u, v, K2); + } + + private static ulong HashLen16(ulong u, ulong v, ulong mul) + { + ulong a = (u ^ v) * mul; + a ^= (a >> 47); + ulong b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; + } + + private static ulong ReadUInt64(byte[] data, uint offset) + { + return BitConverter.ToUInt64(data, (int)offset); + } + + private static uint ReadUInt32(byte[] data, uint offset) + { + return BitConverter.ToUInt32(data, (int)offset); + } + + private static ulong RotateRight(ulong x, int n) + { + return (x >> n) | (x << (64 - n)); + } + + private static ulong ShiftMix(ulong x) + { + return x ^ (x >> 47); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/FletcherUtil.cs b/EasyTool.Core/CodeCategory/FletcherUtil.cs new file mode 100644 index 0000000..340a155 --- /dev/null +++ b/EasyTool.Core/CodeCategory/FletcherUtil.cs @@ -0,0 +1,340 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Fletcher 校验和工具类 + /// Fletcher 是一种简单的校验和算法,由 John G. Fletcher 发明 + /// 包括 Fletcher-8、Fletcher-16、Fletcher-32 和 Fletcher-64 变体 + /// 比 Adler-32 简单,但检测能力略低 + /// + public static class FletcherUtil + { + #region Fletcher-8 + + /// + /// 计算 Fletcher-8 校验和 + /// 使用 4 位累加器,生成 8 位校验和 + /// + /// 输入数据 + /// 8位校验和 + public static byte Compute8(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute8(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-8 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 8位校验和 + public static byte Compute8(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte sum1 = 0; + byte sum2 = 0; + const byte mod = 15; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (byte)((sum1 + (data[i] >> 4)) % mod); + sum2 = (byte)((sum2 + sum1) % mod); + sum1 = (byte)((sum1 + (data[i] & 0x0F)) % mod); + sum2 = (byte)((sum2 + sum1) % mod); + } + + return (byte)((sum2 << 4) | sum1); + } + + #endregion + + #region Fletcher-16 + + /// + /// 计算 Fletcher-16 校验和 + /// 使用 8 位累加器,生成 16 位校验和 + /// + /// 输入数据 + /// 16位校验和 + public static ushort Compute16(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute16(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-16 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 16位校验和 + public static ushort Compute16(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + ushort sum1 = 0; + ushort sum2 = 0; + const ushort mod = 255; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (ushort)((sum1 + data[i]) % mod); + sum2 = (ushort)((sum2 + sum1) % mod); + } + + return (ushort)((sum2 << 8) | sum1); + } + + /// + /// 获取 Fletcher-16 校验和的十六进制表示 + /// + /// 输入数据 + /// 4字符的十六进制字符串 + public static string Compute16Hex(byte[] data) + { + ushort checksum = Compute16(data); + return checksum.ToString("x4"); + } + + #endregion + + #region Fletcher-32 + + /// + /// 计算 Fletcher-32 校验和 + /// 使用 16 位累加器,生成 32 位校验和 + /// + /// 输入数据 + /// 32位校验和 + public static uint Compute32(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute32(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-32 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 32位校验和 + public static uint Compute32(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint sum1 = 0; + uint sum2 = 0; + const uint mod = 65535; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (sum1 + data[i]) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 16) | sum1; + } + + /// + /// 获取 Fletcher-32 校验和的十六进制表示 + /// + /// 输入数据 + /// 8字符的十六进制字符串 + public static string Compute32Hex(byte[] data) + { + uint checksum = Compute32(data); + return checksum.ToString("x8"); + } + + /// + /// 继续计算 Fletcher-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 更新后的校验和 + public static uint Continue32(uint previousChecksum, byte[] data) + { + if (data == null || data.Length == 0) + return previousChecksum; + + uint sum1 = previousChecksum & 0xFFFF; + uint sum2 = (previousChecksum >> 16) & 0xFFFF; + const uint mod = 65535; + + foreach (byte b in data) + { + sum1 = (sum1 + b) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 16) | sum1; + } + + #endregion + + #region Fletcher-64 + + /// + /// 计算 Fletcher-64 校验和 + /// 使用 32 位累加器,生成 64 位校验和 + /// + /// 输入数据 + /// 64位校验和 + public static ulong Compute64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute64(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-64 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位校验和 + public static ulong Compute64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + ulong sum1 = 0; + ulong sum2 = 0; + const ulong mod = 4294967295; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (sum1 + data[i]) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 32) | sum1; + } + + /// + /// 获取 Fletcher-64 校验和的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string Compute64Hex(byte[] data) + { + ulong checksum = Compute64(data); + return checksum.ToString("x16"); + } + + #endregion + + #region 通用方法 + + /// + /// 计算字符串的 Fletcher-16 校验和 + /// + /// 文本 + /// 16位校验和 + public static ushort Compute16String(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute16(data); + } + + /// + /// 计算字符串的 Fletcher-32 校验和 + /// + /// 文本 + /// 32位校验和 + public static uint Compute32String(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute32(data); + } + + /// + /// 验证数据的 Fletcher-16 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify16(byte[] data, ushort expectedChecksum) + { + return Compute16(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-32 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify32(byte[] data, uint expectedChecksum) + { + return Compute32(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-64 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify64(byte[] data, ulong expectedChecksum) + { + return Compute64(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-32 校验和(十六进制格式) + /// + /// 输入数据 + /// 期望的十六进制校验和 + /// 是否匹配 + public static bool Verify32Hex(byte[] data, string expectedHex) + { + if (string.IsNullOrEmpty(expectedHex)) + return false; + + string actual = Compute32Hex(data); + return string.Equals(actual, expectedHex, StringComparison.OrdinalIgnoreCase); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/GostUtil.cs b/EasyTool.Core/CodeCategory/GostUtil.cs new file mode 100644 index 0000000..adc2d62 --- /dev/null +++ b/EasyTool.Core/CodeCategory/GostUtil.cs @@ -0,0 +1,256 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// GOST 哈希工具类 + /// GOST R 34.11-94 是俄罗斯国家标准哈希算法 + /// 输出 256 位(32 字节)哈希值 + /// + public static class GostUtil + { + private const int HashSize = 32; // 256位 + private const int BlockSize = 32; // 256位 + + // S-box (测试向量使用的标准 S-box) + private static readonly byte[,] SBox = new byte[,] + { + { 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 }, + { 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 }, + { 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 }, + { 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 }, + { 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 }, + { 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 }, + { 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 }, + { 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 } + }; + + // 常量 C2-C12 + private static readonly byte[][] C = new byte[][] + { + new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + new byte[] { 0x73, 0xE2, 0x23, 0x04, 0x42, 0xB8, 0x27, 0x10, 0xC4, 0x50, 0x16, 0xEE, 0x5C, 0x7B, 0x1A, 0x11, 0xA8, 0x8E, 0xA4, 0x31, 0x6A, 0x83, 0x93, 0x62, 0x6C, 0x31, 0xF8, 0xDE, 0x36, 0xB9, 0x0B, 0x36 }, + new byte[] { 0x23, 0x7A, 0x3E, 0xA0, 0x89, 0xB9, 0x2B, 0xC4, 0xA9, 0xD6, 0x27, 0xE6, 0xC4, 0xD6, 0x80, 0xE5, 0x0C, 0x10, 0x47, 0x22, 0x49, 0xA9, 0x9D, 0xF4, 0x1D, 0x83, 0x07, 0xC1, 0x02, 0x76, 0xA8, 0x2F }, + new byte[] { 0xB9, 0x38, 0xA1, 0x6D, 0x42, 0x72, 0x9E, 0x6E, 0x4D, 0x95, 0x6D, 0x33, 0x3F, 0xEA, 0x0E, 0x26, 0x9B, 0x4D, 0x6F, 0xD6, 0xC4, 0x72, 0x8D, 0xD4, 0x2D, 0x2B, 0x0E, 0xD1, 0x5D, 0x16, 0x2F, 0x55 }, + new byte[] { 0x5C, 0x75, 0xF1, 0x8C, 0x29, 0x21, 0x6F, 0x0C, 0x9E, 0x84, 0x8A, 0x3A, 0x04, 0xF0, 0x21, 0x00, 0xDF, 0x1A, 0x2F, 0xA4, 0x4C, 0xA7, 0x4E, 0x00, 0x85, 0x38, 0x91, 0x99, 0xE9, 0x7A, 0x9D, 0x84 }, + new byte[] { 0xD4, 0x30, 0x42, 0x96, 0x6F, 0x56, 0x94, 0x6F, 0xFA, 0x0A, 0x4A, 0x2C, 0x6F, 0x90, 0x91, 0x87, 0xA4, 0x5E, 0xA8, 0xC7, 0x86, 0xFD, 0xB7, 0x51, 0x1A, 0xB4, 0x51, 0xAE, 0x3B, 0x7E, 0x6A, 0x67 }, + new byte[] { 0x03, 0xE8, 0x1D, 0x60, 0x81, 0xE3, 0xC3, 0x99, 0x3C, 0x91, 0xD5, 0xDA, 0x49, 0x76, 0x8A, 0xB6, 0x60, 0x4F, 0xB1, 0x4D, 0xE6, 0xA7, 0x8B, 0x00, 0x7F, 0x7C, 0x7E, 0xC2, 0x83, 0xD4, 0x29, 0x6F }, + new byte[] { 0xA2, 0x33, 0xB9, 0xD8, 0x08, 0x41, 0x37, 0x4E, 0xE3, 0xA5, 0xA2, 0xB6, 0xC9, 0x35, 0x78, 0xF7, 0xB3, 0x55, 0xC7, 0x83, 0xC5, 0x54, 0x37, 0x94, 0x7D, 0x58, 0x34, 0x65, 0xB2, 0xCB, 0x1A, 0x2D }, + new byte[] { 0x68, 0x83, 0x2B, 0xC7, 0xCC, 0x5C, 0x59, 0x46, 0x9F, 0xBE, 0x7A, 0x42, 0x42, 0x14, 0xB8, 0x90, 0x6D, 0xE4, 0x58, 0xED, 0x0E, 0x59, 0x6D, 0x8E, 0x6B, 0x7E, 0x2C, 0x8F, 0xB8, 0x2D, 0x93, 0x6B }, + new byte[] { 0xD4, 0x62, 0xE2, 0x41, 0x0F, 0x0F, 0x21, 0xDA, 0x76, 0xA5, 0xE9, 0x69, 0x94, 0x0D, 0x6F, 0xA3, 0xFB, 0x64, 0x59, 0x51, 0x9C, 0xAD, 0xBA, 0x71, 0x8B, 0x40, 0x6B, 0xA4, 0x68, 0x54, 0x51, 0xF7 }, + new byte[] { 0x1A, 0x2E, 0x0C, 0x47, 0xA5, 0x70, 0x9F, 0x24, 0x9C, 0xD0, 0x96, 0xB7, 0xC1, 0x65, 0x00, 0x96, 0x6C, 0x8B, 0xA3, 0x71, 0xB9, 0x1E, 0xB8, 0x5C, 0x1D, 0x36, 0x30, 0xA5, 0xA2, 0xB0, 0x35, 0xB5 }, + new byte[] { 0x4D, 0x04, 0x23, 0xE7, 0x68, 0x2E, 0x3D, 0x77, 0xCB, 0x6A, 0x0E, 0xF4, 0x5A, 0x88, 0x5B, 0x28, 0xDF, 0x1D, 0xD1, 0x9F, 0x21, 0xBA, 0x08, 0x0A, 0x95, 0xFB, 0x6D, 0x65, 0xA5, 0x6C, 0xA6, 0x3D }, + new byte[] { 0x11, 0x35, 0xF5, 0x71, 0x2F, 0xD6, 0x12, 0xD4, 0x6D, 0x9C, 0xF5, 0xE7, 0xBC, 0x3B, 0xEC, 0x03, 0x3F, 0x7D, 0x66, 0x36, 0x0A, 0xFB, 0xBA, 0x66, 0x2D, 0x5F, 0x96, 0x7D, 0x07, 0x12, 0x2D, 0x11 } + }; + + /// + /// 计算 GOST 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] Hash(byte[] data) + { + if (data == null || data.Length == 0) + return new byte[HashSize]; + + byte[] h = new byte[32]; + byte[] sigma = new byte[32]; + byte[] m = new byte[32]; + + // 初始化 H 和 Sigma + ulong length = (ulong)data.Length * 8; + int checksum = 0; + + int pos = 0; + while (pos < data.Length) + { + int len = Math.Min(32, data.Length - pos); + Array.Copy(data, pos, m, 0, len); + if (len < 32) Array.Clear(m, len, 32 - len); + + h = Step(h, m); + sigma = Add(sigma, m); + checksum = (checksum + len) & 0xFF; + pos += 32; + } + + // 最终化 + m = BitConverter.GetBytes(length); + Array.Resize(ref m, 32); + h = Step(h, m); + h = Step(h, sigma); + + return h; + } + + /// + /// 计算字符串的 GOST 哈希值 + /// + /// 输入文本 + /// 十六进制哈希字符串 + public static string HashString(string text) + { + if (string.IsNullOrEmpty(text)) + return new string('0', HashSize * 2); + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] hash = Hash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 验证哈希值 + /// + /// 原始数据 + /// 预期哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] hash) + { + if (hash == null || hash.Length != HashSize) + return false; + + byte[] computed = Hash(data); + return SlowEquals(computed, hash); + } + + /// + /// 验证字符串哈希 + /// + /// 原始文本 + /// 预期哈希值(十六进制) + /// 是否匹配 + public static bool VerifyString(string text, string hashHex) + { + if (string.IsNullOrEmpty(hashHex) || hashHex.Length != HashSize * 2) + return false; + + string computed = HashString(text); + return string.Equals(computed, hashHex, StringComparison.OrdinalIgnoreCase); + } + + private static byte[] Step(byte[] h, byte[] m) + { + byte[] u = (byte[])h.Clone(); + byte[] v = (byte[])m.Clone(); + byte[] w = new byte[32]; + + // Key generation + byte[] k = P(u, v); + + // Encryption + byte[] s = E(k, h); + + // Mixing + for (int i = 0; i < 12; i++) + { + w = X(u, v); + u = A(u); + v = A(A(v)); + } + + w = X(u, v); + s = X(s, w); + + return Psi(s, 12) ?? s; + } + + private static byte[] P(byte[] u, byte[] v) + { + byte[] k = new byte[32]; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 8; j++) + { + k[i * 8 + j] = (byte)(u[i + j * 4] ^ v[i + j * 4]); + } + } + return k; + } + + private static byte[] A(byte[] x) + { + byte[] result = new byte[32]; + for (int i = 0; i < 8; i++) + { + byte c = 0; + for (int j = 7; j >= 0; j--) + { + byte newC = (byte)(x[i * 4 + j] >> 7); + result[i * 4 + j] = (byte)((x[i * 4 + j] << 1) | c); + c = newC; + } + } + return result; + } + + private static byte[] X(byte[] a, byte[] b) + { + byte[] result = new byte[32]; + for (int i = 0; i < 32; i++) + result[i] = (byte)(a[i] ^ b[i]); + return result; + } + + private static byte[] Add(byte[] a, byte[] b) + { + byte[] result = new byte[32]; + int carry = 0; + for (int i = 0; i < 32; i++) + { + int sum = a[i] + b[i] + carry; + result[i] = (byte)(sum & 0xFF); + carry = sum >> 8; + } + return result; + } + + private static byte[] E(byte[] k, byte[] h) + { + byte[] result = new byte[32]; + for (int i = 0; i < 32; i += 8) + { + ulong block = BitConverter.ToUInt64(k, i); + ulong hBlock = BitConverter.ToUInt64(h, i); + block ^= hBlock; + + // S-box substitution + byte[] bytes = BitConverter.GetBytes(block); + for (int j = 0; j < 8; j++) + { + int row = j; + int col = (bytes[j] >> 4) & 0x0F; + bytes[j] = SBox[row, col]; + } + + Array.Copy(bytes, 0, result, i, 8); + } + return result; + } + + private static byte[] Psi(byte[] x, int n) + { + if (x == null) return null; + byte[] result = (byte[])x.Clone(); + + for (int i = 0; i < n; i++) + { + byte tmp = result[0]; + for (int j = 0; j < 31; j++) + result[j] = result[j + 1]; + result[31] = tmp; + } + + return result; + } + + private static bool SlowEquals(byte[] a, byte[] b) + { + uint diff = (uint)a.Length ^ (uint)b.Length; + for (int i = 0; i < a.Length && i < b.Length; i++) + diff |= (uint)(a[i] ^ b[i]); + return diff == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/GrayCodeUtil.cs b/EasyTool.Core/CodeCategory/GrayCodeUtil.cs new file mode 100644 index 0000000..dd4a9af --- /dev/null +++ b/EasyTool.Core/CodeCategory/GrayCodeUtil.cs @@ -0,0 +1,291 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// 格雷码(Gray Code)工具类 + /// 格雷码是一种二进制数系统,相邻的两个数之间只有一个位不同 + /// 由 Frank Gray 发明,常用于位置编码、错误检测等 + /// + public static class GrayCodeUtil + { + /// + /// 将二进制数转换为格雷码 + /// + /// 二进制数 + /// 格雷码 + public static uint BinaryToGray(uint binary) + { + return binary ^ (binary >> 1); + } + + /// + /// 将格雷码转换为二进制数 + /// + /// 格雷码 + /// 二进制数 + public static uint GrayToBinary(uint gray) + { + uint binary = gray; + binary ^= (binary >> 1); + binary ^= (binary >> 2); + binary ^= (binary >> 4); + binary ^= (binary >> 8); + binary ^= (binary >> 16); + return binary; + } + + /// + /// 将二进制数转换为格雷码(64位) + /// + /// 二进制数 + /// 格雷码 + public static ulong BinaryToGray64(ulong binary) + { + return binary ^ (binary >> 1); + } + + /// + /// 将格雷码转换为二进制数(64位) + /// + /// 格雷码 + /// 二进制数 + public static ulong GrayToBinary64(ulong gray) + { + ulong binary = gray; + binary ^= (binary >> 1); + binary ^= (binary >> 2); + binary ^= (binary >> 4); + binary ^= (binary >> 8); + binary ^= (binary >> 16); + binary ^= (binary >> 32); + return binary; + } + + /// + /// 将整数转换为格雷码二进制字符串 + /// + /// 整数值 + /// 位数 + /// 格雷码二进制字符串 + public static string ToGrayBinaryString(int value, int bits = 8) + { + if (bits < 1 || bits > 32) + throw new ArgumentException("Bits must be between 1 and 32", nameof(bits)); + + uint gray = BinaryToGray((uint)value); + return Convert.ToString(gray, 2).PadLeft(bits, '0'); + } + + /// + /// 生成 n 位格雷码序列 + /// + /// 位数 + /// 格雷码序列 + public static uint[] GenerateSequence(int n) + { + if (n < 1 || n > 32) + throw new ArgumentException("N must be between 1 and 32", nameof(n)); + + int count = 1 << n; + uint[] result = new uint[count]; + + for (int i = 0; i < count; i++) + { + result[i] = BinaryToGray((uint)i); + } + + return result; + } + + /// + /// 生成 n 位格雷码二进制字符串序列 + /// + /// 位数 + /// 格雷码二进制字符串序列 + public static string[] GenerateBinarySequence(int n) + { + if (n < 1 || n > 32) + throw new ArgumentException("N must be between 1 and 32", nameof(n)); + + int count = 1 << n; + string[] result = new string[count]; + + for (int i = 0; i < count; i++) + { + uint gray = BinaryToGray((uint)i); + result[i] = Convert.ToString(gray, 2).PadLeft(n, '0'); + } + + return result; + } + + /// + /// 计算两个格雷码之间的汉明距离 + /// + /// 第一个格雷码 + /// 第二个格雷码 + /// 汉明距离 + public static int HammingDistance(uint gray1, uint gray2) + { + // 格雷码的汉明距离等于它们的异或值的位数 + uint xor = gray1 ^ gray2; + int distance = 0; + + while (xor != 0) + { + distance++; + xor &= (xor - 1); // 清除最低位1 + } + + return distance; + } + + /// + /// 检查两个格雷码是否相邻 + /// + /// 第一个格雷码 + /// 第二个格雷码 + /// 是否相邻 + public static bool AreAdjacent(uint gray1, uint gray2) + { + return HammingDistance(gray1, gray2) == 1; + } + + /// + /// 获取格雷码的下一个值 + /// + /// 当前格雷码 + /// 下一个格雷码 + public static uint Next(uint gray) + { + uint binary = GrayToBinary(gray); + return BinaryToGray(binary + 1); + } + + /// + /// 获取格雷码的前一个值 + /// + /// 当前格雷码 + /// 前一个格雷码 + public static uint Previous(uint gray) + { + uint binary = GrayToBinary(gray); + if (binary == 0) + return 0; + + return BinaryToGray(binary - 1); + } + + /// + /// 计算格雷码的奇偶性 + /// + /// 格雷码 + /// 奇偶性(true = 奇数,false = 偶数) + public static bool Parity(uint gray) + { + // 格雷码的奇偶性与最高位相同 + uint binary = GrayToBinary(gray); + return (binary & 1) == 1; + } + + /// + /// 将字节转换为格雷码 + /// + /// 字节数据 + /// 格雷码字节 + public static byte ByteToGray(byte data) + { + return (byte)BinaryToGray(data); + } + + /// + /// 将格雷码字节转换为普通字节 + /// + /// 格雷码字节 + /// 普通字节 + public static byte GrayToByte(byte gray) + { + return (byte)GrayToBinary(gray); + } + + /// + /// 将字节数组转换为格雷码 + /// + /// 字节数组 + /// 格雷码字节数组 + public static byte[] EncodeBytes(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = ByteToGray(data[i]); + } + return result; + } + + /// + /// 将格雷码字节数组转换为普通字节数组 + /// + /// 格雷码字节数组 + /// 普通字节数组 + public static byte[] DecodeBytes(byte[] grayData) + { + if (grayData == null) + throw new ArgumentNullException(nameof(grayData)); + + byte[] result = new byte[grayData.Length]; + for (int i = 0; i < grayData.Length; i++) + { + result[i] = GrayToByte(grayData[i]); + } + return result; + } + + /// + /// 计算格雷码的位翻转位置(相对于前一个值) + /// + /// 格雷码 + /// 位翻转位置(0-based),如果是0则返回-1 + public static int GetBitFlipPosition(uint gray) + { + if (gray == 0) + return -1; + + // 找到最低位1的位置 + int position = 0; + uint temp = gray; + + while ((temp & 1) == 0) + { + temp >>= 1; + position++; + } + + return position; + } + + /// + /// 获取格雷码对应的十进制值 + /// + /// 格雷码 + /// 十进制值 + public static uint ToDecimal(uint gray) + { + return GrayToBinary(gray); + } + + /// + /// 从十进制值创建格雷码 + /// + /// 十进制值 + /// 格雷码 + public static uint FromDecimal(uint @decimal) + { + return BinaryToGray(@decimal); + } + } +} diff --git a/EasyTool.Core/CodeCategory/IDEAUtil.cs b/EasyTool.Core/CodeCategory/IDEAUtil.cs new file mode 100644 index 0000000..04c6e44 --- /dev/null +++ b/EasyTool.Core/CodeCategory/IDEAUtil.cs @@ -0,0 +1,294 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// IDEA (International Data Encryption Algorithm) 对称加密工具类 + /// IDEA 是一种分组密码,曾用于 PGP + /// 64位分组密码,使用 128 位密钥 + /// + public static class IDEAUtil + { + private const int BlockSize = 8; // 64位 + private const int KeySize = 16; // 128位 + private const int Rounds = 8; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + ushort[] subkeys = GenerateEncryptionSubkeys(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + ushort[] subkeys = GenerateDecryptionSubkeys(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey() + { + byte[] key = new byte[KeySize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static ushort[] GenerateEncryptionSubkeys(byte[] key) + { + ushort[] subkeys = new ushort[52]; + + // 将密钥分为8个16位子密钥 + for (int i = 0; i < 8; i++) + { + subkeys[i] = (ushort)((key[i * 2] << 8) | key[i * 2 + 1]); + } + + // 循环移位生成更多子密钥 + for (int i = 8; i < 52; i++) + { + if ((i % 8) == 0) + { + // 左移25位 + byte[] temp = new byte[KeySize]; + for (int j = 0; j < KeySize; j++) + { + int srcIdx = ((j + 3) % KeySize); + int bitPos = (j + 3) >= KeySize ? (j + 3 - KeySize) * 8 % 128 : (j + 3 - 8) * 8 % 128; + temp[j] = key[srcIdx]; + } + + for (int j = 0; j < KeySize; j++) + key[j] = temp[j]; + } + + subkeys[i] = (ushort)((key[((i % 8) * 2) % KeySize] << 8) | + key[((i % 8) * 2 + 1) % KeySize]); + } + + return subkeys; + } + + private static ushort[] GenerateDecryptionSubkeys(byte[] key) + { + ushort[] encSubkeys = GenerateEncryptionSubkeys(key); + ushort[] decSubkeys = new ushort[52]; + + // 解密子密钥是加密子密钥的逆 + for (int i = 0; i < Rounds; i++) + { + int idx = i * 6; + + if (i == 0) + { + decSubkeys[idx] = MulInv(encSubkeys[48 - i * 6]); + decSubkeys[idx + 1] = AddInv(encSubkeys[49 - i * 6]); + decSubkeys[idx + 2] = AddInv(encSubkeys[50 - i * 6]); + decSubkeys[idx + 3] = MulInv(encSubkeys[51 - i * 6]); + } + else + { + decSubkeys[idx] = MulInv(encSubkeys[48 - i * 6]); + decSubkeys[idx + 1] = AddInv(encSubkeys[49 - i * 6]); + decSubkeys[idx + 2] = AddInv(encSubkeys[50 - i * 6]); + decSubkeys[idx + 3] = MulInv(encSubkeys[51 - i * 6]); + } + + decSubkeys[idx + 4] = encSubkeys[46 - i * 6]; + decSubkeys[idx + 5] = encSubkeys[47 - i * 6]; + } + + // 最后一轮的子密钥 + decSubkeys[48] = MulInv(encSubkeys[0]); + decSubkeys[49] = AddInv(encSubkeys[1]); + decSubkeys[50] = AddInv(encSubkeys[2]); + decSubkeys[51] = MulInv(encSubkeys[3]); + + return decSubkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, ushort[] subkeys) + { + // 将64位分为4个16位 + ushort x0 = (ushort)((input[inOffset] << 8) | input[inOffset + 1]); + ushort x1 = (ushort)((input[inOffset + 2] << 8) | input[inOffset + 3]); + ushort x2 = (ushort)((input[inOffset + 4] << 8) | input[inOffset + 5]); + ushort x3 = (ushort)((input[inOffset + 6] << 8) | input[inOffset + 7]); + + int keyIdx = 0; + + for (int round = 0; round < Rounds; round++) + { + ushort y0 = Mul(x0, subkeys[keyIdx++]); + ushort y1 = Add(x1, subkeys[keyIdx++]); + ushort y2 = Add(x2, subkeys[keyIdx++]); + ushort y3 = Mul(x3, subkeys[keyIdx++]); + + ushort t0 = Mul((ushort)(y0 ^ y2), subkeys[keyIdx++]); + ushort t1 = Add((ushort)(y1 ^ y3), t0); + ushort t2 = Mul(t1, subkeys[keyIdx++]); + ushort t3 = Add(t0, t2); + + x0 = (ushort)(y0 ^ t2); + x1 = (ushort)(y2 ^ t2); + x2 = (ushort)(y1 ^ t3); + x3 = (ushort)(y3 ^ t3); + } + + // 最终输出变换 + ushort r0 = Mul(x0, subkeys[keyIdx++]); + ushort r1 = Add(x2, subkeys[keyIdx++]); + ushort r2 = Add(x1, subkeys[keyIdx++]); + ushort r3 = Mul(x3, subkeys[keyIdx++]); + + output[outOffset] = (byte)(r0 >> 8); + output[outOffset + 1] = (byte)r0; + output[outOffset + 2] = (byte)(r1 >> 8); + output[outOffset + 3] = (byte)r1; + output[outOffset + 4] = (byte)(r2 >> 8); + output[outOffset + 5] = (byte)r2; + output[outOffset + 6] = (byte)(r3 >> 8); + output[outOffset + 7] = (byte)r3; + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, ushort[] subkeys) + { + // 解密使用相同的结构,只是子密钥不同 + EncryptBlock(input, inOffset, output, outOffset, subkeys); + } + + // IDEA 的三种基本运算 + private static ushort Mul(ushort a, ushort b) + { + uint result = (uint)(a * b); + if (result != 0) + { + result = (uint)((ushort)(result & 0xFFFF) - (ushort)(result >> 16)); + if (result < 0x10000) + result = (uint)(result + 1); + return (ushort)result; + } + return (ushort)(1 - a - b); + } + + private static ushort Add(ushort a, ushort b) + { + return (ushort)((a + b) & 0xFFFF); + } + + private static ushort MulInv(ushort x) + { + if (x == 0) + return 0; + + int n = 0x10001; + int a = x; + int b = n; + int q, r; + int t1 = 0, t2 = 1; + + while (b > 0) + { + q = a / b; + r = a % b; + int t = t1 - q * t2; + a = b; + b = r; + t1 = t2; + t2 = t; + } + + if (t1 < 0) + t1 += n; + + return (ushort)t1; + } + + private static ushort AddInv(ushort x) + { + return (ushort)(0x10000 - x); + } + } +} diff --git a/EasyTool.Core/CodeCategory/JwtUtil.cs b/EasyTool.Core/CodeCategory/JwtUtil.cs new file mode 100644 index 0000000..4c78f79 --- /dev/null +++ b/EasyTool.Core/CodeCategory/JwtUtil.cs @@ -0,0 +1,526 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Linq; + +namespace EasyTool.CodeCategory +{ + /// + /// JWT(JSON Web Token)工具类 + /// 提供 JWT 的生成、解析、验证功能 + /// 支持 HS256、HS384、HS512 算法 + /// + public static class JwtUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + #region JWT 生成 + + /// + /// 生成 JWT Token + /// + /// 负载 + /// 密钥 + /// 算法(默认HS256) + /// JWT Token + public static string Encode(object payload, string secret, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + if (string.IsNullOrEmpty(secret)) + throw new ArgumentException("Secret cannot be null or empty", nameof(secret)); + + var payloadDict = ObjectToDictionary(payload); + return Encode(payloadDict, secret, algorithm); + } + + /// + /// 生成 JWT Token + /// + /// 负载字典 + /// 密钥 + /// 算法(默认HS256) + /// JWT Token + public static string Encode(Dictionary payload, string secret, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + if (string.IsNullOrEmpty(secret)) + throw new ArgumentException("Secret cannot be null or empty", nameof(secret)); + + // 创建 Header + var header = new Dictionary + { + { "typ", "JWT" }, + { "alg", algorithm.ToString() } + }; + + // 编码 Header 和 Payload + string headerEncoded = Base64UrlEncode(JsonSerializer.Serialize(header)); + string payloadEncoded = Base64UrlEncode(JsonSerializer.Serialize(payload)); + + // 创建签名 + string signatureInput = $"{headerEncoded}.{payloadEncoded}"; + string signature = CreateSignature(signatureInput, secret, algorithm); + + return $"{signatureInput}.{signature}"; + } + + /// + /// 生成带有过期时间的 JWT Token + /// + /// 负载 + /// 密钥 + /// 过期时间 + /// 算法 + /// JWT Token + public static string Encode(object payload, string secret, TimeSpan expiration, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + + var payloadDict = ObjectToDictionary(payload); + + // 添加时间戳 + var now = DateTime.UtcNow; + payloadDict["iat"] = ToUnixTimestamp(now); + payloadDict["exp"] = ToUnixTimestamp(now.Add(expiration)); + payloadDict["nbf"] = ToUnixTimestamp(now); + + return Encode(payloadDict, secret, algorithm); + } + + /// + /// 生成带有完整时间信息的 JWT Token + /// + /// 负载 + /// 密钥 + /// 签发者 + /// 受众 + /// 过期时间 + /// 算法 + /// JWT Token + public static string Encode(object payload, string secret, string issuer, string audience, TimeSpan expiration, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + + var payloadDict = ObjectToDictionary(payload); + + // 添加标准声明 + var now = DateTime.UtcNow; + payloadDict["iss"] = issuer; + payloadDict["aud"] = audience; + payloadDict["iat"] = ToUnixTimestamp(now); + payloadDict["exp"] = ToUnixTimestamp(now.Add(expiration)); + payloadDict["nbf"] = ToUnixTimestamp(now); + payloadDict["jti"] = Guid.NewGuid().ToString(); + + return Encode(payloadDict, secret, algorithm); + } + + #endregion + + #region JWT 解析 + + /// + /// 解析 JWT Token(不验证签名) + /// + /// JWT Token + /// 解析结果 + public static JwtDecodeResult Decode(string token) + { + if (string.IsNullOrEmpty(token)) + throw new ArgumentException("Token cannot be null or empty", nameof(token)); + + var parts = token.Split('.'); + if (parts.Length != 3) + throw new ArgumentException("Invalid token format", nameof(token)); + + try + { + var header = JsonSerializer.Deserialize>(Base64UrlDecode(parts[0])); + var payload = JsonSerializer.Deserialize>(Base64UrlDecode(parts[1])); + + return new JwtDecodeResult + { + Header = header, + Payload = payload, + Signature = parts[2], + IsValid = true + }; + } + catch (Exception ex) + { + return new JwtDecodeResult + { + IsValid = false, + ErrorMessage = ex.Message + }; + } + } + + /// + /// 解析并验证 JWT Token + /// + /// JWT Token + /// 密钥 + /// 解析结果 + public static JwtDecodeResult Decode(string token, string secret) + { + var result = Decode(token); + + if (!result.IsValid) + return result; + + // 验证签名 + if (!VerifySignature(token, secret, result.Header)) + { + result.IsValid = false; + result.ErrorMessage = "Invalid signature"; + return result; + } + + // 验证过期时间 + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + if (DateTime.UtcNow > expTime) + { + result.IsValid = false; + result.ErrorMessage = "Token has expired"; + result.IsExpired = true; + return result; + } + } + + // 验证生效时间 + if (result.Payload.TryGetValue("nbf", out var nbf)) + { + var nbfTime = FromUnixTimestamp(Convert.ToInt64(nbf)); + if (DateTime.UtcNow < nbfTime) + { + result.IsValid = false; + result.ErrorMessage = "Token is not yet valid"; + return result; + } + } + + return result; + } + + /// + /// 解析并验证 JWT Token(带完整验证) + /// + /// JWT Token + /// 密钥 + /// 预期签发者 + /// 预期受众 + /// 解析结果 + public static JwtDecodeResult Decode(string token, string secret, string issuer, string audience) + { + var result = Decode(token, secret); + + if (!result.IsValid) + return result; + + // 验证签发者 + if (!string.IsNullOrEmpty(issuer) && result.Payload.TryGetValue("iss", out var iss)) + { + if (iss.ToString() != issuer) + { + result.IsValid = false; + result.ErrorMessage = "Invalid issuer"; + return result; + } + } + + // 验证受众 + if (!string.IsNullOrEmpty(audience) && result.Payload.TryGetValue("aud", out var aud)) + { + var audList = aud as JsonElement?; + if (audList != null && audList.Value.ValueKind == JsonValueKind.Array) + { + var audiences = audList.Value.EnumerateArray().Select(a => a.GetString()).ToList(); + if (!audiences.Contains(audience)) + { + result.IsValid = false; + result.ErrorMessage = "Invalid audience"; + return result; + } + } + else if (aud.ToString() != audience) + { + result.IsValid = false; + result.ErrorMessage = "Invalid audience"; + return result; + } + } + + return result; + } + + /// + /// 获取 JWT Token 的 Payload(不验证) + /// + /// Payload 类型 + /// JWT Token + /// Payload 对象 + public static T GetPayload(string token) + { + var result = Decode(token); + if (!result.IsValid) + throw new ArgumentException(result.ErrorMessage); + + var json = JsonSerializer.Serialize(result.Payload); + return JsonSerializer.Deserialize(json); + } + + #endregion + + #region JWT 验证 + + /// + /// 验证 JWT Token + /// + /// JWT Token + /// 密钥 + /// 是否有效 + public static bool Verify(string token, string secret) + { + var result = Decode(token, secret); + return result.IsValid; + } + + /// + /// 验证 JWT Token 签名 + /// + /// JWT Token + /// 密钥 + /// 签名是否有效 + public static bool VerifySignature(string token, string secret) + { + var result = Decode(token); + if (!result.IsValid) + return false; + + return VerifySignature(token, secret, result.Header); + } + + private static bool VerifySignature(string token, string secret, Dictionary header) + { + var parts = token.Split('.'); + if (parts.Length != 3) + return false; + + var alg = header["alg"].ToString(); + var algorithm = Enum.Parse(alg); + + string expectedSignature = CreateSignature($"{parts[0]}.{parts[1]}", secret, algorithm); + return expectedSignature == parts[2]; + } + + /// + /// 检查 Token 是否过期 + /// + /// JWT Token + /// 是否过期 + public static bool IsExpired(string token) + { + var result = Decode(token); + if (!result.IsValid) + return true; + + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + return DateTime.UtcNow > expTime; + } + + return false; + } + + /// + /// 获取 Token 剩余有效时间 + /// + /// JWT Token + /// 剩余时间(如果没有过期时间则返回null) + public static TimeSpan? GetRemainingTime(string token) + { + var result = Decode(token); + if (!result.IsValid) + return null; + + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + var remaining = expTime - DateTime.UtcNow; + return remaining > TimeSpan.Zero ? remaining : TimeSpan.Zero; + } + + return null; + } + + #endregion + + #region JWT 刷新 + + /// + /// 刷新 Token(如果有效且未过期太久) + /// + /// 旧 Token + /// 密钥 + /// 新过期时间 + /// 最大刷新延迟(超过此时间不允许刷新) + /// 新 Token,如果无法刷新则返回null + public static string Refresh(string token, string secret, TimeSpan expiration, TimeSpan? maxRefreshDelay = null) + { + var result = Decode(token); + + if (!result.IsValid) + return null; + + // 验证签名 + if (!VerifySignature(token, secret, result.Header)) + return null; + + // 检查是否超出最大刷新延迟 + if (maxRefreshDelay.HasValue && result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + if (DateTime.UtcNow - expTime > maxRefreshDelay.Value) + return null; + } + + // 移除旧的时间戳和ID + var newPayload = new Dictionary(result.Payload); + newPayload.Remove("iat"); + newPayload.Remove("exp"); + newPayload.Remove("nbf"); + newPayload.Remove("jti"); + + // 生成新 Token + var alg = result.Header["alg"].ToString(); + var algorithm = Enum.Parse(alg); + + return Encode(newPayload, secret, expiration, algorithm); + } + + #endregion + + #region 私有方法 + + private static string CreateSignature(string input, string secret, JwtAlgorithm algorithm) + { + byte[] keyBytes = Encoding.UTF8.GetBytes(secret); + byte[] inputBytes = Encoding.UTF8.GetBytes(input); + + using var hmac = algorithm switch + { + JwtAlgorithm.HS256 => new HMACSHA256(keyBytes) as HMAC, + JwtAlgorithm.HS384 => new HMACSHA384(keyBytes), + JwtAlgorithm.HS512 => new HMACSHA512(keyBytes), + _ => throw new ArgumentException($"Unsupported algorithm: {algorithm}") + }; + + byte[] hash = hmac.ComputeHash(inputBytes); + return Base64UrlEncode(hash); + } + + private static string Base64UrlEncode(string input) + { + return Base64UrlEncode(Encoding.UTF8.GetBytes(input)); + } + + private static string Base64UrlEncode(byte[] input) + { + return Convert.ToBase64String(input) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + private static string Base64UrlDecode(string input) + { + string output = input + .Replace('-', '+') + .Replace('_', '/'); + + switch (output.Length % 4) + { + case 0: break; + case 2: output += "=="; break; + case 3: output += "="; break; + default: throw new ArgumentException("Invalid base64url string"); + } + + return Encoding.UTF8.GetString(Convert.FromBase64String(output)); + } + + private static Dictionary ObjectToDictionary(object obj) + { + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize>(json); + } + + private static long ToUnixTimestamp(DateTime dateTime) + { + return (long)(dateTime - Epoch).TotalSeconds; + } + + private static DateTime FromUnixTimestamp(long timestamp) + { + return Epoch.AddSeconds(timestamp); + } + + #endregion + } + + /// + /// JWT 算法 + /// + public enum JwtAlgorithm + { + HS256, + HS384, + HS512 + } + + /// + /// JWT 解析结果 + /// + public class JwtDecodeResult + { + /// + /// JWT Header + /// + public Dictionary Header { get; set; } + + /// + /// JWT Payload + /// + public Dictionary Payload { get; set; } + + /// + /// 签名 + /// + public string Signature { get; set; } + + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 是否过期 + /// + public bool IsExpired { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + } +} diff --git a/EasyTool.Core/CodeCategory/KSUIDUtil.cs b/EasyTool.Core/CodeCategory/KSUIDUtil.cs new file mode 100644 index 0000000..6674f79 --- /dev/null +++ b/EasyTool.Core/CodeCategory/KSUIDUtil.cs @@ -0,0 +1,299 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// KSUID(K-Sortable Unique Identifier)工具类 + /// KSUID 是一种时间排序的唯一标识符,由 Svix 开发 + /// 格式:4字节时间戳 + 16字节随机数 = 20字节 + /// + public static class KSUIDUtil + { + private static readonly DateTime Epoch = new DateTime(2014, 5, 13, 0, 0, 0, DateTimeKind.Utc); + private const int TimestampBytes = 4; + private const int PayloadBytes = 16; + private const int TotalBytes = 20; + private const int EncodedLength = 27; + + // Base62 字符集 + private const string Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + /// + /// 生成新的 KSUID + /// + /// 20字节的 KSUID + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 KSUID + /// + /// 时间戳 + /// 20字节的 KSUID + public static byte[] Generate(DateTimeOffset timestamp) + { + var bytes = new byte[TotalBytes]; + + // 4字节时间戳(大端序,自2014-05-13起的秒数) + uint seconds = (uint)(timestamp.ToUniversalTime() - Epoch).TotalSeconds; + bytes[0] = (byte)(seconds >> 24); + bytes[1] = (byte)(seconds >> 16); + bytes[2] = (byte)(seconds >> 8); + bytes[3] = (byte)seconds; + + // 16字节随机载荷 + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes, 4, 16); + + return bytes; + } + + /// + /// 生成 KSUID 字符串(27个字符的 Base62 编码) + /// + /// 27字符的 KSUID 字符串 + public static string GenerateString() + { + byte[] bytes = Generate(); + return Encode(bytes); + } + + /// + /// 生成指定时间的 KSUID 字符串 + /// + /// 时间戳 + /// 27字符的 KSUID 字符串 + public static string GenerateString(DateTimeOffset timestamp) + { + byte[] bytes = Generate(timestamp); + return Encode(bytes); + } + + /// + /// 将 KSUID 编码为字符串 + /// + /// 20字节的 KSUID + /// 27字符的 Base62 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != TotalBytes) + throw new ArgumentException($"KSUID must be {TotalBytes} bytes", nameof(bytes)); + + // 转换为大整数 + byte[] paddedBytes = new byte[TotalBytes + 1]; + Array.Copy(bytes, 0, paddedBytes, 1, TotalBytes); + + var number = new System.Numerics.BigInteger(paddedBytes); + var result = new char[EncodedLength]; + + for (int i = EncodedLength - 1; i >= 0; i--) + { + number = System.Numerics.BigInteger.DivRem(number, 62, out var remainder); + result[i] = Base62Chars[(int)remainder]; + } + + return new string(result); + } + + /// + /// 将 KSUID 字符串解码为字节数组 + /// + /// 27字符的 KSUID 字符串 + /// 20字节的 KSUID + public static byte[] Decode(string ksuid) + { + if (string.IsNullOrEmpty(ksuid) || ksuid.Length != EncodedLength) + throw new ArgumentException($"KSUID string must be {EncodedLength} characters", nameof(ksuid)); + + // 构建 Base62 解码映射 + var decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base62Chars.Length; i++) + { + decodeMap[Base62Chars[i]] = i; + } + + // 转换为大整数 + var number = System.Numerics.BigInteger.Zero; + + foreach (char c in ksuid) + { + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid character: {c}"); + + number = number * 62 + decodeMap[c]; + } + + // 转换为字节数组 + byte[] allBytes = number.ToByteArray(); + byte[] result = new byte[TotalBytes]; + + // 处理可能的符号位 + int copyLength = Math.Min(allBytes.Length, TotalBytes); + if (allBytes.Length > TotalBytes && allBytes[allBytes.Length - 1] == 0) + { + copyLength = Math.Min(allBytes.Length - 1, TotalBytes); + } + + // 从右侧开始复制(大端序) + int sourceIndex = allBytes.Length - copyLength; + if (sourceIndex < 0) sourceIndex = 0; + int destIndex = TotalBytes - copyLength; + if (destIndex < 0) destIndex = 0; + + for (int i = 0; i < copyLength && sourceIndex + i < allBytes.Length; i++) + { + result[destIndex + i] = allBytes[sourceIndex + i]; + } + + return result; + } + + /// + /// 从 KSUID 提取时间戳 + /// + /// KSUID 字节数组 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] ksuid) + { + if (ksuid == null || ksuid.Length != TotalBytes) + throw new ArgumentException($"KSUID must be {TotalBytes} bytes", nameof(ksuid)); + + uint seconds = ((uint)ksuid[0] << 24) | ((uint)ksuid[1] << 16) | + ((uint)ksuid[2] << 8) | ksuid[3]; + + return Epoch.AddSeconds(seconds); + } + + /// + /// 从 KSUID 字符串提取时间戳 + /// + /// KSUID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string ksuid) + { + byte[] bytes = Decode(ksuid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 KSUID 字符串是否有效 + /// + /// KSUID 字符串 + /// 是否有效 + public static bool IsValid(string ksuid) + { + if (string.IsNullOrEmpty(ksuid) || ksuid.Length != EncodedLength) + return false; + + foreach (char c in ksuid) + { + if (!Base62Chars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 尝试解析 KSUID 字符串 + /// + /// KSUID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string ksuid, out byte[] bytes) + { + bytes = null; + if (!IsValid(ksuid)) + return false; + + try + { + bytes = Decode(ksuid); + return true; + } + catch + { + return false; + } + } + + /// + /// 比较 KSUID 的时间顺序 + /// + /// 第一个 KSUID + /// 第二个 KSUID + /// -1: ksuid1早于ksuid2, 0: 相同, 1: ksuid1晚于ksuid2 + public static int Compare(string ksuid1, string ksuid2) + { + return string.Compare(ksuid1, ksuid2, StringComparison.Ordinal); + } + + /// + /// 批量生成 KSUID + /// + /// 生成数量 + /// KSUID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 生成指定时间范围内的 KSUID + /// + /// 最小时间 + /// 最大时间 + /// KSUID 字符串 + public static string GenerateInRange(DateTimeOffset minTimestamp, DateTimeOffset maxTimestamp) + { + if (minTimestamp > maxTimestamp) + throw new ArgumentException("Min timestamp must be less than or equal to max timestamp"); + + var random = new Random(); + long minSeconds = (long)(minTimestamp.ToUniversalTime() - Epoch).TotalSeconds; + long maxSeconds = (long)(maxTimestamp.ToUniversalTime() - Epoch).TotalSeconds; + long randomSeconds = minSeconds + (long)(random.NextDouble() * (maxSeconds - minSeconds)); + + var timestamp = Epoch.AddSeconds(randomSeconds); + return GenerateString(timestamp); + } + + /// + /// 获取 KSUID 的最小有效时间 + /// + /// KSUID 纪元时间 + public static DateTime GetEpoch() + { + return Epoch; + } + + /// + /// 解析 KSUID 的各个组成部分 + /// + /// KSUID 字符串 + /// 时间戳和载荷 + public static (DateTimeOffset Timestamp, byte[] Payload) Parse(string ksuid) + { + byte[] bytes = Decode(ksuid); + DateTimeOffset timestamp = ExtractTimestamp(bytes); + + byte[] payload = new byte[PayloadBytes]; + Array.Copy(bytes, 4, payload, 0, PayloadBytes); + + return (timestamp, payload); + } + } +} diff --git a/EasyTool.Core/CodeCategory/LZ4Util.cs b/EasyTool.Core/CodeCategory/LZ4Util.cs new file mode 100644 index 0000000..25f3825 --- /dev/null +++ b/EasyTool.Core/CodeCategory/LZ4Util.cs @@ -0,0 +1,339 @@ +using System; +using System.IO; + +namespace EasyTool.CodeCategory +{ + /// + /// LZ4 压缩工具类 + /// LZ4 是一种极快的无损压缩算法 + /// 压缩速度可达 500MB/s,解压速度可达 1GB/s + /// + public static class LZ4Util + { + private const int MinMatch = 4; + private const int MaxOffset = 65535; + private const int MinLookahead = MinMatch + 1; + + #region 压缩 + + /// + /// 压缩数据 + /// + /// 要压缩的数据 + /// 压缩后的数据 + public static byte[] Compress(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + return Compress(data, 0, data.Length); + } + + /// + /// 压缩数据 + /// + /// 要压缩的数据 + /// 起始偏移 + /// 数据长度 + /// 压缩后的数据 + public static byte[] Compress(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return Array.Empty(); + + if (length < MinLookahead) + { + // 太短,直接返回原始数据(带标记) + byte[] result = new byte[length + 1]; + result[0] = 0; // 标记为未压缩 + Array.Copy(data, offset, result, 1, length); + return result; + } + + var output = new MemoryStream(); + var writer = new BinaryWriter(output); + + // 写入原始长度 + writer.Write(length); + + int pos = offset; + int anchor = offset; + int end = offset + length; + + while (pos < end - MinLookahead) + { + int matchPos = FindMatch(data, pos, end, out int matchLength); + + if (matchPos >= 0 && matchLength >= MinMatch) + { + // 写入字面量 + int literalLength = pos - anchor; + WriteToken(writer, literalLength, matchLength); + + // 写入字面量数据 + if (literalLength > 0) + { + writer.Write(data, anchor, literalLength); + } + + // 写入偏移量 + int offset_value = pos - matchPos; + writer.Write((byte)(offset_value >> 8)); + writer.Write((byte)offset_value); + + pos += matchLength; + anchor = pos; + } + else + { + pos++; + } + } + + // 写入最后的字面量 + int finalLiteralLength = end - anchor; + WriteToken(writer, finalLiteralLength, 0); + + if (finalLiteralLength > 0) + { + writer.Write(data, anchor, finalLiteralLength); + } + + return output.ToArray(); + } + + private static int FindMatch(byte[] data, int pos, int end, out int matchLength) + { + matchLength = 0; + int bestMatchPos = -1; + int bestMatchLength = 0; + + int searchStart = Math.Max(0, pos - MaxOffset); + + for (int i = searchStart; i < pos; i++) + { + int len = 0; + int maxLen = Math.Min(end - pos, 255 + MinMatch); + + while (len < maxLen && data[i + len] == data[pos + len]) + { + len++; + } + + if (len >= MinMatch && len > bestMatchLength) + { + bestMatchPos = i; + bestMatchLength = len; + } + } + + matchLength = bestMatchLength; + return bestMatchPos; + } + + private static void WriteToken(BinaryWriter writer, int literalLength, int matchLength) + { + int token = Math.Min(literalLength, 15) << 4; + token |= Math.Min(matchLength - MinMatch, 15); + + writer.Write((byte)token); + + // 写入扩展的字面量长度 + if (literalLength >= 15) + { + literalLength -= 15; + while (literalLength >= 255) + { + writer.Write((byte)255); + literalLength -= 255; + } + writer.Write((byte)literalLength); + } + + // 写入扩展的匹配长度 + if (matchLength - MinMatch >= 15) + { + int extraLength = matchLength - MinMatch - 15; + while (extraLength >= 255) + { + writer.Write((byte)255); + extraLength -= 255; + } + writer.Write((byte)extraLength); + } + } + + #endregion + + #region 解压 + + /// + /// 解压数据 + /// + /// 压缩的数据 + /// 解压后的数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length == 0) + return Array.Empty(); + + return Decompress(compressed, 0, compressed.Length); + } + + /// + /// 解压数据 + /// + /// 压缩的数据 + /// 起始偏移 + /// 数据长度 + /// 解压后的数据 + public static byte[] Decompress(byte[] compressed, int offset, int length) + { + if (compressed == null) + throw new ArgumentNullException(nameof(compressed)); + if (offset < 0 || offset > compressed.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > compressed.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return Array.Empty(); + + var input = new MemoryStream(compressed, offset, length); + var reader = new BinaryReader(input); + + // 读取原始长度 + int originalLength = reader.ReadInt32(); + var output = new byte[originalLength]; + int outPos = 0; + + while (input.Position < input.Length && outPos < originalLength) + { + // 读取 token + int token = reader.ReadByte(); + int literalLength = (token >> 4) & 0x0F; + int matchLength = token & 0x0F; + + // 读取扩展的字面量长度 + if (literalLength == 15) + { + int extra; + do + { + extra = reader.ReadByte(); + literalLength += extra; + } while (extra == 255); + } + + // 复制字面量 + if (literalLength > 0) + { + Array.Copy(compressed, (int)input.Position, output, outPos, literalLength); + input.Position += literalLength; + outPos += literalLength; + } + + if (input.Position >= input.Length || outPos >= originalLength) + break; + + // 读取偏移量 + int matchOffset = (reader.ReadByte() << 8) | reader.ReadByte(); + + // 读取扩展的匹配长度 + matchLength += MinMatch; + if ((token & 0x0F) == 15) + { + int extra; + do + { + extra = reader.ReadByte(); + matchLength += extra; + } while (extra == 255); + } + + // 复制匹配 + int matchPos = outPos - matchOffset; + for (int i = 0; i < matchLength; i++) + { + output[outPos++] = output[matchPos++]; + } + } + + return output; + } + + #endregion + + #region 高级 API + + /// + /// 压缩字符串 + /// + /// 文本 + /// 压缩后的 Base64 字符串 + public static string CompressString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + /// 压缩的 Base64 字符串 + /// 原始文本 + public static string DecompressString(string compressedText) + { + if (string.IsNullOrEmpty(compressedText)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedText); + byte[] data = Decompress(compressed); + return System.Text.Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩后的预计最大长度 + /// + /// 输入长度 + /// 最大输出长度 + public static int CalculateMaxCompressedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // LZ4 最坏情况下可能略微增加大小 + return inputLength + (inputLength / 255) + 16 + 4; // 4 for original length + } + + /// + /// 计算压缩比 + /// + /// 原始数据 + /// 压缩数据 + /// 压缩比(0-1) + public static double CalculateCompressionRatio(byte[] originalData, byte[] compressedData) + { + if (originalData == null || originalData.Length == 0) + return 0; + + if (compressedData == null || compressedData.Length == 0) + return 1; + + return (double)compressedData.Length / originalData.Length; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/LuhnUtil.cs b/EasyTool.Core/CodeCategory/LuhnUtil.cs new file mode 100644 index 0000000..158bc02 --- /dev/null +++ b/EasyTool.Core/CodeCategory/LuhnUtil.cs @@ -0,0 +1,386 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CodeCategory +{ + /// + /// Luhn 校验算法工具类 + /// Luhn 算法是一种简单的校验和算法,用于验证信用卡号、IMEI号、银行卡号等 + /// + public static class LuhnUtil + { + /// + /// 验证数字字符串是否符合 Luhn 算法 + /// + /// 数字字符串 + /// 是否有效 + public static bool IsValid(string number) + { + if (string.IsNullOrEmpty(number)) + return false; + + // 移除空格和连字符 + number = CleanNumber(number); + + if (!IsAllDigits(number)) + return false; + + int sum = CalculateLuhnSum(number); + return sum % 10 == 0; + } + + /// + /// 验证数字数组是否符合 Luhn 算法 + /// + /// 数字数组 + /// 是否有效 + public static bool IsValid(int[] digits) + { + if (digits == null || digits.Length == 0) + return false; + + // 验证所有数字都在 0-9 范围内 + foreach (int d in digits) + { + if (d < 0 || d > 9) + return false; + } + + int sum = CalculateLuhnSum(digits); + return sum % 10 == 0; + } + + /// + /// 计算 Luhn 校验位 + /// + /// 不含校验位的数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + + if (!IsAllDigits(number)) + throw new ArgumentException("Number must contain only digits", nameof(number)); + + return CalculateCheckDigitImpl(number); + } + + /// + /// 计算 Luhn 校验位 + /// + /// 不含校验位的数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be null or empty", nameof(digits)); + + foreach (int d in digits) + { + if (d < 0 || d > 9) + throw new ArgumentException("All digits must be between 0 and 9", nameof(digits)); + } + + return CalculateCheckDigitImpl(digits); + } + + /// + /// 生成带校验位的完整数字 + /// + /// 不含校验位的数字字符串 + /// 带校验位的完整数字 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 生成带校验位的完整数字数组 + /// + /// 不含校验位的数字数组 + /// 带校验位的完整数字数组 + public static int[] AppendCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be null or empty", nameof(digits)); + + int checkDigit = CalculateCheckDigit(digits); + int[] result = new int[digits.Length + 1]; + Array.Copy(digits, result, digits.Length); + result[digits.Length] = checkDigit; + return result; + } + + /// + /// 生成指定长度的有效 Luhn 数字 + /// + /// 总长度(包括校验位) + /// 有效的 Luhn 数字字符串 + public static string Generate(int length) + { + if (length < 2) + throw new ArgumentException("Length must be at least 2", nameof(length)); + + var random = new Random(); + var digits = new int[length - 1]; + + // 生成随机数字(第一位不能为0) + digits[0] = random.Next(1, 10); + for (int i = 1; i < digits.Length; i++) + { + digits[i] = random.Next(0, 10); + } + + return AppendCheckDigit(string.Join("", digits)); + } + + /// < /// 生成指定前缀的有效 Luhn 数字 + /// + /// 前缀 + /// 总长度(包括校验位) + /// 有效的 Luhn 数字字符串 + public static string GenerateWithPrefix(string prefix, int totalLength) + { + if (string.IsNullOrEmpty(prefix)) + throw new ArgumentException("Prefix cannot be null or empty", nameof(prefix)); + if (totalLength < prefix.Length + 1) + throw new ArgumentException("Total length must be greater than prefix length", nameof(totalLength)); + + prefix = CleanNumber(prefix); + if (!IsAllDigits(prefix)) + throw new ArgumentException("Prefix must contain only digits", nameof(prefix)); + + var random = new Random(); + int remainingLength = totalLength - prefix.Length - 1; + var sb = new System.Text.StringBuilder(prefix); + + for (int i = 0; i < remainingLength; i++) + { + sb.Append(random.Next(0, 10)); + } + + return AppendCheckDigit(sb.ToString()); + } + + /// + /// 获取校验位 + /// + /// 带校验位的完整数字字符串 + /// 校验位 + public static int GetCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + if (!IsAllDigits(number)) + throw new ArgumentException("Number must contain only digits", nameof(number)); + + return number[number.Length - 1] - '0'; + } + + /// + /// 移除校验位 + /// + /// 带校验位的完整数字字符串 + /// 不含校验位的数字字符串 + public static string RemoveCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + if (number.Length < 2) + throw new ArgumentException("Number must have at least 2 digits", nameof(number)); + + return number.Substring(0, number.Length - 1); + } + + /// + /// 计算两个有效 Luhn 数字之间的编辑距离(需要改变多少位才能从一个变成另一个) + /// + /// 第一个数字 + /// 第二个数字 + /// 编辑距离 + public static int Distance(string number1, string number2) + { + number1 = CleanNumber(number1); + number2 = CleanNumber(number2); + + if (number1.Length != number2.Length) + throw new ArgumentException("Both numbers must have the same length"); + + int distance = 0; + for (int i = 0; i < number1.Length; i++) + { + if (number1[i] != number2[i]) + distance++; + } + + return distance; + } + + /// + /// 查找可能的错误(单字符错误) + /// + /// 无效的数字字符串 + /// 可能的修正列表(位置和正确值) + public static List<(int Position, int CorrectDigit)> FindPossibleErrors(string invalidNumber) + { + var result = new List<(int Position, int CorrectDigit)>(); + + if (string.IsNullOrEmpty(invalidNumber)) + return result; + + invalidNumber = CleanNumber(invalidNumber); + if (!IsAllDigits(invalidNumber)) + return result; + + var digits = invalidNumber.Select(c => c - '0').ToArray(); + + for (int i = 0; i < digits.Length; i++) + { + int original = digits[i]; + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + if (newDigit == original) + continue; + + digits[i] = newDigit; + if (IsValid(digits)) + { + result.Add((i, newDigit)); + } + } + digits[i] = original; + } + + return result; + } + + #region 私有方法 + + private static string CleanNumber(string number) + { + return number.Replace(" ", "").Replace("-", "").Replace("\t", ""); + } + + private static bool IsAllDigits(string s) + { + foreach (char c in s) + { + if (c < '0' || c > '9') + return false; + } + return true; + } + + private static int CalculateLuhnSum(string number) + { + int sum = 0; + bool doubleDigit = true; + + for (int i = number.Length - 2; i >= 0; i--) + { + int digit = number[i] - '0'; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + // 加上校验位 + sum += number[number.Length - 1] - '0'; + + return sum; + } + + private static int CalculateLuhnSum(int[] digits) + { + int sum = 0; + bool doubleDigit = true; + + for (int i = digits.Length - 2; i >= 0; i--) + { + int digit = digits[i]; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + sum += digits[digits.Length - 1]; + + return sum; + } + + private static int CalculateCheckDigitImpl(string number) + { + int sum = 0; + bool doubleDigit = false; + + for (int i = number.Length - 1; i >= 0; i--) + { + int digit = number[i] - '0'; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + return (10 - (sum % 10)) % 10; + } + + private static int CalculateCheckDigitImpl(int[] digits) + { + int sum = 0; + bool doubleDigit = false; + + for (int i = digits.Length - 1; i >= 0; i--) + { + int digit = digits[i]; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/MorseCodeUtil.cs b/EasyTool.Core/CodeCategory/MorseCodeUtil.cs new file mode 100644 index 0000000..f0c3077 --- /dev/null +++ b/EasyTool.Core/CodeCategory/MorseCodeUtil.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 摩尔斯电码工具类 + /// 摩尔斯电码是一种将文本字符编码为点(.)和划(-)序列的编码方式 + /// 支持字母、数字和常用标点符号 + /// + public static class MorseCodeUtil + { + private static readonly Dictionary CharToMorse = new Dictionary + { + // 字母 + {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, + {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, + {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, + {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, + {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, + {'Z', "--.."}, + + // 数字 + {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, + {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, + {'8', "---.."}, {'9', "----."}, + + // 标点符号 + {'.', ".-.-.-"}, {',', "--..--"}, {'?', "..--.."}, {'\'', ".----."}, + {'!', "-.-.--"}, {'/', "-..-."}, {'(', "-.--."}, {')', "-.--.-"}, + {'&', ".-..."}, {':', "---..."}, {';', "-.-.-."}, {'=', "-...-"}, + {'+', ".-.-."}, {'-', "-....-"}, {'_', "..--.-"}, {'"', ".-..-."}, + {'$', "...-..-"}, {'@', ".--.-."} + }; + + private static readonly Dictionary MorseToChar = new Dictionary(); + + static MorseCodeUtil() + { + // 构建反向映射 + foreach (var kvp in CharToMorse) + { + MorseToChar[kvp.Value] = kvp.Key; + } + } + + /// + /// 将文本编码为摩尔斯电码 + /// + /// 文本 + /// 字母分隔符(默认空格) + /// 单词分隔符(默认斜杠) + /// 摩尔斯电码 + public static string Encode(string text, string letterSeparator = " ", string wordSeparator = " / ") + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var result = new StringBuilder(); + bool prevWasSpace = false; + + foreach (char c in text) + { + if (c == ' ') + { + if (!prevWasSpace) + { + result.Append(wordSeparator); + prevWasSpace = true; + } + } + else + { + char upper = char.ToUpperInvariant(c); + if (CharToMorse.TryGetValue(upper, out string morse)) + { + if (result.Length > 0 && !prevWasSpace) + { + result.Append(letterSeparator); + } + result.Append(morse); + prevWasSpace = false; + } + // 忽略不支持的字符 + } + } + + return result.ToString().Trim(); + } + + /// + /// 将摩尔斯电码解码为文本 + /// + /// 摩尔斯电码 + /// 字母分隔符(默认空格) + /// 单词分隔符(默认斜杠) + /// 文本 + public static string Decode(string morse, string letterSeparator = " ", string wordSeparator = " / ") + { + if (string.IsNullOrEmpty(morse)) + return string.Empty; + + var result = new StringBuilder(); + + // 标准化分隔符 + string normalized = morse.Replace(wordSeparator, " / "); + normalized = normalized.Replace(letterSeparator, " "); + + // 替换多个空格为单个空格(除了单词分隔符) + while (normalized.Contains(" ")) + { + normalized = normalized.Replace(" ", " "); + } + + string[] parts = normalized.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (string part in parts) + { + if (part == "/") + { + result.Append(' '); + } + else if (MorseToChar.TryGetValue(part, out char c)) + { + result.Append(c); + } + else + { + // 未知码,保留原样 + result.Append(part); + } + } + + return result.ToString(); + } + + /// + /// 将文本编码为摩尔斯电码(使用标准分隔符) + /// + /// 文本 + /// 摩尔斯电码 + public static string TextToMorse(string text) + { + return Encode(text); + } + + /// + /// 将摩尔斯电码解码为文本 + /// + /// 摩尔斯电码 + /// 文本 + public static string MorseToText(string morse) + { + return Decode(morse); + } + + /// + /// 获取单个字符的摩尔斯电码 + /// + /// 字符 + /// 摩尔斯电码,如果不支持返回 null + public static string GetMorse(char c) + { + c = char.ToUpperInvariant(c); + return CharToMorse.TryGetValue(c, out string morse) ? morse : null; + } + + /// + /// 获取摩尔斯电码对应的字符 + /// + /// 摩尔斯电码 + /// 字符,如果无效返回 null + public static char? GetChar(string morse) + { + return MorseToChar.TryGetValue(morse, out char c) ? c : (char?)null; + } + + /// + /// 验证摩尔斯电码字符串是否有效 + /// + /// 摩尔斯电码 + /// 是否有效 + public static bool IsValidMorse(string morse) + { + if (string.IsNullOrEmpty(morse)) + return false; + + foreach (char c in morse) + { + if (c != '.' && c != '-' && c != ' ' && c != '/') + return false; + } + + return true; + } + + /// + /// 验证文本是否可以完全编码为摩尔斯电码 + /// + /// 文本 + /// 是否可以编码 + public static bool CanEncode(string text) + { + if (string.IsNullOrEmpty(text)) + return true; + + foreach (char c in text) + { + if (c == ' ') + continue; + + if (!CharToMorse.ContainsKey(char.ToUpperInvariant(c))) + return false; + } + + return true; + } + + /// + /// 获取不支持的字符 + /// + /// 文本 + /// 不支持的字符列表 + public static List GetUnsupportedChars(string text) + { + var unsupported = new List(); + + if (string.IsNullOrEmpty(text)) + return unsupported; + + foreach (char c in text) + { + if (c == ' ') + continue; + + if (!CharToMorse.ContainsKey(char.ToUpperInvariant(c)) && !unsupported.Contains(c)) + { + unsupported.Add(c); + } + } + + return unsupported; + } + + /// + /// 将摩尔斯电码转换为音频信号参数 + /// + /// 摩尔斯电码 + /// 点持续时间(毫秒) + /// 信号参数列表(true = 信号,false = 停顿,后面跟持续时间) + public static List<(bool Signal, int DurationMs)> ToSignalTiming(string morse, int dotDuration = 100) + { + var timing = new List<(bool Signal, int DurationMs)>(); + + if (string.IsNullOrEmpty(morse)) + return timing; + + foreach (char c in morse) + { + switch (c) + { + case '.': + timing.Add((true, dotDuration)); // 点 + timing.Add((false, dotDuration)); // 点间停顿 + break; + case '-': + timing.Add((true, dotDuration * 3)); // 划 + timing.Add((false, dotDuration)); // 点间停顿 + break; + case ' ': + // 单词间停顿(减去前面的点间停顿) + if (timing.Count > 0 && !timing[timing.Count - 1].Signal) + { + timing[timing.Count - 1] = (false, dotDuration * 6); + } + else + { + timing.Add((false, dotDuration * 7)); + } + break; + case '/': + timing.Add((false, dotDuration * 7)); // 单词间停顿 + break; + } + } + + return timing; + } + + /// + /// 获取支持的字符列表 + /// + /// 支持的字符 + public static string GetSupportedChars() + { + var chars = new StringBuilder(); + foreach (var c in CharToMorse.Keys) + { + chars.Append(c); + } + return chars.ToString(); + } + + /// + /// 获取摩尔斯电码表 + /// + /// 字符到摩尔斯电码的映射 + public static Dictionary GetMorseTable() + { + return new Dictionary(CharToMorse); + } + } +} diff --git a/EasyTool.Core/CodeCategory/MurmurHashUtil.cs b/EasyTool.Core/CodeCategory/MurmurHashUtil.cs new file mode 100644 index 0000000..ee8cb98 --- /dev/null +++ b/EasyTool.Core/CodeCategory/MurmurHashUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// MurmurHash 高性能非加密哈希工具类 + /// MurmurHash 是一种非加密型哈希函数,适用于一般的哈希检索操作 + /// 特点:高随机分布、高性能、低碰撞率 + /// + public static class MurmurHashUtil + { + #region MurmurHash3 32-bit + + private const uint C1_32 = 0xcc9e2d51; + private const uint C2_32 = 0x1b873593; + private const uint R1_32 = 15; + private const uint R2_32 = 13; + private const uint M_32 = 5; + private const uint N_32 = 0xe6546b64; + + /// + /// 计算 MurmurHash3 32位哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32位哈希值 + public static uint Hash32(byte[] data, uint seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + uint h = seed; + int length = data.Length; + int blocks = length / 4; + + // 处理4字节块 + for (int i = 0; i < blocks; i++) + { + int blockOffset = i * 4; + uint k = (uint)(data[blockOffset] | (data[blockOffset + 1] << 8) | (data[blockOffset + 2] << 16) | (data[blockOffset + 3] << 24)); + k *= C1_32; + k = RotateLeft32(k, (int)R1_32); + k *= C2_32; + + h ^= k; + h = RotateLeft32(h, (int)R2_32); + h = h * M_32 + N_32; + } + + // 处理剩余字节 + int remaining = length % 4; + int offset = blocks * 4; + uint tail = 0; + + switch (remaining) + { + case 3: + tail ^= (uint)data[offset + 2] << 16; + goto case 2; + case 2: + tail ^= (uint)data[offset + 1] << 8; + goto case 1; + case 1: + tail ^= data[offset]; + tail *= C1_32; + tail = RotateLeft32(tail, (int)R1_32); + tail *= C2_32; + h ^= tail; + break; + } + + // 最终混合 + h ^= (uint)length; + h = FinalMix32(h); + + return h; + } + + /// + /// 计算字符串的 MurmurHash3 32位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 32位哈希值 + public static uint Hash32(string text, uint seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash32(encoding.GetBytes(text), seed); + } + + private static uint FinalMix32(uint h) + { + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; + } + + #endregion + + #region MurmurHash3 64-bit + + private const ulong C1_64 = 0x87c37b91114253d5; + private const ulong C2_64 = 0x4cf5ad432745937f; + private const int R1_64 = 31; + private const int R2_64 = 27; + private const ulong M_64 = 5; + private const ulong N1_64 = 0x52dce729; + private const ulong N2_64 = 0x38495ab5; + + /// + /// 计算 MurmurHash3 64位哈希值(128位截断为64位) + /// + /// 输入数据 + /// 种子值(默认0) + /// 64位哈希值 + public static ulong Hash64(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + return h1 ^ h2; + } + + /// + /// 计算字符串的 MurmurHash3 64位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 64位哈希值 + public static ulong Hash64(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash64(encoding.GetBytes(text), seed); + } + + #endregion + + #region MurmurHash3 128-bit + + /// + /// 计算 MurmurHash3 128位哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 128位哈希值(两个64位值) + public static (ulong H1, ulong H2) Hash128(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return (0, 0); + + ulong h1 = seed; + ulong h2 = seed; + int length = data.Length; + int blocks = length / 16; + + // 处理16字节块 + for (int i = 0; i < blocks; i++) + { + ulong k1 = BitConverter.ToUInt64(data, i * 16); + ulong k2 = BitConverter.ToUInt64(data, i * 16 + 8); + + k1 *= C1_64; + k1 = RotateLeft64(k1, R1_64); + k1 *= C2_64; + h1 ^= k1; + + h1 = RotateLeft64(h1, R2_64); + h1 += h2; + h1 = h1 * M_64 + N1_64; + + k2 *= C2_64; + k2 = RotateLeft64(k2, R2_64); + k2 *= C1_64; + h2 ^= k2; + + h2 = RotateLeft64(h2, R1_64); + h2 += h1; + h2 = h2 * M_64 + N2_64; + } + + // 处理剩余字节 + int remaining = length % 16; + int offset = blocks * 16; + ulong tail1 = 0; + ulong tail2 = 0; + + switch (remaining) + { + case 15: + tail2 ^= (ulong)data[offset + 14] << 48; + goto case 14; + case 14: + tail2 ^= (ulong)data[offset + 13] << 40; + goto case 13; + case 13: + tail2 ^= (ulong)data[offset + 12] << 32; + goto case 12; + case 12: + tail2 ^= (ulong)data[offset + 11] << 24; + goto case 11; + case 11: + tail2 ^= (ulong)data[offset + 10] << 16; + goto case 10; + case 10: + tail2 ^= (ulong)data[offset + 9] << 8; + goto case 9; + case 9: + tail2 ^= data[offset + 8]; + tail2 *= C2_64; + tail2 = RotateLeft64(tail2, R2_64); + tail2 *= C1_64; + h2 ^= tail2; + goto case 8; + case 8: + tail1 ^= (ulong)data[offset + 7] << 56; + goto case 7; + case 7: + tail1 ^= (ulong)data[offset + 6] << 48; + goto case 6; + case 6: + tail1 ^= (ulong)data[offset + 5] << 40; + goto case 5; + case 5: + tail1 ^= (ulong)data[offset + 4] << 32; + goto case 4; + case 4: + tail1 ^= (ulong)data[offset + 3] << 24; + goto case 3; + case 3: + tail1 ^= (ulong)data[offset + 2] << 16; + goto case 2; + case 2: + tail1 ^= (ulong)data[offset + 1] << 8; + goto case 1; + case 1: + tail1 ^= data[offset]; + tail1 *= C1_64; + tail1 = RotateLeft64(tail1, R1_64); + tail1 *= C2_64; + h1 ^= tail1; + break; + } + + // 最终混合 + h1 ^= (ulong)length; + h2 ^= (ulong)length; + + h1 += h2; + h2 += h1; + + h1 = FinalMix64(h1); + h2 = FinalMix64(h2); + + h1 += h2; + h2 += h1; + + return (h1, h2); + } + + /// + /// 计算字符串的 MurmurHash3 128位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 128位哈希值(两个64位值) + public static (ulong H1, ulong H2) Hash128(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + encoding ??= Encoding.UTF8; + return Hash128(encoding.GetBytes(text), seed); + } + + /// + /// 计算 MurmurHash3 128位哈希值并返回字节数组 + /// + /// 输入数据 + /// 种子值(默认0) + /// 16字节的哈希值 + public static byte[] Hash128Bytes(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + var result = new byte[16]; + Array.Copy(BitConverter.GetBytes(h1), 0, result, 0, 8); + Array.Copy(BitConverter.GetBytes(h2), 0, result, 8, 8); + return result; + } + + /// + /// 计算 MurmurHash3 128位哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32字符的十六进制字符串 + public static string Hash128Hex(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + return h1.ToString("x16") + h2.ToString("x16"); + } + + private static ulong FinalMix64(ulong k) + { + k ^= k >> 33; + k *= 0xff51afd7ed558ccd; + k ^= k >> 33; + k *= 0xc4ceb9fe1a85ec53; + k ^= k >> 33; + return k; + } + + #endregion + + #region 辅助方法 + + private static uint RotateLeft32(uint x, int r) + { + return (x << r) | (x >> (32 - r)); + } + + private static ulong RotateLeft64(ulong x, int r) + { + return (x << r) | (x >> (64 - r)); + } + + #endregion + + #region 一致性哈希支持 + + /// + /// 计算一致性哈希位置(用于分布式系统) + /// + /// 键值 + /// 桶的数量 + /// 桶的索引(0 到 buckets-1) + public static int ConsistentHash(string key, int buckets) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (buckets <= 0) + throw new ArgumentException("Buckets must be greater than 0", nameof(buckets)); + + uint hash = Hash32(key); + return (int)(hash % (uint)buckets); + } + + /// + /// 计算一致性哈希位置(带虚拟节点) + /// + /// 键值 + /// 桶的数量 + /// 每个桶的虚拟节点数 + /// 桶的索引(0 到 buckets-1) + public static int ConsistentHash(string key, int buckets, int virtualNodes) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (buckets <= 0) + throw new ArgumentException("Buckets must be greater than 0", nameof(buckets)); + if (virtualNodes <= 0) + throw new ArgumentException("Virtual nodes must be greater than 0", nameof(virtualNodes)); + + uint hash = Hash32(key); + int totalNodes = buckets * virtualNodes; + int nodeIndex = (int)(hash % (uint)totalNodes); + return nodeIndex / virtualNodes; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/NanoIdUtil.cs b/EasyTool.Core/CodeCategory/NanoIdUtil.cs new file mode 100644 index 0000000..c6a4302 --- /dev/null +++ b/EasyTool.Core/CodeCategory/NanoIdUtil.cs @@ -0,0 +1,197 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// NanoId 生成器工具类 + /// NanoId 是一个小巧、安全、URL友好的唯一字符串ID生成器 + /// + public static class NanoIdUtil + { + // 默认字母表(URL安全) + private const string DefaultAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + // 数字字母表(仅数字) + private const string NumbersAlphabet = "0123456789"; + + // 小写字母表 + private const string LowercaseAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; + + // 无歧义字符字母表(排除 l, 1, I, O, 0 等) + private const string NoDoppelgangersAlphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"; + + // 密码安全字母表(包含特殊字符) + private const string PasswordAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}|;:,.<>?"; + + /// + /// 生成默认长度的 NanoId(21位) + /// + /// NanoId 字符串 + public static string Generate() + { + return Generate(21); + } + + /// + /// 生成指定长度的 NanoId + /// + /// ID长度 + /// NanoId 字符串 + public static string Generate(int size) + { + return Generate(size, DefaultAlphabet); + } + + /// + /// 生成指定长度和字母表的 NanoId + /// + /// ID长度 + /// 自定义字母表 + /// NanoId 字符串 + public static string Generate(int size, string alphabet) + { + if (size <= 0) + throw new ArgumentException("Size must be greater than 0", nameof(size)); + if (string.IsNullOrEmpty(alphabet)) + throw new ArgumentException("Alphabet cannot be null or empty", nameof(alphabet)); + + return GenerateImpl(size, alphabet); + } + + /// + /// 生成仅数字的 NanoId + /// + /// ID长度(默认21) + /// 仅数字的 ID + public static string GenerateNumbers(int size = 21) + { + return Generate(size, NumbersAlphabet); + } + + /// + /// 生成小写字母+数字的 NanoId + /// + /// ID长度(默认21) + /// 小写字母数字 ID + public static string GenerateLowercase(int size = 21) + { + return Generate(size, LowercaseAlphabet); + } + + /// + /// 生成无歧义字符的 NanoId(排除 l, 1, I, O, 0 等) + /// + /// ID长度(默认21) + /// 无歧义字符的 ID + public static string GenerateNoDoppelgangers(int size = 21) + { + return Generate(size, NoDoppelgangersAlphabet); + } + + /// + /// 生成密码安全的 NanoId(包含特殊字符) + /// + /// ID长度(默认21) + /// 包含特殊字符的 ID + public static string GeneratePassword(int size = 21) + { + return Generate(size, PasswordAlphabet); + } + + /// + /// 生成指定长度的自定义 NanoId(使用自定义随机数生成器) + /// + /// ID长度 + /// 自定义字母表 + /// 自定义随机数生成器 + /// NanoId 字符串 + public static string Generate(int size, string alphabet, Random random) + { + if (size <= 0) + throw new ArgumentException("Size must be greater than 0", nameof(size)); + if (string.IsNullOrEmpty(alphabet)) + throw new ArgumentException("Alphabet cannot be null or empty", nameof(alphabet)); + if (random == null) + throw new ArgumentNullException(nameof(random)); + + var chars = new char[size]; + for (int i = 0; i < size; i++) + { + chars[i] = alphabet[random.Next(alphabet.Length)]; + } + return new string(chars); + } + + /// + /// 异步生成 NanoId(适用于大量生成场景) + /// + /// ID长度(默认21) + /// NanoId 字符串 + public static string GenerateAsync(int size = 21) + { + return Generate(size); + } + + /// + /// 批量生成 NanoId + /// + /// 生成数量 + /// 每个ID的长度(默认21) + /// NanoId 数组 + public static string[] GenerateBatch(int count, int size = 21) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(size); + } + return result; + } + + #region 私有实现 + + private static string GenerateImpl(int size, string alphabet) + { + // 计算掩码 + int mask = (2 << (int)Math.Floor(Math.Log(alphabet.Length - 1) / Math.Log(2))) - 1; + // 计算每个字符需要的平均字节数 + int step = (int)Math.Ceiling(1.6 * mask * size / alphabet.Length); + + var result = new char[size]; + int pos = 0; + + using (var rng = RandomNumberGenerator.Create()) + { + byte[] buffer = new byte[step]; + + while (true) + { + rng.GetBytes(buffer); + + for (int i = 0; i < step && pos < size; i++) + { + int index = buffer[i] & mask; + + if (index < alphabet.Length) + { + result[pos++] = alphabet[index]; + } + } + + if (pos >= size) + { + break; + } + } + } + + return new string(result); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/PunycodeUtil.cs b/EasyTool.Core/CodeCategory/PunycodeUtil.cs new file mode 100644 index 0000000..85808a2 --- /dev/null +++ b/EasyTool.Core/CodeCategory/PunycodeUtil.cs @@ -0,0 +1,367 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Punycode 编码工具类 + /// Punycode 是一种将 Unicode 字符串转换为 ASCII 的编码方案 + /// 主要用于国际化域名(IDN),如 "例子.测试" → "xn--fsqu00a.xn--0zwm56d" + /// RFC 3492 标准 + /// + public static class PunycodeUtil + { + private const int Base = 36; + private const int TMin = 1; + private const int TMax = 26; + private const int Skew = 38; + private const int Damp = 700; + private const int InitialBias = 72; + private const int InitialN = 0x80; + private const int Delimiter = '-'; + + private const string Base36Chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + + /// + /// 将 Unicode 字符串编码为 Punycode + /// + /// Unicode 字符串 + /// Punycode 编码字符串 + public static string Encode(string input) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + // 检查是否全是 ASCII + bool allAscii = true; + foreach (char c in input) + { + if (c > 0x7F) + { + allAscii = false; + break; + } + } + + if (allAscii) + return input; + + var result = new StringBuilder(); + int n = InitialN; + int delta = 0; + int bias = InitialBias; + int h = 0; + + // 处理基本字符(ASCII) + foreach (char c in input) + { + if (c < 0x80) + { + result.Append(c); + h++; + } + } + + int b = h; + if (b > 0) + { + result.Append((char)Delimiter); + } + + int inputLength = input.Length; + int m = 0; + + while (h < inputLength) + { + // 找到最小的非基本字符 + m = int.MaxValue; + foreach (char c in input) + { + if (c >= n && c < m) + { + m = c; + } + } + + delta += (m - n) * (h + 1); + n = m; + + foreach (char c in input) + { + if (c < n) + { + delta++; + } + else if (c == n) + { + int q = delta; + int k = Base; + + while (true) + { + int t = k <= bias ? TMin : (k >= bias + TMax ? TMax : k - bias); + if (q < t) + break; + + result.Append(Base36Chars[t + (q - t) % (Base - t)]); + q = (q - t) / (Base - t); + k += Base; + } + + result.Append(Base36Chars[q]); + bias = Adapt(delta, h + 1, h == b); + delta = 0; + h++; + } + } + + delta++; + n++; + } + + return result.ToString(); + } + + /// + /// 将 Punycode 字符串解码为 Unicode + /// + /// Punycode 编码字符串 + /// Unicode 字符串 + public static string Decode(string input) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + // 查找分隔符位置 + int delimiterPos = input.LastIndexOf((char)Delimiter); + + var result = new StringBuilder(); + + // 处理基本字符 + if (delimiterPos > 0) + { + for (int idx = 0; idx < delimiterPos; idx++) + { + char c = input[idx]; + if (c < 0x80) + { + result.Append(c); + } + else + { + throw new ArgumentException("Invalid Punycode string: non-ASCII character in basic part"); + } + } + } + + int i = 0; + int n = InitialN; + int bias = InitialBias; + int pos = delimiterPos + 1; + + while (pos < input.Length) + { + int oldi = i; + int w = 1; + + for (int k = Base; ; k += Base) + { + if (pos >= input.Length) + throw new ArgumentException("Invalid Punycode string: unexpected end"); + + char c = input[pos++]; + int digit = DecodeDigit(c); + + if (digit > (int.MaxValue - i) / w) + throw new ArgumentException("Invalid Punycode string: overflow"); + + i += digit * w; + + int t = k <= bias ? TMin : (k >= bias + TMax ? TMax : k - bias); + + if (digit < t) + break; + + if (w > int.MaxValue / (Base - t)) + throw new ArgumentException("Invalid Punycode string: overflow"); + + w *= (Base - t); + } + + bias = Adapt(i - oldi, result.Length + 1, oldi == 0); + + if (i / (result.Length + 1) > int.MaxValue - n) + throw new ArgumentException("Invalid Punycode string: overflow"); + + n += i / (result.Length + 1); + i %= (result.Length + 1); + + result.Insert(i, (char)n); + i++; + } + + return result.ToString(); + } + + /// + /// 将域名编码为 IDN 格式(带 xn-- 前缀) + /// + /// Unicode 域名 + /// ASCII 域名 + public static string EncodeDomain(string domain) + { + if (string.IsNullOrEmpty(domain)) + return string.Empty; + + var parts = domain.Split('.'); + var result = new StringBuilder(); + + for (int i = 0; i < parts.Length; i++) + { + if (i > 0) + result.Append('.'); + + string encoded = Encode(parts[i]); + + // 如果包含非 ASCII 字符,添加 xn-- 前缀 + bool needsPrefix = false; + foreach (char c in parts[i]) + { + if (c > 0x7F) + { + needsPrefix = true; + break; + } + } + + if (needsPrefix) + { + result.Append("xn--"); + result.Append(encoded); + } + else + { + result.Append(parts[i]); + } + } + + return result.ToString(); + } + + /// + /// 将 IDN 域名解码为 Unicode 格式 + /// + /// ASCII 域名 + /// Unicode 域名 + public static string DecodeDomain(string domain) + { + if (string.IsNullOrEmpty(domain)) + return string.Empty; + + var parts = domain.Split('.'); + var result = new StringBuilder(); + + for (int i = 0; i < parts.Length; i++) + { + if (i > 0) + result.Append('.'); + + string part = parts[i]; + + // 检查是否有 xn-- 前缀(不区分大小写) + if (part.Length > 4 && + part.StartsWith("xn--", StringComparison.OrdinalIgnoreCase)) + { + string punycode = part.Substring(4); + result.Append(Decode(punycode)); + } + else + { + result.Append(part); + } + } + + return result.ToString(); + } + + /// + /// 验证 Punycode 字符串是否有效 + /// + /// Punycode 字符串 + /// 是否有效 + public static bool IsValid(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + try + { + string decoded = Decode(input); + string reencoded = Encode(decoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 尝试解码 Punycode 字符串 + /// + /// Punycode 字符串 + /// 解码结果 + /// 是否解码成功 + public static bool TryDecode(string input, out string result) + { + result = null; + + if (string.IsNullOrEmpty(input)) + { + result = string.Empty; + return true; + } + + try + { + result = Decode(input); + return true; + } + catch + { + return false; + } + } + + #region 私有方法 + + private static int Adapt(int delta, int numpoints, bool firsttime) + { + delta = firsttime ? delta / Damp : delta / 2; + delta += delta / numpoints; + + int k = 0; + while (delta > ((Base - TMin) * TMax) / 2) + { + delta /= Base - TMin; + k += Base; + } + + return k + (Base - TMin + 1) * delta / (delta + Skew); + } + + private static int DecodeDigit(char c) + { + if (c >= 'a' && c <= 'z') + return c - 'a'; + if (c >= 'A' && c <= 'Z') + return c - 'A'; + if (c >= '0' && c <= '9') + return c - '0' + 26; + + throw new ArgumentException($"Invalid Punycode character: {c}"); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs b/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs new file mode 100644 index 0000000..3f0dfdd --- /dev/null +++ b/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs @@ -0,0 +1,236 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Quoted-Printable 编码工具类 + /// Quoted-Printable 是一种将 8 位数据编码为 7 位 ASCII 的编码方式 + /// 常用于电子邮件(MIME),将非 ASCII 字符编码为 =XX 格式 + /// RFC 2045 标准 + /// + public static class QuotedPrintableUtil + { + private const int MaxLineLength = 76; + + /// + /// 将字节数组编码为 Quoted-Printable 字符串 + /// + /// 要编码的数据 + /// 每行最大长度 + /// Quoted-Printable 编码字符串 + public static string Encode(byte[] data, int lineLength = 76) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + int currentLineLength = 0; + + foreach (byte b in data) + { + string encoded = EncodeByte(b); + int encodedLength = encoded.Length; + + // 检查是否需要换行 + if (currentLineLength + encodedLength > lineLength - 1) + { + result.Append("=\r\n"); + currentLineLength = 0; + } + + result.Append(encoded); + currentLineLength += encodedLength; + } + + return result.ToString(); + } + + /// + /// 将 Quoted-Printable 字符串解码为字节数组 + /// + /// Quoted-Printable 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + + for (int i = 0; i < encoded.Length; i++) + { + char c = encoded[i]; + + if (c == '=') + { + if (i + 1 >= encoded.Length) + break; + + char next = encoded[i + 1]; + + // 软换行 + if (next == '\r' || next == '\n') + { + i++; + if (next == '\r' && i + 1 < encoded.Length && encoded[i + 1] == '\n') + i++; + continue; + } + + // 编码字符 + if (i + 2 < encoded.Length) + { + string hex = encoded.Substring(i + 1, 2); + if (TryParseHex(hex, out byte b)) + { + result.Add(b); + i += 2; + continue; + } + } + } + else if (c != '\r' && c != '\n') + { + result.Add((byte)c); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Quoted-Printable(使用指定编码) + /// + /// 文本 + /// 编码方式 + /// 每行最大长度 + /// Quoted-Printable 编码字符串 + public static string EncodeString(string text, Encoding encoding = null, int lineLength = 76) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = encoding.GetBytes(text); + return Encode(bytes, lineLength); + } + + /// + /// 将 Quoted-Printable 字符串解码为文本(使用指定编码) + /// + /// Quoted-Printable 编码字符串 + /// 编码方式 + /// 解码后的文本 + public static string DecodeToString(string encoded, Encoding encoding = null) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + encoding ??= Encoding.UTF8; + return encoding.GetString(bytes); + } + + /// + /// 验证 Quoted-Printable 字符串是否有效 + /// + /// Quoted-Printable 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return true; + + for (int i = 0; i < encoded.Length; i++) + { + char c = encoded[i]; + + if (c == '=') + { + if (i + 1 >= encoded.Length) + return false; + + char next = encoded[i + 1]; + + // 软换行 + if (next == '\r' || next == '\n') + continue; + + // 编码字符 + if (i + 2 >= encoded.Length) + return false; + + string hex = encoded.Substring(i + 1, 2); + if (!IsHexDigit(hex[0]) || !IsHexDigit(hex[1])) + return false; + + i += 2; + } + else if (c < 32 && c != '\r' && c != '\n' && c != '\t') + { + return false; + } + else if (c > 126) + { + return false; + } + } + + return true; + } + + private static string EncodeByte(byte b) + { + // 可打印 ASCII 字符(33-126,除了 61 '=') + if (b >= 33 && b <= 126 && b != 61) + { + return ((char)b).ToString(); + } + + // 制表符和空格(特殊处理) + if (b == 9 || b == 32) + { + return "=" + b.ToString("X2"); + } + + // 其他字符编码为 =XX + return "=" + b.ToString("X2"); + } + + private static bool TryParseHex(string hex, out byte result) + { + result = 0; + + if (hex.Length != 2) + return false; + + if (!IsHexDigit(hex[0]) || !IsHexDigit(hex[1])) + return false; + + result = Convert.ToByte(hex, 16); + return true; + } + + private static bool IsHexDigit(char c) + { + return (c >= '0' && c <= '9') || + (c >= 'A' && c <= 'F') || + (c >= 'a' && c <= 'f'); + } + + /// + /// 获取 Quoted-Printable 编码后的预计最大长度 + /// + /// 输入长度 + /// 最大输出长度 + public static int CalculateMaxEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // 最坏情况:每个字符都编码为 =XX,加上软换行 + return inputLength * 3 + (inputLength * 3 / MaxLineLength) * 3; + } + } +} diff --git a/EasyTool.Core/CodeCategory/RC4Util.cs b/EasyTool.Core/CodeCategory/RC4Util.cs new file mode 100644 index 0000000..32d19f0 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RC4Util.cs @@ -0,0 +1,303 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// RC4 流加密工具类 + /// RC4 是一种广泛使用的流密码,由 Ron Rivest 设计 + /// 注意:RC4 已被认为不安全,建议使用 ChaCha20 替代 + /// 保留用于兼容旧系统 + /// + public static class RC4Util + { + /// + /// 使用 RC4 加密/解密数据(对称操作) + /// + /// 输入数据 + /// 密钥(1-256字节) + /// 加密/解密后的数据 + public static byte[] Process(byte[] data, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + return Process(data, 0, data.Length, key); + } + + /// + /// 使用 RC4 加密/解密数据 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 密钥 + /// 加密/解密后的数据 + public static byte[] Process(byte[] data, int offset, int length, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte[] result = new byte[length]; + byte[] s = new byte[256]; + byte[] k = new byte[256]; + + // 密钥调度算法(KSA) + for (int i = 0; i < 256; i++) + { + s[i] = (byte)i; + k[i] = key[i % key.Length]; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + s[i] + k[i]) & 0xFF; + Swap(ref s[i], ref s[j]); + } + + // 伪随机生成算法(PRGA) + int a = 0; + int b = 0; + + for (int i = 0; i < length; i++) + { + a = (a + 1) & 0xFF; + b = (b + s[a]) & 0xFF; + Swap(ref s[a], ref s[b]); + byte t = (byte)((s[a] + s[b]) & 0xFF); + result[i] = (byte)(data[offset + i] ^ s[t]); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Process(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Process(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 加密字符串并返回十六进制 + /// + /// 明文 + /// 密钥 + /// 十六进制密文 + public static string EncryptToHex(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Process(data, key); + return BitConverter.ToString(encrypted).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制解密字符串 + /// + /// 十六进制密文 + /// 密钥 + /// 明文字符串 + public static string DecryptFromHex(string cipherHex, byte[] key) + { + if (string.IsNullOrEmpty(cipherHex)) + return string.Empty; + + byte[] data = HexToBytes(cipherHex); + byte[] decrypted = Process(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度(1-256) + /// 随机密钥 + public static byte[] GenerateKey(int length = 16) + { + if (length < 1 || length > 256) + throw new ArgumentException("Key length must be between 1 and 256", nameof(length)); + + byte[] key = new byte[length]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 密钥长度 + /// 十六进制密钥 + public static string GenerateKeyHex(int length = 16) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 创建 RC4 流处理器(用于流式处理) + /// + /// 密钥 + /// RC4 处理器 + public static RC4Processor CreateProcessor(byte[] key) + { + return new RC4Processor(key); + } + + private static void Swap(ref byte a, ref byte b) + { + byte temp = a; + a = b; + b = temp; + } + + private static byte[] HexToBytes(string hex) + { + if (hex.Length % 2 != 0) + hex = "0" + hex; + + byte[] bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return bytes; + } + } + + /// + /// RC4 流处理器(支持状态保持) + /// + public class RC4Processor + { + private readonly byte[] _s = new byte[256]; + private int _i; + private int _j; + + /// + /// 创建 RC4 处理器 + /// + /// 密钥 + public RC4Processor(byte[] key) + { + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + // KSA + for (int i = 0; i < 256; i++) + { + _s[i] = (byte)i; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + _s[i] + key[i % key.Length]) & 0xFF; + Swap(ref _s[i], ref _s[j]); + } + + _i = 0; + _j = 0; + } + + /// + /// 处理一个字节 + /// + /// 输入字节 + /// 输出字节 + public byte ProcessByte(byte input) + { + _i = (_i + 1) & 0xFF; + _j = (_j + _s[_i]) & 0xFF; + Swap(ref _s[_i], ref _s[_j]); + byte t = (byte)((_s[_i] + _s[_j]) & 0xFF); + return (byte)(input ^ _s[t]); + } + + /// + /// 处理多个字节 + /// + /// 输入数据 + /// 输出数据 + public byte[] Process(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = ProcessByte(data[i]); + } + return result; + } + + /// + /// 重置处理器状态 + /// + /// 密钥 + public void Reset(byte[] key) + { + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + for (int i = 0; i < 256; i++) + { + _s[i] = (byte)i; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + _s[i] + key[i % key.Length]) & 0xFF; + Swap(ref _s[i], ref _s[j]); + } + + _i = 0; + _j = 0; + } + + private static void Swap(ref byte a, ref byte b) + { + byte temp = a; + a = b; + b = temp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/RIPEMD160Util.cs b/EasyTool.Core/CodeCategory/RIPEMD160Util.cs new file mode 100644 index 0000000..ef0ed12 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RIPEMD160Util.cs @@ -0,0 +1,362 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// RIPEMD-160 哈希工具类 + /// RIPEMD-160 是一种 160 位加密哈希函数 + /// 由欧洲 RIPE 项目开发,比特币地址使用此算法 + /// 比 SHA-1 更安全 + /// + public static class RIPEMD160Util + { + private const int DigestSize = 20; + private const int BlockSize = 64; + + // 初始值 + private static readonly uint[] IV = new uint[] + { + 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, + 0x7658def0, 0x890abc12, 0xfedcba34, 0x01234567, 0x32107654 + }; + + /// + /// 计算 RIPEMD-160 哈希值 + /// + /// 输入数据 + /// 20字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + data = Array.Empty(); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算 RIPEMD-160 哈希值 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 20字节哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint[] h = new uint[10]; + Array.Copy(IV, h, 10); + + byte[] padded = PadMessage(data, offset, length); + int blocks = padded.Length / BlockSize; + + for (int i = 0; i < blocks; i++) + { + ProcessBlock(padded, i * BlockSize, h); + } + + byte[] result = new byte[DigestSize]; + for (int i = 0; i < 5; i++) + { + result[i * 4] = (byte)h[i]; + result[i * 4 + 1] = (byte)(h[i] >> 8); + result[i * 4 + 2] = (byte)(h[i] >> 16); + result[i * 4 + 3] = (byte)(h[i] >> 24); + } + + return result; + } + + /// + /// 计算字符串的 RIPEMD-160 哈希值 + /// + /// 文本 + /// 20字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 RIPEMD-160 哈希的十六进制表示 + /// + /// 输入数据 + /// 40字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 计算字符串的 RIPEMD-160 哈希十六进制表示 + /// + /// 文本 + /// 40字符的十六进制字符串 + public static string ComputeStringHex(string text) + { + byte[] hash = ComputeString(text); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + private static byte[] PadMessage(byte[] data, int offset, int length) + { + long bitLength = (long)length * 8; + int padding = 64 - ((length + 9) % 64); + if (padding == 64) padding = 0; + + byte[] result = new byte[length + 1 + padding + 8]; + Array.Copy(data, offset, result, 0, length); + + result[length] = 0x80; + + // 添加长度(小端序) + for (int i = 0; i < 8; i++) + { + result[result.Length - 8 + i] = (byte)(bitLength >> (i * 8)); + } + + return result; + } + + private static void ProcessBlock(byte[] block, int offset, uint[] h) + { + uint[] x = new uint[16]; + for (int i = 0; i < 16; i++) + { + x[i] = BitConverter.ToUInt32(block, offset + i * 4); + } + + uint al = h[0], bl = h[1], cl = h[2], dl = h[3], el = h[4]; + uint ar = h[5], br = h[6], cr = h[7], dr = h[8], er = h[9]; + + // 左侧 + al = F1(al, bl, cl, dl, el, x[0], 11); + el = F1(el, al, bl, cl, dl, x[1], 14); + dl = F1(dl, el, al, bl, cl, x[2], 15); + cl = F1(cl, dl, el, al, bl, x[3], 12); + bl = F1(bl, cl, dl, el, al, x[4], 5); + al = F1(al, bl, cl, dl, el, x[5], 8); + el = F1(el, al, bl, cl, dl, x[6], 7); + dl = F1(dl, el, al, bl, cl, x[7], 9); + cl = F1(cl, dl, el, al, bl, x[8], 11); + bl = F1(bl, cl, dl, el, al, x[9], 13); + al = F1(al, bl, cl, dl, el, x[10], 14); + el = F1(el, al, bl, cl, dl, x[11], 15); + dl = F1(dl, el, al, bl, cl, x[12], 6); + cl = F1(cl, dl, el, al, bl, x[13], 7); + bl = F1(bl, cl, dl, el, al, x[14], 9); + al = F1(al, bl, cl, dl, el, x[15], 8); + + // 右侧 + ar = F5(ar, br, cr, dr, er, x[5], 8); + er = F5(er, ar, br, cr, dr, x[14], 9); + dr = F5(dr, er, ar, br, cr, x[7], 9); + cr = F5(cr, dr, er, ar, br, x[0], 11); + br = F5(br, cr, dr, er, ar, x[9], 13); + ar = F5(ar, br, cr, dr, er, x[2], 15); + er = F5(er, ar, br, cr, dr, x[11], 15); + dr = F5(dr, er, ar, br, cr, x[4], 5); + cr = F5(cr, dr, er, ar, br, x[13], 7); + br = F5(br, cr, dr, er, ar, x[6], 7); + ar = F5(ar, br, cr, dr, er, x[15], 8); + er = F5(er, ar, br, cr, dr, x[8], 11); + dr = F5(dr, er, ar, br, cr, x[1], 14); + cr = F5(cr, dr, er, ar, br, x[10], 14); + br = F5(br, cr, dr, er, ar, x[3], 12); + ar = F5(ar, br, cr, dr, er, x[12], 6); + + // 第二轮左侧 + bl = F2(bl, cl, dl, el, al, x[7], 7); + al = F2(al, bl, cl, dl, el, x[4], 6); + el = F2(el, al, bl, cl, dl, x[13], 8); + dl = F2(dl, el, al, bl, cl, x[1], 13); + cl = F2(cl, dl, el, al, bl, x[10], 11); + bl = F2(bl, cl, dl, el, al, x[6], 9); + al = F2(al, bl, cl, dl, el, x[15], 7); + el = F2(el, al, bl, cl, dl, x[3], 15); + dl = F2(dl, el, al, bl, cl, x[12], 7); + cl = F2(cl, dl, el, al, bl, x[0], 12); + bl = F2(bl, cl, dl, el, al, x[9], 15); + al = F2(al, bl, cl, dl, el, x[5], 9); + el = F2(el, al, bl, cl, dl, x[2], 11); + dl = F2(dl, el, al, bl, cl, x[14], 7); + cl = F2(cl, dl, el, al, bl, x[11], 13); + bl = F2(bl, cl, dl, el, al, x[8], 12); + + // 第二轮右侧 + br = F4(br, cr, dr, er, ar, x[6], 9); + ar = F4(ar, br, cr, dr, er, x[11], 13); + er = F4(er, ar, br, cr, dr, x[3], 15); + dr = F4(dr, er, ar, br, cr, x[7], 7); + cr = F4(cr, dr, er, ar, br, x[0], 12); + br = F4(br, cr, dr, er, ar, x[13], 8); + ar = F4(ar, br, cr, dr, er, x[5], 9); + er = F4(er, ar, br, cr, dr, x[10], 11); + dr = F4(dr, er, ar, br, cr, x[14], 7); + cr = F4(cr, dr, er, ar, br, x[15], 7); + br = F4(br, cr, dr, er, ar, x[8], 12); + ar = F4(ar, br, cr, dr, er, x[12], 7); + er = F4(er, ar, br, cr, dr, x[4], 6); + dr = F4(dr, er, ar, br, cr, x[9], 15); + cr = F4(cr, dr, er, ar, br, x[1], 13); + br = F4(br, cr, dr, er, ar, x[2], 11); + + // 第三轮左侧 + cl = F3(cl, dl, el, al, bl, x[3], 11); + bl = F3(bl, cl, dl, el, al, x[10], 13); + al = F3(al, bl, cl, dl, el, x[14], 6); + el = F3(el, al, bl, cl, dl, x[4], 7); + dl = F3(dl, el, al, bl, cl, x[9], 14); + cl = F3(cl, dl, el, al, bl, x[15], 9); + bl = F3(bl, cl, dl, el, al, x[8], 13); + al = F3(al, bl, cl, dl, el, x[1], 15); + el = F3(el, al, bl, cl, dl, x[2], 14); + dl = F3(dl, el, al, bl, cl, x[7], 8); + cl = F3(cl, dl, el, al, bl, x[0], 13); + bl = F3(bl, cl, dl, el, al, x[6], 6); + al = F3(al, bl, cl, dl, el, x[13], 5); + el = F3(el, al, bl, cl, dl, x[11], 12); + dl = F3(dl, el, al, bl, cl, x[5], 7); + cl = F3(cl, dl, el, al, bl, x[12], 5); + + // 第三轮右侧 + cr = F3(cr, dr, er, ar, br, x[15], 8); + br = F3(br, cr, dr, er, ar, x[5], 9); + ar = F3(ar, br, cr, dr, er, x[1], 14); + er = F3(er, ar, br, cr, dr, x[3], 9); + dr = F3(dr, er, ar, br, cr, x[7], 13); + cr = F3(cr, dr, er, ar, br, x[14], 15); + br = F3(br, cr, dr, er, ar, x[6], 7); + ar = F3(ar, br, cr, dr, er, x[9], 12); + er = F3(er, ar, br, cr, dr, x[11], 8); + dr = F3(dr, er, ar, br, cr, x[8], 9); + cr = F3(cr, dr, er, ar, br, x[12], 11); + br = F3(br, cr, dr, er, ar, x[2], 7); + ar = F3(ar, br, cr, dr, er, x[10], 7); + er = F3(er, ar, br, cr, dr, x[0], 12); + dr = F3(dr, er, ar, br, cr, x[4], 7); + cr = F3(cr, dr, er, ar, br, x[13], 7); + + // 第四轮左侧 + dl = F4(dl, el, al, bl, cl, x[1], 11); + cl = F4(cl, dl, el, al, bl, x[9], 12); + bl = F4(bl, cl, dl, el, al, x[11], 14); + al = F4(al, bl, cl, dl, el, x[10], 15); + el = F4(el, al, bl, cl, dl, x[0], 14); + dl = F4(dl, el, al, bl, cl, x[8], 15); + cl = F4(cl, dl, el, al, bl, x[12], 9); + bl = F4(bl, cl, dl, el, al, x[4], 8); + al = F4(al, bl, cl, dl, el, x[13], 9); + el = F4(el, al, bl, cl, dl, x[3], 14); + dl = F4(dl, el, al, bl, cl, x[7], 5); + cl = F4(cl, dl, el, al, bl, x[15], 6); + bl = F4(bl, cl, dl, el, al, x[14], 8); + al = F4(al, bl, cl, dl, el, x[5], 6); + el = F4(el, al, bl, cl, dl, x[6], 5); + dl = F4(dl, el, al, bl, cl, x[2], 12); + + // 第四轮右侧 + dr = F2(dr, er, ar, br, cr, x[8], 15); + cr = F2(cr, dr, er, ar, br, x[6], 5); + br = F2(br, cr, dr, er, ar, x[4], 8); + ar = F2(ar, br, cr, dr, er, x[1], 11); + er = F2(er, ar, br, cr, dr, x[3], 14); + dr = F2(dr, er, ar, br, cr, x[11], 14); + cr = F2(cr, dr, er, ar, br, x[15], 6); + br = F2(br, cr, dr, er, ar, x[0], 14); + ar = F2(ar, br, cr, dr, er, x[5], 6); + er = F2(er, ar, br, cr, dr, x[12], 9); + dr = F2(dr, er, ar, br, cr, x[2], 12); + cr = F2(cr, dr, er, ar, br, x[13], 9); + br = F2(br, cr, dr, er, ar, x[9], 12); + ar = F2(ar, br, cr, dr, er, x[7], 5); + er = F2(er, ar, br, cr, dr, x[10], 15); + dr = F2(dr, er, ar, br, cr, x[14], 8); + + // 第五轮左侧 + el = F5(el, al, bl, cl, dl, x[4], 9); + dl = F5(dl, el, al, bl, cl, x[0], 15); + cl = F5(cl, dl, el, al, bl, x[5], 5); + bl = F5(bl, cl, dl, el, al, x[9], 11); + al = F5(al, bl, cl, dl, el, x[7], 6); + el = F5(el, al, bl, cl, dl, x[12], 8); + dl = F5(dl, el, al, bl, cl, x[2], 13); + cl = F5(cl, dl, el, al, bl, x[10], 12); + bl = F5(bl, cl, dl, el, al, x[14], 5); + al = F5(al, bl, cl, dl, el, x[1], 12); + el = F5(el, al, bl, cl, dl, x[3], 13); + dl = F5(dl, el, al, bl, cl, x[8], 14); + cl = F5(cl, dl, el, al, bl, x[11], 11); + bl = F5(bl, cl, dl, el, al, x[6], 8); + al = F5(al, bl, cl, dl, el, x[15], 5); + el = F5(el, al, bl, cl, dl, x[13], 6); + + // 第五轮右侧 + er = F1(er, ar, br, cr, dr, x[12], 8); + dr = F1(dr, er, ar, br, cr, x[15], 5); + cr = F1(cr, dr, er, ar, br, x[10], 12); + br = F1(br, cr, dr, er, ar, x[4], 9); + ar = F1(ar, br, cr, dr, er, x[1], 12); + er = F1(er, ar, br, cr, dr, x[5], 5); + dr = F1(dr, er, ar, br, cr, x[8], 14); + cr = F1(cr, dr, er, ar, br, x[7], 6); + br = F1(br, cr, dr, er, ar, x[6], 8); + ar = F1(ar, br, cr, dr, er, x[2], 13); + er = F1(er, ar, br, cr, dr, x[13], 6); + dr = F1(dr, er, ar, br, cr, x[14], 5); + cr = F1(cr, dr, er, ar, br, x[0], 15); + br = F1(br, cr, dr, er, ar, x[3], 13); + ar = F1(ar, br, cr, dr, er, x[9], 11); + er = F1(er, ar, br, cr, dr, x[11], 11); + + // 最终更新 + uint t = h[1] + cl + dr; + h[1] = h[2] + dl + er; + h[2] = h[3] + el + ar; + h[3] = h[4] + al + br; + h[4] = h[0] + bl + cr; + h[0] = t; + } + + private static uint F1(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + (b ^ c ^ d) + x, s) + e; + } + + private static uint F2(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b & c) | (~b & d)) + x + 0x5a827999, s) + e; + } + + private static uint F3(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b | ~c) ^ d) + x + 0x6ed9eba1, s) + e; + } + + private static uint F4(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b & d) | (c & ~d)) + x + 0x8f1bbcdc, s) + e; + } + + private static uint F5(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + (b ^ (c | ~d)) + x + 0xa953fd4e, s) + e; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + } +} diff --git a/EasyTool.Core/CodeCategory/ROT13Util.cs b/EasyTool.Core/CodeCategory/ROT13Util.cs new file mode 100644 index 0000000..a0bcdb4 --- /dev/null +++ b/EasyTool.Core/CodeCategory/ROT13Util.cs @@ -0,0 +1,143 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ROT13/ROT47 编码工具类 + /// ROT13 是一种简单的字母替换加密(凯撒密码的特例) + /// ROT47 扩展到所有 ASCII 可打印字符 + /// 注意:这不是真正的加密,只是一种混淆方式 + /// + public static class ROT13Util + { + /// + /// 使用 ROT13 编码文本 + /// + /// 文本 + /// 编码后的文本 + public static string Encode(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT13 解码文本(编码和解码相同) + /// + /// 文本 + /// 解码后的文本 + public static string Decode(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT13 编码文本(Encode 的别名) + /// + /// 文本 + /// 编码后的文本 + public static string ROT13(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT47 编码文本 + /// + /// 文本 + /// 编码后的文本 + public static string ROT47(string text) + { + return Rotate47(text); + } + + /// + /// 使用指定偏移量旋转字母 + /// + /// 文本 + /// 偏移量 + /// 旋转后的文本 + public static string Rotate(string text, int shift) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + shift = ((shift % 26) + 26) % 26; // 标准化偏移量 + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + result.Append((char)('A' + (c - 'A' + shift) % 26)); + } + else if (c >= 'a' && c <= 'z') + { + result.Append((char)('a' + (c - 'a' + shift) % 26)); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 使用 ROT47 旋转字符 + /// + /// 文本 + /// 旋转后的文本 + public static string Rotate47(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 33 && c <= 126) + { + result.Append((char)(33 + ((c - 33 + 47) % 94))); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 检测文本是否可能是 ROT13 编码(启发式) + /// + /// 文本 + /// 可能性评分(0-1) + public static double DetectROT13(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + int letterCount = 0; + int nonLetterCount = 0; + + foreach (char c in text) + { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + letterCount++; + else if (c >= 33 && c <= 126) + nonLetterCount++; + } + + if (letterCount == 0) + return 0; + + // ROT13 编码的文本通常有较高的字母比例 + return (double)letterCount / (letterCount + nonLetterCount); + } + } +} diff --git a/EasyTool.Core/CodeCategory/RabbitUtil.cs b/EasyTool.Core/CodeCategory/RabbitUtil.cs new file mode 100644 index 0000000..a09cf52 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RabbitUtil.cs @@ -0,0 +1,269 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Rabbit 流加密工具类 + /// Rabbit 是一种高速流密码,设计用于软件实现 + /// 128位密钥,64位IV(可选) + /// + public static class RabbitUtil + { + private const int KeySize = 16; + private const int IvSize = 8; + + /// + /// 加密数据 + /// + /// 明文 + /// 密钥(16字节) + /// 初始化向量(8字节,可选) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] iv = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + var state = Initialize(key, iv); + byte[] result = new byte[plainText.Length]; + + for (int i = 0; i < plainText.Length; i++) + { + if (i % 16 == 0) + { + NextState(state); + } + + byte keyByte = (byte)(state.S[i % 16] ^ (state.S[(i % 16) + 1] >> 8)); + result[i] = (byte)(plainText[i] ^ keyByte); + } + + return result; + } + + /// + /// 解密数据(加密和解密相同) + /// + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv = null) + { + return Encrypt(cipherText, key, iv); + } + + /// + /// 加密字符串并返回 Base64(包含 IV) + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] iv = new byte[IvSize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(iv); + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key, iv); + + byte[] result = new byte[IvSize + encrypted.Length]; + Array.Copy(iv, result, IvSize); + Array.Copy(encrypted, 0, result, IvSize, encrypted.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串(包含 IV) + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < IvSize) + throw new ArgumentException("Invalid cipher text", nameof(cipherText)); + + byte[] iv = new byte[IvSize]; + Array.Copy(data, iv, IvSize); + + byte[] encrypted = new byte[data.Length - IvSize]; + Array.Copy(data, IvSize, encrypted, 0, encrypted.Length); + + byte[] decrypted = Decrypt(encrypted, key, iv); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey() + { + byte[] key = new byte[KeySize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static RabbitState Initialize(byte[] key, byte[] iv) + { + var state = new RabbitState(); + + // 密钥初始化 + for (int i = 0; i < 8; i++) + { + state.X[i] = (ushort)((key[(i * 2) % 16] << 8) | key[(i * 2 + 1) % 16]); + state.C[i] = state.X[i]; + } + + state.Carry = 0; + + // 执行4次状态更新 + for (int i = 0; i < 4; i++) + { + NextState(state); + } + + // 复制状态到 C + for (int i = 0; i < 8; i++) + { + state.C[(i + 4) % 8] ^= state.X[i]; + } + + // 如果有 IV,进行 IV 设置 + if (iv != null && iv.Length >= IvSize) + { + SetupIv(state, iv); + } + + // 生成初始密钥流 + NextState(state); + for (int i = 0; i < 16; i++) + { + state.S[i] = 0; + } + ExtractKeyStream(state); + + return state; + } + + private static void SetupIv(RabbitState state, byte[] iv) + { + // 将 64 位 IV 映射到计数器 + state.C[0] ^= (ushort)((iv[0] << 8) | iv[1]); + state.C[1] ^= (ushort)((iv[2] << 8) | iv[3]); + state.C[2] ^= (ushort)((iv[4] << 8) | iv[5]); + state.C[3] ^= (ushort)((iv[6] << 8) | iv[7]); + state.C[4] ^= (ushort)((iv[4] << 8) | iv[5]); + state.C[5] ^= (ushort)((iv[6] << 8) | iv[7]); + state.C[6] ^= (ushort)((iv[0] << 8) | iv[1]); + state.C[7] ^= (ushort)((iv[2] << 8) | iv[3]); + + // 执行4次状态更新 + for (int i = 0; i < 4; i++) + { + NextState(state); + } + } + + private static void NextState(RabbitState state) + { + uint[] g = new uint[8]; + uint[] newC = new uint[8]; + ushort[] newX = new ushort[8]; + uint newCarry; + + // 计数器更新 + uint a = 0x4D34D34D; + uint b = 0xD34D34D3; + uint c = 0x34D34D34; + + newC[0] = (uint)((state.C[0] + a + state.Carry) & 0xFFFFFFFF); + newC[1] = (uint)((state.C[1] + b + (newC[0] < state.C[0] ? 1u : 0)) & 0xFFFFFFFF); + newC[2] = (uint)((state.C[2] + c + (newC[1] < state.C[1] ? 1u : 0)) & 0xFFFFFFFF); + newC[3] = (uint)((state.C[3] + a + (newC[2] < state.C[2] ? 1u : 0)) & 0xFFFFFFFF); + newC[4] = (uint)((state.C[4] + b + (newC[3] < state.C[3] ? 1u : 0)) & 0xFFFFFFFF); + newC[5] = (uint)((state.C[5] + c + (newC[4] < state.C[4] ? 1u : 0)) & 0xFFFFFFFF); + newC[6] = (uint)((state.C[6] + a + (newC[5] < state.C[5] ? 1u : 0)) & 0xFFFFFFFF); + newC[7] = (uint)((state.C[7] + b + (newC[6] < state.C[6] ? 1u : 0)) & 0xFFFFFFFF); + + newCarry = newC[7] < state.C[7] ? 1u : 0u; + + // G 函数 + for (int i = 0; i < 8; i++) + { + g[i] = GFunction((ushort)newC[i]); + } + + // 状态更新 + newX[0] = (ushort)((g[0] + RotateLeft16((ushort)g[7], 16) + RotateLeft16((ushort)g[6], 16)) & 0xFFFF); + newX[1] = (ushort)((g[1] + RotateLeft16((ushort)g[0], 8) + g[7]) & 0xFFFF); + newX[2] = (ushort)((g[2] + RotateLeft16((ushort)g[1], 16) + RotateLeft16((ushort)g[0], 16)) & 0xFFFF); + newX[3] = (ushort)((g[3] + RotateLeft16((ushort)g[2], 8) + g[1]) & 0xFFFF); + newX[4] = (ushort)((g[4] + RotateLeft16((ushort)g[3], 16) + RotateLeft16((ushort)g[2], 16)) & 0xFFFF); + newX[5] = (ushort)((g[5] + RotateLeft16((ushort)g[4], 8) + g[3]) & 0xFFFF); + newX[6] = (ushort)((g[6] + RotateLeft16((ushort)g[5], 16) + RotateLeft16((ushort)g[4], 16)) & 0xFFFF); + newX[7] = (ushort)((g[7] + RotateLeft16((ushort)g[6], 8) + g[5]) & 0xFFFF); + + for (int i = 0; i < 8; i++) + { + state.X[i] = (ushort)(newX[i] & 0xFFFF); + state.C[i] = (ushort)(newC[i] & 0xFFFF); + } + state.Carry = newCarry; + + ExtractKeyStream(state); + } + + private static uint GFunction(ushort x) + { + uint result = (uint)(x * x); + return (result ^ (result >> 16)) & 0xFFFF; + } + + private static void ExtractKeyStream(RabbitState state) + { + state.S[0] = (byte)(state.X[0] ^ (state.X[5] >> 8)); + state.S[1] = (byte)(state.X[0] >> 8 ^ state.X[3]); + state.S[2] = (byte)(state.X[2] ^ (state.X[7] >> 8)); + state.S[3] = (byte)(state.X[2] >> 8 ^ state.X[5]); + state.S[4] = (byte)(state.X[4] ^ (state.X[1] >> 8)); + state.S[5] = (byte)(state.X[4] >> 8 ^ state.X[7]); + state.S[6] = (byte)(state.X[6] ^ (state.X[3] >> 8)); + state.S[7] = (byte)(state.X[6] >> 8 ^ state.X[1]); + state.S[8] = (byte)(state.X[0] ^ state.X[5]); + state.S[9] = (byte)((state.X[0] >> 8) ^ (state.X[3] >> 8)); + state.S[10] = (byte)(state.X[2] ^ state.X[7]); + state.S[11] = (byte)((state.X[2] >> 8) ^ (state.X[5] >> 8)); + state.S[12] = (byte)(state.X[4] ^ state.X[1]); + state.S[13] = (byte)((state.X[4] >> 8) ^ (state.X[7] >> 8)); + state.S[14] = (byte)(state.X[6] ^ state.X[3]); + state.S[15] = (byte)((state.X[6] >> 8) ^ (state.X[1] >> 8)); + } + + private static ushort RotateLeft16(ushort x, int n) + { + return (ushort)((x << n) | (x >> (16 - n))); + } + + private class RabbitState + { + public ushort[] X = new ushort[8]; + public ushort[] C = new ushort[8]; + public uint Carry; + public byte[] S = new byte[16]; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Salsa20Util.cs b/EasyTool.Core/CodeCategory/Salsa20Util.cs new file mode 100644 index 0000000..e078460 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Salsa20Util.cs @@ -0,0 +1,189 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Salsa20 流加密工具类 + /// Salsa20 是一种高速流密码,由 Daniel J. Bernstein 设计 + /// ChaCha20 是 Salsa20 的改进版本 + /// + public static class Salsa20Util + { + private static readonly uint[] Sigma = new uint[] { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; + + /// + /// 使用 Salsa20 加密数据 + /// + /// 明文 + /// 密钥(16或32字节) + /// 随机数(8字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] nonce) + { + return Encrypt(plainText, 0, plainText?.Length ?? 0, key, nonce, 0); + } + + /// + /// 使用 Salsa20 加密数据 + /// + public static byte[] Encrypt(byte[] plainText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 32)) + throw new ArgumentException("Key must be 16 or 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 8) + throw new ArgumentException("Nonce must be 8 bytes", nameof(nonce)); + + byte[] cipherText = new byte[length]; + Process(plainText, offset, length, cipherText, 0, key, nonce, initialCounter); + return cipherText; + } + + /// + /// 使用 Salsa20 解密数据(加密和解密相同) + /// + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce) + { + return Decrypt(cipherText, 0, cipherText?.Length ?? 0, key, nonce, 0); + } + + /// + /// 使用 Salsa20 解密数据 + /// + public static byte[] Decrypt(byte[] cipherText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + return Encrypt(cipherText, offset, length, key, nonce, initialCounter); + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); + byte[] nonce = new byte[8]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(nonce); + + byte[] cipherBytes = Encrypt(plainBytes, key, nonce); + + byte[] result = new byte[8 + cipherBytes.Length]; + Array.Copy(nonce, result, 8); + Array.Copy(cipherBytes, 0, result, 8, cipherBytes.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < 8) + throw new ArgumentException("Invalid cipher text"); + + byte[] nonce = new byte[8]; + Array.Copy(data, nonce, 8); + + byte[] cipherBytes = new byte[data.Length - 8]; + Array.Copy(data, 8, cipherBytes, 0, cipherBytes.Length); + + byte[] plainBytes = Decrypt(cipherBytes, key, nonce); + return Encoding.UTF8.GetString(plainBytes); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 32) + throw new ArgumentException("Key length must be 16 or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static void Process(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] key, byte[] nonce, uint counter) + { + uint[] state = new uint[16]; + uint[] block = new uint[16]; + + // 初始化状态 + state[0] = Sigma[0]; + state[1] = (key.Length == 32) ? BitConverter.ToUInt32(key, 0) : Sigma[0]; + state[2] = (key.Length == 32) ? BitConverter.ToUInt32(key, 4) : Sigma[1]; + state[3] = (key.Length == 32) ? BitConverter.ToUInt32(key, 8) : Sigma[2]; + state[4] = (key.Length == 32) ? BitConverter.ToUInt32(key, 12) : Sigma[3]; + state[5] = (key.Length == 32) ? Sigma[1] : BitConverter.ToUInt32(key, 0); + state[6] = (key.Length == 32) ? BitConverter.ToUInt32(key, 16) : BitConverter.ToUInt32(key, 4); + state[7] = (key.Length == 32) ? BitConverter.ToUInt32(key, 20) : BitConverter.ToUInt32(key, 8); + state[8] = (key.Length == 32) ? BitConverter.ToUInt32(key, 24) : BitConverter.ToUInt32(key, 12); + state[9] = (key.Length == 32) ? BitConverter.ToUInt32(key, 28) : Sigma[0]; + state[10] = Sigma[2]; + state[11] = BitConverter.ToUInt32(nonce, 0); + state[12] = BitConverter.ToUInt32(nonce, 4); + state[13] = counter; + state[14] = Sigma[3]; + state[15] = (key.Length == 32) ? Sigma[3] : Sigma[1]; + + int processed = 0; + while (processed < inputLength) + { + Array.Copy(state, block, 16); + + // 20 轮 + for (int i = 0; i < 10; i++) + { + QuarterRound(ref block[0], ref block[4], ref block[8], ref block[12]); + QuarterRound(ref block[5], ref block[9], ref block[13], ref block[1]); + QuarterRound(ref block[10], ref block[14], ref block[2], ref block[6]); + QuarterRound(ref block[15], ref block[3], ref block[7], ref block[11]); + } + + for (int i = 0; i < 16; i++) + block[i] += state[i]; + + int blockSize = Math.Min(64, inputLength - processed); + for (int i = 0; i < blockSize; i++) + { + output[outputOffset + processed + i] = (byte)(input[inputOffset + processed + i] ^ (block[i / 4] >> ((i % 4) * 8))); + } + + processed += blockSize; + state[13]++; + } + } + + private static void QuarterRound(ref uint a, ref uint b, ref uint c, ref uint d) + { + b ^= RotateLeft(a + d, 7); + c ^= RotateLeft(b + a, 9); + d ^= RotateLeft(c + b, 13); + a ^= RotateLeft(d + c, 18); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/ScryptUtil.cs b/EasyTool.Core/CodeCategory/ScryptUtil.cs new file mode 100644 index 0000000..a63eabd --- /dev/null +++ b/EasyTool.Core/CodeCategory/ScryptUtil.cs @@ -0,0 +1,387 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Scrypt 密码哈希工具类 + /// Scrypt 是一种内存密集型的密钥派生函数,专门设计用于抵抗硬件攻击 + /// 常用于加密货币钱包和密码存储 + /// + public static class ScryptUtil + { + // 默认参数 + private const int DefaultN = 32768; // CPU/内存成本参数(必须为2的幂) + private const int DefaultR = 8; // 块大小参数 + private const int DefaultP = 1; // 并行化参数 + private const int DefaultDkLen = 32; // 派生密钥长度 + + /// + /// 使用 Scrypt 哈希密码 + /// + /// 密码 + /// 盐值(可选,默认自动生成) + /// CPU/内存成本参数(必须为2的幂,默认32768) + /// 块大小参数(默认8) + /// 并行化参数(默认1) + /// 派生密钥长度(默认32字节) + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt = null, int n = DefaultN, int r = DefaultR, int p = DefaultP, int dkLen = DefaultDkLen) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + ValidateParameters(n, r, p); + + salt ??= GenerateSalt(); + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + + byte[] hash = DeriveKey(passwordBytes, salt, n, r, p, dkLen); + + // 格式:$scrypt$N=,r=,p=

$$ + return $"$scrypt$N={n},r={r},p={p}${Convert.ToBase64String(salt)}${Convert.ToBase64String(hash)}"; + } + + ///

+ /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + var (n, r, p, salt, expectedHash) = ParseHash(hash); + if (salt == null || expectedHash == null) + return false; + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] computedHash = DeriveKey(passwordBytes, salt, n, r, p, expectedHash.Length); + + return ConstantTimeEquals(computedHash, expectedHash); + } + catch + { + return false; + } + } + + /// + /// 使用 Scrypt 派生密钥 + /// + /// 密码 + /// 盐值 + /// CPU/内存成本参数 + /// 块大小参数 + /// 并行化参数 + /// 派生密钥长度 + /// 派生密钥 + public static byte[] DeriveKey(byte[] password, byte[] salt, int n = DefaultN, int r = DefaultR, int p = DefaultP, int dkLen = DefaultDkLen) + { + if (password == null || password.Length == 0) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length == 0) + throw new ArgumentException("Salt cannot be null or empty", nameof(salt)); + + ValidateParameters(n, r, p); + + // 使用 PBKDF2-HMAC-SHA256 进行初始密钥派生 + byte[] b = PBKDF2(password, salt, 1, p * 128 * r); + + // 对每个块执行 ROMix + for (int i = 0; i < p; i++) + { + int offset = i * 128 * r; + ROMix(b, offset, n, r); + } + + // 再次使用 PBKDF2 派生最终密钥 + return PBKDF2(password, b, 1, dkLen); + } + + /// + /// 生成随机盐值 + /// + /// 盐值长度(默认16字节) + /// 盐值 + public static byte[] GenerateSalt(int length = 16) + { + byte[] salt = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return salt; + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的CPU/内存成本 + /// 新的块大小 + /// 新的并行化参数 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int n = DefaultN, int r = DefaultR, int p = DefaultP) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (oldN, oldR, oldP, _, _) = ParseHash(hash); + return oldN != n || oldR != r || oldP != p; + } + catch + { + return true; + } + } + + #region 私有方法 + + private static void ValidateParameters(int n, int r, int p) + { + if (n <= 1 || (n & (n - 1)) != 0) + throw new ArgumentException("N must be a power of 2 greater than 1", nameof(n)); + + if (r <= 0) + throw new ArgumentException("R must be greater than 0", nameof(r)); + + if (p <= 0) + throw new ArgumentException("P must be greater than 0", nameof(p)); + + // 检查内存使用限制 + long blockSize = 128 * r * p; + long totalMemory = blockSize * n; + + if (totalMemory > int.MaxValue) + throw new ArgumentException("Parameters would use too much memory"); + } + + private static (int n, int r, int p, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (!hash.StartsWith("$scrypt$")) + return (0, 0, 0, null, null); + + string[] parts = hash.Split('$'); + if (parts.Length < 5) + return (0, 0, 0, null, null); + + // 解析参数 + string[] parameters = parts[2].Split(','); + int n = 0, r = 0, p = 0; + + foreach (string param in parameters) + { + string[] kv = param.Split('='); + if (kv.Length != 2) + continue; + + switch (kv[0]) + { + case "N": + n = int.Parse(kv[1]); + break; + case "r": + r = int.Parse(kv[1]); + break; + case "p": + p = int.Parse(kv[1]); + break; + } + } + + byte[] salt = Convert.FromBase64String(parts[3]); + byte[] expectedHash = Convert.FromBase64String(parts[4]); + + return (n, r, p, salt, expectedHash); + } + + private static byte[] PBKDF2(byte[] password, byte[] salt, int iterations, int dkLen) + { + using var hmac = new HMACSHA256(password); + byte[] result = new byte[dkLen]; + int hashLen = hmac.HashSize / 8; + int blocks = (dkLen + hashLen - 1) / hashLen; + + for (int block = 1; block <= blocks; block++) + { + byte[] blockBytes = BitConverter.GetBytes(block); + if (BitConverter.IsLittleEndian) + Array.Reverse(blockBytes); + + byte[] input = new byte[salt.Length + 4]; + Array.Copy(salt, input, salt.Length); + Array.Copy(blockBytes, 0, input, salt.Length, 4); + + byte[] u = hmac.ComputeHash(input); + byte[] output = new byte[u.Length]; + Array.Copy(u, output, u.Length); + + for (int i = 1; i < iterations; i++) + { + u = hmac.ComputeHash(u); + for (int j = 0; j < output.Length; j++) + { + output[j] ^= u[j]; + } + } + + int offset = (block - 1) * hashLen; + int length = Math.Min(hashLen, dkLen - offset); + Array.Copy(output, 0, result, offset, length); + } + + return result; + } + + private static void ROMix(byte[] b, int offset, int n, int r) + { + int blockSize = 128 * r; + uint[] v = new uint[n * blockSize / 4]; + uint[] x = new uint[blockSize / 4]; + + // 将字节转换为 uint 数组 + for (int i = 0; i < blockSize / 4; i++) + { + x[i] = BitConverter.ToUInt32(b, offset + i * 4); + } + + // 第一步:填充 V + for (int i = 0; i < n; i++) + { + Array.Copy(x, 0, v, i * blockSize / 4, blockSize / 4); + BlockMix(x, r); + } + + // 第二步:混合 + for (int i = 0; i < n; i++) + { + int j = (int)(Integerify(x) % (ulong)n); + for (int k = 0; k < blockSize / 4; k++) + { + x[k] ^= v[j * blockSize / 4 + k]; + } + BlockMix(x, r); + } + + // 将结果写回 + for (int i = 0; i < blockSize / 4; i++) + { + byte[] bytes = BitConverter.GetBytes(x[i]); + Array.Copy(bytes, 0, b, offset + i * 4, 4); + } + } + + private static void BlockMix(uint[] b, int r) + { + int blockSize = 128 * r; + uint[] x = new uint[64]; + uint[] y = new uint[blockSize]; + + // 复制最后一个块到 x + Array.Copy(b, b.Length - 64, x, 0, 64); + + // 混合每个块 + for (int i = 0; i < blockSize / 64; i++) + { + for (int j = 0; j < 64; j++) + { + x[j] ^= b[i * 64 + j]; + } + Salsa20_8(x); + + // 根据位置决定输出位置 + if (i % 2 == 0) + { + Array.Copy(x, 0, y, i / 2 * 64, 64); + } + else + { + Array.Copy(x, 0, y, (blockSize / 64 / 2 + i / 2) * 64, 64); + } + } + + Array.Copy(y, b, blockSize); + } + + private static void Salsa20_8(uint[] x) + { + uint[] z = new uint[16]; + Array.Copy(x, z, 16); + + for (int i = 0; i < 8; i += 2) + { + z[4] ^= RotateLeft(z[0] + z[12], 7); + z[8] ^= RotateLeft(z[4] + z[0], 9); + z[12] ^= RotateLeft(z[8] + z[4], 13); + z[0] ^= RotateLeft(z[12] + z[8], 18); + z[9] ^= RotateLeft(z[5] + z[1], 7); + z[13] ^= RotateLeft(z[9] + z[5], 9); + z[1] ^= RotateLeft(z[13] + z[9], 13); + z[5] ^= RotateLeft(z[1] + z[13], 18); + z[14] ^= RotateLeft(z[10] + z[6], 7); + z[2] ^= RotateLeft(z[14] + z[10], 9); + z[6] ^= RotateLeft(z[2] + z[14], 13); + z[10] ^= RotateLeft(z[6] + z[2], 18); + z[3] ^= RotateLeft(z[15] + z[11], 7); + z[7] ^= RotateLeft(z[3] + z[15], 9); + z[11] ^= RotateLeft(z[7] + z[3], 13); + z[15] ^= RotateLeft(z[11] + z[7], 18); + + z[1] ^= RotateLeft(z[0] + z[3], 7); + z[2] ^= RotateLeft(z[1] + z[0], 9); + z[3] ^= RotateLeft(z[2] + z[1], 13); + z[0] ^= RotateLeft(z[3] + z[2], 18); + z[6] ^= RotateLeft(z[5] + z[4], 7); + z[7] ^= RotateLeft(z[6] + z[5], 9); + z[4] ^= RotateLeft(z[7] + z[6], 13); + z[5] ^= RotateLeft(z[4] + z[7], 18); + z[11] ^= RotateLeft(z[10] + z[9], 7); + z[8] ^= RotateLeft(z[11] + z[10], 9); + z[9] ^= RotateLeft(z[8] + z[11], 13); + z[10] ^= RotateLeft(z[9] + z[8], 18); + z[12] ^= RotateLeft(z[15] + z[14], 7); + z[13] ^= RotateLeft(z[12] + z[15], 9); + z[14] ^= RotateLeft(z[13] + z[12], 13); + z[15] ^= RotateLeft(z[14] + z[13], 18); + } + + for (int i = 0; i < 16; i++) + { + x[i] += z[i]; + } + } + + private static ulong Integerify(uint[] b) + { + return ((ulong)b[19] << 32) | b[0]; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/SerpentUtil.cs b/EasyTool.Core/CodeCategory/SerpentUtil.cs new file mode 100644 index 0000000..4cf82cf --- /dev/null +++ b/EasyTool.Core/CodeCategory/SerpentUtil.cs @@ -0,0 +1,318 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Serpent 对称加密工具类 + /// Serpent 是 AES 的最终候选算法之一 + /// 128位分组密码,支持128/192/256位密钥 + /// 使用 32 轮加密,安全性极高 + /// + public static class SerpentUtil + { + private const int BlockSize = 16; + private const int Rounds = 32; + + // S-boxes (8个 4x4 S-box) + private static readonly byte[,] SBox = new byte[,] + { + { 3, 8, 15, 1, 10, 6, 5, 11, 14, 13, 4, 2, 7, 0, 9, 12 }, + { 15, 12, 2, 7, 9, 0, 5, 10, 1, 11, 14, 8, 6, 13, 3, 4 }, + { 8, 6, 7, 9, 3, 12, 10, 15, 13, 1, 14, 4, 0, 11, 5, 2 }, + { 0, 15, 11, 8, 12, 9, 6, 3, 13, 1, 2, 4, 10, 7, 5, 14 }, + { 1, 15, 8, 3, 12, 0, 11, 6, 2, 5, 4, 10, 9, 14, 7, 13 }, + { 15, 5, 2, 11, 4, 10, 9, 12, 0, 3, 14, 8, 13, 6, 7, 1 }, + { 7, 2, 12, 5, 8, 4, 6, 11, 14, 9, 1, 15, 13, 3, 10, 0 }, + { 1, 13, 15, 0, 14, 8, 2, 11, 7, 4, 12, 10, 9, 3, 5, 6 } + }; + + private static readonly byte[,] SBoxInv = new byte[,] + { + { 13, 3, 11, 0, 10, 6, 5, 12, 1, 14, 4, 7, 15, 9, 8, 2 }, + { 5, 8, 2, 14, 15, 6, 12, 3, 11, 4, 7, 9, 1, 13, 10, 0 }, + { 12, 9, 15, 4, 11, 14, 1, 2, 0, 3, 6, 13, 5, 8, 10, 7 }, + { 0, 9, 10, 7, 11, 14, 6, 13, 3, 5, 12, 2, 4, 8, 15, 1 }, + { 5, 0, 8, 3, 10, 9, 7, 14, 2, 12, 11, 6, 4, 15, 13, 1 }, + { 8, 15, 2, 9, 4, 1, 13, 14, 11, 6, 5, 3, 7, 12, 10, 0 }, + { 15, 10, 1, 13, 5, 3, 6, 0, 4, 9, 14, 7, 2, 12, 8, 11 }, + { 3, 0, 6, 13, 9, 14, 15, 8, 5, 12, 11, 7, 10, 1, 4, 2 } + }; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + uint[] subkeys = GenerateSubkeys(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + uint[] subkeys = GenerateSubkeys(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static uint[] GenerateSubkeys(byte[] key) + { + uint[] subkeys = new uint[132]; // 33 * 4 = 132 + + // 扩展密钥到 256 位 + uint[] expandedKey = new uint[8]; + for (int i = 0; i < Math.Min(key.Length / 4, 8); i++) + { + expandedKey[i] = BitConverter.ToUInt32(key, i * 4); + } + + // 填充剩余部分 + if (key.Length < 32) + { + for (int i = key.Length / 4; i < 8; i++) + { + expandedKey[i] = 0; + } + } + + // 生成子密钥 + uint phi = 0x9E3779B9; + uint[] w = new uint[140]; + + for (int i = 0; i < 8; i++) + w[i] = expandedKey[i]; + + for (int i = 8; i < 140; i++) + { + uint x = w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ phi ^ (uint)i; + w[i] = RotateLeft(x, 11); + } + + // 应用 S-box + for (int i = 0; i < 33; i++) + { + int sboxIdx = (35 - i) % 8; + + for (int j = 0; j < 4; j++) + { + uint val = w[i * 4 + j + 8]; + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBox[sboxIdx, b0]; + b1 = SBox[sboxIdx, b1]; + b2 = SBox[sboxIdx, b2]; + b3 = SBox[sboxIdx, b3]; + + subkeys[i * 4 + j] = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + } + + return subkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint[] block = new uint[4]; + for (int i = 0; i < 4; i++) + block[i] = BitConverter.ToUInt32(input, inOffset + i * 4); + + // 32轮加密 + for (int i = 0; i < Rounds; i++) + { + // 密钥加 + for (int j = 0; j < 4; j++) + block[j] ^= subkeys[i * 4 + j]; + + // S-box 替换 + int sboxIdx = i % 8; + for (int j = 0; j < 4; j++) + { + block[j] = ApplySBox(block[j], sboxIdx); + } + + // 线性变换(最后一轮除外) + if (i < Rounds - 1) + { + block[0] = RotateLeft(block[0], 13); + block[2] = RotateLeft(block[2], 3); + block[1] = RotateLeft(block[1] ^ block[0] ^ block[2], 1); + block[3] = RotateLeft(block[3] ^ block[2] ^ (block[0] << 3), 7); + block[0] ^= block[1] ^ block[3]; + block[2] ^= block[3] ^ (block[1] << 7); + } + } + + // 最后一轮密钥加 + for (int i = 0; i < 4; i++) + block[i] ^= subkeys[Rounds * 4 + i]; + + for (int i = 0; i < 4; i++) + BitConverter.GetBytes(block[i]).CopyTo(output, outOffset + i * 4); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint[] block = new uint[4]; + for (int i = 0; i < 4; i++) + block[i] = BitConverter.ToUInt32(input, inOffset + i * 4); + + // 逆向密钥加 + for (int i = 0; i < 4; i++) + block[i] ^= subkeys[Rounds * 4 + i]; + + // 32轮解密 + for (int i = Rounds - 1; i >= 0; i--) + { + // 逆向 S-box + int sboxIdx = i % 8; + for (int j = 0; j < 4; j++) + { + block[j] = ApplySBoxInv(block[j], sboxIdx); + } + + // 逆向密钥加 + for (int j = 0; j < 4; j++) + block[j] ^= subkeys[i * 4 + j]; + + // 逆向线性变换(第一轮除外) + if (i > 0) + { + block[2] ^= block[3] ^ (block[1] << 7); + block[0] ^= block[1] ^ block[3]; + block[3] = RotateRight(block[3] ^ block[2] ^ (block[0] << 3), 7); + block[1] = RotateRight(block[1] ^ block[0] ^ block[2], 1); + block[2] = RotateRight(block[2], 3); + block[0] = RotateRight(block[0], 13); + } + } + + for (int i = 0; i < 4; i++) + BitConverter.GetBytes(block[i]).CopyTo(output, outOffset + i * 4); + } + + private static uint ApplySBox(uint val, int sboxIdx) + { + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBox[sboxIdx, b0]; + b1 = SBox[sboxIdx, b1]; + b2 = SBox[sboxIdx, b2]; + b3 = SBox[sboxIdx, b3]; + + return (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + + private static uint ApplySBoxInv(uint val, int sboxIdx) + { + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBoxInv[sboxIdx, b0]; + b1 = SBoxInv[sboxIdx, b1]; + b2 = SBoxInv[sboxIdx, b2]; + b3 = SBoxInv[sboxIdx, b3]; + + return (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/SipHashUtil.cs b/EasyTool.Core/CodeCategory/SipHashUtil.cs new file mode 100644 index 0000000..d88bbb9 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SipHashUtil.cs @@ -0,0 +1,340 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// SipHash 哈希工具类 + /// SipHash 是一种快速、安全的哈希算法,专为哈希表设计 + /// 由 Jean-Philippe Aumasson 和 Daniel J. Bernstein 开发 + /// 用于防止哈希碰撞攻击(HashDoS) + /// + public static class SipHashUtil + { + /// + /// 使用 SipHash-2-4 计算 64 位哈希值 + /// + /// 输入数据 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, 0, data.Length, key, 2, 4); + } + + /// + /// 使用 SipHash-2-4 计算 64 位哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, offset, length, key, 2, 4); + } + + /// + /// 使用 SipHash-4-8 计算 64 位哈希值(更安全,更慢) + /// + /// 输入数据 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64Secure(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, 0, data.Length, key, 4, 8); + } + + /// + /// 使用 SipHash 计算 128 位哈希值(SipHash-2-4) + /// + /// 输入数据 + /// 密钥(16字节) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute128(data, 0, data.Length, key, 2, 4); + } + + /// + /// 使用 SipHash 计算 128 位哈希值(SipHash-4-8,更安全) + /// + /// 输入数据 + /// 密钥(16字节) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128Secure(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute128(data, 0, data.Length, key, 4, 8); + } + + /// + /// 计算字符串的 SipHash-2-4 哈希值 + /// + /// 文本 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeString64(string text, byte[] key) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash64(Array.Empty(), key); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data, key); + } + + /// + /// 获取 SipHash-2-4 哈希值的十六进制表示 + /// + /// 输入数据 + /// 密钥(16字节) + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data, byte[] key) + { + ulong hash = ComputeHash64(data, key); + return hash.ToString("x16"); + } + + /// + /// 获取 SipHash-128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 密钥(16字节) + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data, byte[] key) + { + var (low, high) = ComputeHash128(data, key); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 生成随机密钥 + /// + /// 16字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制字符串解析密钥 + /// + /// 32字符的十六进制字符串 + /// 16字节密钥 + public static byte[] ParseKeyHex(string hex) + { + if (string.IsNullOrEmpty(hex) || hex.Length != 32) + throw new ArgumentException("Hex key must be 32 characters", nameof(hex)); + + byte[] key = new byte[16]; + for (int i = 0; i < 16; i++) + { + key[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return key; + } + + #region 私有方法 + + private static ulong Compute(byte[] data, int offset, int length, byte[] key, int cRounds, int dRounds) + { + // 初始化 + ulong k0 = BitConverter.ToUInt64(key, 0); + ulong k1 = BitConverter.ToUInt64(key, 8); + + ulong v0 = k0 ^ 0x736f6d6570736575; + ulong v1 = k1 ^ 0x646f72616e646f6d; + ulong v2 = k0 ^ 0x6c7967656e657261; + ulong v3 = k1 ^ 0x7465646279746573; + + int end = offset + length; + int current = offset; + + // 处理完整的 8 字节块 + while (current + 8 <= end) + { + ulong m = BitConverter.ToUInt64(data, current); + v3 ^= m; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= m; + current += 8; + } + + // 处理最后一个块 + ulong lastBlock = (ulong)length << 56; + int remaining = end - current; + int shift = 0; + + for (int i = 0; i < remaining; i++) + { + lastBlock |= (ulong)data[current + i] << shift; + shift += 8; + } + + v3 ^= lastBlock; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= lastBlock; + + // 最终化 + v2 ^= 0xFF; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + return v0 ^ v1 ^ v2 ^ v3; + } + + private static (ulong Low, ulong High) Compute128(byte[] data, int offset, int length, byte[] key, int cRounds, int dRounds) + { + // 初始化 + ulong k0 = BitConverter.ToUInt64(key, 0); + ulong k1 = BitConverter.ToUInt64(key, 8); + + ulong v0 = k0 ^ 0x736f6d6570736575; + ulong v1 = k1 ^ 0x646f72616e646f6d; + ulong v2 = k0 ^ 0x6c7967656e657261; + ulong v3 = k1 ^ 0x7465646279746573; + + int end = offset + length; + int current = offset; + + // 处理完整的 8 字节块 + while (current + 8 <= end) + { + ulong m = BitConverter.ToUInt64(data, current); + v3 ^= m; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= m; + current += 8; + } + + // 处理最后一个块 + ulong lastBlock = (ulong)length << 56; + int remaining = end - current; + int shift = 0; + + for (int i = 0; i < remaining; i++) + { + lastBlock |= (ulong)data[current + i] << shift; + shift += 8; + } + + v3 ^= lastBlock; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= lastBlock; + + // 最终化(128位输出) + v2 ^= 0xEE; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + ulong low = v0 ^ v1 ^ v2 ^ v3; + + // 第二轮 + v1 ^= 0xDD; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + ulong high = v0 ^ v1 ^ v2 ^ v3; + + return (low, high); + } + + private static void SipRound(ref ulong v0, ref ulong v1, ref ulong v2, ref ulong v3) + { + v0 += v1; + v1 = RotateLeft(v1, 13); + v1 ^= v0; + v0 = RotateLeft(v0, 32); + + v2 += v3; + v3 = RotateLeft(v3, 16); + v3 ^= v2; + + v0 += v3; + v3 = RotateLeft(v3, 21); + v3 ^= v0; + + v2 += v1; + v1 = RotateLeft(v1, 17); + v1 ^= v2; + v2 = RotateLeft(v2, 32); + } + + private static ulong RotateLeft(ulong x, int n) + { + return (x << n) | (x >> (64 - n)); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Sm2Util.cs b/EasyTool.Core/CodeCategory/Sm2Util.cs new file mode 100644 index 0000000..3f63971 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm2Util.cs @@ -0,0 +1,657 @@ +using System; +using System.Numerics; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM2 椭圆曲线公钥密码工具类 + /// SM2 是中国国家密码管理局发布的椭圆曲线公钥密码算法 + /// 用于数字签名、密钥交换和公钥加密 + /// 基于 256 位椭圆曲线 + /// + public static class Sm2Util + { + // SM2 推荐椭圆曲线参数 + private static readonly BigInteger P = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger A = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger B = BigInteger.Parse("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger N = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger Gx = BigInteger.Parse("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger Gy = BigInteger.Parse("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", System.Globalization.NumberStyles.HexNumber); + + // 基点 G + private static readonly ECPoint G = new ECPoint { X = Gx, Y = Gy }; + + // 用户 ID(默认值) + private const string DefaultUserId = "1234567812345678"; + + #region 密钥生成 + + /// + /// 生成 SM2 密钥对 + /// + /// 密钥对(私钥和公钥) + public static (byte[] PrivateKey, byte[] PublicKey) GenerateKeyPair() + { + byte[] privateKey = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + + // 生成私钥(1 到 n-1 之间的随机数) + do + { + rng.GetBytes(privateKey); + var d = new BigInteger(privateKey, true, true); + if (d > 0 && d < N) + break; + } while (true); + + // 计算公钥 P = d * G + var publicKey = ScalarMultiply(privateKey, G); + + return (privateKey, EncodePoint(publicKey)); + } + + /// + /// 从私钥导出公钥 + /// + /// 私钥(32字节) + /// 公钥(65字节,未压缩格式) + public static byte[] DerivePublicKey(byte[] privateKey) + { + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + var publicKey = ScalarMultiply(privateKey, G); + return EncodePoint(publicKey); + } + + #endregion + + #region 加密解密 + + /// + /// 使用 SM2 公钥加密数据 + /// + /// 明文 + /// 公钥(65字节) + /// 密文(C1 || C3 || C2 格式) + public static byte[] Encrypt(byte[] plainText, byte[] publicKey) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (publicKey == null || publicKey.Length != 65) + throw new ArgumentException("Public key must be 65 bytes", nameof(publicKey)); + + var pubPoint = DecodePoint(publicKey); + + byte[] kBytes = new byte[32]; + ECPoint c1; + BigInteger k; + + using var rng = RandomNumberGenerator.Create(); + + // 生成随机数 k + do + { + rng.GetBytes(kBytes); + k = new BigInteger(kBytes, true, true); + if (k > 0 && k < N) + break; + } while (true); + + // C1 = k * G + c1 = ScalarMultiply(kBytes, G); + + // S = k * PB(检查 S 是否为无穷远点) + var s = ScalarMultiply(kBytes, pubPoint); + if (IsInfinity(s)) + throw new CryptographicException("Invalid public key"); + + // KDF 密钥派生 + byte[] kdfInput = new byte[64]; + Array.Copy(s.X.ToByteArray(true, true), 0, kdfInput, 0, 32); + Array.Copy(s.Y.ToByteArray(true, true), 0, kdfInput, 32, 32); + + byte[] kdfOutput = Kdf(kdfInput, plainText.Length); + + // C2 = M XOR KDF_output + byte[] c2 = new byte[plainText.Length]; + for (int i = 0; i < plainText.Length; i++) + { + c2[i] = (byte)(plainText[i] ^ kdfOutput[i]); + } + + // C3 = SM3(C1x || C1y || M) + byte[] c3Input = new byte[64 + plainText.Length]; + Array.Copy(c1.X.ToByteArray(true, true), 0, c3Input, 0, 32); + Array.Copy(c1.Y.ToByteArray(true, true), 0, c3Input, 32, 32); + Array.Copy(plainText, 0, c3Input, 64, plainText.Length); + + byte[] c3 = Sm3Hash(c3Input); + + // 组合结果:C1 || C3 || C2 + byte[] result = new byte[65 + 32 + plainText.Length]; + Array.Copy(EncodePoint(c1), 0, result, 0, 65); + Array.Copy(c3, 0, result, 65, 32); + Array.Copy(c2, 0, result, 97, c2.Length); + + return result; + } + + /// + /// 使用 SM2 私钥解密数据 + /// + /// 密文 + /// 私钥(32字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] privateKey) + { + if (cipherText == null || cipherText.Length < 97) + throw new ArgumentException("Invalid cipher text", nameof(cipherText)); + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + // 解析密文 + byte[] c1Bytes = new byte[65]; + byte[] c3 = new byte[32]; + int c2Length = cipherText.Length - 97; + byte[] c2 = new byte[c2Length]; + + Array.Copy(cipherText, 0, c1Bytes, 0, 65); + Array.Copy(cipherText, 65, c3, 0, 32); + Array.Copy(cipherText, 97, c2, 0, c2Length); + + var c1 = DecodePoint(c1Bytes); + + // S = dB * C1(检查 S 是否为无穷远点) + var s = ScalarMultiply(privateKey, c1); + if (IsInfinity(s)) + throw new CryptographicException("Invalid cipher text"); + + // KDF 密钥派生 + byte[] kdfInput = new byte[64]; + Array.Copy(s.X.ToByteArray(true, true), 0, kdfInput, 0, 32); + Array.Copy(s.Y.ToByteArray(true, true), 0, kdfInput, 32, 32); + + byte[] kdfOutput = Kdf(kdfInput, c2Length); + + // M = C2 XOR KDF_output + byte[] plainText = new byte[c2Length]; + for (int i = 0; i < c2Length; i++) + { + plainText[i] = (byte)(c2[i] ^ kdfOutput[i]); + } + + // 验证 C3 = SM3(C1x || C1y || M) + byte[] c3Input = new byte[64 + plainText.Length]; + Array.Copy(c1.X.ToByteArray(true, true), 0, c3Input, 0, 32); + Array.Copy(c1.Y.ToByteArray(true, true), 0, c3Input, 32, 32); + Array.Copy(plainText, 0, c3Input, 64, plainText.Length); + + byte[] expectedC3 = Sm3Hash(c3Input); + + if (!ConstantTimeEquals(c3, expectedC3)) + throw new CryptographicException("Invalid cipher text: checksum mismatch"); + + return plainText; + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 公钥 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] publicKey) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, publicKey); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 私钥 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] privateKey) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, privateKey); + return Encoding.UTF8.GetString(decrypted); + } + + #endregion + + #region 签名验签 + + /// + /// 使用 SM2 私钥签名数据 + /// + /// 要签名的数据 + /// 私钥(32字节) + /// 用户 ID(可选) + /// 签名(64字节,R || S) + public static byte[] Sign(byte[] data, byte[] privateKey, string userId = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + userId ??= DefaultUserId; + + // 计算 Z 值 + var publicKey = ScalarMultiply(privateKey, G); + byte[] z = CalculateZ(publicKey, userId); + + // e = SM3(Z || M) + byte[] eInput = new byte[z.Length + data.Length]; + Array.Copy(z, eInput, z.Length); + Array.Copy(data, 0, eInput, z.Length, data.Length); + byte[] eHash = Sm3Hash(eInput); + BigInteger e = new BigInteger(eHash, true, true); + + byte[] kBytes = new byte[32]; + BigInteger r, s; + var d = new BigInteger(privateKey, true, true); + + using var rng = RandomNumberGenerator.Create(); + + do + { + // 生成随机数 k + do + { + rng.GetBytes(kBytes); + var k = new BigInteger(kBytes, true, true); + if (k > 0 && k < N) + break; + } while (true); + + // 计算 x1, y1 = k * G + var point = ScalarMultiply(kBytes, G); + + // r = (e + x1) mod n + r = (e + point.X) % N; + if (r == 0 || r + new BigInteger(kBytes, true, true) == N) + continue; + + // s = ((1 + d)^-1 * (k - r * d)) mod n + var dPlusOne = (d + 1) % N; + var dPlusOneInv = ModInverse(dPlusOne, N); + s = (dPlusOneInv * ((new BigInteger(kBytes, true, true) - r * d) % N + N)) % N; + + if (s != 0) + break; + + } while (true); + + // 组合签名 R || S + byte[] result = new byte[64]; + Array.Copy(r.ToByteArray(true, true), 0, result, 0, Math.Min(32, r.ToByteArray(true, true).Length)); + Array.Copy(s.ToByteArray(true, true), 0, result, 32, Math.Min(32, s.ToByteArray(true, true).Length)); + + return result; + } + + /// + /// 使用 SM2 公钥验证签名 + /// + /// 原始数据 + /// 签名(64字节) + /// 公钥(65字节) + /// 用户 ID(可选) + /// 签名是否有效 + public static bool Verify(byte[] data, byte[] signature, byte[] publicKey, string userId = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (signature == null || signature.Length != 64) + throw new ArgumentException("Signature must be 64 bytes", nameof(signature)); + if (publicKey == null || publicKey.Length != 65) + throw new ArgumentException("Public key must be 65 bytes", nameof(publicKey)); + + userId ??= DefaultUserId; + + // 解析签名 + byte[] rBytes = new byte[32]; + byte[] sBytes = new byte[32]; + Array.Copy(signature, 0, rBytes, 0, 32); + Array.Copy(signature, 32, sBytes, 0, 32); + + BigInteger r = new BigInteger(rBytes, true, true); + BigInteger s = new BigInteger(sBytes, true, true); + + // 验证 r, s 范围 + if (r < 1 || r >= N || s < 1 || s >= N) + return false; + + // 计算 Z 值 + var pubPoint = DecodePoint(publicKey); + byte[] z = CalculateZ(pubPoint, userId); + + // e = SM3(Z || M) + byte[] eInput = new byte[z.Length + data.Length]; + Array.Copy(z, eInput, z.Length); + Array.Copy(data, 0, eInput, z.Length, data.Length); + byte[] eHash = Sm3Hash(eInput); + BigInteger e = new BigInteger(eHash, true, true); + + // t = (r + s) mod n + BigInteger t = (r + s) % N; + if (t == 0) + return false; + + // 计算 (x1, y1) = s * G + t * PA + var sG = ScalarMultiply(sBytes, G); + + // 需要将 t 转换为字节数组 + byte[] tBytes = t.ToByteArray(true, true); + if (tBytes.Length > 32) + return false; + byte[] tBytesPadded = new byte[32]; + Array.Copy(tBytes, tBytesPadded, tBytes.Length); + + var tPA = ScalarMultiply(tBytesPadded, pubPoint); + var point = PointAdd(sG, tPA); + + // 验证 R = (e + x1) mod n == r + BigInteger R = (e + point.X) % N; + + return R == r; + } + + /// + /// 对字符串签名并返回 Base64 + /// + /// 要签名的文本 + /// 私钥 + /// 用户 ID + /// Base64 签名 + public static string SignToBase64(string text, byte[] privateKey, string userId = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] signature = Sign(data, privateKey, userId); + return Convert.ToBase64String(signature); + } + + /// + /// 验证 Base64 签名 + /// + /// 原始文本 + /// Base64 签名 + /// 公钥 + /// 用户 ID + /// 签名是否有效 + public static bool VerifyFromBase64(string text, string signatureBase64, byte[] publicKey, string userId = null) + { + if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(signatureBase64)) + return false; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] signature = Convert.FromBase64String(signatureBase64); + return Verify(data, signature, publicKey, userId); + } + + #endregion + + #region 私有方法 + + private static ECPoint ScalarMultiply(byte[] k, ECPoint point) + { + var scalar = new BigInteger(k, true, true); + var result = PointMultiply(scalar, point); + return result; + } + + private static ECPoint PointMultiply(BigInteger k, ECPoint point) + { + if (k == 0 || IsInfinity(point)) + return InfinityPoint(); + + ECPoint result = InfinityPoint(); + ECPoint temp = point; + var absK = BigInteger.Abs(k); + + while (absK > 0) + { + if ((absK & 1) == 1) + { + result = PointAdd(result, temp); + } + temp = PointDouble(temp); + absK >>= 1; + } + + return result; + } + + private static ECPoint PointAdd(ECPoint p1, ECPoint p2) + { + if (IsInfinity(p1)) return p2; + if (IsInfinity(p2)) return p1; + + if (p1.X == p2.X) + { + if ((p1.Y + p2.Y) % P == 0) + return InfinityPoint(); + + return PointDouble(p1); + } + + // λ = (y2 - y1) / (x2 - x1) + var dx = (p2.X - p1.X + P) % P; + var dy = (p2.Y - p1.Y + P) % P; + var lambda = (dy * ModInverse(dx, P)) % P; + + // x3 = λ² - x1 - x2 + var x3 = (lambda * lambda - p1.X - p2.X + 2 * P) % P; + + // y3 = λ(x1 - x3) - y1 + var y3 = (lambda * (p1.X - x3 + P) - p1.Y + P) % P; + + return new ECPoint { X = x3, Y = y3 }; + } + + private static ECPoint PointDouble(ECPoint p) + { + if (IsInfinity(p)) + return InfinityPoint(); + + // λ = (3x² + a) / (2y) + var x2 = (p.X * p.X) % P; + var numerator = (3 * x2 + A) % P; + var denominator = (2 * p.Y) % P; + var lambda = (numerator * ModInverse(denominator, P)) % P; + + // x3 = λ² - 2x + var x3 = (lambda * lambda - 2 * p.X + P) % P; + + // y3 = λ(x - x3) - y + var y3 = (lambda * (p.X - x3 + P) - p.Y + P) % P; + + return new ECPoint { X = x3, Y = y3 }; + } + + private static ECPoint InfinityPoint() + { + return new ECPoint { X = BigInteger.Zero, Y = BigInteger.Zero }; + } + + private static bool IsInfinity(ECPoint p) + { + return p.X == BigInteger.Zero && p.Y == BigInteger.Zero; + } + + private static BigInteger ModInverse(BigInteger a, BigInteger n) + { + if (a < 0) a = (a % n + n) % n; + + BigInteger t = 0, newT = 1; + BigInteger r = n, newR = a; + + while (newR != 0) + { + var quotient = r / newR; + var tempT = t; + t = newT; + newT = tempT - quotient * newT; + + var tempR = r; + r = newR; + newR = tempR - quotient * newR; + } + + if (t < 0) t = (t % n + n) % n; + + return t; + } + + private static byte[] EncodePoint(ECPoint point) + { + byte[] result = new byte[65]; + result[0] = 0x04; // 未压缩格式 + + var xBytes = point.X.ToByteArray(true, true); + var yBytes = point.Y.ToByteArray(true, true); + + Array.Copy(xBytes, 0, result, 1 + (32 - xBytes.Length), xBytes.Length); + Array.Copy(yBytes, 0, result, 33 + (32 - yBytes.Length), yBytes.Length); + + return result; + } + + private static ECPoint DecodePoint(byte[] data) + { + if (data == null || data.Length != 65 || data[0] != 0x04) + throw new ArgumentException("Invalid point encoding", nameof(data)); + + byte[] xBytes = new byte[32]; + byte[] yBytes = new byte[32]; + Array.Copy(data, 1, xBytes, 0, 32); + Array.Copy(data, 33, yBytes, 0, 32); + + return new ECPoint + { + X = new BigInteger(xBytes, true, true), + Y = new BigInteger(yBytes, true, true) + }; + } + + private static byte[] CalculateZ(ECPoint publicKey, string userId) + { + byte[] idBytes = Encoding.UTF8.GetBytes(userId); + int idBits = idBytes.Length * 8; + + byte[] entl = new byte[2]; + entl[0] = (byte)((idBits >> 8) & 0xFF); + entl[1] = (byte)(idBits & 0xFF); + + // Z = SM3(ENTLA || IDA || a || b || Gx || Gy || Ax || Ay) + byte[] aBytes = A.ToByteArray(true, true); + byte[] bBytes = B.ToByteArray(true, true); + byte[] gxBytes = Gx.ToByteArray(true, true); + byte[] gyBytes = Gy.ToByteArray(true, true); + byte[] axBytes = publicKey.X.ToByteArray(true, true); + byte[] ayBytes = publicKey.Y.ToByteArray(true, true); + + byte[] input = new byte[2 + idBytes.Length + 32 * 6]; + int offset = 0; + + Array.Copy(entl, 0, input, offset, 2); + offset += 2; + Array.Copy(idBytes, 0, input, offset, idBytes.Length); + offset += idBytes.Length; + + CopyPadded(aBytes, input, ref offset, 32); + CopyPadded(bBytes, input, ref offset, 32); + CopyPadded(gxBytes, input, ref offset, 32); + CopyPadded(gyBytes, input, ref offset, 32); + CopyPadded(axBytes, input, ref offset, 32); + CopyPadded(ayBytes, input, ref offset, 32); + + return Sm3Hash(input); + } + + private static void CopyPadded(byte[] src, byte[] dest, ref int offset, int length) + { + int padLength = length - src.Length; + if (padLength > 0) + { + offset += padLength; + } + Array.Copy(src, 0, dest, offset, Math.Min(src.Length, length)); + offset += Math.Min(src.Length, length); + } + + private static byte[] Kdf(byte[] z, int keyLength) + { + byte[] result = new byte[keyLength]; + int counter = 1; + int generated = 0; + + while (generated < keyLength) + { + byte[] counterBytes = BitConverter.GetBytes(counter); + if (BitConverter.IsLittleEndian) + Array.Reverse(counterBytes); + + byte[] input = new byte[z.Length + 4]; + Array.Copy(z, input, z.Length); + Array.Copy(counterBytes, 0, input, z.Length, 4); + + byte[] hash = Sm3Hash(input); + + int copyLength = Math.Min(32, keyLength - generated); + Array.Copy(hash, 0, result, generated, copyLength); + generated += copyLength; + counter++; + } + + return result; + } + + private static byte[] Sm3Hash(byte[] data) + { + return Sm3Util.ComputeHash(data); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + + /// + /// 椭圆曲线点 + /// + private struct ECPoint + { + public BigInteger X; + public BigInteger Y; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Sm3Util.cs b/EasyTool.Core/CodeCategory/Sm3Util.cs new file mode 100644 index 0000000..e68720e --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm3Util.cs @@ -0,0 +1,358 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM3 密码哈希算法工具类 + /// SM3 是中国国家密码管理局发布的密码哈希函数标准 + /// 输出256位(32字节)哈希值,安全性类似于 SHA-256 + /// + public static class Sm3Util + { + // SM3 初始向量 + private static readonly uint[] IV = new uint[] + { + 0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, + 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e + }; + + /// + /// 计算数据的 SM3 哈希值 + /// + /// 输入数据 + /// 32字节的哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算数据的 SM3 哈希值 + /// + /// 输入数据 + /// 起始位置 + /// 长度 + /// 32字节的哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset >= data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + // 填充消息 + byte[] padded = PadMessage(data, offset, length); + + // 初始化哈希值 + uint[] v = new uint[8]; + Array.Copy(IV, v, 8); + + // 处理每个512位块 + for (int i = 0; i < padded.Length; i += 64) + { + ProcessBlock(padded, i, v); + } + + // 转换为字节数组 + byte[] result = new byte[32]; + for (int i = 0; i < 8; i++) + { + result[i * 4] = (byte)(v[i] >> 24); + result[i * 4 + 1] = (byte)(v[i] >> 16); + result[i * 4 + 2] = (byte)(v[i] >> 8); + result[i * 4 + 3] = (byte)v[i]; + } + + return result; + } + + /// + /// 计算字符串的 SM3 哈希值 + /// + /// 输入字符串 + /// 编码方式(默认UTF-8) + /// 32字节的哈希值 + public static byte[] ComputeHash(string text, Encoding encoding = null) + { + if (text == null) + throw new ArgumentNullException(nameof(text)); + + encoding ??= Encoding.UTF8; + return ComputeHash(encoding.GetBytes(text)); + } + + /// + /// 计算数据的 SM3 哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 64字符的十六进制字符串 + public static string ComputeHashHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BytesToHex(hash); + } + + /// + /// 计算字符串的 SM3 哈希值并返回十六进制字符串 + /// + /// 输入字符串 + /// 编码方式(默认UTF-8) + /// 64字符的十六进制字符串 + public static string ComputeHashHex(string text, Encoding encoding = null) + { + byte[] hash = ComputeHash(text, encoding); + return BytesToHex(hash); + } + + /// + /// 验证数据哈希值 + /// + /// 原始数据 + /// 预期的哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] expectedHash) + { + if (expectedHash == null || expectedHash.Length != 32) + return false; + + byte[] computed = ComputeHash(data); + return ConstantTimeEquals(computed, expectedHash); + } + + /// + /// 验证数据哈希值(十六进制格式) + /// + /// 原始数据 + /// 预期的哈希值(十六进制) + /// 是否匹配 + public static bool VerifyHex(byte[] data, string expectedHashHex) + { + if (string.IsNullOrEmpty(expectedHashHex) || expectedHashHex.Length != 64) + return false; + + string computed = ComputeHashHex(data); + return string.Equals(computed, expectedHashHex, StringComparison.OrdinalIgnoreCase); + } + + #region 私有方法 + + private static byte[] PadMessage(byte[] data, int offset, int length) + { + // 计算填充后的长度 + long bitLength = (long)length * 8; + int paddedLength = length + 1 + 8; + + // 使长度为64的倍数 + while (paddedLength % 64 != 0) + { + paddedLength++; + } + + byte[] padded = new byte[paddedLength]; + Array.Copy(data, offset, padded, 0, length); + + // 添加1位和7个0位(0x80) + padded[length] = 0x80; + + // 添加长度(大端序,64位) + padded[paddedLength - 8] = (byte)(bitLength >> 56); + padded[paddedLength - 7] = (byte)(bitLength >> 48); + padded[paddedLength - 6] = (byte)(bitLength >> 40); + padded[paddedLength - 5] = (byte)(bitLength >> 32); + padded[paddedLength - 4] = (byte)(bitLength >> 24); + padded[paddedLength - 3] = (byte)(bitLength >> 16); + padded[paddedLength - 2] = (byte)(bitLength >> 8); + padded[paddedLength - 1] = (byte)bitLength; + + return padded; + } + + private static void ProcessBlock(byte[] block, int offset, uint[] v) + { + uint[] w = new uint[68]; + uint[] w1 = new uint[64]; + + // 准备消息扩展 + for (int i = 0; i < 16; i++) + { + w[i] = ((uint)block[offset + i * 4] << 24) | + ((uint)block[offset + i * 4 + 1] << 16) | + ((uint)block[offset + i * 4 + 2] << 8) | + block[offset + i * 4 + 3]; + } + + for (int i = 16; i < 68; i++) + { + w[i] = P1(w[i - 16] ^ w[i - 9] ^ RotateLeft(w[i - 3], 15)) ^ + RotateLeft(w[i - 13], 7) ^ w[i - 6]; + if (w[i] < 0) w[i] = (uint)(int)w[i]; + } + + for (int i = 0; i < 64; i++) + { + w1[i] = w[i] ^ w[i + 4]; + } + + // 压缩函数 + uint a = v[0], b = v[1], c = v[2], d = v[3]; + uint e = v[4], f = v[5], g = v[6], h = v[7]; + + for (int i = 0; i < 64; i++) + { + uint ss1 = RotateLeft(RotateLeft(a, 12) + e + RotateLeft(T(i), i % 32), 7); + uint ss2 = ss1 ^ RotateLeft(a, 12); + uint tt1 = FF(a, b, c, i) + d + ss2 + w1[i]; + uint tt2 = GG(e, f, g, i) + h + ss1 + w[i]; + + d = c; + c = RotateLeft(b, 9); + b = a; + a = tt1; + h = g; + g = RotateLeft(f, 19); + f = e; + e = P0(tt2); + } + + v[0] ^= a; + v[1] ^= b; + v[2] ^= c; + v[3] ^= d; + v[4] ^= e; + v[5] ^= f; + v[6] ^= g; + v[7] ^= h; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static uint T(int j) + { + return j < 16 ? 0x79cc4519u : 0x7a879d8au; + } + + private static uint FF(uint x, uint y, uint z, int j) + { + if (j < 16) + { + return x ^ y ^ z; + } + return (x & y) | (x & z) | (y & z); + } + + private static uint GG(uint x, uint y, uint z, int j) + { + if (j < 16) + { + return x ^ y ^ z; + } + return (x & y) | (~x & z); + } + + private static uint P0(uint x) + { + return x ^ RotateLeft(x, 9) ^ RotateLeft(x, 17); + } + + private static uint P1(uint x) + { + return x ^ RotateLeft(x, 15) ^ RotateLeft(x, 23); + } + + private static string BytesToHex(byte[] bytes) + { + var sb = new StringBuilder(bytes.Length * 2); + foreach (byte b in bytes) + { + sb.Append(b.ToString("x2")); + } + return sb.ToString(); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + + #endregion + + #region HMAC-SM3 + + /// + /// 计算 HMAC-SM3 + /// + /// 密钥 + /// 数据 + /// 32字节的HMAC值 + public static byte[] Hmac(byte[] key, byte[] data) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (data == null) + throw new ArgumentNullException(nameof(data)); + + // 如果密钥太长,先哈希 + if (key.Length > 64) + { + key = ComputeHash(key); + } + + // 填充密钥到64字节 + byte[] paddedKey = new byte[64]; + Array.Copy(key, paddedKey, key.Length); + + // 计算内部和外部填充 + byte[] innerPad = new byte[64]; + byte[] outerPad = new byte[64]; + + for (int i = 0; i < 64; i++) + { + innerPad[i] = (byte)(paddedKey[i] ^ 0x36); + outerPad[i] = (byte)(paddedKey[i] ^ 0x5c); + } + + // 计算 HMAC + byte[] innerData = new byte[64 + data.Length]; + Array.Copy(innerPad, innerData, 64); + Array.Copy(data, 0, innerData, 64, data.Length); + byte[] innerHash = ComputeHash(innerData); + + byte[] outerData = new byte[64 + 32]; + Array.Copy(outerPad, outerData, 64); + Array.Copy(innerHash, 0, outerData, 64, 32); + + return ComputeHash(outerData); + } + + /// + /// 计算 HMAC-SM3 并返回十六进制字符串 + /// + /// 密钥 + /// 数据 + /// 64字符的十六进制字符串 + public static string HmacHex(byte[] key, byte[] data) + { + byte[] hmac = Hmac(key, data); + return BytesToHex(hmac); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Sm4Util.cs b/EasyTool.Core/CodeCategory/Sm4Util.cs new file mode 100644 index 0000000..459bd66 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm4Util.cs @@ -0,0 +1,449 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM4 对称加密算法工具类 + /// SM4 是中国国家密码管理局发布的分组密码标准 + /// 分组长度128位,密钥长度128位 + /// + public static class Sm4Util + { + // SM4 S盒 + private static readonly byte[] SBOX = new byte[] + { + 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, + 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, + 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, + 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, + 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, + 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, + 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, + 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, + 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, + 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, + 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, + 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, + 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, + 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, + 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48 + }; + + // 系统参数 FK + private static readonly uint[] FK = new uint[] { 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc }; + + // 固定参数 CK + private static readonly uint[] CK = new uint[] + { + 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, + 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, + 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, + 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, + 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, + 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, + 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, + 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279 + }; + + private const int BLOCK_SIZE = 16; // 128位 + + /// + /// SM4 加密(ECB模式) + /// + /// 明文 + /// 密钥(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + return Encrypt(plainText, key, Sm4Mode.ECB, null); + } + + /// + /// SM4 加密 + /// + /// 明文 + /// 密钥(16字节) + /// 加密模式 + /// 初始向量(CBC模式需要,16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, Sm4Mode mode, byte[] iv) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (mode == Sm4Mode.CBC && (iv == null || iv.Length != 16)) + throw new ArgumentException("IV must be 16 bytes for CBC mode", nameof(iv)); + + // 生成轮密钥 + uint[] roundKeys = GenerateRoundKeys(key); + + // PKCS7 填充 + byte[] padded = Pkcs7Pad(plainText); + + byte[] result = new byte[padded.Length]; + byte[] temp = new byte[BLOCK_SIZE]; + + if (mode == Sm4Mode.CBC) + { + Array.Copy(iv, temp, BLOCK_SIZE); + } + + for (int i = 0; i < padded.Length; i += BLOCK_SIZE) + { + if (mode == Sm4Mode.CBC) + { + // CBC模式:明文先与IV异或 + for (int j = 0; j < BLOCK_SIZE; j++) + { + temp[j] = (byte)(padded[i + j] ^ temp[j]); + } + EncryptBlock(temp, roundKeys); + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + else + { + Array.Copy(padded, i, temp, 0, BLOCK_SIZE); + EncryptBlock(temp, roundKeys); + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + } + + return result; + } + + /// + /// SM4 加密字符串并返回 Base64 + /// + /// 明文字符串 + /// 密钥(16字节) + /// 编码方式(默认UTF-8) + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key, Encoding encoding = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + + encoding ??= Encoding.UTF8; + byte[] encrypted = Encrypt(encoding.GetBytes(plainText), key); + return Convert.ToBase64String(encrypted); + } + + /// + /// SM4 加密字符串并返回 Base64 + /// + /// 明文字符串 + /// 密钥字符串 + /// 编码方式(默认UTF-8) + /// Base64 密文 + public static string EncryptToBase64(string plainText, string keyString, Encoding encoding = null) + { + encoding ??= Encoding.UTF8; + byte[] key = GetKeyFromString(keyString, encoding); + return EncryptToBase64(plainText, key, encoding); + } + + /// + /// SM4 解密(ECB模式) + /// + /// 密文 + /// 密钥(16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + return Decrypt(cipherText, key, Sm4Mode.ECB, null); + } + + /// + /// SM4 解密 + /// + /// 密文 + /// 密钥(16字节) + /// 加密模式 + /// 初始向量(CBC模式需要,16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, Sm4Mode mode, byte[] iv) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (cipherText.Length == 0 || cipherText.Length % BLOCK_SIZE != 0) + throw new ArgumentException("Cipher text length must be a multiple of 16", nameof(cipherText)); + if (mode == Sm4Mode.CBC && (iv == null || iv.Length != 16)) + throw new ArgumentException("IV must be 16 bytes for CBC mode", nameof(iv)); + + // 生成轮密钥(解密时使用逆序) + uint[] roundKeys = GenerateRoundKeys(key); + Array.Reverse(roundKeys); + + byte[] result = new byte[cipherText.Length]; + byte[] temp = new byte[BLOCK_SIZE]; + byte[] prevBlock = mode == Sm4Mode.CBC ? iv : null; + + for (int i = 0; i < cipherText.Length; i += BLOCK_SIZE) + { + Array.Copy(cipherText, i, temp, 0, BLOCK_SIZE); + EncryptBlock(temp, roundKeys); // 使用逆序的轮密钥 + + if (mode == Sm4Mode.CBC && prevBlock != null) + { + // CBC模式:解密后与前一个密文块异或 + for (int j = 0; j < BLOCK_SIZE; j++) + { + result[i + j] = (byte)(temp[j] ^ prevBlock[j]); + } + prevBlock = new byte[BLOCK_SIZE]; + Array.Copy(cipherText, i, prevBlock, 0, BLOCK_SIZE); + } + else + { + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + } + + // 移除 PKCS7 填充 + return Pkcs7Unpad(result); + } + + /// + /// SM4 解密 Base64 字符串 + /// + /// Base64 密文 + /// 密钥(16字节) + /// 编码方式(默认UTF-8) + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, Encoding encoding = null) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + + encoding ??= Encoding.UTF8; + byte[] cipher = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(cipher, key); + return encoding.GetString(decrypted); + } + + /// + /// SM4 解密 Base64 字符串 + /// + /// Base64 密文 + /// 密钥字符串 + /// 编码方式(默认UTF-8) + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, string keyString, Encoding encoding = null) + { + encoding ??= Encoding.UTF8; + byte[] key = GetKeyFromString(keyString, encoding); + return DecryptFromBase64(cipherText, key, encoding); + } + + /// + /// 生成随机密钥 + /// + /// 16字节随机密钥 + public static byte[] GenerateKey() + { + var key = new byte[16]; + new Random().NextBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制字符串 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #region 私有方法 + + private static uint[] GenerateRoundKeys(byte[] key) + { + uint[] roundKeys = new uint[32]; + uint[] mk = new uint[4]; + + // 将密钥转换为4个32位字 + for (int i = 0; i < 4; i++) + { + mk[i] = ((uint)key[i * 4] << 24) | + ((uint)key[i * 4 + 1] << 16) | + ((uint)key[i * 4 + 2] << 8) | + key[i * 4 + 3]; + } + + // 初始化轮密钥 + uint[] k = new uint[36]; + for (int i = 0; i < 4; i++) + { + k[i] = mk[i] ^ FK[i]; + } + + // 生成32个轮密钥 + for (int i = 0; i < 32; i++) + { + k[i + 4] = k[i] ^ TPrime(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]); + roundKeys[i] = k[i + 4]; + } + + return roundKeys; + } + + private static void EncryptBlock(byte[] block, uint[] roundKeys) + { + uint[] x = new uint[4]; + + // 将16字节转换为4个32位字 + for (int i = 0; i < 4; i++) + { + x[i] = ((uint)block[i * 4] << 24) | + ((uint)block[i * 4 + 1] << 16) | + ((uint)block[i * 4 + 2] << 8) | + block[i * 4 + 3]; + } + + // 32轮加密 + for (int i = 0; i < 32; i++) + { + uint temp = x[0]; + x[0] = x[1]; + x[1] = x[2]; + x[2] = x[3]; + x[3] = temp ^ T(x[1] ^ x[2] ^ x[3] ^ roundKeys[i]); + } + + // 反序并输出 + for (int i = 0; i < 4; i++) + { + block[i * 4] = (byte)(x[3 - i] >> 24); + block[i * 4 + 1] = (byte)(x[3 - i] >> 16); + block[i * 4 + 2] = (byte)(x[3 - i] >> 8); + block[i * 4 + 3] = (byte)x[3 - i]; + } + } + + private static uint T(uint x) + { + byte[] bytes = new byte[4]; + bytes[0] = (byte)(x >> 24); + bytes[1] = (byte)(x >> 16); + bytes[2] = (byte)(x >> 8); + bytes[3] = (byte)x; + + // S盒替换 + for (int i = 0; i < 4; i++) + { + bytes[i] = SBOX[bytes[i]]; + } + + uint result = ((uint)bytes[0] << 24) | + ((uint)bytes[1] << 16) | + ((uint)bytes[2] << 8) | + bytes[3]; + + // L变换 + return result ^ RotateLeft(result, 2) ^ RotateLeft(result, 10) ^ + RotateLeft(result, 18) ^ RotateLeft(result, 24); + } + + private static uint TPrime(uint x) + { + byte[] bytes = new byte[4]; + bytes[0] = (byte)(x >> 24); + bytes[1] = (byte)(x >> 16); + bytes[2] = (byte)(x >> 8); + bytes[3] = (byte)x; + + // S盒替换 + for (int i = 0; i < 4; i++) + { + bytes[i] = SBOX[bytes[i]]; + } + + uint result = ((uint)bytes[0] << 24) | + ((uint)bytes[1] << 16) | + ((uint)bytes[2] << 8) | + bytes[3]; + + // L'变换 + return result ^ RotateLeft(result, 13) ^ RotateLeft(result, 23); + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static byte[] Pkcs7Pad(byte[] data) + { + int padLen = BLOCK_SIZE - (data.Length % BLOCK_SIZE); + byte[] result = new byte[data.Length + padLen]; + Array.Copy(data, result, data.Length); + for (int i = data.Length; i < result.Length; i++) + { + result[i] = (byte)padLen; + } + return result; + } + + private static byte[] Pkcs7Unpad(byte[] data) + { + if (data.Length == 0) + return data; + + int padLen = data[data.Length - 1]; + if (padLen > BLOCK_SIZE || padLen == 0) + return data; + + // 验证填充 + for (int i = data.Length - padLen; i < data.Length; i++) + { + if (data[i] != padLen) + return data; + } + + byte[] result = new byte[data.Length - padLen]; + Array.Copy(data, result, result.Length); + return result; + } + + private static byte[] GetKeyFromString(string keyString, Encoding encoding) + { + byte[] keyBytes = encoding.GetBytes(keyString); + if (keyBytes.Length == 16) + return keyBytes; + if (keyBytes.Length < 16) + { + byte[] result = new byte[16]; + Array.Copy(keyBytes, result, keyBytes.Length); + return result; + } + byte[] truncated = new byte[16]; + Array.Copy(keyBytes, truncated, 16); + return truncated; + } + + #endregion + } + + /// + /// SM4 加密模式 + /// + public enum Sm4Mode + { + /// + /// 电子密码本模式 + /// + ECB, + + /// + /// 密码分组链接模式 + /// + CBC + } +} diff --git a/EasyTool.Core/CodeCategory/SnappyUtil.cs b/EasyTool.Core/CodeCategory/SnappyUtil.cs new file mode 100644 index 0000000..2f7285c --- /dev/null +++ b/EasyTool.Core/CodeCategory/SnappyUtil.cs @@ -0,0 +1,350 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace EasyTool.CodeCategory +{ + /// + /// Snappy 压缩工具类 + /// Snappy 是 Google 开发的快速压缩算法,注重速度而非压缩率 + /// 广泛用于大数据处理框架如 Hadoop、Spark + /// + public static class SnappyUtil + { + private const int MaxBlockSize = 65536; + private const int MaxInputSize = 2147483647; + + // 操作类型 + private const byte Literal = 0; + private const byte Copy1ByteOffset = 1; + private const byte Copy2ByteOffset = 2; + private const byte Copy4ByteOffset = 3; + + /// + /// 压缩数据 + /// + /// 原始数据 + /// 压缩后的数据 + public static byte[] Compress(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + using var output = new MemoryStream(); + using var writer = new BinaryWriter(output); + + // 写入变长长度 + WriteVarInt(writer, data.Length); + + int pos = 0; + while (pos < data.Length) + { + int remaining = data.Length - pos; + int blockSize = Math.Min(remaining, MaxBlockSize); + + CompressBlock(data, pos, blockSize, writer); + pos += blockSize; + } + + return output.ToArray(); + } + + /// + /// 解压数据 + /// + /// 压缩数据 + /// 原始数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length == 0) + return Array.Empty(); + + using var input = new MemoryStream(compressed); + using var reader = new BinaryReader(input); + + // 读取原始长度 + int originalLength = ReadVarInt(reader); + byte[] result = new byte[originalLength]; + + int pos = 0; + while (pos < originalLength) + { + int remaining = originalLength - pos; + int blockSize = Math.Min(remaining, MaxBlockSize); + + DecompressBlock(reader, result, pos, blockSize); + pos += blockSize; + } + + return result; + } + + /// + /// 压缩字符串 + /// + public static string CompressToBase64(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + public static string DecompressFromBase64(string compressedBase64) + { + if (string.IsNullOrEmpty(compressedBase64)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedBase64); + byte[] data = Decompress(compressed); + return System.Text.Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩后预估大小 + /// + public static int MaxCompressedLength(int sourceLength) + { + if (sourceLength < 0) + throw new ArgumentException("Source length cannot be negative", nameof(sourceLength)); + + // 变长整数最大 5 字节 + 每个块最大开销 + int blocks = (sourceLength + MaxBlockSize - 1) / MaxBlockSize; + return 5 + sourceLength + blocks * 4; + } + + private static void CompressBlock(byte[] input, int inputOffset, int inputLength, BinaryWriter writer) + { + int pos = inputOffset; + int end = inputOffset + inputLength; + + while (pos < end) + { + // 查找最长匹配 + int matchOffset = 0; + int matchLength = 0; + + // 简化的哈希表查找 + if (pos + 4 <= end) + { + FindMatch(input, pos, end, ref matchOffset, ref matchLength); + } + + if (matchLength >= 4) + { + // 写入字面量(如果有) + // 写入复制操作 + int offset = pos - matchOffset - 1; + int length = matchLength - 4; + + if (offset < 2048 && length < 8) + { + // Copy1 或 Copy2 + writer.Write((byte)((length << 2) | Copy1ByteOffset | (offset > 255 ? 0x80 : 0))); + if (offset > 255) + writer.Write((byte)((offset >> 8) | ((length - 8) << 5))); + writer.Write((byte)(offset & 0xFF)); + } + else if (offset < 65536) + { + writer.Write((byte)((length << 2) | Copy2ByteOffset)); + writer.Write((byte)(offset & 0xFF)); + writer.Write((byte)((offset >> 8) & 0xFF)); + } + else + { + writer.Write((byte)((length << 2) | Copy4ByteOffset)); + writer.Write((byte)(offset & 0xFF)); + writer.Write((byte)((offset >> 8) & 0xFF)); + writer.Write((byte)((offset >> 16) & 0xFF)); + writer.Write((byte)((offset >> 24) & 0xFF)); + } + + pos += matchLength; + } + else + { + // 写入字面量 + int literalLength = 1; + while (pos + literalLength < end && literalLength < 60) + { + if (FindMatchAt(input, pos + literalLength, end)) + break; + literalLength++; + } + + WriteLiteral(input, pos, literalLength, writer); + pos += literalLength; + } + } + } + + private static void FindMatch(byte[] input, int pos, int end, ref int matchOffset, ref int matchLength) + { + // 简化的匹配查找 + int searchStart = Math.Max(0, pos - 65536); + int bestLength = 0; + int bestOffset = 0; + + for (int i = searchStart; i < pos; i++) + { + int length = 0; + int maxLen = Math.Min(end - pos, 64); + + while (length < maxLen && input[i + length] == input[pos + length]) + { + length++; + } + + if (length > bestLength) + { + bestLength = length; + bestOffset = i; + } + } + + if (bestLength >= 4) + { + matchOffset = bestOffset; + matchLength = bestLength; + } + } + + private static bool FindMatchAt(byte[] input, int pos, int end) + { + if (pos + 4 > end) + return false; + + int searchStart = Math.Max(0, pos - 65536); + for (int i = searchStart; i < pos; i++) + { + int length = 0; + while (length < 4 && input[i + length] == input[pos + length]) + length++; + + if (length >= 4) + return true; + } + + return false; + } + + private static void WriteLiteral(byte[] input, int offset, int length, BinaryWriter writer) + { + if (length < 60) + { + writer.Write((byte)((length - 1) << 2)); + } + else if (length < 256) + { + writer.Write((byte)(60 << 2)); + writer.Write((byte)(length - 1)); + } + else + { + writer.Write((byte)(61 << 2)); + writer.Write((byte)((length - 1) & 0xFF)); + writer.Write((byte)(((length - 1) >> 8) & 0xFF)); + } + + writer.Write(input, offset, length); + } + + private static void DecompressBlock(BinaryReader reader, byte[] output, int outputOffset, int outputLength) + { + int pos = outputOffset; + int end = outputOffset + outputLength; + + while (pos < end) + { + byte op = reader.ReadByte(); + int opType = op & 0x03; + + if (opType == Literal) + { + int length; + if ((op >> 2) < 60) + { + length = (op >> 2) + 1; + } + else if ((op >> 2) == 60) + { + length = reader.ReadByte() + 1; + } + else + { + int extraBytes = (op >> 2) - 60 + 1; + length = 0; + for (int i = 0; i < extraBytes; i++) + { + length |= reader.ReadByte() << (i * 8); + } + length += 1; + } + + byte[] literal = reader.ReadBytes(length); + Array.Copy(literal, 0, output, pos, length); + pos += length; + } + else + { + int length, offset; + + if (opType == Copy1ByteOffset) + { + length = ((op >> 2) & 0x07) + 4; + offset = ((op & 0xE0) << 3) | reader.ReadByte(); + } + else if (opType == Copy2ByteOffset) + { + length = (op >> 2) + 1; + offset = reader.ReadByte() | (reader.ReadByte() << 8); + } + else + { + length = (op >> 2) + 1; + offset = reader.ReadByte() | (reader.ReadByte() << 8) | + (reader.ReadByte() << 16) | (reader.ReadByte() << 24); + } + + int srcPos = pos - offset; + for (int i = 0; i < length; i++) + { + output[pos++] = output[srcPos++]; + } + } + } + } + + private static void WriteVarInt(BinaryWriter writer, int value) + { + while (value >= 0x80) + { + writer.Write((byte)(value | 0x80)); + value >>= 7; + } + writer.Write((byte)value); + } + + private static int ReadVarInt(BinaryReader reader) + { + int result = 0; + int shift = 0; + byte b; + + do + { + b = reader.ReadByte(); + result |= (b & 0x7F) << shift; + shift += 7; + } while ((b & 0x80) != 0); + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/SonyflakeUtil.cs b/EasyTool.Core/CodeCategory/SonyflakeUtil.cs new file mode 100644 index 0000000..3e4ac6f --- /dev/null +++ b/EasyTool.Core/CodeCategory/SonyflakeUtil.cs @@ -0,0 +1,167 @@ +using System; +using System.Threading; + +namespace EasyTool.CodeCategory +{ + /// + /// Sonyflake ID 工具类 + /// Sonyflake 是 Sony 开发的分布式唯一 ID 生成算法 + /// 结构:39位时间戳 + 8位序列号 + 16位机器ID = 63位 + /// 比雪花 ID 使用更少的时间戳位,支持更长时间 + /// + public static class SonyflakeUtil + { + private static readonly DateTime Epoch = new DateTime(2014, 9, 1, 0, 0, 0, DateTimeKind.Utc); + private static long _lastTimestamp = -1L; + private static ushort _sequence = 0; + private static readonly object _lock = new object(); + private static readonly ushort _machineId; + + private const int TimestampBits = 39; + private const int SequenceBits = 8; + private const int MachineIdBits = 16; + + private const ushort MaxSequence = (1 << SequenceBits) - 1; + private const ushort MaxMachineId = (1 << MachineIdBits) - 1; + + static SonyflakeUtil() + { + // 自动生成机器 ID + byte[] bytes = new byte[2]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + _machineId = (ushort)((bytes[0] << 8) | bytes[1]); + } + + /// + /// 生成 Sonyflake ID + /// + /// 63位 ID + public static ulong Generate() + { + return Generate(_machineId); + } + + /// + /// 生成 Sonyflake ID(指定机器 ID) + /// + /// 机器 ID(0-65535) + /// 63位 ID + public static ulong Generate(ushort machineId) + { + if (machineId > MaxMachineId) + throw new ArgumentException($"Machine ID must be between 0 and {MaxMachineId}", nameof(machineId)); + + lock (_lock) + { + long timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence++; + if (_sequence > MaxSequence) + { + timestamp = WaitForNextTimestamp(_lastTimestamp); + _sequence = 0; + } + } + else + { + _sequence = 0; + } + + _lastTimestamp = timestamp; + + // 组合 ID + return ((ulong)timestamp << (SequenceBits + MachineIdBits)) | + ((ulong)_sequence << MachineIdBits) | + machineId; + } + } + + /// + /// 批量生成 Sonyflake ID + /// + /// 数量 + /// ID 数组 + public static ulong[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new ulong[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 从 Sonyflake ID 提取时间戳 + /// + /// Sonyflake ID + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(ulong id) + { + long timestamp = (long)(id >> (SequenceBits + MachineIdBits)); + return Epoch.AddMilliseconds(timestamp * 10); // 每 10ms 一个时间单位 + } + + /// + /// 从 Sonyflake ID 提取机器 ID + /// + /// Sonyflake ID + /// 机器 ID + public static ushort ExtractMachineId(ulong id) + { + return (ushort)(id & MaxMachineId); + } + + /// + /// 从 Sonyflake ID 提取序列号 + /// + /// Sonyflake ID + /// 序列号 + public static ushort ExtractSequence(ulong id) + { + return (ushort)((id >> MachineIdBits) & MaxSequence); + } + + /// + /// 解析 Sonyflake ID + /// + /// Sonyflake ID + /// 时间戳、机器 ID、序列号 + public static (DateTimeOffset Timestamp, ushort MachineId, ushort Sequence) Parse(ulong id) + { + return (ExtractTimestamp(id), ExtractMachineId(id), ExtractSequence(id)); + } + + /// + /// 获取当前机器 ID + /// + public static ushort MachineId => _machineId; + + /// + /// 获取 Sonyflake 纪元时间 + /// + public static DateTime GetEpoch() => Epoch; + + private static long GetCurrentTimestamp() + { + return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds / 10; + } + + private static long WaitForNextTimestamp(long lastTimestamp) + { + long timestamp = GetCurrentTimestamp(); + while (timestamp <= lastTimestamp) + { + Thread.SpinWait(10); + timestamp = GetCurrentTimestamp(); + } + return timestamp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/SqidsUtil.cs b/EasyTool.Core/CodeCategory/SqidsUtil.cs new file mode 100644 index 0000000..89a5963 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SqidsUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Sqids(以前叫 Hashids)工具类 + /// Sqids 是一种将数字数组编码为短字符串的算法 + /// 可逆、可配置字母表、无碰撞 + /// 常用于生成短 URL、混淆 ID 等 + /// + public static class SqidsUtil + { + private const string DefaultAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + private const int MinAlphabetLength = 3; + + private static readonly byte[] DefaultSalt = Array.Empty(); + + #region 默认实例方法 + + /// + /// 使用默认配置编码单个数字 + /// + /// 要编码的数字 + /// 编码字符串 + public static string Encode(ulong number) + { + return Encode(new[] { number }); + } + + /// + /// 使用默认配置编码数字数组 + /// + /// 要编码的数字数组 + /// 编码字符串 + public static string Encode(ulong[] numbers) + { + return Encode(numbers, DefaultAlphabet, DefaultSalt, 0); + } + + /// + /// 使用默认配置解码为单个数字 + /// + /// 编码字符串 + /// 解码的数字 + public static ulong DecodeSingle(string encoded) + { + var numbers = Decode(encoded); + if (numbers.Length == 0) + throw new ArgumentException("Invalid encoded string"); + return numbers[0]; + } + + /// + /// 使用默认配置解码 + /// + /// 编码字符串 + /// 解码的数字数组 + public static ulong[] Decode(string encoded) + { + return Decode(encoded, DefaultAlphabet, DefaultSalt); + } + + #endregion + + #region 自定义配置方法 + + /// + /// 使用自定义配置编码 + /// + /// 要编码的数字数组 + /// 自定义字母表 + /// 盐值 + /// 最小长度 + /// 编码字符串 + public static string Encode(ulong[] numbers, string alphabet, byte[] salt = null, int minLength = 0) + { + if (numbers == null || numbers.Length == 0) + throw new ArgumentException("Numbers cannot be empty", nameof(numbers)); + if (string.IsNullOrEmpty(alphabet) || alphabet.Length < MinAlphabetLength) + throw new ArgumentException($"Alphabet must be at least {MinAlphabetLength} characters", nameof(alphabet)); + + salt ??= Array.Empty(); + alphabet = ShuffleAlphabet(alphabet, salt); + + // 计算前缀 + char prefix = alphabet[0]; + string alphabetWithoutPrefix = alphabet.Substring(1) + alphabet[0]; + + var result = new StringBuilder(); + result.Append(prefix); + + // 编码每个数字 + for (int i = 0; i < numbers.Length; i++) + { + ulong number = numbers[i]; + string currentAlphabet = ConsistentShuffle(alphabetWithoutPrefix, salt, i); + + string encoded = EncodeNumber(number, currentAlphabet); + result.Append(encoded); + + if (i < numbers.Length - 1) + { + char separator = currentAlphabet[(int)(number % (ulong)(currentAlphabet.Length - 1))]; + result.Append(separator); + alphabetWithoutPrefix = RotateAlphabet(alphabetWithoutPrefix, encoded[encoded.Length - 1]); + } + } + + // 填充到最小长度 + string finalResult = result.ToString(); + if (minLength > 0 && finalResult.Length < minLength) + { + int diff = minLength - finalResult.Length; + string paddedAlphabet = ConsistentShuffle(alphabet, salt, 0); + finalResult = paddedAlphabet.Substring(0, diff / 2) + finalResult + paddedAlphabet.Substring(paddedAlphabet.Length - (diff - diff / 2)); + } + + return finalResult; + } + + /// + /// 使用自定义配置解码 + /// + /// 编码字符串 + /// 自定义字母表 + /// 盐值 + /// 解码的数字数组 + public static ulong[] Decode(string encoded, string alphabet, byte[] salt = null) + { + if (string.IsNullOrEmpty(encoded)) + throw new ArgumentException("Encoded string cannot be empty", nameof(encoded)); + if (string.IsNullOrEmpty(alphabet) || alphabet.Length < MinAlphabetLength) + throw new ArgumentException($"Alphabet must be at least {MinAlphabetLength} characters", nameof(alphabet)); + + salt ??= Array.Empty(); + alphabet = ShuffleAlphabet(alphabet, salt); + + // 移除填充 + encoded = RemovePadding(encoded, alphabet, salt); + + // 获取前缀 + char prefix = encoded[0]; + if (!alphabet.Contains(prefix)) + throw new ArgumentException("Invalid encoded string: unknown prefix"); + + string alphabetWithoutPrefix = alphabet.Substring(1) + alphabet[0]; + + var numbers = new List(); + string remaining = encoded.Substring(1); + + for (int i = 0; remaining.Length > 0; i++) + { + string currentAlphabet = ConsistentShuffle(alphabetWithoutPrefix, salt, i); + + // 找到分隔符 + int separatorIndex = -1; + for (int j = 0; j < remaining.Length; j++) + { + if (!currentAlphabet.Contains(remaining[j])) + { + separatorIndex = j; + break; + } + } + + string encodedNumber; + if (separatorIndex >= 0) + { + encodedNumber = remaining.Substring(0, separatorIndex); + remaining = remaining.Substring(separatorIndex + 1); + } + else + { + encodedNumber = remaining; + remaining = ""; + } + + ulong number = DecodeNumber(encodedNumber, currentAlphabet); + numbers.Add(number); + + if (encodedNumber.Length > 0) + { + alphabetWithoutPrefix = RotateAlphabet(alphabetWithoutPrefix, encodedNumber[encodedNumber.Length - 1]); + } + } + + return numbers.ToArray(); + } + + #endregion + + #region Sqids 实例 + + /// + /// 创建 Sqids 编码器实例 + /// + /// 字母表 + /// 盐值 + /// 最小长度 + /// Sqids 实例 + public static SqidsEncoder Create(string alphabet = null, byte[] salt = null, int minLength = 0) + { + return new SqidsEncoder(alphabet ?? DefaultAlphabet, salt, minLength); + } + + #endregion + + #region 私有方法 + + private static string ShuffleAlphabet(string alphabet, byte[] salt) + { + char[] chars = alphabet.ToCharArray(); + + if (salt.Length == 0) + return new string(chars); + + int j = chars.Length - 1; + int v = 0; + int p = 0; + + for (int i = chars.Length - 1; i > 0; i--, j--) + { + v %= salt.Length; + p += salt[v]; + int k = (salt[v] + p + i) % (i + 1); + + char temp = chars[i]; + chars[i] = chars[k]; + chars[k] = temp; + + v++; + } + + return new string(chars); + } + + private static string ConsistentShuffle(string alphabet, byte[] salt, int iteration) + { + if (salt.Length == 0) + return alphabet; + + char[] chars = alphabet.ToCharArray(); + int v = iteration % salt.Length; + + for (int i = chars.Length - 1; i > 0; i--) + { + int k = (salt[v] + i) % (i + 1); + + char temp = chars[i]; + chars[i] = chars[k]; + chars[k] = temp; + + v = (v + 1) % salt.Length; + } + + return new string(chars); + } + + private static string RotateAlphabet(string alphabet, char c) + { + int index = alphabet.IndexOf(c); + if (index < 0) + return alphabet; + + return alphabet.Substring(index + 1) + alphabet.Substring(0, index + 1); + } + + private static string EncodeNumber(ulong number, string alphabet) + { + var result = new StringBuilder(); + int baseLength = alphabet.Length; + + do + { + result.Insert(0, alphabet[(int)(number % (ulong)baseLength)]); + number /= (ulong)baseLength; + } while (number > 0); + + return result.ToString(); + } + + private static ulong DecodeNumber(string encoded, string alphabet) + { + ulong result = 0; + int baseLength = alphabet.Length; + + foreach (char c in encoded) + { + int index = alphabet.IndexOf(c); + if (index < 0) + throw new ArgumentException($"Invalid character: {c}"); + + result = result * (ulong)baseLength + (ulong)index; + } + + return result; + } + + private static string RemovePadding(string encoded, string alphabet, byte[] salt) + { + // 检查是否有有效的数字字符 + for (int i = 1; i < encoded.Length; i++) + { + if (alphabet.Contains(encoded[i])) + return encoded.Substring(i - 1); + } + + return encoded; + } + + #endregion + } + + /// + /// Sqids 编码器实例 + /// + public class SqidsEncoder + { + private readonly string _alphabet; + private readonly byte[] _salt; + private readonly int _minLength; + + /// + /// 创建 Sqids 编码器 + /// + /// 字母表 + /// 盐值 + /// 最小长度 + public SqidsEncoder(string alphabet, byte[] salt, int minLength) + { + _alphabet = alphabet; + _salt = salt ?? Array.Empty(); + _minLength = minLength; + } + + /// + /// 编码单个数字 + /// + /// 数字 + /// 编码字符串 + public string Encode(ulong number) + { + return SqidsUtil.Encode(new[] { number }, _alphabet, _salt, _minLength); + } + + /// + /// 编码数字数组 + /// + /// 数字数组 + /// 编码字符串 + public string Encode(ulong[] numbers) + { + return SqidsUtil.Encode(numbers, _alphabet, _salt, _minLength); + } + + /// + /// 解码为单个数字 + /// + /// 编码字符串 + /// 数字 + public ulong DecodeSingle(string encoded) + { + var numbers = SqidsUtil.Decode(encoded, _alphabet, _salt); + if (numbers.Length == 0) + throw new ArgumentException("Invalid encoded string"); + return numbers[0]; + } + + /// + /// 解码 + /// + /// 编码字符串 + /// 数字数组 + public ulong[] Decode(string encoded) + { + return SqidsUtil.Decode(encoded, _alphabet, _salt); + } + } +} diff --git a/EasyTool.Core/CodeCategory/TOTPUtil.cs b/EasyTool.Core/CodeCategory/TOTPUtil.cs new file mode 100644 index 0000000..1a2426f --- /dev/null +++ b/EasyTool.Core/CodeCategory/TOTPUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// TOTP(Time-based One-Time Password)和 HOTP(HMAC-based One-Time Password)工具类 + /// 用于生成和验证一次性密码,常用于双因素认证(2FA) + /// + public static class TOTPUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + // Base32 字符集(用于密钥编码) + private const string Base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + + #region TOTP(基于时间) + + /// + /// 生成 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码位数(默认6位) + /// 时间周期(默认30秒) + /// 验证码 + public static string GenerateTOTP(string secret, int digits = 6, int period = 30) + { + byte[] key = Base32Decode(secret); + return GenerateTOTP(key, digits, period); + } + + /// + /// 生成 TOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码位数 + /// 时间周期(秒) + /// 验证码 + public static string GenerateTOTP(byte[] secret, int digits = 6, int period = 30) + { + long counter = GetCurrentCounter(period); + return GenerateHOTP(secret, counter, digits); + } + + /// + /// 生成指定时间的 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 时间戳 + /// 验证码位数 + /// 时间周期 + /// 验证码 + public static string GenerateTOTP(string secret, DateTime timestamp, int digits = 6, int period = 30) + { + byte[] key = Base32Decode(secret); + long counter = GetCounter(timestamp, period); + return GenerateHOTP(key, counter, digits); + } + + /// + /// 验证 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码 + /// 验证码位数 + /// 时间周期 + /// 允许的时间窗口(前后各多少个周期) + /// 是否验证通过 + public static bool VerifyTOTP(string secret, string code, int digits = 6, int period = 30, int window = 1) + { + byte[] key = Base32Decode(secret); + return VerifyTOTP(key, code, digits, period, window); + } + + /// + /// 验证 TOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码 + /// 验证码位数 + /// 时间周期 + /// 允许的时间窗口 + /// 是否验证通过 + public static bool VerifyTOTP(byte[] secret, string code, int digits = 6, int period = 30, int window = 1) + { + if (string.IsNullOrEmpty(code) || code.Length != digits) + return false; + + long currentCounter = GetCurrentCounter(period); + + // 检查时间窗口内的所有可能值 + for (int i = -window; i <= window; i++) + { + long counter = currentCounter + i; + string expectedCode = GenerateHOTP(secret, counter, digits); + if (ConstantTimeEquals(code, expectedCode)) + { + return true; + } + } + + return false; + } + + #endregion + + #region HOTP(基于计数器) + + /// + /// 生成 HOTP 验证码 + /// + /// 密钥(Base32编码) + /// 计数器值 + /// 验证码位数 + /// 验证码 + public static string GenerateHOTP(string secret, long counter, int digits = 6) + { + byte[] key = Base32Decode(secret); + return GenerateHOTP(key, counter, digits); + } + + /// + /// 生成 HOTP 验证码 + /// + /// 密钥(字节数组) + /// 计数器值 + /// 验证码位数 + /// 验证码 + public static string GenerateHOTP(byte[] secret, long counter, int digits = 6) + { + // 将计数器转换为大端序字节数组 + byte[] counterBytes = BitConverter.GetBytes(counter); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(counterBytes); + } + + // 使用 HMAC-SHA1 计算 + using var hmac = new HMACSHA1(secret); + byte[] hash = hmac.ComputeHash(counterBytes); + + // 动态截断 + int offset = hash[hash.Length - 1] & 0x0F; + int binaryCode = ((hash[offset] & 0x7F) << 24) | + ((hash[offset + 1] & 0xFF) << 16) | + ((hash[offset + 2] & 0xFF) << 8) | + (hash[offset + 3] & 0xFF); + + int code = binaryCode % (int)Math.Pow(10, digits); + + return code.ToString().PadLeft(digits, '0'); + } + + /// + /// 验证 HOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码 + /// 计数器值 + /// 验证码位数 + /// 允许的计数器窗口 + /// 验证结果和下一个计数器值 + public static (bool Valid, long NextCounter) VerifyHOTP(string secret, string code, long counter, int digits = 6, int window = 10) + { + byte[] key = Base32Decode(secret); + return VerifyHOTP(key, code, counter, digits, window); + } + + /// + /// 验证 HOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码 + /// 计数器值 + /// 验证码位数 + /// 允许的计数器窗口 + /// 验证结果和下一个计数器值 + public static (bool Valid, long NextCounter) VerifyHOTP(byte[] secret, string code, long counter, int digits = 6, int window = 10) + { + if (string.IsNullOrEmpty(code) || code.Length != digits) + return (false, counter); + + for (long i = counter; i < counter + window; i++) + { + string expectedCode = GenerateHOTP(secret, i, digits); + if (ConstantTimeEquals(code, expectedCode)) + { + return (true, i + 1); + } + } + + return (false, counter); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成随机密钥 + /// + /// 密钥长度(字节,默认20) + /// Base32编码的密钥 + public static string GenerateSecret(int length = 20) + { + byte[] bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return Base32Encode(bytes); + } + + /// + /// 生成随机密钥(字节数组) + /// + /// 密钥长度 + /// 密钥字节数组 + public static byte[] GenerateSecretBytes(int length = 20) + { + byte[] bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return bytes; + } + + #endregion + + #region URI 生成(用于二维码) + + /// + /// 生成 otpauth:// 格式的 URI + /// + /// 发行者(应用名称) + /// 账户名 + /// 密钥(Base32编码) + /// 验证码位数 + /// 时间周期 + /// otpauth URI + public static string GetOtpAuthUri(string issuer, string account, string secret, int digits = 6, int period = 30) + { + string encodedIssuer = Uri.EscapeDataString(issuer); + string encodedAccount = Uri.EscapeDataString(account); + + return $"otpauth://totp/{encodedIssuer}:{encodedAccount}?secret={secret}&issuer={encodedIssuer}&digits={digits}&period={period}"; + } + + /// + /// 生成 HOTP 的 otpauth:// 格式 URI + /// + /// 发行者 + /// 账户名 + /// 密钥 + /// 计数器 + /// 验证码位数 + /// otpauth URI + public static string GetHotpAuthUri(string issuer, string account, string secret, long counter, int digits = 6) + { + string encodedIssuer = Uri.EscapeDataString(issuer); + string encodedAccount = Uri.EscapeDataString(account); + + return $"otpauth://hotp/{encodedIssuer}:{encodedAccount}?secret={secret}&issuer={encodedIssuer}&digits={digits}&counter={counter}"; + } + + #endregion + + #region 时间工具 + + /// + /// 获取当前计数器值 + /// + /// 时间周期 + /// 计数器值 + public static long GetCurrentCounter(int period = 30) + { + return GetCounter(DateTime.UtcNow, period); + } + + /// + /// 获取指定时间的计数器值 + /// + /// 时间戳 + /// 时间周期 + /// 计数器值 + public static long GetCounter(DateTime timestamp, int period = 30) + { + long elapsedSeconds = (long)(timestamp.ToUniversalTime() - Epoch).TotalSeconds; + return elapsedSeconds / period; + } + + /// + /// 获取当前验证码的剩余有效时间 + /// + /// 时间周期 + /// 剩余秒数 + public static int GetRemainingSeconds(int period = 30) + { + long elapsedSeconds = (long)(DateTime.UtcNow - Epoch).TotalSeconds; + return period - (int)(elapsedSeconds % period); + } + + #endregion + + #region 私有方法 + + private static string Base32Encode(byte[] data) + { + var result = new StringBuilder((data.Length * 8 + 4) / 5); + + int i = 0; + int remainingBits = 0; + int currentByte = 0; + + while (i < data.Length || remainingBits > 0) + { + if (remainingBits < 5 && i < data.Length) + { + currentByte = (currentByte << 8) | data[i++]; + remainingBits += 8; + } + + if (remainingBits >= 5) + { + int index = (currentByte >> (remainingBits - 5)) & 0x1F; + result.Append(Base32Chars[index]); + remainingBits -= 5; + } + else if (remainingBits > 0) + { + int index = (currentByte << (5 - remainingBits)) & 0x1F; + result.Append(Base32Chars[index]); + remainingBits = 0; + } + } + + return result.ToString(); + } + + private static byte[] Base32Decode(string data) + { + data = data.ToUpperInvariant().Replace(" ", "").Replace("-", ""); + + var result = new List(); + int currentByte = 0; + int remainingBits = 0; + + foreach (char c in data) + { + int value = Base32Chars.IndexOf(c); + if (value < 0) + continue; + + currentByte = (currentByte << 5) | value; + remainingBits += 5; + + while (remainingBits >= 8) + { + result.Add((byte)((currentByte >> (remainingBits - 8)) & 0xFF)); + remainingBits -= 8; + } + } + + return result.ToArray(); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/TSIDUtil.cs b/EasyTool.Core/CodeCategory/TSIDUtil.cs new file mode 100644 index 0000000..cd2a776 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TSIDUtil.cs @@ -0,0 +1,422 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; +using System.Threading; + +namespace EasyTool.CodeCategory +{ + /// + /// TSID(Time-Sorted ID)工具类 + /// TSID 是一种时间排序的唯一标识符 + /// 支持多种格式:TSID-256(8字符)、TSID-512(13字符)、TSID-1024(18字符) + /// + public static class TSIDUtil + { + private static readonly DateTime Epoch = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static long _lastTimestamp = -1L; + private static int _sequence = 0; + private static readonly object _lock = new object(); + + // Base32 编码字符集(Crockford) + private const string Base32Chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + + // 节点ID(自动生成) + private static readonly int _nodeId; + + static TSIDUtil() + { + // 自动生成节点ID(0-31) + byte[] nodeIdBytes = new byte[1]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(nodeIdBytes); + _nodeId = nodeIdBytes[0] & 0x1F; + } + + #region TSID-256(8字符,32位) + + /// + /// 生成 TSID-256(8字符) + /// + /// 8字符的 TSID-256 + public static string GenerateTsid256() + { + var bytes = GenerateTsid256Bytes(); + return EncodeBase32(bytes, 5); + } + + /// + /// 生成 TSID-256 字节数组 + /// + /// 4字节的 TSID-256 + public static byte[] GenerateTsid256Bytes() + { + long timestamp = GetCurrentTimestamp(); + int sequence; + + lock (_lock) + { + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & 0xFF; + if (_sequence == 0) + { + timestamp = WaitForNextTimestamp(_lastTimestamp); + } + } + else + { + _sequence = 0; + } + _lastTimestamp = timestamp; + sequence = _sequence; + } + + // 32位:24位时间戳 + 8位序列号 + uint value = ((uint)(timestamp & 0xFFFFFF) << 8) | (uint)sequence; + + return BitConverter.GetBytes(value); + } + + #endregion + + #region TSID-512(13字符,51位) + + /// + /// 生成 TSID-512(13字符) + /// + /// 13字符的 TSID-512 + public static string GenerateTsid512() + { + return GenerateTsid512(_nodeId); + } + + /// + /// 生成 TSID-512(指定节点ID) + /// + /// 节点ID(0-31) + /// 13字符的 TSID-512 + public static string GenerateTsid512(int nodeId) + { + var bytes = GenerateTsid512Bytes(nodeId); + return EncodeBase32(bytes, 8); + } + + /// + /// 生成 TSID-512 字节数组 + /// + /// 节点ID(0-31) + /// 8字节的 TSID-512 + public static byte[] GenerateTsid512Bytes(int nodeId) + { + if (nodeId < 0 || nodeId > 31) + throw new ArgumentException("Node ID must be between 0 and 31", nameof(nodeId)); + + long timestamp = GetCurrentTimestamp(); + int sequence; + + lock (_lock) + { + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & 0x7FFF; + if (_sequence == 0) + { + timestamp = WaitForNextTimestamp(_lastTimestamp); + } + } + else + { + _sequence = 0; + } + _lastTimestamp = timestamp; + sequence = _sequence; + } + + // 使用序列号:42位时间戳 + 5位节点ID + 16位序列号 + ulong value = ((ulong)(timestamp & 0x3FFFFFFFFFF) << 21) | + ((ulong)((uint)nodeId & 0x1F) << 16) | + (ulong)((uint)sequence & 0xFFFF); + + return BitConverter.GetBytes(value); + } + + #endregion + + #region TSID-1024(18字符,90位) + + /// + /// 生成 TSID-1024(18字符) + /// + /// 18字符的 TSID-1024 + public static string GenerateTsid1024() + { + return GenerateTsid1024(_nodeId); + } + + /// + /// 生成 TSID-1024(指定节点ID) + /// + /// 节点ID(0-31) + /// 18字符的 TSID-1024 + public static string GenerateTsid1024(int nodeId) + { + var bytes = GenerateTsid1024Bytes(nodeId); + return EncodeBase32(bytes, 12); + } + + /// + /// 生成 TSID-1024 字节数组 + /// + /// 节点ID(0-31) + /// 12字节的 TSID-1024 + public static byte[] GenerateTsid1024Bytes(int nodeId) + { + if (nodeId < 0 || nodeId > 31) + throw new ArgumentException("Node ID must be between 0 and 31", nameof(nodeId)); + + long timestamp = GetCurrentTimestamp(); + int sequence; + byte[] random; + + lock (_lock) + { + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & 0x3FFFFF; + if (_sequence == 0) + { + timestamp = WaitForNextTimestamp(_lastTimestamp); + } + } + else + { + _sequence = 0; + } + _lastTimestamp = timestamp; + sequence = _sequence; + } + + // 生成随机部分 + random = new byte[4]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(random); + + var result = new byte[12]; + + // 时间戳(48位,6字节) + result[0] = (byte)(timestamp >> 40); + result[1] = (byte)(timestamp >> 32); + result[2] = (byte)(timestamp >> 24); + result[3] = (byte)(timestamp >> 16); + result[4] = (byte)(timestamp >> 8); + result[5] = (byte)timestamp; + + // 节点ID + 随机(32位,4字节) + result[6] = (byte)(nodeId << 3 | (random[0] >> 5)); + result[7] = (byte)((random[0] << 3) | (random[1] >> 5)); + result[8] = (byte)((random[1] << 3) | (random[2] >> 5)); + result[9] = (byte)((random[2] << 3) | (random[3] >> 5)); + result[10] = (byte)(random[3] << 3); + + // 序列号(16位,2字节) + result[10] |= (byte)(sequence >> 13); + result[11] = (byte)(sequence >> 5); + + return result; + } + + #endregion + + #region 通用方法 + + /// + /// 生成默认 TSID(TSID-512) + /// + /// 13字符的 TSID + public static string Generate() + { + return GenerateTsid512(); + } + + /// + /// 从 TSID 提取时间戳 + /// + /// TSID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string tsid) + { + byte[] bytes = DecodeBase32(tsid); + + // 提取时间戳(前42位) + long timestamp = 0; + for (int i = 0; i < Math.Min(6, bytes.Length); i++) + { + timestamp = (timestamp << 8) | bytes[i]; + } + + // 根据长度调整 + if (tsid.Length <= 8) + { + timestamp = (timestamp >> 8) & 0xFFFFFF; + } + + return Epoch.AddMilliseconds(timestamp); + } + + /// + /// 验证 TSID 是否有效 + /// + /// TSID 字符串 + /// 是否有效 + public static bool IsValid(string tsid) + { + if (string.IsNullOrEmpty(tsid)) + return false; + + int len = tsid.Length; + if (len != 8 && len != 13 && len != 18) + return false; + + foreach (char c in tsid) + { + if (!Base32Chars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 尝试解析 TSID + /// + /// TSID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string tsid, out byte[] bytes) + { + bytes = null; + if (!IsValid(tsid)) + return false; + + try + { + bytes = DecodeBase32(tsid); + return true; + } + catch + { + return false; + } + } + + /// + /// 批量生成 TSID + /// + /// 数量 + /// TSID 数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 比较 TSID 的时间顺序 + /// + /// 第一个 TSID + /// 第二个 TSID + /// -1: tsid1早于tsid2, 0: 相同, 1: tsid1晚于tsid2 + public static int Compare(string tsid1, string tsid2) + { + return string.Compare(tsid1, tsid2, StringComparison.Ordinal); + } + + /// + /// 获取或设置节点ID + /// + public static int NodeId => _nodeId; + + #endregion + + #region 私有方法 + + private static long GetCurrentTimestamp() + { + return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + } + + private static long WaitForNextTimestamp(long lastTimestamp) + { + long timestamp = GetCurrentTimestamp(); + while (timestamp <= lastTimestamp) + { + Thread.SpinWait(10); + timestamp = GetCurrentTimestamp(); + } + return timestamp; + } + + private static string EncodeBase32(byte[] bytes, int length) + { + var result = new StringBuilder(length); + + int bits = 0; + int value = 0; + + foreach (byte b in bytes) + { + value = (value << 8) | b; + bits += 8; + + while (bits >= 5) + { + result.Append(Base32Chars[(value >> (bits - 5)) & 0x1F]); + bits -= 5; + } + } + + if (bits > 0) + { + result.Append(Base32Chars[(value << (5 - bits)) & 0x1F]); + } + + return result.ToString().PadLeft(length, '0'); + } + + private static byte[] DecodeBase32(string encoded) + { + var result = new List(); + + int bits = 0; + int value = 0; + + foreach (char c in encoded) + { + int index = Base32Chars.IndexOf(char.ToUpperInvariant(c)); + if (index < 0) + throw new ArgumentException($"Invalid character: {c}"); + + value = (value << 5) | index; + bits += 5; + + while (bits >= 8) + { + result.Add((byte)((value >> (bits - 8)) & 0xFF)); + bits -= 8; + } + } + + return result.ToArray(); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/TigerUtil.cs b/EasyTool.Core/CodeCategory/TigerUtil.cs new file mode 100644 index 0000000..7e00dd1 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TigerUtil.cs @@ -0,0 +1,291 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Tiger 哈希工具类 + /// Tiger 是由 Ross Anderson 和 Eli Biham 设计的快速哈希算法 + /// 专为 64 位处理器优化,输出 192 位(24 字节) + /// + public static class TigerUtil + { + private const int HashSize = 24; // 192位 + private const int BlockSize = 64; // 512位 + + // S-boxes + private static readonly ulong[] S = new ulong[] + { + 0x02AAB17CF7E90C5E, 0xAC424B03E243A8EC, 0x72CD5BE30DD5FCD3, 0x6D019B93F6F97F3A, + 0xCD9978FFD21F9193, 0x7573A1C970802FAE, 0xB164326B922A83BB, 0x46883EEE04915870, + 0xEAACE3057103ECE6, 0xC54169B808A3535C, 0x4CE754918DDEC47C, 0x0AA2F4DFDC0DF40C, + 0x10B76F18A74DBEFA, 0xC6CCB6235AD1AB6A, 0x13726121572FE2FF, 0x1A488C6F199D921E, + 0x4BC9F9F4DA0007CA, 0x26F5E6F6E85241C7, 0x859079DBEA5947B6, 0x4F1885B5EB4F880C, + 0xD78E761EA6F7CBA0, 0x8E36428C52B5C17D, 0x69CF6827373063C1, 0xB607C93D9BB4C56E, + 0x7D820E760E76B5EA, 0x645C9CC6F07FDC42, 0xBF38A078243342E0, 0x5F6B343C9D2E7D04, + 0xF2C28AEB600B0EC6, 0x6C0ED85F7254BCAC, 0x71592281A4DB4FE5, 0x1967FA69CE0FED9F, + 0xFD5293F8B96545DB, 0xC879E84D5BB62F8F, 0x860248920193194E, 0xA4F953AA47EE7048, + 0xD957E363A198BF6B, 0x327894F2FDDC3BBA, 0x9F7F973ED03B1AE9, 0x1B505014AE5AC36B, + 0xE7CC8C8EFB4C41F7, 0x7D4DA8DE2296204E, 0x7E9791D04B8C6B88, 0x39A8B0D45C357F47, + 0x723F453E1A6ED868, 0x59E59E13C6A5C3BF, 0xB6F3169AB9916821, 0x9E6B0E7A3A2888F7 + }; + + /// + /// 计算 Tiger 哈希值 + /// + /// 输入数据 + /// 24字节哈希值 + public static byte[] Hash(byte[] data) + { + if (data == null || data.Length == 0) + return new byte[HashSize]; + + // 初始值 + ulong a = 0x0123456789ABCDEF; + ulong b = 0xFEDCBA9876543210; + ulong c = 0xF096A5B4C3B2E187; + + // 填充 + byte[] padded = PadMessage(data); + int blocks = padded.Length / BlockSize; + + for (int i = 0; i < blocks; i++) + { + ulong[] x = new ulong[8]; + for (int j = 0; j < 8; j++) + { + x[j] = BitConverter.ToUInt64(padded, i * BlockSize + j * 8); + } + + TigerRound(ref a, ref b, ref c, x); + } + + return Combine(a, b, c); + } + + /// + /// 计算字符串的 Tiger 哈希值 + /// + /// 输入文本 + /// 十六进制哈希字符串 + public static string HashString(string text) + { + if (string.IsNullOrEmpty(text)) + return new string('0', HashSize * 2); + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] hash = Hash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 验证哈希值 + /// + /// 原始数据 + /// 预期哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] hash) + { + if (hash == null || hash.Length != HashSize) + return false; + + byte[] computed = Hash(data); + return SlowEquals(computed, hash); + } + + /// + /// 验证字符串哈希 + /// + /// 原始文本 + /// 预期哈希值(十六进制) + /// 是否匹配 + public static bool VerifyString(string text, string hashHex) + { + if (string.IsNullOrEmpty(hashHex) || hashHex.Length != HashSize * 2) + return false; + + string computed = HashString(text); + return string.Equals(computed, hashHex, StringComparison.OrdinalIgnoreCase); + } + + private static byte[] PadMessage(byte[] data) + { + int length = data.Length; + int paddingLength = BlockSize - ((length + 9) % BlockSize); + if (paddingLength == BlockSize) paddingLength = 0; + + byte[] padded = new byte[length + 1 + paddingLength + 8]; + Array.Copy(data, padded, length); + + // 添加 0x01 填充 + padded[length] = 0x01; + + // 添加长度(位数) + ulong bitLength = (ulong)length * 8; + for (int i = 0; i < 8; i++) + { + padded[padded.Length - 1 - i] = (byte)(bitLength >> (i * 8)); + } + + return padded; + } + + private static void TigerRound(ref ulong a, ref ulong b, ref ulong c, ulong[] x) + { + // 保存原始值 + ulong aa = a, bb = b, cc = c; + + // Pass 1 + c ^= x[0]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[2]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[3]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[5]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[6]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + // Pass 2 + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[6]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[5]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[3]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[2]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[0]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + // Pass 3 + c ^= x[0]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[2]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[3]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[5]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[6]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + // 反馈 + a ^= aa; + b -= bb; + c += cc; + } + + private static ulong Table(ulong x, int i) + { + byte b = (byte)(x >> (i * 8)); + return S[b % S.Length]; + } + + private static byte[] Combine(ulong a, ulong b, ulong c) + { + byte[] result = new byte[24]; + BitConverter.GetBytes(a).CopyTo(result, 0); + BitConverter.GetBytes(b).CopyTo(result, 8); + BitConverter.GetBytes(c).CopyTo(result, 16); + return result; + } + + private static bool SlowEquals(byte[] a, byte[] b) + { + uint diff = (uint)a.Length ^ (uint)b.Length; + for (int i = 0; i < a.Length && i < b.Length; i++) + diff |= (uint)(a[i] ^ b[i]); + return diff == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/TimestampUtil.cs b/EasyTool.Core/CodeCategory/TimestampUtil.cs new file mode 100644 index 0000000..8104870 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TimestampUtil.cs @@ -0,0 +1,581 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 时间戳工具类 + /// 提供时间戳的生成、转换、格式化等功能 + /// 支持10位秒级和13位毫秒级时间戳 + /// + public static class TimestampUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + #region 获取时间戳 + + /// + /// 获取当前时间的秒级时间戳(10位) + /// + /// 秒级时间戳 + public static long Now() + { + return (long)(DateTime.UtcNow - Epoch).TotalSeconds; + } + + /// + /// 获取当前时间的毫秒级时间戳(13位) + /// + /// 毫秒级时间戳 + public static long NowMs() + { + return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + } + + /// + /// 获取当前时间的微秒级时间戳(16位) + /// + /// 微秒级时间戳 + public static long NowUs() + { + var ts = DateTime.UtcNow - Epoch; + return (long)(ts.TotalMilliseconds * 1000); + } + + /// + /// 获取当前时间的纳秒级时间戳(19位) + /// + /// 纳秒级时间戳 + public static long NowNs() + { + var ts = DateTime.UtcNow - Epoch; + return (long)(ts.TotalMilliseconds * 1000000); + } + + /// + /// 获取当前时间戳字符串 + /// + /// 精度:s(秒), ms(毫秒), us(微秒), ns(纳秒) + /// 时间戳字符串 + public static string NowString(string precision = "ms") + { + return precision.ToLowerInvariant() switch + { + "s" => Now().ToString(), + "ms" => NowMs().ToString(), + "us" => NowUs().ToString(), + "ns" => NowNs().ToString(), + _ => NowMs().ToString() + }; + } + + #endregion + + #region DateTime 转时间戳 + + /// + /// 将 DateTime 转换为秒级时间戳 + /// + /// 日期时间 + /// 秒级时间戳 + public static long ToTimestamp(DateTime dateTime) + { + return (long)(dateTime.ToUniversalTime() - Epoch).TotalSeconds; + } + + /// + /// 将 DateTime 转换为毫秒级时间戳 + /// + /// 日期时间 + /// 毫秒级时间戳 + public static long ToTimestampMs(DateTime dateTime) + { + return (long)(dateTime.ToUniversalTime() - Epoch).TotalMilliseconds; + } + + /// < /// 将 DateTime 转换为指定精度的时间戳 + ///
+ /// 日期时间 + /// 精度:s, ms, us, ns + /// 时间戳 + public static long ToTimestamp(DateTime dateTime, string precision) + { + var ts = dateTime.ToUniversalTime() - Epoch; + return precision.ToLowerInvariant() switch + { + "s" => (long)ts.TotalSeconds, + "ms" => (long)ts.TotalMilliseconds, + "us" => (long)(ts.TotalMilliseconds * 1000), + "ns" => (long)(ts.TotalMilliseconds * 1000000), + _ => (long)ts.TotalMilliseconds + }; + } + + #endregion + + #region 时间戳转 DateTime + + /// + /// 将时间戳转换为 DateTime(自动识别精度) + /// + /// 时间戳 + /// DateTime + public static DateTime FromTimestamp(long timestamp) + { + // 自动判断精度 + if (timestamp > 1000000000000L) + { + // 13位毫秒级 + if (timestamp > 10000000000000L) + { + // 16位微秒级 + if (timestamp > 100000000000000L) + { + // 19位纳秒级 + return Epoch.AddTicks(timestamp / 100); + } + return Epoch.AddTicks(timestamp / 10); + } + return FromTimestampMs(timestamp); + } + else + { + // 10位秒级 + return FromTimestampSeconds(timestamp); + } + } + + /// + /// 将秒级时间戳转换为 DateTime + /// + /// 秒级时间戳 + /// DateTime + public static DateTime FromTimestampSeconds(long timestamp) + { + return Epoch.AddSeconds(timestamp); + } + + /// + /// 将毫秒级时间戳转换为 DateTime + /// + /// 毫秒级时间戳 + /// DateTime + public static DateTime FromTimestampMs(long timestamp) + { + return Epoch.AddMilliseconds(timestamp); + } + + /// + /// 将字符串时间戳转换为 DateTime + /// + /// 时间戳字符串 + /// DateTime + public static DateTime FromString(string timestamp) + { + if (string.IsNullOrEmpty(timestamp)) + throw new ArgumentException("Timestamp cannot be null or empty", nameof(timestamp)); + + if (long.TryParse(timestamp, out long ts)) + { + return FromTimestamp(ts); + } + + throw new ArgumentException("Invalid timestamp format", nameof(timestamp)); + } + + #endregion + + #region 格式化 + + /// + /// 格式化当前时间戳 + /// + /// 日期格式(默认 yyyy-MM-dd HH:mm:ss) + /// 格式化的日期字符串 + public static string Format(string format = "yyyy-MM-dd HH:mm:ss") + { + return DateTime.UtcNow.ToString(format); + } + + /// + /// 格式化时间戳 + /// + /// 时间戳 + /// 日期格式 + /// 格式化的日期字符串 + public static string Format(long timestamp, string format = "yyyy-MM-dd HH:mm:ss") + { + return FromTimestamp(timestamp).ToString(format); + } + + /// + /// 格式化为 ISO 8601 格式 + /// + /// 时间戳 + /// ISO 8601 格式字符串 + public static string ToIso8601(long timestamp) + { + return FromTimestamp(timestamp).ToString("o"); + } + + /// + /// 从 ISO 8601 格式解析 + /// + /// ISO 8601 格式字符串 + /// 时间戳(毫秒) + public static long FromIso8601(string iso8601) + { + if (DateTime.TryParse(iso8601, out DateTime dt)) + { + return ToTimestampMs(dt); + } + throw new ArgumentException("Invalid ISO 8601 format", nameof(iso8601)); + } + + #endregion + + #region 时间计算 + + /// + /// 计算两个时间戳之间的时间差 + /// + /// 开始时间戳 + /// 结束时间戳 + /// 时间差 + public static TimeSpan Diff(long start, long end) + { + var startTime = FromTimestamp(start); + var endTime = FromTimestamp(end); + return endTime - startTime; + } + + /// + /// 添加秒数 + /// + /// 时间戳(秒) + /// 秒数 + /// 新的时间戳 + public static long AddSeconds(long timestamp, int seconds) + { + return timestamp + seconds; + } + + /// + /// 添加分钟 + /// + /// 时间戳(秒) + /// 分钟数 + /// 新的时间戳 + public static long AddMinutes(long timestamp, int minutes) + { + return timestamp + minutes * 60; + } + + /// + /// 添加小时 + /// + /// 时间戳(秒) + /// 小时数 + /// 新的时间戳 + public static long AddHours(long timestamp, int hours) + { + return timestamp + hours * 3600; + } + + /// + /// 添加天数 + /// + /// 时间戳(秒) + /// 天数 + /// 新的时间戳 + public static long AddDays(long timestamp, int days) + { + return timestamp + days * 86400; + } + + /// + /// 获取今天开始时间戳(00:00:00) + /// + /// 秒级时间戳 + public static long TodayStart() + { + var today = DateTime.UtcNow.Date; + return ToTimestamp(today); + } + + /// + /// 获取今天结束时间戳(23:59:59) + /// + /// 秒级时间戳 + public static long TodayEnd() + { + var today = DateTime.UtcNow.Date.AddDays(1).AddSeconds(-1); + return ToTimestamp(today); + } + + /// + /// 获取本周开始时间戳 + /// + /// 秒级时间戳 + public static long WeekStart() + { + var today = DateTime.UtcNow.Date; + var diff = (7 + (today.DayOfWeek - DayOfWeek.Monday)) % 7; + var weekStart = today.AddDays(-diff); + return ToTimestamp(weekStart); + } + + /// + /// 获取本月开始时间戳 + /// + /// 秒级时间戳 + public static long MonthStart() + { + var today = DateTime.UtcNow; + var monthStart = new DateTime(today.Year, today.Month, 1); + return ToTimestamp(monthStart); + } + + /// + /// 获取本年开始时间戳 + /// + /// 秒级时间戳 + public static long YearStart() + { + var today = DateTime.UtcNow; + var yearStart = new DateTime(today.Year, 1, 1); + return ToTimestamp(yearStart); + } + + #endregion + + #region 验证和比较 + + /// + /// 验证时间戳是否有效 + /// + /// 时间戳 + /// 是否有效 + public static bool IsValid(long timestamp) + { + try + { + var dt = FromTimestamp(timestamp); + return dt.Year >= 1970 && dt.Year <= 2100; + } + catch + { + return false; + } + } + + /// + /// 验证时间戳字符串是否有效 + /// + /// 时间戳字符串 + /// 是否有效 + public static bool IsValid(string timestamp) + { + if (string.IsNullOrEmpty(timestamp)) + return false; + + if (long.TryParse(timestamp, out long ts)) + { + return IsValid(ts); + } + + return false; + } + + /// + /// 比较两个时间戳 + /// + /// 时间戳1 + /// 时间戳2 + /// -1: ts1<ts2, 0: 相等, 1: ts1>ts2 + public static int Compare(long ts1, long ts2) + { + return ts1.CompareTo(ts2); + } + + /// + /// 判断时间戳是否在指定范围内 + /// + /// 时间戳 + /// 开始时间戳 + /// 结束时间戳 + /// 是否在范围内 + public static bool IsBetween(long timestamp, long start, long end) + { + return timestamp >= start && timestamp <= end; + } + + /// + /// 判断是否是今天 + /// + /// 时间戳 + /// 是否是今天 + public static bool IsToday(long timestamp) + { + var dt = FromTimestamp(timestamp); + var today = DateTime.UtcNow.Date; + return dt.Date == today; + } + + /// + /// 判断是否是昨天 + /// + /// 时间戳 + /// 是否是昨天 + public static bool IsYesterday(long timestamp) + { + var dt = FromTimestamp(timestamp); + var yesterday = DateTime.UtcNow.Date.AddDays(-1); + return dt.Date == yesterday; + } + + /// + /// 判断是否是明天 + /// + /// 时间戳 + /// 是否是明天 + public static bool IsTomorrow(long timestamp) + { + var dt = FromTimestamp(timestamp); + var tomorrow = DateTime.UtcNow.Date.AddDays(1); + return dt.Date == tomorrow; + } + + #endregion + + #region 批量转换 + + /// + /// 批量将 DateTime 转换为时间戳 + /// + /// 日期时间数组 + /// 是否使用毫秒精度 + /// 时间戳数组 + public static long[] BatchToTimestamp(DateTime[] dateTimes, bool milliseconds = false) + { + var result = new long[dateTimes.Length]; + for (int i = 0; i < dateTimes.Length; i++) + { + result[i] = milliseconds ? ToTimestampMs(dateTimes[i]) : ToTimestamp(dateTimes[i]); + } + return result; + } + + /// + /// 批量将时间戳转换为 DateTime + /// + /// 时间戳数组 + /// DateTime 数组 + public static DateTime[] BatchFromTimestamp(long[] timestamps) + { + var result = new DateTime[timestamps.Length]; + for (int i = 0; i < timestamps.Length; i++) + { + result[i] = FromTimestamp(timestamps[i]); + } + return result; + } + + #endregion + + #region 友好显示 + + /// + /// 获取友好的时间显示(如:刚刚、5分钟前、昨天等) + /// + /// 时间戳 + /// 友好显示 + public static string Friendly(long timestamp) + { + var dt = FromTimestamp(timestamp); + var now = DateTime.UtcNow; + var diff = now - dt; + + if (diff.TotalSeconds < 60) + { + return "刚刚"; + } + else if (diff.TotalMinutes < 60) + { + return $"{(int)diff.TotalMinutes}分钟前"; + } + else if (diff.TotalHours < 24) + { + return $"{(int)diff.TotalHours}小时前"; + } + else if (diff.TotalDays < 2) + { + return "昨天"; + } + else if (diff.TotalDays < 7) + { + return $"{(int)diff.TotalDays}天前"; + } + else if (diff.TotalDays < 30) + { + return $"{(int)(diff.TotalDays / 7)}周前"; + } + else if (diff.TotalDays < 365) + { + return $"{(int)(diff.TotalDays / 30)}个月前"; + } + else + { + return $"{(int)(diff.TotalDays / 365)}年前"; + } + } + + /// + /// 获取剩余时间的友好显示 + /// + /// 目标时间戳 + /// 友好显示 + public static string FriendlyRemaining(long timestamp) + { + var dt = FromTimestamp(timestamp); + var now = DateTime.UtcNow; + var diff = dt - now; + + if (diff.TotalSeconds <= 0) + { + return "已过期"; + } + else if (diff.TotalSeconds < 60) + { + return $"{(int)diff.TotalSeconds}秒后"; + } + else if (diff.TotalMinutes < 60) + { + return $"{(int)diff.TotalMinutes}分钟后"; + } + else if (diff.TotalHours < 24) + { + return $"{(int)diff.TotalHours}小时后"; + } + else if (diff.TotalDays < 7) + { + return $"{(int)diff.TotalDays}天后"; + } + else if (diff.TotalDays < 30) + { + return $"{(int)(diff.TotalDays / 7)}周后"; + } + else if (diff.TotalDays < 365) + { + return $"{(int)(diff.TotalDays / 30)}个月后"; + } + else + { + return $"{(int)(diff.TotalDays / 365)}年后"; + } + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/TwofishUtil.cs b/EasyTool.Core/CodeCategory/TwofishUtil.cs new file mode 100644 index 0000000..0dcfb5c --- /dev/null +++ b/EasyTool.Core/CodeCategory/TwofishUtil.cs @@ -0,0 +1,308 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// Twofish 对称加密工具类 + /// Twofish 是 AES 的最终候选算法之一,由 Bruce Schneier 团队设计 + /// 128位分组密码,支持128/192/256位密钥 + /// + public static class TwofishUtil + { + private const int BlockSize = 16; // 128位 + private const int Rounds = 16; + + // MDS 矩阵常量 + private static readonly byte[] MDS_GF_FDBK = new byte[] { 0x69, 0x23, 0x5D, 0x53 }; + private static readonly byte[,] MDS = new byte[,] + { + { 0x01, 0xEF, 0x5B, 0x5B }, + { 0x5B, 0xEF, 0xEF, 0x01 }, + { 0xEF, 0x5B, 0x01, 0xEF }, + { 0xEF, 0x01, 0xEF, 0x5B } + }; + + // Q-box 常量 + private static readonly byte[] Q0 = new byte[] + { + 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80,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x5A, + 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, + 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, + 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, + 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0 + }; + + private static readonly byte[] Q1 = new byte[] + { + 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, + 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, + 0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, + 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, + 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, + 0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, + 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, + 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, + 0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, + 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, + 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, + 0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, + 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, + 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, + 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, + 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91 + }; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + // 填充到块大小的倍数 + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + uint[] subkeys = GenerateSubkeys(key); + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + byte[] result = new byte[cipherText.Length]; + uint[] subkeys = GenerateSubkeys(key); + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static uint[] GenerateSubkeys(byte[] key) + { + int k = key.Length / 8; // 密钥中的64位块数 + uint[] subkeys = new uint[40]; + + // 简化的密钥扩展 + for (int i = 0; i < 40; i++) + { + uint val = 0; + for (int j = 0; j < 4; j++) + { + int idx = (i * 4 + j) % key.Length; + val |= (uint)(key[idx] << (j * 8)); + } + + // 应用 Q-box 和 MDS + val = (uint)(Q0[val & 0xFF] ^ Q1[(val >> 8) & 0xFF] ^ + Q0[(val >> 16) & 0xFF] ^ Q1[(val >> 24) & 0xFF]); + + subkeys[i] = val; + } + + return subkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + // 读取输入块 + uint x0 = BitConverter.ToUInt32(input, inOffset); + uint x1 = BitConverter.ToUInt32(input, inOffset + 4); + uint x2 = BitConverter.ToUInt32(input, inOffset + 8); + uint x3 = BitConverter.ToUInt32(input, inOffset + 12); + + // 输入白化 + x0 ^= subkeys[0]; + x1 ^= subkeys[1]; + x2 ^= subkeys[2]; + x3 ^= subkeys[3]; + + // 16轮加密 + for (int i = 0; i < Rounds; i++) + { + uint t0 = F(x0, subkeys[4 + 2 * i]); + uint t1 = F(RotateLeft(x1, 8), subkeys[5 + 2 * i]); + + x2 ^= RotateLeft(t0, 1); + x2 = RotateRight(x2, 1); + x3 = RotateLeft(x3, 1); + x3 ^= RotateLeft(t1, 2); + + // 交换 + if (i < Rounds - 1) + { + uint tmp = x0; + x0 = x2; + x2 = tmp; + tmp = x1; + x1 = x3; + x3 = tmp; + } + } + + // 输出白化 + x2 ^= subkeys[38]; + x3 ^= subkeys[39]; + x0 ^= subkeys[36]; + x1 ^= subkeys[37]; + + // 写入输出 + BitConverter.GetBytes(x2).CopyTo(output, outOffset); + BitConverter.GetBytes(x3).CopyTo(output, outOffset + 4); + BitConverter.GetBytes(x0).CopyTo(output, outOffset + 8); + BitConverter.GetBytes(x1).CopyTo(output, outOffset + 12); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint x0 = BitConverter.ToUInt32(input, inOffset); + uint x1 = BitConverter.ToUInt32(input, inOffset + 4); + uint x2 = BitConverter.ToUInt32(input, inOffset + 8); + uint x3 = BitConverter.ToUInt32(input, inOffset + 12); + + // 输入白化(逆) + x0 ^= subkeys[38]; + x1 ^= subkeys[39]; + x2 ^= subkeys[36]; + x3 ^= subkeys[37]; + + // 16轮解密(逆序) + for (int i = Rounds - 1; i >= 0; i--) + { + uint t0 = F(x0, subkeys[4 + 2 * i]); + uint t1 = F(RotateLeft(x1, 8), subkeys[5 + 2 * i]); + + x2 ^= RotateLeft(t0, 1); + x2 = RotateRight(x2, 1); + x3 = RotateLeft(x3, 1); + x3 ^= RotateLeft(t1, 2); + + // 交换 + if (i > 0) + { + uint tmp = x0; + x0 = x2; + x2 = tmp; + tmp = x1; + x1 = x3; + x3 = tmp; + } + } + + // 输出白化(逆) + x2 ^= subkeys[0]; + x3 ^= subkeys[1]; + x0 ^= subkeys[2]; + x1 ^= subkeys[3]; + + BitConverter.GetBytes(x2).CopyTo(output, outOffset); + BitConverter.GetBytes(x3).CopyTo(output, outOffset + 4); + BitConverter.GetBytes(x0).CopyTo(output, outOffset + 8); + BitConverter.GetBytes(x1).CopyTo(output, outOffset + 12); + } + + private static uint F(uint x, uint k) + { + x ^= k; + byte b0 = (byte)(x & 0xFF); + byte b1 = (byte)((x >> 8) & 0xFF); + byte b2 = (byte)((x >> 16) & 0xFF); + byte b3 = (byte)((x >> 24) & 0xFF); + + return (uint)(Q0[b0] | (Q1[b1] << 8) | (Q0[b2] << 16) | (Q1[b3] << 24)); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/TypeIDUtil.cs b/EasyTool.Core/CodeCategory/TypeIDUtil.cs new file mode 100644 index 0000000..bd28f30 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TypeIDUtil.cs @@ -0,0 +1,315 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// TypeID 工具类 + /// TypeID 是一种类型化的唯一标识符,将类型前缀与 UUIDv7 结合 + /// 格式:{prefix}_{base32-encoded-uuidv7} + /// 例如:user_01ARZ3NDEKTSV4RRFFQ69G5FAV + /// + public static class TypeIDUtil + { + private const string Base32Chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + /// + /// 生成带类型前缀的 TypeID + /// + /// 类型前缀(小写字母,1-63字符) + /// TypeID 字符串 + public static string Generate(string prefix) + { + ValidatePrefix(prefix); + byte[] uuid = GenerateUUIDv7(); + string encoded = EncodeBase32(uuid); + return $"{prefix}_{encoded}"; + } + + /// + /// 生成不带前缀的 TypeID(仅 UUIDv7 的 Base32 编码) + /// + /// TypeID 字符串 + public static string Generate() + { + byte[] uuid = GenerateUUIDv7(); + return EncodeBase32(uuid); + } + + /// + /// 从 TypeID 提取前缀 + /// + /// TypeID 字符串 + /// 类型前缀 + public static string ExtractPrefix(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + throw new ArgumentException("TypeID cannot be empty", nameof(typeId)); + + int separatorIndex = typeId.IndexOf('_'); + if (separatorIndex < 0) + return string.Empty; + + return typeId.Substring(0, separatorIndex); + } + + /// + /// 从 TypeID 提取 UUID 字节数组 + /// + /// TypeID 字符串 + /// 16字节 UUID + public static byte[] ExtractUUID(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + throw new ArgumentException("TypeID cannot be empty", nameof(typeId)); + + int separatorIndex = typeId.IndexOf('_'); + string encoded = separatorIndex >= 0 ? typeId.Substring(separatorIndex + 1) : typeId; + + return DecodeBase32(encoded); + } + + /// + /// 从 TypeID 提取时间戳 + /// + /// TypeID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string typeId) + { + byte[] uuid = ExtractUUID(typeId); + + // 提取 48 位时间戳(前 6 字节) + long unixMs = ((long)uuid[0] << 40) | ((long)uuid[1] << 32) | + ((long)uuid[2] << 24) | ((long)uuid[3] << 16) | + ((long)uuid[4] << 8) | uuid[5]; + + return UnixEpoch.AddMilliseconds(unixMs); + } + + /// + /// 验证 TypeID 格式是否有效 + /// + /// TypeID 字符串 + /// 是否有效 + public static bool IsValid(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + return false; + + int separatorIndex = typeId.IndexOf('_'); + + if (separatorIndex < 0) + { + // 无前缀,仅检查 Base32 编码 + return IsValidBase32(typeId) && typeId.Length == 26; + } + + string prefix = typeId.Substring(0, separatorIndex); + string encoded = typeId.Substring(separatorIndex + 1); + + return IsValidPrefix(prefix) && IsValidBase32(encoded) && encoded.Length == 26; + } + + /// + /// 解析 TypeID + /// + /// TypeID 字符串 + /// 前缀和 UUID + public static (string Prefix, byte[] UUID) Parse(string typeId) + { + if (!IsValid(typeId)) + throw new ArgumentException("Invalid TypeID format", nameof(typeId)); + + string prefix = ExtractPrefix(typeId); + byte[] uuid = ExtractUUID(typeId); + + return (prefix, uuid); + } + + /// + /// 从 UUID 和前缀创建 TypeID + /// + /// 类型前缀 + /// 16字节 UUID + /// TypeID 字符串 + public static string FromUUID(string prefix, byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + if (!string.IsNullOrEmpty(prefix)) + { + ValidatePrefix(prefix); + return $"{prefix}_{EncodeBase32(uuid)}"; + } + + return EncodeBase32(uuid); + } + + #region 私有方法 + + private static byte[] GenerateUUIDv7() + { + byte[] uuid = new byte[16]; + using var rng = RandomNumberGenerator.Create(); + + long unixMs = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + + // 48位时间戳 + uuid[0] = (byte)(unixMs >> 40); + uuid[1] = (byte)(unixMs >> 32); + uuid[2] = (byte)(unixMs >> 24); + uuid[3] = (byte)(unixMs >> 16); + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + + // 随机部分 + rng.GetBytes(uuid, 6, 10); + + // 设置版本 (7) 和变体 + uuid[6] = (byte)((uuid[6] & 0x0F) | 0x70); + uuid[8] = (byte)((uuid[8] & 0x3F) | 0x80); + + return uuid; + } + + private static string EncodeBase32(byte[] data) + { + var result = new StringBuilder(26); + + // 将 16 字节转换为 26 个 Base32 字符 + // 128 位 = 26 * 5 位 - 2 位(最后 2 位忽略) + ulong high = ((ulong)data[0] << 56) | ((ulong)data[1] << 48) | + ((ulong)data[2] << 40) | ((ulong)data[3] << 32) | + ((ulong)data[4] << 24) | ((ulong)data[5] << 16) | + ((ulong)data[6] << 8) | data[7]; + + ulong low = ((ulong)data[8] << 56) | ((ulong)data[9] << 48) | + ((ulong)data[10] << 40) | ((ulong)data[11] << 32) | + ((ulong)data[12] << 24) | ((ulong)data[13] << 16) | + ((ulong)data[14] << 8) | data[15]; + + result.Append(Base32Chars[(int)((high >> 59) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 54) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 49) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 44) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 39) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 34) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 29) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 24) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 19) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 14) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 9) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 4) & 0x1F)]); + result.Append(Base32Chars[(int)(((high << 1) | (low >> 63)) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 58) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 53) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 48) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 43) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 38) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 33) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 28) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 23) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 18) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 13) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 8) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 3) & 0x1F)]); + result.Append(Base32Chars[(int)((low << 2) & 0x1F)]); + + return result.ToString(); + } + + private static byte[] DecodeBase32(string encoded) + { + if (encoded.Length != 26) + throw new ArgumentException("Encoded string must be 26 characters", nameof(encoded)); + + // 构建解码映射 + var decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base32Chars.Length; i++) + { + decodeMap[Base32Chars[i]] = i; + decodeMap[char.ToLowerInvariant(Base32Chars[i])] = i; + } + + // 解码每个字符 + int[] values = new int[26]; + for (int i = 0; i < 26; i++) + { + char c = encoded[i]; + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid Base32 character: {c}", nameof(encoded)); + values[i] = decodeMap[c]; + } + + // 重组为 16 字节 + byte[] result = new byte[16]; + + // 使用 BigInteger 进行重组 + System.Numerics.BigInteger bigInt = 0; + for (int i = 0; i < 26; i++) + { + bigInt = (bigInt << 5) | values[i]; + } + + byte[] bytes = bigInt.ToByteArray(); + int copyLength = Math.Min(bytes.Length, 16); + int startIdx = bytes.Length > 16 ? bytes.Length - 16 : 0; + + for (int i = 0; i < copyLength; i++) + { + result[16 - copyLength + i] = bytes[startIdx + i]; + } + + return result; + } + + private static void ValidatePrefix(string prefix) + { + if (string.IsNullOrEmpty(prefix)) + throw new ArgumentException("Prefix cannot be empty", nameof(prefix)); + + if (prefix.Length > 63) + throw new ArgumentException("Prefix must be at most 63 characters", nameof(prefix)); + + foreach (char c in prefix) + { + if (c < 'a' || c > 'z') + throw new ArgumentException("Prefix must contain only lowercase letters", nameof(prefix)); + } + } + + private static bool IsValidPrefix(string prefix) + { + if (string.IsNullOrEmpty(prefix) || prefix.Length > 63) + return false; + + foreach (char c in prefix) + { + if (c < 'a' || c > 'z') + return false; + } + + return true; + } + + private static bool IsValidBase32(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + foreach (char c in encoded) + { + if (!Base32Chars.Contains(c) && !Base32Chars.Contains(char.ToUpperInvariant(c))) + return false; + } + + return true; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/UUEncodeUtil.cs b/EasyTool.Core/CodeCategory/UUEncodeUtil.cs new file mode 100644 index 0000000..b7ff021 --- /dev/null +++ b/EasyTool.Core/CodeCategory/UUEncodeUtil.cs @@ -0,0 +1,209 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// UUEncode 编码工具类 + /// UUEncode 是一种将二进制数据编码为 ASCII 文本的编码方式 + /// 早期用于 Unix 系统之间的文件传输 + /// + public static class UUEncodeUtil + { + /// + /// 将字节数组编码为 UUEncode 格式 + /// + /// 要编码的数据 + /// 文件名(可选) + /// 文件权限(默认 644) + /// UUEncode 编码字符串 + public static string Encode(byte[] data, string fileName = null, int mode = 644) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + // 写入头行 + result.AppendLine($"begin {mode} {fileName ?? "file.bin"}"); + + int offset = 0; + while (offset < data.Length) + { + int lineLength = Math.Min(45, data.Length - offset); + EncodeLine(data, offset, lineLength, result); + offset += lineLength; + } + + // 写入结束行 + result.AppendLine("`"); + result.AppendLine("end"); + + return result.ToString(); + } + + /// + /// 将 UUEncode 字符串解码为字节数组 + /// + /// UUEncode 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var lines = encoded.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + var result = new System.Collections.Generic.List(); + bool inData = false; + + foreach (string line in lines) + { + if (line.StartsWith("begin ")) + { + inData = true; + continue; + } + + if (line == "`" || line == "end") + { + inData = false; + continue; + } + + if (inData && line.Length > 0) + { + DecodeLine(line, result); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 UUEncode(使用 UTF-8) + /// + public static string EncodeString(string text, string fileName = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + return Encode(data, fileName); + } + + /// + /// 将 UUEncode 字符串解码为文本(使用 UTF-8) + /// + public static string DecodeToString(string encoded) + { + byte[] data = Decode(encoded); + return data.Length > 0 ? Encoding.UTF8.GetString(data) : string.Empty; + } + + /// + /// 验证 UUEncode 字符串是否有效 + /// + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + var lines = encoded.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + + bool foundBegin = false; + bool foundEnd = false; + + foreach (string line in lines) + { + if (line.StartsWith("begin ")) + { + foundBegin = true; + continue; + } + + if (line == "end") + { + foundEnd = true; + break; + } + + if (foundBegin && line == "`") + { + continue; + } + + if (foundBegin && line.Length > 0) + { + char lengthChar = line[0]; + if (lengthChar < ' ' || lengthChar > 'M') + return false; + } + } + + return foundBegin && foundEnd; + } + + private static void EncodeLine(byte[] data, int offset, int length, StringBuilder result) + { + // 行长度字符 + result.Append((char)(' ' + length)); + + int i = 0; + while (i < length) + { + byte b0 = data[offset + i]; + byte b1 = (i + 1 < length) ? data[offset + i + 1] : (byte)0; + byte b2 = (i + 2 < length) ? data[offset + i + 2] : (byte)0; + + result.Append((char)(' ' + ((b0 >> 2) & 0x3F))); + result.Append((char)(' ' + (((b0 << 4) | (b1 >> 4)) & 0x3F))); + result.Append((char)(' ' + (((b1 << 2) | (b2 >> 6)) & 0x3F))); + result.Append((char)(' ' + (b2 & 0x3F))); + + i += 3; + } + + result.AppendLine(); + } + + private static void DecodeLine(string line, System.Collections.Generic.List result) + { + if (string.IsNullOrEmpty(line) || line[0] == '`') + return; + + int length = line[0] - ' '; + if (length < 0 || length > 45) + return; + + int decodedLength = 0; + + for (int i = 1; i < line.Length && decodedLength < length; i += 4) + { + byte c0 = (byte)((i < line.Length ? line[i] : ' ') - ' '); + byte c1 = (byte)((i + 1 < line.Length ? line[i + 1] : ' ') - ' '); + byte c2 = (byte)((i + 2 < line.Length ? line[i + 2] : ' ') - ' '); + byte c3 = (byte)((i + 3 < line.Length ? line[i + 3] : ' ') - ' '); + + byte b0 = (byte)((c0 << 2) | (c1 >> 4)); + byte b1 = (byte)((c1 << 4) | (c2 >> 2)); + byte b2 = (byte)((c2 << 6) | c3); + + if (decodedLength < length) + { + result.Add(b0); + decodedLength++; + } + if (decodedLength < length) + { + result.Add(b1); + decodedLength++; + } + if (decodedLength < length) + { + result.Add(b2); + decodedLength++; + } + } + } + } +} diff --git a/EasyTool.Core/CodeCategory/UUIDv7Util.cs b/EasyTool.Core/CodeCategory/UUIDv7Util.cs new file mode 100644 index 0000000..2cf48f7 --- /dev/null +++ b/EasyTool.Core/CodeCategory/UUIDv7Util.cs @@ -0,0 +1,293 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// UUID v7 工具类 + /// UUID v7 是一种时间排序的 UUID,使用 Unix 时间戳(毫秒) + /// 格式:48位时间戳 + 4位版本 + 12位随机 + 2位变体 + 62位随机 + /// 兼容 RFC 9562 标准 + /// + public static class UUIDv7Util + { + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly object _lock = new object(); + private static long _lastTimestamp = -1; + private static byte[] _lastRandom = new byte[8]; + private static int _sequence = 0; + + /// + /// 生成 UUID v7 + /// + /// UUID 字节数组(16字节) + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 UUID v7 + /// + /// 时间戳 + /// UUID 字节数组(16字节) + public static byte[] Generate(DateTimeOffset timestamp) + { + long unixMs = (long)(timestamp.ToUniversalTime() - UnixEpoch).TotalMilliseconds; + + byte[] uuid = new byte[16]; + using var rng = RandomNumberGenerator.Create(); + + lock (_lock) + { + // 48位时间戳(大端序) + uuid[0] = (byte)(unixMs >> 40); + uuid[1] = (byte)(unixMs >> 32); + uuid[2] = (byte)(unixMs >> 24); + uuid[3] = (byte)(unixMs >> 16); + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + + if (unixMs == _lastTimestamp) + { + // 同一毫秒内递增序列 + _sequence++; + if (_sequence > 0xFFF) + { + // 序列溢出,等待下一毫秒 + unixMs = WaitForNextMs(unixMs); + _sequence = 0; + + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + } + + // 使用递增的序列 + uuid[6] = (byte)((0x70 | ((_sequence >> 8) & 0x0F))); // 版本 7 + uuid[7] = (byte)_sequence; + } + else + { + _sequence = 0; + _lastTimestamp = unixMs; + + // 新的随机部分 + rng.GetBytes(_lastRandom); + + uuid[6] = (byte)((_lastRandom[0] & 0x0F) | 0x70); // 版本 7 + uuid[7] = _lastRandom[1]; + } + + // 随机部分(62位) + rng.GetBytes(uuid, 8, 8); + + // 设置变体(10xx) + uuid[8] = (byte)((uuid[8] & 0x3F) | 0x80); + } + + return uuid; + } + + /// + /// 生成 UUID v7 字符串 + /// + /// 36字符的 UUID 字符串 + public static string GenerateString() + { + byte[] uuid = Generate(); + return Format(uuid); + } + + /// + /// 生成不带连字符的 UUID v7 字符串 + /// + /// 32字符的 UUID 字符串 + public static string GenerateStringNoHyphens() + { + byte[] uuid = Generate(); + return BitConverter.ToString(uuid).Replace("-", "").ToLower(); + } + + /// + /// 批量生成 UUID v7 + /// + /// 数量 + /// UUID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 从 UUID v7 提取时间戳 + /// + /// UUID 字节数组或字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + // 提取 48 位时间戳 + long unixMs = ((long)uuid[0] << 40) | ((long)uuid[1] << 32) | + ((long)uuid[2] << 24) | ((long)uuid[3] << 16) | + ((long)uuid[4] << 8) | uuid[5]; + + return UnixEpoch.AddMilliseconds(unixMs); + } + + /// + /// 从 UUID v7 字符串提取时间戳 + /// + /// UUID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string uuid) + { + byte[] bytes = Parse(uuid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 UUID v7 是否有效 + /// + /// UUID 字符串 + /// 是否有效 + public static bool IsValid(string uuid) + { + if (string.IsNullOrEmpty(uuid)) + return false; + + // 移除连字符 + string clean = uuid.Replace("-", ""); + if (clean.Length != 32) + return false; + + // 检查字符 + foreach (char c in clean) + { + if (!Uri.IsHexDigit(c)) + return false; + } + + // 检查版本 + byte version = Convert.ToByte(clean.Substring(12, 2), 16); + if ((version & 0xF0) != 0x70) + return false; + + // 检查变体 + byte variant = Convert.ToByte(clean.Substring(16, 2), 16); + if ((variant & 0xC0) != 0x80) + return false; + + return true; + } + + /// + /// 验证 UUID v7 字节数组是否有效 + /// + /// UUID 字节数组 + /// 是否有效 + public static bool IsValid(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + return false; + + // 检查版本(必须是 7) + if ((uuid[6] & 0xF0) != 0x70) + return false; + + // 检查变体(必须是 10xx) + if ((uuid[8] & 0xC0) != 0x80) + return false; + + return true; + } + + /// + /// 解析 UUID 字符串为字节数组 + /// + /// UUID 字符串 + /// 16字节数组 + public static byte[] Parse(string uuid) + { + if (string.IsNullOrEmpty(uuid)) + throw new ArgumentException("UUID cannot be empty", nameof(uuid)); + + string clean = uuid.Replace("-", ""); + if (clean.Length != 32) + throw new ArgumentException("Invalid UUID format", nameof(uuid)); + + byte[] bytes = new byte[16]; + for (int i = 0; i < 16; i++) + { + bytes[i] = Convert.ToByte(clean.Substring(i * 2, 2), 16); + } + + return bytes; + } + + /// + /// 格式化 UUID 字节数组为字符串 + /// + /// UUID 字节数组 + /// 36字符的 UUID 字符串 + public static string Format(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + return $"{uuid[0]:x2}{uuid[1]:x2}{uuid[2]:x2}{uuid[3]:x2}-" + + $"{uuid[4]:x2}{uuid[5]:x2}-" + + $"{uuid[6]:x2}{uuid[7]:x2}-" + + $"{uuid[8]:x2}{uuid[9]:x2}-" + + $"{uuid[10]:x2}{uuid[11]:x2}{uuid[12]:x2}{uuid[13]:x2}{uuid[14]:x2}{uuid[15]:x2}"; + } + + /// + /// 比较 UUID v7 的时间顺序 + /// + /// 第一个 UUID + /// 第二个 UUID + /// -1: uuid1早于uuid2, 0: 相同, 1: uuid1晚于uuid2 + public static int Compare(string uuid1, string uuid2) + { + byte[] bytes1 = Parse(uuid1); + byte[] bytes2 = Parse(uuid2); + + // 比较前 6 字节(时间戳) + for (int i = 0; i < 6; i++) + { + if (bytes1[i] < bytes2[i]) return -1; + if (bytes1[i] > bytes2[i]) return 1; + } + + // 比较序列部分 + for (int i = 6; i < 16; i++) + { + if (bytes1[i] < bytes2[i]) return -1; + if (bytes1[i] > bytes2[i]) return 1; + } + + return 0; + } + + private static long WaitForNextMs(long lastTimestamp) + { + long timestamp = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + while (timestamp <= lastTimestamp) + { + timestamp = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + } + return timestamp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/UlidUtil.cs b/EasyTool.Core/CodeCategory/UlidUtil.cs new file mode 100644 index 0000000..62f28aa --- /dev/null +++ b/EasyTool.Core/CodeCategory/UlidUtil.cs @@ -0,0 +1,362 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ULID(Universally Unique Lexicographically Sortable Identifier)工具类 + /// ULID 是一个48位时间戳 + 80位随机数的128位唯一标识符 + /// 特点:时间排序、128位兼容UUID、URL安全、大小写不敏感 + /// + public static class UlidUtil + { + // ULID 时间戳起始时间(1970-01-01) + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + // Base32 编码字符集(Crockford's Base32) + private const string EncodingChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + + // Base32 解码字符映射 + private static readonly int[] DecodingMap = BuildDecodingMap(); + + /// + /// 生成新的 ULID + /// + /// 26个字符的 ULID 字符串 + public static string Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 基于指定时间生成 ULID + /// + /// 时间戳 + /// 26个字符的 ULID 字符串 + public static string Generate(DateTimeOffset timestamp) + { + var bytes = new byte[16]; + WriteTimestamp(bytes, timestamp.ToUniversalTime().ToUnixTimeMilliseconds()); + WriteRandomness(bytes, 6); + return Encode(bytes); + } + + /// + /// 生成 ULID 字节数组 + /// + /// 16字节的 ULID + public static byte[] GenerateBytes() + { + return GenerateBytes(DateTimeOffset.UtcNow); + } + + /// + /// 基于指定时间生成 ULID 字节数组 + /// + /// 时间戳 + /// 16字节的 ULID + public static byte[] GenerateBytes(DateTimeOffset timestamp) + { + var bytes = new byte[16]; + WriteTimestamp(bytes, timestamp.ToUniversalTime().ToUnixTimeMilliseconds()); + WriteRandomness(bytes, 6); + return bytes; + } + + /// + /// 生成 GUID 格式的 ULID + /// + /// GUID + public static Guid GenerateGuid() + { + var bytes = GenerateBytes(); + return new Guid(bytes); + } + + /// + /// 将 ULID 字符串转换为字节数组 + /// + /// ULID 字符串 + /// 16字节的数组 + public static byte[] Decode(string ulid) + { + if (string.IsNullOrEmpty(ulid)) + throw new ArgumentException("ULID cannot be null or empty", nameof(ulid)); + if (ulid.Length != 26) + throw new ArgumentException("ULID must be exactly 26 characters", nameof(ulid)); + + return DecodeImpl(ulid); + } + + /// + /// 将字节数组编码为 ULID 字符串 + /// + /// 16字节的数组 + /// 26个字符的 ULID 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != 16) + throw new ArgumentException("Bytes must be exactly 16 bytes", nameof(bytes)); + + return EncodeImpl(bytes); + } + + /// + /// 从 ULID 字符串提取时间戳 + /// + /// ULID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string ulid) + { + var bytes = Decode(ulid); + return ExtractTimestamp(bytes); + } + + /// < /// 从 ULID 字节数组提取时间戳 + ///
+ /// 16字节的 ULID + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] bytes) + { + if (bytes == null || bytes.Length != 16) + throw new ArgumentException("Bytes must be exactly 16 bytes", nameof(bytes)); + + long timestamp = ((long)bytes[0] << 40) | + ((long)bytes[1] << 32) | + ((long)bytes[2] << 24) | + ((long)bytes[3] << 16) | + ((long)bytes[4] << 8) | + bytes[5]; + + return DateTimeOffset.FromUnixTimeMilliseconds(timestamp); + } + + /// + /// 验证 ULID 字符串是否有效 + /// + /// ULID 字符串 + /// 是否有效 + public static bool IsValid(string ulid) + { + if (string.IsNullOrEmpty(ulid) || ulid.Length != 26) + return false; + + foreach (char c in ulid) + { + if (c < '0' || c > 'Z') + return false; + if (c > '9' && c < 'A') + return false; + if (c == 'I' || c == 'L' || c == 'O' || c == 'U') + return false; + } + + return true; + } + + /// + /// 尝试解析 ULID 字符串 + /// + /// ULID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string ulid, out byte[] bytes) + { + bytes = null; + if (!IsValid(ulid)) + return false; + + try + { + bytes = DecodeImpl(ulid); + return true; + } + catch + { + return false; + } + } + + /// + /// 比较两个 ULID 的时间顺序 + /// + /// 第一个 ULID + /// 第二个 ULID + /// 比较结果:-1表示ulid1早于ulid2,0表示相同,1表示ulid1晚于ulid2 + public static int Compare(string ulid1, string ulid2) + { + return string.Compare(ulid1, ulid2, StringComparison.Ordinal); + } + + /// + /// 生成指定时间范围内的随机 ULID + /// + /// 最小时间 + /// 最大时间 + /// ULID 字符串 + public static string GenerateInRange(DateTimeOffset minTimestamp, DateTimeOffset maxTimestamp) + { + if (minTimestamp > maxTimestamp) + throw new ArgumentException("Min timestamp must be less than or equal to max timestamp"); + +#if NET6_0_OR_GREATER + var random = Random.Shared; +#else + var random = new Random(Guid.NewGuid().GetHashCode()); +#endif + long minMs = minTimestamp.ToUniversalTime().ToUnixTimeMilliseconds(); + long maxMs = maxTimestamp.ToUniversalTime().ToUnixTimeMilliseconds(); + long randomMs = minMs + (long)(random.NextDouble() * (maxMs - minMs)); + + var bytes = new byte[16]; + WriteTimestamp(bytes, randomMs); + WriteRandomness(bytes, 6); + return Encode(bytes); + } + + /// + /// 批量生成 ULID + /// + /// 生成数量 + /// ULID 数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 将 ULID 转换为小写 + /// + /// ULID 字符串 + /// 小写的 ULID + public static string ToLower(string ulid) + { + return ulid?.ToLowerInvariant(); + } + + /// + /// 将 ULID 转换为大写 + /// + /// ULID 字符串 + /// 大写的 ULID + public static string ToUpper(string ulid) + { + return ulid?.ToUpperInvariant(); + } + + #region 私有方法 + + private static int[] BuildDecodingMap() + { + var map = new int[256]; + for (int i = 0; i < 256; i++) + { + map[i] = -1; + } + for (int i = 0; i < EncodingChars.Length; i++) + { + map[(byte)EncodingChars[i]] = i; + map[(byte)char.ToLowerInvariant(EncodingChars[i])] = i; + } + // 处理可能出现的歧义字符 + map[(byte)'i'] = 1; map[(byte)'I'] = 1; + map[(byte)'l'] = 1; map[(byte)'L'] = 1; + map[(byte)'o'] = 0; map[(byte)'O'] = 0; + map[(byte)'u'] = 32; map[(byte)'U'] = 32; + return map; + } + + private static void WriteTimestamp(byte[] bytes, long timestamp) + { + bytes[0] = (byte)(timestamp >> 40); + bytes[1] = (byte)(timestamp >> 32); + bytes[2] = (byte)(timestamp >> 24); + bytes[3] = (byte)(timestamp >> 16); + bytes[4] = (byte)(timestamp >> 8); + bytes[5] = (byte)timestamp; + } + + private static void WriteRandomness(byte[] bytes, int offset) + { + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(bytes, offset, 10); + } + } + + private static string EncodeImpl(byte[] bytes) + { + var result = new char[26]; + + // 编码时间戳(6字节 -> 10字符) + result[0] = EncodingChars[(bytes[0] >> 5) & 0x07]; + result[1] = EncodingChars[bytes[0] & 0x1F]; + result[2] = EncodingChars[(bytes[1] >> 3) & 0x1F]; + result[3] = EncodingChars[((bytes[1] << 2) | (bytes[2] >> 6)) & 0x1F]; + result[4] = EncodingChars[(bytes[2] >> 1) & 0x1F]; + result[5] = EncodingChars[((bytes[2] << 4) | (bytes[3] >> 4)) & 0x1F]; + result[6] = EncodingChars[((bytes[3] << 1) | (bytes[4] >> 7)) & 0x1F]; + result[7] = EncodingChars[(bytes[4] >> 2) & 0x1F]; + result[8] = EncodingChars[((bytes[4] << 3) | (bytes[5] >> 5)) & 0x1F]; + result[9] = EncodingChars[bytes[5] & 0x1F]; + + // 编码随机数(10字节 -> 16字符) + result[10] = EncodingChars[(bytes[6] >> 3) & 0x1F]; + result[11] = EncodingChars[((bytes[6] << 2) | (bytes[7] >> 6)) & 0x1F]; + result[12] = EncodingChars[(bytes[7] >> 1) & 0x1F]; + result[13] = EncodingChars[((bytes[7] << 4) | (bytes[8] >> 4)) & 0x1F]; + result[14] = EncodingChars[((bytes[8] << 1) | (bytes[9] >> 7)) & 0x1F]; + result[15] = EncodingChars[(bytes[9] >> 2) & 0x1F]; + result[16] = EncodingChars[((bytes[9] << 3) | (bytes[10] >> 5)) & 0x1F]; + result[17] = EncodingChars[bytes[10] & 0x1F]; + result[18] = EncodingChars[(bytes[11] >> 3) & 0x1F]; + result[19] = EncodingChars[((bytes[11] << 2) | (bytes[12] >> 6)) & 0x1F]; + result[20] = EncodingChars[(bytes[12] >> 1) & 0x1F]; + result[21] = EncodingChars[((bytes[12] << 4) | (bytes[13] >> 4)) & 0x1F]; + result[22] = EncodingChars[((bytes[13] << 1) | (bytes[14] >> 7)) & 0x1F]; + result[23] = EncodingChars[(bytes[14] >> 2) & 0x1F]; + result[24] = EncodingChars[((bytes[14] << 3) | (bytes[15] >> 5)) & 0x1F]; + result[25] = EncodingChars[bytes[15] & 0x1F]; + + return new string(result); + } + + private static byte[] DecodeImpl(string ulid) + { + var bytes = new byte[16]; + + // 解码时间戳(10字符 -> 6字节) + bytes[0] = (byte)((DecodingMap[ulid[0]] << 5) | DecodingMap[ulid[1]]); + bytes[1] = (byte)((DecodingMap[ulid[2]] << 3) | (DecodingMap[ulid[3]] >> 2)); + bytes[2] = (byte)((DecodingMap[ulid[3]] << 6) | (DecodingMap[ulid[4]] << 1) | (DecodingMap[ulid[5]] >> 4)); + bytes[3] = (byte)((DecodingMap[ulid[5]] << 4) | (DecodingMap[ulid[6]] >> 1)); + bytes[4] = (byte)((DecodingMap[ulid[6]] << 7) | (DecodingMap[ulid[7]] << 2) | (DecodingMap[ulid[8]] >> 3)); + bytes[5] = (byte)((DecodingMap[ulid[8]] << 5) | DecodingMap[ulid[9]]); + + // 解码随机数(16字符 -> 10字节) + bytes[6] = (byte)((DecodingMap[ulid[10]] << 3) | (DecodingMap[ulid[11]] >> 2)); + bytes[7] = (byte)((DecodingMap[ulid[11]] << 6) | (DecodingMap[ulid[12]] << 1) | (DecodingMap[ulid[13]] >> 4)); + bytes[8] = (byte)((DecodingMap[ulid[13]] << 4) | (DecodingMap[ulid[14]] >> 1)); + bytes[9] = (byte)((DecodingMap[ulid[14]] << 7) | (DecodingMap[ulid[15]] << 2) | (DecodingMap[ulid[16]] >> 3)); + bytes[10] = (byte)((DecodingMap[ulid[16]] << 5) | DecodingMap[ulid[17]]); + bytes[11] = (byte)((DecodingMap[ulid[18]] << 3) | (DecodingMap[ulid[19]] >> 2)); + bytes[12] = (byte)((DecodingMap[ulid[19]] << 6) | (DecodingMap[ulid[20]] << 1) | (DecodingMap[ulid[21]] >> 4)); + bytes[13] = (byte)((DecodingMap[ulid[21]] << 4) | (DecodingMap[ulid[22]] >> 1)); + bytes[14] = (byte)((DecodingMap[ulid[22]] << 7) | (DecodingMap[ulid[23]] << 2) | (DecodingMap[ulid[24]] >> 3)); + bytes[15] = (byte)((DecodingMap[ulid[24]] << 5) | DecodingMap[ulid[25]]); + + return bytes; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/VerhoeffUtil.cs b/EasyTool.Core/CodeCategory/VerhoeffUtil.cs new file mode 100644 index 0000000..3493aa8 --- /dev/null +++ b/EasyTool.Core/CodeCategory/VerhoeffUtil.cs @@ -0,0 +1,290 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Verhoeff 算法校验和工具类 + /// Verhoeff 算法是一种检测单个数字错误的校验和算法 + /// 由 Dutch mathematician Jacobus Verhoeff 发明 + /// 能检测所有单个数字错误和所有相邻数字交换错误 + /// 使用二面体群 D5 + /// + public static class VerhoeffUtil + { + // 乘法表(二面体群 D5) + private static readonly int[,] MultiplicationTable = new int[,] + { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}, + {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, + {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}, + {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}, + {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, + {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, + {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, + {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + }; + + // 置换表 + private static readonly int[,] PermutationTable = new int[,] + { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, + {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, + {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, + {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, + {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, + {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, + {7, 0, 4, 6, 9, 1, 3, 2, 5, 8} + }; + + // 逆元表(用于查找校验位) + private static readonly int[] InverseTable = new int[] { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 }; + + /// + /// 计算数字字符串的 Verhoeff 校验位 + /// + /// 数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + return CalculateCheckDigit(GetDigits(number)); + } + + /// + /// 计算数字数组的 Verhoeff 校验位 + /// + /// 数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be empty", nameof(digits)); + + int checksum = 0; + int length = digits.Length; + + for (int i = 0; i < length; i++) + { + int digit = digits[length - 1 - i]; + if (digit < 0 || digit > 9) + throw new ArgumentException($"Invalid digit: {digit}", nameof(digits)); + + int permIndex = (i + 1) % 8; + int permutedDigit = PermutationTable[permIndex, digit]; + checksum = MultiplicationTable[checksum, permutedDigit]; + } + + return InverseTable[checksum]; + } + + /// + /// 生成带校验位的数字字符串 + /// + /// 原始数字字符串 + /// 带校验位的数字字符串 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 验证带校验位的数字字符串是否有效 + /// + /// 带校验位的数字字符串 + /// 是否有效 + public static bool Validate(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return false; + + return Validate(GetDigits(numberWithCheckDigit)); + } + + /// + /// 验证带校验位的数字数组是否有效 + /// + /// 带校验位的数字数组 + /// 是否有效 + public static bool Validate(int[] digitsWithCheckDigit) + { + if (digitsWithCheckDigit == null || digitsWithCheckDigit.Length < 2) + return false; + + int checksum = 0; + int length = digitsWithCheckDigit.Length; + + for (int i = 0; i < length; i++) + { + int digit = digitsWithCheckDigit[length - 1 - i]; + if (digit < 0 || digit > 9) + return false; + + int permIndex = i % 8; + int permutedDigit = PermutationTable[permIndex, digit]; + checksum = MultiplicationTable[checksum, permutedDigit]; + } + + return checksum == 0; + } + + /// + /// 从带校验位的字符串中提取原始数字 + /// + /// 带校验位的数字字符串 + /// 原始数字字符串,如果无效则返回 null + public static string ExtractNumber(string numberWithCheckDigit) + { + if (!Validate(numberWithCheckDigit)) + return null; + + return numberWithCheckDigit.Substring(0, numberWithCheckDigit.Length - 1); + } + + /// + /// 获取校验位 + /// + /// 带校验位的数字字符串 + /// 校验位,如果格式无效则返回 -1 + public static int GetCheckDigit(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return -1; + + if (!int.TryParse(numberWithCheckDigit[numberWithCheckDigit.Length - 1].ToString(), out int digit)) + return -1; + + return digit; + } + + /// + /// 生成随机数字序列并添加校验位 + /// + /// 数字序列长度(不含校验位) + /// 带校验位的随机数字字符串 + public static string GenerateRandom(int length) + { + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + var random = new Random(); + var digits = new int[length]; + + for (int i = 0; i < length; i++) + { + digits[i] = random.Next(10); + } + + int checkDigit = CalculateCheckDigit(digits); + + var result = new System.Text.StringBuilder(length + 1); + foreach (int digit in digits) + { + result.Append(digit); + } + result.Append(checkDigit); + + return result.ToString(); + } + + /// + /// 批量验证多个数字字符串 + /// + /// 数字字符串数组 + /// 验证结果数组 + public static bool[] ValidateBatch(string[] numbers) + { + if (numbers == null) + throw new ArgumentNullException(nameof(numbers)); + + var results = new bool[numbers.Length]; + for (int i = 0; i < numbers.Length; i++) + { + results[i] = Validate(numbers[i]); + } + return results; + } + + /// + /// 检测并纠正单个数字错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrect(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试纠正每个位置的错误 + for (int pos = 0; pos < numberWithCheckDigit.Length; pos++) + { + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + corrected[pos] = (char)('0' + newDigit); + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + } + + return null; + } + + /// + /// 检测相邻数字交换错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrectTransposition(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 3) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试交换相邻数字 + for (int i = 0; i < numberWithCheckDigit.Length - 1; i++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + char temp = corrected[i]; + corrected[i] = corrected[i + 1]; + corrected[i + 1] = temp; + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + + return null; + } + + private static int[] GetDigits(string number) + { + var digits = new int[number.Length]; + for (int i = 0; i < number.Length; i++) + { + if (!char.IsDigit(number[i])) + throw new ArgumentException($"Invalid character: {number[i]}", nameof(number)); + + digits[i] = number[i] - '0'; + } + return digits; + } + } +} diff --git a/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs b/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs new file mode 100644 index 0000000..72d3e68 --- /dev/null +++ b/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs @@ -0,0 +1,108 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 维吉尼亚密码工具类 + /// 维吉尼亚密码是一种多表替换密码 + /// 使用关键词进行加密,比凯撒密码更安全 + /// + public static class VigenereCipherUtil + { + /// + /// 使用维吉尼亚密码加密 + /// + /// 明文 + /// 密钥 + /// 密文 + public static string Encrypt(string text, string key) + { + return Process(text, key, true); + } + + /// + /// 使用维吉尼亚密码解密 + /// + /// 密文 + /// 密钥 + /// 明文 + public static string Decrypt(string text, string key) + { + return Process(text, key, false); + } + + private static string Process(string text, string key, bool encrypt) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + // 准备密钥(只保留字母) + var cleanKey = new StringBuilder(); + foreach (char c in key) + { + if (char.IsLetter(c)) + cleanKey.Append(char.ToUpperInvariant(c)); + } + + if (cleanKey.Length == 0) + throw new ArgumentException("Key must contain at least one letter", nameof(key)); + + var result = new StringBuilder(text.Length); + int keyIndex = 0; + + foreach (char c in text) + { + if (char.IsLetter(c)) + { + char baseChar = char.IsUpper(c) ? 'A' : 'a'; + int textValue = c - baseChar; + int keyValue = cleanKey[keyIndex % cleanKey.Length] - 'A'; + + int resultValue; + if (encrypt) + { + resultValue = (textValue + keyValue) % 26; + } + else + { + resultValue = (textValue - keyValue + 26) % 26; + } + + result.Append((char)(baseChar + resultValue)); + keyIndex++; + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度 + /// 随机密钥 + public static string GenerateKey(int length) + { + if (length < 1) + throw new ArgumentException("Key length must be at least 1", nameof(length)); + + var random = new Random(); + var key = new StringBuilder(length); + + for (int i = 0; i < length; i++) + { + key.Append((char)('A' + random.Next(26))); + } + + return key.ToString(); + } + } +} diff --git a/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs b/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs new file mode 100644 index 0000000..6bd65cd --- /dev/null +++ b/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs @@ -0,0 +1,347 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Whirlpool 哈希工具类 + /// Whirlpool 是一种 512 位加密哈希函数 + /// 由 Vincent Rijmen(AES 共同设计者)和 Paulo S. L. M. Barreto 设计 + /// 被 ISO/IEC 10118-3 标准采纳 + /// + public static class WhirlpoolUtil + { + private static readonly ulong[] C = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292, 0x15c8b35a11a3a085, 0x38de11c0b9d4e859, + 0xae96d0d8a14f9f56, 0x7e42927360e92d49, 0x89b38c2355b7cb40, 0x6b19c2786b1a6f45, + 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, + 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, + 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, + 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, 0x9b1c8c6bbfb21a4d, + 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, + 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, + 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, + 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, + 0x9b1c8c6bbfb21a4d, 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, + 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, + 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, + 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8 + }; + + private const int DigestSize = 64; + private const int BlockSize = 64; + private const int Rounds = 10; + + /// + /// 计算 Whirlpool 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + data = Array.Empty(); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算 Whirlpool 哈希值 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64字节哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + var hasher = new WhirlpoolHasher(); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 Whirlpool 哈希值 + /// + /// 文本 + /// 64字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 Whirlpool 哈希的十六进制表示 + /// + /// 输入数据 + /// 128字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 计算字符串的 Whirlpool 哈希十六进制表示 + /// + /// 文本 + /// 128字符的十六进制字符串 + public static string ComputeStringHex(string text) + { + byte[] hash = ComputeString(text); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 创建 Whirlpool 哈希器(用于流式处理) + /// + /// 哈希器实例 + public static WhirlpoolHasher CreateHasher() + { + return new WhirlpoolHasher(); + } + } + + /// + /// Whirlpool 哈希器 + /// + public class WhirlpoolHasher + { + private const int DigestSize = 64; + private const int BlockSize = 64; + private const int Rounds = 10; + + private ulong[] hash = new ulong[8]; + private byte[] buffer = new byte[BlockSize]; + private int bufferLength; + private ulong totalBits; + + private static readonly ulong[] C = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292, 0x15c8b35a11a3a085, 0x38de11c0b9d4e859, + 0xae96d0d8a14f9f56, 0x7e42927360e92d49, 0x89b38c2355b7cb40, 0x6b19c2786b1a6f45, + 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, + 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, + 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, + 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, 0x9b1c8c6bbfb21a4d, + 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, + 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, + 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, + 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, + 0x9b1c8c6bbfb21a4d, 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, + 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, + 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, + 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8 + }; + + private static readonly ulong[] RC = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292 + }; + + public WhirlpoolHasher() + { + Array.Clear(hash, 0, 8); + bufferLength = 0; + totalBits = 0; + } + + /// + /// 更新哈希器数据 + /// + /// 输入数据 + /// 偏移 + /// 长度 + public void Update(byte[] data, int offset, int length) + { + if (data == null || length == 0) + return; + + totalBits += (ulong)length * 8; + + int pos = 0; + + if (bufferLength > 0) + { + int copy = Math.Min(BlockSize - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == BlockSize) + { + ProcessBlock(buffer, 0); + bufferLength = 0; + } + } + + while (pos + BlockSize <= length) + { + ProcessBlock(data, offset + pos); + pos += BlockSize; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + /// + /// 完成哈希计算 + /// + /// 64字节哈希值 + public byte[] Final() + { + // 填充 + buffer[bufferLength++] = 0x80; + + if (bufferLength > 32) + { + while (bufferLength < BlockSize) + buffer[bufferLength++] = 0; + ProcessBlock(buffer, 0); + bufferLength = 0; + } + + while (bufferLength < 32) + buffer[bufferLength++] = 0; + + // 添加长度 + for (int i = 0; i < 8; i++) + { + buffer[56 + i] = (byte)(totalBits >> (56 - i * 8)); + } + + ProcessBlock(buffer, 0); + + // 输出 + byte[] result = new byte[DigestSize]; + for (int i = 0; i < 8; i++) + { + byte[] bytes = BitConverter.GetBytes(hash[i]); + if (BitConverter.IsLittleEndian) + Array.Reverse(bytes); + Array.Copy(bytes, 0, result, i * 8, 8); + } + + return result; + } + + private void ProcessBlock(byte[] block, int offset) + { + ulong[] K = new ulong[8]; + ulong[] L = new ulong[8]; + ulong[] M = new ulong[8]; + ulong[] state = new ulong[8]; + + // 读取块 + for (int i = 0; i < 8; i++) + { + K[i] = hash[i]; + state[i] = ReadUInt64(block, offset + i * 8); + M[i] = state[i]; + } + + // 初始变换 + for (int i = 0; i < 8; i++) + { + state[i] ^= K[i]; + } + + // 轮函数 + for (int r = 0; r < Rounds; r++) + { + // 计算 L + for (int i = 0; i < 8; i++) + { + L[i] = 0; + for (int j = 0; j < 8; j++) + { + L[i] ^= Multiply(C[(r * 8 + i) % 64], K[j]); + } + } + + // 更新 K + Array.Copy(L, K, 8); + K[0] ^= RC[r]; + + // 计算 state + for (int i = 0; i < 8; i++) + { + L[i] = 0; + for (int j = 0; j < 8; j++) + { + L[i] ^= Multiply(C[(r * 8 + i) % 64], state[j]); + } + } + + Array.Copy(L, state, 8); + + for (int i = 0; i < 8; i++) + { + state[i] ^= K[i]; + } + } + + // 更新哈希 + for (int i = 0; i < 8; i++) + { + hash[i] ^= state[i] ^ M[i]; + } + } + + private static ulong ReadUInt64(byte[] data, int offset) + { + return ((ulong)data[offset] << 56) | + ((ulong)data[offset + 1] << 48) | + ((ulong)data[offset + 2] << 40) | + ((ulong)data[offset + 3] << 32) | + ((ulong)data[offset + 4] << 24) | + ((ulong)data[offset + 5] << 16) | + ((ulong)data[offset + 6] << 8) | + data[offset + 7]; + } + + private static ulong Multiply(ulong a, ulong b) + { + ulong result = 0; + ulong hi = 0x0100000000000000; // x^63 的模约简 + + for (int i = 0; i < 64; i++) + { + if ((b & 1) != 0) + result ^= a; + + bool carry = (a & 0x8000000000000000) != 0; + a <<= 1; + + if (carry) + a ^= hi; + + b >>= 1; + } + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/XidUtil.cs b/EasyTool.Core/CodeCategory/XidUtil.cs new file mode 100644 index 0000000..de1117f --- /dev/null +++ b/EasyTool.Core/CodeCategory/XidUtil.cs @@ -0,0 +1,324 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// XID 全局唯一ID工具类 + /// XID 是一个全局唯一、时间排序的ID生成器,与MongoDB ObjectId兼容 + /// 格式:4字节时间戳 + 3字节机器ID + 2字节进程ID + 3字节计数器 = 12字节 + /// + public static class XidUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly byte[] MachineId; + private static readonly ushort ProcessId; + private static int _counter; + private static readonly object _lock = new object(); + + // Base32 编码字符集(小写) + private const string Base32Chars = "0123456789abcdefghijklmnopqrstuv"; + + static XidUtil() + { + // 生成机器ID(3字节) + MachineId = new byte[3]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(MachineId); + + // 获取进程ID + ProcessId = (ushort)Environment.CurrentManagedThreadId; + + // 初始化计数器 + var counterBytes = new byte[3]; + rng.GetBytes(counterBytes); + _counter = (counterBytes[0] << 16) | (counterBytes[1] << 8) | counterBytes[2]; + } + + /// + /// 生成新的 XID + /// + /// 12字节的 XID + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 XID + /// + /// 时间戳 + /// 12字节的 XID + public static byte[] Generate(DateTimeOffset timestamp) + { + var bytes = new byte[12]; + + // 4字节时间戳(大端序) + uint time = (uint)(timestamp.ToUnixTimeSeconds()); + bytes[0] = (byte)(time >> 24); + bytes[1] = (byte)(time >> 16); + bytes[2] = (byte)(time >> 8); + bytes[3] = (byte)time; + + // 3字节机器ID + bytes[4] = MachineId[0]; + bytes[5] = MachineId[1]; + bytes[6] = MachineId[2]; + + // 2字节进程ID(大端序) + bytes[7] = (byte)(ProcessId >> 8); + bytes[8] = (byte)ProcessId; + + // 3字节计数器(大端序) + int counter; + lock (_lock) + { + counter = _counter++; + } + + bytes[9] = (byte)(counter >> 16); + bytes[10] = (byte)(counter >> 8); + bytes[11] = (byte)counter; + + return bytes; + } + + /// + /// 生成 XID 字符串(20个字符的 Base32 编码) + /// + /// 20字符的 XID 字符串 + public static string GenerateString() + { + byte[] bytes = Generate(); + return Encode(bytes); + } + + /// + /// 生成指定时间的 XID 字符串 + /// + /// 时间戳 + /// 20字符的 XID 字符串 + public static string GenerateString(DateTimeOffset timestamp) + { + byte[] bytes = Generate(timestamp); + return Encode(bytes); + } + + /// + /// 将 XID 编码为字符串 + /// + /// 12字节的 XID + /// 20字符的 Base32 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != 12) + throw new ArgumentException("XID must be 12 bytes", nameof(bytes)); + + // 使用自定义 Base32 编码 + char[] result = new char[20]; + + ulong value = ((ulong)bytes[0] << 32) | ((ulong)bytes[1] << 24) | + ((ulong)bytes[2] << 16) | ((ulong)bytes[3] << 8) | bytes[4]; + + result[0] = Base32Chars[(int)((value >> 35) & 0x1f)]; + result[1] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[2] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[3] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[4] = Base32Chars[(int)((value >> 15) & 0x1f)]; + result[5] = Base32Chars[(int)((value >> 10) & 0x1f)]; + result[6] = Base32Chars[(int)((value >> 5) & 0x1f)]; + result[7] = Base32Chars[(int)(value & 0x1f)]; + + value = ((ulong)bytes[5] << 36) | ((ulong)bytes[6] << 28) | + ((ulong)bytes[7] << 20) | ((ulong)bytes[8] << 12) | + ((ulong)bytes[9] << 4) | ((ulong)bytes[10] >> 4); + + result[8] = Base32Chars[(int)((value >> 35) & 0x1f)]; + result[9] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[10] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[11] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[12] = Base32Chars[(int)((value >> 15) & 0x1f)]; + result[13] = Base32Chars[(int)((value >> 10) & 0x1f)]; + result[14] = Base32Chars[(int)((value >> 5) & 0x1f)]; + result[15] = Base32Chars[(int)(value & 0x1f)]; + + value = ((ulong)(bytes[10] & 0x0f) << 32) | ((ulong)bytes[11] << 24); + + result[16] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[17] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[18] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[19] = Base32Chars[(int)((value >> 15) & 0x1f)]; + + return new string(result); + } + + /// + /// 将 XID 字符串解码为字节数组 + /// + /// 20字符的 XID 字符串 + /// 12字节的 XID + public static byte[] Decode(string xid) + { + if (string.IsNullOrEmpty(xid) || xid.Length != 20) + throw new ArgumentException("XID string must be 20 characters", nameof(xid)); + + byte[] result = new byte[12]; + + // 构建 Base32 解码映射 + int[] decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base32Chars.Length; i++) + { + decodeMap[Base32Chars[i]] = i; + decodeMap[char.ToUpperInvariant(Base32Chars[i])] = i; + } + + // 解码 + int DecodeChar(char c) + { + int v = c < 128 ? decodeMap[c] : -1; + if (v < 0) throw new ArgumentException($"Invalid character: {c}"); + return v; + } + + int v0 = DecodeChar(xid[0]); + int v1 = DecodeChar(xid[1]); + int v2 = DecodeChar(xid[2]); + int v3 = DecodeChar(xid[3]); + int v4 = DecodeChar(xid[4]); + int v5 = DecodeChar(xid[5]); + int v6 = DecodeChar(xid[6]); + int v7 = DecodeChar(xid[7]); + + result[0] = (byte)((v0 << 3) | (v1 >> 2)); + result[1] = (byte)((v1 << 6) | (v2 << 1) | (v3 >> 4)); + result[2] = (byte)((v3 << 4) | (v4 >> 1)); + result[3] = (byte)((v4 << 7) | (v5 << 2) | (v6 >> 3)); + result[4] = (byte)((v6 << 5) | v7); + + int v8 = DecodeChar(xid[8]); + int v9 = DecodeChar(xid[9]); + int v10 = DecodeChar(xid[10]); + int v11 = DecodeChar(xid[11]); + int v12 = DecodeChar(xid[12]); + int v13 = DecodeChar(xid[13]); + int v14 = DecodeChar(xid[14]); + int v15 = DecodeChar(xid[15]); + + result[5] = (byte)((v8 << 3) | (v9 >> 2)); + result[6] = (byte)((v9 << 6) | (v10 << 1) | (v11 >> 4)); + result[7] = (byte)((v11 << 4) | (v12 >> 1)); + result[8] = (byte)((v12 << 7) | (v13 << 2) | (v14 >> 3)); + result[9] = (byte)((v14 << 5) | v15); + + int v16 = DecodeChar(xid[16]); + int v17 = DecodeChar(xid[17]); + int v18 = DecodeChar(xid[18]); + int v19 = DecodeChar(xid[19]); + + result[10] = (byte)((v16 << 3) | (v17 >> 2)); + result[11] = (byte)((v17 << 6) | (v18 << 1) | (v19 >> 4)); + + return result; + } + + /// + /// 从 XID 提取时间戳 + /// + /// XID 字节数组或字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] xid) + { + if (xid == null || xid.Length != 12) + throw new ArgumentException("XID must be 12 bytes", nameof(xid)); + + uint time = ((uint)xid[0] << 24) | ((uint)xid[1] << 16) | + ((uint)xid[2] << 8) | xid[3]; + + return Epoch.AddSeconds(time); + } + + /// + /// 从 XID 字符串提取时间戳 + /// + /// XID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string xid) + { + byte[] bytes = Decode(xid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 XID 字符串是否有效 + /// + /// XID 字符串 + /// 是否有效 + public static bool IsValid(string xid) + { + if (string.IsNullOrEmpty(xid) || xid.Length != 20) + return false; + + foreach (char c in xid) + { + if (!Base32Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + /// + /// 尝试解析 XID 字符串 + /// + /// XID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string xid, out byte[] bytes) + { + bytes = null; + if (!IsValid(xid)) + return false; + + try + { + bytes = Decode(xid); + return true; + } + catch + { + return false; + } + } + + /// + /// 批量生成 XID + /// + /// 生成数量 + /// XID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 比较 XID 的时间顺序 + /// + /// 第一个 XID + /// 第二个 XID + /// -1: xid1早于xid2, 0: 相同, 1: xid1晚于xid2 + public static int Compare(string xid1, string xid2) + { + return string.Compare(xid1, xid2, StringComparison.Ordinal); + } + } +} diff --git a/EasyTool.Core/CodeCategory/XorCipherUtil.cs b/EasyTool.Core/CodeCategory/XorCipherUtil.cs new file mode 100644 index 0000000..ce3e72b --- /dev/null +++ b/EasyTool.Core/CodeCategory/XorCipherUtil.cs @@ -0,0 +1,174 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 异或加密工具类 + /// XOR 加密是一种简单的对称加密 + /// 加密和解密使用相同的操作 + /// 注意:这不是安全的加密方式,仅用于简单混淆 + /// + public static class XorCipherUtil + { + /// + /// 使用单字节密钥进行异或加密/解密 + /// + /// 数据 + /// 单字节密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, byte key) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = (byte)(data[i] ^ key); + } + return result; + } + + /// + /// 使用字节数组密钥进行异或加密/解密 + /// + /// 数据 + /// 密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, byte[] key) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = (byte)(data[i] ^ key[i % key.Length]); + } + return result; + } + + /// + /// 使用字符串密钥进行异或加密/解密 + /// + /// 数据 + /// 字符串密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + byte[] keyBytes = Encoding.UTF8.GetBytes(key); + return Process(data, keyBytes); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// Base64 密文 + public static string EncryptToBase64(string text, string key) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] encrypted = Process(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 明文 + public static string DecryptFromBase64(string cipherText, string key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Process(data, key); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 加密字符串并返回十六进制 + /// + /// 明文 + /// 密钥 + /// 十六进制密文 + public static string EncryptToHex(string text, string key) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] encrypted = Process(data, key); + return BitConverter.ToString(encrypted).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制解密字符串 + /// + /// 十六进制密文 + /// 密钥 + /// 明文 + public static string DecryptFromHex(string cipherHex, string key) + { + if (string.IsNullOrEmpty(cipherHex)) + return string.Empty; + + byte[] data = HexToBytes(cipherHex); + byte[] decrypted = Process(data, key); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度 + /// 随机密钥 + public static byte[] GenerateKey(int length) + { + if (length < 1) + throw new ArgumentException("Key length must be at least 1", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥字符串 + /// + /// 密钥长度 + /// 随机密钥字符串 + public static string GenerateKeyString(int length) + { + byte[] key = GenerateKey(length); + return Convert.ToBase64String(key); + } + + private static byte[] HexToBytes(string hex) + { + if (hex.Length % 2 != 0) + hex = "0" + hex; + + byte[] bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return bytes; + } + } +} diff --git a/EasyTool.Core/CodeCategory/XxHashUtil.cs b/EasyTool.Core/CodeCategory/XxHashUtil.cs new file mode 100644 index 0000000..54c6563 --- /dev/null +++ b/EasyTool.Core/CodeCategory/XxHashUtil.cs @@ -0,0 +1,378 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// xxHash 超高性能哈希工具类 + /// xxHash 是一种极快的非加密哈希算法,特别适合大文件和流式数据 + /// 特点:速度极快、分布均匀、可移植性好 + /// + public static class XxHashUtil + { + #region XXHash32 + + private const uint PRIME32_1 = 2654435761U; + private const uint PRIME32_2 = 2246822519U; + private const uint PRIME32_3 = 3266489917U; + private const uint PRIME32_4 = 668265263U; + private const uint PRIME32_5 = 374761393U; + + /// + /// 计算 XXHash32 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32位哈希值 + public static uint Hash32(byte[] data, uint seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + int length = data.Length; + int index = 0; + uint h32; + + if (length >= 16) + { + uint v1 = seed + PRIME32_1 + PRIME32_2; + uint v2 = seed + PRIME32_2; + uint v3 = seed; + uint v4 = seed - PRIME32_1; + + int limit = length - 16; + do + { + v1 = Round32(v1, ReadUInt32(data, index)); + index += 4; + v2 = Round32(v2, ReadUInt32(data, index)); + index += 4; + v3 = Round32(v3, ReadUInt32(data, index)); + index += 4; + v4 = Round32(v4, ReadUInt32(data, index)); + index += 4; + } while (index <= limit); + + h32 = RotateLeft32(v1, 1) + RotateLeft32(v2, 7) + RotateLeft32(v3, 12) + RotateLeft32(v4, 18); + } + else + { + h32 = seed + PRIME32_5; + } + + h32 += (uint)length; + + // 处理剩余4字节块 + while (index <= length - 4) + { + h32 += ReadUInt32(data, index) * PRIME32_3; + h32 = RotateLeft32(h32, 17) * PRIME32_4; + index += 4; + } + + // 处理剩余单字节 + while (index < length) + { + h32 += data[index] * PRIME32_5; + h32 = RotateLeft32(h32, 11) * PRIME32_1; + index++; + } + + // 最终混合 + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; + } + + /// + /// 计算字符串的 XXHash32 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 32位哈希值 + public static uint Hash32(string text, uint seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash32(encoding.GetBytes(text), seed); + } + + private static uint Round32(uint acc, uint input) + { + acc += input * PRIME32_2; + acc = RotateLeft32(acc, 13); + acc *= PRIME32_1; + return acc; + } + + #endregion + + #region XXHash64 + + private const ulong PRIME64_1 = 11400714785074694791UL; + private const ulong PRIME64_2 = 14029467366897019727UL; + private const ulong PRIME64_3 = 1609587929392839161UL; + private const ulong PRIME64_4 = 9650029242287828579UL; + private const ulong PRIME64_5 = 2870177450012600261UL; + + /// + /// 计算 XXHash64 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 64位哈希值 + public static ulong Hash64(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + int length = data.Length; + int index = 0; + ulong h64; + + if (length >= 32) + { + ulong v1 = seed + PRIME64_1 + PRIME64_2; + ulong v2 = seed + PRIME64_2; + ulong v3 = seed; + ulong v4 = seed - PRIME64_1; + + int limit = length - 32; + do + { + v1 = Round64(v1, ReadUInt64(data, index)); + index += 8; + v2 = Round64(v2, ReadUInt64(data, index)); + index += 8; + v3 = Round64(v3, ReadUInt64(data, index)); + index += 8; + v4 = Round64(v4, ReadUInt64(data, index)); + index += 8; + } while (index <= limit); + + h64 = RotateLeft64(v1, 1) + RotateLeft64(v2, 7) + RotateLeft64(v3, 12) + RotateLeft64(v4, 18); + h64 = MergeRound64(h64, v1); + h64 = MergeRound64(h64, v2); + h64 = MergeRound64(h64, v3); + h64 = MergeRound64(h64, v4); + } + else + { + h64 = seed + PRIME64_5; + } + + h64 += (ulong)length; + + // 处理剩余8字节块 + while (index <= length - 8) + { + h64 ^= Round64(0, ReadUInt64(data, index)); + h64 = RotateLeft64(h64, 27) * PRIME64_1 + PRIME64_4; + index += 8; + } + + // 处理剩余4字节块 + if (index <= length - 4) + { + h64 ^= ReadUInt32(data, index) * PRIME64_1; + h64 = RotateLeft64(h64, 23) * PRIME64_2 + PRIME64_3; + index += 4; + } + + // 处理剩余单字节 + while (index < length) + { + h64 ^= data[index] * PRIME64_5; + h64 = RotateLeft64(h64, 11) * PRIME64_1; + index++; + } + + // 最终混合 + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; + } + + /// + /// 计算字符串的 XXHash64 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 64位哈希值 + public static ulong Hash64(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash64(encoding.GetBytes(text), seed); + } + + private static ulong Round64(ulong acc, ulong input) + { + acc += input * PRIME64_2; + acc = RotateLeft64(acc, 31); + acc *= PRIME64_1; + return acc; + } + + private static ulong MergeRound64(ulong acc, ulong val) + { + val = Round64(0, val); + acc ^= val; + acc = acc * PRIME64_1 + PRIME64_4; + return acc; + } + + #endregion + + #region XXHash128 (XXH3) + + private const ulong SECRET_DEFAULT_SIZE = 192; + private const ulong STRIPE_LEN = 64; + private const ulong SECRET_CONSUME_RATE = 8; + + /// + /// 计算 XXHash128 (XXH3) 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) Hash128(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return (0, 0); + + // 简化版 XXH3 实现 + ulong h64 = Hash64(data, seed); + ulong h64_2 = Hash64(data, seed ^ 0x5bd1e995); + + return (h64, h64_2); + } + + /// + /// 计算字符串的 XXHash128 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) Hash128(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + encoding ??= Encoding.UTF8; + return Hash128(encoding.GetBytes(text), seed); + } + + /// + /// 计算 XXHash128 哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32字符的十六进制字符串 + public static string Hash128Hex(byte[] data, ulong seed = 0) + { + var (low, high) = Hash128(data, seed); + return low.ToString("x16") + high.ToString("x16"); + } + + #endregion + + #region 辅助方法 + + private static uint ReadUInt32(byte[] data, int offset) + { + return (uint)(data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)); + } + + private static ulong ReadUInt64(byte[] data, int offset) + { + return (ulong)ReadUInt32(data, offset) | ((ulong)ReadUInt32(data, offset + 4) << 32); + } + + private static uint RotateLeft32(uint x, int r) + { + return (x << r) | (x >> (32 - r)); + } + + private static ulong RotateLeft64(ulong x, int r) + { + return (x << r) | (x >> (64 - r)); + } + + #endregion + + #region 实用方法 + + /// + /// 计算哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 位数:32 或 64(默认64) + /// 种子值 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, int bits = 64, ulong seed = 0) + { + if (data == null || data.Length == 0) + return bits == 32 ? "00000000" : "0000000000000000"; + + return bits switch + { + 32 => Hash32(data, (uint)seed).ToString("x8"), + 64 => Hash64(data, seed).ToString("x16"), + 128 => Hash128Hex(data, seed), + _ => throw new ArgumentException("Bits must be 32, 64, or 128", nameof(bits)) + }; + } + + /// + /// 验证数据哈希值 + /// + /// 数据 + /// 预期的哈希值(十六进制) + /// 种子值 + /// 是否匹配 + public static bool Verify(byte[] data, string expectedHash, ulong seed = 0) + { + if (data == null || string.IsNullOrEmpty(expectedHash)) + return false; + + int bits = expectedHash.Length * 4; + string computed = ComputeHex(data, bits, seed); + return string.Equals(computed, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 用于数据分片的哈希 + /// + /// 键值 + /// 分片数量 + /// 分片索引(0 到 shards-1) + public static int GetShard(string key, int shards) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (shards <= 0) + throw new ArgumentException("Shards must be greater than 0", nameof(shards)); + + ulong hash = Hash64(key); + return (int)(hash % (ulong)shards); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/ZstdUtil.cs b/EasyTool.Core/CodeCategory/ZstdUtil.cs new file mode 100644 index 0000000..1828a55 --- /dev/null +++ b/EasyTool.Core/CodeCategory/ZstdUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Zstandard (Zstd) 压缩工具类 + /// Zstd 是 Facebook 开发的快速压缩算法 + /// 提供了很好的压缩率和速度平衡 + /// 注意:这是一个简化实现,建议生产环境使用官方 Zstd.Net 库 + /// + public static class ZstdUtil + { + // Zstd 魔数 + private const uint MagicNumber = 0xFD2FB528; + + // 帧头标志 + private const byte FrameHeaderSizeMin = 6; + private const byte FrameHeaderSizeMax = 14; + + // 块类型 + private const byte BlockTypeRaw = 0; + private const byte BlockTypeRle = 1; + private const byte BlockTypeCompressed = 2; + private const byte BlockTypeReserved = 3; + + // 默认压缩级别 + public const int DefaultCompressionLevel = 3; + public const int MinCompressionLevel = 1; + public const int MaxCompressionLevel = 22; + + /// + /// 压缩数据 + /// + /// 原始数据 + /// 压缩级别(1-22,默认3) + /// 压缩后的数据 + public static byte[] Compress(byte[] data, int compressionLevel = DefaultCompressionLevel) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + if (compressionLevel < MinCompressionLevel || compressionLevel > MaxCompressionLevel) + throw new ArgumentException($"Compression level must be between {MinCompressionLevel} and {MaxCompressionLevel}", nameof(compressionLevel)); + + using var output = new MemoryStream(); + using var writer = new BinaryWriter(output); + + // 写入魔数 + writer.Write(MagicNumber); + + // 写入帧头 + byte frameHeaderDescriptor = 0x20; // 单段标志 + writer.Write(frameHeaderDescriptor); + + // 写入窗口描述符(可选,这里简化处理) + byte windowDescriptor = CalculateWindowDescriptor(data.Length); + writer.Write(windowDescriptor); + + // 写入字典ID(0表示无字典) + // 根据帧头描述符,这里不需要 + + // 写入内容大小(可选) + byte fcsField = (byte)(frameHeaderDescriptor >> 6); + if (fcsField == 0) + { + // 单段模式,写入原始内容大小(变长) + WriteVariableLength(writer, (ulong)data.Length); + } + + // 写入数据块 + int pos = 0; + while (pos < data.Length) + { + int blockSize = Math.Min(data.Length - pos, 128 * 1024); // 128KB 块 + bool isLast = (pos + blockSize) >= data.Length; + + WriteBlock(writer, data, pos, blockSize, isLast, compressionLevel); + pos += blockSize; + } + + // 写入内容校验(可选,这里不包含) + + return output.ToArray(); + } + + /// + /// 解压数据 + /// + /// 压缩数据 + /// 原始数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length < 4) + return Array.Empty(); + + using var input = new MemoryStream(compressed); + using var reader = new BinaryReader(input); + + // 读取并验证魔数 + uint magic = reader.ReadUInt32(); + if (magic != MagicNumber) + throw new InvalidDataException("Invalid Zstd magic number"); + + // 读取帧头描述符 + byte frameHeaderDescriptor = reader.ReadByte(); + bool singleSegment = (frameHeaderDescriptor & 0x20) != 0; + bool contentChecksumFlag = (frameHeaderDescriptor & 0x04) != 0; + bool dictionaryIdFlag = (frameHeaderDescriptor & 0x01) != 0; + byte fcsField = (byte)((frameHeaderDescriptor >> 6) & 0x03); + + // 读取窗口描述符(非单段模式) + ulong windowSize = 0; + if (!singleSegment) + { + byte windowDescriptor = reader.ReadByte(); + windowSize = CalculateWindowSize(windowDescriptor); + } + + // 读取字典ID + if (dictionaryIdFlag) + { + int dictIdSize = 1 << (frameHeaderDescriptor & 0x03); + for (int i = 0; i < dictIdSize; i++) + reader.ReadByte(); + } + + // 读取内容大小 + ulong contentSize = 0; + if (singleSegment || fcsField > 0) + { + contentSize = ReadVariableLength(reader); + if (fcsField >= 2) + { + contentSize |= (ulong)reader.ReadByte() << 8; + if (fcsField >= 3) + { + contentSize |= (ulong)reader.ReadByte() << 16; + contentSize |= (ulong)reader.ReadByte() << 24; + } + } + } + + // 读取并解压数据块 + using var output = new MemoryStream(); + bool lastBlock = false; + + while (!lastBlock) + { + // 读取块头 + uint blockHeader = reader.ReadUInt32(); + lastBlock = (blockHeader & 0x01) != 0; + int blockType = (int)((blockHeader >> 1) & 0x03); + int blockSize = (int)((blockHeader >> 3) & 0x7FFFFF); + + switch (blockType) + { + case BlockTypeRaw: + byte[] rawData = reader.ReadBytes(blockSize); + output.Write(rawData, 0, rawData.Length); + break; + + case BlockTypeRle: + byte rleByte = reader.ReadByte(); + for (int i = 0; i < blockSize; i++) + output.WriteByte(rleByte); + break; + + case BlockTypeCompressed: + byte[] compressedBlock = reader.ReadBytes(blockSize); + byte[] decompressed = DecompressBlock(compressedBlock); + output.Write(decompressed, 0, decompressed.Length); + break; + + default: + throw new InvalidDataException($"Unknown block type: {blockType}"); + } + } + + // 读取内容校验(如果有) + if (contentChecksumFlag) + { + reader.ReadUInt32(); // 跳过校验和 + } + + return output.ToArray(); + } + + /// + /// 压缩字符串 + /// + public static string CompressToBase64(string text, int compressionLevel = DefaultCompressionLevel) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data, compressionLevel); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + public static string DecompressFromBase64(string compressedBase64) + { + if (string.IsNullOrEmpty(compressedBase64)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedBase64); + byte[] data = Decompress(compressed); + return Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩绑定的最大输出大小 + /// + public static int Bound(int sourceSize) + { + if (sourceSize < 0) + throw new ArgumentException("Source size cannot be negative", nameof(sourceSize)); + + return sourceSize + (sourceSize / 255) + 16; + } + + private static void WriteBlock(BinaryWriter writer, byte[] data, int offset, int length, bool isLast, int compressionLevel) + { + // 简化实现:使用 LZ4 风格的快速压缩 + byte[] compressed = CompressBlockSimple(data, offset, length); + + if (compressed.Length >= length) + { + // 原始数据更好 + uint header = (uint)length << 3 | (uint)BlockTypeRaw << 1 | (isLast ? 1u : 0u); + writer.Write(header); + writer.Write(data, offset, length); + } + else + { + uint header = (uint)compressed.Length << 3 | (uint)BlockTypeCompressed << 1 | (isLast ? 1u : 0u); + writer.Write(header); + writer.Write(compressed); + } + } + + private static byte[] CompressBlockSimple(byte[] data, int offset, int length) + { + using var output = new MemoryStream(); + + int pos = offset; + int end = offset + length; + + while (pos < end) + { + // 查找匹配 + int bestMatch = 0; + int bestLength = 0; + + int searchStart = Math.Max(offset, pos - 8192); + for (int i = searchStart; i < pos; i++) + { + int matchLen = 0; + while (pos + matchLen < end && data[i + matchLen] == data[pos + matchLen] && matchLen < 255) + matchLen++; + + if (matchLen > bestLength) + { + bestLength = matchLen; + bestMatch = i; + } + } + + if (bestLength >= 4) + { + // 写入匹配 + int distance = pos - bestMatch; + output.WriteByte((byte)(bestLength - 4)); + output.WriteByte((byte)(distance & 0xFF)); + output.WriteByte((byte)((distance >> 8) & 0xFF)); + pos += bestLength; + } + else + { + // 写入字面量 + output.WriteByte(0xFF); // 标记为字面量 + output.WriteByte(data[pos]); + pos++; + } + } + + return output.ToArray(); + } + + private static byte[] DecompressBlock(byte[] compressed) + { + using var output = new MemoryStream(); + int pos = 0; + + while (pos < compressed.Length) + { + byte marker = compressed[pos++]; + + if (marker == 0xFF) + { + // 字面量 + if (pos < compressed.Length) + output.WriteByte(compressed[pos++]); + } + else + { + // 匹配 + int length = marker + 4; + if (pos + 1 < compressed.Length) + { + int distance = compressed[pos] | (compressed[pos + 1] << 8); + pos += 2; + + int srcPos = (int)output.Position - distance; + for (int i = 0; i < length; i++) + { + byte b = output.ToArray()[srcPos + i]; + output.WriteByte(b); + } + } + } + } + + return output.ToArray(); + } + + private static byte CalculateWindowDescriptor(int size) + { + // 计算适合的窗口大小 + int exponent = 10; // 最小 1KB + while ((1 << exponent) < size && exponent < 30) + exponent++; + + return (byte)(exponent - 10); + } + + private static ulong CalculateWindowSize(byte descriptor) + { + int exponent = (descriptor & 0x1F) + 10; + int mantissa = (descriptor >> 5) & 0x07; + + return (1ul << exponent) + (ulong)mantissa * (1ul << (exponent - 3)); + } + + private static void WriteVariableLength(BinaryWriter writer, ulong value) + { + while (value >= 128) + { + writer.Write((byte)(value | 0x80)); + value >>= 7; + } + writer.Write((byte)value); + } + + private static ulong ReadVariableLength(BinaryReader reader) + { + ulong result = 0; + int shift = 0; + byte b; + + do + { + b = reader.ReadByte(); + result |= (ulong)(b & 0x7F) << shift; + shift += 7; + } while ((b & 0x80) != 0); + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/ZucUtil.cs b/EasyTool.Core/CodeCategory/ZucUtil.cs new file mode 100644 index 0000000..5ca48fa --- /dev/null +++ b/EasyTool.Core/CodeCategory/ZucUtil.cs @@ -0,0 +1,436 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// ZUC(祖冲之)流加密工具类 + /// ZUC 是中国自主设计的流密码算法,以中国数学家祖冲之命名 + /// 用于 4G LTE 通信加密,是 3GPP 标准的一部分 + /// + public static class ZucUtil + { + // S-box + private static readonly byte[] S0 = new byte[] + { + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12 + }; + + private static readonly byte[] S1 = new byte[] + { + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e, + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e + }; + + /// + /// 使用 ZUC 加密数据 + /// + /// 明文 + /// 密钥(16字节) + /// 初始向量(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] iv) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + + using var zuc = new ZucCipher(key, iv); + return zuc.Process(plainText); + } + + /// + /// 使用 ZUC 解密数据 + /// + /// 密文 + /// 密钥(16字节) + /// 初始向量(16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv) + { + return Encrypt(cipherText, key, iv); + } + + /// + /// 生成随机密钥 + /// + /// 16字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机 IV + /// + /// 16字节 IV + public static byte[] GenerateIV() + { + byte[] iv = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(iv); + return iv; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 生成随机 IV 并返回十六进制 + /// + /// 32字符的十六进制 IV + public static string GenerateIVHex() + { + byte[] iv = GenerateIV(); + return BitConverter.ToString(iv).Replace("-", "").ToLower(); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// 初始向量 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key, byte[] iv) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key, iv); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 初始向量 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, byte[] iv) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key, iv); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成密钥流 + /// + /// 密钥 + /// 初始向量 + /// 密钥流长度(字) + /// 密钥流 + public static uint[] GenerateKeyStream(byte[] key, byte[] iv, int length) + { + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + using var zuc = new ZucCipher(key, iv); + var keyStream = new uint[length]; + for (int i = 0; i < length; i++) + { + keyStream[i] = zuc.GenerateKeyStreamWord(); + } + return keyStream; + } + + /// + /// 创建 ZUC 处理器 + /// + /// 密钥 + /// 初始向量 + /// ZUC 处理器 + public static ZucCipher CreateCipher(byte[] key, byte[] iv) + { + return new ZucCipher(key, iv); + } + } + + /// + /// ZUC 流加密器 + /// + public class ZucCipher : IDisposable + { + private readonly uint[] _lfsr = new uint[16]; + private readonly uint[] _fsm = new uint[3]; + private bool _initialized = false; + private bool _disposed = false; + + private static readonly uint[] EK = new uint[] + { + 0x44D7, 0x26BC, 0x626B, 0x135E, 0x5789, 0x35E2, 0x7135, 0x09AF, + 0x4D78, 0x2F13, 0x6BC4, 0x1AF1, 0x5E26, 0x3C4A, 0x278E, 0x03F2 + }; + + /// + /// 创建 ZUC 加密器 + /// + /// 密钥(16字节) + /// 初始向量(16字节) + public ZucCipher(byte[] key, byte[] iv) + { + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + + Initialize(key, iv); + } + + private void Initialize(byte[] key, byte[] iv) + { + // 初始化 LFSR + for (int i = 0; i < 16; i++) + { + _lfsr[i] = ((uint)key[i] << 16) | ((uint)iv[i] << 8) | EK[i]; + } + + // 初始化 FSM + _fsm[0] = 0; + _fsm[1] = 0; + _fsm[2] = 0; + + // 运行 32 轮初始化 + for (int i = 0; i < 32; i++) + { + uint z = GenerateKeyStreamWord(); + _lfsr[0] = (_lfsr[0] ^ z) & 0x7FFFFFFF; + LfsrShift(); + } + + _initialized = true; + } + + /// + /// 生成一个密钥流字(32位) + /// + /// 32位密钥流字 + public uint GenerateKeyStreamWord() + { + // F 函数 + uint fOutput = FFunction(); + + // 比特重组 + uint w = BitReorganization(); + + // LFSR 更新 + if (_initialized) + { + LfsrWithMode(); + } + else + { + LfsrShift(); + } + + return w ^ fOutput; + } + + /// + /// 处理数据 + /// + /// 输入数据 + /// 处理后的数据 + public byte[] Process(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + int processed = 0; + + while (processed < data.Length) + { + uint keyStream = GenerateKeyStreamWord(); + byte[] keyBytes = BitConverter.GetBytes(keyStream); + + for (int i = 0; i < 4 && processed < data.Length; i++) + { + result[processed] = (byte)(data[processed] ^ keyBytes[i]); + processed++; + } + } + + return result; + } + + private uint FFunction() + { + uint r1 = _fsm[0]; + uint r2 = _fsm[1]; + + // 简化的 F 函数 + uint w1 = (r1 + _lfsr[4]) & 0x7FFFFFFF; + uint w2 = (r2 ^ _lfsr[10]) & 0x7FFFFFFF; + + _fsm[0] = STransform(w1); + _fsm[1] = STransform(w2); + _fsm[2] = r1; + + return _fsm[0] ^ _fsm[1] ^ _fsm[2]; + } + + private uint BitReorganization() + { + uint x0 = _lfsr[15]; + uint x1 = _lfsr[14]; + uint x2 = _lfsr[11]; + uint x3 = _lfsr[9]; + uint x4 = _lfsr[7]; + uint x5 = _lfsr[5]; + uint x6 = _lfsr[2]; + uint x7 = _lfsr[0]; + + return ((x0 << 23) | (x1 >> 9)) ^ ((x2 << 15) | (x3 >> 17)) ^ + ((x4 << 7) | (x5 >> 25)) ^ x6 ^ x7; + } + + private void LfsrShift() + { + uint s0 = _lfsr[0]; + uint s4 = _lfsr[4]; + uint s10 = _lfsr[10]; + uint s13 = _lfsr[13]; + uint s15 = _lfsr[15]; + + // 多项式: x^31 - 1 + uint newBit = (s0 ^ s4 ^ s10 ^ s13 ^ s15) & 0x7FFFFFFF; + + for (int i = 0; i < 15; i++) + { + _lfsr[i] = _lfsr[i + 1]; + } + + _lfsr[15] = newBit; + } + + private void LfsrWithMode() + { + uint s0 = _lfsr[0]; + uint s4 = _lfsr[4]; + uint s10 = _lfsr[10]; + uint s13 = _lfsr[13]; + uint s15 = _lfsr[15]; + + uint u = (s0 ^ s4 ^ s10 ^ s13 ^ s15) & 0x7FFFFFFF; + + for (int i = 0; i < 15; i++) + { + _lfsr[i] = _lfsr[i + 1]; + } + + _lfsr[15] = u; + } + + private uint STransform(uint x) + { + byte b0 = (byte)(x & 0xFF); + byte b1 = (byte)((x >> 8) & 0xFF); + byte b2 = (byte)((x >> 16) & 0xFF); + byte b3 = (byte)((x >> 24) & 0xFF); + + b0 = SBox(b0); + b1 = SBox(b1); + b2 = SBox(b2); + b3 = SBox(b3); + + return (uint)((b3 << 24) | (b2 << 16) | (b1 << 8) | b0); + } + + private byte SBox(byte x) + { + int low = x & 0x0F; + int high = (x >> 4) & 0x0F; + return (byte)((_s0[high] << 4) | _s1[low]); + } + + // 内部使用的 S-box 副本 + private static readonly byte[] _s0 = new byte[] + { + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12 + }; + + private static readonly byte[] _s1 = new byte[] + { + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e + }; + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + Array.Clear(_lfsr, 0, _lfsr.Length); + Array.Clear(_fsm, 0, _fsm.Length); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs b/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs new file mode 100644 index 0000000..227db1e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs @@ -0,0 +1,842 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 集合展平工具类 + /// + public static class FlattenUtil + { + /// + /// 展平嵌套集合 + /// + public static IEnumerable Flatten(IEnumerable> source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + return source.SelectMany(x => x); + } + + /// + /// 递归展平 + /// + public static IEnumerable FlattenRecursive(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + foreach (var item in source) + { + if (item is IEnumerable enumerable) + { + foreach (var subItem in enumerable) + { + yield return subItem; + } + } + else if (item is T value) + { + yield return value; + } + else if (item is IEnumerable nested) + { + foreach (var subItem in FlattenRecursive(nested)) + { + yield return subItem; + } + } + } + } + + /// + /// 展平字典 + /// + public static IEnumerable> Flatten( + IEnumerable> dictionaries) + { + if (dictionaries == null) + throw new ArgumentNullException(nameof(dictionaries)); + + return dictionaries.SelectMany(d => d); + } + } + + /// + /// 集合分组工具类 + /// + public static class GroupingUtil + { + /// + /// 将连续相同的元素分组 + /// + public static IEnumerable> GroupConsecutive(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + var currentGroup = new List { enumerator.Current }; + var current = enumerator.Current; + + while (enumerator.MoveNext()) + { + if (EqualityComparer.Default.Equals(enumerator.Current, current)) + { + currentGroup.Add(enumerator.Current); + } + else + { + yield return currentGroup; + currentGroup = new List { enumerator.Current }; + current = enumerator.Current; + } + } + + yield return currentGroup; + } + + /// + /// 将连续满足条件的元素分组 + /// + public static IEnumerable> GroupConsecutive(IEnumerable source, Func belongsToSameGroup) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (belongsToSameGroup == null) + throw new ArgumentNullException(nameof(belongsToSameGroup)); + + var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + var currentGroup = new List { enumerator.Current }; + var current = enumerator.Current; + + while (enumerator.MoveNext()) + { + if (belongsToSameGroup(current, enumerator.Current)) + { + currentGroup.Add(enumerator.Current); + } + else + { + yield return currentGroup; + currentGroup = new List { enumerator.Current }; + } + current = enumerator.Current; + } + + yield return currentGroup; + } + + /// + /// 按固定大小分组 + /// + public static IEnumerable> GroupBySize(IEnumerable source, int groupSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (groupSize <= 0) + throw new ArgumentOutOfRangeException(nameof(groupSize)); + + var group = new List(groupSize); + foreach (var item in source) + { + group.Add(item); + if (group.Count == groupSize) + { + yield return group; + group = new List(groupSize); + } + } + + if (group.Count > 0) + { + yield return group; + } + } + + /// + /// 按条件分组的数量分组 + /// + public static IEnumerable> GroupWhile(IEnumerable source, Func, T, bool> shouldInclude) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (shouldInclude == null) + throw new ArgumentNullException(nameof(shouldInclude)); + + var group = new List(); + foreach (var item in source) + { + if (group.Count == 0 || shouldInclude(group, item)) + { + group.Add(item); + } + else + { + yield return group; + group = new List { item }; + } + } + + if (group.Count > 0) + { + yield return group; + } + } + } + + /// + /// 集合合并工具类 + /// + public static class MergeUtil + { + /// + /// 合并两个有序集合 + /// + public static IEnumerable MergeOrdered(IEnumerable first, IEnumerable second) where T : IComparable + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + using var enum1 = first.GetEnumerator(); + using var enum2 = second.GetEnumerator(); + + bool hasFirst = enum1.MoveNext(); + bool hasSecond = enum2.MoveNext(); + + while (hasFirst && hasSecond) + { + if (enum1.Current.CompareTo(enum2.Current) <= 0) + { + yield return enum1.Current; + hasFirst = enum1.MoveNext(); + } + else + { + yield return enum2.Current; + hasSecond = enum2.MoveNext(); + } + } + + while (hasFirst) + { + yield return enum1.Current; + hasFirst = enum1.MoveNext(); + } + + while (hasSecond) + { + yield return enum2.Current; + hasSecond = enum2.MoveNext(); + } + } + + /// + /// 合并多个有序集合 + /// + public static IEnumerable MergeOrdered(params IEnumerable[] sources) where T : IComparable + { + if (sources == null || sources.Length == 0) + yield break; + + var enumerators = sources + .Select(s => s?.GetEnumerator()) + .Where(e => e != null) + .ToList(); + + var hasMore = new bool[enumerators.Count]; + for (int i = 0; i < enumerators.Count; i++) + { + hasMore[i] = enumerators[i].MoveNext(); + } + + while (hasMore.Any(x => x)) + { + int minIndex = -1; + T minValue = default; + + for (int i = 0; i < enumerators.Count; i++) + { + if (!hasMore[i]) + continue; + + if (minIndex == -1 || enumerators[i].Current.CompareTo(minValue) < 0) + { + minIndex = i; + minValue = enumerators[i].Current; + } + } + + if (minIndex >= 0) + { + yield return minValue; + hasMore[minIndex] = enumerators[minIndex].MoveNext(); + } + } + + foreach (var e in enumerators) + { + e.Dispose(); + } + } + + /// + /// 合并字典(后者覆盖前者) + /// + public static Dictionary Merge( + IDictionary first, + IDictionary second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + var result = new Dictionary(first); + foreach (var kvp in second) + { + result[kvp.Key] = kvp.Value; + } + return result; + } + + /// + /// 合并字典(自定义冲突解决) + /// + public static Dictionary Merge( + IDictionary first, + IDictionary second, + Func conflictResolver) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + if (conflictResolver == null) + throw new ArgumentNullException(nameof(conflictResolver)); + + var result = new Dictionary(first); + foreach (var kvp in second) + { + if (result.TryGetValue(kvp.Key, out var existing)) + { + result[kvp.Key] = conflictResolver(kvp.Key, existing, kvp.Value); + } + else + { + result[kvp.Key] = kvp.Value; + } + } + return result; + } + } + + /// + /// 集合查找工具类 + /// + public static class SearchUtil + { + /// + /// 二分查找 + /// + public static int BinarySearch(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count - 1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + int cmp = list[mid].CompareTo(value); + + if (cmp == 0) + return mid; + if (cmp < 0) + left = mid + 1; + else + right = mid - 1; + } + + return ~left; // 返回插入点的补码 + } + + /// + /// 查找第一个大于等于指定值的元素索引 + /// + public static int LowerBound(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count; + + while (left < right) + { + int mid = left + (right - left) / 2; + if (list[mid].CompareTo(value) < 0) + left = mid + 1; + else + right = mid; + } + + return left; + } + + /// + /// 查找第一个大于指定值的元素索引 + /// + public static int UpperBound(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count; + + while (left < right) + { + int mid = left + (right - left) / 2; + if (list[mid].CompareTo(value) <= 0) + left = mid + 1; + else + right = mid; + } + + return left; + } + + /// + /// 查找范围内的元素数量 + /// + public static int CountInRange(IList list, T min, T max) where T : IComparable + { + return UpperBound(list, max) - LowerBound(list, min); + } + + /// + /// 查找众数(出现次数最多的元素) + /// + public static T FindMajority(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + // Boyer-Moore 多数投票算法 + T candidate = default; + int count = 0; + + foreach (var item in list) + { + if (count == 0) + { + candidate = item; + count = 1; + } + else if (EqualityComparer.Default.Equals(item, candidate)) + { + count++; + } + else + { + count--; + } + } + + // 验证 + count = list.Count(x => EqualityComparer.Default.Equals(x, candidate)); + if (count > list.Count / 2) + return candidate; + + throw new InvalidOperationException("No majority element found"); + } + + /// + /// 尝试查找众数 + /// + public static bool TryFindMajority(IEnumerable source, out T majority) + { + try + { + majority = FindMajority(source); + return true; + } + catch + { + majority = default; + return false; + } + } + } + + /// + /// 集合序列工具类 + /// + public static class SequenceUtil + { + /// + /// 生成等差数列 + /// + public static IEnumerable Range(int start, int count, int step = 1) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return start + i * step; + } + } + + /// + /// 生成等差数列(浮点数) + /// + public static IEnumerable Range(double start, int count, double step) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return start + i * step; + } + } + + /// + /// 生成重复序列 + /// + public static IEnumerable Repeat(T value, int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return value; + } + } + + /// + /// 循环生成序列 + /// + public static IEnumerable Cycle(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + int index = 0; + while (true) + { + yield return list[index]; + index = (index + 1) % list.Count; + } + } + + /// + /// 循环生成指定次数 + /// + public static IEnumerable Cycle(IEnumerable source, int count) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + for (int i = 0; i < count; i++) + { + yield return list[i % list.Count]; + } + } + + /// + /// 生成斐波那契数列 + /// + public static IEnumerable Fibonacci(int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + long a = 0, b = 1; + for (int i = 0; i < count; i++) + { + yield return a; + (a, b) = (b, a + b); + } + } + + /// + /// 生成迭代序列 + /// + public static IEnumerable Iterate(T initial, Func next, int count) + { + if (next == null) + throw new ArgumentNullException(nameof(next)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + T current = initial; + for (int i = 0; i < count; i++) + { + yield return current; + current = next(current); + } + } + } + + /// + /// 集合集合操作工具类 + /// + public static class SetOperationUtil + { + /// + /// 笛卡尔积 + /// + public static IEnumerable<(T1, T2)> CartesianProduct( + IEnumerable first, + IEnumerable second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + return from a in first + from b in second + select (a, b); + } + + /// + /// 多集合笛卡尔积 + /// + public static IEnumerable> CartesianProduct(IEnumerable> sources) + { + if (sources == null) + throw new ArgumentNullException(nameof(sources)); + + var lists = sources.Select(s => s.ToList()).ToList(); + if (lists.Count == 0) + { + yield return new List(); + yield break; + } + + var indices = new int[lists.Count]; + var counts = lists.Select(l => l.Count).ToArray(); + + if (counts.Any(c => c == 0)) + yield break; + + while (true) + { + var result = new List(); + for (int i = 0; i < lists.Count; i++) + { + result.Add(lists[i][indices[i]]); + } + yield return result; + + // 增加索引 + int j = lists.Count - 1; + while (j >= 0) + { + indices[j]++; + if (indices[j] < counts[j]) + break; + indices[j] = 0; + j--; + } + + if (j < 0) + break; + } + } + + /// + /// 幂集(所有子集) + /// + public static IEnumerable> PowerSet(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + int count = 1 << list.Count; // 2^n + + for (int i = 0; i < count; i++) + { + var subset = new List(); + for (int j = 0; j < list.Count; j++) + { + if ((i & (1 << j)) != 0) + { + subset.Add(list[j]); + } + } + yield return subset; + } + } + + /// + /// 获取指定大小的所有子集 + /// + public static IEnumerable> SubsetsOfSize(IEnumerable source, int size) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (size < 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + var list = source.ToList(); + if (size > list.Count) + yield break; + + var indices = Enumerable.Range(0, size).ToArray(); + + while (true) + { + yield return indices.Select(i => list[i]).ToList(); + + // 找到可以增加的索引 + int i = size - 1; + while (i >= 0 && indices[i] == list.Count - size + i) + i--; + + if (i < 0) + break; + + indices[i]++; + for (int j = i + 1; j < size; j++) + { + indices[j] = indices[j - 1] + 1; + } + } + } + } + + /// + /// 集合排序工具类 + /// + public static class SortingUtil + { + /// + /// 快速选择(找到第 k 小的元素) + /// + public static T QuickSelect(IList list, int k) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + if (k < 0 || k >= list.Count) + throw new ArgumentOutOfRangeException(nameof(k)); + + var arr = list.ToArray(); + return QuickSelectInternal(arr, 0, arr.Length - 1, k); + } + + private static T QuickSelectInternal(T[] arr, int left, int right, int k) where T : IComparable + { + if (left == right) + return arr[left]; + + int pivotIndex = Partition(arr, left, right); + + if (k == pivotIndex) + return arr[k]; + if (k < pivotIndex) + return QuickSelectInternal(arr, left, pivotIndex - 1, k); + return QuickSelectInternal(arr, pivotIndex + 1, right, k); + } + + private static int Partition(T[] arr, int left, int right) where T : IComparable + { + T pivot = arr[right]; + int i = left; + + for (int j = left; j < right; j++) + { + if (arr[j].CompareTo(pivot) <= 0) + { + Swap(arr, i, j); + i++; + } + } + + Swap(arr, i, right); + return i; + } + + private static void Swap(T[] arr, int i, int j) + { + T temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + /// + /// 多键排序 + /// + public static List SortByMultiple(IEnumerable source, params Func[] selectors) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (selectors == null || selectors.Length == 0) + return source.ToList(); + + var list = source.ToList(); + list.Sort((a, b) => + { + foreach (var selector in selectors) + { + int cmp = selector(a).CompareTo(selector(b)); + if (cmp != 0) + return cmp; + } + return 0; + }); + return list; + } + + /// + /// 稳定排序 + /// + public static List StableSort(IEnumerable source, Comparison comparison) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (comparison == null) + throw new ArgumentNullException(nameof(comparison)); + + var list = source.Select((x, i) => new { Value = x, Index = i }).ToList(); + list.Sort((a, b) => + { + int cmp = comparison(a.Value, b.Value); + return cmp != 0 ? cmp : a.Index.CompareTo(b.Index); + }); + return list.Select(x => x.Value).ToList(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs new file mode 100644 index 0000000..cc070ec --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs @@ -0,0 +1,607 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级布隆过滤器工具类 + /// + public static class AdvancedBloomFilterUtil + { + /// + /// 创建计数布隆过滤器 + /// + public static CountingBloomFilter CreateCounting(int capacity, double falsePositiveRate = 0.01) + { + return new CountingBloomFilter(capacity, falsePositiveRate); + } + + /// + /// 创建布谷鸟过滤器 + /// + public static CuckooFilter CreateCuckoo(int capacity, int fingerprintSize = 8) + { + return new CuckooFilter(capacity, fingerprintSize); + } + } + + /// + /// 计数布隆过滤器 + /// 支持删除操作 + /// + public class CountingBloomFilter + { + private readonly byte[] _counters; + private readonly int _hashCount; + private readonly int _size; + private readonly HashFunction[] _hashFunctions; + private int _count; + + private delegate int HashFunction(byte[] data); + + /// + /// 已添加元素数量 + /// + public int Count => _count; + + /// + /// 容量 + /// + public int Capacity { get; } + + /// + /// 位大小 + /// + public int Size => _size; + + /// + /// 哈希函数数量 + /// + public int HashCount => _hashCount; + + /// + /// 创建计数布隆过滤器 + /// + public CountingBloomFilter(int capacity, double falsePositiveRate = 0.01) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + Capacity = capacity; + + // 计算最优参数 + _size = (int)Math.Ceiling(-capacity * Math.Log(falsePositiveRate) / Math.Pow(Math.Log(2), 2)); + _hashCount = (int)Math.Ceiling(_size * Math.Log(2) / capacity); + + _counters = new byte[_size]; + _hashFunctions = new HashFunction[_hashCount]; + _count = 0; + + // 初始化哈希函数 + for (int i = 0; i < _hashCount; i++) + { + int seed = (int)(i * 0x9e3779b9); + _hashFunctions[i] = data => HashWithSeed(data, seed); + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] < byte.MaxValue) + { + _counters[index]++; + } + } + _count++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + if (!Contains(data)) + return false; + + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] > 0) + { + _counters[index]--; + } + } + _count--; + return true; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] == 0) + return false; + } + return true; + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + _count = 0; + } + + /// + /// 估计假阳率 + /// + public double EstimatedFalsePositiveRate() + { + if (_count == 0) + return 0; + + double ratio = (double)_count / Capacity; + return Math.Pow(1 - Math.Exp(-_hashCount * ratio), _hashCount); + } + + private static int HashWithSeed(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } + + /// + /// 布谷鸟过滤器 + /// 支持删除,比布隆过滤器更低的空间占用 + /// + public class CuckooFilter + { + private readonly byte[][][] _buckets; + private readonly int _bucketCount; + private readonly int _fingerprintSize; + private readonly int _maxKickOuts; + private int _count; + private readonly Random _random; + + /// + /// 已添加元素数量 + /// + public int Count => _count; + + /// + /// 容量 + /// + public int Capacity => _bucketCount; + + /// + /// 负载因子 + /// + public double LoadFactor => (double)_count / _bucketCount; + + /// + /// 创建布谷鸟过滤器 + /// + public CuckooFilter(int capacity, int fingerprintSize = 8) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + if (fingerprintSize < 1 || fingerprintSize > 16) + throw new ArgumentOutOfRangeException(nameof(fingerprintSize), "Fingerprint size must be between 1 and 16"); + + _bucketCount = capacity; + _fingerprintSize = fingerprintSize; + _maxKickOuts = 500; + _count = 0; + _random = new Random(); + + _buckets = new byte[capacity][][]; + for (int i = 0; i < capacity; i++) + { + _buckets[i] = new byte[4][]; // 每个桶4个槽 + } + } + + /// + /// 添加元素 + /// + public bool Add(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + // 尝试添加到任一桶 + if (TryAddToBucket(i1, fingerprint)) + { + _count++; + return true; + } + if (TryAddToBucket(i2, fingerprint)) + { + _count++; + return true; + } + + // 需要踢出 + int i = _random.Next(2) == 0 ? i1 : i2; + + for (int n = 0; n < _maxKickOuts; n++) + { + // 随机选择一个槽踢出 + int slot = _random.Next(4); + var oldFingerprint = _buckets[i][slot]; + _buckets[i][slot] = fingerprint; + fingerprint = oldFingerprint; + + i = AltIndex(i, fingerprint); + + if (TryAddToBucket(i, fingerprint)) + { + _count++; + return true; + } + } + + return false; + } + + /// + /// 添加字符串 + /// + public bool Add(string value) + { + return Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + if (RemoveFromBucket(i1, fingerprint) || RemoveFromBucket(i2, fingerprint)) + { + _count--; + return true; + } + + return false; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + return BucketContains(i1, fingerprint) || BucketContains(i2, fingerprint); + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + for (int i = 0; i < _bucketCount; i++) + { + for (int j = 0; j < 4; j++) + { + _buckets[i][j] = null; + } + } + _count = 0; + } + + private byte[] ComputeFingerprint(byte[] data) + { + unchecked + { + int hash = 17; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + + var fingerprint = new byte[_fingerprintSize]; + for (int i = 0; i < _fingerprintSize; i++) + { + fingerprint[i] = (byte)((hash >> (i * 8)) & 0xFF); + } + + // 确保不为空 + if (fingerprint.All(b => b == 0)) + { + fingerprint[0] = 1; + } + + return fingerprint; + } + } + + private int Hash1(byte[] data) + { + unchecked + { + int hash = 0; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return Math.Abs(hash) % _bucketCount; + } + } + + private int AltIndex(int index, byte[] fingerprint) + { + unchecked + { + int hash = 0; + foreach (byte b in fingerprint) + { + hash = hash * 31 + b; + } + return (index ^ hash) % _bucketCount; + } + } + + private bool TryAddToBucket(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (_buckets[index][i] == null) + { + _buckets[index][i] = fingerprint; + return true; + } + } + return false; + } + + private bool RemoveFromBucket(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (FingerprintEquals(_buckets[index][i], fingerprint)) + { + _buckets[index][i] = null; + return true; + } + } + return false; + } + + private bool BucketContains(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (FingerprintEquals(_buckets[index][i], fingerprint)) + { + return true; + } + } + return false; + } + + private bool FingerprintEquals(byte[] a, byte[] b) + { + if (a == null || b == null) + return false; + + if (a.Length != b.Length) + return false; + + for (int i = 0; i < a.Length; i++) + { + if (a[i] != b[i]) + return false; + } + + return true; + } + } + + /// + /// 可扩展布隆过滤器 + /// 当填满时自动扩展容量 + /// + public class ScalableBloomFilter + { + private readonly List _filters; + private readonly double _initialFalsePositiveRate; + private readonly double _scalingFactor; + private readonly int _initialCapacity; + private int _totalCapacity; + + /// + /// 已添加元素数量 + /// + public int Count { get; private set; } + + /// + /// 当前容量 + /// + public int Capacity => _totalCapacity; + + /// + /// 过滤器数量 + /// + public int FilterCount => _filters.Count; + + /// + /// 创建可扩展布隆过滤器 + /// + public ScalableBloomFilter(int initialCapacity = 1000, double falsePositiveRate = 0.01, double scalingFactor = 2) + { + _initialCapacity = initialCapacity; + _initialFalsePositiveRate = falsePositiveRate; + _scalingFactor = scalingFactor; + + _filters = new List(); + _totalCapacity = 0; + Count = 0; + + AddFilter(); + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + // 查找有空位的过滤器 + foreach (var filter in _filters) + { + if (filter.Count < filter.Capacity) + { + filter.Add(data); + Count++; + return; + } + } + + // 需要添加新过滤器 + AddFilter(); + _filters[_filters.Count - 1].Add(data); + Count++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + for (int i = _filters.Count - 1; i >= 0; i--) + { + if (_filters[i].Contains(data)) + { + if (_filters[i].Remove(data)) + { + Count--; + return true; + } + } + } + return false; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + return _filters.Any(f => f.Contains(data)); + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + _filters.Clear(); + _totalCapacity = 0; + Count = 0; + AddFilter(); + } + + private void AddFilter() + { + int capacity = (int)(_initialCapacity * Math.Pow(_scalingFactor, _filters.Count)); + double fpr = _initialFalsePositiveRate / Math.Pow(_scalingFactor, _filters.Count); + + var filter = new CountingBloomFilter(capacity, fpr); + _filters.Add(filter); + _totalCapacity += capacity; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs new file mode 100644 index 0000000..a3adeee --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs @@ -0,0 +1,687 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级集合工具类 + /// + public static class AdvancedCollectionsUtil + { + /// + /// 创建Roaring Bitmap + /// + public static RoaringBitmap CreateRoaringBitmap() + { + return new RoaringBitmap(); + } + + /// + /// 从整数集合创建Roaring Bitmap + /// + public static RoaringBitmap CreateRoaringBitmap(IEnumerable values) + { + var bitmap = new RoaringBitmap(); + foreach (var value in values) + { + bitmap.Add(value); + } + return bitmap; + } + + /// + /// 创建流式处理器 + /// + public static StreamProcessor CreateStreamProcessor(int windowSize) + { + return new StreamProcessor(windowSize); + } + } + + /// + /// Roaring Bitmap + /// 压缩位图,高效存储和操作整数集合 + /// + public class RoaringBitmap : IEnumerable + { + private readonly Dictionary _containers; + + private abstract class Container + { + public abstract int Count { get; } + public abstract bool Contains(ushort value); + public abstract void Add(ushort value); + public abstract void Remove(ushort value); + public abstract IEnumerator GetEnumerator(); + } + + private class ArrayContainer : Container + { + private readonly List _values; + private const int MaxSize = 4096; + + public override int Count => _values.Count; + + public ArrayContainer() + { + _values = new List(); + } + + public override bool Contains(ushort value) + { + return _values.BinarySearch(value) >= 0; + } + + public override void Add(ushort value) + { + int index = _values.BinarySearch(value); + if (index < 0) + { + _values.Insert(~index, value); + } + } + + public override void Remove(ushort value) + { + int index = _values.BinarySearch(value); + if (index >= 0) + { + _values.RemoveAt(index); + } + } + + public override IEnumerator GetEnumerator() + { + return _values.GetEnumerator(); + } + + public bool IsFull => _values.Count >= MaxSize; + + public BitmapContainer ToBitmapContainer() + { + var bitmap = new BitmapContainer(); + foreach (var value in _values) + { + bitmap.Add(value); + } + return bitmap; + } + } + + private class BitmapContainer : Container + { + private readonly ulong[] _bitmap; + private int _count; + + public override int Count => _count; + + public BitmapContainer() + { + _bitmap = new ulong[1024]; // 65536 bits / 64 = 1024 + _count = 0; + } + + public override bool Contains(ushort value) + { + int index = value / 64; + int bit = value % 64; + return (_bitmap[index] & (1UL << bit)) != 0; + } + + public override void Add(ushort value) + { + int index = value / 64; + int bit = value % 64; + if ((_bitmap[index] & (1UL << bit)) == 0) + { + _bitmap[index] |= 1UL << bit; + _count++; + } + } + + public override void Remove(ushort value) + { + int index = value / 64; + int bit = value % 64; + if ((_bitmap[index] & (1UL << bit)) != 0) + { + _bitmap[index] &= ~(1UL << bit); + _count--; + } + } + + public override IEnumerator GetEnumerator() + { + for (int i = 0; i < _bitmap.Length; i++) + { + if (_bitmap[i] == 0) + continue; + + for (int bit = 0; bit < 64; bit++) + { + if ((_bitmap[i] & (1UL << bit)) != 0) + { + yield return (ushort)(i * 64 + bit); + } + } + } + } + } + + /// + /// 元素数量 + /// + public int Count { get; private set; } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 创建Roaring Bitmap + /// + public RoaringBitmap() + { + _containers = new Dictionary(); + Count = 0; + } + + /// + /// 添加值 + /// + public void Add(int value) + { + if (value < 0) + throw new ArgumentOutOfRangeException(nameof(value), "值不能为负数"); + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + if (!_containers.TryGetValue(high, out var container)) + { + container = new ArrayContainer(); + _containers[high] = container; + } + + int oldCount = container.Count; + container.Add(low); + + if (container.Count > oldCount) + Count++; + + // 检查是否需要转换为位图容器 + if (container is ArrayContainer ac && ac.IsFull) + { + _containers[high] = ac.ToBitmapContainer(); + } + } + + /// + /// 批量添加 + /// + public void AddRange(IEnumerable values) + { + foreach (var value in values) + { + Add(value); + } + } + + /// + /// 移除值 + /// + public bool Remove(int value) + { + if (value < 0) + return false; + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + if (!_containers.TryGetValue(high, out var container)) + return false; + + int oldCount = container.Count; + container.Remove(low); + + if (container.Count < oldCount) + { + Count--; + return true; + } + + return false; + } + + /// + /// 是否包含值 + /// + public bool Contains(int value) + { + if (value < 0) + return false; + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + return _containers.TryGetValue(high, out var container) && container.Contains(low); + } + + /// + /// 与操作 + /// + public void And(RoaringBitmap other) + { + if (other == null) + return; + + var keysToRemove = new List(); + + foreach (var kvp in _containers) + { + if (!other._containers.TryGetValue(kvp.Key, out var otherContainer)) + { + keysToRemove.Add(kvp.Key); + } + else + { + // 简化实现:创建新的位图容器 + var result = new BitmapContainer(); + foreach (var value in kvp.Value) + { + if (otherContainer.Contains(value)) + { + result.Add(value); + } + } + + if (result.Count > 0) + { + _containers[kvp.Key] = result; + } + else + { + keysToRemove.Add(kvp.Key); + } + } + } + + foreach (var key in keysToRemove) + { + var container = _containers[key]; + Count -= container.Count; + _containers.Remove(key); + } + } + + /// + /// 或操作 + /// + public void Or(RoaringBitmap other) + { + if (other == null) + return; + + foreach (var kvp in other._containers) + { + if (!_containers.TryGetValue(kvp.Key, out var container)) + { + container = new BitmapContainer(); + _containers[kvp.Key] = container; + } + + foreach (var value in kvp.Value) + { + int oldCount = container.Count; + container.Add(value); + if (container.Count > oldCount) + Count++; + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + _containers.Clear(); + Count = 0; + } + + public IEnumerator GetEnumerator() + { + foreach (var kvp in _containers.OrderBy(x => x.Key)) + { + int high = kvp.Key << 16; + foreach (var low in kvp.Value) + { + yield return high | low; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + /// + /// 流式数据处理器 + /// 支持滑动窗口聚合 + /// + public class StreamProcessor + { + private readonly int _windowSize; + private readonly Queue _window; + private readonly List> _aggregators; + private long _totalCount; + + /// + /// 窗口大小 + /// + public int WindowSize => _windowSize; + + /// + /// 当前窗口内元素数量 + /// + public int WindowCount => _window.Count; + + /// + /// 总处理元素数量 + /// + public long TotalCount => _totalCount; + + /// + /// 创建流式处理器 + /// + public StreamProcessor(int windowSize) + { + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + _windowSize = windowSize; + _window = new Queue(); + _aggregators = new List>(); + _totalCount = 0; + } + + /// + /// 添加聚合器 + /// + public void AddAggregator(IAggregator aggregator) + { + if (aggregator == null) + throw new ArgumentNullException(nameof(aggregator)); + _aggregators.Add(aggregator); + } + + /// + /// 处理元素 + /// + public void Process(T item) + { + // 通知聚合器有新元素 + foreach (var aggregator in _aggregators) + { + aggregator.Add(item); + } + + _window.Enqueue(item); + _totalCount++; + + // 如果窗口满了,移除最旧的元素 + if (_window.Count > _windowSize) + { + var removed = _window.Dequeue(); + foreach (var aggregator in _aggregators) + { + aggregator.Remove(removed); + } + } + } + + /// + /// 批量处理 + /// + public void ProcessRange(IEnumerable items) + { + foreach (var item in items) + { + Process(item); + } + } + + /// + /// 获取聚合结果 + /// + public TResult GetResult(string aggregatorName) + { + var aggregator = _aggregators.FirstOrDefault(a => a.Name == aggregatorName); + if (aggregator is IAggregator typedAggregator) + { + return typedAggregator.GetResult(); + } + throw new ArgumentException($"Aggregator '{aggregatorName}' not found or has different result type"); + } + + /// + /// 获取所有聚合结果 + /// + public Dictionary GetAllResults() + { + return _aggregators.ToDictionary(a => a.Name, a => a.GetResultObject()); + } + + /// + /// 清空窗口 + /// + public void Clear() + { + _window.Clear(); + foreach (var aggregator in _aggregators) + { + aggregator.Reset(); + } + _totalCount = 0; + } + + /// + /// 获取窗口内元素 + /// + public IReadOnlyCollection GetWindow() + { + return _window.ToArray(); + } + } + + /// + /// 聚合器接口 + /// + public interface IAggregator + { + /// + /// 名称 + /// + string Name { get; } + + /// + /// 添加元素 + /// + void Add(T item); + + /// + /// 移除元素 + /// + void Remove(T item); + + /// + /// 重置 + /// + void Reset(); + + /// + /// 获取结果(对象形式) + /// + object GetResultObject(); + } + + /// + /// 聚合器接口(带结果类型) + /// + public interface IAggregator : IAggregator + { + /// + /// 获取结果 + /// + TResult GetResult(); + } + + /// + /// 计数聚合器 + /// + public class CountAggregator : IAggregator + { + private long _count; + + public string Name => "Count"; + + public void Add(T item) => _count++; + + public void Remove(T item) => _count--; + + public void Reset() => _count = 0; + + public long GetResult() => _count; + + public object GetResultObject() => GetResult(); + } + + /// + /// 求和聚合器 + /// + public class SumAggregator : IAggregator + { + private double _sum; + + public string Name => "Sum"; + + public void Add(double item) => _sum += item; + + public void Remove(double item) => _sum -= item; + + public void Reset() => _sum = 0; + + public double GetResult() => _sum; + + public object GetResultObject() => GetResult(); + } + + /// + /// 平均值聚合器 + /// + public class AverageAggregator : IAggregator + { + private double _sum; + private long _count; + + public string Name => "Average"; + + public void Add(double item) + { + _sum += item; + _count++; + } + + public void Remove(double item) + { + _sum -= item; + _count--; + } + + public void Reset() + { + _sum = 0; + _count = 0; + } + + public double GetResult() => _count > 0 ? _sum / _count : 0; + + public object GetResultObject() => GetResult(); + } + + /// + /// 最小值聚合器 + /// + public class MinAggregator : IAggregator where T : IComparable + { + private readonly List _items = new List(); + + public string Name => "Min"; + + public void Add(T item) => _items.Add(item); + + public void Remove(T item) => _items.Remove(item); + + public void Reset() => _items.Clear(); + + public T GetResult() => _items.Count > 0 ? _items.Min() : default; + + public object GetResultObject() => GetResult(); + } + + /// + /// 最大值聚合器 + /// + public class MaxAggregator : IAggregator where T : IComparable + { + private readonly List _items = new List(); + + public string Name => "Max"; + + public void Add(T item) => _items.Add(item); + + public void Remove(T item) => _items.Remove(item); + + public void Reset() => _items.Clear(); + + public T GetResult() => _items.Count > 0 ? _items.Max() : default; + + public object GetResultObject() => GetResult(); + } + + /// + /// 频率聚合器 + /// + public class FrequencyAggregator : IAggregator> + { + private readonly Dictionary _frequency = new Dictionary(); + + public string Name => "Frequency"; + + public void Add(T item) + { + if (_frequency.ContainsKey(item)) + _frequency[item]++; + else + _frequency[item] = 1; + } + + public void Remove(T item) + { + if (_frequency.ContainsKey(item)) + { + _frequency[item]--; + if (_frequency[item] == 0) + _frequency.Remove(item); + } + } + + public void Reset() => _frequency.Clear(); + + public Dictionary GetResult() => new Dictionary(_frequency); + + public object GetResultObject() => GetResult(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs new file mode 100644 index 0000000..e805394 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs @@ -0,0 +1,742 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级堆工具类 + /// + public static class AdvancedHeapUtil + { + /// + /// 创建配对堆 + /// + public static PairingHeap CreatePairing() where T : IComparable + { + return new PairingHeap(); + } + + /// + /// 创建斐波那契堆 + /// + public static FibonacciHeap CreateFibonacci() where T : IComparable + { + return new FibonacciHeap(); + } + + /// + /// 创建二项堆 + /// + public static BinomialHeap CreateBinomial() where T : IComparable + { + return new BinomialHeap(); + } + } + + /// + /// 配对堆 + /// 时间复杂度:插入O(1),删除最小O(log n)摊还,合并O(1) + /// + public class PairingHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node Child { get; set; } + public Node Sibling { get; set; } + public Node Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node _root; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + return _root.Value; + } + } + + /// + /// 创建配对堆 + /// + public PairingHeap() + { + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + _root = Merge(_root, node); + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + + T minValue = _root.Value; + _root = MergePairs(_root.Child); + _count--; + return minValue; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + return _root.Value; + } + + /// + /// 合并另一个堆 + /// + public void Merge(PairingHeap other) + { + if (other == null) + return; + + _root = Merge(_root, other._root); + _count += other._count; + other._root = null; + other._count = 0; + } + + private Node Merge(Node a, Node b) + { + if (a == null) + return b; + if (b == null) + return a; + + if (a.Value.CompareTo(b.Value) <= 0) + { + b.Sibling = a.Child; + b.Parent = a; + a.Child = b; + return a; + } + else + { + a.Sibling = b.Child; + a.Parent = b; + b.Child = a; + return b; + } + } + + private Node MergePairs(Node node) + { + if (node == null || node.Sibling == null) + return node; + + // 收集所有兄弟节点 + var nodes = new List(); + while (node != null) + { + nodes.Add(node); + node = node.Sibling; + } + + // 从左到右两两合并 + var merged = new List(); + for (int i = 0; i < nodes.Count; i += 2) + { + if (i + 1 < nodes.Count) + { + merged.Add(Merge(nodes[i], nodes[i + 1])); + } + else + { + merged.Add(nodes[i]); + } + } + + // 从右到左合并 + Node result = merged[merged.Count - 1]; + for (int i = merged.Count - 2; i >= 0; i--) + { + result = Merge(result, merged[i]); + } + + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + /// + /// 斐波那契堆 + /// 时间复杂度:插入O(1),删除最小O(log n)摊还,降低键O(1)摊还 + /// + public class FibonacciHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node Parent { get; set; } + public Node Child { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public int Degree { get; set; } + public bool Mark { get; set; } + + public Node(T value) + { + Value = value; + Left = this; + Right = this; + } + } + + private Node _min; + private int _count; + private readonly List _degreeList; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + return _min.Value; + } + } + + /// + /// 创建斐波那契堆 + /// + public FibonacciHeap() + { + _min = null; + _count = 0; + _degreeList = new List(); + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + + if (_min == null) + { + _min = node; + } + else + { + AddToRootList(node); + if (node.Value.CompareTo(_min.Value) < 0) + _min = node; + } + + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + + T minValue = _min.Value; + + // 将子节点添加到根列表 + if (_min.Child != null) + { + var child = _min.Child; + do + { + var next = child.Right; + child.Parent = null; + AddToRootList(child); + child = next; + } while (child != _min.Child); + } + + // 从根列表移除最小节点 + RemoveFromRootList(_min); + + if (_min == _min.Right) + { + _min = null; + } + else + { + _min = _min.Right; + Consolidate(); + } + + _count--; + return minValue; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + return _min.Value; + } + + /// + /// 合并另一个堆 + /// + public void Merge(FibonacciHeap other) + { + if (other == null || other._min == null) + return; + + if (_min == null) + { + _min = other._min; + } + else + { + // 连接根列表 + var thisRight = _min.Right; + var otherLeft = other._min.Left; + + _min.Right = other._min; + other._min.Left = _min; + thisRight.Left = otherLeft; + otherLeft.Right = thisRight; + + if (other._min.Value.CompareTo(_min.Value) < 0) + _min = other._min; + } + + _count += other._count; + other._min = null; + other._count = 0; + } + + private void AddToRootList(Node node) + { + if (_min == null) + { + _min = node; + node.Left = node; + node.Right = node; + } + else + { + node.Left = _min; + node.Right = _min.Right; + _min.Right.Left = node; + _min.Right = node; + } + } + + private void RemoveFromRootList(Node node) + { + node.Left.Right = node.Right; + node.Right.Left = node.Left; + } + + private void Consolidate() + { + _degreeList.Clear(); + var maxDegree = (int)Math.Floor(Math.Log(_count) / Math.Log(2)) + 1; + + for (int i = 0; i <= maxDegree; i++) + { + _degreeList.Add(null); + } + + var roots = new List(); + var current = _min; + do + { + roots.Add(current); + current = current.Right; + } while (current != _min); + + foreach (var root in roots) + { + var x = root; + int d = x.Degree; + + while (d < _degreeList.Count && _degreeList[d] != null) + { + var y = _degreeList[d]; + if (x.Value.CompareTo(y.Value) > 0) + { + var temp = x; + x = y; + y = temp; + } + + Link(y, x); + _degreeList[d] = null; + d++; + } + + if (d >= _degreeList.Count) + { + for (int i = _degreeList.Count; i <= d; i++) + _degreeList.Add(null); + } + + _degreeList[d] = x; + } + + _min = null; + foreach (var node in _degreeList) + { + if (node != null) + { + if (_min == null || node.Value.CompareTo(_min.Value) < 0) + { + _min = node; + } + } + } + } + + private void Link(Node child, Node parent) + { + RemoveFromRootList(child); + + child.Parent = parent; + child.Left = child; + child.Right = child; + + if (parent.Child == null) + { + parent.Child = child; + } + else + { + child.Left = parent.Child; + child.Right = parent.Child.Right; + parent.Child.Right.Left = child; + parent.Child.Right = child; + } + + parent.Degree++; + child.Mark = false; + } + + /// + /// 清空 + /// + public void Clear() + { + _min = null; + _count = 0; + _degreeList.Clear(); + } + } + + /// + /// 二项堆 + /// 时间复杂度:插入O(log n),删除最小O(log n),合并O(log n) + /// + public class BinomialHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public int Degree { get; set; } + public Node Child { get; set; } + public Node Sibling { get; set; } + public Node Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node _head; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_head == null) + throw new InvalidOperationException("Heap is empty"); + + var min = _head; + var current = _head.Sibling; + while (current != null) + { + if (current.Value.CompareTo(min.Value) < 0) + min = current; + current = current.Sibling; + } + return min.Value; + } + } + + /// + /// 创建二项堆 + /// + public BinomialHeap() + { + _head = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + var newHead = Union(_head, node); + _head = newHead; + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_head == null) + throw new InvalidOperationException("Heap is empty"); + + // 找到最小节点及其前驱 + Node minPrev = null; + Node min = _head; + Node prev = null; + Node current = _head; + + while (current != null) + { + if (current.Value.CompareTo(min.Value) < 0) + { + min = current; + minPrev = prev; + } + prev = current; + current = current.Sibling; + } + + // 从根列表中移除最小节点 + if (minPrev == null) + { + _head = min.Sibling; + } + else + { + minPrev.Sibling = min.Sibling; + } + + // 反转最小节点的子节点 + Node newHead = null; + var child = min.Child; + while (child != null) + { + var next = child.Sibling; + child.Sibling = newHead; + child.Parent = null; + newHead = child; + child = next; + } + + // 合并 + _head = Union(_head, newHead); + _count--; + + return min.Value; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + return Min; + } + + /// + /// 合并另一个堆 + /// + public void Merge(BinomialHeap other) + { + if (other == null) + return; + + _head = Union(_head, other._head); + _count += other._count; + other._head = null; + other._count = 0; + } + + private Node Union(Node h1, Node h2) + { + if (h1 == null) + return h2; + if (h2 == null) + return h1; + + Node head; + if (h1.Degree <= h2.Degree) + { + head = h1; + h1 = h1.Sibling; + } + else + { + head = h2; + h2 = h2.Sibling; + } + + Node tail = head; + while (h1 != null && h2 != null) + { + if (h1.Degree <= h2.Degree) + { + tail.Sibling = h1; + h1 = h1.Sibling; + } + else + { + tail.Sibling = h2; + h2 = h2.Sibling; + } + tail = tail.Sibling; + } + + tail.Sibling = h1 ?? h2; + + return Consolidate(head); + } + + private Node Consolidate(Node head) + { + if (head == null) + return null; + + Node prev = null; + Node current = head; + Node next = head.Sibling; + + while (next != null) + { + if (current.Degree != next.Degree || + (next.Sibling != null && next.Sibling.Degree == current.Degree)) + { + prev = current; + current = next; + } + else + { + if (current.Value.CompareTo(next.Value) <= 0) + { + current.Sibling = next.Sibling; + Link(next, current); + } + else + { + if (prev == null) + { + head = next; + } + else + { + prev.Sibling = next; + } + Link(current, next); + current = next; + } + } + next = current.Sibling; + } + + return head; + } + + private void Link(Node child, Node parent) + { + child.Parent = parent; + child.Sibling = parent.Child; + parent.Child = child; + parent.Degree++; + } + + /// + /// 清空 + /// + public void Clear() + { + _head = null; + _count = 0; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs new file mode 100644 index 0000000..578ad30 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs @@ -0,0 +1,758 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级搜索树工具类 + /// + public static class AdvancedSearchTreeUtil + { + /// + /// 创建树堆(Treap) + /// + public static Treap CreateTreap() where T : IComparable + { + return new Treap(); + } + + /// + /// 创建伸展树(Splay Tree) + /// + public static SplayTree CreateSplayTree() where T : IComparable + { + return new SplayTree(); + } + + /// + /// 创建后缀数组 + /// + public static SuffixArray CreateSuffixArray(string text) + { + return new SuffixArray(text); + } + } + + #region Treap(树堆) + + /// + /// 树堆(Treap) + /// 结合二叉搜索树和堆的性质,通过随机优先级保持平衡 + /// + public class Treap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public int Priority { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public int Count { get; set; } // 子树大小 + public int Size => 1 + (Left?.Size ?? 0) + (Right?.Size ?? 0); + + public Node(T value, int priority) + { + Value = value; + Priority = priority; + Count = 1; + } + } + + private Node _root; + private readonly Random _random; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建树堆 + /// + public Treap() + { + _random = new Random(); + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + _root = Insert(_root, value, _random.Next()); + _count++; + } + + private Node Insert(Node node, T value, int priority) + { + if (node == null) + return new Node(value, priority); + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Insert(node.Left, value, priority); + if (node.Left.Priority > node.Priority) + node = RotateRight(node); + } + else if (cmp > 0) + { + node.Right = Insert(node.Right, value, priority); + if (node.Right.Priority > node.Priority) + node = RotateLeft(node); + } + + return node; + } + + /// + /// 删除元素 + /// + public bool Remove(T value) + { + if (!Contains(value)) return false; + _root = Remove(_root, value); + _count--; + return true; + } + + private Node Remove(Node node, T value) + { + if (node == null) return null; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Remove(node.Left, value); + } + else if (cmp > 0) + { + node.Right = Remove(node.Right, value); + } + else + { + if (node.Left == null) return node.Right; + if (node.Right == null) return node.Left; + + if (node.Left.Priority > node.Right.Priority) + { + node = RotateRight(node); + node.Right = Remove(node.Right, value); + } + else + { + node = RotateLeft(node); + node.Left = Remove(node.Left, value); + } + } + + return node; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + return Contains(_root, value); + } + + private bool Contains(Node node, T value) + { + if (node == null) return false; + int cmp = value.CompareTo(node.Value); + if (cmp < 0) return Contains(node.Left, value); + if (cmp > 0) return Contains(node.Right, value); + return true; + } + + /// + /// 查找第k小元素 + /// + public T FindKth(int k) + { + if (k < 0 || k >= _count) + throw new ArgumentOutOfRangeException(nameof(k)); + return FindKth(_root, k); + } + + private T FindKth(Node node, int k) + { + int leftSize = node.Left?.Size ?? 0; + if (k < leftSize) + return FindKth(node.Left, k); + if (k > leftSize) + return FindKth(node.Right, k - leftSize - 1); + return node.Value; + } + + /// + /// 获取元素的排名(从0开始) + /// + public int Rank(T value) + { + return Rank(_root, value); + } + + private int Rank(Node node, T value) + { + if (node == null) return 0; + int cmp = value.CompareTo(node.Value); + int leftSize = node.Left?.Size ?? 0; + if (cmp < 0) + return Rank(node.Left, value); + if (cmp > 0) + return leftSize + 1 + Rank(node.Right, value); + return leftSize; + } + + /// + /// 获取最小值 + /// + public T Min() + { + if (_root == null) throw new InvalidOperationException("Treap is empty"); + var node = _root; + while (node.Left != null) node = node.Left; + return node.Value; + } + + /// + /// 获取最大值 + /// + public T Max() + { + if (_root == null) throw new InvalidOperationException("Treap is empty"); + var node = _root; + while (node.Right != null) node = node.Right; + return node.Value; + } + + private static Node RotateRight(Node node) + { + var left = node.Left; + node.Left = left.Right; + left.Right = node; + return left; + } + + private static Node RotateLeft(Node node) + { + var right = node.Right; + node.Right = right.Left; + right.Left = node; + return right; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + #endregion + + #region SplayTree(伸展树) + + /// + /// 伸展树(Splay Tree) + /// 自调整二叉搜索树,通过伸展操作将访问的节点移到根 + /// + public class SplayTree where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public Node Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node _root; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建伸展树 + /// + public SplayTree() + { + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + if (_root == null) + { + _root = new Node(value); + _count++; + return; + } + + Splay(value); + + int cmp = value.CompareTo(_root.Value); + if (cmp == 0) return; // 已存在 + + var newNode = new Node(value); + _count++; + + if (cmp < 0) + { + newNode.Left = _root.Left; + newNode.Right = _root; + _root.Left = null; + } + else + { + newNode.Right = _root.Right; + newNode.Left = _root; + _root.Right = null; + } + + if (newNode.Left != null) newNode.Left.Parent = newNode; + if (newNode.Right != null) newNode.Right.Parent = newNode; + _root = newNode; + } + + /// + /// 删除元素 + /// + public bool Remove(T value) + { + if (_root == null) return false; + + Splay(value); + + if (value.CompareTo(_root.Value) != 0) return false; + + if (_root.Left == null) + { + _root = _root.Right; + if (_root != null) _root.Parent = null; + } + else + { + var rightTree = _root.Right; + _root = _root.Left; + _root.Parent = null; + + // 将左子树的最大值伸展到根 + Splay(value); + _root.Right = rightTree; + if (rightTree != null) rightTree.Parent = _root; + } + + _count--; + return true; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + if (_root == null) return false; + + Splay(value); + return value.CompareTo(_root.Value) == 0; + } + + /// + /// 查找元素(会将其伸展到根) + /// + public T Find(T value) + { + if (!Contains(value)) + throw new KeyNotFoundException("Value not found"); + return _root.Value; + } + + /// + /// 获取最小值 + /// + public T Min() + { + if (_root == null) throw new InvalidOperationException("Tree is empty"); + var node = _root; + while (node.Left != null) node = node.Left; + Splay(node.Value); + return _root.Value; + } + + /// + /// 获取最大值 + /// + public T Max() + { + if (_root == null) throw new InvalidOperationException("Tree is empty"); + var node = _root; + while (node.Right != null) node = node.Right; + Splay(node.Value); + return _root.Value; + } + + private void Splay(T value) + { + var node = FindNode(value); + if (node == null) return; + + while (node.Parent != null) + { + var parent = node.Parent; + var grandparent = parent.Parent; + + if (grandparent == null) + { + // Zig 或 Zag + if (node == parent.Left) + RotateRight(parent); + else + RotateLeft(parent); + } + else if (node == parent.Left && parent == grandparent.Left) + { + // Zig-Zig + RotateRight(grandparent); + RotateRight(parent); + } + else if (node == parent.Right && parent == grandparent.Right) + { + // Zag-Zag + RotateLeft(grandparent); + RotateLeft(parent); + } + else if (node == parent.Right && parent == grandparent.Left) + { + // Zig-Zag + RotateLeft(parent); + RotateRight(grandparent); + } + else + { + // Zag-Zig + RotateRight(parent); + RotateLeft(grandparent); + } + } + + _root = node; + } + + private Node FindNode(T value) + { + var node = _root; + while (node != null) + { + int cmp = value.CompareTo(node.Value); + if (cmp < 0) node = node.Left; + else if (cmp > 0) node = node.Right; + else return node; + } + return null; + } + + private void RotateLeft(Node node) + { + var right = node.Right; + node.Right = right.Left; + if (right.Left != null) right.Left.Parent = node; + right.Parent = node.Parent; + if (node.Parent == null) _root = right; + else if (node == node.Parent.Left) node.Parent.Left = right; + else node.Parent.Right = right; + right.Left = node; + node.Parent = right; + } + + private void RotateRight(Node node) + { + var left = node.Left; + node.Left = left.Right; + if (left.Right != null) left.Right.Parent = node; + left.Parent = node.Parent; + if (node.Parent == null) _root = left; + else if (node == node.Parent.Left) node.Parent.Left = left; + else node.Parent.Right = left; + left.Right = node; + node.Parent = left; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + #endregion + + #region SuffixArray(后缀数组) + + /// + /// 后缀数组 + /// 用于字符串处理的高效数据结构 + /// + public class SuffixArray + { + private readonly string _text; + private readonly int[] _suffixArray; + private readonly int[] _lcpArray; + + /// + /// 原始文本 + /// + public string Text => _text; + + /// + /// 文本长度 + /// + public int Length => _text.Length; + + /// + /// 获取后缀数组 + /// + public int[] SuffixArrayValue => _suffixArray; + + /// + /// 获取LCP数组(最长公共前缀) + /// + public int[] LCPArray => _lcpArray; + + /// + /// 创建后缀数组 + /// + public SuffixArray(string text) + { + if (text == null) + throw new ArgumentNullException(nameof(text)); + + _text = text; + _suffixArray = BuildSuffixArray(text); + _lcpArray = BuildLCPArray(text, _suffixArray); + } + + /// + /// 查找模式串所有出现位置 + /// + public List Search(string pattern) + { + var result = new List(); + if (string.IsNullOrEmpty(pattern) || pattern.Length > _text.Length) + return result; + + int left = 0, right = _text.Length - 1; + while (left <= right) + { + int mid = (left + right) / 2; + int cmp = Compare(pattern, _suffixArray[mid]); + if (cmp < 0) right = mid - 1; + else if (cmp > 0) left = mid + 1; + else + { + // 找到匹配,向两边扩展 + int i = mid; + while (i >= 0 && Compare(pattern, _suffixArray[i]) == 0) + { + result.Add(_suffixArray[i]); + i--; + } + i = mid + 1; + while (i < _text.Length && Compare(pattern, _suffixArray[i]) == 0) + { + result.Add(_suffixArray[i]); + i++; + } + break; + } + } + + result.Sort(); + return result; + } + + /// + /// 检查是否包含模式串 + /// + public bool Contains(string pattern) + { + if (string.IsNullOrEmpty(pattern)) return true; + if (pattern.Length > _text.Length) return false; + + int left = 0, right = _text.Length - 1; + while (left <= right) + { + int mid = (left + right) / 2; + int cmp = Compare(pattern, _suffixArray[mid]); + if (cmp == 0) return true; + if (cmp < 0) right = mid - 1; + else left = mid + 1; + } + + return false; + } + + /// + /// 统计模式串出现次数 + /// + public int Count(string pattern) + { + return Search(pattern).Count; + } + + /// + /// 获取最长重复子串 + /// + public string GetLongestRepeatedSubstring() + { + int maxLength = 0, maxIndex = 0; + for (int i = 0; i < _lcpArray.Length; i++) + { + if (_lcpArray[i] > maxLength) + { + maxLength = _lcpArray[i]; + maxIndex = _suffixArray[i]; + } + } + return _text.Substring(maxIndex, maxLength); + } + + /// + /// 获取所有最长公共子串 + /// + public List GetAllLongestCommonSubstrings(int minLength = 2) + { + var result = new List(); + for (int i = 0; i < _lcpArray.Length; i++) + { + if (_lcpArray[i] >= minLength) + { + string substr = _text.Substring(_suffixArray[i], _lcpArray[i]); + if (!result.Contains(substr)) + result.Add(substr); + } + } + return result; + } + + private int Compare(string pattern, int start) + { + for (int i = 0; i < pattern.Length && start + i < _text.Length; i++) + { + if (pattern[i] < _text[start + i]) return -1; + if (pattern[i] > _text[start + i]) return 1; + } + if (pattern.Length > _text.Length - start) return 1; + if (pattern.Length < _text.Length - start) return -1; + return 0; + } + + private static int[] BuildSuffixArray(string text) + { + int n = text.Length; + var sa = new int[n]; + var rank = new int[n]; + var tempRank = new int[n]; + + // 初始化 + for (int i = 0; i < n; i++) + { + sa[i] = i; + rank[i] = text[i]; + } + + // 倍增法 + for (int k = 1; k < n; k *= 2) + { + // 按第二关键字排序 + var tempSa = new int[n]; + int p = 0; + for (int i = n - k; i < n; i++) tempSa[p++] = i; + for (int i = 0; i < n; i++) if (sa[i] >= k) tempSa[p++] = sa[i] - k; + + // 按第一关键字计数排序 + var cnt = new int[Math.Max(256, n)]; + for (int i = 0; i < n; i++) cnt[rank[i]]++; + for (int i = 1; i < cnt.Length; i++) cnt[i] += cnt[i - 1]; + for (int i = n - 1; i >= 0; i--) sa[--cnt[rank[tempSa[i]]]] = tempSa[i]; + + // 重新计算rank + tempRank[sa[0]] = 0; + p = 0; + for (int i = 1; i < n; i++) + { + int curr = rank[sa[i]] * (sa[i] + k < n ? rank[sa[i] + k] + 1 : 0); + int prev = rank[sa[i - 1]] * (sa[i - 1] + k < n ? rank[sa[i - 1] + k] + 1 : 0); + tempRank[sa[i]] = curr == prev ? p : ++p; + } + for (int i = 0; i < n; i++) rank[i] = tempRank[i]; + + if (p == n - 1) break; + } + + return sa; + } + + private static int[] BuildLCPArray(string text, int[] sa) + { + int n = text.Length; + var lcp = new int[n]; + var rank = new int[n]; + + for (int i = 0; i < n; i++) rank[sa[i]] = i; + + int k = 0; + for (int i = 0; i < n; i++) + { + if (rank[i] == 0) + { + k = 0; + continue; + } + + int j = sa[rank[i] - 1]; + while (i + k < n && j + k < n && text[i + k] == text[j + k]) k++; + + lcp[rank[i]] = k; + if (k > 0) k--; + } + + return lcp; + } + } + + #endregion +} diff --git a/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs b/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs new file mode 100644 index 0000000..4f306cb --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs @@ -0,0 +1,377 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Aho-Corasick 自动机工具类 + /// 用于多模式字符串匹配,线性时间复杂度 + /// 常用于敏感词过滤、关键词检测等场景 + /// + public static class AhoCorasickUtil + { + /// + /// 创建 Aho-Corasick 自动机 + /// + public static AhoCorasickAutomaton Create() + { + return new AhoCorasickAutomaton(); + } + + /// + /// 从关键词集合创建 Aho-Corasick 自动机 + /// + public static AhoCorasickAutomaton Create(IEnumerable keywords) + { + var automaton = new AhoCorasickAutomaton(); + foreach (var keyword in keywords) + { + automaton.AddKeyword(keyword); + } + automaton.Build(); + return automaton; + } + } + + /// + /// Aho-Corasick 自动机实现 + /// + public class AhoCorasickAutomaton + { + private class Node + { + public Dictionary Children { get; } = new Dictionary(); + public Node Fail { get; set; } + public List Output { get; } = new List(); + public int Depth { get; set; } + } + + private readonly Node _root; + private bool _built; + + /// + /// 已添加的关键词数量 + /// + public int KeywordCount { get; private set; } + + /// + /// 是否已构建 + /// + public bool IsBuilt => _built; + + /// + /// 创建 Aho-Corasick 自动机 + /// + public AhoCorasickAutomaton() + { + _root = new Node { Depth = 0 }; + _built = false; + KeywordCount = 0; + } + + /// + /// 添加关键词 + /// + public void AddKeyword(string keyword) + { + if (string.IsNullOrEmpty(keyword)) + throw new ArgumentException("Keyword cannot be null or empty"); + if (_built) + throw new InvalidOperationException("Cannot add keywords after building"); + + var current = _root; + foreach (char c in keyword) + { + if (!current.Children.TryGetValue(c, out var child)) + { + child = new Node { Depth = current.Depth + 1 }; + current.Children[c] = child; + } + current = child; + } + + if (current.Output.Count == 0 || !current.Output.Contains(keyword)) + { + current.Output.Add(keyword); + KeywordCount++; + } + } + + /// + /// 批量添加关键词 + /// + public void AddKeywords(IEnumerable keywords) + { + foreach (var keyword in keywords) + { + AddKeyword(keyword); + } + } + + /// + /// 构建自动机(构建失败指针) + /// + public void Build() + { + if (_built) return; + + var queue = new Queue(); + + // 第一层节点的失败指针都指向根节点 + foreach (var child in _root.Children.Values) + { + child.Fail = _root; + queue.Enqueue(child); + } + + // BFS构建失败指针 + while (queue.Count > 0) + { + var current = queue.Dequeue(); + + foreach (var kvp in current.Children) + { + char c = kvp.Key; + var child = kvp.Value; + + // 沿着失败指针找到能匹配当前字符的节点 + var fail = current.Fail; + while (fail != null && !fail.Children.ContainsKey(c)) + { + fail = fail.Fail; + } + + child.Fail = fail?.Children.GetValueOrDefault(c) ?? _root; + + // 合并输出 + child.Output.AddRange(child.Fail.Output); + + queue.Enqueue(child); + } + } + + _built = true; + } + + /// + /// 在文本中搜索所有匹配 + /// + public IEnumerable Search(string text) + { + if (!_built) + throw new InvalidOperationException("Automaton must be built before searching"); + if (string.IsNullOrEmpty(text)) + yield break; + + var current = _root; + for (int i = 0; i < text.Length; i++) + { + char c = text[i]; + + // 沿着失败指针找到能匹配的节点 + while (current != _root && !current.Children.ContainsKey(c)) + { + current = current.Fail; + } + + if (current.Children.TryGetValue(c, out var next)) + { + current = next; + } + + // 输出所有匹配 + foreach (var output in current.Output) + { + yield return new MatchResult + { + Keyword = output, + StartIndex = i - output.Length + 1, + EndIndex = i + }; + } + } + } + + /// + /// 检查文本是否包含任何关键词 + /// + public bool ContainsAny(string text) + { + if (!_built || string.IsNullOrEmpty(text)) + return false; + + var current = _root; + foreach (char c in text) + { + while (current != _root && !current.Children.ContainsKey(c)) + { + current = current.Fail; + } + + if (current.Children.TryGetValue(c, out var next)) + { + current = next; + } + + if (current.Output.Count > 0) + return true; + } + + return false; + } + + /// + /// 替换文本中的所有关键词 + /// + public string Replace(string text, char replaceChar = '*') + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var chars = text.ToCharArray(); + foreach (var match in Search(text)) + { + for (int i = match.StartIndex; i <= match.EndIndex; i++) + { + chars[i] = replaceChar; + } + } + return new string(chars); + } + + /// + /// 替换文本中的所有关键词(自定义替换字符串) + /// + public string Replace(string text, string replacement) + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var sb = new StringBuilder(); + int lastIndex = 0; + var matches = new List(Search(text)); + + // 按开始位置排序 + matches.Sort((a, b) => a.StartIndex.CompareTo(b.StartIndex)); + + foreach (var match in matches) + { + if (match.StartIndex >= lastIndex) + { + sb.Append(text.Substring(lastIndex, match.StartIndex - lastIndex)); + sb.Append(replacement); + lastIndex = match.EndIndex + 1; + } + } + + sb.Append(text.Substring(lastIndex)); + return sb.ToString(); + } + + /// + /// 高亮文本中的所有关键词 + /// + public string Highlight(string text, string prefix = "[", string suffix = "]") + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var sb = new StringBuilder(); + int lastIndex = 0; + var matches = new List(Search(text)); + matches.Sort((a, b) => a.StartIndex.CompareTo(b.StartIndex)); + + // 合并重叠的匹配 + var merged = MergeOverlaps(matches); + + foreach (var match in merged) + { + if (match.StartIndex >= lastIndex) + { + sb.Append(text.Substring(lastIndex, match.StartIndex - lastIndex)); + sb.Append(prefix); + sb.Append(text.Substring(match.StartIndex, match.EndIndex - match.StartIndex + 1)); + sb.Append(suffix); + lastIndex = match.EndIndex + 1; + } + } + + sb.Append(text.Substring(lastIndex)); + return sb.ToString(); + } + + private List MergeOverlaps(List matches) + { + if (matches.Count == 0) return matches; + + var result = new List(); + var current = matches[0]; + + for (int i = 1; i < matches.Count; i++) + { + if (matches[i].StartIndex <= current.EndIndex) + { + // 合并重叠 + current = new MatchResult + { + StartIndex = current.StartIndex, + EndIndex = Math.Max(current.EndIndex, matches[i].EndIndex), + Keyword = current.Keyword + }; + } + else + { + result.Add(current); + current = matches[i]; + } + } + result.Add(current); + + return result; + } + + /// + /// 清空自动机 + /// + public void Clear() + { + _root.Children.Clear(); + _root.Fail = null; + _root.Output.Clear(); + _built = false; + KeywordCount = 0; + } + } + + /// + /// 匹配结果 + /// + public class MatchResult + { + /// + /// 匹配的关键词 + /// + public string Keyword { get; set; } + + /// + /// 开始索引 + /// + public int StartIndex { get; set; } + + /// + /// 结束索引 + /// + public int EndIndex { get; set; } + + /// + /// 匹配长度 + /// + public int Length => EndIndex - StartIndex + 1; + + public override string ToString() + { + return $"{{Keyword: {Keyword}, Start: {StartIndex}, End: {EndIndex}}}"; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs index ac3f4a4..22f5116 100644 --- a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs @@ -127,12 +127,15 @@ public static T[] Concat(this T[]? array, T[]? other) if (array == null || array.Length < 2) return array; - var random = new Random(); var result = (T[])array.Clone(); for (int i = result.Length - 1; i > 0; i--) { - int j = random.Next(i + 1); +#if NET6_0_OR_GREATER + int j = Random.Shared.Next(i + 1); +#else + int j = new Random(Guid.NewGuid().GetHashCode()).Next(i + 1); +#endif (result[i], result[j]) = (result[j], result[i]); } diff --git a/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs b/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs new file mode 100644 index 0000000..b830a1b --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 双向字典工具类 + /// 支持键值双向查找的字典 + /// + public static class BiDictionaryUtil + { + /// + /// 创建双向字典 + /// + public static BiDictionary Create() + where TKey : notnull + where TValue : notnull + { + return new BiDictionary(); + } + + /// + /// 从字典创建双向字典 + /// + public static BiDictionary FromDictionary(IDictionary dictionary) + where TKey : notnull + where TValue : notnull + { + return new BiDictionary(dictionary); + } + } + + /// + /// 双向字典实现 + /// + /// 键类型 + /// 值类型 + public class BiDictionary : IDictionary + where TKey : notnull + where TValue : notnull + { + private readonly Dictionary _forward; + private readonly Dictionary _reverse; + + /// + /// 反向查找字典(值->键) + /// + public IReadOnlyDictionary Reverse => _reverse; + + /// + /// 键集合 + /// + public ICollection Keys => _forward.Keys; + + /// + /// 值集合 + /// + public ICollection Values => _forward.Values; + + /// + /// 元素数量 + /// + public int Count => _forward.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 通过键访问值 + /// + public TValue this[TKey key] + { + get => _forward[key]; + set + { + if (_forward.TryGetValue(key, out var oldValue)) + { + _reverse.Remove(oldValue); + } + _forward[key] = value; + _reverse[value] = key; + } + } + + /// + /// 创建双向字典 + /// + public BiDictionary() + { + _forward = new Dictionary(); + _reverse = new Dictionary(); + } + + /// + /// 从字典创建双向字典 + /// + public BiDictionary(IDictionary dictionary) : this() + { + if (dictionary == null) + throw new ArgumentNullException(nameof(dictionary)); + + foreach (var pair in dictionary) + { + Add(pair.Key, pair.Value); + } + } + + /// + /// 添加键值对 + /// + public void Add(TKey key, TValue value) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (value == null) + throw new ArgumentNullException(nameof(value)); + + if (_forward.ContainsKey(key)) + throw new ArgumentException($"Key '{key}' already exists", nameof(key)); + if (_reverse.ContainsKey(value)) + throw new ArgumentException($"Value '{value}' already exists", nameof(value)); + + _forward.Add(key, value); + _reverse.Add(value, key); + } + + /// + /// 添加键值对 + /// + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// 尝试添加键值对 + /// + public bool TryAdd(TKey key, TValue value) + { + if (_forward.ContainsKey(key) || _reverse.ContainsKey(value)) + return false; + + Add(key, value); + return true; + } + + /// + /// 通过键查找值 + /// + public bool TryGetValue(TKey key, out TValue value) + { + return _forward.TryGetValue(key, out value); + } + + /// + /// 通过值查找键 + /// + public bool TryGetKey(TValue value, out TKey key) + { + return _reverse.TryGetValue(value, out key); + } + + /// + /// 通过键获取值,不存在则返回默认值 + /// + public TValue GetValueOrDefault(TKey key, TValue defaultValue = default) + { + return _forward.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 通过值获取键,不存在则返回默认值 + /// + public TKey GetKeyOrDefault(TValue value, TKey defaultKey = default) + { + return _reverse.TryGetValue(value, out var key) ? key : defaultKey; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _forward.ContainsKey(key); + } + + /// + /// 是否包含值 + /// + public bool ContainsValue(TValue value) + { + return _reverse.ContainsKey(value); + } + + /// + /// 是否包含键值对 + /// + public bool Contains(KeyValuePair item) + { + return _forward.TryGetValue(item.Key, out var value) && + EqualityComparer.Default.Equals(value, item.Value); + } + + /// + /// 移除键值对 + /// + public bool Remove(TKey key) + { + if (!_forward.TryGetValue(key, out var value)) + return false; + + _forward.Remove(key); + _reverse.Remove(value); + return true; + } + + /// + /// 通过值移除键值对 + /// + public bool RemoveByValue(TValue value) + { + if (!_reverse.TryGetValue(value, out var key)) + return false; + + _forward.Remove(key); + _reverse.Remove(value); + return true; + } + + /// + /// 移除键值对 + /// + public bool Remove(KeyValuePair item) + { + if (!Contains(item)) + return false; + + return Remove(item.Key); + } + + /// + /// 清空 + /// + public void Clear() + { + _forward.Clear(); + _reverse.Clear(); + } + + /// + /// 复制到数组 + /// + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + if (arrayIndex < 0 || arrayIndex + Count > array.Length) + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + + int i = arrayIndex; + foreach (var pair in _forward) + { + array[i++] = pair; + } + } + + /// + /// 交换键值(创建新的 Value->Key 映射) + /// + public BiDictionary Inverse() + { + return new BiDictionary(_reverse); + } + + public IEnumerator> GetEnumerator() + { + return _forward.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs b/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs new file mode 100644 index 0000000..265c822 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs @@ -0,0 +1,207 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 布隆过滤器工具类 + /// 一种空间效率很高的概率型数据结构,用于判断元素是否在集合中 + /// 可能存在假阳性(误报),但不存在假阴性 + /// + public static class BloomFilterUtil + { + /// + /// 创建布隆过滤器 + /// + /// 元素类型 + /// 预期元素数量 + /// 可接受的假阳性概率(0-1) + /// 布隆过滤器实例 + public static BloomFilter Create(int expectedItemCount, double falsePositiveProbability = 0.01) + { + return new BloomFilter(expectedItemCount, falsePositiveProbability); + } + + /// + /// 计算最佳位数组大小 + /// + public static int CalculateOptimalBitSize(int expectedItemCount, double falsePositiveProbability) + { + return (int)Math.Ceiling(-expectedItemCount * Math.Log(falsePositiveProbability) / Math.Pow(Math.Log(2), 2)); + } + + /// + /// 计算最佳哈希函数数量 + /// + public static int CalculateOptimalHashCount(int bitSize, int expectedItemCount) + { + return (int)Math.Ceiling(bitSize / (double)expectedItemCount * Math.Log(2)); + } + } + + /// + /// 布隆过滤器实现 + /// + /// 元素类型 + public class BloomFilter + { + private readonly BitArray _bits; + private readonly int _hashCount; + private readonly Func[] _hashFunctions; + private int _itemCount; + + /// + /// 位数组大小 + /// + public int BitSize => _bits.Length; + + /// + /// 哈希函数数量 + /// + public int HashCount => _hashCount; + + /// + /// 已添加元素数量 + /// + public int ItemCount => _itemCount; + + /// + /// 当前估计的假阳性概率 + /// + public double CurrentFalsePositiveProbability + { + get + { + if (_itemCount == 0) return 0; + double ratio = (double)_itemCount * _hashCount / BitSize; + return Math.Pow(1 - Math.Exp(-ratio), _hashCount); + } + } + + /// + /// 创建布隆过滤器 + /// + /// 预期元素数量 + /// 可接受的假阳性概率 + public BloomFilter(int expectedItemCount, double falsePositiveProbability = 0.01) + { + if (expectedItemCount <= 0) + throw new ArgumentOutOfRangeException(nameof(expectedItemCount)); + if (falsePositiveProbability <= 0 || falsePositiveProbability >= 1) + throw new ArgumentOutOfRangeException(nameof(falsePositiveProbability)); + + int bitSize = BloomFilterUtil.CalculateOptimalBitSize(expectedItemCount, falsePositiveProbability); + _hashCount = BloomFilterUtil.CalculateOptimalHashCount(bitSize, expectedItemCount); + + _bits = new BitArray(bitSize); + _hashFunctions = CreateHashFunctions(_hashCount); + _itemCount = 0; + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + foreach (var hashFunc in _hashFunctions) + { + int index = Math.Abs(hashFunc(item)) % BitSize; + _bits[index] = true; + } + _itemCount++; + } + + /// + /// 批量添加元素 + /// + public void AddRange(IEnumerable items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + Add(item); + } + } + + /// + /// 检查元素可能存在 + /// + /// true 表示可能存在(可能有假阳性),false 表示一定不存在 + public bool MightContain(T item) + { + if (item == null) + return false; + + foreach (var hashFunc in _hashFunctions) + { + int index = Math.Abs(hashFunc(item)) % BitSize; + if (!_bits[index]) + return false; + } + return true; + } + + /// + /// 清空过滤器 + /// + public void Clear() + { + _bits.SetAll(false); + _itemCount = 0; + } + + /// + /// 获取位数组数据 + /// + public byte[] GetBytes() + { + byte[] bytes = new byte[(_bits.Length + 7) / 8]; + _bits.CopyTo(bytes, 0); + return bytes; + } + + /// + /// 从字节数组恢复位数组 + /// + public void SetBytes(byte[] bytes) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + var newBits = new BitArray(bytes); + if (newBits.Length != _bits.Length) + throw new ArgumentException("Byte array length does not match filter size", nameof(bytes)); + + for (int i = 0; i < _bits.Length; i++) + { + _bits[i] = newBits[i]; + } + } + + private static Func[] CreateHashFunctions(int count) + { + var functions = new Func[count]; + + // 使用双重哈希技术生成多个哈希函数 + // h(i) = hash1(x) + i * hash2(x) + for (int i = 0; i < count; i++) + { + int seed = i * 31 + 17; + functions[i] = item => + { + int hash1 = item?.GetHashCode() ?? 0; + int hash2 = ((hash1 >> 16) ^ hash1) * seed; + return hash1 + hash2 * seed; + }; + } + + return functions; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/CacheUtil.cs b/EasyTool.Core/CollectionsCategory/CacheUtil.cs new file mode 100644 index 0000000..8c764f7 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CacheUtil.cs @@ -0,0 +1,888 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace EasyTool.CollectionsCategory +{ + /// + /// LFU(最不经常使用)缓存工具类 + /// + public static class LFUCacheUtil + { + /// + /// 创建 LFU 缓存 + /// + public static LFUCache Create(int capacity) + { + return new LFUCache(capacity); + } + } + + /// + /// LFU 缓存实现 + /// + public class LFUCache + { + private readonly int _capacity; + private readonly Dictionary _cache; + private readonly Dictionary> _frequencyLists; + private int _minFrequency; + + private class CacheItem + { + public TValue Value { get; set; } + public int Frequency { get; set; } + public LinkedListNode Node { get; set; } + } + + /// + /// 当前元素数量 + /// + public int Count => _cache.Count; + + /// + /// 缓存容量 + /// + public int Capacity => _capacity; + + /// + /// 创建 LFU 缓存 + /// + public LFUCache(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _cache = new Dictionary(); + _frequencyLists = new Dictionary>(); + _minFrequency = 0; + } + + /// + /// 获取值 + /// + public bool TryGet(TKey key, out TValue value) + { + if (_cache.TryGetValue(key, out var item)) + { + UpdateFrequency(item); + value = item.Value; + return true; + } + + value = default; + return false; + } + + /// + /// 获取或添加值 + /// + public TValue GetOrAdd(TKey key, Func valueFactory) + { + if (TryGet(key, out var value)) + return value; + + value = valueFactory(key); + Add(key, value); + return value; + } + + /// + /// 添加或更新值 + /// + public void Add(TKey key, TValue value) + { + if (_cache.TryGetValue(key, out var item)) + { + item.Value = value; + UpdateFrequency(item); + return; + } + + if (_cache.Count >= _capacity) + { + Evict(); + } + + var newNode = new CacheItem { Value = value, Frequency = 1 }; + if (!_frequencyLists.ContainsKey(1)) + { + _frequencyLists[1] = new LinkedList(); + } + newNode.Node = _frequencyLists[1].AddLast(key); + _cache[key] = newNode; + _minFrequency = 1; + } + + /// + /// 移除指定键 + /// + public bool Remove(TKey key) + { + if (!_cache.TryGetValue(key, out var item)) + return false; + + _frequencyLists[item.Frequency].Remove(item.Node); + _cache.Remove(key); + return true; + } + + /// + /// 清空缓存 + /// + public void Clear() + { + _cache.Clear(); + _frequencyLists.Clear(); + _minFrequency = 0; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _cache.ContainsKey(key); + } + + private void UpdateFrequency(CacheItem item) + { + int oldFreq = item.Frequency; + int newFreq = oldFreq + 1; + + _frequencyLists[oldFreq].Remove(item.Node); + if (_frequencyLists[oldFreq].Count == 0) + { + _frequencyLists.Remove(oldFreq); + if (_minFrequency == oldFreq) + { + _minFrequency = newFreq; + } + } + + item.Frequency = newFreq; + if (!_frequencyLists.ContainsKey(newFreq)) + { + _frequencyLists[newFreq] = new LinkedList(); + } + item.Node = _frequencyLists[newFreq].AddLast(_cache.First(x => x.Value == item).Key); + } + + private void Evict() + { + if (_minFrequency == 0 || !_frequencyLists.ContainsKey(_minFrequency)) + return; + + var list = _frequencyLists[_minFrequency]; + var keyToRemove = list.First.Value; + list.RemoveFirst(); + _cache.Remove(keyToRemove); + + if (list.Count == 0) + { + _frequencyLists.Remove(_minFrequency); + } + } + } + + /// + /// FIFO(先进先出)缓存工具类 + /// + public static class FIFOCacheUtil + { + /// + /// 创建 FIFO 缓存 + /// + public static FIFOCache Create(int capacity) + { + return new FIFOCache(capacity); + } + } + + /// + /// FIFO 缓存实现 + /// + public class FIFOCache + { + private readonly int _capacity; + private readonly Dictionary _cache; + private readonly Queue _queue; + + /// + /// 当前元素数量 + /// + public int Count => _cache.Count; + + /// + /// 缓存容量 + /// + public int Capacity => _capacity; + + /// + /// 创建 FIFO 缓存 + /// + public FIFOCache(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _cache = new Dictionary(); + _queue = new Queue(); + } + + /// + /// 获取值 + /// + public bool TryGet(TKey key, out TValue value) + { + return _cache.TryGetValue(key, out value); + } + + /// + /// 获取或添加值 + /// + public TValue GetOrAdd(TKey key, Func valueFactory) + { + if (_cache.TryGetValue(key, out var value)) + return value; + + value = valueFactory(key); + Add(key, value); + return value; + } + + /// + /// 添加或更新值 + /// + public void Add(TKey key, TValue value) + { + if (_cache.ContainsKey(key)) + { + _cache[key] = value; + return; + } + + if (_cache.Count >= _capacity) + { + var oldestKey = _queue.Dequeue(); + _cache.Remove(oldestKey); + } + + _cache[key] = value; + _queue.Enqueue(key); + } + + /// + /// 移除指定键 + /// + public bool Remove(TKey key) + { + if (!_cache.Remove(key)) + return false; + + // 需要重建队列以移除中间元素 + var newQueue = new Queue(); + foreach (var k in _queue) + { + if (!EqualityComparer.Default.Equals(k, key)) + { + newQueue.Enqueue(k); + } + } + return true; + } + + /// + /// 清空缓存 + /// + public void Clear() + { + _cache.Clear(); + _queue.Clear(); + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _cache.ContainsKey(key); + } + } + + /// + /// 定时缓存工具类 + /// + public static class TimedCacheUtil + { + /// + /// 创建定时缓存 + /// + public static TimedCache Create(TimeSpan expiration) + { + return new TimedCache(expiration); + } + + /// + /// 创建滑动过期缓存 + /// + public static TimedCache CreateSliding(TimeSpan expiration) + { + return new TimedCache(expiration, true); + } + } + + /// + /// 定时缓存实现 + /// + public class TimedCache + { + private readonly TimeSpan _expiration; + private readonly bool _slidingExpiration; + private readonly Dictionary _cache; + + private class CacheItem + { + public TValue Value { get; set; } + public DateTime ExpirationTime { get; set; } + } + + /// + /// 当前元素数量 + /// + public int Count => _cache.Count; + + /// + /// 过期时间 + /// + public TimeSpan Expiration => _expiration; + + /// + /// 创建定时缓存 + /// + public TimedCache(TimeSpan expiration, bool slidingExpiration = false) + { + if (expiration <= TimeSpan.Zero) + throw new ArgumentOutOfRangeException(nameof(expiration)); + + _expiration = expiration; + _slidingExpiration = slidingExpiration; + _cache = new Dictionary(); + } + + /// + /// 获取值 + /// + public bool TryGet(TKey key, out TValue value) + { + CleanupExpired(); + + if (_cache.TryGetValue(key, out var item)) + { + if (DateTime.UtcNow < item.ExpirationTime) + { + if (_slidingExpiration) + { + item.ExpirationTime = DateTime.UtcNow.Add(_expiration); + } + value = item.Value; + return true; + } + _cache.Remove(key); + } + + value = default; + return false; + } + + /// + /// 获取或添加值 + /// + public TValue GetOrAdd(TKey key, Func valueFactory) + { + if (TryGet(key, out var value)) + return value; + + value = valueFactory(key); + Add(key, value); + return value; + } + + /// + /// 添加或更新值 + /// + public void Add(TKey key, TValue value) + { + _cache[key] = new CacheItem + { + Value = value, + ExpirationTime = DateTime.UtcNow.Add(_expiration) + }; + } + + /// + /// 添加带自定义过期时间的值 + /// + public void Add(TKey key, TValue value, TimeSpan customExpiration) + { + _cache[key] = new CacheItem + { + Value = value, + ExpirationTime = DateTime.UtcNow.Add(customExpiration) + }; + } + + /// + /// 移除指定键 + /// + public bool Remove(TKey key) + { + return _cache.Remove(key); + } + + /// + /// 清空缓存 + /// + public void Clear() + { + _cache.Clear(); + } + + /// + /// 是否包含键(未过期) + /// + public bool ContainsKey(TKey key) + { + CleanupExpired(); + return _cache.ContainsKey(key); + } + + /// + /// 清理过期项 + /// + public void CleanupExpired() + { + var now = DateTime.UtcNow; + var expired = _cache.Where(x => x.Value.ExpirationTime <= now).Select(x => x.Key).ToList(); + foreach (var key in expired) + { + _cache.Remove(key); + } + } + } + + /// + /// 限流器工具类 + /// + public static class RateLimiterUtil + { + /// + /// 创建令牌桶限流器 + /// + public static TokenBucketRateLimiter CreateTokenBucket(int capacity, int refillRate, TimeSpan refillPeriod) + { + return new TokenBucketRateLimiter(capacity, refillRate, refillPeriod); + } + + /// + /// 创建滑动窗口限流器 + /// + public static SlidingWindowRateLimiter CreateSlidingWindow(int limit, TimeSpan window) + { + return new SlidingWindowRateLimiter(limit, window); + } + + /// + /// 创建固定窗口限流器 + /// + public static FixedWindowRateLimiter CreateFixedWindow(int limit, TimeSpan window) + { + return new FixedWindowRateLimiter(limit, window); + } + } + + /// + /// 令牌桶限流器 + /// + public class TokenBucketRateLimiter + { + private readonly int _capacity; + private readonly int _refillRate; + private readonly TimeSpan _refillPeriod; + private double _tokens; + private DateTime _lastRefill; + private readonly object _lock = new object(); + + /// + /// 桶容量 + /// + public int Capacity => _capacity; + + /// + /// 当前令牌数 + /// + public int AvailableTokens + { + get + { + lock (_lock) + { + Refill(); + return (int)_tokens; + } + } + } + + /// + /// 创建令牌桶限流器 + /// + public TokenBucketRateLimiter(int capacity, int refillRate, TimeSpan refillPeriod) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + if (refillRate <= 0) + throw new ArgumentOutOfRangeException(nameof(refillRate)); + + _capacity = capacity; + _refillRate = refillRate; + _refillPeriod = refillPeriod; + _tokens = capacity; + _lastRefill = DateTime.UtcNow; + } + + /// + /// 尝试获取令牌 + /// + public bool TryAcquire(int tokens = 1) + { + lock (_lock) + { + Refill(); + if (_tokens >= tokens) + { + _tokens -= tokens; + return true; + } + return false; + } + } + + /// + /// 等待获取令牌 + /// + public void Acquire(int tokens = 1) + { + while (!TryAcquire(tokens)) + { + Thread.Sleep(10); + } + } + + private void Refill() + { + var now = DateTime.UtcNow; + var elapsed = now - _lastRefill; + var tokensToAdd = elapsed.TotalMilliseconds / _refillPeriod.TotalMilliseconds * _refillRate; + + if (tokensToAdd > 0) + { + _tokens = Math.Min(_capacity, _tokens + tokensToAdd); + _lastRefill = now; + } + } + } + + /// + /// 滑动窗口限流器 + /// + public class SlidingWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private readonly Queue _timestamps; + private readonly object _lock = new object(); + + /// + /// 限制 + /// + public int Limit => _limit; + + /// + /// 窗口大小 + /// + public TimeSpan Window => _window; + + /// + /// 创建滑动窗口限流器 + /// + public SlidingWindowRateLimiter(int limit, TimeSpan window) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit)); + + _limit = limit; + _window = window; + _timestamps = new Queue(); + } + + /// + /// 尝试获取许可 + /// + public bool TryAcquire() + { + lock (_lock) + { + Cleanup(); + if (_timestamps.Count < _limit) + { + _timestamps.Enqueue(DateTime.UtcNow); + return true; + } + return false; + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + Cleanup(); + return _timestamps.Count; + } + } + } + + private void Cleanup() + { + var cutoff = DateTime.UtcNow - _window; + while (_timestamps.Count > 0 && _timestamps.Peek() < cutoff) + { + _timestamps.Dequeue(); + } + } + } + + /// + /// 固定窗口限流器 + /// + public class FixedWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private int _count; + private DateTime _windowStart; + private readonly object _lock = new object(); + + /// + /// 限制 + /// + public int Limit => _limit; + + /// + /// 窗口大小 + /// + public TimeSpan Window => _window; + + /// + /// 创建固定窗口限流器 + /// + public FixedWindowRateLimiter(int limit, TimeSpan window) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit)); + + _limit = limit; + _window = window; + _count = 0; + _windowStart = DateTime.UtcNow; + } + + /// + /// 尝试获取许可 + /// + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTime.UtcNow; + if (now - _windowStart >= _window) + { + _count = 0; + _windowStart = now; + } + + if (_count < _limit) + { + _count++; + return true; + } + return false; + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + var now = DateTime.UtcNow; + if (now - _windowStart >= _window) + return 0; + return _count; + } + } + } + + /// + /// 获取距离下一个窗口的时间 + /// + public TimeSpan TimeUntilNextWindow + { + get + { + lock (_lock) + { + var elapsed = DateTime.UtcNow - _windowStart; + return _window - elapsed; + } + } + } + } + + /// + /// 对象池工具类 + /// + public static class ObjectPoolUtil + { + /// + /// 创建对象池 + /// + public static ObjectPool Create(Func factory, int maxSize = 100) where T : class + { + return new ObjectPool(factory, maxSize); + } + } + + /// + /// 对象池实现 + /// + public class ObjectPool where T : class + { + private readonly Func _factory; + private readonly int _maxSize; + private readonly Stack _pool; + private readonly object _lock = new object(); + + /// + /// 池中可用对象数 + /// + public int AvailableCount + { + get + { + lock (_lock) + { + return _pool.Count; + } + } + } + + /// + /// 最大大小 + /// + public int MaxSize => _maxSize; + + /// + /// 创建对象池 + /// + public ObjectPool(Func factory, int maxSize = 100) + { + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); + _maxSize = maxSize > 0 ? maxSize : throw new ArgumentOutOfRangeException(nameof(maxSize)); + _pool = new Stack(); + } + + /// + /// 获取对象 + /// + public T Get() + { + lock (_lock) + { + if (_pool.Count > 0) + { + return _pool.Pop(); + } + } + return _factory(); + } + + /// + /// 归还对象 + /// + public void Return(T obj) + { + if (obj == null) + return; + + // 如果对象实现了 IResettable,重置它 + if (obj is IResettable resettable) + { + resettable.Reset(); + } + + lock (_lock) + { + if (_pool.Count < _maxSize) + { + _pool.Push(obj); + } + } + } + + /// + /// 清空池 + /// + public void Clear() + { + lock (_lock) + { + _pool.Clear(); + } + } + + /// + /// 预热池 + /// + public void Warmup(int count) + { + for (int i = 0; i < count && i < _maxSize; i++) + { + Return(_factory()); + } + } + } + + /// + /// 可重置接口 + /// + public interface IResettable + { + /// + /// 重置对象状态 + /// + void Reset(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs b/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs new file mode 100644 index 0000000..0a217e4 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs @@ -0,0 +1,598 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 基数估计和一致性哈希工具类 + /// + public static class CardinalityAndHashUtil + { + /// + /// 创建 HyperLogLog + /// + public static HyperLogLog CreateHyperLogLog(int precision = 14) + { + return new HyperLogLog(precision); + } + + /// + /// 创建一致性哈希 + /// + public static ConsistentHash CreateConsistentHash(int virtualNodes = 150) + { + return new ConsistentHash(virtualNodes); + } + + /// + /// 创建线性计数器 + /// + public static LinearCounter CreateLinearCounter(int size) + { + return new LinearCounter(size); + } + } + + /// + /// HyperLogLog 基数估计器 + /// 使用极小内存估计超大集合的不同元素数量 + /// + public class HyperLogLog + { + private readonly byte[] _registers; + private readonly int _precision; + private readonly int _m; + private readonly double _alpha; + + /// + /// 精度参数 + /// + public int Precision => _precision; + + /// + /// 寄存器数量 + /// + public int RegisterCount => _m; + + /// + /// 内存使用(字节) + /// + public int MemoryBytes => _registers.Length; + + /// + /// 创建 HyperLogLog + /// + /// 精度参数(4-16),越大越精确但占用更多内存 + public HyperLogLog(int precision = 14) + { + if (precision < 4 || precision > 16) + throw new ArgumentOutOfRangeException(nameof(precision), "Precision must be between 4 and 16"); + + _precision = precision; + _m = 1 << precision; + _registers = new byte[_m]; + + // 计算 alpha 常数 + switch (_m) + { + case 16: + _alpha = 0.673; + break; + case 32: + _alpha = 0.697; + break; + case 64: + _alpha = 0.709; + break; + default: + _alpha = 0.7213 / (1 + 1.079 / _m); + break; + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + ulong hash = MurmurHash3(data); + int index = (int)(hash >> (64 - _precision)); + int leadingZeros = CountLeadingZeros(hash << _precision) + 1; + + if (leadingZeros > _registers[index]) + { + _registers[index] = (byte)leadingZeros; + } + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public void Add(int value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 添加长整数 + /// + public void Add(long value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计基数 + /// + public long Estimate() + { + double sum = 0; + int zeros = 0; + + foreach (var reg in _registers) + { + sum += Math.Pow(2, -reg); + if (reg == 0) + zeros++; + } + + double estimate = _alpha * _m * _m / sum; + + // 小范围修正 + if (estimate <= 2.5 * _m) + { + if (zeros > 0) + { + estimate = _m * Math.Log((double)_m / zeros); + } + } + // 大范围修正 + else if (estimate > (1L << 32) / 30.0) + { + estimate = -(1L << 32) * Math.Log(1 - estimate / (1L << 32)); + } + + return (long)estimate; + } + + /// + /// 合并另一个 HyperLogLog + /// + public void Merge(HyperLogLog other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._precision != _precision) + throw new ArgumentException("Cannot merge HyperLogLog with different precision"); + + for (int i = 0; i < _m; i++) + { + if (other._registers[i] > _registers[i]) + { + _registers[i] = other._registers[i]; + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_registers, 0, _registers.Length); + } + + private static ulong MurmurHash3(byte[] data) + { + const ulong c1 = 0x87c37b91114253d5; + const ulong c2 = 0x4cf5ad432745937f; + + int length = data.Length; + int blocks = length / 8; + + ulong h1 = 0; + int i = 0; + + for (int j = 0; j < blocks; j++) + { + ulong k1 = BitConverter.ToUInt64(data, i); + i += 8; + + k1 *= c1; + k1 = RotateLeft(k1, 31); + k1 *= c2; + h1 ^= k1; + + h1 = RotateLeft(h1, 27); + h1 = h1 * 5 + 0x52dce729; + } + + ulong remaining = 0; + int remainingLength = length - blocks * 8; + if (remainingLength > 0) + { + for (int j = 0; j < remainingLength; j++) + { + remaining |= (ulong)data[i + j] << (j * 8); + } + + remaining *= c1; + remaining = RotateLeft(remaining, 31); + remaining *= c2; + h1 ^= remaining; + } + + h1 ^= (ulong)length; + h1 ^= h1 >> 33; + h1 *= 0xff51afd7ed558ccd; + h1 ^= h1 >> 33; + h1 *= 0xc4ceb9fe1a85ec53; + h1 ^= h1 >> 33; + + return h1; + } + + private static ulong RotateLeft(ulong x, int k) + { + return (x << k) | (x >> (64 - k)); + } + + private static int CountLeadingZeros(ulong x) + { + if (x == 0) + return 64; + + int n = 0; + if ((x & 0xFFFFFFFF00000000) == 0) { n += 32; x <<= 32; } + if ((x & 0xFFFF000000000000) == 0) { n += 16; x <<= 16; } + if ((x & 0xFF00000000000000) == 0) { n += 8; x <<= 8; } + if ((x & 0xF000000000000000) == 0) { n += 4; x <<= 4; } + if ((x & 0xC000000000000000) == 0) { n += 2; x <<= 2; } + if ((x & 0x8000000000000000) == 0) { n += 1; } + + return n; + } + } + + /// + /// 一致性哈希 + /// 用于分布式系统中的负载均衡 + /// + public class ConsistentHash + { + private readonly SortedDictionary _ring; + private readonly int _virtualNodes; + private readonly HashSet _nodes; + + /// + /// 节点数量 + /// + public int NodeCount => _nodes.Count; + + /// + /// 虚拟节点数量 + /// + public int VirtualNodeCount => _virtualNodes; + + /// + /// 环上总位置数 + /// + public int RingSize => _ring.Count; + + /// + /// 创建一致性哈希 + /// + /// 每个物理节点的虚拟节点数 + public ConsistentHash(int virtualNodes = 150) + { + if (virtualNodes <= 0) + throw new ArgumentOutOfRangeException(nameof(virtualNodes)); + + _ring = new SortedDictionary(); + _virtualNodes = virtualNodes; + _nodes = new HashSet(); + } + + /// + /// 添加节点 + /// + public void AddNode(TNode node) + { + if (node == null) + throw new ArgumentNullException(nameof(node)); + + if (_nodes.Contains(node)) + return; + + _nodes.Add(node); + + for (int i = 0; i < _virtualNodes; i++) + { + ulong hash = HashNode(node, i); + _ring[hash] = node; + } + } + + /// + /// 移除节点 + /// + public bool RemoveNode(TNode node) + { + if (node == null) + throw new ArgumentNullException(nameof(node)); + + if (!_nodes.Remove(node)) + return false; + + for (int i = 0; i < _virtualNodes; i++) + { + ulong hash = HashNode(node, i); + _ring.Remove(hash); + } + + return true; + } + + /// + /// 获取键对应的节点 + /// + public TNode GetNode(string key) + { + if (_ring.Count == 0) + throw new InvalidOperationException("No nodes available"); + + ulong hash = HashKey(key); + + // 查找第一个大于等于 hash 的节点 + foreach (var kvp in _ring) + { + if (kvp.Key >= hash) + return kvp.Value; + } + + // 如果没有找到,返回第一个节点(环形) + return _ring.First().Value; + } + + /// + /// 获取键对应的多个节点(用于复制) + /// + public List GetNodes(string key, int count) + { + if (count <= 0) + throw new ArgumentOutOfRangeException(nameof(count)); + if (_ring.Count == 0) + throw new InvalidOperationException("No nodes available"); + + var result = new List(); + var uniqueNodes = new HashSet(); + ulong hash = HashKey(key); + + // 找到起始位置 + var candidates = _ring.Where(kvp => kvp.Key >= hash).ToList(); + if (candidates.Count == 0) + { + candidates = _ring.ToList(); + } + + int startIndex = 0; + for (int i = 0; i < candidates.Count; i++) + { + if (candidates[i].Key >= hash) + { + startIndex = i; + break; + } + } + + // 收集不同的节点 + int index = startIndex; + while (uniqueNodes.Count < count && uniqueNodes.Count < _nodes.Count) + { + var node = candidates[index % candidates.Count].Value; + if (uniqueNodes.Add(node)) + { + result.Add(node); + } + index++; + } + + return result; + } + + /// + /// 获取节点负责的键范围 + /// + public List GetNodeRanges(TNode node) + { + var ranges = new List(); + + foreach (var kvp in _ring) + { + if (EqualityComparer.Default.Equals(kvp.Value, node)) + { + ranges.Add(kvp.Key); + } + } + + return ranges; + } + + /// + /// 清空所有节点 + /// + public void Clear() + { + _ring.Clear(); + _nodes.Clear(); + } + + /// + /// 获取所有节点 + /// + public IReadOnlyCollection GetNodes() + { + return _nodes; + } + + private ulong HashNode(TNode node, int replicaIndex) + { + string key = $"{node}:#{replicaIndex}"; + return HashKey(key); + } + + private ulong HashKey(string key) + { + byte[] data = System.Text.Encoding.UTF8.GetBytes(key); + return MurmurHash2(data); + } + + private static ulong MurmurHash2(byte[] data) + { + const ulong m = 0xc6a4a7935bd1e995; + const int r = 47; + + ulong h = 0 ^ (ulong)data.Length * m; + + int length = data.Length; + int i = 0; + + while (i + 8 <= length) + { + ulong k = BitConverter.ToUInt64(data, i); + i += 8; + + k *= m; + k ^= k >> r; + k *= m; + + h ^= k; + h *= m; + } + + switch (length - i) + { + case 7: h ^= (ulong)data[i + 6] << 48; goto case 6; + case 6: h ^= (ulong)data[i + 5] << 40; goto case 5; + case 5: h ^= (ulong)data[i + 4] << 32; goto case 4; + case 4: h ^= (ulong)data[i + 3] << 24; goto case 3; + case 3: h ^= (ulong)data[i + 2] << 16; goto case 2; + case 2: h ^= (ulong)data[i + 1] << 8; goto case 1; + case 1: + h ^= data[i]; + h *= m; + break; + } + + h ^= h >> r; + h *= m; + h ^= h >> r; + + return h; + } + } + + /// + /// 线性计数器 + /// 简单的基数估计,适合小到中等规模数据 + /// + public class LinearCounter + { + private readonly BitSet _bits; + private readonly int _size; + + /// + /// 位大小 + /// + public int Size => _size; + + /// + /// 创建线性计数器 + /// + public LinearCounter(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _bits = new BitSet(size); + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + int hash = Math.Abs(Hash(data)) % _size; + _bits.Set(hash); + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 估计基数 + /// + public long Estimate() + { + int setBits = _bits.Cardinality; + if (setBits == 0) + return 0; + + // 使用最大似然估计 + double ratio = (double)(_size - setBits) / _size; + if (ratio <= 0) + return _size; + + return (long)(-_size * Math.Log(ratio)); + } + + /// + /// 合并另一个计数器 + /// + public void Merge(LinearCounter other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._size != _size) + throw new ArgumentException("Cannot merge counters with different sizes"); + + _bits.Or(other._bits); + } + + /// + /// 清空 + /// + public void Clear() + { + _bits.ClearAll(); + } + + private static int Hash(byte[] data) + { + unchecked + { + int hash = 17; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs b/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs new file mode 100644 index 0000000..4923152 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 环形缓冲区工具类 + /// 固定大小的循环缓冲区,当满时自动覆盖最旧的数据 + /// 适用于日志缓冲、事件队列、滑动窗口等场景 + /// + public static class CircularBufferUtil + { + /// + /// 创建环形缓冲区 + /// + /// 元素类型 + /// 容量 + /// 环形缓冲区实例 + public static CircularBuffer Create(int capacity) + { + return new CircularBuffer(capacity); + } + + /// + /// 从集合创建环形缓冲区 + /// + public static CircularBuffer FromEnumerable(IEnumerable collection, int capacity) + { + return new CircularBuffer(capacity, collection); + } + } + + /// + /// 环形缓冲区实现 + /// + /// 元素类型 + public class CircularBuffer : IEnumerable, IReadOnlyCollection + { + private readonly T[] _buffer; + private int _head; + private int _tail; + private int _count; + + /// + /// 缓冲区容量 + /// + public int Capacity => _buffer.Length; + + /// + /// 当前元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 是否已满 + /// + public bool IsFull => _count == Capacity; + + /// + /// 索引访问元素 + /// + public T this[int index] + { + get + { + if (index < 0 || index >= _count) + throw new ArgumentOutOfRangeException(nameof(index)); + return _buffer[(_head + index) % Capacity]; + } + } + + /// + /// 创建环形缓冲区 + /// + /// 容量(必须大于0) + public CircularBuffer(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0"); + + _buffer = new T[capacity]; + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 从集合创建环形缓冲区 + /// + public CircularBuffer(int capacity, IEnumerable collection) : this(capacity) + { + if (collection == null) + throw new ArgumentNullException(nameof(collection)); + + foreach (var item in collection) + { + Push(item); + } + } + + /// + /// 添加元素到尾部 + /// + public void Push(T item) + { + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + + if (IsFull) + { + _head = (_head + 1) % Capacity; + } + else + { + _count++; + } + } + + /// + /// 批量添加元素 + /// + public void PushRange(IEnumerable items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + Push(item); + } + } + + /// + /// 从头部移除并返回元素 + /// + public T Pop() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + var item = _buffer[_head]; + _buffer[_head] = default; + _head = (_head + 1) % Capacity; + _count--; + + return item; + } + + /// + /// 从尾部移除并返回元素 + /// + public T PopLast() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + _tail = (_tail - 1 + Capacity) % Capacity; + var item = _buffer[_tail]; + _buffer[_tail] = default; + _count--; + + return item; + } + + /// + /// 查看头部元素(不移除) + /// + public T Peek() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + return _buffer[_head]; + } + + /// + /// 查看尾部元素(不移除) + /// + public T PeekLast() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + int index = (_tail - 1 + Capacity) % Capacity; + return _buffer[index]; + } + + /// + /// 清空缓冲区 + /// + public void Clear() + { + Array.Clear(_buffer, 0, Capacity); + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 判断是否包含指定元素 + /// + public bool Contains(T item) + { + for (int i = 0; i < _count; i++) + { + int index = (_head + i) % Capacity; + if (EqualityComparer.Default.Equals(_buffer[index], item)) + return true; + } + return false; + } + + /// + /// 复制到数组 + /// + public T[] ToArray() + { + var result = new T[_count]; + for (int i = 0; i < _count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + + /// + /// 获取最新的N个元素 + /// + public T[] GetLatest(int count) + { + count = Math.Min(count, _count); + var result = new T[count]; + + for (int i = 0; i < count; i++) + { + int sourceIndex = (_head + _count - count + i) % Capacity; + result[i] = _buffer[sourceIndex]; + } + + return result; + } + + /// + /// 获取最旧的N个元素 + /// + public T[] GetOldest(int count) + { + count = Math.Min(count, _count); + var result = new T[count]; + + for (int i = 0; i < count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + + return result; + } + + public IEnumerator GetEnumerator() + { + for (int i = 0; i < _count; i++) + { + yield return _buffer[(_head + i) % Capacity]; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs b/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs new file mode 100644 index 0000000..b016ed4 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs @@ -0,0 +1,211 @@ +using System; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Count-Min Sketch 工具类 + /// 概率数据结构,用于估计元素频率 + /// 空间复杂度远小于精确计数,但有少量误差 + /// + public static class CountMinSketchUtil + { + /// + /// 创建 Count-Min Sketch + /// + /// 宽度(哈希桶数量) + /// 深度(哈希函数数量) + public static CountMinSketch Create(int width = 1000, int depth = 5) + { + return new CountMinSketch(width, depth); + } + + /// + /// 根据期望误差率和置信度创建 + /// + /// 期望误差率(0-1) + /// 置信度(0-1) + public static CountMinSketch CreateWithAccuracy(double errorRate = 0.01, double confidence = 0.99) + { + if (errorRate <= 0 || errorRate >= 1) + throw new ArgumentOutOfRangeException(nameof(errorRate), "Error rate must be between 0 and 1"); + if (confidence <= 0 || confidence >= 1) + throw new ArgumentOutOfRangeException(nameof(confidence), "Confidence must be between 0 and 1"); + + int width = (int)Math.Ceiling(Math.E / errorRate); + int depth = (int)Math.Ceiling(-Math.Log(1 - confidence)); + return new CountMinSketch(width, depth); + } + } + + /// + /// Count-Min Sketch 实现 + /// + public class CountMinSketch + { + private readonly int _width; + private readonly int _depth; + private readonly ulong[,] _counters; + private readonly int[] _seeds; + private ulong _totalCount; + + /// + /// 宽度 + /// + public int Width => _width; + + /// + /// 深度 + /// + public int Depth => _depth; + + /// + /// 总计数 + /// + public ulong TotalCount => _totalCount; + + /// + /// 创建 Count-Min Sketch + /// + public CountMinSketch(int width, int depth) + { + if (width <= 0) + throw new ArgumentOutOfRangeException(nameof(width)); + if (depth <= 0) + throw new ArgumentOutOfRangeException(nameof(depth)); + + _width = width; + _depth = depth; + _counters = new ulong[depth, width]; + _seeds = new int[depth]; + _totalCount = 0; + + // 初始化不同的哈希种子 + var random = new Random(12345); + for (int i = 0; i < depth; i++) + { + _seeds[i] = random.Next(); + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + _counters[i, index]++; + } + _totalCount++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public void Add(int value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 添加长整数 + /// + public void Add(long value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计元素频率 + /// + public ulong Estimate(byte[] data) + { + ulong min = ulong.MaxValue; + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + if (_counters[i, index] < min) + min = _counters[i, index]; + } + return min; + } + + /// + /// 估计字符串频率 + /// + public ulong Estimate(string value) + { + return Estimate(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 估计整数频率 + /// + public ulong Estimate(int value) + { + return Estimate(BitConverter.GetBytes(value)); + } + + /// + /// 合并另一个 Count-Min Sketch + /// + public void Merge(CountMinSketch other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._width != _width || other._depth != _depth) + throw new ArgumentException("Cannot merge Count-Min Sketch with different dimensions"); + + for (int i = 0; i < _depth; i++) + { + for (int j = 0; j < _width; j++) + { + _counters[i, j] += other._counters[i, j]; + } + } + _totalCount += other._totalCount; + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + _totalCount = 0; + } + + /// + /// 获取估计误差上限 + /// + public double GetErrorBound() + { + if (_totalCount == 0) return 0; + return (double)_totalCount / _width; + } + + private static int Hash(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/DistributionUtil.cs b/EasyTool.Core/CollectionsCategory/DistributionUtil.cs new file mode 100644 index 0000000..cba410d --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/DistributionUtil.cs @@ -0,0 +1,819 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 概率分布工具类 + /// + public static class DistributionUtil + { + /// + /// 创建离散分布 + /// + public static DiscreteDistribution CreateDiscrete() where T : notnull + { + return new DiscreteDistribution(); + } + + /// + /// 从权重创建离散分布 + /// + public static DiscreteDistribution CreateDiscrete(IEnumerable items, IEnumerable weights) where T : notnull + { + var dist = new DiscreteDistribution(); + var itemList = new List(items); + var weightList = new List(weights); + + if (itemList.Count != weightList.Count) + throw new ArgumentException("Items and weights must have the same length"); + + for (int i = 0; i < itemList.Count; i++) + { + dist.Add(itemList[i], weightList[i]); + } + + return dist; + } + + /// + /// 创建正态分布 + /// + public static NormalDistribution CreateNormal(double mean = 0, double stdDev = 1) + { + return new NormalDistribution(mean, stdDev); + } + + /// + /// 创建泊松分布 + /// + public static PoissonDistribution CreatePoisson(double lambda) + { + return new PoissonDistribution(lambda); + } + + /// + /// 创建指数分布 + /// + public static ExponentialDistribution CreateExponential(double rate) + { + return new ExponentialDistribution(rate); + } + + /// + /// 创建二项分布 + /// + public static BinomialDistribution CreateBinomial(int n, double p) + { + return new BinomialDistribution(n, p); + } + + /// + /// 创建几何分布 + /// + public static GeometricDistribution CreateGeometric(double p) + { + return new GeometricDistribution(p); + } + + /// + /// 创建均匀分布 + /// + public static UniformDistribution CreateUniform(double min, double max) + { + return new UniformDistribution(min, max); + } + + /// + /// 创建均匀整数分布 + /// + public static UniformIntDistribution CreateUniformInt(int min, int max) + { + return new UniformIntDistribution(min, max); + } + } + + /// + /// 离散概率分布 + /// + public class DiscreteDistribution where T : notnull + { + private readonly List _items; + private readonly List _cumulativeWeights; + private double _totalWeight; + private readonly Random _random; + + /// + /// 项目数量 + /// + public int Count => _items.Count; + + /// + /// 总权重 + /// + public double TotalWeight => _totalWeight; + + /// + /// 创建离散分布 + /// + public DiscreteDistribution() + { + _items = new List(); + _cumulativeWeights = new List(); + _totalWeight = 0; + _random = new Random(); + } + + /// + /// 添加项目 + /// + public void Add(T item, double weight) + { + if (weight < 0) + throw new ArgumentOutOfRangeException(nameof(weight), "Weight must be non-negative"); + + _items.Add(item); + _totalWeight += weight; + _cumulativeWeights.Add(_totalWeight); + } + + /// + /// 采样一个项目 + /// + public T Sample() + { + if (_items.Count == 0) + throw new InvalidOperationException("Distribution is empty"); + + double r = _random.NextDouble() * _totalWeight; + + int index = _cumulativeWeights.BinarySearch(r); + if (index < 0) + index = ~index; + + return _items[index]; + } + + /// + /// 采样多个项目 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 获取项目的概率 + /// + public double GetProbability(T item) + { + int index = _items.IndexOf(item); + if (index < 0) + return 0; + + double weight = index == 0 + ? _cumulativeWeights[0] + : _cumulativeWeights[index] - _cumulativeWeights[index - 1]; + + return weight / _totalWeight; + } + + /// + /// 清空分布 + /// + public void Clear() + { + _items.Clear(); + _cumulativeWeights.Clear(); + _totalWeight = 0; + } + } + + /// + /// 正态分布(高斯分布) + /// + public class NormalDistribution + { + private readonly double _mean; + private readonly double _stdDev; + private readonly Random _random; + private double? _spare; + + /// + /// 均值 + /// + public double Mean => _mean; + + /// + /// 标准差 + /// + public double StdDev => _stdDev; + + /// + /// 方差 + /// + public double Variance => _stdDev * _stdDev; + + /// + /// 创建正态分布 + /// + public NormalDistribution(double mean = 0, double stdDev = 1) + { + if (stdDev <= 0) + throw new ArgumentOutOfRangeException(nameof(stdDev), "Standard deviation must be positive"); + + _mean = mean; + _stdDev = stdDev; + _random = new Random(); + _spare = null; + } + + /// + /// 采样一个值(Box-Muller 变换) + /// + public double Sample() + { + if (_spare.HasValue) + { + double result = _spare.Value; + _spare = null; + return result; + } + + double u1, u2, s; + do + { + u1 = 2.0 * _random.NextDouble() - 1.0; + u2 = 2.0 * _random.NextDouble() - 1.0; + s = u1 * u1 + u2 * u2; + } while (s >= 1.0 || s == 0); + + double mul = Math.Sqrt(-2.0 * Math.Log(s) / s); + _spare = _mean + _stdDev * u2 * mul; + return _mean + _stdDev * u1 * mul; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + double exp = -0.5 * Math.Pow((x - _mean) / _stdDev, 2); + return Math.Exp(exp) / (_stdDev * Math.Sqrt(2 * Math.PI)); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + return 0.5 * (1 + Erf((x - _mean) / (_stdDev * Math.Sqrt(2)))); + } + + private static double Erf(double x) + { + // Abramowitz and Stegun approximation + double a1 = 0.254829592; + double a2 = -0.284496736; + double a3 = 1.421413741; + double a4 = -1.453152027; + double a5 = 1.061405429; + double p = 0.3275911; + + int sign = x >= 0 ? 1 : -1; + x = Math.Abs(x); + + double t = 1.0 / (1.0 + p * x); + double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x); + + return sign * y; + } + } + + /// + /// 泊松分布 + /// + public class PoissonDistribution + { + private readonly double _lambda; + private readonly Random _random; + + /// + /// Lambda 参数(期望值) + /// + public double Lambda => _lambda; + + /// + /// 均值 + /// + public double Mean => _lambda; + + /// + /// 方差 + /// + public double Variance => _lambda; + + /// + /// 创建泊松分布 + /// + public PoissonDistribution(double lambda) + { + if (lambda <= 0) + throw new ArgumentOutOfRangeException(nameof(lambda), "Lambda must be positive"); + + _lambda = lambda; + _random = new Random(); + } + + /// + /// 采样一个值(Knuth 算法) + /// + public int Sample() + { + double L = Math.Exp(-_lambda); + int k = 0; + double p = 1.0; + + do + { + k++; + p *= _random.NextDouble(); + } while (p > L); + + return k - 1; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0) + return 0; + return Math.Pow(_lambda, k) * Math.Exp(-_lambda) / Factorial(k); + } + + private static long Factorial(int n) + { + if (n <= 1) + return 1; + long result = 1; + for (int i = 2; i <= n; i++) + result *= i; + return result; + } + } + + /// + /// 指数分布 + /// + public class ExponentialDistribution + { + private readonly double _rate; + private readonly Random _random; + + /// + /// 速率参数(λ) + /// + public double Rate => _rate; + + /// + /// 均值 + /// + public double Mean => 1.0 / _rate; + + /// + /// 方差 + /// + public double Variance => 1.0 / (_rate * _rate); + + /// + /// 创建指数分布 + /// + public ExponentialDistribution(double rate) + { + if (rate <= 0) + throw new ArgumentOutOfRangeException(nameof(rate), "Rate must be positive"); + + _rate = rate; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public double Sample() + { + return -Math.Log(1 - _random.NextDouble()) / _rate; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + if (x < 0) + return 0; + return _rate * Math.Exp(-_rate * x); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + if (x < 0) + return 0; + return 1 - Math.Exp(-_rate * x); + } + } + + /// + /// 二项分布 + /// + public class BinomialDistribution + { + private readonly int _n; + private readonly double _p; + private readonly Random _random; + + /// + /// 试验次数 + /// + public int N => _n; + + /// + /// 成功概率 + /// + public double P => _p; + + /// + /// 均值 + /// + public double Mean => _n * _p; + + /// + /// 方差 + /// + public double Variance => _n * _p * (1 - _p); + + /// + /// 创建二项分布 + /// + public BinomialDistribution(int n, double p) + { + if (n <= 0) + throw new ArgumentOutOfRangeException(nameof(n), "N must be positive"); + if (p < 0 || p > 1) + throw new ArgumentOutOfRangeException(nameof(p), "P must be between 0 and 1"); + + _n = n; + _p = p; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public int Sample() + { + int successes = 0; + for (int i = 0; i < _n; i++) + { + if (_random.NextDouble() < _p) + successes++; + } + return successes; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0 || k > _n) + return 0; + return BinomialCoefficient(_n, k) * Math.Pow(_p, k) * Math.Pow(1 - _p, _n - k); + } + + private static long BinomialCoefficient(int n, int k) + { + if (k > n - k) + k = n - k; + + long result = 1; + for (int i = 0; i < k; i++) + { + result = result * (n - i) / (i + 1); + } + return result; + } + } + + /// + /// 几何分布 + /// + public class GeometricDistribution + { + private readonly double _p; + private readonly Random _random; + + /// + /// 成功概率 + /// + public double P => _p; + + /// + /// 均值 + /// + public double Mean => 1.0 / _p; + + /// + /// 方差 + /// + public double Variance => (1 - _p) / (_p * _p); + + /// + /// 创建几何分布 + /// + public GeometricDistribution(double p) + { + if (p <= 0 || p > 1) + throw new ArgumentOutOfRangeException(nameof(p), "P must be between 0 and 1"); + + _p = p; + _random = new Random(); + } + + /// + /// 采样一个值(返回第一次成功前的失败次数) + /// + public int Sample() + { + double u = _random.NextDouble(); + return (int)Math.Floor(Math.Log(1 - u) / Math.Log(1 - _p)); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0) + return 0; + return Math.Pow(1 - _p, k) * _p; + } + + /// + /// 累积分布函数 + /// + public double CDF(int k) + { + if (k < 0) + return 0; + return 1 - Math.Pow(1 - _p, k + 1); + } + } + + /// + /// 均匀分布(连续) + /// + public class UniformDistribution + { + private readonly double _min; + private readonly double _max; + private readonly Random _random; + + /// + /// 最小值 + /// + public double Min => _min; + + /// + /// 最大值 + /// + public double Max => _max; + + /// + /// 均值 + /// + public double Mean => (_min + _max) / 2; + + /// + /// 方差 + /// + public double Variance => (_max - _min) * (_max - _min) / 12; + + /// + /// 创建均匀分布 + /// + public UniformDistribution(double min, double max) + { + if (max <= min) + throw new ArgumentException("Max must be greater than min"); + + _min = min; + _max = max; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public double Sample() + { + return _min + _random.NextDouble() * (_max - _min); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + if (x < _min || x > _max) + return 0; + return 1.0 / (_max - _min); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + if (x < _min) + return 0; + if (x > _max) + return 1; + return (x - _min) / (_max - _min); + } + } + + /// + /// 均匀整数分布 + /// + public class UniformIntDistribution + { + private readonly int _min; + private readonly int _max; + private readonly Random _random; + + /// + /// 最小值(包含) + /// + public int Min => _min; + + /// + /// 最大值(包含) + /// + public int Max => _max; + + /// + /// 均值 + /// + public double Mean => (_min + _max) / 2.0; + + /// + /// 方差 + /// + public double Variance => ((_max - _min + 1) * (_max - _min + 1) - 1) / 12.0; + + /// + /// 创建均匀整数分布 + /// + public UniformIntDistribution(int min, int max) + { + if (max < min) + throw new ArgumentException("Max must be greater than or equal to min"); + + _min = min; + _max = max; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public int Sample() + { + return _random.Next(_min, _max + 1); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < _min || k > _max) + return 0; + return 1.0 / (_max - _min + 1); + } + + /// + /// 累积分布函数 + /// + public double CDF(int k) + { + if (k < _min) + return 0; + if (k > _max) + return 1; + return (double)(k - _min + 1) / (_max - _min + 1); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs b/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs new file mode 100644 index 0000000..57879fb --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树状数组(Fenwick Tree / Binary Indexed Tree)工具类 + /// 用于高效计算前缀和,支持单点更新 + /// 时间复杂度:查询和更新都是 O(log n) + /// + public static class FenwickTreeUtil + { + /// + /// 创建树状数组 + /// + /// 大小 + public static FenwickTree Create(int size) + { + return new FenwickTree(size); + } + + /// + /// 从数组创建树状数组 + /// + public static FenwickTree Create(long[] array) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + return new FenwickTree(array); + } + + /// + /// 创建支持范围更新的树状数组 + /// + public static FenwickTreeRange CreateRange(int size) + { + return new FenwickTreeRange(size); + } + + /// + /// 创建二维树状数组 + /// + public static FenwickTree2D Create2D(int rows, int cols) + { + return new FenwickTree2D(rows, cols); + } + } + + /// + /// 树状数组(Fenwick Tree) + /// + public class FenwickTree + { + private readonly long[] _tree; + private readonly int _size; + + /// + /// 大小 + /// + public int Size => _size; + + /// + /// 创建树状数组 + /// + public FenwickTree(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _tree = new long[size + 1]; + } + + /// + /// 从数组创建树状数组 + /// + public FenwickTree(long[] array) : this(array.Length) + { + for (int i = 0; i < array.Length; i++) + { + Update(i, array[i]); + } + } + + /// + /// 单点更新(增加值) + /// + /// 索引(0-based) + /// 增量 + public void Update(int index, long delta) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + index++; // 转为1-based + while (index <= _size) + { + _tree[index] += delta; + index += index & (-index); // LowBit + } + } + + /// + /// 设置指定位置的值 + /// + public void Set(int index, long value) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + long current = Query(index, index); + Update(index, value - current); + } + + /// + /// 查询前缀和 [0, index] + /// + public long Query(int index) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + index++; // 转为1-based + long sum = 0; + while (index > 0) + { + sum += _tree[index]; + index -= index & (-index); // LowBit + } + return sum; + } + + /// + /// 查询区间和 [left, right] + /// + public long Query(int left, int right) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + if (left == 0) + return Query(right); + return Query(right) - Query(left - 1); + } + + /// + /// 获取指定位置的值 + /// + public long Get(int index) + { + return Query(index, index); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_tree, 0, _tree.Length); + } + } + + /// + /// 支持范围更新的树状数组 + /// + public class FenwickTreeRange + { + private readonly FenwickTree _tree1; + private readonly FenwickTree _tree2; + private readonly int _size; + + /// + /// 大小 + /// + public int Size => _size; + + /// + /// 创建支持范围更新的树状数组 + /// + public FenwickTreeRange(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _tree1 = new FenwickTree(size); + _tree2 = new FenwickTree(size); + } + + /// + /// 区间更新 [left, right] 增加delta + /// + public void UpdateRange(int left, int right, long delta) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + _tree1.Update(left, delta); + _tree1.Update(right + 1, -delta); + _tree2.Update(left, delta * (left - 1)); + _tree2.Update(right + 1, -delta * right); + } + + /// + /// 单点更新 + /// + public void Update(int index, long delta) + { + UpdateRange(index, index, delta); + } + + /// + /// 查询前缀和 [0, index] + /// + public long Query(int index) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + return _tree1.Query(index) * index - _tree2.Query(index); + } + + /// + /// 查询区间和 [left, right] + /// + public long Query(int left, int right) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + if (left == 0) + return Query(right); + return Query(right) - Query(left - 1); + } + + /// + /// 清空 + /// + public void Clear() + { + _tree1.Clear(); + _tree2.Clear(); + } + } + + /// + /// 二维树状数组 + /// + public class FenwickTree2D + { + private readonly long[,] _tree; + private readonly int _rows; + private readonly int _cols; + + /// + /// 行数 + /// + public int Rows => _rows; + + /// + /// 列数 + /// + public int Cols => _cols; + + /// + /// 创建二维树状数组 + /// + public FenwickTree2D(int rows, int cols) + { + if (rows <= 0 || cols <= 0) + throw new ArgumentException("Rows and cols must be positive"); + + _rows = rows; + _cols = cols; + _tree = new long[rows + 1, cols + 1]; + } + + /// + /// 单点更新 + /// + public void Update(int row, int col, long delta) + { + if (row < 0 || row >= _rows || col < 0 || col >= _cols) + throw new ArgumentOutOfRangeException(); + + row++; col++; + for (int i = row; i <= _rows; i += i & (-i)) + { + for (int j = col; j <= _cols; j += j & (-j)) + { + _tree[i, j] += delta; + } + } + } + + /// + /// 查询前缀和 [(0,0), (row, col)] + /// + public long Query(int row, int col) + { + if (row < 0 || row >= _rows || col < 0 || col >= _cols) + throw new ArgumentOutOfRangeException(); + + row++; col++; + long sum = 0; + for (int i = row; i > 0; i -= i & (-i)) + { + for (int j = col; j > 0; j -= j & (-j)) + { + sum += _tree[i, j]; + } + } + return sum; + } + + /// + /// 查询矩形区域和 + /// + public long Query(int row1, int col1, int row2, int col2) + { + if (row1 < 0 || row2 >= _rows || row1 > row2 || + col1 < 0 || col2 >= _cols || col1 > col2) + throw new ArgumentException("Invalid range"); + + if (row1 == 0 && col1 == 0) + return Query(row2, col2); + if (row1 == 0) + return Query(row2, col2) - Query(row2, col1 - 1); + if (col1 == 0) + return Query(row2, col2) - Query(row1 - 1, col2); + + return Query(row2, col2) - Query(row1 - 1, col2) + - Query(row2, col1 - 1) + Query(row1 - 1, col1 - 1); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_tree, 0, _tree.Length); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/GraphUtil.cs b/EasyTool.Core/CollectionsCategory/GraphUtil.cs new file mode 100644 index 0000000..b4738cc --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/GraphUtil.cs @@ -0,0 +1,992 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 图工具类 + /// + public static class GraphUtil + { + /// + /// 创建图 + /// + public static Graph Create() where T : IEquatable + { + return new Graph(); + } + + /// + /// 创建有向图 + /// + public static Graph CreateDirected() where T : IEquatable + { + return new Graph(true); + } + } + + /// + /// 图实现 + /// + public class Graph where T : IEquatable + { + private readonly Dictionary> _adjacencyList; + private readonly bool _isDirected; + + /// + /// 顶点数量 + /// + public int VertexCount => _adjacencyList.Count; + + /// + /// 边数量 + /// + public int EdgeCount { get; private set; } + + /// + /// 是否为有向图 + /// + public bool IsDirected => _isDirected; + + /// + /// 所有顶点 + /// + public IEnumerable Vertices => _adjacencyList.Keys; + + /// + /// 创建图 + /// + public Graph() : this(false) + { + } + + /// + /// 创建图 + /// + public Graph(bool isDirected) + { + _adjacencyList = new Dictionary>(); + _isDirected = isDirected; + EdgeCount = 0; + } + + /// + /// 添加顶点 + /// + public void AddVertex(T vertex) + { + if (!_adjacencyList.ContainsKey(vertex)) + { + _adjacencyList[vertex] = new List(); + } + } + + /// + /// 添加边 + /// + public void AddEdge(T from, T to, double weight = 1) + { + AddVertex(from); + AddVertex(to); + + _adjacencyList[from].Add(new Edge(to, weight)); + EdgeCount++; + + if (!_isDirected) + { + _adjacencyList[to].Add(new Edge(from, weight)); + } + } + + /// + /// 移除顶点 + /// + public bool RemoveVertex(T vertex) + { + if (!_adjacencyList.ContainsKey(vertex)) + return false; + + int edgeCount = _adjacencyList[vertex].Count; + _adjacencyList.Remove(vertex); + EdgeCount -= edgeCount; + + // 移除所有指向该顶点的边 + foreach (var edges in _adjacencyList.Values) + { + int removed = edges.RemoveAll(e => e.Target.Equals(vertex)); + if (!_isDirected) + EdgeCount -= removed; + } + + return true; + } + + /// + /// 移除边 + /// + public bool RemoveEdge(T from, T to) + { + if (!_adjacencyList.TryGetValue(from, out var edges)) + return false; + + int removed = edges.RemoveAll(e => e.Target.Equals(to)); + if (removed > 0) + { + EdgeCount--; + + if (!_isDirected && _adjacencyList.TryGetValue(to, out var reverseEdges)) + { + reverseEdges.RemoveAll(e => e.Target.Equals(from)); + } + + return true; + } + + return false; + } + + /// + /// 获取邻居 + /// + public IEnumerable GetNeighbors(T vertex) + { + if (!_adjacencyList.TryGetValue(vertex, out var edges)) + return Enumerable.Empty(); + + return edges.Select(e => e.Target); + } + + /// + /// 获取边权重 + /// + public double GetEdgeWeight(T from, T to) + { + if (!_adjacencyList.TryGetValue(from, out var edges)) + return double.PositiveInfinity; + + var edge = edges.FirstOrDefault(e => e.Target.Equals(to)); + return edge?.Weight ?? double.PositiveInfinity; + } + + /// + /// 是否包含顶点 + /// + public bool ContainsVertex(T vertex) + { + return _adjacencyList.ContainsKey(vertex); + } + + /// + /// 是否包含边 + /// + public bool ContainsEdge(T from, T to) + { + if (!_adjacencyList.TryGetValue(from, out var edges)) + return false; + + return edges.Any(e => e.Target.Equals(to)); + } + + /// + /// 获取顶点的度 + /// + public int GetDegree(T vertex) + { + if (!_adjacencyList.TryGetValue(vertex, out var edges)) + return 0; + + return edges.Count; + } + + private class Edge + { + public T Target { get; } + public double Weight { get; } + + public Edge(T target, double weight) + { + Target = target; + Weight = weight; + } + } + } + + /// + /// 图遍历工具类 + /// + public static class GraphTraversalUtil + { + /// + /// 深度优先搜索 + /// + public static List DFS(Graph graph, T start) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + var result = new List(); + var visited = new HashSet(); + + DFSVisit(graph, start, visited, result); + + return result; + } + + private static void DFSVisit(Graph graph, T vertex, HashSet visited, List result) where T : IEquatable + { + if (visited.Contains(vertex)) + return; + + visited.Add(vertex); + result.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (!visited.Contains(neighbor)) + { + DFSVisit(graph, neighbor, visited, result); + } + } + } + + /// + /// 广度优先搜索 + /// + public static List BFS(Graph graph, T start) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + var result = new List(); + var visited = new HashSet(); + var queue = new Queue(); + + queue.Enqueue(start); + visited.Add(start); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + result.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + if (!visited.Contains(neighbor)) + { + visited.Add(neighbor); + queue.Enqueue(neighbor); + } + } + } + + return result; + } + + /// + /// 查找路径(BFS) + /// + public static List FindPath(Graph graph, T start, T end) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + if (!graph.ContainsVertex(start) || !graph.ContainsVertex(end)) + return null; + + var visited = new HashSet(); + var parent = new Dictionary(); + var queue = new Queue(); + + queue.Enqueue(start); + visited.Add(start); + parent[start] = default; + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + + if (current.Equals(end)) + { + return ReconstructPath(parent, start, end); + } + + foreach (var neighbor in graph.GetNeighbors(current)) + { + if (!visited.Contains(neighbor)) + { + visited.Add(neighbor); + parent[neighbor] = current; + queue.Enqueue(neighbor); + } + } + } + + return null; + } + + private static List ReconstructPath(Dictionary parent, T start, T end) + { + var path = new List(); + var current = end; + + while (!current.Equals(default)) + { + path.Add(current); + if (current.Equals(start)) + break; + current = parent[current]; + } + + path.Reverse(); + return path; + } + } + + /// + /// 拓扑排序工具类 + /// + public static class TopologicalSortUtil + { + /// + /// 拓扑排序 + /// + public static List Sort(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (!graph.IsDirected) + throw new ArgumentException("Topological sort requires a directed graph"); + + var inDegree = new Dictionary(); + foreach (var vertex in graph.Vertices) + { + inDegree[vertex] = 0; + } + + foreach (var vertex in graph.Vertices) + { + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + inDegree[neighbor]++; + } + } + + var queue = new Queue(); + foreach (var kvp in inDegree) + { + if (kvp.Value == 0) + { + queue.Enqueue(kvp.Key); + } + } + + var result = new List(); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + result.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + inDegree[neighbor]--; + if (inDegree[neighbor] == 0) + { + queue.Enqueue(neighbor); + } + } + } + + if (result.Count != graph.VertexCount) + { + throw new InvalidOperationException("Graph contains a cycle"); + } + + return result; + } + + /// + /// 尝试拓扑排序 + /// + public static bool TrySort(Graph graph, out List result) where T : IEquatable + { + try + { + result = Sort(graph); + return true; + } + catch + { + result = null; + return false; + } + } + } + + /// + /// 环检测工具类 + /// + public static class CycleDetectionUtil + { + /// + /// 检测是否有环 + /// + public static bool HasCycle(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + if (graph.IsDirected) + { + return HasCycleDirected(graph); + } + else + { + return HasCycleUndirected(graph); + } + } + + private static bool HasCycleDirected(Graph graph) where T : IEquatable + { + var white = new HashSet(graph.Vertices); // 未访问 + var gray = new HashSet(); // 正在访问 + var black = new HashSet(); // 已完成 + + foreach (var vertex in graph.Vertices) + { + if (white.Contains(vertex)) + { + if (DFSCycleDirected(graph, vertex, white, gray, black)) + return true; + } + } + + return false; + } + + private static bool DFSCycleDirected(Graph graph, T vertex, HashSet white, HashSet gray, HashSet black) where T : IEquatable + { + white.Remove(vertex); + gray.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (black.Contains(neighbor)) + continue; + + if (gray.Contains(neighbor)) + return true; + + if (DFSCycleDirected(graph, neighbor, white, gray, black)) + return true; + } + + gray.Remove(vertex); + black.Add(vertex); + return false; + } + + private static bool HasCycleUndirected(Graph graph) where T : IEquatable + { + var visited = new HashSet(); + + foreach (var vertex in graph.Vertices) + { + if (!visited.Contains(vertex)) + { + if (DFSCycleUndirected(graph, vertex, default, visited)) + return true; + } + } + + return false; + } + + private static bool DFSCycleUndirected(Graph graph, T vertex, T parent, HashSet visited) where T : IEquatable + { + visited.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (!visited.Contains(neighbor)) + { + if (DFSCycleUndirected(graph, neighbor, vertex, visited)) + return true; + } + else if (!neighbor.Equals(parent)) + { + return true; + } + } + + return false; + } + + /// + /// 查找环 + /// + public static List FindCycle(Graph graph) where T : IEquatable + { + if (graph == null || !graph.IsDirected) + return null; + + var visited = new HashSet(); + var recStack = new HashSet(); + var path = new List(); + + foreach (var vertex in graph.Vertices) + { + if (FindCycleDFS(graph, vertex, visited, recStack, path)) + { + return path; + } + } + + return null; + } + + private static bool FindCycleDFS(Graph graph, T vertex, HashSet visited, HashSet recStack, List path) where T : IEquatable + { + visited.Add(vertex); + recStack.Add(vertex); + path.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (!visited.Contains(neighbor)) + { + if (FindCycleDFS(graph, neighbor, visited, recStack, path)) + return true; + } + else if (recStack.Contains(neighbor)) + { + // 找到环,截取环部分 + int start = path.IndexOf(neighbor); + path.RemoveRange(0, start); + return true; + } + } + + recStack.Remove(vertex); + path.RemoveAt(path.Count - 1); + return false; + } + } + + /// + /// 连通分量工具类 + /// + public static class ConnectedComponentsUtil + { + /// + /// 获取连通分量 + /// + public static List> GetComponents(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + var visited = new HashSet(); + var components = new List>(); + + foreach (var vertex in graph.Vertices) + { + if (!visited.Contains(vertex)) + { + var component = new List(); + DFSComponent(graph, vertex, visited, component); + components.Add(component); + } + } + + return components; + } + + private static void DFSComponent(Graph graph, T vertex, HashSet visited, List component) where T : IEquatable + { + visited.Add(vertex); + component.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (!visited.Contains(neighbor)) + { + DFSComponent(graph, neighbor, visited, component); + } + } + } + + /// + /// 判断是否连通 + /// + public static bool IsConnected(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + + if (graph.VertexCount == 0) + return true; + + return GetComponents(graph).Count == 1; + } + + /// + /// 获取强连通分量(Kosaraju 算法) + /// + public static List> GetStronglyConnectedComponents(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (!graph.IsDirected) + throw new ArgumentException("Strongly connected components require a directed graph"); + + var visited = new HashSet(); + var finishOrder = new Stack(); + + // 第一次 DFS 获取完成顺序 + foreach (var vertex in graph.Vertices) + { + if (!visited.Contains(vertex)) + { + DFSOrder(graph, vertex, visited, finishOrder); + } + } + + // 构建转置图 + var transpose = Transpose(graph); + + // 第二次 DFS 按完成顺序的逆序 + visited.Clear(); + var components = new List>(); + + while (finishOrder.Count > 0) + { + var vertex = finishOrder.Pop(); + if (!visited.Contains(vertex)) + { + var component = new List(); + DFSComponent(transpose, vertex, visited, component); + components.Add(component); + } + } + + return components; + } + + private static void DFSOrder(Graph graph, T vertex, HashSet visited, Stack finishOrder) where T : IEquatable + { + visited.Add(vertex); + + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + if (!visited.Contains(neighbor)) + { + DFSOrder(graph, neighbor, visited, finishOrder); + } + } + + finishOrder.Push(vertex); + } + + private static Graph Transpose(Graph graph) where T : IEquatable + { + var transpose = new Graph(true); + + foreach (var vertex in graph.Vertices) + { + transpose.AddVertex(vertex); + } + + foreach (var vertex in graph.Vertices) + { + foreach (var neighbor in graph.GetNeighbors(vertex)) + { + transpose.AddEdge(neighbor, vertex); + } + } + + return transpose; + } + } + + /// + /// 最短路径工具类 + /// + public static class ShortestPathUtil + { + /// + /// Dijkstra 最短路径算法 + /// + public static Dictionary Dijkstra(Graph graph, T start) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (!graph.ContainsVertex(start)) + throw new ArgumentException("Start vertex not found"); + + var distances = new Dictionary(); + var visited = new HashSet(); + var pq = PriorityQueueUtil.CreateMin(); + + foreach (var vertex in graph.Vertices) + { + distances[vertex] = double.PositiveInfinity; + } + distances[start] = 0; + pq.Enqueue(start, 0); + + while (pq.Count > 0) + { + var current = pq.Dequeue(); + + if (visited.Contains(current)) + continue; + + visited.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + var weight = graph.GetEdgeWeight(current, neighbor); + var newDist = distances[current] + weight; + + if (newDist < distances[neighbor]) + { + distances[neighbor] = newDist; + pq.Enqueue(neighbor, newDist); + } + } + } + + return distances; + } + + /// + /// Dijkstra 最短路径(带路径) + /// + public static (Dictionary Distances, Dictionary Previous) DijkstraWithPath(Graph graph, T start) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (!graph.ContainsVertex(start)) + throw new ArgumentException("Start vertex not found"); + + var distances = new Dictionary(); + var previous = new Dictionary(); + var visited = new HashSet(); + var pq = PriorityQueueUtil.CreateMin(); + + foreach (var vertex in graph.Vertices) + { + distances[vertex] = double.PositiveInfinity; + } + distances[start] = 0; + pq.Enqueue(start, 0); + + while (pq.Count > 0) + { + var current = pq.Dequeue(); + + if (visited.Contains(current)) + continue; + + visited.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + var weight = graph.GetEdgeWeight(current, neighbor); + var newDist = distances[current] + weight; + + if (newDist < distances[neighbor]) + { + distances[neighbor] = newDist; + previous[neighbor] = current; + pq.Enqueue(neighbor, newDist); + } + } + } + + return (distances, previous); + } + + /// + /// 重建路径 + /// + public static List ReconstructPath(Dictionary previous, T start, T end) + { + var path = new List(); + var current = end; + + while (!current.Equals(default)) + { + path.Add(current); + if (current.Equals(start)) + break; + + if (!previous.ContainsKey(current)) + return null; // 无法到达 + + current = previous[current]; + } + + path.Reverse(); + return path.Count > 0 && path[0].Equals(start) ? path : null; + } + + /// + /// Bellman-Ford 算法(支持负权边) + /// + public static Dictionary BellmanFord(Graph graph, T start) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (!graph.ContainsVertex(start)) + throw new ArgumentException("Start vertex not found"); + + var distances = new Dictionary(); + + foreach (var vertex in graph.Vertices) + { + distances[vertex] = double.PositiveInfinity; + } + distances[start] = 0; + + // 松弛 V-1 次 + for (int i = 0; i < graph.VertexCount - 1; i++) + { + foreach (var u in graph.Vertices) + { + if (distances[u] == double.PositiveInfinity) + continue; + + foreach (var v in graph.GetNeighbors(u)) + { + var weight = graph.GetEdgeWeight(u, v); + if (distances[u] + weight < distances[v]) + { + distances[v] = distances[u] + weight; + } + } + } + } + + // 检查负环 + foreach (var u in graph.Vertices) + { + if (distances[u] == double.PositiveInfinity) + continue; + + foreach (var v in graph.GetNeighbors(u)) + { + var weight = graph.GetEdgeWeight(u, v); + if (distances[u] + weight < distances[v]) + { + throw new InvalidOperationException("Graph contains a negative cycle"); + } + } + } + + return distances; + } + } + + /// + /// 最小生成树工具类 + /// + public static class MSTUtil + { + /// + /// Prim 算法 + /// + public static List<(T From, T To, double Weight)> Prim(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (graph.IsDirected) + throw new ArgumentException("MST requires an undirected graph"); + if (graph.VertexCount == 0) + return new List<(T, T, double)>(); + + var mst = new List<(T From, T To, double Weight)>(); + var visited = new HashSet(); + var pq = PriorityQueueUtil.CreateMin<(T From, T To), double>(); + + var startVertex = graph.Vertices.First(); + visited.Add(startVertex); + + foreach (var neighbor in graph.GetNeighbors(startVertex)) + { + var weight = graph.GetEdgeWeight(startVertex, neighbor); + pq.Enqueue((startVertex, neighbor), weight); + } + + while (pq.Count > 0 && visited.Count < graph.VertexCount) + { + var (from, to) = pq.Dequeue(); + + if (visited.Contains(to)) + continue; + + visited.Add(to); + var weight = graph.GetEdgeWeight(from, to); + mst.Add((from, to, weight)); + + foreach (var neighbor in graph.GetNeighbors(to)) + { + if (!visited.Contains(neighbor)) + { + var neighborWeight = graph.GetEdgeWeight(to, neighbor); + pq.Enqueue((to, neighbor), neighborWeight); + } + } + } + + return mst; + } + + /// + /// Kruskal 算法 + /// + public static List<(T From, T To, double Weight)> Kruskal(Graph graph) where T : IEquatable + { + if (graph == null) + throw new ArgumentNullException(nameof(graph)); + if (graph.IsDirected) + throw new ArgumentException("MST requires an undirected graph"); + + var mst = new List<(T From, T To, double Weight)>(); + var edges = new List<(T From, T To, double Weight)>(); + var processed = new HashSet<(T, T)>(); + + foreach (var from in graph.Vertices) + { + foreach (var to in graph.GetNeighbors(from)) + { + var key = from.GetHashCode() < to.GetHashCode() ? (from, to) : (to, from); + if (!processed.Contains(key)) + { + processed.Add(key); + var weight = graph.GetEdgeWeight(from, to); + edges.Add((from, to, weight)); + } + } + } + + edges.Sort((a, b) => a.Weight.CompareTo(b.Weight)); + + var uf = UnionFindUtil.Create(graph.Vertices.ToList()); + + foreach (var edge in edges) + { + if (!uf.Connected(edge.From, edge.To)) + { + uf.Union(edge.From, edge.To); + mst.Add(edge); + } + } + + return mst; + } + } + +} diff --git a/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs b/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs new file mode 100644 index 0000000..1f45265 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs @@ -0,0 +1,419 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Heavy Keeper 工具类 + /// 用于检测数据流中的 Heavy Hitters(高频元素) + /// 基于概率衰减的计数器,适合实时数据流分析 + /// + public static class HeavyKeeperUtil + { + /// + /// 创建 Heavy Keeper + /// + /// 宽度(哈希桶数量) + /// 深度(哈希函数数量) + /// 衰减因子(0-1) + public static HeavyKeeper Create(int width = 1000, int depth = 5, double decay = 0.9) + { + return new HeavyKeeper(width, depth, decay); + } + } + + /// + /// Heavy Keeper 实现 + /// + public class HeavyKeeper + { + private readonly int _width; + private readonly int _depth; + private readonly double _decay; + private readonly ulong[,] _counters; + private readonly ulong[,] _fingerprints; + private readonly int[] _seeds; + private ulong _totalCount; + + /// + /// 宽度 + /// + public int Width => _width; + + /// + /// 深度 + /// + public int Depth => _depth; + + /// + /// 衰减因子 + /// + public double Decay => _decay; + + /// + /// 总计数 + /// + public ulong TotalCount => _totalCount; + + /// + /// 创建 Heavy Keeper + /// + public HeavyKeeper(int width, int depth, double decay = 0.9) + { + if (width <= 0) + throw new ArgumentOutOfRangeException(nameof(width)); + if (depth <= 0) + throw new ArgumentOutOfRangeException(nameof(depth)); + if (decay <= 0 || decay >= 1) + throw new ArgumentOutOfRangeException(nameof(decay), "Decay must be between 0 and 1"); + + _width = width; + _depth = depth; + _decay = decay; + _counters = new ulong[depth, width]; + _fingerprints = new ulong[depth, width]; + _seeds = new int[depth]; + _totalCount = 0; + + var random = new Random(12345); + for (int i = 0; i < depth; i++) + { + _seeds[i] = random.Next(); + } + } + + /// + /// 添加元素 + /// + /// 估计的频率 + public ulong Add(byte[] data) + { + ulong fingerprint = ComputeFingerprint(data); + ulong maxCount = 0; + + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + + if (_fingerprints[i, index] == fingerprint) + { + // 匹配,增加计数 + _counters[i, index]++; + if (_counters[i, index] > maxCount) + maxCount = _counters[i, index]; + } + else if (_counters[i, index] == 0) + { + // 空桶,直接放入 + _fingerprints[i, index] = fingerprint; + _counters[i, index] = 1; + if (maxCount == 0) maxCount = 1; + } + else + { + // 不匹配,以一定概率衰减并替换 + double probability = Math.Pow(_decay, _counters[i, index]); + if (RandomDouble() < probability) + { + _counters[i, index]--; + if (_counters[i, index] == 0) + { + _fingerprints[i, index] = fingerprint; + _counters[i, index] = 1; + if (maxCount == 0) maxCount = 1; + } + } + } + } + + _totalCount++; + return maxCount; + } + + /// + /// 添加字符串 + /// + public ulong Add(string value) + { + return Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public ulong Add(int value) + { + return Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计元素频率 + /// + public ulong Estimate(byte[] data) + { + ulong fingerprint = ComputeFingerprint(data); + ulong maxCount = 0; + + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + + if (_fingerprints[i, index] == fingerprint) + { + if (_counters[i, index] > maxCount) + maxCount = _counters[i, index]; + } + } + + return maxCount; + } + + /// + /// 估计字符串频率 + /// + public ulong Estimate(string value) + { + return Estimate(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 获取 Top-K 高频元素 + /// + public List<(T Item, ulong Count)> GetTopK(IEnumerable items, int k) + { + var counts = new Dictionary(); + + foreach (var item in items) + { + byte[] data; + if (typeof(T) == typeof(string)) + data = System.Text.Encoding.UTF8.GetBytes(item.ToString()); + else if (typeof(T) == typeof(int)) + data = BitConverter.GetBytes(Convert.ToInt32(item)); + else + data = System.Text.Encoding.UTF8.GetBytes(item.ToString()); + + ulong count = Estimate(data); + if (count > 0) + { + counts[item] = count; + } + } + + return counts.OrderByDescending(x => x.Value) + .Take(k) + .Select(x => (x.Key, x.Value)) + .ToList(); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + Array.Clear(_fingerprints, 0, _fingerprints.Length); + _totalCount = 0; + } + + private static ulong ComputeFingerprint(byte[] data) + { + unchecked + { + ulong hash = 14695981039346656037; + foreach (byte b in data) + { + hash ^= b; + hash *= 1099511628211; + } + return hash; + } + } + + private static int Hash(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + + private static double RandomDouble() + { +#if NETSTANDARD2_1 + return _random.NextDouble(); +#else + return Random.Shared.NextDouble(); +#endif + } + + private static readonly Random _random = new Random(); + } + + /// + /// 流式 Top-K 工具 + /// 使用最小堆维护 Top-K 元素 + /// + public class StreamTopK + { + private readonly int _k; + private readonly Dictionary _counts; + private readonly HeavyKeeperPriorityQueue _minHeap; + + /// + /// K值 + /// + public int K => _k; + + /// + /// 当前元素数量 + /// + public int Count => _counts.Count; + + /// + /// 创建流式 Top-K + /// + public StreamTopK(int k) + { + if (k <= 0) + throw new ArgumentOutOfRangeException(nameof(k)); + + _k = k; + _counts = new Dictionary(); + _minHeap = new HeavyKeeperPriorityQueue(); + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (_counts.ContainsKey(item)) + { + _counts[item]++; + } + else + { + _counts[item] = 1; + } + } + + /// + /// 获取当前 Top-K + /// + public List<(T Item, ulong Count)> GetTopK() + { + var heap = new HeavyKeeperPriorityQueue(); + foreach (var kvp in _counts) + { + if (heap.Count < _k) + { + heap.Enqueue(kvp.Key, kvp.Value); + } + else if (kvp.Value > heap.Peek().Priority) + { + heap.Dequeue(); + heap.Enqueue(kvp.Key, kvp.Value); + } + } + + var result = new List<(T, ulong)>(); + while (heap.Count > 0) + { + var item = heap.Dequeue(); + result.Add((item.Element, item.Priority)); + } + + result.Reverse(); + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _counts.Clear(); + _minHeap.Clear(); + } + } + + // 内部使用的优先队列元素 + internal struct PriorityQueueElement + { + public T Element { get; } + public ulong Value { get; } + + public PriorityQueueElement(T element, ulong value) + { + Element = element; + Value = value; + } + } + + // 简单的优先队列实现(内部使用,避免与 PriorityQueueUtil 中的 PriorityQueue 冲突) + internal class HeavyKeeperPriorityQueue where TPriority : IComparable + { + private readonly List<(T Element, TPriority Priority)> _heap = new(); + + public int Count => _heap.Count; + + public void Enqueue(T element, TPriority priority) + { + _heap.Add((element, priority)); + int i = _heap.Count - 1; + while (i > 0) + { + int parent = (i - 1) / 2; + if (_heap[parent].Priority.CompareTo(priority) <= 0) break; + _heap[i] = _heap[parent]; + i = parent; + } + _heap[i] = (element, priority); + } + + public (T Element, TPriority Priority) Dequeue() + { + if (_heap.Count == 0) throw new InvalidOperationException("Queue is empty"); + var result = _heap[0]; + var last = _heap[_heap.Count - 1]; + _heap.RemoveAt(_heap.Count - 1); + + if (_heap.Count > 0) + { + int i = 0; + while (true) + { + int left = 2 * i + 1; + if (left >= _heap.Count) break; + int right = left + 1; + int smallest = left; + if (right < _heap.Count && _heap[right].Priority.CompareTo(_heap[left].Priority) < 0) + smallest = right; + if (last.Priority.CompareTo(_heap[smallest].Priority) <= 0) break; + _heap[i] = _heap[smallest]; + i = smallest; + } + _heap[i] = last; + } + + return result; + } + + public (T Element, TPriority Priority) Peek() + { + if (_heap.Count == 0) throw new InvalidOperationException("Queue is empty"); + return _heap[0]; + } + + public void Clear() => _heap.Clear(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs index 34a7875..2f8a71b 100644 --- a/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs +++ b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs @@ -263,6 +263,13 @@ public static IEnumerable Page(this IEnumerable source, int pageIndex, #region 随机操作 +#if NET6_0_OR_GREATER + private static System.Random GetSharedRandom() => System.Random.Shared; +#else + private static readonly System.Threading.ThreadLocal ThreadLocalRandom = new(() => new System.Random(Guid.NewGuid().GetHashCode())); + private static System.Random GetSharedRandom() => ThreadLocalRandom.Value!; +#endif + /// /// 随机排序 /// @@ -271,8 +278,8 @@ public static IEnumerable Shuffle(this IEnumerable source) if (source == null) yield break; - var random = new Random(); var list = source.ToList(); + var random = GetSharedRandom(); for (int i = list.Count - 1; i > 0; i--) { @@ -298,7 +305,7 @@ public static T Random(this IEnumerable source) if (list.Count == 0) return default; - var random = new Random(); + var random = GetSharedRandom(); return list[random.Next(list.Count)]; } @@ -315,7 +322,7 @@ public static IEnumerable RandomTake(this IEnumerable source, int count yield break; count = Math.Min(count, list.Count); - var random = new Random(); + var random = GetSharedRandom(); var selected = new HashSet(); while (selected.Count < count) diff --git a/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs b/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs new file mode 100644 index 0000000..5a80107 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs @@ -0,0 +1,233 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// LRU 缓存工具类 + /// 最近最少使用淘汰策略的缓存 + /// + public static class LRUCacheUtil + { + /// + /// 创建 LRU 缓存 + /// + /// 键类型 + /// 值类型 + /// 容量 + /// LRU 缓存实例 + public static LRUCache Create(int capacity) + where TKey : notnull + { + return new LRUCache(capacity); + } + } + + /// + /// LRU 缓存实现 + /// + /// 键类型 + /// 值类型 + public class LRUCache where TKey : notnull + { + private readonly int _capacity; + private readonly Dictionary> _cache; + private readonly LinkedList _lruList; + + /// + /// 当前缓存数量 + /// + public int Count => _cache.Count; + + /// + /// 缓存容量 + /// + public int Capacity => _capacity; + + /// + /// 缓存命中率 + /// + public double HitRate => _totalRequests == 0 ? 0 : (double)_hits / _totalRequests; + + private long _hits; + private long _totalRequests; + + /// + /// 创建 LRU 缓存 + /// + /// 容量 + public LRUCache(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0"); + + _capacity = capacity; + _cache = new Dictionary>(); + _lruList = new LinkedList(); + } + + /// + /// 获取或设置缓存值 + /// + public TValue this[TKey key] + { + get => Get(key); + set => Put(key, value); + } + + /// + /// 获取缓存值 + /// + public TValue Get(TKey key) + { + _totalRequests++; + + if (_cache.TryGetValue(key, out var node)) + { + _hits++; + // 移动到链表头部(最近使用) + _lruList.Remove(node); + _lruList.AddFirst(node); + return node.Value.Value; + } + + throw new KeyNotFoundException($"Key '{key}' not found in cache"); + } + + /// + /// 尝试获取缓存值 + /// + public bool TryGet(TKey key, out TValue value) + { + _totalRequests++; + + if (_cache.TryGetValue(key, out var node)) + { + _hits++; + _lruList.Remove(node); + _lruList.AddFirst(node); + value = node.Value.Value; + return true; + } + + value = default; + return false; + } + + /// + /// 获取缓存值,不存在则通过工厂创建并缓存 + /// + public TValue GetOrAdd(TKey key, Func factory) + { + if (TryGet(key, out var value)) + return value; + + value = factory(key); + Put(key, value); + return value; + } + + /// + /// 添加或更新缓存 + /// + public void Put(TKey key, TValue value) + { + if (_cache.TryGetValue(key, out var existingNode)) + { + // 更新已存在的键 + _lruList.Remove(existingNode); + existingNode.Value.Value = value; + _lruList.AddFirst(existingNode); + } + else + { + // 添加新键 + if (_cache.Count >= _capacity) + { + // 淘汰最久未使用的项 + var last = _lruList.Last; + _lruList.RemoveLast(); + _cache.Remove(last.Value.Key); + } + + var cacheItem = new CacheItem { Key = key, Value = value }; + var node = _lruList.AddFirst(cacheItem); + _cache[key] = node; + } + } + + /// + /// 移除缓存 + /// + public bool Remove(TKey key) + { + if (_cache.TryGetValue(key, out var node)) + { + _lruList.Remove(node); + _cache.Remove(key); + return true; + } + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _cache.ContainsKey(key); + } + + /// + /// 清空缓存 + /// + public void Clear() + { + _cache.Clear(); + _lruList.Clear(); + _hits = 0; + _totalRequests = 0; + } + + /// + /// 获取所有键 + /// + public IEnumerable GetKeys() + { + var node = _lruList.First; + while (node != null) + { + yield return node.Value.Key; + node = node.Next; + } + } + + /// + /// 获取所有值(按LRU顺序) + /// + public IEnumerable GetValues() + { + var node = _lruList.First; + while (node != null) + { + yield return node.Value.Value; + node = node.Next; + } + } + + /// + /// 重置统计信息 + /// + public void ResetStatistics() + { + _hits = 0; + _totalRequests = 0; + } + + private class CacheItem + { + public TKey Key { get; set; } + public TValue Value { get; set; } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/MatrixUtil.cs b/EasyTool.Core/CollectionsCategory/MatrixUtil.cs new file mode 100644 index 0000000..3786fb0 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MatrixUtil.cs @@ -0,0 +1,363 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 矩阵工具类 + /// 提供二维矩阵的常用操作 + /// + public static class MatrixUtil + { + /// + /// 创建矩阵 + /// + public static Matrix Create(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 从二维数组创建矩阵 + /// + public static Matrix FromArray(T[,] array) + { + return new Matrix(array); + } + + /// + /// 创建全零矩阵 + /// + public static Matrix Zeros(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 创建全一矩阵 + /// + public static Matrix Ones(int rows, int cols) + { + var matrix = new Matrix(rows, cols); + for (int i = 0; i < rows; i++) + for (int j = 0; j < cols; j++) + matrix[i, j] = 1; + return matrix; + } + + /// + /// 创建单位矩阵 + /// + public static Matrix Identity(int size) + { + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + matrix[i, i] = 1; + return matrix; + } + + /// + /// 创建对角矩阵 + /// + public static Matrix Diagonal(T[] diagonal) + { + int n = diagonal.Length; + var matrix = new Matrix(n, n); + for (int i = 0; i < n; i++) + matrix[i, i] = diagonal[i]; + return matrix; + } + } + + /// + /// 矩阵实现 + /// + /// 元素类型 + public class Matrix + { + private readonly T[,] _data; + + /// + /// 行数 + /// + public int Rows { get; } + + /// + /// 列数 + /// + public int Columns { get; } + + /// + /// 元素总数 + /// + public int Length => Rows * Columns; + + /// + /// 访问元素 + /// + public T this[int row, int col] + { + get + { + ValidateIndex(row, col); + return _data[row, col]; + } + set + { + ValidateIndex(row, col); + _data[row, col] = value; + } + } + + /// + /// 创建矩阵 + /// + public Matrix(int rows, int cols) + { + if (rows <= 0 || cols <= 0) + throw new ArgumentOutOfRangeException("Rows and columns must be positive"); + + Rows = rows; + Columns = cols; + _data = new T[rows, cols]; + } + + /// + /// 从二维数组创建矩阵 + /// + public Matrix(T[,] array) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + + Rows = array.GetLength(0); + Columns = array.GetLength(1); + _data = new T[Rows, Columns]; + Array.Copy(array, _data, array.Length); + } + + /// + /// 获取行 + /// + public T[] GetRow(int row) + { + if (row < 0 || row >= Rows) + throw new ArgumentOutOfRangeException(nameof(row)); + + var result = new T[Columns]; + for (int i = 0; i < Columns; i++) + result[i] = _data[row, i]; + return result; + } + + /// + /// 获取列 + /// + public T[] GetColumn(int col) + { + if (col < 0 || col >= Columns) + throw new ArgumentOutOfRangeException(nameof(col)); + + var result = new T[Rows]; + for (int i = 0; i < Rows; i++) + result[i] = _data[i, col]; + return result; + } + + /// + /// 设置行 + /// + public void SetRow(int row, T[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != Columns) + throw new ArgumentException("Values length must match column count"); + + for (int i = 0; i < Columns; i++) + _data[row, i] = values[i]; + } + + ///
+ /// 设置列 + /// + public void SetColumn(int col, T[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != Rows) + throw new ArgumentException("Values length must match row count"); + + for (int i = 0; i < Rows; i++) + _data[i, col] = values[i]; + } + + /// + /// 转置 + /// + public Matrix Transpose() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[j, i] = _data[i, j]; + return result; + } + + /// + /// 翻转行 + /// + public Matrix FlipVertical() + { + var result = new Matrix(Rows, Columns); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[Rows - 1 - i, j] = _data[i, j]; + return result; + } + + /// + /// 翻转列 + /// + public Matrix FlipHorizontal() + { + var result = new Matrix(Rows, Columns); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[i, Columns - 1 - j] = _data[i, j]; + return result; + } + + /// + /// 顺时针旋转90度 + /// + public Matrix Rotate90() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[j, Rows - 1 - i] = _data[i, j]; + return result; + } + + /// + /// 旋转180度 + /// + public Matrix Rotate180() + { + return FlipVertical().FlipHorizontal(); + } + + /// + /// 逆时针旋转90度 + /// + public Matrix Rotate270() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[Columns - 1 - j, i] = _data[i, j]; + return result; + } + + /// + /// 获取子矩阵 + /// + public Matrix SubMatrix(int startRow, int startCol, int rows, int cols) + { + if (startRow < 0 || startCol < 0 || startRow + rows > Rows || startCol + cols > Columns) + throw new ArgumentOutOfRangeException(); + + var result = new Matrix(rows, cols); + for (int i = 0; i < rows; i++) + for (int j = 0; j < cols; j++) + result[i, j] = _data[startRow + i, startCol + j]; + return result; + } + + /// + /// 克隆矩阵 + /// + public Matrix Clone() + { + return new Matrix(_data); + } + + /// + /// 转换为二维数组 + /// + public T[,] ToArray() + { + var result = new T[Rows, Columns]; + Array.Copy(_data, result, _data.Length); + return result; + } + + /// + /// 转换为交错数组 + /// + public T[][] ToJaggedArray() + { + var result = new T[Rows][]; + for (int i = 0; i < Rows; i++) + { + result[i] = new T[Columns]; + for (int j = 0; j < Columns; j++) + result[i][j] = _data[i, j]; + } + return result; + } + + /// + /// 展平为一维数组 + /// + public T[] Flatten() + { + var result = new T[Length]; + int index = 0; + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[index++] = _data[i, j]; + return result; + } + + /// + /// 遍历所有元素 + /// + public IEnumerable Enumerate() + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + yield return _data[i, j]; + } + + /// + /// 填充所有元素 + /// + public void Fill(T value) + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + _data[i, j] = value; + } + + /// + /// 使用函数填充 + /// + public void Fill(Func generator) + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + _data[i, j] = generator(i, j); + } + + private void ValidateIndex(int row, int col) + { + if (row < 0 || row >= Rows) + throw new ArgumentOutOfRangeException(nameof(row)); + if (col < 0 || col >= Columns) + throw new ArgumentOutOfRangeException(nameof(col)); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs b/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs new file mode 100644 index 0000000..86fbf88 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs @@ -0,0 +1,747 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 复合字典工具类 + /// + public static class MultiKeyDictionaryUtil + { + /// + /// 创建双键字典 + /// + public static TwoKeyDictionary CreateTwoKey() + where TKey1 : notnull + where TKey2 : notnull + { + return new TwoKeyDictionary(); + } + + /// + /// 创建复合键字典 + /// + public static CompositeKeyDictionary CreateComposite() + where TKey : notnull + { + return new CompositeKeyDictionary(); + } + + /// + /// 创建区间映射 + /// + public static RangeMap CreateRangeMap() where T : IComparable + { + return new RangeMap(); + } + } + + /// + /// 双键字典 + /// 通过两个键可以分别查找值 + /// + public class TwoKeyDictionary + where TKey1 : notnull + where TKey2 : notnull + { + private readonly Dictionary> _data; + private readonly Dictionary> _reverseData; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 第一个键的集合 + /// + public ICollection Keys1 => _data.Keys; + + /// + /// 第二个键的集合 + /// + public ICollection Keys2 => _reverseData.Keys; + + /// + /// 通过第一个键访问 + /// + public Dictionary this[TKey1 key1] + { + get + { + if (!_data.TryGetValue(key1, out var inner)) + { + inner = new Dictionary(); + _data[key1] = inner; + } + return inner; + } + } + + /// + /// 创建双键字典 + /// + public TwoKeyDictionary() + { + _data = new Dictionary>(); + _reverseData = new Dictionary>(); + _count = 0; + } + + /// + /// 添加元素 + /// + public void Add(TKey1 key1, TKey2 key2, TValue value) + { + if (!_data.TryGetValue(key1, out var inner1)) + { + inner1 = new Dictionary(); + _data[key1] = inner1; + } + + if (!inner1.ContainsKey(key2)) + { + _count++; + } + + inner1[key2] = value; + + if (!_reverseData.TryGetValue(key2, out var inner2)) + { + inner2 = new Dictionary(); + _reverseData[key2] = inner2; + } + inner2[key1] = value; + } + + /// + /// 通过双键获取值 + /// + public bool TryGetValue(TKey1 key1, TKey2 key2, out TValue value) + { + value = default; + if (!_data.TryGetValue(key1, out var inner)) + return false; + return inner.TryGetValue(key2, out value); + } + + /// + /// 通过双键获取值 + /// + public TValue GetValue(TKey1 key1, TKey2 key2) + { + if (!TryGetValue(key1, key2, out var value)) + throw new KeyNotFoundException($"Key pair ({key1}, {key2}) not found"); + return value; + } + + /// + /// 通过第一个键获取所有值 + /// + public bool TryGetByKey1(TKey1 key1, out Dictionary values) + { + return _data.TryGetValue(key1, out values); + } + + /// + /// 通过第二个键获取所有值 + /// + public bool TryGetByKey2(TKey2 key2, out Dictionary values) + { + return _reverseData.TryGetValue(key2, out values); + } + + /// + /// 移除元素 + /// + public bool Remove(TKey1 key1, TKey2 key2) + { + if (!_data.TryGetValue(key1, out var inner1)) + return false; + + if (!inner1.Remove(key2)) + return false; + + _count--; + + if (inner1.Count == 0) + _data.Remove(key1); + + if (_reverseData.TryGetValue(key2, out var inner2)) + { + inner2.Remove(key1); + if (inner2.Count == 0) + _reverseData.Remove(key2); + } + + return true; + } + + /// + /// 移除第一个键的所有元素 + /// + public bool RemoveByKey1(TKey1 key1) + { + if (!_data.TryGetValue(key1, out var inner)) + return false; + + _count -= inner.Count; + + foreach (var key2 in inner.Keys) + { + if (_reverseData.TryGetValue(key2, out var reverseInner)) + { + reverseInner.Remove(key1); + if (reverseInner.Count == 0) + _reverseData.Remove(key2); + } + } + + _data.Remove(key1); + return true; + } + + /// + /// 移除第二个键的所有元素 + /// + public bool RemoveByKey2(TKey2 key2) + { + if (!_reverseData.TryGetValue(key2, out var inner)) + return false; + + _count -= inner.Count; + + foreach (var key1 in inner.Keys) + { + if (_data.TryGetValue(key1, out var forwardInner)) + { + forwardInner.Remove(key2); + if (forwardInner.Count == 0) + _data.Remove(key1); + } + } + + _reverseData.Remove(key2); + return true; + } + + /// + /// 是否包含键对 + /// + public bool ContainsKey(TKey1 key1, TKey2 key2) + { + return _data.TryGetValue(key1, out var inner) && inner.ContainsKey(key2); + } + + /// + /// 是否包含第一个键 + /// + public bool ContainsKey1(TKey1 key1) + { + return _data.ContainsKey(key1); + } + + /// + /// 是否包含第二个键 + /// + public bool ContainsKey2(TKey2 key2) + { + return _reverseData.ContainsKey(key2); + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + _reverseData.Clear(); + _count = 0; + } + + /// + /// 获取所有键值对 + /// + public IEnumerable<(TKey1 Key1, TKey2 Key2, TValue Value)> GetAll() + { + foreach (var kvp1 in _data) + { + foreach (var kvp2 in kvp1.Value) + { + yield return (kvp1.Key, kvp2.Key, kvp2.Value); + } + } + } + } + + /// + /// 复合键字典 + /// 使用多个键组成的元组作为键 + /// + public class CompositeKeyDictionary where TKey : notnull + { + private readonly Dictionary _data; + private readonly IEqualityComparer _keyComparer; + private readonly List>> _indexes; + + /// + /// 元素数量 + /// + public int Count => _data.Count; + + /// + /// 创建复合键字典 + /// + public CompositeKeyDictionary() + { + _data = new Dictionary(new ArrayEqualityComparer()); + _keyComparer = EqualityComparer.Default; + _indexes = new List>>(); + } + + /// + /// 创建具有指定键数量的复合键字典 + /// + public CompositeKeyDictionary(int keyCount) : this() + { + for (int i = 0; i < keyCount; i++) + { + _indexes.Add(new Dictionary>()); + } + } + + /// + /// 添加元素 + /// + public void Add(TValue value, params TKey[] keys) + { + if (keys == null || keys.Length == 0) + throw new ArgumentException("At least one key is required"); + + _data[keys] = value; + + // 建立索引 + while (_indexes.Count < keys.Length) + { + _indexes.Add(new Dictionary>()); + } + + for (int i = 0; i < keys.Length; i++) + { + var index = _indexes[i]; + if (!index.TryGetValue(keys[i], out var list)) + { + list = new List(); + index[keys[i]] = list; + } + list.Add(keys); + } + } + + /// + /// 获取值 + /// + public bool TryGetValue(out TValue value, params TKey[] keys) + { + return _data.TryGetValue(keys, out value); + } + + /// + /// 获取值 + /// + public TValue Get(params TKey[] keys) + { + if (!_data.TryGetValue(keys, out var value)) + throw new KeyNotFoundException(); + return value; + } + + /// + /// 通过部分键查找 + /// + public List FindByKey(int keyIndex, TKey key) + { + var result = new List(); + + if (keyIndex < 0 || keyIndex >= _indexes.Count) + return result; + + if (!_indexes[keyIndex].TryGetValue(key, out var keyLists)) + return result; + + foreach (var keys in keyLists) + { + if (_data.TryGetValue(keys, out var value)) + { + result.Add(value); + } + } + + return result; + } + + /// + /// 移除 + /// + public bool Remove(params TKey[] keys) + { + if (!_data.Remove(keys)) + return false; + + for (int i = 0; i < keys.Length && i < _indexes.Count; i++) + { + if (_indexes[i].TryGetValue(keys[i], out var list)) + { + list.RemoveAll(k => KeysEqual(k, keys)); + if (list.Count == 0) + _indexes[i].Remove(keys[i]); + } + } + + return true; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(params TKey[] keys) + { + return _data.ContainsKey(keys); + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + foreach (var index in _indexes) + { + index.Clear(); + } + } + + private bool KeysEqual(TKey[] a, TKey[] b) + { + if (a.Length != b.Length) + return false; + for (int i = 0; i < a.Length; i++) + { + if (!_keyComparer.Equals(a[i], b[i])) + return false; + } + return true; + } + + private class ArrayEqualityComparer : IEqualityComparer + { + public bool Equals(T[] x, T[] y) + { + if (x == null || y == null) + return x == y; + if (x.Length != y.Length) + return false; + var comparer = EqualityComparer.Default; + for (int i = 0; i < x.Length; i++) + { + if (!comparer.Equals(x[i], y[i])) + return false; + } + return true; + } + + public int GetHashCode(T[] obj) + { + if (obj == null) + return 0; + int hash = 17; + var comparer = EqualityComparer.Default; + foreach (var item in obj) + { + hash = hash * 31 + (item == null ? 0 : comparer.GetHashCode(item)); + } + return hash; + } + } + } + + /// + /// 区间映射 + /// 将键范围映射到值 + /// + public class RangeMap where T : IComparable + { + private readonly List _entries; + + private class RangeEntry + { + public T Min { get; set; } + public T Max { get; set; } + public object Value { get; set; } + public bool MinInclusive { get; set; } + public bool MaxInclusive { get; set; } + } + + /// + /// 区间数量 + /// + public int Count => _entries.Count; + + /// + /// 创建区间映射 + /// + public RangeMap() + { + _entries = new List(); + } + + /// + /// 添加区间映射(闭区间) + /// + public void Add(T min, T max, object value) + { + Add(min, max, value, true, true); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, object value, bool minInclusive, bool maxInclusive) + { + if (min.CompareTo(max) > 0) + throw new ArgumentException("Min must be less than or equal to max"); + + _entries.Add(new RangeEntry + { + Min = min, + Max = max, + Value = value, + MinInclusive = minInclusive, + MaxInclusive = maxInclusive + }); + } + + /// + /// 添加单点映射 + /// + public void Add(T point, object value) + { + Add(point, point, value, true, true); + } + + /// + /// 添加无穷下界区间 + /// + public void AddBelow(T max, object value, bool inclusive = false) + { + _entries.Add(new RangeEntry + { + Min = default, + Max = max, + Value = value, + MinInclusive = false, + MaxInclusive = inclusive + }); + } + + /// + /// 添加无穷上界区间 + /// + public void AddAbove(T min, object value, bool inclusive = false) + { + _entries.Add(new RangeEntry + { + Min = min, + Max = default, + Value = value, + MinInclusive = inclusive, + MaxInclusive = false + }); + } + + /// + /// 查找值 + /// + public object Find(T key) + { + foreach (var entry in _entries) + { + if (Contains(entry, key)) + return entry.Value; + } + return null; + } + + /// + /// 查找所有匹配值 + /// + public List FindAll(T key) + { + var result = new List(); + foreach (var entry in _entries) + { + if (Contains(entry, key)) + result.Add(entry.Value); + } + return result; + } + + /// + /// 泛型查找 + /// + public TValue Find(T key) + { + var result = Find(key); + return result == null ? default : (TValue)result; + } + + private bool Contains(RangeEntry entry, T key) + { + int minCmp = entry.Min == null || entry.Min.Equals(default) ? -1 : key.CompareTo(entry.Min); + int maxCmp = entry.Max == null || entry.Max.Equals(default) ? 1 : key.CompareTo(entry.Max); + + bool minOk = entry.MinInclusive ? minCmp >= 0 : minCmp > 0; + bool maxOk = entry.MaxInclusive ? maxCmp <= 0 : maxCmp < 0; + + return minOk && maxOk; + } + + /// + /// 移除区间 + /// + public bool Remove(T min, T max) + { + return _entries.RemoveAll(e => + e.Min.CompareTo(min) == 0 && e.Max.CompareTo(max) == 0) > 0; + } + + /// + /// 清空 + /// + public void Clear() + { + _entries.Clear(); + } + + /// + /// 获取所有区间 + /// + public IEnumerable<(T Min, T Max, object Value)> GetAllRanges() + { + return _entries.Select(e => (e.Min, e.Max, e.Value)); + } + } + + /// + /// 类型化区间映射 + /// + public class RangeMap where T : IComparable + { + private readonly List _entries; + + private class RangeEntry + { + public T Min { get; set; } + public T Max { get; set; } + public TValue Value { get; set; } + public bool MinInclusive { get; set; } + public bool MaxInclusive { get; set; } + } + + /// + /// 区间数量 + /// + public int Count => _entries.Count; + + /// + /// 创建区间映射 + /// + public RangeMap() + { + _entries = new List(); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, TValue value) + { + Add(min, max, value, true, true); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, TValue value, bool minInclusive, bool maxInclusive) + { + if (min.CompareTo(max) > 0) + throw new ArgumentException("Min must be less than or equal to max"); + + _entries.Add(new RangeEntry + { + Min = min, + Max = max, + Value = value, + MinInclusive = minInclusive, + MaxInclusive = maxInclusive + }); + } + + /// + /// 查找值 + /// + public TValue Find(T key) + { + foreach (var entry in _entries) + { + if (Contains(entry, key)) + return entry.Value; + } + return default; + } + + /// + /// 查找所有匹配值 + /// + public List FindAll(T key) + { + var result = new List(); + foreach (var entry in _entries) + { + if (Contains(entry, key)) + result.Add(entry.Value); + } + return result; + } + + /// + /// 尝试查找 + /// + public bool TryFind(T key, out TValue value) + { + value = Find(key); + return !EqualityComparer.Default.Equals(value, default); + } + + private bool Contains(RangeEntry entry, T key) + { + int minCmp = key.CompareTo(entry.Min); + int maxCmp = key.CompareTo(entry.Max); + + bool minOk = entry.MinInclusive ? minCmp >= 0 : minCmp > 0; + bool maxOk = entry.MaxInclusive ? maxCmp <= 0 : maxCmp < 0; + + return minOk && maxOk; + } + + /// + /// 清空 + /// + public void Clear() + { + _entries.Clear(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs b/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs new file mode 100644 index 0000000..49fac46 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 多值字典工具类 + /// 一个键可以对应多个值 + /// + public static class MultiMapUtil + { + /// + /// 创建多值字典 + /// + public static MultiMap Create() + { + return new MultiMap(); + } + + /// + /// 创建多值字典(使用指定的集合工厂) + /// + public static MultiMap Create(Func> collectionFactory) + { + return new MultiMap(collectionFactory); + } + } + + /// + /// 多值字典实现 + /// + /// 键类型 + /// 值类型 + public class MultiMap + { + private readonly Dictionary> _dictionary; + private readonly Func> _collectionFactory; + private int _valueCount; + + /// + /// 键数量 + /// + public int KeyCount => _dictionary.Count; + + /// + /// 值总数 + /// + public int ValueCount => _valueCount; + + /// + /// 所有键 + /// + public ICollection Keys => _dictionary.Keys; + + /// + /// 获取指定键的所有值 + /// + public ICollection this[TKey key] + { + get + { + if (_dictionary.TryGetValue(key, out var values)) + return values; + return new List(); + } + } + + /// + /// 创建多值字典(默认使用 List) + /// + public MultiMap() : this(() => new List()) { } + + /// + /// 创建多值字典(使用指定集合工厂) + /// + public MultiMap(Func> collectionFactory) + { + _dictionary = new Dictionary>(); + _collectionFactory = collectionFactory ?? (() => new List()); + _valueCount = 0; + } + + /// + /// 添加键值对 + /// + public void Add(TKey key, TValue value) + { + if (!_dictionary.TryGetValue(key, out var values)) + { + values = _collectionFactory(); + _dictionary[key] = values; + } + values.Add(value); + _valueCount++; + } + + /// + /// 批量添加值到指定键 + /// + public void AddRange(TKey key, IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + foreach (var value in values) + { + Add(key, value); + } + } + + /// + /// 移除指定键的指定值 + /// + public bool Remove(TKey key, TValue value) + { + if (_dictionary.TryGetValue(key, out var values)) + { + if (values.Remove(value)) + { + _valueCount--; + if (values.Count == 0) + { + _dictionary.Remove(key); + } + return true; + } + } + return false; + } + + /// + /// 移除指定键的所有值 + /// + public bool RemoveAll(TKey key) + { + if (_dictionary.TryGetValue(key, out var values)) + { + _valueCount -= values.Count; + _dictionary.Remove(key); + return true; + } + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _dictionary.ContainsKey(key); + } + + /// + /// 是否包含指定键值对 + /// + public bool Contains(TKey key, TValue value) + { + if (_dictionary.TryGetValue(key, out var values)) + { + return values.Contains(value); + } + return false; + } + + /// + /// 获取指定键的值数量 + /// + public int GetValueCount(TKey key) + { + if (_dictionary.TryGetValue(key, out var values)) + return values.Count; + return 0; + } + + /// + /// 尝试获取值 + /// + public bool TryGetValues(TKey key, out ICollection values) + { + return _dictionary.TryGetValue(key, out values); + } + + /// + /// 清空 + /// + public void Clear() + { + _dictionary.Clear(); + _valueCount = 0; + } + + /// + /// 获取所有键值对 + /// + public IEnumerable> GetAllKeyValuePairs() + { + foreach (var kvp in _dictionary) + { + foreach (var value in kvp.Value) + { + yield return new KeyValuePair(kvp.Key, value); + } + } + } + + /// + /// 获取所有值 + /// + public IEnumerable GetAllValues() + { + return _dictionary.Values.SelectMany(v => v); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs b/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs new file mode 100644 index 0000000..b576423 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 排列组合工具类 + /// 提供排列和组合的生成功能 + /// + public static class PermutationUtil + { + /// + /// 生成所有全排列 + /// + /// 元素类型 + /// 元素集合 + /// 所有排列 + public static IEnumerable> Permutations(IEnumerable elements) + { + var list = new List(elements); + return PermutationsCore(list, 0, list.Count); + } + + /// + /// 生成指定长度的排列 + /// + public static IEnumerable> Permutations(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + return PermutationsCore(list, 0, length); + } + + /// + /// 生成可重复排列(每个位置可以选择任意元素) + /// + public static IEnumerable> PermutationsWithRepetition(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count == 0) + yield break; + + var indices = new int[length]; + var current = new T[length]; + + while (true) + { + for (int i = 0; i < length; i++) + { + current[i] = list[indices[i]]; + } + yield return new List(current); + + int pos = length - 1; + while (pos >= 0) + { + indices[pos]++; + if (indices[pos] < list.Count) + break; + indices[pos] = 0; + pos--; + } + + if (pos < 0) + break; + } + } + + private static IEnumerable> PermutationsCore(List list, int start, int length) + { + if (start == length) + { + yield return new List(list.GetRange(0, length)); + yield break; + } + + for (int i = start; i < list.Count; i++) + { + Swap(list, start, i); + foreach (var perm in PermutationsCore(list, start + 1, length)) + { + yield return perm; + } + Swap(list, start, i); + } + } + + /// + /// 计算排列数 A(n,r) = n! / (n-r)! + /// + public static long Count(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("Invalid parameters"); + if (r == 0) return 1; + + long result = 1; + for (int i = 0; i < r; i++) + { + result *= (n - i); + } + return result; + } + + private static void Swap(List list, int i, int j) + { + if (i != j) + { + T temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + } + } + + /// + /// 组合工具类 + /// + public static class CombinationUtil + { + /// + /// 生成所有组合 + /// + /// 元素类型 + /// 元素集合 + /// 组合长度 + /// 所有组合 + public static IEnumerable> Combinations(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count < length) + yield break; + + var indices = new int[length]; + for (int i = 0; i < length; i++) + { + indices[i] = i; + } + + while (true) + { + yield return GetItems(list, indices); + + int pos = length - 1; + while (pos >= 0 && indices[pos] == list.Count - length + pos) + { + pos--; + } + + if (pos < 0) + break; + + indices[pos]++; + for (int i = pos + 1; i < length; i++) + { + indices[i] = indices[i - 1] + 1; + } + } + } + + /// + /// 生成所有长度的组合(从1到n) + /// + public static IEnumerable> AllCombinations(IEnumerable elements) + { + var list = new List(elements); + for (int length = 1; length <= list.Count; length++) + { + foreach (var combo in Combinations(list, length)) + { + yield return combo; + } + } + } + + /// + /// 生成可重复组合 + /// + public static IEnumerable> CombinationsWithRepetition(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count == 0) + yield break; + + var indices = new int[length]; + + while (true) + { + yield return GetItems(list, indices); + + int pos = length - 1; + while (pos >= 0 && indices[pos] == list.Count - 1) + { + pos--; + } + + if (pos < 0) + break; + + indices[pos]++; + for (int i = pos + 1; i < length; i++) + { + indices[i] = indices[pos]; + } + } + } + + /// + /// 计算组合数 C(n,r) = n! / (r! * (n-r)!) + /// + public static long Count(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("Invalid parameters"); + if (r == 0 || r == n) return 1; + + // 优化:使用较小的 r 计算 + if (r > n - r) + r = n - r; + + long result = 1; + for (int i = 0; i < r; i++) + { + result = result * (n - i) / (i + 1); + } + return result; + } + + private static IEnumerable GetItems(IList list, int[] indices) + { + var result = new T[indices.Length]; + for (int i = 0; i < indices.Length; i++) + { + result[i] = list[indices[i]]; + } + return result; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs b/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs new file mode 100644 index 0000000..c9cdf76 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs @@ -0,0 +1,324 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 优先队列工具类 + /// 基于 binary heap 实现的优先队列,支持自定义优先级比较器 + /// 兼容 netstandard2.1(.NET 6 才内置 PriorityQueue) + /// + public static class PriorityQueueUtil + { +#if NETSTANDARD2_1 + /// + /// 创建最小堆优先队列(最小值优先出队) + /// + public static PriorityQueue CreateMin() + where TPriority : IComparable + { + return new PriorityQueue(Comparer.Default); + } + + /// + /// 创建最大堆优先队列(最大值优先出队) + /// + public static PriorityQueue CreateMax() + where TPriority : IComparable + { + return new PriorityQueue(Comparer.Default, true); + } + + /// + /// 创建自定义比较器的优先队列 + /// + public static PriorityQueue Create( + IComparer comparer, bool maxHeap = false) + { + return new PriorityQueue(comparer, maxHeap); + } +#else + /// + /// 创建最小堆优先队列(最小值优先出队) + /// + public static System.Collections.Generic.PriorityQueue CreateMin() + where TPriority : IComparable + { + return new System.Collections.Generic.PriorityQueue(); + } + + /// + /// 创建最大堆优先队列(最大值优先出队) + /// + public static System.Collections.Generic.PriorityQueue CreateMax() + where TPriority : IComparable + { + // 使用反向比较器实现最大堆 + var comparer = Comparer.Default; + var reverseComparer = Comparer.Create((x, y) => comparer.Compare(y, x)); + return new System.Collections.Generic.PriorityQueue(reverseComparer); + } + + /// + /// 创建自定义比较器的优先队列 + /// + public static System.Collections.Generic.PriorityQueue Create( + IComparer comparer, bool maxHeap = false) + { + if (maxHeap) + { + var reverseComparer = Comparer.Create((x, y) => comparer.Compare(y, x)); + return new System.Collections.Generic.PriorityQueue(reverseComparer); + } + return new System.Collections.Generic.PriorityQueue(comparer); + } +#endif + } + +#if NETSTANDARD2_1 + /// + /// 优先队列实现(仅用于 netstandard2.1,.NET 6+ 使用内置实现) + /// + /// 元素类型 + /// 优先级类型 + public class PriorityQueue + { + private readonly List<(TElement Element, TPriority Priority)> _heap; + private readonly IComparer _comparer; + private readonly bool _isMaxHeap; + + /// + /// 元素数量 + /// + public int Count => _heap.Count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _heap.Count == 0; + + /// + /// 创建优先队列 + /// + /// 优先级比较器 + /// 是否为最大堆(默认最小堆) + public PriorityQueue(IComparer comparer, bool maxHeap = false) + { + _heap = new List<(TElement, TPriority)>(); + _comparer = comparer ?? Comparer.Default; + _isMaxHeap = maxHeap; + } + + /// + /// 创建带初始容量的优先队列 + /// + public PriorityQueue(int initialCapacity, IComparer comparer, bool maxHeap = false) + { + _heap = new List<(TElement, TPriority)>(initialCapacity); + _comparer = comparer ?? Comparer.Default; + _isMaxHeap = maxHeap; + } + + /// + /// 入队 + /// + public void Enqueue(TElement element, TPriority priority) + { + _heap.Add((element, priority)); + SiftUp(_heap.Count - 1); + } + + /// + /// 批量入队 + /// + public void EnqueueRange(IEnumerable<(TElement Element, TPriority Priority)> items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + Enqueue(item.Element, item.Priority); + } + } + + /// + /// 出队(返回优先级最高/最低的元素) + /// + public TElement Dequeue() + { + if (_heap.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + var result = _heap[0].Element; + int lastIndex = _heap.Count - 1; + + _heap[0] = _heap[lastIndex]; + _heap.RemoveAt(lastIndex); + + if (_heap.Count > 0) + { + SiftDown(0); + } + + return result; + } + + /// + /// 出队并返回元素和优先级 + /// + public (TElement Element, TPriority Priority) DequeueWithPriority() + { + if (_heap.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + var result = _heap[0]; + int lastIndex = _heap.Count - 1; + + _heap[0] = _heap[lastIndex]; + _heap.RemoveAt(lastIndex); + + if (_heap.Count > 0) + { + SiftDown(0); + } + + return result; + } + + /// + /// 查看队首元素(不移除) + /// + public TElement Peek() + { + if (_heap.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + return _heap[0].Element; + } + + /// + /// 查看队首元素和优先级(不移除) + /// + public (TElement Element, TPriority Priority) PeekWithPriority() + { + if (_heap.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + return _heap[0]; + } + + /// + /// 尝试出队 + /// + public bool TryDequeue(out TElement element, out TPriority priority) + { + if (_heap.Count == 0) + { + element = default; + priority = default; + return false; + } + + var result = DequeueWithPriority(); + element = result.Element; + priority = result.Priority; + return true; + } + + /// + /// 尝试查看队首 + /// + public bool TryPeek(out TElement element, out TPriority priority) + { + if (_heap.Count == 0) + { + element = default; + priority = default; + return false; + } + + element = _heap[0].Element; + priority = _heap[0].Priority; + return true; + } + + /// + /// 清空队列 + /// + public void Clear() + { + _heap.Clear(); + } + + /// + /// 获取所有元素(不保证顺序) + /// + public IEnumerable UnorderedItems() + { + foreach (var item in _heap) + { + yield return item.Element; + } + } + + /// + /// 获取所有元素和优先级(不保证顺序) + /// + public IEnumerable<(TElement Element, TPriority Priority)> UnorderedItemsWithPriority() + { + return _heap; + } + + private void SiftUp(int index) + { + while (index > 0) + { + int parentIndex = (index - 1) / 2; + if (Compare(index, parentIndex) <= 0) + break; + + Swap(index, parentIndex); + index = parentIndex; + } + } + + private void SiftDown(int index) + { + int count = _heap.Count; + + while (true) + { + int leftChild = index * 2 + 1; + int rightChild = index * 2 + 2; + int extremeIndex = index; + + if (leftChild < count && Compare(leftChild, extremeIndex) > 0) + extremeIndex = leftChild; + + if (rightChild < count && Compare(rightChild, extremeIndex) > 0) + extremeIndex = rightChild; + + if (extremeIndex == index) + break; + + Swap(index, extremeIndex); + index = extremeIndex; + } + } + + private int Compare(int i, int j) + { + int result = _comparer.Compare(_heap[i].Priority, _heap[j].Priority); + return _isMaxHeap ? result : -result; + } + + private void Swap(int i, int j) + { + var temp = _heap[i]; + _heap[i] = _heap[j]; + _heap[j] = temp; + } + } +#endif +} diff --git a/EasyTool.Core/CollectionsCategory/SkipListUtil.cs b/EasyTool.Core/CollectionsCategory/SkipListUtil.cs new file mode 100644 index 0000000..09f2b7c --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SkipListUtil.cs @@ -0,0 +1,360 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 跳表工具类 + /// 一种随机化的数据结构,基于并联的链表,实现高效查找、插入、删除 + /// 平均时间复杂度 O(log n) + /// + public static class SkipListUtil + { + /// + /// 创建跳表 + /// + public static SkipList Create() + where TKey : IComparable + { + return new SkipList(); + } + + /// + /// 创建指定最大层级的跳表 + /// + public static SkipList Create(int maxLevel, double probability = 0.5) + where TKey : IComparable + { + return new SkipList(maxLevel, probability); + } + } + + /// + /// 跳表实现 + /// + /// 键类型 + /// 值类型 + public class SkipList : IDictionary + where TKey : IComparable + { + private class SkipListNode + { + public TKey Key { get; set; } + public TValue Value { get; set; } + public SkipListNode[] Forward { get; set; } + + public SkipListNode(int level, TKey key = default, TValue value = default) + { + Key = key; + Value = value; + Forward = new SkipListNode[level + 1]; + } + } + + private readonly SkipListNode _header; + private readonly int _maxLevel; + private readonly double _probability; + private readonly Random _random; + private int _level; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 键集合 + /// + public ICollection Keys => GetElements().Select(x => x.Key).ToList(); + + /// + /// 值集合 + /// + public ICollection Values => GetElements().Select(x => x.Value).ToList(); + + /// + /// 索引访问 + /// + public TValue this[TKey key] + { + get => Get(key); + set => Add(key, value); + } + + /// + /// 创建跳表(默认最大16层) + /// + public SkipList() : this(16, 0.5) { } + + /// + /// 创建指定层级的跳表 + /// + public SkipList(int maxLevel, double probability = 0.5) + { + if (maxLevel <= 0) + throw new ArgumentOutOfRangeException(nameof(maxLevel)); + if (probability <= 0 || probability >= 1) + throw new ArgumentOutOfRangeException(nameof(probability)); + + _maxLevel = maxLevel; + _probability = probability; + _random = new Random(); + _level = 0; + _count = 0; + _header = new SkipListNode(_maxLevel); + } + + /// + /// 添加元素 + /// + public void Add(TKey key, TValue value) + { + var update = new SkipListNode[_maxLevel + 1]; + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + update[i] = current; + } + + current = current.Forward[0]; + + if (current != null && current.Key.CompareTo(key) == 0) + { + current.Value = value; + return; + } + + int newLevel = RandomLevel(); + + if (newLevel > _level) + { + for (int i = _level + 1; i <= newLevel; i++) + { + update[i] = _header; + } + _level = newLevel; + } + + var newNode = new SkipListNode(newLevel, key, value); + + for (int i = 0; i <= newLevel; i++) + { + newNode.Forward[i] = update[i].Forward[i]; + update[i].Forward[i] = newNode; + } + + _count++; + } + + /// + /// 添加键值对 + /// + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// 获取值 + /// + public TValue Get(TKey key) + { + if (!TryGetValue(key, out var value)) + throw new KeyNotFoundException($"Key '{key}' not found"); + return value; + } + + /// + /// 尝试获取值 + /// + public bool TryGetValue(TKey key, out TValue value) + { + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + } + + current = current.Forward[0]; + + if (current != null && current.Key.CompareTo(key) == 0) + { + value = current.Value; + return true; + } + + value = default; + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return TryGetValue(key, out _); + } + + /// + /// 是否包含键值对 + /// + public bool Contains(KeyValuePair item) + { + if (!TryGetValue(item.Key, out var value)) + return false; + return EqualityComparer.Default.Equals(value, item.Value); + } + + /// + /// 移除元素 + /// + public bool Remove(TKey key) + { + var update = new SkipListNode[_maxLevel + 1]; + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + update[i] = current; + } + + current = current.Forward[0]; + + if (current == null || current.Key.CompareTo(key) != 0) + return false; + + for (int i = 0; i <= _level; i++) + { + if (update[i].Forward[i] != current) + break; + update[i].Forward[i] = current.Forward[i]; + } + + while (_level > 0 && _header.Forward[_level] == null) + { + _level--; + } + + _count--; + return true; + } + + /// + /// 移除键值对 + /// + public bool Remove(KeyValuePair item) + { + if (!Contains(item)) + return false; + return Remove(item.Key); + } + + /// + /// 清空 + /// + public void Clear() + { + for (int i = 0; i <= _maxLevel; i++) + { + _header.Forward[i] = null; + } + _level = 0; + _count = 0; + } + + /// + /// 复制到数组 + /// + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + foreach (var item in GetElements()) + { + array[arrayIndex++] = item; + } + } + + /// + /// 获取第一个元素 + /// + public KeyValuePair? First() + { + if (_header.Forward[0] == null) + return null; + return new KeyValuePair(_header.Forward[0].Key, _header.Forward[0].Value); + } + + /// + /// 获取范围 + /// + public IEnumerable> GetRange(TKey start, TKey end) + { + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(start) < 0) + { + current = current.Forward[i]; + } + } + + current = current.Forward[0]; + + while (current != null && current.Key.CompareTo(end) <= 0) + { + yield return new KeyValuePair(current.Key, current.Value); + current = current.Forward[0]; + } + } + + private int RandomLevel() + { + int level = 0; + while (_random.NextDouble() < _probability && level < _maxLevel) + { + level++; + } + return level; + } + + private IEnumerable> GetElements() + { + var current = _header.Forward[0]; + while (current != null) + { + yield return new KeyValuePair(current.Key, current.Value); + current = current.Forward[0]; + } + } + + public IEnumerator> GetEnumerator() + { + return GetElements().GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs b/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs new file mode 100644 index 0000000..e3ed449 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs @@ -0,0 +1,675 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 空间索引工具类 + /// + public static class SpatialIndexUtil + { + /// + /// 创建KD树(2维) + /// + public static KDTree CreateKDTree() + { + return new KDTree(2); + } + + /// + /// 创建KD树(指定维度) + /// + public static KDTree CreateKDTree(int dimensions) + { + return new KDTree(dimensions); + } + + /// + /// 创建四叉树 + /// + public static QuadTree CreateQuadTree(double minX, double minY, double maxX, double maxY) + { + return new QuadTree(minX, minY, maxX, maxY); + } + + /// + /// 创建网格索引 + /// + public static GridIndex CreateGridIndex(double minX, double minY, double maxX, double maxY, int cellCountX, int cellCountY) + { + return new GridIndex(minX, minY, maxX, maxY, cellCountX, cellCountY); + } + } + + /// + /// KD树(K维树) + /// 用于高维空间中的最近邻搜索 + /// + public class KDTree + { + private class KDNode + { + public double[] Point { get; set; } + public T Value { get; set; } + public KDNode Left { get; set; } + public KDNode Right { get; set; } + } + + private KDNode _root; + private readonly int _dimensions; + private int _count; + + /// + /// 维度 + /// + public int Dimensions => _dimensions; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建KD树 + /// + public KDTree(int dimensions) + { + if (dimensions <= 0) + throw new ArgumentOutOfRangeException(nameof(dimensions)); + + _dimensions = dimensions; + _root = null; + _count = 0; + } + + /// + /// 插入点 + /// + public void Insert(double[] point, T value) + { + if (point == null || point.Length != _dimensions) + throw new ArgumentException($"Point must have {_dimensions} dimensions"); + + _root = Insert(_root, point, value, 0); + _count++; + } + + private KDNode Insert(KDNode node, double[] point, T value, int depth) + { + if (node == null) + { + return new KDNode { Point = point, Value = value }; + } + + int axis = depth % _dimensions; + + if (point[axis] < node.Point[axis]) + { + node.Left = Insert(node.Left, point, value, depth + 1); + } + else + { + node.Right = Insert(node.Right, point, value, depth + 1); + } + + return node; + } + + /// + /// 查找最近邻 + /// + public (double[] Point, T Value)? FindNearest(double[] target) + { + if (target == null || target.Length != _dimensions) + throw new ArgumentException($"Target must have {_dimensions} dimensions"); + + if (_root == null) + return null; + + KDNode best = null; + double bestDist = double.MaxValue; + + FindNearest(_root, target, 0, ref best, ref bestDist); + + return best == null ? null : (best.Point, best.Value); + } + + private void FindNearest(KDNode node, double[] target, int depth, ref KDNode best, ref double bestDist) + { + if (node == null) + return; + + double dist = Distance(node.Point, target); + if (dist < bestDist) + { + bestDist = dist; + best = node; + } + + int axis = depth % _dimensions; + double diff = target[axis] - node.Point[axis]; + + KDNode near = diff < 0 ? node.Left : node.Right; + KDNode far = diff < 0 ? node.Right : node.Left; + + FindNearest(near, target, depth + 1, ref best, ref bestDist); + + // 检查是否需要搜索另一侧 + if (diff * diff < bestDist) + { + FindNearest(far, target, depth + 1, ref best, ref bestDist); + } + } + + /// + /// 查找K个最近邻 + /// + public List<(double[] Point, T Value, double Distance)> FindKNearest(double[] target, int k) + { + if (target == null || target.Length != _dimensions) + throw new ArgumentException($"Target must have {_dimensions} dimensions"); + + var result = new List<(double[] Point, T Value, double Distance)>(); + + if (_root == null) + return result; + + var heap = new List<(double Dist, KDNode Node)>(); + + FindKNearest(_root, target, 0, heap, k); + + foreach (var (dist, node) in heap) + { + result.Add((node.Point, node.Value, dist)); + } + + return result.OrderBy(x => x.Distance).ToList(); + } + + private void FindKNearest(KDNode node, double[] target, int depth, List<(double Dist, KDNode Node)> heap, int k) + { + if (node == null) + return; + + double dist = Distance(node.Point, target); + + if (heap.Count < k) + { + heap.Add((dist, node)); + heap.Sort((a, b) => b.Dist.CompareTo(a.Dist)); + } + else if (dist < heap[0].Dist) + { + heap[0] = (dist, node); + heap.Sort((a, b) => b.Dist.CompareTo(a.Dist)); + } + + int axis = depth % _dimensions; + double diff = target[axis] - node.Point[axis]; + + KDNode near = diff < 0 ? node.Left : node.Right; + KDNode far = diff < 0 ? node.Right : node.Left; + + FindKNearest(near, target, depth + 1, heap, k); + + double maxDist = heap.Count < k ? double.MaxValue : heap[0].Dist; + if (diff * diff < maxDist) + { + FindKNearest(far, target, depth + 1, heap, k); + } + } + + /// + /// 范围查询 + /// + public List<(double[] Point, T Value)> RangeQuery(double[] min, double[] max) + { + var result = new List<(double[] Point, T Value)>(); + RangeQuery(_root, min, max, 0, result); + return result; + } + + private void RangeQuery(KDNode node, double[] min, double[] max, int depth, List<(double[] Point, T Value)> result) + { + if (node == null) + return; + + bool inside = true; + for (int i = 0; i < _dimensions; i++) + { + if (node.Point[i] < min[i] || node.Point[i] > max[i]) + { + inside = false; + break; + } + } + + if (inside) + { + result.Add((node.Point, node.Value)); + } + + int axis = depth % _dimensions; + + if (min[axis] <= node.Point[axis]) + { + RangeQuery(node.Left, min, max, depth + 1, result); + } + if (max[axis] >= node.Point[axis]) + { + RangeQuery(node.Right, min, max, depth + 1, result); + } + } + + private double Distance(double[] a, double[] b) + { + double sum = 0; + for (int i = 0; i < _dimensions; i++) + { + double diff = a[i] - b[i]; + sum += diff * diff; + } + return Math.Sqrt(sum); + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + /// + /// 四叉树 + /// 用于二维空间的区域查询 + /// + public class QuadTree + { + private class QuadNode + { + public double X { get; set; } + public double Y { get; set; } + public T Value { get; set; } + + public QuadNode(double x, double y, T value) + { + X = x; + Y = y; + Value = value; + } + } + + private class QuadTreeNode + { + public double MinX { get; set; } + public double MinY { get; set; } + public double MaxX { get; set; } + public double MaxY { get; set; } + + public List Points { get; set; } + public QuadTreeNode[] Children { get; set; } + + public int Capacity { get; set; } + public bool IsDivided { get; set; } + + public QuadTreeNode(double minX, double minY, double maxX, double maxY, int capacity = 4) + { + MinX = minX; + MinY = minY; + MaxX = maxX; + MaxY = maxY; + Capacity = capacity; + Points = new List(); + Children = null; + IsDivided = false; + } + + public double MidX => (MinX + MaxX) / 2; + public double MidY => (MinY + MaxY) / 2; + + public bool Contains(double x, double y) + { + return x >= MinX && x <= MaxX && y >= MinY && y <= MaxY; + } + + public bool Intersects(double minX, double minY, double maxX, double maxY) + { + return !(MaxX < minX || MinX > maxX || MaxY < minY || MinY > maxY); + } + + public void Subdivide() + { + Children = new QuadTreeNode[4]; + Children[0] = new QuadTreeNode(MinX, MidY, MidX, MaxY, Capacity); // NW + Children[1] = new QuadTreeNode(MidX, MidY, MaxX, MaxY, Capacity); // NE + Children[2] = new QuadTreeNode(MinX, MinY, MidX, MidY, Capacity); // SW + Children[3] = new QuadTreeNode(MidX, MinY, MaxX, MidY, Capacity); // SE + IsDivided = true; + } + } + + private readonly QuadTreeNode _root; + private readonly int _capacity; + private int _count; + + /// + /// 边界 + /// + public (double MinX, double MinY, double MaxX, double MaxY) Bounds => + (_root.MinX, _root.MinY, _root.MaxX, _root.MaxY); + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建四叉树 + /// + public QuadTree(double minX, double minY, double maxX, double maxY, int capacity = 4) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _root = new QuadTreeNode(minX, minY, maxX, maxY, capacity); + _count = 0; + } + + /// + /// 插入点 + /// + public bool Insert(double x, double y, T value) + { + if (!_root.Contains(x, y)) + return false; + + Insert(_root, x, y, value); + _count++; + return true; + } + + private void Insert(QuadTreeNode node, double x, double y, T value) + { + if (node.IsDivided) + { + int index = GetChildIndex(node, x, y); + if (index >= 0) + { + Insert(node.Children[index], x, y, value); + } + return; + } + + if (node.Points.Count < node.Capacity) + { + node.Points.Add(new QuadNode(x, y, value)); + } + else + { + node.Subdivide(); + + // 重新分配现有点 + foreach (var point in node.Points) + { + int index = GetChildIndex(node, point.X, point.Y); + if (index >= 0) + { + node.Children[index].Points.Add(point); + } + } + node.Points.Clear(); + + // 插入新点 + int newIndex = GetChildIndex(node, x, y); + if (newIndex >= 0) + { + node.Children[newIndex].Points.Add(new QuadNode(x, y, value)); + } + } + } + + private int GetChildIndex(QuadTreeNode node, double x, double y) + { + bool inWest = x < node.MidX; + bool inNorth = y >= node.MidY; + + if (inWest && inNorth) return 0; // NW + if (!inWest && inNorth) return 1; // NE + if (inWest && !inNorth) return 2; // SW + if (!inWest && !inNorth) return 3; // SE + + return -1; + } + + /// + /// 范围查询 + /// + public List<(double X, double Y, T Value)> Query(double minX, double minY, double maxX, double maxY) + { + var result = new List<(double X, double Y, T Value)>(); + Query(_root, minX, minY, maxX, maxY, result); + return result; + } + + private void Query(QuadTreeNode node, double minX, double minY, double maxX, double maxY, List<(double X, double Y, T Value)> result) + { + if (!node.Intersects(minX, minY, maxX, maxY)) + return; + + if (node.IsDivided) + { + foreach (var child in node.Children) + { + Query(child, minX, minY, maxX, maxY, result); + } + } + else + { + foreach (var point in node.Points) + { + if (point.X >= minX && point.X <= maxX && point.Y >= minY && point.Y <= maxY) + { + result.Add((point.X, point.Y, point.Value)); + } + } + } + } + + /// + /// 查找圆内所有点 + /// + public List<(double X, double Y, T Value)> QueryCircle(double centerX, double centerY, double radius) + { + var result = new List<(double X, double Y, T Value)>(); + QueryCircle(_root, centerX, centerY, radius, result); + return result; + } + + private void QueryCircle(QuadTreeNode node, double centerX, double centerY, double radius, List<(double X, double Y, T Value)> result) + { + if (!node.Intersects(centerX - radius, centerY - radius, centerX + radius, centerY + radius)) + return; + + if (node.IsDivided) + { + foreach (var child in node.Children) + { + QueryCircle(child, centerX, centerY, radius, result); + } + } + else + { + double radiusSquared = radius * radius; + foreach (var point in node.Points) + { + double dx = point.X - centerX; + double dy = point.Y - centerY; + if (dx * dx + dy * dy <= radiusSquared) + { + result.Add((point.X, point.Y, point.Value)); + } + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + _root.Points.Clear(); + _root.Children = null; + _root.IsDivided = false; + _count = 0; + } + } + + /// + /// 网格索引 + /// 将空间划分为网格,快速查找 + /// + public class GridIndex + { + private readonly Dictionary>> _grid; + private readonly double _minX, _minY, _maxX, _maxY; + private readonly double _cellWidth, _cellHeight; + private readonly int _cellCountX, _cellCountY; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 创建网格索引 + /// + public GridIndex(double minX, double minY, double maxX, double maxY, int cellCountX, int cellCountY) + { + if (maxX <= minX || maxY <= minY) + throw new ArgumentException("Invalid bounds"); + if (cellCountX <= 0 || cellCountY <= 0) + throw new ArgumentOutOfRangeException("Cell counts must be positive"); + + _minX = minX; + _minY = minY; + _maxX = maxX; + _maxY = maxY; + _cellCountX = cellCountX; + _cellCountY = cellCountY; + _cellWidth = (maxX - minX) / cellCountX; + _cellHeight = (maxY - minY) / cellCountY; + _grid = new Dictionary>>(); + _count = 0; + } + + /// + /// 插入点 + /// + public bool Insert(double x, double y, T value) + { + if (x < _minX || x > _maxX || y < _minY || y > _maxY) + return false; + + int cellX = GetCellX(x); + int cellY = GetCellY(y); + + if (!_grid.TryGetValue(cellX, out var column)) + { + column = new Dictionary>(); + _grid[cellX] = column; + } + + if (!column.TryGetValue(cellY, out var cell)) + { + cell = new List<(double X, double Y, T Value)>(); + column[cellY] = cell; + } + + cell.Add((x, y, value)); + _count++; + return true; + } + + /// + /// 范围查询 + /// + public List<(double X, double Y, T Value)> Query(double minX, double minY, double maxX, double maxY) + { + var result = new List<(double X, double Y, T Value)>(); + + int startCellX = Math.Max(0, GetCellX(minX)); + int startCellY = Math.Max(0, GetCellY(minY)); + int endCellX = Math.Min(_cellCountX - 1, GetCellX(maxX)); + int endCellY = Math.Min(_cellCountY - 1, GetCellY(maxY)); + + for (int x = startCellX; x <= endCellX; x++) + { + if (!_grid.TryGetValue(x, out var column)) + continue; + + for (int y = startCellY; y <= endCellY; y++) + { + if (!column.TryGetValue(y, out var cell)) + continue; + + foreach (var point in cell) + { + if (point.X >= minX && point.X <= maxX && point.Y >= minY && point.Y <= maxY) + { + result.Add(point); + } + } + } + } + + return result; + } + + /// + /// 获取单元格内的所有点 + /// + public List<(double X, double Y, T Value)> GetCell(int cellX, int cellY) + { + if (_grid.TryGetValue(cellX, out var column) && column.TryGetValue(cellY, out var cell)) + { + return new List<(double X, double Y, T Value)>(cell); + } + return new List<(double X, double Y, T Value)>(); + } + + /// + /// 清空 + /// + public void Clear() + { + _grid.Clear(); + _count = 0; + } + + private int GetCellX(double x) + { + return Math.Min(_cellCountX - 1, Math.Max(0, (int)((x - _minX) / _cellWidth))); + } + + private int GetCellY(double y) + { + return Math.Min(_cellCountY - 1, Math.Max(0, (int)((y - _minY) / _cellHeight))); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs b/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs new file mode 100644 index 0000000..da7d6e5 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs @@ -0,0 +1,1215 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 位集合工具类 + /// + public static class BitSetUtil + { + /// + /// 创建位集合 + /// + public static BitSet Create(int capacity) + { + return new BitSet(capacity); + } + + /// + /// 从数组创建位集合 + /// + public static BitSet FromArray(bool[] values) + { + var bitSet = new BitSet(values.Length); + for (int i = 0; i < values.Length; i++) + { + if (values[i]) + bitSet.Set(i); + } + return bitSet; + } + } + + /// + /// 位集合实现 + /// + public class BitSet + { + private readonly int[] _data; + private readonly int _capacity; + + /// + /// 位数 + /// + public int Capacity => _capacity; + + /// + /// 设置为 1 的位数 + /// + public int Cardinality + { + get + { + int count = 0; + for (int i = 0; i < _data.Length; i++) + { + count += PopCount(_data[i]); + } + return count; + } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Cardinality == 0; + + /// + /// 访问指定位 + /// + public bool this[int index] + { + get => Get(index); + set + { + if (value) + Set(index); + else + Clear(index); + } + } + + /// + /// 创建位集合 + /// + public BitSet(int capacity) + { + if (capacity < 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _data = new int[(capacity + 31) / 32]; + } + + /// + /// 设置指定位为 1 + /// + public void Set(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] |= 1 << (index % 32); + } + + /// + /// 设置指定位为 0 + /// + public void Clear(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] &= ~(1 << (index % 32)); + } + + /// + /// 翻转指定位 + /// + public void Flip(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] ^= 1 << (index % 32); + } + + /// + /// 获取指定位的值 + /// + public bool Get(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + return (_data[index / 32] & (1 << (index % 32))) != 0; + } + + /// + /// 设置所有位为 1 + /// + public void SetAll() + { + for (int i = 0; i < _data.Length; i++) + { + _data[i] = -1; + } + ClearExtraBits(); + } + + /// + /// 设置所有位为 0 + /// + public void ClearAll() + { + Array.Clear(_data, 0, _data.Length); + } + + /// + /// 与操作 + /// + public void And(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] &= other._data[i]; + } + } + + /// + /// 或操作 + /// + public void Or(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] |= other._data[i]; + } + } + + /// + /// 异或操作 + /// + public void Xor(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] ^= other._data[i]; + } + } + + /// + /// 与非操作 + /// + public void AndNot(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] &= ~other._data[i]; + } + } + + /// + /// 获取下一个设置为 1 的位 + /// + public int NextSetBit(int fromIndex) + { + if (fromIndex < 0) + throw new ArgumentOutOfRangeException(nameof(fromIndex)); + + int wordIndex = fromIndex / 32; + if (wordIndex >= _data.Length) + return -1; + + int word = _data[wordIndex] & (~0 << (fromIndex % 32)); + + while (true) + { + if (word != 0) + { + int result = wordIndex * 32 + TrailingZeroCount(word); + return result < _capacity ? result : -1; + } + + wordIndex++; + if (wordIndex >= _data.Length) + return -1; + + word = _data[wordIndex]; + } + } + + /// + /// 获取下一个设置为 0 的位 + /// + public int NextClearBit(int fromIndex) + { + if (fromIndex < 0) + throw new ArgumentOutOfRangeException(nameof(fromIndex)); + + int wordIndex = fromIndex / 32; + if (wordIndex >= _data.Length) + return -1; + + int word = ~_data[wordIndex] & (~0 << (fromIndex % 32)); + + while (true) + { + if (word != 0) + { + int result = wordIndex * 32 + TrailingZeroCount(word); + return result < _capacity ? result : -1; + } + + wordIndex++; + if (wordIndex >= _data.Length) + return fromIndex < _capacity ? fromIndex : -1; + + word = ~_data[wordIndex]; + } + } + + /// + /// 克隆 + /// + public BitSet Clone() + { + var clone = new BitSet(_capacity); + Array.Copy(_data, clone._data, _data.Length); + return clone; + } + + /// + /// 转换为布尔数组 + /// + public bool[] ToArray() + { + var result = new bool[_capacity]; + for (int i = 0; i < _capacity; i++) + { + result[i] = Get(i); + } + return result; + } + + private void ClearExtraBits() + { + int extraBits = _data.Length * 32 - _capacity; + if (extraBits > 0) + { + _data[_data.Length - 1] &= ~(-1 << (32 - extraBits)); + } + } + + private static int PopCount(int x) + { + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4)) & 0x0F0F0F0F; + return (x * 0x01010101) >> 24; + } + + private static int TrailingZeroCount(int x) + { + if (x == 0) + return 32; + + int count = 0; + while ((x & 1) == 0) + { + count++; + x >>= 1; + } + return count; + } + } + + /// + /// 稀疏数组工具类 + /// + public static class SparseArrayUtil + { + /// + /// 创建稀疏数组 + /// + public static SparseArray Create(int capacity = 16) + { + return new SparseArray(capacity); + } + } + + /// + /// 稀疏数组实现 + /// 使用字典存储非默认值元素,节省内存 + /// + public class SparseArray + { + private readonly T _defaultValue; + private readonly Dictionary _data; + private int _length; + + /// + /// 逻辑长度 + /// + public int Length => _length; + + /// + /// 非默认值元素数量 + /// + public int NonDefaultCount => _data.Count; + + /// + /// 访问元素 + /// + public T this[int index] + { + get + { + if (index < 0 || index >= _length) + throw new ArgumentOutOfRangeException(nameof(index)); + + return _data.TryGetValue(index, out var value) ? value : _defaultValue; + } + set + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + if (index >= _length) + _length = index + 1; + + if (EqualityComparer.Default.Equals(value, _defaultValue)) + { + _data.Remove(index); + } + else + { + _data[index] = value; + } + } + } + + /// + /// 创建稀疏数组 + /// + public SparseArray(int capacity = 16) : this(default, capacity) + { + } + + /// + /// 创建稀疏数组(指定默认值) + /// + public SparseArray(T defaultValue, int capacity = 16) + { + _defaultValue = defaultValue; + _data = new Dictionary(capacity); + _length = 0; + } + + /// + /// 设置长度 + /// + public void SetLength(int length) + { + if (length < 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + _length = length; + + // 移除超出长度的元素 + var keysToRemove = _data.Keys.Where(k => k >= length).ToList(); + foreach (var key in keysToRemove) + { + _data.Remove(key); + } + } + + /// + /// 获取所有非默认值索引 + /// + public IEnumerable GetNonDefaultIndices() + { + return _data.Keys; + } + + /// + /// 转换为常规数组 + /// + public T[] ToArray() + { + var result = new T[_length]; + for (int i = 0; i < _length; i++) + { + result[i] = this[i]; + } + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + _length = 0; + } + } + + /// + /// 有界队列工具类 + /// + public static class BoundedQueueUtil + { + /// + /// 创建有界队列 + /// + public static BoundedQueue Create(int capacity) + { + return new BoundedQueue(capacity); + } + } + + /// + /// 有界队列实现 + /// 当队列满时,可选择阻塞、丢弃新元素或丢弃旧元素 + /// + public class BoundedQueue + { + private readonly Queue _queue; + private readonly int _capacity; + private readonly object _lock = new object(); + + /// + /// 容量 + /// + public int Capacity => _capacity; + + /// + /// 当前数量 + /// + public int Count + { + get + { + lock (_lock) + { + return _queue.Count; + } + } + } + + /// + /// 是否已满 + /// + public bool IsFull => Count >= _capacity; + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 溢出策略 + /// + public OverflowPolicy Policy { get; set; } + + /// + /// 创建有界队列 + /// + public BoundedQueue(int capacity, OverflowPolicy policy = OverflowPolicy.Block) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _queue = new Queue(capacity); + Policy = policy; + } + + /// + /// 尝试入队 + /// + public bool TryEnqueue(T item) + { + lock (_lock) + { + switch (Policy) + { + case OverflowPolicy.Block: + if (_queue.Count >= _capacity) + return false; + break; + + case OverflowPolicy.DropNewest: + if (_queue.Count >= _capacity) + return false; + break; + + case OverflowPolicy.DropOldest: + if (_queue.Count >= _capacity) + _queue.Dequeue(); + break; + } + + _queue.Enqueue(item); + return true; + } + } + + /// + /// 尝试出队 + /// + public bool TryDequeue(out T item) + { + lock (_lock) + { + if (_queue.Count == 0) + { + item = default; + return false; + } + + item = _queue.Dequeue(); + return true; + } + } + + /// + /// 尝试查看队首 + /// + public bool TryPeek(out T item) + { + lock (_lock) + { + if (_queue.Count == 0) + { + item = default; + return false; + } + + item = _queue.Peek(); + return true; + } + } + + /// + /// 清空 + /// + public void Clear() + { + lock (_lock) + { + _queue.Clear(); + } + } + } + + /// + /// 溢出策略 + /// + public enum OverflowPolicy + { + /// + /// 阻塞(拒绝新元素) + /// + Block, + + /// + /// 丢弃最新元素 + /// + DropNewest, + + /// + /// 丢弃最旧元素 + /// + DropOldest + } + + /// + /// 延迟队列工具类 + /// + public static class DelayedQueueUtil + { + /// + /// 创建延迟队列 + /// + public static DelayedQueue Create() + { + return new DelayedQueue(); + } + } + + /// + /// 延迟队列实现 + /// 元素在指定时间后才能被取出 + /// + public class DelayedQueue + { + private readonly List _items; + private readonly object _lock = new object(); + + /// + /// 当前元素数量 + /// + public int Count + { + get + { + lock (_lock) + { + return _items.Count; + } + } + } + + /// + /// 可用元素数量 + /// + public int AvailableCount + { + get + { + lock (_lock) + { + return _items.Count(i => i.IsAvailable); + } + } + } + + /// + /// 创建延迟队列 + /// + public DelayedQueue() + { + _items = new List(); + } + + /// + /// 入队(延迟指定时间) + /// + public void Enqueue(T item, TimeSpan delay) + { + lock (_lock) + { + var availableAt = DateTime.UtcNow.Add(delay); + _items.Add(new DelayedItem(item, availableAt)); + } + } + + /// + /// 入队(指定可用时间) + /// + public void EnqueueAt(T item, DateTime availableAt) + { + lock (_lock) + { + _items.Add(new DelayedItem(item, availableAt)); + } + } + + /// + /// 尝试出队(仅返回已到期的元素) + /// + public bool TryDequeue(out T item) + { + lock (_lock) + { + var now = DateTime.UtcNow; + var index = _items.FindIndex(i => i.AvailableAt <= now); + + if (index >= 0) + { + item = _items[index].Value; + _items.RemoveAt(index); + return true; + } + + item = default; + return false; + } + } + + /// + /// 尝试查看队首 + /// + public bool TryPeek(out T item, out TimeSpan remainingDelay) + { + lock (_lock) + { + CleanupExpired(); + + if (_items.Count == 0) + { + item = default; + remainingDelay = TimeSpan.Zero; + return false; + } + + var first = _items.OrderBy(i => i.AvailableAt).First(); + item = first.Value; + remainingDelay = first.AvailableAt - DateTime.UtcNow; + + if (remainingDelay < TimeSpan.Zero) + remainingDelay = TimeSpan.Zero; + + return true; + } + } + + /// + /// 清空 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + } + } + + private void CleanupExpired() + { + var now = DateTime.UtcNow; + _items.RemoveAll(i => i.AvailableAt <= now); + } + + private class DelayedItem + { + public T Value { get; } + public DateTime AvailableAt { get; } + public bool IsAvailable => DateTime.UtcNow >= AvailableAt; + + public DelayedItem(T value, DateTime availableAt) + { + Value = value; + AvailableAt = availableAt; + } + } + } + + /// + /// 区间树工具类 + /// + public static class IntervalTreeUtil + { + /// + /// 创建区间树 + /// + public static IntervalTree Create() where T : IComparable + { + return new IntervalTree(); + } + } + + /// + /// 区间树实现 + /// 高效查询与指定区间重叠的所有区间 + /// + public class IntervalTree where T : IComparable + { + private IntervalNode _root; + + /// + /// 区间数量 + /// + public int Count { get; private set; } + + /// + /// 创建区间树 + /// + public IntervalTree() + { + Count = 0; + } + + /// + /// 添加区间 + /// + public void Add(T start, T end, object data = null) + { + if (start.CompareTo(end) > 0) + throw new ArgumentException("Start must be less than or equal to end"); + + var interval = new Interval(start, end, data); + _root = Insert(_root, interval); + Count++; + } + + /// + /// 查询与指定点重叠的区间 + /// + public List Query(T point) + { + var result = new List(); + Query(_root, point, result); + return result; + } + + /// + /// 查询与指定区间重叠的区间 + /// + public List Query(T start, T end) + { + var result = new List(); + Query(_root, start, end, result); + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + Count = 0; + } + + private IntervalNode Insert(IntervalNode node, Interval interval) + { + if (node == null) + { + return new IntervalNode(interval); + } + + int cmp = interval.Start.CompareTo(node.Interval.Start); + if (cmp < 0) + { + node.Left = Insert(node.Left, interval); + } + else + { + node.Right = Insert(node.Right, interval); + } + + // 更新最大值 + if (interval.End.CompareTo(node.MaxEnd) > 0) + { + node.MaxEnd = interval.End; + } + + return node; + } + + private void Query(IntervalNode node, T point, List result) + { + if (node == null) + return; + + // 如果点小于区间起点,且大于最大终点,则没有重叠 + if (point.CompareTo(node.Interval.Start) < 0 && + point.CompareTo(node.MaxEnd) > 0) + { + return; + } + + // 检查左子树 + if (node.Left != null && point.CompareTo(node.Left.MaxEnd) <= 0) + { + Query(node.Left, point, result); + } + + // 检查当前节点 + if (node.Interval.Contains(point)) + { + result.Add(node.Interval); + } + + // 检查右子树(如果点 >= 当前区间起点) + if (point.CompareTo(node.Interval.Start) >= 0) + { + Query(node.Right, point, result); + } + } + + private void Query(IntervalNode node, T start, T end, List result) + { + if (node == null) + return; + + // 如果查询区间完全在最大终点之后,无需继续 + if (start.CompareTo(node.MaxEnd) > 0) + return; + + // 检查左子树 + Query(node.Left, start, end, result); + + // 检查当前节点 + if (node.Interval.Overlaps(start, end)) + { + result.Add(node.Interval); + } + + // 如果查询区间完全在当前区间之前,无需检查右子树 + if (end.CompareTo(node.Interval.Start) < 0) + return; + + // 检查右子树 + Query(node.Right, start, end, result); + } + + private class IntervalNode + { + public Interval Interval { get; } + public IntervalNode Left { get; set; } + public IntervalNode Right { get; set; } + public T MaxEnd { get; set; } + + public IntervalNode(Interval interval) + { + Interval = interval; + MaxEnd = interval.End; + } + } + + /// + /// 区间 + /// + public class Interval + { + /// + /// 起点 + /// + public T Start { get; } + + /// + /// 终点 + /// + public T End { get; } + + /// + /// 关联数据 + /// + public object Data { get; } + + /// + /// 创建区间 + /// + public Interval(T start, T end, object data = null) + { + Start = start; + End = end; + Data = data; + } + + /// + /// 是否包含指定点 + /// + public bool Contains(T point) + { + return Start.CompareTo(point) <= 0 && End.CompareTo(point) >= 0; + } + + /// + /// 是否与指定区间重叠 + /// + public bool Overlaps(T start, T end) + { + return Start.CompareTo(end) <= 0 && End.CompareTo(start) >= 0; + } + + /// + /// 是否与指定区间重叠 + /// + public bool Overlaps(Interval other) + { + return Overlaps(other.Start, other.End); + } + + public override string ToString() + { + return $"[{Start}, {End}]"; + } + } + } + + /// + /// 有序多重集工具类 + /// + public static class SortedMultiSetUtil + { + /// + /// 创建有序多重集 + /// + public static SortedMultiSet Create() where T : IComparable + { + return new SortedMultiSet(); + } + } + + /// + /// 有序多重集实现 + /// 允许重复元素,保持排序 + /// + public class SortedMultiSet : IEnumerable where T : IComparable + { + private readonly SortedDictionary _dict; + private int _count; + + /// + /// 元素总数 + /// + public int Count => _count; + + /// + /// 不同元素数量 + /// + public int UniqueCount => _dict.Count; + + /// + /// 最小值 + /// + public T Min => _dict.Count > 0 ? _dict.First().Key : throw new InvalidOperationException("Set is empty"); + + /// + /// 最大值 + /// + public T Max => _dict.Count > 0 ? _dict.Last().Key : throw new InvalidOperationException("Set is empty"); + + /// + /// 创建有序多重集 + /// + public SortedMultiSet() + { + _dict = new SortedDictionary(); + _count = 0; + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (_dict.ContainsKey(item)) + { + _dict[item]++; + } + else + { + _dict[item] = 1; + } + _count++; + } + + /// + /// 移除一个元素 + /// + public bool Remove(T item) + { + if (!_dict.TryGetValue(item, out var count)) + return false; + + if (count == 1) + { + _dict.Remove(item); + } + else + { + _dict[item] = count - 1; + } + _count--; + return true; + } + + /// + /// 移除所有指定元素 + /// + public int RemoveAll(T item) + { + if (!_dict.TryGetValue(item, out var count)) + return 0; + + _dict.Remove(item); + _count -= count; + return count; + } + + /// + /// 获取元素数量 + /// + public int GetCount(T item) + { + return _dict.TryGetValue(item, out var count) ? count : 0; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T item) + { + return _dict.ContainsKey(item); + } + + /// + /// 清空 + /// + public void Clear() + { + _dict.Clear(); + _count = 0; + } + + /// + /// 获取小于指定值的元素数量 + /// + public int CountLessThan(T value) + { + int count = 0; + foreach (var kvp in _dict) + { + if (kvp.Key.CompareTo(value) >= 0) + break; + count += kvp.Value; + } + return count; + } + + /// + /// 获取大于指定值的元素数量 + /// + public int CountGreaterThan(T value) + { + int count = 0; + foreach (var kvp in _dict.Reverse()) + { + if (kvp.Key.CompareTo(value) <= 0) + break; + count += kvp.Value; + } + return count; + } + + /// + /// 获取指定范围内的元素数量 + /// + public int CountInRange(T min, T max) + { + int count = 0; + foreach (var kvp in _dict) + { + if (kvp.Key.CompareTo(max) > 0) + break; + if (kvp.Key.CompareTo(min) >= 0) + count += kvp.Value; + } + return count; + } + + public IEnumerator GetEnumerator() + { + foreach (var kvp in _dict) + { + for (int i = 0; i < kvp.Value; i++) + { + yield return kvp.Key; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs b/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs new file mode 100644 index 0000000..f915691 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs @@ -0,0 +1,723 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 统计工具类 + /// 提供常用的统计分析功能 + /// + public static class StatisticsUtil + { + /// + /// 计算中位数 + /// + public static double Median(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var sorted = values.OrderBy(x => x).ToList(); + if (sorted.Count == 0) + throw new ArgumentException("Collection is empty"); + + int count = sorted.Count; + int mid = count / 2; + + if (count % 2 == 0) + { + return (sorted[mid - 1] + sorted[mid]) / 2.0; + } + return sorted[mid]; + } + + /// + /// 计算中位数(泛型版本) + /// + public static double Median(IEnumerable values, Func selector) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + + return Median(values.Select(selector)); + } + + /// + /// 计算百分位数 + /// + /// 数据集合 + /// 百分位数(0-100) + public static double Percentile(IEnumerable values, double percentile) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (percentile < 0 || percentile > 100) + throw new ArgumentOutOfRangeException(nameof(percentile), "Percentile must be between 0 and 100"); + + var sorted = values.OrderBy(x => x).ToList(); + if (sorted.Count == 0) + throw new ArgumentException("Collection is empty"); + + if (percentile == 0) + return sorted[0]; + if (percentile == 100) + return sorted[sorted.Count - 1]; + + double position = (sorted.Count - 1) * percentile / 100.0; + int lower = (int)Math.Floor(position); + int upper = (int)Math.Ceiling(position); + + if (lower == upper) + return sorted[lower]; + + return sorted[lower] + (position - lower) * (sorted[upper] - sorted[lower]); + } + + /// + /// 计算百分位数(泛型版本) + /// + public static double Percentile(IEnumerable values, Func selector, double percentile) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + + return Percentile(values.Select(selector), percentile); + } + + /// + /// 计算四分位数 + /// + /// Q1, Q2(中位数), Q3 + public static (double Q1, double Q2, double Q3) Quartiles(IEnumerable values) + { + var sorted = values.OrderBy(x => x).ToList(); + if (sorted.Count == 0) + throw new ArgumentException("Collection is empty"); + + return ( + Percentile(sorted, 25), + Percentile(sorted, 50), + Percentile(sorted, 75) + ); + } + + /// + /// 计算标准差(总体标准差) + /// + public static double StandardDeviation(IEnumerable values) + { + return StandardDeviation(values, false); + } + + /// + /// 计算标准差 + /// + /// 数据集合 + /// 是否为样本标准差(使用 n-1) + public static double StandardDeviation(IEnumerable values, bool isSample) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + if (list.Count == 1 && isSample) + throw new ArgumentException("Sample standard deviation requires at least 2 values"); + + double mean = list.Average(); + double sumSquaredDiff = list.Sum(x => Math.Pow(x - mean, 2)); + int divisor = isSample ? list.Count - 1 : list.Count; + + return Math.Sqrt(sumSquaredDiff / divisor); + } + + /// + /// 计算标准差(泛型版本) + /// + public static double StandardDeviation(IEnumerable values, Func selector, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + + return StandardDeviation(values.Select(selector), isSample); + } + + /// + /// 计算方差 + /// + /// 数据集合 + /// 是否为样本方差 + public static double Variance(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + if (list.Count == 1 && isSample) + throw new ArgumentException("Sample variance requires at least 2 values"); + + double mean = list.Average(); + double sumSquaredDiff = list.Sum(x => Math.Pow(x - mean, 2)); + int divisor = isSample ? list.Count - 1 : list.Count; + + return sumSquaredDiff / divisor; + } + + /// + /// 计算方差(泛型版本) + /// + public static double Variance(IEnumerable values, Func selector, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + + return Variance(values.Select(selector), isSample); + } + + /// + /// 计算众数(出现次数最多的值) + /// + public static T Mode(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var groups = values.GroupBy(x => x).ToList(); + if (groups.Count == 0) + throw new ArgumentException("Collection is empty"); + + int maxCount = groups.Max(g => g.Count()); + var modes = groups.Where(g => g.Count() == maxCount).Select(g => g.Key).ToList(); + + if (modes.Count > 1) + throw new ArgumentException("Multiple modes exist"); + + return modes[0]; + } + + /// + /// 获取所有众数 + /// + public static List Modes(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var groups = values.GroupBy(x => x).ToList(); + if (groups.Count == 0) + throw new ArgumentException("Collection is empty"); + + int maxCount = groups.Max(g => g.Count()); + return groups.Where(g => g.Count() == maxCount).Select(g => g.Key).ToList(); + } + + /// + /// 计算频率分布 + /// + public static Dictionary Frequency(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + return values.GroupBy(x => x) + .ToDictionary(g => g.Key, g => g.Count()); + } + + /// + /// 计算相对频率分布 + /// + public static Dictionary RelativeFrequency(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + return list.GroupBy(x => x) + .ToDictionary(g => g.Key, g => (double)g.Count() / list.Count); + } + + /// + /// 计算累计频率分布 + /// + public static Dictionary CumulativeFrequency(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var freq = Frequency(values); + var sorted = freq.OrderBy(x => x.Key).ToList(); + var result = new Dictionary(); + int cumulative = 0; + + foreach (var kvp in sorted) + { + cumulative += kvp.Value; + result[kvp.Key] = cumulative; + } + + return result; + } + + /// + /// 计算范围(极差) + /// + public static double Range(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + return list.Max() - list.Min(); + } + + /// + /// 计算范围(泛型版本) + /// + public static double Range(IEnumerable values, Func selector) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (selector == null) + throw new ArgumentNullException(nameof(selector)); + + return Range(values.Select(selector)); + } + + /// + /// 计算四分位距(IQR) + /// + public static double InterquartileRange(IEnumerable values) + { + var (q1, q2, q3) = Quartiles(values); + return q3 - q1; + } + + /// + /// 计算变异系数(CV) + /// + public static double CoefficientOfVariation(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + double mean = list.Average(); + if (mean == 0) + throw new ArgumentException("Mean is zero, cannot calculate coefficient of variation"); + + return StandardDeviation(list, isSample) / Math.Abs(mean); + } + + /// + /// 计算偏度 + /// + public static double Skewness(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count < 3) + throw new ArgumentException("Skewness requires at least 3 values"); + + double mean = list.Average(); + double stdDev = StandardDeviation(list, isSample); + if (stdDev == 0) + return 0; + + int n = list.Count; + double skew = list.Sum(x => Math.Pow((x - mean) / stdDev, 3)); + + if (isSample) + { + return skew * n / ((n - 1) * (n - 2)); + } + return skew / n; + } + + /// + /// 计算峰度 + /// + public static double Kurtosis(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count < 4) + throw new ArgumentException("Kurtosis requires at least 4 values"); + + double mean = list.Average(); + double stdDev = StandardDeviation(list, isSample); + if (stdDev == 0) + return 0; + + int n = list.Count; + double kurt = list.Sum(x => Math.Pow((x - mean) / stdDev, 4)); + + if (isSample) + { + return kurt * n * (n + 1) / ((n - 1) * (n - 2) * (n - 3)) - 3.0 * (n - 1) * (n - 1) / ((n - 2) * (n - 3)); + } + return kurt / n - 3; + } + + /// + /// 计算协方差 + /// + public static double Covariance(IEnumerable x, IEnumerable y, bool isSample = false) + { + if (x == null) + throw new ArgumentNullException(nameof(x)); + if (y == null) + throw new ArgumentNullException(nameof(y)); + + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("Collections must have the same length"); + if (xList.Count == 0) + throw new ArgumentException("Collections are empty"); + if (xList.Count == 1 && isSample) + throw new ArgumentException("Sample covariance requires at least 2 values"); + + double meanX = xList.Average(); + double meanY = yList.Average(); + + double sum = 0; + for (int i = 0; i < xList.Count; i++) + { + sum += (xList[i] - meanX) * (yList[i] - meanY); + } + + int divisor = isSample ? xList.Count - 1 : xList.Count; + return sum / divisor; + } + + /// + /// 计算皮尔逊相关系数 + /// + public static double Correlation(IEnumerable x, IEnumerable y) + { + if (x == null) + throw new ArgumentNullException(nameof(x)); + if (y == null) + throw new ArgumentNullException(nameof(y)); + + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("Collections must have the same length"); + if (xList.Count < 2) + throw new ArgumentException("Correlation requires at least 2 values"); + + double stdDevX = StandardDeviation(xList, true); + double stdDevY = StandardDeviation(yList, true); + + if (stdDevX == 0 || stdDevY == 0) + return 0; + + return Covariance(xList, yList, true) / (stdDevX * stdDevY); + } + + /// + /// 计算几何平均数 + /// + public static double GeometricMean(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + if (list.Any(x => x <= 0)) + throw new ArgumentException("All values must be positive for geometric mean"); + + double logSum = list.Sum(x => Math.Log(x)); + return Math.Exp(logSum / list.Count); + } + + /// + /// 计算调和平均数 + /// + public static double HarmonicMean(IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + if (list.Any(x => x <= 0)) + throw new ArgumentException("All values must be positive for harmonic mean"); + + double sumReciprocals = list.Sum(x => 1.0 / x); + return list.Count / sumReciprocals; + } + + /// + /// 计算移动平均 + /// + public static List MovingAverage(IEnumerable values, int windowSize) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var list = values.ToList(); + if (list.Count < windowSize) + throw new ArgumentException("Window size cannot be larger than collection size"); + + var result = new List(); + double sum = 0; + + for (int i = 0; i < list.Count; i++) + { + sum += list[i]; + if (i >= windowSize) + { + sum -= list[i - windowSize]; + } + if (i >= windowSize - 1) + { + result.Add(sum / windowSize); + } + } + + return result; + } + + /// + /// 计算指数移动平均 + /// + public static List ExponentialMovingAverage(IEnumerable values, double alpha) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (alpha <= 0 || alpha > 1) + throw new ArgumentOutOfRangeException(nameof(alpha), "Alpha must be between 0 and 1"); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + var result = new List { list[0] }; + + for (int i = 1; i < list.Count; i++) + { + double ema = alpha * list[i] + (1 - alpha) * result[i - 1]; + result.Add(ema); + } + + return result; + } + + /// + /// 计算加权平均 + /// + public static double WeightedAverage(IEnumerable values, IEnumerable weights) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (weights == null) + throw new ArgumentNullException(nameof(weights)); + + var valueList = values.ToList(); + var weightList = weights.ToList(); + + if (valueList.Count != weightList.Count) + throw new ArgumentException("Values and weights must have the same length"); + if (valueList.Count == 0) + throw new ArgumentException("Collections are empty"); + + double sumWeighted = 0; + double sumWeights = 0; + + for (int i = 0; i < valueList.Count; i++) + { + sumWeighted += valueList[i] * weightList[i]; + sumWeights += weightList[i]; + } + + if (sumWeights == 0) + throw new ArgumentException("Sum of weights cannot be zero"); + + return sumWeighted / sumWeights; + } + + /// + /// 计算Z分数(标准化) + /// + public static List ZScore(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + double mean = list.Average(); + double stdDev = StandardDeviation(list, isSample); + + if (stdDev == 0) + return list.Select(_ => 0.0).ToList(); + + return list.Select(x => (x - mean) / stdDev).ToList(); + } + + /// + /// 计算百分等级 + /// + public static double PercentileRank(IEnumerable values, double value) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + int below = list.Count(x => x < value); + int equal = list.Count(x => x == value); + + return (below + 0.5 * equal) / list.Count * 100; + } + + /// + /// 计算描述性统计摘要 + /// + public static StatisticSummary Summary(IEnumerable values, bool isSample = false) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + var list = values.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + var (q1, q2, q3) = Quartiles(list); + + return new StatisticSummary + { + Count = list.Count, + Min = list.Min(), + Max = list.Max(), + Range = list.Max() - list.Min(), + Sum = list.Sum(), + Mean = list.Average(), + Median = q2, + Mode = list.GroupBy(x => x).OrderByDescending(g => g.Count()).First().Key, + StandardDeviation = StandardDeviation(list, isSample), + Variance = Variance(list, isSample), + Q1 = q1, + Q3 = q3, + IQR = q3 - q1 + }; + } + } + + /// + /// 统计摘要 + /// + public class StatisticSummary + { + /// + /// 元素数量 + /// + public int Count { get; set; } + + /// + /// 最小值 + /// + public double Min { get; set; } + + /// + /// 最大值 + /// + public double Max { get; set; } + + /// + /// 范围(极差) + /// + public double Range { get; set; } + + /// + /// 总和 + /// + public double Sum { get; set; } + + /// + /// 平均值 + /// + public double Mean { get; set; } + + /// + /// 中位数 + /// + public double Median { get; set; } + + /// + /// 众数 + /// + public double Mode { get; set; } + + /// + /// 标准差 + /// + public double StandardDeviation { get; set; } + + /// + /// 方差 + /// + public double Variance { get; set; } + + /// + /// 第一四分位数 + /// + public double Q1 { get; set; } + + /// + /// 第三四分位数 + /// + public double Q3 { get; set; } + + /// + /// 四分位距 + /// + public double IQR { get; set; } + + /// + /// 返回字符串表示 + /// + public override string ToString() + { + return $"Count: {Count}, Min: {Min:F4}, Max: {Max:F4}, Mean: {Mean:F4}, Median: {Median:F4}, StdDev: {StandardDeviation:F4}"; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TDigestUtil.cs b/EasyTool.Core/CollectionsCategory/TDigestUtil.cs new file mode 100644 index 0000000..e0bba80 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TDigestUtil.cs @@ -0,0 +1,352 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// T-Digest 工具类 + /// 用于流式数据的分位数估计 + /// 特别适合大数据集和分布式环境 + /// + public static class TDigestUtil + { + /// + /// 创建 T-Digest + /// + /// 压缩参数,越大越精确但占用更多内存 + public static TDigest Create(double compression = 100) + { + return new TDigest(compression); + } + } + + /// + /// T-Digest 实现 + /// + public class TDigest + { + private readonly double _compression; + private readonly List _centroids; + private long _count; + private double _min; + private double _max; + + private class Centroid + { + public double Mean { get; set; } + public double Weight { get; set; } + + public Centroid(double mean, double weight) + { + Mean = mean; + Weight = weight; + } + } + + /// + /// 压缩参数 + /// + public double Compression => _compression; + + /// + /// 已添加的数据点数量 + /// + public long Count => _count; + + /// + /// 最小值 + /// + public double Min => _min; + + /// + /// 最大值 + /// + public double Max => _max; + + /// + /// 质心数量 + /// + public int CentroidCount => _centroids.Count; + + /// + /// 创建 T-Digest + /// + public TDigest(double compression = 100) + { + if (compression <= 0) + throw new ArgumentOutOfRangeException(nameof(compression)); + + _compression = compression; + _centroids = new List(); + _count = 0; + _min = double.MaxValue; + _max = double.MinValue; + } + + /// + /// 添加数据点 + /// + public void Add(double value) + { + Add(value, 1); + } + + /// + /// 添加带权重的数据点 + /// + public void Add(double value, double weight) + { + if (double.IsNaN(value) || double.IsInfinity(value)) + throw new ArgumentException("Value cannot be NaN or infinity"); + + _count++; + if (value < _min) _min = value; + if (value > _max) _max = value; + + // 找到最近的质心 + int nearestIndex = -1; + double nearestDistance = double.MaxValue; + + for (int i = 0; i < _centroids.Count; i++) + { + double dist = Math.Abs(_centroids[i].Mean - value); + if (dist < nearestDistance) + { + nearestDistance = dist; + nearestIndex = i; + } + } + + // 如果没有质心或距离太远,创建新质心 + if (nearestIndex < 0 || _centroids.Count == 0) + { + _centroids.Add(new Centroid(value, weight)); + } + else + { + // 检查是否可以合并 + double q = GetQuantile(_centroids[nearestIndex].Mean); + double k = 4 * _count * q * (1 - q) / _compression; + double maxWeight = Math.Max(1, k); + + if (_centroids[nearestIndex].Weight + weight <= maxWeight) + { + // 合并到最近的质心 + var centroid = _centroids[nearestIndex]; + double newWeight = centroid.Weight + weight; + centroid.Mean = (centroid.Mean * centroid.Weight + value * weight) / newWeight; + centroid.Weight = newWeight; + } + else + { + // 创建新质心 + _centroids.Add(new Centroid(value, weight)); + } + } + + // 定期压缩 + if (_centroids.Count > 3 * _compression) + { + Compress(); + } + } + + /// + /// 批量添加数据 + /// + public void AddRange(IEnumerable values) + { + foreach (var value in values) + { + Add(value); + } + } + + /// + /// 压缩质心 + /// + public void Compress() + { + if (_centroids.Count <= 1) return; + + // 按均值排序 + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + var newCentroids = new List(); + double cumulativeWeight = 0; + + foreach (var centroid in _centroids) + { + if (newCentroids.Count == 0) + { + newCentroids.Add(new Centroid(centroid.Mean, centroid.Weight)); + cumulativeWeight = centroid.Weight; + continue; + } + + double q = cumulativeWeight / _count; + double k = 4 * _count * q * (1 - q) / _compression; + double maxWeight = Math.Max(1, k); + + var last = newCentroids[newCentroids.Count - 1]; + if (last.Weight + centroid.Weight <= maxWeight) + { + // 合并 + double newWeight = last.Weight + centroid.Weight; + last.Mean = (last.Mean * last.Weight + centroid.Mean * centroid.Weight) / newWeight; + last.Weight = newWeight; + } + else + { + newCentroids.Add(new Centroid(centroid.Mean, centroid.Weight)); + } + + cumulativeWeight += centroid.Weight; + } + + _centroids.Clear(); + _centroids.AddRange(newCentroids); + } + + /// + /// 估计分位数 + /// + /// 分位数(0-1) + public double Quantile(double q) + { + if (_count == 0) + throw new InvalidOperationException("No data has been added"); + if (q < 0 || q > 1) + throw new ArgumentOutOfRangeException(nameof(q), "Quantile must be between 0 and 1"); + + if (_centroids.Count == 0) return 0; + if (_centroids.Count == 1) return _centroids[0].Mean; + + // 确保已压缩 + Compress(); + + double targetWeight = q * _count; + + if (q <= 0) return _min; + if (q >= 1) return _max; + + // 按均值排序 + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + double cumulativeWeight = 0; + + for (int i = 0; i < _centroids.Count; i++) + { + double nextWeight = cumulativeWeight + _centroids[i].Weight; + + if (nextWeight > targetWeight) + { + // 在当前质心范围内 + double prevWeight = cumulativeWeight; + double deltaWeight = targetWeight - prevWeight; + double fraction = deltaWeight / _centroids[i].Weight; + + if (i == 0) + { + return _min + fraction * (_centroids[i].Mean - _min); + } + else + { + double prevMean = _centroids[i - 1].Mean; + return prevMean + fraction * (_centroids[i].Mean - prevMean); + } + } + + cumulativeWeight = nextWeight; + } + + return _max; + } + + /// + /// 获取值对应的分位数位置 + /// + public double GetQuantile(double value) + { + if (_count == 0) + return 0; + + Compress(); + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + if (value <= _min) return 0; + if (value >= _max) return 1; + + double cumulativeWeight = 0; + + for (int i = 0; i < _centroids.Count; i++) + { + if (_centroids[i].Mean >= value) + { + if (i == 0) + { + double fraction = (value - _min) / (_centroids[i].Mean - _min); + return (cumulativeWeight + fraction * _centroids[i].Weight / 2) / _count; + } + else + { + double prevMean = _centroids[i - 1].Mean; + double fraction = (value - prevMean) / (_centroids[i].Mean - prevMean); + double prevWeight = cumulativeWeight - _centroids[i - 1].Weight / 2; + return (prevWeight + fraction * _centroids[i].Weight) / _count; + } + } + + cumulativeWeight += _centroids[i].Weight; + } + + return 1; + } + + /// + /// 估计中位数 + /// + public double Median() => Quantile(0.5); + + /// + /// 估计第25百分位数 + /// + public double Q1() => Quantile(0.25); + + /// + /// 估计第75百分位数 + /// + public double Q3() => Quantile(0.75); + + /// + /// 估计四分位距 + /// + public double IQR() => Q3() - Q1(); + + /// + /// 合并另一个 T-Digest + /// + public void Merge(TDigest other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + foreach (var centroid in other._centroids) + { + Add(centroid.Mean, centroid.Weight); + } + } + + /// + /// 清空 + /// + public void Clear() + { + _centroids.Clear(); + _count = 0; + _min = double.MaxValue; + _max = double.MinValue; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TopKUtil.cs b/EasyTool.Core/CollectionsCategory/TopKUtil.cs new file mode 100644 index 0000000..e819bd4 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TopKUtil.cs @@ -0,0 +1,225 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Top-K 选择工具类 + /// 高效地从集合中选出前K个最大/最小元素 + /// 使用快速选择算法,平均时间复杂度 O(n) + /// + public static class TopKUtil + { + /// + /// 获取前K个最大元素 + /// + /// 元素类型 + /// 源集合 + /// 数量 + /// 前K个最大元素(降序) + public static IEnumerable TopK(IEnumerable source, int k) where T : IComparable + { + return TopK(source, k, Comparer.Default, false); + } + + /// + /// 获取前K个最小元素 + /// + public static IEnumerable BottomK(IEnumerable source, int k) where T : IComparable + { + return TopK(source, k, Comparer.Default, true); + } + + /// + /// 获取前K个元素(使用比较器) + /// + /// 元素类型 + /// 源集合 + /// 数量 + /// 比较器 + /// 是否升序(true=最小K个,false=最大K个) + /// 前K个元素 + public static IEnumerable TopK(IEnumerable source, int k, IComparer comparer, bool ascending) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (k <= 0) + return Enumerable.Empty(); + + var list = source.ToList(); + if (k >= list.Count) + return ascending ? list.OrderBy(x => x, comparer) : list.OrderByDescending(x => x, comparer); + + // 使用快速选择算法 + int actualK = Math.Min(k, list.Count); + + if (ascending) + { + QuickSelect(list, 0, list.Count - 1, actualK - 1, comparer); + var result = list.Take(actualK).ToList(); + result.Sort(comparer); + return result; + } + else + { + // 对于最大K个,我们找第(n-k)小的元素 + int targetIndex = list.Count - actualK; + QuickSelect(list, 0, list.Count - 1, targetIndex, comparer); + var result = list.Skip(targetIndex).ToList(); + result.Sort(comparer); + result.Reverse(); + return result; + } + } + + /// + /// 获取前K个最大元素(使用选择器) + /// + public static IEnumerable TopKBy(IEnumerable source, int k, Func keySelector) + where TKey : IComparable + { + return TopKBy(source, k, keySelector, Comparer.Default, false); + } + + /// + /// 获取前K个最小元素(使用选择器) + /// + public static IEnumerable BottomKBy(IEnumerable source, int k, Func keySelector) + where TKey : IComparable + { + return TopKBy(source, k, keySelector, Comparer.Default, true); + } + + /// + /// 获取前K个元素(使用选择器和比较器) + /// + public static IEnumerable TopKBy(IEnumerable source, int k, Func keySelector, + IComparer comparer, bool ascending) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (keySelector == null) + throw new ArgumentNullException(nameof(keySelector)); + + var list = source.ToList(); + if (k <= 0 || list.Count == 0) + return Enumerable.Empty(); + + // 带索引的快速选择 + var indexed = list.Select((item, index) => new { Item = item, Key = keySelector(item), Index = index }).ToList(); + + if (ascending) + { + indexed = indexed.OrderBy(x => x.Key, comparer).Take(k).ToList(); + } + else + { + indexed = indexed.OrderByDescending(x => x.Key, comparer).Take(k).ToList(); + } + + return indexed.Select(x => x.Item); + } + + /// + /// 使用堆获取前K个元素(适用于大数据流) + /// + public static IEnumerable TopKUsingHeap(IEnumerable source, int k) where T : IComparable + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (k <= 0) + return Enumerable.Empty(); + +#if NETSTANDARD2_1 + var minHeap = new PriorityQueue(Comparer.Default, false); + + foreach (var item in source) + { + if (minHeap.Count < k) + { + minHeap.Enqueue(item, item); + } + else if (item.CompareTo(minHeap.Peek()) > 0) + { + minHeap.Dequeue(); + minHeap.Enqueue(item, item); + } + } + + var result = new List(); + while (minHeap.Count > 0) + { + result.Add(minHeap.Dequeue()); + } +#else + var minHeap = new System.Collections.Generic.PriorityQueue(); + + foreach (var item in source) + { + if (minHeap.Count < k) + { + minHeap.Enqueue(item, item); + } + else if (item.CompareTo(minHeap.Peek()) > 0) + { + minHeap.Dequeue(); + minHeap.Enqueue(item, item); + } + } + + var result = new List(); + while (minHeap.Count > 0) + { + result.Add(minHeap.Dequeue()); + } +#endif + + result.Reverse(); + return result; + } + + private static void QuickSelect(List list, int left, int right, int k, IComparer comparer) + { + while (left < right) + { + int pivotIndex = Partition(list, left, right, comparer); + + if (k == pivotIndex) + return; + else if (k < pivotIndex) + right = pivotIndex - 1; + else + left = pivotIndex + 1; + } + } + + private static int Partition(List list, int left, int right, IComparer comparer) + { + T pivot = list[right]; + int i = left; + + for (int j = left; j < right; j++) + { + if (comparer.Compare(list[j], pivot) <= 0) + { + Swap(list, i, j); + i++; + } + } + + Swap(list, i, right); + return i; + } + + private static void Swap(List list, int i, int j) + { + if (i != j) + { + T temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs b/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs new file mode 100644 index 0000000..d2f4fd8 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs @@ -0,0 +1,443 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树形结构构建工具类 + /// 用于将扁平列表转换为树形结构 + /// + public static class TreeBuildUtil + { + /// + /// 创建树构建器 + /// + /// 节点类型 + /// 键类型 + /// 树构建器 + public static TreeBuilder CreateBuilder() + where T : class + where TKey : notnull + { + return new TreeBuilder(); + } + + /// + /// 将扁平列表构建为树形结构 + /// + /// 节点类型 + /// 键类型 + /// 扁平列表 + /// ID选择器 + /// 父ID选择器 + /// 子节点设置器 + /// 根节点判断(可选,默认为父ID为null或默认值) + /// 树形结构列表 + public static List Build( + IEnumerable items, + Func idSelector, + Func parentIdSelector, + Action> childrenSetter, + Func? rootPredicate = null) + where T : class + where TKey : notnull + { + if (items == null) + return new List(); + + var itemList = items.ToList(); + var lookup = new Dictionary(); + var childrenLookup = new Dictionary>(); + + // 第一遍:建立ID映射 + foreach (var item in itemList) + { + var id = idSelector(item); + lookup[id] = item; + } + + // 第二遍:建立父子关系 + foreach (var item in itemList) + { + var parentId = parentIdSelector(item); + if (parentId == null || EqualityComparer.Default.Equals(parentId, default!)) + continue; + + if (!childrenLookup.ContainsKey(parentId)) + childrenLookup[parentId] = new List(); + + childrenLookup[parentId].Add(item); + } + + // 设置子节点 + foreach (var kvp in childrenLookup) + { + if (lookup.TryGetValue(kvp.Key, out var parent)) + { + childrenSetter(parent, kvp.Value); + } + } + + // 获取根节点 + if (rootPredicate != null) + { + return itemList.Where(rootPredicate).ToList(); + } + + return itemList.Where(item => + { + var parentId = parentIdSelector(item); + return parentId == null || + EqualityComparer.Default.Equals(parentId, default!) || + !lookup.ContainsKey(parentId); + }).ToList(); + } + + /// + /// 将树形结构扁平化 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 扁平化列表 + public static List Flatten( + IEnumerable roots, + Func?> childrenSelector) + { + var result = new List(); + FlattenInternal(roots, childrenSelector, result); + return result; + } + + private static void FlattenInternal( + IEnumerable items, + Func?> childrenSelector, + List result) + { + foreach (var item in items) + { + result.Add(item); + var children = childrenSelector(item); + if (children != null) + { + FlattenInternal(children, childrenSelector, result); + } + } + } + + /// + /// 遍历树(深度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 遍历操作 + public static void Traverse( + IEnumerable roots, + Func?> childrenSelector, + Action action) + { + TraverseInternal(roots, childrenSelector, action, 0); + } + + private static void TraverseInternal( + IEnumerable items, + Func?> childrenSelector, + Action action, + int level) + { + foreach (var item in items) + { + action(item, level); + var children = childrenSelector(item); + if (children != null) + { + TraverseInternal(children, childrenSelector, action, level + 1); + } + } + } + + /// + /// 遍历树(广度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 遍历操作 + public static void TraverseBreadthFirst( + IEnumerable roots, + Func?> childrenSelector, + Action action) + { + var queue = new Queue<(T Item, int Level)>(); + + foreach (var root in roots) + { + queue.Enqueue((root, 0)); + } + + while (queue.Count > 0) + { + var (item, level) = queue.Dequeue(); + action(item, level); + + var children = childrenSelector(item); + if (children != null) + { + foreach (var child in children) + { + queue.Enqueue((child, level + 1)); + } + } + } + } + + /// + /// 查找节点 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 查找条件 + /// 找到的节点 + public static T? Find( + IEnumerable roots, + Func?> childrenSelector, + Func predicate) + { + foreach (var root in roots) + { + if (predicate(root)) + return root; + + var children = childrenSelector(root); + if (children != null) + { + var found = Find(children, childrenSelector, predicate); + if (found != null) + return found; + } + } + + return default; + } + + /// + /// 查找所有匹配节点 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 查找条件 + /// 找到的节点列表 + public static List FindAll( + IEnumerable roots, + Func?> childrenSelector, + Func predicate) + { + var result = new List(); + FindAllInternal(roots, childrenSelector, predicate, result); + return result; + } + + private static void FindAllInternal( + IEnumerable items, + Func?> childrenSelector, + Func predicate, + List result) + { + foreach (var item in items) + { + if (predicate(item)) + result.Add(item); + + var children = childrenSelector(item); + if (children != null) + { + FindAllInternal(children, childrenSelector, predicate, result); + } + } + } + + /// + /// 获取节点路径 + /// + /// 节点类型 + /// 键类型 + /// 所有节点 + /// 目标节点ID + /// ID选择器 + /// 父ID选择器 + /// 从根到目标的路径 + public static List GetPath( + IEnumerable items, + TKey targetId, + Func idSelector, + Func parentIdSelector) + where TKey : notnull + { + var result = new List(); + var lookup = items.ToDictionary(idSelector); + + if (!lookup.TryGetValue(targetId, out var current)) + return result; + + result.Add(current); + + while (true) + { + var parentId = parentIdSelector(current); + if (parentId == null || EqualityComparer.Default.Equals(parentId, default!)) + break; + + if (!lookup.TryGetValue(parentId, out var parent)) + break; + + result.Insert(0, parent); + current = parent; + } + + return result; + } + + /// + /// 计算树的深度 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 最大深度 + public static int GetDepth( + IEnumerable roots, + Func?> childrenSelector) + { + var maxDepth = 0; + + Traverse(roots, childrenSelector, (_, level) => + { + if (level > maxDepth) + maxDepth = level; + }); + + return maxDepth + 1; + } + + /// + /// 统计节点数量 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 总节点数 + public static int Count( + IEnumerable roots, + Func?> childrenSelector) + { + var count = 0; + Traverse(roots, childrenSelector, (_, _) => count++); + return count; + } + } + + /// + /// 树构建器 + /// + /// 节点类型 + /// 键类型 + public class TreeBuilder + where T : class + where TKey : notnull + { + private Func? _idSelector; + private Func? _parentIdSelector; + private Action>? _childrenSetter; + private Func? _rootPredicate; + + /// + /// 设置ID选择器 + /// + public TreeBuilder WithId(Func selector) + { + _idSelector = selector; + return this; + } + + /// + /// 设置父ID选择器 + /// + public TreeBuilder WithParentId(Func selector) + { + _parentIdSelector = selector; + return this; + } + + /// + /// 设置子节点设置器 + /// + public TreeBuilder WithChildren(Action> setter) + { + _childrenSetter = setter; + return this; + } + + /// + /// 设置根节点判断条件 + /// + public TreeBuilder WithRootPredicate(Func predicate) + { + _rootPredicate = predicate; + return this; + } + + /// + /// 构建树 + /// + /// 扁平列表 + /// 树形结构 + public List Build(IEnumerable items) + { + if (_idSelector == null) + throw new InvalidOperationException("必须设置ID选择器"); + if (_parentIdSelector == null) + throw new InvalidOperationException("必须设置父ID选择器"); + if (_childrenSetter == null) + throw new InvalidOperationException("必须设置子节点设置器"); + + return TreeBuildUtil.Build(items, _idSelector, _parentIdSelector, _childrenSetter, _rootPredicate); + } + } + + /// + /// 树节点基类 + /// + /// 节点类型 + public class TreeNode where T : TreeNode + { + /// + /// 子节点列表 + /// + public List Children { get; set; } = new(); + + /// + /// 添加子节点 + /// + public void AddChild(T child) + { + Children.Add(child); + } + + /// + /// 移除子节点 + /// + public bool RemoveChild(T child) + { + return Children.Remove(child); + } + + /// + /// 是否为叶子节点 + /// + public bool IsLeaf => Children.Count == 0; + } +} diff --git a/EasyTool.Core/CollectionsCategory/TreeUtil.cs b/EasyTool.Core/CollectionsCategory/TreeUtil.cs new file mode 100644 index 0000000..368c24d --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TreeUtil.cs @@ -0,0 +1,1237 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树工具类 + /// + public static class TreeUtil + { + /// + /// 创建通用树 + /// + public static Tree Create(T value) + { + return new Tree(value); + } + + /// + /// 从层次结构创建树 + /// + public static Tree FromHierarchy( + IEnumerable items, + Func keySelector, + Func parentKeySelector, + TKey rootParentKey = default) where TKey : IEquatable + { + var itemDict = items.ToDictionary(keySelector); + var childrenDict = items.GroupBy(parentKeySelector).ToDictionary(g => g.Key, g => g.ToList()); + + T rootItem; + if (rootParentKey == null || rootParentKey.Equals(default)) + { + rootItem = items.FirstOrDefault(i => parentKeySelector(i) == null || parentKeySelector(i).Equals(default)); + } + else + { + rootItem = items.FirstOrDefault(i => parentKeySelector(i).Equals(rootParentKey)); + } + + if (rootItem == null) + throw new ArgumentException("Cannot find root item"); + + var root = new Tree(rootItem); + BuildTree(root, keySelector(rootItem), keySelector, childrenDict); + return root; + } + + private static void BuildTree( + Tree parent, + TKey parentKey, + Func keySelector, + Dictionary> childrenDict) where TKey : IEquatable + { + if (!childrenDict.TryGetValue(parentKey, out var children)) + return; + + foreach (var child in children) + { + var childNode = parent.AddChild(child); + BuildTree(childNode, keySelector(child), keySelector, childrenDict); + } + } + } + + /// + /// 通用树节点 + /// + public class Tree + { + private readonly List> _children; + + /// + /// 节点值 + /// + public T Value { get; set; } + + /// + /// 父节点 + /// + public Tree Parent { get; private set; } + + /// + /// 子节点 + /// + public IReadOnlyList> Children => _children; + + /// + /// 深度 + /// + public int Depth + { + get + { + int depth = 0; + var current = Parent; + while (current != null) + { + depth++; + current = current.Parent; + } + return depth; + } + } + + /// + /// 高度 + /// + public int Height + { + get + { + if (_children.Count == 0) + return 0; + return 1 + _children.Max(c => c.Height); + } + } + + /// + /// 是否为根节点 + /// + public bool IsRoot => Parent == null; + + /// + /// 是否为叶节点 + /// + public bool IsLeaf => _children.Count == 0; + + /// + /// 子节点数量 + /// + public int ChildCount => _children.Count; + + /// + /// 创建树节点 + /// + public Tree(T value) + { + Value = value; + _children = new List>(); + } + + /// + /// 添加子节点 + /// + public Tree AddChild(T value) + { + var child = new Tree(value) { Parent = this }; + _children.Add(child); + return child; + } + + /// + /// 添加子节点 + /// + public void AddChild(Tree child) + { + child.Parent = this; + _children.Add(child); + } + + /// + /// 移除子节点 + /// + public bool RemoveChild(Tree child) + { + if (_children.Remove(child)) + { + child.Parent = null; + return true; + } + return false; + } + + /// + /// 清空子节点 + /// + public void ClearChildren() + { + foreach (var child in _children) + { + child.Parent = null; + } + _children.Clear(); + } + + /// + /// 获取根节点 + /// + public Tree GetRoot() + { + var current = this; + while (current.Parent != null) + { + current = current.Parent; + } + return current; + } + + /// + /// 获取所有祖先 + /// + public IEnumerable> GetAncestors() + { + var current = Parent; + while (current != null) + { + yield return current; + current = current.Parent; + } + } + + /// + /// 获取所有后代 + /// + public IEnumerable> GetDescendants() + { + foreach (var child in _children) + { + yield return child; + foreach (var descendant in child.GetDescendants()) + { + yield return descendant; + } + } + } + + /// + /// 前序遍历 + /// + public IEnumerable> PreOrderTraversal() + { + yield return this; + foreach (var child in _children) + { + foreach (var node in child.PreOrderTraversal()) + { + yield return node; + } + } + } + + /// + /// 后序遍历 + /// + public IEnumerable> PostOrderTraversal() + { + foreach (var child in _children) + { + foreach (var node in child.PostOrderTraversal()) + { + yield return node; + } + } + yield return this; + } + + /// + /// 层序遍历(广度优先) + /// + public IEnumerable> LevelOrderTraversal() + { + var queue = new Queue>(); + queue.Enqueue(this); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + yield return current; + + foreach (var child in current._children) + { + queue.Enqueue(child); + } + } + } + + /// + /// 查找节点 + /// + public Tree Find(Func predicate) + { + if (predicate(Value)) + return this; + + foreach (var child in _children) + { + var found = child.Find(predicate); + if (found != null) + return found; + } + + return null; + } + + /// + /// 查找所有匹配节点 + /// + public IEnumerable> FindAll(Func predicate) + { + if (predicate(Value)) + yield return this; + + foreach (var child in _children) + { + foreach (var found in child.FindAll(predicate)) + { + yield return found; + } + } + } + + /// + /// 获取路径 + /// + public List> GetPath() + { + var path = new List>(); + var current = this; + while (current != null) + { + path.Insert(0, current); + current = current.Parent; + } + return path; + } + } + + /// + /// 二叉树工具类 + /// + public static class BinaryTreeUtil + { + /// + /// 创建二叉树 + /// + public static BinaryTree Create(T value) + { + return new BinaryTree(value); + } + + /// + /// 从层序数组创建完全二叉树 + /// + public static BinaryTree FromArray(T[] values) where T : class + { + if (values == null || values.Length == 0 || values[0] == null) + return null; + + var root = new BinaryTree(values[0]); + var queue = new Queue>(); + queue.Enqueue(root); + + int i = 1; + while (queue.Count > 0 && i < values.Length) + { + var current = queue.Dequeue(); + + if (i < values.Length && values[i] != null) + { + current.Left = new BinaryTree(values[i]) { Parent = current }; + queue.Enqueue(current.Left); + } + i++; + + if (i < values.Length && values[i] != null) + { + current.Right = new BinaryTree(values[i]) { Parent = current }; + queue.Enqueue(current.Right); + } + i++; + } + + return root; + } + } + + /// + /// 二叉树节点 + /// + public class BinaryTree + { + /// + /// 节点值 + /// + public T Value { get; set; } + + /// + /// 左子节点 + /// + public BinaryTree Left { get; set; } + + /// + /// 右子节点 + /// + public BinaryTree Right { get; set; } + + /// + /// 父节点 + /// + public BinaryTree Parent { get; set; } + + /// + /// 是否为叶节点 + /// + public bool IsLeaf => Left == null && Right == null; + + /// + /// 是否为根节点 + /// + public bool IsRoot => Parent == null; + + /// + /// 高度 + /// + public int Height + { + get + { + int leftHeight = Left?.Height ?? 0; + int rightHeight = Right?.Height ?? 0; + return 1 + Math.Max(leftHeight, rightHeight); + } + } + + /// + /// 节点数量 + /// + public int NodeCount + { + get + { + int count = 1; + if (Left != null) count += Left.NodeCount; + if (Right != null) count += Right.NodeCount; + return count; + } + } + + /// + /// 创建二叉树节点 + /// + public BinaryTree(T value) + { + Value = value; + } + + /// + /// 前序遍历 + /// + public IEnumerable> PreOrderTraversal() + { + yield return this; + if (Left != null) + { + foreach (var node in Left.PreOrderTraversal()) + yield return node; + } + if (Right != null) + { + foreach (var node in Right.PreOrderTraversal()) + yield return node; + } + } + + /// + /// 中序遍历 + /// + public IEnumerable> InOrderTraversal() + { + if (Left != null) + { + foreach (var node in Left.InOrderTraversal()) + yield return node; + } + yield return this; + if (Right != null) + { + foreach (var node in Right.InOrderTraversal()) + yield return node; + } + } + + /// + /// 后序遍历 + /// + public IEnumerable> PostOrderTraversal() + { + if (Left != null) + { + foreach (var node in Left.PostOrderTraversal()) + yield return node; + } + if (Right != null) + { + foreach (var node in Right.PostOrderTraversal()) + yield return node; + } + yield return this; + } + + /// + /// 层序遍历 + /// + public IEnumerable> LevelOrderTraversal() + { + var queue = new Queue>(); + queue.Enqueue(this); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + yield return current; + + if (current.Left != null) + queue.Enqueue(current.Left); + if (current.Right != null) + queue.Enqueue(current.Right); + } + } + + /// + /// 反转二叉树 + /// + public void Invert() + { + var temp = Left; + Left = Right; + Right = temp; + + Left?.Invert(); + Right?.Invert(); + } + + /// + /// 克隆 + /// + public BinaryTree Clone() + { + var clone = new BinaryTree(Value); + if (Left != null) + { + clone.Left = Left.Clone(); + clone.Left.Parent = clone; + } + if (Right != null) + { + clone.Right = Right.Clone(); + clone.Right.Parent = clone; + } + return clone; + } + + /// + /// 获取指定深度的所有节点 + /// + public List> GetNodesAtDepth(int depth) + { + var result = new List>(); + GetNodesAtDepth(this, depth, 0, result); + return result; + } + + private static void GetNodesAtDepth(BinaryTree node, int targetDepth, int currentDepth, List> result) + { + if (node == null) + return; + + if (currentDepth == targetDepth) + { + result.Add(node); + return; + } + + GetNodesAtDepth(node.Left, targetDepth, currentDepth + 1, result); + GetNodesAtDepth(node.Right, targetDepth, currentDepth + 1, result); + } + } + + /// + /// 二叉搜索树工具类 + /// + public static class BinarySearchTreeUtil + { + /// + /// 创建二叉搜索树 + /// + public static BinarySearchTree Create() where T : IComparable + { + return new BinarySearchTree(); + } + + /// + /// 从集合创建二叉搜索树 + /// + public static BinarySearchTree FromEnumerable(IEnumerable items) where T : IComparable + { + var bst = new BinarySearchTree(); + foreach (var item in items) + { + bst.Add(item); + } + return bst; + } + } + + /// + /// 二叉搜索树 + /// + public class BinarySearchTree where T : IComparable + { + private BSTNode _root; + private int _count; + + /// + /// 节点数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_root == null) + throw new InvalidOperationException("Tree is empty"); + return FindMin(_root).Value; + } + } + + /// + /// 最大值 + /// + public T Max + { + get + { + if (_root == null) + throw new InvalidOperationException("Tree is empty"); + return FindMax(_root).Value; + } + } + + private class BSTNode + { + public T Value { get; set; } + public BSTNode Left { get; set; } + public BSTNode Right { get; set; } + + public BSTNode(T value) + { + Value = value; + } + } + + /// + /// 添加元素 + /// + public void Add(T value) + { + _root = Add(_root, value); + } + + private BSTNode Add(BSTNode node, T value) + { + if (node == null) + { + _count++; + return new BSTNode(value); + } + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + node.Left = Add(node.Left, value); + else if (cmp > 0) + node.Right = Add(node.Right, value); + + return node; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + return Find(_root, value) != null; + } + + private BSTNode Find(BSTNode node, T value) + { + if (node == null) + return null; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + return Find(node.Left, value); + if (cmp > 0) + return Find(node.Right, value); + return node; + } + + /// + /// 移除元素 + /// + public bool Remove(T value) + { + int oldCount = _count; + _root = Remove(_root, value); + return _count < oldCount; + } + + private BSTNode Remove(BSTNode node, T value) + { + if (node == null) + return null; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Remove(node.Left, value); + } + else if (cmp > 0) + { + node.Right = Remove(node.Right, value); + } + else + { + _count--; + if (node.Left == null) + return node.Right; + if (node.Right == null) + return node.Left; + + // 两个子节点都存在,用后继节点替换 + var successor = FindMin(node.Right); + node.Value = successor.Value; + node.Right = Remove(node.Right, successor.Value); + _count++; // 因为上面递归会再次减 + } + + return node; + } + + private BSTNode FindMin(BSTNode node) + { + while (node.Left != null) + node = node.Left; + return node; + } + + private BSTNode FindMax(BSTNode node) + { + while (node.Right != null) + node = node.Right; + return node; + } + + /// + /// 中序遍历 + /// + public IEnumerable InOrderTraversal() + { + return InOrderTraversal(_root); + } + + private IEnumerable InOrderTraversal(BSTNode node) + { + if (node == null) + yield break; + + foreach (var value in InOrderTraversal(node.Left)) + yield return value; + + yield return node.Value; + + foreach (var value in InOrderTraversal(node.Right)) + yield return value; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + + /// + /// 查找小于指定值的最大元素 + /// + public T? Floor(T value) + { + var node = Floor(_root, value); + return node == null ? default : node.Value; + } + + private BSTNode Floor(BSTNode node, T value) + { + if (node == null) + return null; + + int cmp = value.CompareTo(node.Value); + if (cmp == 0) + return node; + if (cmp < 0) + return Floor(node.Left, value); + + var rightFloor = Floor(node.Right, value); + return rightFloor ?? node; + } + + /// + /// 查找大于指定值的最小元素 + /// + public T? Ceiling(T value) + { + var node = Ceiling(_root, value); + return node == null ? default : node.Value; + } + + private BSTNode Ceiling(BSTNode node, T value) + { + if (node == null) + return null; + + int cmp = value.CompareTo(node.Value); + if (cmp == 0) + return node; + if (cmp > 0) + return Ceiling(node.Right, value); + + var leftCeiling = Ceiling(node.Left, value); + return leftCeiling ?? node; + } + + /// + /// 获取排名(小于指定值的元素数量) + /// + public int Rank(T value) + { + return Rank(_root, value); + } + + private int Rank(BSTNode node, T value) + { + if (node == null) + return 0; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + return Rank(node.Left, value); + if (cmp > 0) + return 1 + CountNodes(node.Left) + Rank(node.Right, value); + return CountNodes(node.Left); + } + + private int CountNodes(BSTNode node) + { + if (node == null) + return 0; + return 1 + CountNodes(node.Left) + CountNodes(node.Right); + } + } + + /// + /// 线段树工具类 + /// + public static class SegmentTreeUtil + { + /// + /// 创建线段树(求和) + /// + public static SegmentTree Create(int[] values) + { + return new SegmentTree(values, (a, b) => a + b, 0); + } + + /// + /// 创建线段树(自定义操作) + /// + public static SegmentTree Create(int[] values, Func operation, int identity) + { + return new SegmentTree(values, operation, identity); + } + } + + /// + /// 线段树(区间查询/更新) + /// + public class SegmentTree + { + private readonly int[] _tree; + private readonly int[] _lazy; + private readonly int _n; + private readonly Func _operation; + private readonly int _identity; + + /// + /// 元素数量 + /// + public int Count => _n; + + /// + /// 创建线段树 + /// + public SegmentTree(int[] values, Func operation, int identity) + { + if (values == null || values.Length == 0) + throw new ArgumentException("Values cannot be null or empty"); + + _n = values.Length; + _operation = operation; + _identity = identity; + _tree = new int[4 * _n]; + _lazy = new int[4 * _n]; + + Build(values, 1, 0, _n - 1); + } + + private void Build(int[] values, int node, int start, int end) + { + if (start == end) + { + _tree[node] = values[start]; + } + else + { + int mid = (start + end) / 2; + Build(values, 2 * node, start, mid); + Build(values, 2 * node + 1, mid + 1, end); + _tree[node] = _operation(_tree[2 * node], _tree[2 * node + 1]); + } + } + + /// + /// 区间查询 + /// + public int Query(int left, int right) + { + if (left < 0 || right >= _n || left > right) + throw new ArgumentOutOfRangeException(); + return Query(1, 0, _n - 1, left, right); + } + + private int Query(int node, int start, int end, int left, int right) + { + if (right < start || left > end) + return _identity; + + if (left <= start && end <= right) + return _tree[node]; + + int mid = (start + end) / 2; + int leftResult = Query(2 * node, start, mid, left, right); + int rightResult = Query(2 * node + 1, mid + 1, end, left, right); + return _operation(leftResult, rightResult); + } + + /// + /// 单点更新 + /// + public void Update(int index, int value) + { + if (index < 0 || index >= _n) + throw new ArgumentOutOfRangeException(nameof(index)); + Update(1, 0, _n - 1, index, value); + } + + private void Update(int node, int start, int end, int index, int value) + { + if (start == end) + { + _tree[node] = value; + } + else + { + int mid = (start + end) / 2; + if (index <= mid) + Update(2 * node, start, mid, index, value); + else + Update(2 * node + 1, mid + 1, end, index, value); + _tree[node] = _operation(_tree[2 * node], _tree[2 * node + 1]); + } + } + + /// + /// 获取单个值 + /// + public int Get(int index) + { + return Query(index, index); + } + } + + /// + /// AVL树工具类 + /// + public static class AVLTreeUtil + { + /// + /// 创建AVL树 + /// + public static AVLTree Create() where T : IComparable + { + return new AVLTree(); + } + } + + /// + /// AVL树(自平衡二叉搜索树) + /// + public class AVLTree where T : IComparable + { + private AVLNode _root; + private int _count; + + private class AVLNode + { + public T Value { get; set; } + public AVLNode Left { get; set; } + public AVLNode Right { get; set; } + public int Height { get; set; } + + public AVLNode(T value) + { + Value = value; + Height = 1; + } + + public int BalanceFactor => GetHeight(Left) - GetHeight(Right); + + private static int GetHeight(AVLNode node) => node?.Height ?? 0; + } + + /// + /// 节点数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 添加元素 + /// + public void Add(T value) + { + _root = Add(_root, value); + } + + private AVLNode Add(AVLNode node, T value) + { + if (node == null) + { + _count++; + return new AVLNode(value); + } + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + node.Left = Add(node.Left, value); + else if (cmp > 0) + node.Right = Add(node.Right, value); + else + return node; // 重复值不添加 + + UpdateHeight(node); + return Balance(node); + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + return Contains(_root, value); + } + + private bool Contains(AVLNode node, T value) + { + if (node == null) + return false; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + return Contains(node.Left, value); + if (cmp > 0) + return Contains(node.Right, value); + return true; + } + + /// + /// 移除元素 + /// + public bool Remove(T value) + { + int oldCount = _count; + _root = Remove(_root, value); + return _count < oldCount; + } + + private AVLNode Remove(AVLNode node, T value) + { + if (node == null) + return null; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Remove(node.Left, value); + } + else if (cmp > 0) + { + node.Right = Remove(node.Right, value); + } + else + { + _count--; + if (node.Left == null) + return node.Right; + if (node.Right == null) + return node.Left; + + var successor = FindMin(node.Right); + node.Value = successor.Value; + node.Right = Remove(node.Right, successor.Value); + _count++; + } + + UpdateHeight(node); + return Balance(node); + } + + private AVLNode FindMin(AVLNode node) + { + while (node.Left != null) + node = node.Left; + return node; + } + + private void UpdateHeight(AVLNode node) + { + int leftHeight = node.Left?.Height ?? 0; + int rightHeight = node.Right?.Height ?? 0; + node.Height = 1 + Math.Max(leftHeight, rightHeight); + } + + private AVLNode Balance(AVLNode node) + { + int balance = node.BalanceFactor; + + // 左重 + if (balance > 1) + { + if (node.Left.BalanceFactor < 0) + node.Left = RotateLeft(node.Left); + return RotateRight(node); + } + + // 右重 + if (balance < -1) + { + if (node.Right.BalanceFactor > 0) + node.Right = RotateRight(node.Right); + return RotateLeft(node); + } + + return node; + } + + private AVLNode RotateRight(AVLNode y) + { + var x = y.Left; + y.Left = x.Right; + x.Right = y; + + UpdateHeight(y); + UpdateHeight(x); + + return x; + } + + private AVLNode RotateLeft(AVLNode x) + { + var y = x.Right; + x.Right = y.Left; + y.Left = x; + + UpdateHeight(x); + UpdateHeight(y); + + return y; + } + + /// + /// 中序遍历 + /// + public IEnumerable InOrderTraversal() + { + return InOrderTraversal(_root); + } + + private IEnumerable InOrderTraversal(AVLNode node) + { + if (node == null) + yield break; + + foreach (var value in InOrderTraversal(node.Left)) + yield return value; + + yield return node.Value; + + foreach (var value in InOrderTraversal(node.Right)) + yield return value; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TrieUtil.cs b/EasyTool.Core/CollectionsCategory/TrieUtil.cs new file mode 100644 index 0000000..8c0619e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TrieUtil.cs @@ -0,0 +1,262 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Trie 树(前缀树)工具类 + /// 用于高效的字符串前缀搜索、自动补全等场景 + /// + public static class TrieUtil + { + /// + /// 创建 Trie 树 + /// + public static Trie Create() + { + return new Trie(); + } + + /// + /// 从字符串集合创建 Trie 树 + /// + public static Trie Create(IEnumerable words) + { + var trie = new Trie(); + foreach (var word in words) + { + trie.Add(word); + } + return trie; + } + } + + /// + /// Trie 树实现 + /// + public class Trie + { + private readonly TrieNode _root; + + /// + /// 已存储的单词数量 + /// + public int Count { get; private set; } + + /// + /// 创建 Trie 树 + /// + public Trie() + { + _root = new TrieNode(); + Count = 0; + } + + /// + /// 添加单词 + /// + public void Add(string word) + { + if (word == null) + throw new ArgumentNullException(nameof(word)); + + var node = _root; + foreach (char c in word) + { + if (!node.Children.ContainsKey(c)) + { + node.Children[c] = new TrieNode(); + } + node = node.Children[c]; + } + + if (!node.IsEndOfWord) + { + node.IsEndOfWord = true; + Count++; + } + } + + /// + /// 批量添加单词 + /// + public void AddRange(IEnumerable words) + { + foreach (var word in words) + { + Add(word); + } + } + + /// + /// 移除单词 + /// + public bool Remove(string word) + { + if (word == null) + throw new ArgumentNullException(nameof(word)); + + return Remove(_root, word, 0); + } + + private bool Remove(TrieNode node, string word, int index) + { + if (index == word.Length) + { + if (!node.IsEndOfWord) + return false; + + node.IsEndOfWord = false; + Count--; + return node.Children.Count == 0; + } + + char c = word[index]; + if (!node.Children.ContainsKey(c)) + return false; + + bool shouldDeleteChild = Remove(node.Children[c], word, index + 1); + + if (shouldDeleteChild) + { + node.Children.Remove(c); + return !node.IsEndOfWord && node.Children.Count == 0; + } + + return false; + } + + /// + /// 是否包含完整单词 + /// + public bool Contains(string word) + { + var node = FindNode(word); + return node != null && node.IsEndOfWord; + } + + /// + /// 是否包含指定前缀 + /// + public bool StartsWith(string prefix) + { + return FindNode(prefix) != null; + } + + /// + /// 获取所有以指定前缀开头的单词 + /// + public IEnumerable GetWordsWithPrefix(string prefix) + { + var node = FindNode(prefix); + if (node == null) + return Enumerable.Empty(); + + return GetAllWords(node, prefix); + } + + /// + /// 自动补全(获取以指定前缀开头的所有单词) + /// + public IEnumerable AutoComplete(string prefix, int maxResults = 10) + { + return GetWordsWithPrefix(prefix).Take(maxResults); + } + + /// + /// 获取所有单词 + /// + public IEnumerable GetAllWords() + { + return GetAllWords(_root, ""); + } + + /// + /// 清空 + /// + public void Clear() + { + _root.Children.Clear(); + Count = 0; + } + + /// + /// 获取最长公共前缀 + /// + public string GetLongestCommonPrefix() + { + var prefix = new System.Text.StringBuilder(); + var node = _root; + + while (node.Children.Count == 1 && !node.IsEndOfWord) + { + var child = node.Children.First(); + prefix.Append(child.Key); + node = child.Value; + } + + return prefix.ToString(); + } + + /// + /// 计算与指定单词匹配的前缀长度 + /// + public int MatchPrefixLength(string word) + { + if (word == null) + return 0; + + var node = _root; + int length = 0; + + foreach (char c in word) + { + if (!node.Children.ContainsKey(c)) + break; + + length++; + node = node.Children[c]; + } + + return length; + } + + private TrieNode FindNode(string prefix) + { + if (prefix == null) + return null; + + var node = _root; + foreach (char c in prefix) + { + if (!node.Children.ContainsKey(c)) + return null; + node = node.Children[c]; + } + return node; + } + + private IEnumerable GetAllWords(TrieNode node, string prefix) + { + if (node.IsEndOfWord) + { + yield return prefix; + } + + foreach (var child in node.Children) + { + foreach (var word in GetAllWords(child.Value, prefix + child.Key)) + { + yield return word; + } + } + } + + private class TrieNode + { + public Dictionary Children { get; } = new Dictionary(); + public bool IsEndOfWord { get; set; } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs b/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs new file mode 100644 index 0000000..b06c669 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs @@ -0,0 +1,273 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 并查集工具类 + /// 用于高效处理元素分组和连通性问题 + /// 支持 union-find 操作,近乎常数时间复杂度 + /// + public static class UnionFindUtil + { + /// + /// 创建并查集 + /// + /// 元素数量 + /// 并查集实例 + public static UnionFind Create(int size) + { + return new UnionFind(size); + } + + /// + /// 从元素集合创建并查集 + /// + public static UnionFind Create(IEnumerable elements) + { + return new UnionFind(elements); + } + } + + /// + /// 整数并查集实现 + /// + public class UnionFind + { + private readonly int[] _parent; + private readonly int[] _rank; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 创建并查集 + /// + /// 元素数量 + public UnionFind(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _parent = new int[size]; + _rank = new int[size]; + _count = size; + + for (int i = 0; i < size; i++) + { + _parent[i] = i; + _rank[i] = 1; + } + } + + /// + /// 查找元素所属的集合(带路径压缩) + /// + public int Find(int x) + { + if (x < 0 || x >= _parent.Length) + throw new ArgumentOutOfRangeException(nameof(x)); + + if (_parent[x] != x) + { + _parent[x] = Find(_parent[x]); // 路径压缩 + } + return _parent[x]; + } + + /// + /// 合并两个元素所属的集合 + /// + public void Union(int x, int y) + { + int rootX = Find(x); + int rootY = Find(y); + + if (rootX == rootY) + return; + + // 按秩合并 + if (_rank[rootX] < _rank[rootY]) + { + _parent[rootX] = rootY; + } + else if (_rank[rootX] > _rank[rootY]) + { + _parent[rootY] = rootX; + } + else + { + _parent[rootY] = rootX; + _rank[rootX]++; + } + + _count--; + } + + /// + /// 判断两个元素是否属于同一集合 + /// + public bool Connected(int x, int y) + { + return Find(x) == Find(y); + } + + /// + /// 获取元素所在集合的大小 + /// + public int GetSetSize(int x) + { + int root = Find(x); + int size = 0; + for (int i = 0; i < _parent.Length; i++) + { + if (Find(i) == root) + size++; + } + return size; + } + + /// + /// 获取所有集合 + /// + public Dictionary> GetAllSets() + { + var sets = new Dictionary>(); + + for (int i = 0; i < _parent.Length; i++) + { + int root = Find(i); + if (!sets.ContainsKey(root)) + { + sets[root] = new List(); + } + sets[root].Add(i); + } + + return sets; + } + } + + /// + /// 泛型并查集实现 + /// + /// 元素类型 + public class UnionFind + { + private readonly Dictionary _parent; + private readonly Dictionary _rank; + private int _count; + + /// + /// 集合数量 + /// + public int Count => _count; + + /// + /// 创建并查集 + /// + public UnionFind(IEnumerable elements) + { + if (elements == null) + throw new ArgumentNullException(nameof(elements)); + + _parent = new Dictionary(); + _rank = new Dictionary(); + + foreach (var element in elements) + { + _parent[element] = element; + _rank[element] = 1; + } + + _count = _parent.Count; + } + + /// + /// 添加元素 + /// + public void Add(T element) + { + if (!_parent.ContainsKey(element)) + { + _parent[element] = element; + _rank[element] = 1; + _count++; + } + } + + /// + /// 查找元素所属的集合 + /// + public T Find(T x) + { + if (!_parent.ContainsKey(x)) + throw new KeyNotFoundException($"Element '{x}' not found"); + + if (!EqualityComparer.Default.Equals(_parent[x], x)) + { + _parent[x] = Find(_parent[x]); + } + return _parent[x]; + } + + /// + /// 合并两个元素所属的集合 + /// + public void Union(T x, T y) + { + T rootX = Find(x); + T rootY = Find(y); + + if (EqualityComparer.Default.Equals(rootX, rootY)) + return; + + if (_rank[rootX] < _rank[rootY]) + { + _parent[rootX] = rootY; + } + else if (_rank[rootX] > _rank[rootY]) + { + _parent[rootY] = rootX; + } + else + { + _parent[rootY] = rootX; + _rank[rootX]++; + } + + _count--; + } + + /// + /// 判断两个元素是否属于同一集合 + /// + public bool Connected(T x, T y) + { + return EqualityComparer.Default.Equals(Find(x), Find(y)); + } + + /// + /// 获取所有集合 + /// + public Dictionary> GetAllSets() + { + var sets = new Dictionary>(); + + foreach (var element in _parent.Keys) + { + T root = Find(element); + if (!sets.ContainsKey(root)) + { + sets[root] = new List(); + } + sets[root].Add(element); + } + + return sets; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/WindowUtil.cs b/EasyTool.Core/CollectionsCategory/WindowUtil.cs new file mode 100644 index 0000000..c1a1786 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/WindowUtil.cs @@ -0,0 +1,1015 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 滑动窗口工具类 + /// 提供滑动窗口、滑动平均等功能 + /// + public static class SlidingWindowUtil + { + /// + /// 创建滑动窗口枚举器 + /// + /// 元素类型 + /// 源集合 + /// 窗口大小 + /// 每个窗口的元素数组 + public static IEnumerable Windows(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var window = new Queue(); + foreach (var item in source) + { + window.Enqueue(item); + if (window.Count > windowSize) + { + window.Dequeue(); + } + if (window.Count == windowSize) + { + yield return window.ToArray(); + } + } + } + + /// + /// 创建滑动窗口(带步长) + /// + public static IEnumerable Windows(IEnumerable source, int windowSize, int step) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + if (step <= 0) + throw new ArgumentOutOfRangeException(nameof(step)); + + var list = source.ToList(); + for (int i = 0; i <= list.Count - windowSize; i += step) + { + var window = new T[windowSize]; + for (int j = 0; j < windowSize; j++) + { + window[j] = list[i + j]; + } + yield return window; + } + } + + /// + /// 滑动求和 + /// + public static IEnumerable SlidingSum(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + double sum = 0; + var queue = new Queue(); + + foreach (var item in source) + { + queue.Enqueue(item); + sum += item; + + if (queue.Count > windowSize) + { + sum -= queue.Dequeue(); + } + + if (queue.Count == windowSize) + { + yield return sum; + } + } + } + + /// + /// 滑动平均 + /// + public static IEnumerable SlidingAverage(IEnumerable source, int windowSize) + { + return SlidingSum(source, windowSize).Select(sum => sum / windowSize); + } + + /// + /// 滑动最大值 + /// + public static IEnumerable SlidingMax(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var deque = new LinkedList(); // 存储索引 + var list = source.ToList(); + + for (int i = 0; i < list.Count; i++) + { + // 移除窗口外的元素 + while (deque.Count > 0 && deque.First.Value <= i - windowSize) + { + deque.RemoveFirst(); + } + + // 移除比当前元素小的元素(它们不可能是最大值) + while (deque.Count > 0 && list[deque.Last.Value] <= list[i]) + { + deque.RemoveLast(); + } + + deque.AddLast(i); + + if (i >= windowSize - 1) + { + yield return list[deque.First.Value]; + } + } + } + + /// + /// 滑动最小值 + /// + public static IEnumerable SlidingMin(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var deque = new LinkedList(); + var list = source.ToList(); + + for (int i = 0; i < list.Count; i++) + { + while (deque.Count > 0 && deque.First.Value <= i - windowSize) + { + deque.RemoveFirst(); + } + + while (deque.Count > 0 && list[deque.Last.Value] >= list[i]) + { + deque.RemoveLast(); + } + + deque.AddLast(i); + + if (i >= windowSize - 1) + { + yield return list[deque.First.Value]; + } + } + } + } + + /// + /// 分块工具类 + /// + public static class ChunkUtil + { + /// + /// 将集合分块 + /// + public static IEnumerable> Chunk(IEnumerable source, int chunkSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (chunkSize <= 0) + throw new ArgumentOutOfRangeException(nameof(chunkSize)); + + var chunk = new List(chunkSize); + foreach (var item in source) + { + chunk.Add(item); + if (chunk.Count == chunkSize) + { + yield return chunk; + chunk = new List(chunkSize); + } + } + + if (chunk.Count > 0) + { + yield return chunk; + } + } + + /// + /// 将集合分成指定数量的块 + /// + public static List> SplitInto(IEnumerable source, int chunkCount) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (chunkCount <= 0) + throw new ArgumentOutOfRangeException(nameof(chunkCount)); + + var list = source.ToList(); + if (list.Count == 0) + return new List>(); + + var result = new List>(); + int baseSize = list.Count / chunkCount; + int extra = list.Count % chunkCount; + + int index = 0; + for (int i = 0; i < chunkCount; i++) + { + int size = baseSize + (i < extra ? 1 : 0); + var chunk = new List(); + for (int j = 0; j < size && index < list.Count; j++) + { + chunk.Add(list[index++]); + } + result.Add(chunk); + } + + return result; + } + + /// + /// 按条件分块 + /// + public static IEnumerable> ChunkBy(IEnumerable source, Func predicate) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicate == null) + throw new ArgumentNullException(nameof(predicate)); + + var chunk = new List(); + foreach (var item in source) + { + if (predicate(item)) + { + if (chunk.Count > 0) + { + yield return chunk; + chunk = new List(); + } + } + else + { + chunk.Add(item); + } + } + + if (chunk.Count > 0) + { + yield return chunk; + } + } + } + + /// + /// 分区工具类 + /// + public static class PartitionUtil + { + /// + /// 按谓词将集合分成两部分 + /// + public static (List True, List False) Partition(IEnumerable source, Func predicate) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicate == null) + throw new ArgumentNullException(nameof(predicate)); + + var trueList = new List(); + var falseList = new List(); + + foreach (var item in source) + { + if (predicate(item)) + trueList.Add(item); + else + falseList.Add(item); + } + + return (trueList, falseList); + } + + /// + /// 将集合分成多个分区 + /// + public static List> PartitionBy(IEnumerable source, params Func[] predicates) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicates == null || predicates.Length == 0) + throw new ArgumentException("At least one predicate is required"); + + var result = new List>(); + for (int i = 0; i < predicates.Length; i++) + { + result.Add(new List()); + } + result.Add(new List()); // 默认分区(不满足任何谓词) + + foreach (var item in source) + { + bool matched = false; + for (int i = 0; i < predicates.Length; i++) + { + if (predicates[i](item)) + { + result[i].Add(item); + matched = true; + break; + } + } + if (!matched) + { + result[predicates.Length].Add(item); + } + } + + return result; + } + + /// + /// 交替分区 + /// + public static (List First, List Second) Alternate(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var first = new List(); + var second = new List(); + bool isFirst = true; + + foreach (var item in source) + { + if (isFirst) + first.Add(item); + else + second.Add(item); + isFirst = !isFirst; + } + + return (first, second); + } + + /// + /// 按比例分割 + /// + public static (List First, List Second) SplitByRatio(IEnumerable source, double firstRatio) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (firstRatio < 0 || firstRatio > 1) + throw new ArgumentOutOfRangeException(nameof(firstRatio)); + + var list = source.ToList(); + int firstCount = (int)(list.Count * firstRatio); + + var first = new List(); + var second = new List(); + + for (int i = 0; i < list.Count; i++) + { + if (i < firstCount) + first.Add(list[i]); + else + second.Add(list[i]); + } + + return (first, second); + } + } + + /// + /// 交错工具类 + /// + public static class InterleaveUtil + { + /// + /// 交错合并两个集合 + /// + public static IEnumerable Interleave(IEnumerable first, IEnumerable second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + using var enum1 = first.GetEnumerator(); + using var enum2 = second.GetEnumerator(); + + bool hasFirst, hasSecond; + while (true) + { + hasFirst = enum1.MoveNext(); + hasSecond = enum2.MoveNext(); + + if (!hasFirst && !hasSecond) + break; + + if (hasFirst) + yield return enum1.Current; + if (hasSecond) + yield return enum2.Current; + } + } + + /// + /// 交错合并多个集合 + /// + public static IEnumerable Interleave(params IEnumerable[] sources) + { + if (sources == null || sources.Length == 0) + yield break; + + var enumerators = new List>(); + try + { + foreach (var source in sources) + { + if (source != null) + enumerators.Add(source.GetEnumerator()); + } + + bool anyHasNext = true; + while (anyHasNext) + { + anyHasNext = false; + foreach (var e in enumerators) + { + if (e.MoveNext()) + { + yield return e.Current; + anyHasNext = true; + } + } + } + } + finally + { + foreach (var e in enumerators) + { + e.Dispose(); + } + } + } + + /// + /// 以指定元素为分隔交错 + /// + public static IEnumerable Intersperse(IEnumerable source, T separator) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + bool first = true; + foreach (var item in source) + { + if (!first) + yield return separator; + first = false; + yield return item; + } + } + } + + /// + /// 旋转工具类 + /// + public static class RotateUtil + { + /// + /// 左旋转 + /// + public static List RotateLeft(IEnumerable source, int positions) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + return list; + + positions = positions % list.Count; + if (positions < 0) + positions += list.Count; + + if (positions == 0) + return list; + + var result = new List(list.Count); + result.AddRange(list.Skip(positions)); + result.AddRange(list.Take(positions)); + return result; + } + + /// + /// 右旋转 + /// + public static List RotateRight(IEnumerable source, int positions) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + return list; + + positions = positions % list.Count; + if (positions < 0) + positions += list.Count; + + if (positions == 0) + return list; + + return RotateLeft(list, list.Count - positions); + } + } + + /// + /// 水库采样工具类 + /// + public static class ReservoirSamplingUtil + { + /// + /// 从集合中随机采样指定数量的元素 + /// + public static List Sample(IEnumerable source, int sampleSize, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (sampleSize <= 0) + throw new ArgumentOutOfRangeException(nameof(sampleSize)); + + random ??= new Random(); + var reservoir = new List(); + int index = 0; + + foreach (var item in source) + { + if (index < sampleSize) + { + reservoir.Add(item); + } + else + { + int j = random.Next(index + 1); + if (j < sampleSize) + { + reservoir[j] = item; + } + } + index++; + } + + return reservoir; + } + + /// + /// 加权随机采样 + /// + public static List WeightedSample(IEnumerable source, Func weightSelector, int sampleSize, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + if (sampleSize <= 0) + throw new ArgumentOutOfRangeException(nameof(sampleSize)); + + random ??= new Random(); + var list = source.ToList(); + + // 使用别名方法采样 + var weights = list.Select(weightSelector).ToList(); + double totalWeight = weights.Sum(); + + var result = new List(); + var used = new HashSet(); + + while (result.Count < sampleSize && used.Count < list.Count) + { + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + if (used.Contains(i)) + continue; + + cumulative += weights[i]; + if (r <= cumulative) + { + result.Add(list[i]); + used.Add(i); + totalWeight -= weights[i]; + break; + } + } + } + + return result; + } + } + + /// + /// 集合差异工具类 + /// + public static class CollectionDiffUtil + { + /// + /// 计算集合差异 + /// + public static CollectionDiff Diff(IEnumerable oldCollection, IEnumerable newCollection) + { + if (oldCollection == null) + throw new ArgumentNullException(nameof(oldCollection)); + if (newCollection == null) + throw new ArgumentNullException(nameof(newCollection)); + + var oldSet = oldCollection.ToHashSet(); + var newSet = newCollection.ToHashSet(); + + var added = newSet.Except(oldSet).ToList(); + var removed = oldSet.Except(newSet).ToList(); + var unchanged = oldSet.Intersect(newSet).ToList(); + + return new CollectionDiff + { + Added = added, + Removed = removed, + Unchanged = unchanged + }; + } + + /// + /// 计算集合差异(使用键选择器) + /// + public static CollectionDiffByKey DiffByKey( + IEnumerable oldCollection, + IEnumerable newCollection, + Func keySelector) where TKey : IEquatable + { + if (oldCollection == null) + throw new ArgumentNullException(nameof(oldCollection)); + if (newCollection == null) + throw new ArgumentNullException(nameof(newCollection)); + if (keySelector == null) + throw new ArgumentNullException(nameof(keySelector)); + + var oldDict = oldCollection.ToDictionary(keySelector); + var newDict = newCollection.ToDictionary(keySelector); + + var oldKeys = oldDict.Keys.ToHashSet(); + var newKeys = newDict.Keys.ToHashSet(); + + var added = newKeys.Except(oldKeys).Select(k => newDict[k]).ToList(); + var removed = oldKeys.Except(newKeys).Select(k => oldDict[k]).ToList(); + var unchanged = oldKeys.Intersect(newKeys).ToList(); + + // 检测修改的项 + var modified = new List>(); + foreach (var key in unchanged) + { + if (!EqualityComparer.Default.Equals(oldDict[key], newDict[key])) + { + modified.Add(new CollectionDiffItem + { + Key = key, + OldValue = oldDict[key], + NewValue = newDict[key] + }); + } + } + + return new CollectionDiffByKey + { + Added = added, + Removed = removed, + Modified = modified, + UnchangedKeys = unchanged + }; + } + + /// + /// 同步集合 + /// + public static void Sync( + ICollection target, + IEnumerable source, + IEqualityComparer comparer = null) + { + if (target == null) + throw new ArgumentNullException(nameof(target)); + if (source == null) + throw new ArgumentNullException(nameof(source)); + + comparer ??= EqualityComparer.Default; + var sourceSet = source.ToHashSet(comparer); + + // 移除不在源中的项 + var toRemove = target.Where(t => !sourceSet.Contains(t)).ToList(); + foreach (var item in toRemove) + { + target.Remove(item); + } + + // 添加不在目标中的项 + var targetSet = target.ToHashSet(comparer); + foreach (var item in sourceSet) + { + if (!targetSet.Contains(item)) + { + target.Add(item); + } + } + } + } + + /// + /// 集合差异结果 + /// + public class CollectionDiff + { + /// + /// 新增的元素 + /// + public List Added { get; set; } + + /// + /// 移除的元素 + /// + public List Removed { get; set; } + + /// + /// 未变化的元素 + /// + public List Unchanged { get; set; } + + /// + /// 是否有变化 + /// + public bool HasChanges => Added.Count > 0 || Removed.Count > 0; + } + + /// + /// 按键的集合差异结果 + /// + public class CollectionDiffByKey + { + /// + /// 新增的元素 + /// + public List Added { get; set; } + + /// + /// 移除的元素 + /// + public List Removed { get; set; } + + /// + /// 修改的元素 + /// + public List> Modified { get; set; } + + /// + /// 未变化的键 + /// + public List UnchangedKeys { get; set; } + + /// + /// 是否有变化 + /// + public bool HasChanges => Added.Count > 0 || Removed.Count > 0 || Modified.Count > 0; + } + + /// + /// 差异项 + /// + public class CollectionDiffItem + { + /// + /// 键 + /// + public TKey Key { get; set; } + + /// + /// 旧值 + /// + public T OldValue { get; set; } + + /// + /// 新值 + /// + public T NewValue { get; set; } + } + + /// + /// 加权随机工具类 + /// + public static class WeightedRandomUtil + { + /// + /// 按权重随机选择一个元素 + /// + public static T Select(IEnumerable source, Func weightSelector, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + + random ??= new Random(); + var list = source.ToList(); + + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + var weights = list.Select(weightSelector).ToList(); + double totalWeight = weights.Sum(); + + if (totalWeight <= 0) + throw new ArgumentException("Total weight must be positive"); + + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + cumulative += weights[i]; + if (r <= cumulative) + { + return list[i]; + } + } + + return list[list.Count - 1]; + } + + /// + /// 按权重随机选择多个元素(不放回) + /// + public static List SelectMultiple(IEnumerable source, Func weightSelector, int count, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + if (count <= 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + random ??= new Random(); + var list = source.ToList(); + var weights = list.Select(weightSelector).ToList(); + var result = new List(); + var selected = new HashSet(); + + while (result.Count < count && selected.Count < list.Count) + { + double totalWeight = 0; + for (int i = 0; i < list.Count; i++) + { + if (!selected.Contains(i)) + totalWeight += weights[i]; + } + + if (totalWeight <= 0) + break; + + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + if (selected.Contains(i)) + continue; + + cumulative += weights[i]; + if (r <= cumulative) + { + result.Add(list[i]); + selected.Add(i); + break; + } + } + } + + return result; + } + + /// + /// 创建别名表以进行高效加权随机采样 + /// + public static AliasTable CreateAliasTable(IEnumerable source, Func weightSelector) + { + return new AliasTable(source, weightSelector); + } + } + + /// + /// 别名表(用于 O(1) 加权随机采样) + /// + public class AliasTable + { + private readonly T[] _items; + private readonly double[] _prob; + private readonly int[] _alias; + private readonly Random _random; + + /// + /// 创建别名表 + /// + public AliasTable(IEnumerable source, Func weightSelector) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + + _items = source.ToArray(); + if (_items.Length == 0) + throw new ArgumentException("Collection is empty"); + + int n = _items.Length; + var weights = _items.Select(weightSelector).ToArray(); + double totalWeight = weights.Sum(); + + _prob = new double[n]; + _alias = new int[n]; + _random = new Random(); + + // 归一化权重 + for (int i = 0; i < n; i++) + { + _prob[i] = weights[i] * n / totalWeight; + } + + var small = new Stack(); + var large = new Stack(); + + for (int i = 0; i < n; i++) + { + if (_prob[i] < 1.0) + small.Push(i); + else + large.Push(i); + } + + while (small.Count > 0 && large.Count > 0) + { + int l = small.Pop(); + int g = large.Pop(); + + _alias[l] = g; + _prob[g] = _prob[g] + _prob[l] - 1.0; + + if (_prob[g] < 1.0) + small.Push(g); + else + large.Push(g); + } + + while (large.Count > 0) + { + _prob[large.Pop()] = 1.0; + } + + while (small.Count > 0) + { + _prob[small.Pop()] = 1.0; + } + } + + /// + /// 随机选择一个元素 + /// + public T Next() + { + int i = _random.Next(_items.Length); + return _random.NextDouble() < _prob[i] ? _items[i] : _items[_alias[i]]; + } + + /// + /// 随机选择多个元素(可能重复) + /// + public List NextMultiple(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Next()); + } + return result; + } + } +} diff --git a/EasyTool.Core/ColorCategory/ColorUtil.cs b/EasyTool.Core/ColorCategory/ColorUtil.cs new file mode 100644 index 0000000..f7bd219 --- /dev/null +++ b/EasyTool.Core/ColorCategory/ColorUtil.cs @@ -0,0 +1,493 @@ +using System; + +namespace EasyTool.ColorCategory +{ + /// + /// 颜色工具类 + /// 提供颜色空间转换和颜色操作功能 + /// + public static class ColorUtil + { + /// + /// RGB 转 HSL + /// + public static HSL RGBToHSL(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double max = Math.Max(rd, Math.Max(gd, bd)); + double min = Math.Min(rd, Math.Min(gd, bd)); + double h = 0, s = 0, l = (max + min) / 2; + + if (Math.Abs(max - min) > 0.0001) + { + double d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + + if (Math.Abs(max - rd) < 0.0001) + h = (gd - bd) / d + (gd < bd ? 6 : 0); + else if (Math.Abs(max - gd) < 0.0001) + h = (bd - rd) / d + 2; + else + h = (rd - gd) / d + 4; + + h /= 6; + } + + return new HSL(h * 360, s * 100, l * 100); + } + + /// + /// HSL 转 RGB + /// + public static RGB HSLToRGB(double h, double s, double l) + { + h /= 360; + s /= 100; + l /= 100; + + double r, g, b; + + if (Math.Abs(s) < 0.0001) + { + r = g = b = l; + } + else + { + double q = l < 0.5 ? l * (1 + s) : l + s - l * s; + double p = 2 * l - q; + + r = HueToRGB(p, q, h + 1.0 / 3.0); + g = HueToRGB(p, q, h); + b = HueToRGB(p, q, h - 1.0 / 3.0); + } + + return new RGB((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255)); + } + + private static double HueToRGB(double p, double q, double t) + { + if (t < 0) t += 1; + if (t > 1) t -= 1; + + if (t < 1.0 / 6.0) return p + (q - p) * 6 * t; + if (t < 1.0 / 2.0) return q; + if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6; + + return p; + } + + /// + /// RGB 转 HSV + /// + public static HSV RGBToHSV(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double max = Math.Max(rd, Math.Max(gd, bd)); + double min = Math.Min(rd, Math.Min(gd, bd)); + double h = 0, s = max == 0 ? 0 : (max - min) / max, v = max; + + if (Math.Abs(max - min) > 0.0001) + { + double d = max - min; + + if (Math.Abs(max - rd) < 0.0001) + h = (gd - bd) / d + (gd < bd ? 6 : 0); + else if (Math.Abs(max - gd) < 0.0001) + h = (bd - rd) / d + 2; + else + h = (rd - gd) / d + 4; + + h /= 6; + } + + return new HSV(h * 360, s * 100, v * 100); + } + + /// + /// HSV 转 RGB + /// + public static RGB HSVToRGB(double h, double s, double v) + { + h /= 360; + s /= 100; + v /= 100; + + int i = (int)Math.Floor(h * 6); + double f = h * 6 - i; + double p = v * (1 - s); + double q = v * (1 - f * s); + double t = v * (1 - (1 - f) * s); + + double r, g, b; + + switch (i % 6) + { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + default: r = v; g = p; b = q; break; + } + + return new RGB((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255)); + } + + /// + /// RGB 转 CMYK + /// + public static CMYK RGBToCMYK(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double k = 1 - Math.Max(rd, Math.Max(gd, bd)); + + if (Math.Abs(k - 1) < 0.0001) + { + return new CMYK(0, 0, 0, 100); + } + + double c = (1 - rd - k) / (1 - k); + double m = (1 - gd - k) / (1 - k); + double y = (1 - bd - k) / (1 - k); + + return new CMYK(c * 100, m * 100, y * 100, k * 100); + } + + /// + /// CMYK 转 RGB + /// + public static RGB CMYKToRGB(double c, double m, double y, double k) + { + c /= 100; + m /= 100; + y /= 100; + k /= 100; + + int r = (int)Math.Round(255 * (1 - c) * (1 - k)); + int g = (int)Math.Round(255 * (1 - m) * (1 - k)); + int b = (int)Math.Round(255 * (1 - y) * (1 - k)); + + return new RGB(r, g, b); + } + + /// + /// RGB 转十六进制 + /// + public static string RGBToHex(int r, int g, int b) + { + return $"#{r:X2}{g:X2}{b:X2}"; + } + + /// + /// 十六进制转 RGB + /// + public static RGB HexToRGB(string hex) + { + hex = hex.TrimStart('#'); + + if (hex.Length == 3) + { + hex = $"{hex[0]}{hex[0]}{hex[1]}{hex[1]}{hex[2]}{hex[2]}"; + } + + int r = Convert.ToInt32(hex.Substring(0, 2), 16); + int g = Convert.ToInt32(hex.Substring(2, 2), 16); + int b = Convert.ToInt32(hex.Substring(4, 2), 16); + + return new RGB(r, g, b); + } + + /// + /// 计算两个颜色的对比度 + /// + public static double ContrastRatio(RGB color1, RGB color2) + { + double lum1 = RelativeLuminance(color1); + double lum2 = RelativeLuminance(color2); + + double lighter = Math.Max(lum1, lum2); + double darker = Math.Min(lum1, lum2); + + return (lighter + 0.05) / (darker + 0.05); + } + + /// + /// 计算相对亮度 + /// + public static double RelativeLuminance(RGB color) + { + double r = color.R / 255.0; + double g = color.G / 255.0; + double b = color.B / 255.0; + + r = r <= 0.03928 ? r / 12.92 : Math.Pow((r + 0.055) / 1.055, 2.4); + g = g <= 0.03928 ? g / 12.92 : Math.Pow((g + 0.055) / 1.055, 2.4); + b = b <= 0.03928 ? b / 12.92 : Math.Pow((b + 0.055) / 1.055, 2.4); + + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + + /// + /// 混合两个颜色 + /// + public static RGB Blend(RGB color1, RGB color2, double ratio = 0.5) + { + ratio = Math.Max(0, Math.Min(1, ratio)); + + int r = (int)Math.Round(color1.R * (1 - ratio) + color2.R * ratio); + int g = (int)Math.Round(color1.G * (1 - ratio) + color2.G * ratio); + int b = (int)Math.Round(color1.B * (1 - ratio) + color2.B * ratio); + + return new RGB(r, g, b); + } + + /// + /// 调整亮度 + /// + public static RGB AdjustBrightness(RGB color, double amount) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL(hsl.H, hsl.S, Math.Max(0, Math.Min(100, hsl.L + amount))); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 调整饱和度 + /// + public static RGB AdjustSaturation(RGB color, double amount) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL(hsl.H, Math.Max(0, Math.Min(100, hsl.S + amount)), hsl.L); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 获取互补色 + /// + public static RGB GetComplementary(RGB color) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL((hsl.H + 180) % 360, hsl.S, hsl.L); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 获取灰度色 + /// + public static RGB ToGrayscale(RGB color) + { + int gray = (int)Math.Round(0.299 * color.R + 0.587 * color.G + 0.114 * color.B); + return new RGB(gray, gray, gray); + } + + /// + /// 反转颜色 + /// + public static RGB Invert(RGB color) + { + return new RGB(255 - color.R, 255 - color.G, 255 - color.B); + } + } + + /// + /// RGB 颜色 + /// + public readonly struct RGB + { + /// 红 (0-255) + public int R { get; } + /// 绿 (0-255) + public int G { get; } + /// 蓝 (0-255) + public int B { get; } + + public RGB(int r, int g, int b) + { + R = Math.Clamp(r, 0, 255); + G = Math.Clamp(g, 0, 255); + B = Math.Clamp(b, 0, 255); + } + + public string ToHex() => ColorUtil.RGBToHex(R, G, B); + + public override string ToString() => $"RGB({R}, {G}, {B})"; + } + + /// + /// HSL 颜色 + /// + public readonly struct HSL + { + /// 色相 (0-360) + public double H { get; } + /// 饱和度 (0-100) + public double S { get; } + /// 亮度 (0-100) + public double L { get; } + + public HSL(double h, double s, double l) + { + H = ((h % 360) + 360) % 360; + S = Math.Clamp(s, 0, 100); + L = Math.Clamp(l, 0, 100); + } + + public RGB ToRGB() => ColorUtil.HSLToRGB(H, S, L); + + public override string ToString() => $"HSL({H:F1}°, {S:F1}%, {L:F1}%)"; + } + + /// + /// HSV 颜色 + /// + public readonly struct HSV + { + /// 色相 (0-360) + public double H { get; } + /// 饱和度 (0-100) + public double S { get; } + /// 明度 (0-100) + public double V { get; } + + public HSV(double h, double s, double v) + { + H = ((h % 360) + 360) % 360; + S = Math.Clamp(s, 0, 100); + V = Math.Clamp(v, 0, 100); + } + + public RGB ToRGB() => ColorUtil.HSVToRGB(H, S, V); + + public override string ToString() => $"HSV({H:F1}°, {S:F1}%, {V:F1}%)"; + } + + /// + /// CMYK 颜色 + /// + public readonly struct CMYK + { + /// 青 (0-100) + public double C { get; } + /// 品红 (0-100) + public double M { get; } + /// 黄 (0-100) + public double Y { get; } + /// 黑 (0-100) + public double K { get; } + + public CMYK(double c, double m, double y, double k) + { + C = Math.Clamp(c, 0, 100); + M = Math.Clamp(m, 0, 100); + Y = Math.Clamp(y, 0, 100); + K = Math.Clamp(k, 0, 100); + } + + public RGB ToRGB() => ColorUtil.CMYKToRGB(C, M, Y, K); + + public override string ToString() => $"CMYK({C:F1}%, {M:F1}%, {Y:F1}%, {K:F1}%)"; + } + + /// + /// 调色板工具类 + /// + public static class ColorPaletteUtil + { + /// + /// 生成类似色配色方案 + /// + public static RGB[] GetAnalogous(RGB baseColor, int count = 3) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + var colors = new RGB[count]; + + for (int i = 0; i < count; i++) + { + double h = (hsl.H + i * 30 - (count - 1) * 15 + 360) % 360; + colors[i] = ColorUtil.HSLToRGB(h, hsl.S, hsl.L); + } + + return colors; + } + + /// + /// 生成互补色配色方案 + /// + public static RGB[] GetComplementary(RGB baseColor) + { + return new[] { baseColor, ColorUtil.GetComplementary(baseColor) }; + } + + /// + /// 生成三色配色方案 + /// + public static RGB[] GetTriadic(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 120) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 240) % 360, hsl.S, hsl.L) + }; + } + + /// + /// 生成四色配色方案 + /// + public static RGB[] GetTetradic(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 90) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 180) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 270) % 360, hsl.S, hsl.L) + }; + } + + /// + /// 生成单色配色方案 + /// + public static RGB[] GetMonochromatic(RGB baseColor, int count = 5) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + var colors = new RGB[count]; + + for (int i = 0; i < count; i++) + { + double l = 20 + i * (60.0 / (count - 1)); + colors[i] = ColorUtil.HSLToRGB(hsl.H, hsl.S, l); + } + + return colors; + } + + /// + /// 生成分裂互补色配色方案 + /// + public static RGB[] GetSplitComplementary(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 150) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 210) % 360, hsl.S, hsl.L) + }; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs b/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs new file mode 100644 index 0000000..29a5f5d --- /dev/null +++ b/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs @@ -0,0 +1,274 @@ +using System; + +namespace EasyTool.ConvertCategory +{ + /// + /// 坐标转换工具类 + /// 提供常见坐标系统之间的转换(WGS84、GCJ02、BD09) + /// + public static class CoordinateConvertUtil + { + // 坐标系常量 + private const double Pi = 3.1415926535897932384626; + private const double A = 6378245.0; // 长半轴 + private const double EE = 0.00669342162296594323; // 扁率 + + /// + /// 坐标系类型 + /// + public enum CoordinateSystem + { + /// WGS84(GPS原始坐标) + WGS84, + /// GCJ02(国测局坐标/火星坐标) + GCJ02, + /// BD09(百度坐标) + BD09 + } + + /// + /// 经纬度坐标 + /// + public struct GeoPoint + { + /// 经度 + public double Longitude { get; set; } + /// 纬度 + public double Latitude { get; set; } + /// 坐标系 + public CoordinateSystem CoordinateSystem { get; set; } + + public GeoPoint(double longitude, double latitude, CoordinateSystem coordinateSystem = CoordinateSystem.WGS84) + { + Longitude = longitude; + Latitude = latitude; + CoordinateSystem = coordinateSystem; + } + + public override string ToString() => $"({Longitude:F6}, {Latitude:F6})"; + } + + /// + /// WGS84 转 GCJ02 + /// + public static GeoPoint WGS84ToGCJ02(double longitude, double latitude) + { + if (OutOfChina(longitude, latitude)) + { + return new GeoPoint(longitude, latitude, CoordinateSystem.GCJ02); + } + + double dLat = TransformLat(longitude - 105.0, latitude - 35.0); + double dLon = TransformLon(longitude - 105.0, latitude - 35.0); + + double radLat = latitude / 180.0 * Pi; + double magic = Math.Sin(radLat); + magic = 1 - EE * magic * magic; + double sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * Pi); + dLon = (dLon * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * Pi); + + double mgLat = latitude + dLat; + double mgLon = longitude + dLon; + + return new GeoPoint(mgLon, mgLat, CoordinateSystem.GCJ02); + } + + /// + /// GCJ02 转 WGS84 + /// + public static GeoPoint GCJ02ToWGS84(double longitude, double latitude) + { + if (OutOfChina(longitude, latitude)) + { + return new GeoPoint(longitude, latitude, CoordinateSystem.WGS84); + } + + double dLat = TransformLat(longitude - 105.0, latitude - 35.0); + double dLon = TransformLon(longitude - 105.0, latitude - 35.0); + + double radLat = latitude / 180.0 * Pi; + double magic = Math.Sin(radLat); + magic = 1 - EE * magic * magic; + double sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * Pi); + dLon = (dLon * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * Pi); + + double mgLat = latitude + dLat; + double mgLon = longitude + dLon; + + return new GeoPoint(longitude * 2 - mgLon, latitude * 2 - mgLat, CoordinateSystem.WGS84); + } + + /// + /// GCJ02 转 BD09 + /// + public static GeoPoint GCJ02ToBD09(double longitude, double latitude) + { + double x = longitude; + double y = latitude; + + double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * Pi * 3000.0 / 180.0); + double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * Pi * 3000.0 / 180.0); + + double bdLon = z * Math.Cos(theta) + 0.0065; + double bdLat = z * Math.Sin(theta) + 0.006; + + return new GeoPoint(bdLon, bdLat, CoordinateSystem.BD09); + } + + /// + /// BD09 转 GCJ02 + /// + public static GeoPoint BD09ToGCJ02(double longitude, double latitude) + { + double x = longitude - 0.0065; + double y = latitude - 0.006; + + double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Pi * 3000.0 / 180.0); + double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Pi * 3000.0 / 180.0); + + double gcjLon = z * Math.Cos(theta); + double gcjLat = z * Math.Sin(theta); + + return new GeoPoint(gcjLon, gcjLat, CoordinateSystem.GCJ02); + } + + /// + /// BD09 转 WGS84 + /// + public static GeoPoint BD09ToWGS84(double longitude, double latitude) + { + var gcj02 = BD09ToGCJ02(longitude, latitude); + return GCJ02ToWGS84(gcj02.Longitude, gcj02.Latitude); + } + + /// + /// WGS84 转 BD09 + /// + public static GeoPoint WGS84ToBD09(double longitude, double latitude) + { + var gcj02 = WGS84ToGCJ02(longitude, latitude); + return GCJ02ToBD09(gcj02.Longitude, gcj02.Latitude); + } + + /// + /// 通用坐标转换 + /// + public static GeoPoint Convert(double longitude, double latitude, CoordinateSystem from, CoordinateSystem to) + { + if (from == to) + return new GeoPoint(longitude, latitude, to); + + return (from, to) switch + { + (CoordinateSystem.WGS84, CoordinateSystem.GCJ02) => WGS84ToGCJ02(longitude, latitude), + (CoordinateSystem.WGS84, CoordinateSystem.BD09) => WGS84ToBD09(longitude, latitude), + (CoordinateSystem.GCJ02, CoordinateSystem.WGS84) => GCJ02ToWGS84(longitude, latitude), + (CoordinateSystem.GCJ02, CoordinateSystem.BD09) => GCJ02ToBD09(longitude, latitude), + (CoordinateSystem.BD09, CoordinateSystem.WGS84) => BD09ToWGS84(longitude, latitude), + (CoordinateSystem.BD09, CoordinateSystem.GCJ02) => BD09ToGCJ02(longitude, latitude), + _ => new GeoPoint(longitude, latitude, to) + }; + } + + /// + /// 计算两点之间的距离(米) + /// 使用 Haversine 公式 + /// + public static double Distance(double lon1, double lat1, double lon2, double lat2) + { + const double R = 6371000; // 地球半径(米) + + double dLat = (lat2 - lat1) * Pi / 180; + double dLon = (lon2 - lon1) * Pi / 180; + + double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(lat1 * Pi / 180) * Math.Cos(lat2 * Pi / 180) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return R * c; + } + + /// + /// 计算两点之间的距离 + /// + public static double Distance(GeoPoint p1, GeoPoint p2) + { + return Distance(p1.Longitude, p1.Latitude, p2.Longitude, p2.Latitude); + } + + /// + /// 计算方位角(从北向顺时针) + /// + public static double Bearing(double lon1, double lat1, double lon2, double lat2) + { + double dLon = (lon2 - lon1) * Pi / 180; + + double y = Math.Sin(dLon) * Math.Cos(lat2 * Pi / 180); + double x = Math.Cos(lat1 * Pi / 180) * Math.Sin(lat2 * Pi / 180) - + Math.Sin(lat1 * Pi / 180) * Math.Cos(lat2 * Pi / 180) * Math.Cos(dLon); + + double bearing = Math.Atan2(y, x) * 180 / Pi; + return (bearing + 360) % 360; + } + + /// + /// 根据起点、方位角和距离计算终点 + /// + public static GeoPoint Destination(double lon1, double lat1, double bearing, double distance) + { + const double R = 6371000; + + double brng = bearing * Pi / 180; + double d = distance / R; + + double lat1Rad = lat1 * Pi / 180; + double lon1Rad = lon1 * Pi / 180; + + double lat2Rad = Math.Asin(Math.Sin(lat1Rad) * Math.Cos(d) + + Math.Cos(lat1Rad) * Math.Sin(d) * Math.Cos(brng)); + + double lon2Rad = lon1Rad + Math.Atan2( + Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat1Rad), + Math.Cos(d) - Math.Sin(lat1Rad) * Math.Sin(lat2Rad)); + + return new GeoPoint(lon2Rad * 180 / Pi, lat2Rad * 180 / Pi); + } + + /// + /// 判断是否在中国境内 + /// + public static bool OutOfChina(double longitude, double latitude) + { + if (longitude < 72.004 || longitude > 137.8347) + return true; + if (latitude < 0.8293 || latitude > 55.8271) + return true; + + return false; + } + + private static double TransformLat(double x, double y) + { + double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Pi) + 20.0 * Math.Sin(2.0 * x * Pi)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(y * Pi) + 40.0 * Math.Sin(y / 3.0 * Pi)) * 2.0 / 3.0; + ret += (160.0 * Math.Sin(y / 12.0 * Pi) + 320 * Math.Sin(y * Pi / 30.0)) * 2.0 / 3.0; + return ret; + } + + private static double TransformLon(double x, double y) + { + double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Pi) + 20.0 * Math.Sin(2.0 * x * Pi)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(x * Pi) + 40.0 * Math.Sin(x / 3.0 * Pi)) * 2.0 / 3.0; + ret += (150.0 * Math.Sin(x / 12.0 * Pi) + 300.0 * Math.Sin(x / 30.0 * Pi)) * 2.0 / 3.0; + return ret; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs b/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs new file mode 100644 index 0000000..eea4dc6 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs @@ -0,0 +1,436 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ConvertCategory +{ + /// + /// 单位转换工具类 + /// 提供长度、重量、温度、面积、体积等常用单位转换 + /// + public static class UnitConvertUtil + { + #region 长度 + + /// + /// 长度单位 + /// + public enum LengthUnit + { + Millimeter, Centimeter, Meter, Kilometer, + Inch, Foot, Yard, Mile, + Nanometer, Micrometer, Decimeter + } + + private static readonly Dictionary LengthToMeter = new() + { + { LengthUnit.Nanometer, 1e-9 }, + { LengthUnit.Micrometer, 1e-6 }, + { LengthUnit.Millimeter, 0.001 }, + { LengthUnit.Centimeter, 0.01 }, + { LengthUnit.Decimeter, 0.1 }, + { LengthUnit.Meter, 1 }, + { LengthUnit.Kilometer, 1000 }, + { LengthUnit.Inch, 0.0254 }, + { LengthUnit.Foot, 0.3048 }, + { LengthUnit.Yard, 0.9144 }, + { LengthUnit.Mile, 1609.344 } + }; + + /// + /// 长度转换 + /// + public static double ConvertLength(double value, LengthUnit from, LengthUnit to) + { + double meters = value * LengthToMeter[from]; + return meters / LengthToMeter[to]; + } + + /// + /// 获取所有可转换的长度单位 + /// + public static LengthUnit[] GetLengthUnits() => (LengthUnit[])Enum.GetValues(typeof(LengthUnit)); + + #endregion + + #region 重量 + + /// + /// 重量单位 + /// + public enum WeightUnit + { + Milligram, Gram, Kilogram, Ton, + Ounce, Pound, Jin, Liang + } + + private static readonly Dictionary WeightToGram = new() + { + { WeightUnit.Milligram, 0.001 }, + { WeightUnit.Gram, 1 }, + { WeightUnit.Kilogram, 1000 }, + { WeightUnit.Ton, 1000000 }, + { WeightUnit.Ounce, 28.349523125 }, + { WeightUnit.Pound, 453.59237 }, + { WeightUnit.Jin, 500 }, // 市斤 + { WeightUnit.Liang, 50 } // 市两 + }; + + /// + /// 重量转换 + /// + public static double ConvertWeight(double value, WeightUnit from, WeightUnit to) + { + double grams = value * WeightToGram[from]; + return grams / WeightToGram[to]; + } + + #endregion + + #region 温度 + + /// + /// 温度单位 + /// + public enum TemperatureUnit + { + Celsius, Fahrenheit, Kelvin + } + + /// + /// 温度转换 + /// + public static double ConvertTemperature(double value, TemperatureUnit from, TemperatureUnit to) + { + // 先转换为摄氏度 + double celsius = from switch + { + TemperatureUnit.Celsius => value, + TemperatureUnit.Fahrenheit => (value - 32) * 5 / 9, + TemperatureUnit.Kelvin => value - 273.15, + _ => throw new ArgumentException("Invalid temperature unit") + }; + + // 再从摄氏度转换为目标单位 + return to switch + { + TemperatureUnit.Celsius => celsius, + TemperatureUnit.Fahrenheit => celsius * 9 / 5 + 32, + TemperatureUnit.Kelvin => celsius + 273.15, + _ => throw new ArgumentException("Invalid temperature unit") + }; + } + + #endregion + + #region 面积 + + /// + /// 面积单位 + /// + public enum AreaUnit + { + SquareMillimeter, SquareCentimeter, SquareMeter, SquareKilometer, + SquareInch, SquareFoot, SquareYard, SquareMile, + Hectare, Acre, Mu + } + + private static readonly Dictionary AreaToSquareMeter = new() + { + { AreaUnit.SquareMillimeter, 0.000001 }, + { AreaUnit.SquareCentimeter, 0.0001 }, + { AreaUnit.SquareMeter, 1 }, + { AreaUnit.SquareKilometer, 1000000 }, + { AreaUnit.SquareInch, 0.00064516 }, + { AreaUnit.SquareFoot, 0.09290304 }, + { AreaUnit.SquareYard, 0.83612736 }, + { AreaUnit.SquareMile, 2589988.110336 }, + { AreaUnit.Hectare, 10000 }, + { AreaUnit.Acre, 4046.8564224 }, + { AreaUnit.Mu, 666.66666666667 } // 市亩 + }; + + /// + /// 面积转换 + /// + public static double ConvertArea(double value, AreaUnit from, AreaUnit to) + { + double sqMeters = value * AreaToSquareMeter[from]; + return sqMeters / AreaToSquareMeter[to]; + } + + #endregion + + #region 体积 + + /// + /// 体积单位 + /// + public enum VolumeUnit + { + CubicMillimeter, CubicCentimeter, CubicMeter, CubicKilometer, + Milliliter, Liter, + CubicInch, CubicFoot, CubicYard, + GallonUS, GallonUK, PintUS, PintUK, + FluidOunceUS, FluidOunceUK + } + + private static readonly Dictionary VolumeToLiter = new() + { + { VolumeUnit.CubicMillimeter, 0.000001 }, + { VolumeUnit.CubicCentimeter, 0.001 }, + { VolumeUnit.CubicMeter, 1000 }, + { VolumeUnit.CubicKilometer, 1e12 }, + { VolumeUnit.Milliliter, 0.001 }, + { VolumeUnit.Liter, 1 }, + { VolumeUnit.CubicInch, 0.016387064 }, + { VolumeUnit.CubicFoot, 28.316846592 }, + { VolumeUnit.CubicYard, 764.554857984 }, + { VolumeUnit.GallonUS, 3.785411784 }, + { VolumeUnit.GallonUK, 4.54609 }, + { VolumeUnit.PintUS, 0.473176473 }, + { VolumeUnit.PintUK, 0.56826125 }, + { VolumeUnit.FluidOunceUS, 0.0295735295625 }, + { VolumeUnit.FluidOunceUK, 0.0284130625 } + }; + + /// + /// 体积转换 + /// + public static double ConvertVolume(double value, VolumeUnit from, VolumeUnit to) + { + double liters = value * VolumeToLiter[from]; + return liters / VolumeToLiter[to]; + } + + #endregion + + #region 速度 + + /// + /// 速度单位 + /// + public enum SpeedUnit + { + MeterPerSecond, KilometerPerHour, MilePerHour, + Knot, FootPerSecond + } + + private static readonly Dictionary SpeedToMps = new() + { + { SpeedUnit.MeterPerSecond, 1 }, + { SpeedUnit.KilometerPerHour, 1000.0 / 3600 }, + { SpeedUnit.MilePerHour, 0.44704 }, + { SpeedUnit.Knot, 0.514444444 }, + { SpeedUnit.FootPerSecond, 0.3048 } + }; + + /// + /// 速度转换 + /// + public static double ConvertSpeed(double value, SpeedUnit from, SpeedUnit to) + { + double mps = value * SpeedToMps[from]; + return mps / SpeedToMps[to]; + } + + #endregion + + #region 时间 + + /// + /// 时间单位 + /// + public enum TimeUnit + { + Millisecond, Second, Minute, Hour, Day, Week, + Month, Year, Decade, Century + } + + private static readonly Dictionary TimeToSecond = new() + { + { TimeUnit.Millisecond, 0.001 }, + { TimeUnit.Second, 1 }, + { TimeUnit.Minute, 60 }, + { TimeUnit.Hour, 3600 }, + { TimeUnit.Day, 86400 }, + { TimeUnit.Week, 604800 }, + { TimeUnit.Month, 2629746 }, // 平均月份 + { TimeUnit.Year, 31556952 }, // 平均年 + { TimeUnit.Decade, 315569520 }, + { TimeUnit.Century, 3155695200 } + }; + + /// + /// 时间转换 + /// + public static double ConvertTime(double value, TimeUnit from, TimeUnit to) + { + double seconds = value * TimeToSecond[from]; + return seconds / TimeToSecond[to]; + } + + #endregion + + #region 压力 + + /// + /// 压力单位 + /// + public enum PressureUnit + { + Pascal, Kilopascal, Megapascal, Bar, + Psi, Atm, Torr, MmHg + } + + private static readonly Dictionary PressureToPascal = new() + { + { PressureUnit.Pascal, 1 }, + { PressureUnit.Kilopascal, 1000 }, + { PressureUnit.Megapascal, 1000000 }, + { PressureUnit.Bar, 100000 }, + { PressureUnit.Psi, 6894.757293168 }, + { PressureUnit.Atm, 101325 }, + { PressureUnit.Torr, 133.3223684211 }, + { PressureUnit.MmHg, 133.322 } + }; + + /// + /// 压力转换 + /// + public static double ConvertPressure(double value, PressureUnit from, PressureUnit to) + { + double pascals = value * PressureToPascal[from]; + return pascals / PressureToPascal[to]; + } + + #endregion + + #region 角度 + + /// + /// 角度单位 + /// + public enum AngleUnit + { + Degree, Radian, Gradian, Turn + } + + /// + /// 角度转换 + /// + public static double ConvertAngle(double value, AngleUnit from, AngleUnit to) + { + // 先转换为度 + double degrees = from switch + { + AngleUnit.Degree => value, + AngleUnit.Radian => value * 180 / Math.PI, + AngleUnit.Gradian => value * 0.9, + AngleUnit.Turn => value * 360, + _ => throw new ArgumentException("Invalid angle unit") + }; + + // 再从度转换为目标单位 + return to switch + { + AngleUnit.Degree => degrees, + AngleUnit.Radian => degrees * Math.PI / 180, + AngleUnit.Gradian => degrees / 0.9, + AngleUnit.Turn => degrees / 360, + _ => throw new ArgumentException("Invalid angle unit") + }; + } + + #endregion + + #region 数据大小 + + /// + /// 数据大小单位 + /// + public enum DataUnit + { + Bit, Byte, + Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, + Kibibyte, Mebibyte, Gibibyte, Tebibyte, Pebibyte + } + + private static readonly Dictionary DataToByte = new() + { + { DataUnit.Bit, 0.125 }, + { DataUnit.Byte, 1 }, + { DataUnit.Kilobyte, 1000 }, + { DataUnit.Megabyte, 1000000 }, + { DataUnit.Gigabyte, 1e9 }, + { DataUnit.Terabyte, 1e12 }, + { DataUnit.Petabyte, 1e15 }, + { DataUnit.Kibibyte, 1024 }, + { DataUnit.Mebibyte, 1048576 }, + { DataUnit.Gibibyte, 1073741824 }, + { DataUnit.Tebibyte, 1099511627776 }, + { DataUnit.Pebibyte, 1125899906842624 } + }; + + /// + /// 数据大小转换 + /// + public static double ConvertData(double value, DataUnit from, DataUnit to) + { + double bytes = value * DataToByte[from]; + return bytes / DataToByte[to]; + } + + /// + /// 自动格式化数据大小 + /// + public static string FormatDataSize(double bytes) + { + string[] units = { "B", "KB", "MB", "GB", "TB", "PB" }; + int unitIndex = 0; + + while (bytes >= 1024 && unitIndex < units.Length - 1) + { + bytes /= 1024; + unitIndex++; + } + + return $"{bytes:F2} {units[unitIndex]}"; + } + + #endregion + + #region 能量 + + /// + /// 能量单位 + /// + public enum EnergyUnit + { + Joule, Kilojoule, Megajoule, Calorie, Kilocalorie, + WattHour, KilowattHour, BritishThermalUnit + } + + private static readonly Dictionary EnergyToJoule = new() + { + { EnergyUnit.Joule, 1 }, + { EnergyUnit.Kilojoule, 1000 }, + { EnergyUnit.Megajoule, 1000000 }, + { EnergyUnit.Calorie, 4.184 }, + { EnergyUnit.Kilocalorie, 4184 }, + { EnergyUnit.WattHour, 3600 }, + { EnergyUnit.KilowattHour, 3600000 }, + { EnergyUnit.BritishThermalUnit, 1055.06 } + }; + + /// + /// 能量转换 + /// + public static double ConvertEnergy(double value, EnergyUnit from, EnergyUnit to) + { + double joules = value * EnergyToJoule[from]; + return joules / EnergyToJoule[to]; + } + + #endregion + } +} diff --git a/EasyTool.Core/DateTimeCategory/CronUtil.cs b/EasyTool.Core/DateTimeCategory/CronUtil.cs new file mode 100644 index 0000000..0123aac --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/CronUtil.cs @@ -0,0 +1,338 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.DateTimeCategory +{ + /// + /// Cron 表达式工具类 + /// 支持 Cron 表达式解析、验证和下一次执行时间计算 + /// + public static class CronUtil + { + /// + /// 解析 Cron 表达式 + /// + public static CronExpression Parse(string cronExpression) + { + return new CronExpression(cronExpression); + } + + /// + /// 验证 Cron 表达式是否有效 + /// + public static bool IsValid(string cronExpression) + { + try + { + new CronExpression(cronExpression); + return true; + } + catch + { + return false; + } + } + + /// + /// 获取下一次执行时间 + /// + public static DateTime? GetNextExecution(string cronExpression, DateTime from) + { + return Parse(cronExpression).GetNextExecution(from); + } + + /// + /// 获取接下来的N次执行时间 + /// + public static IEnumerable GetNextExecutions(string cronExpression, DateTime from, int count) + { + return Parse(cronExpression).GetNextExecutions(from, count); + } + } + + /// + /// Cron 表达式 + /// + public class CronExpression + { + private static readonly int[] MonthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + private readonly HashSet _seconds; + private readonly HashSet _minutes; + private readonly HashSet _hours; + private readonly HashSet _daysOfMonth; + private readonly HashSet _months; + private readonly HashSet _daysOfWeek; + + /// + /// 原始表达式 + /// + public string Expression { get; } + + /// + /// 创建 Cron 表达式 + /// + public CronExpression(string expression) + { + if (string.IsNullOrWhiteSpace(expression)) + throw new ArgumentException("Cron expression cannot be empty"); + + Expression = expression; + + var parts = expression.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length != 5 && parts.Length != 6) + throw new ArgumentException("Cron expression must have 5 or 6 fields"); + + int offset = parts.Length == 6 ? 0 : 1; + + _seconds = parts.Length == 6 ? ParseField(parts[0], 0, 59) : new HashSet { 0 }; + _minutes = ParseField(parts[offset], 0, 59); + _hours = ParseField(parts[offset + 1], 0, 23); + _daysOfMonth = ParseField(parts[offset + 2], 1, 31); + _months = ParseField(parts[offset + 3], 1, 12); + _daysOfWeek = ParseField(parts[offset + 4], 0, 6); + } + + /// + /// 获取下一次执行时间 + /// + public DateTime? GetNextExecution(DateTime from) + { + return GetNextExecutions(from, 1).FirstOrDefault(); + } + + /// + /// 获取接下来的N次执行时间 + /// + public IEnumerable GetNextExecutions(DateTime from, int count) + { + var current = from.AddSeconds(1); + int found = 0; + int maxIterations = 366 * 24 * 60 * 60; // 最多查找一年 + + while (found < count && maxIterations-- > 0) + { + if (Matches(current)) + { + yield return current; + found++; + current = current.AddSeconds(1); + } + else + { + current = SkipToNextCandidate(current); + } + } + } + + /// + /// 判断指定时间是否匹配 + /// + public bool Matches(DateTime time) + { + if (!_seconds.Contains(time.Second)) return false; + if (!_minutes.Contains(time.Minute)) return false; + if (!_hours.Contains(time.Hour)) return false; + if (!_months.Contains(time.Month)) return false; + + // 日和周是"或"关系 + bool dayMatch = _daysOfMonth.Contains(time.Day); + bool weekMatch = _daysOfWeek.Contains((int)time.DayOfWeek); + + // 如果两者都设置了,只要有一个匹配即可 + // 如果其中一个设置为*,则另一个起作用 + if (_daysOfMonth.Contains(-1) && _daysOfWeek.Contains(-1)) + return true; + if (_daysOfMonth.Contains(-1)) + return weekMatch; + if (_daysOfWeek.Contains(-1)) + return dayMatch; + + return dayMatch || weekMatch; + } + + private DateTime SkipToNextCandidate(DateTime current) + { + // 优化:跳过不可能匹配的时间 + if (!_months.Contains(current.Month)) + { + // 跳到下个月 + return new DateTime(current.Year, current.Month, 1).AddMonths(1); + } + + if (!_hours.Contains(current.Hour)) + { + // 跳到下一个小时 + return current.AddHours(1).AddMinutes(-current.Minute).AddSeconds(-current.Second); + } + + if (!_minutes.Contains(current.Minute)) + { + // 跳到下一分钟 + return current.AddMinutes(1).AddSeconds(-current.Second); + } + + // 逐秒查找 + return current.AddSeconds(1); + } + + private static HashSet ParseField(string field, int min, int max) + { + var result = new HashSet(); + int wildcard = -1; + + // 处理 L (Last) + if (field == "L") + { + result.Add(max); + return result; + } + + // 处理 * 或 ? + if (field == "*" || field == "?") + { + result.Add(wildcard); + return result; + } + + // 分割逗号分隔的部分 + foreach (var part in field.Split(',')) + { + string currentPart = part.Trim(); + + // 处理步长 + int step = 1; + if (currentPart.Contains('/')) + { + var stepParts = currentPart.Split('/'); + currentPart = stepParts[0]; + step = int.Parse(stepParts[1]); + } + + // 处理范围 + int start, end; + if (currentPart == "*") + { + start = min; + end = max; + } + else if (currentPart.Contains('-')) + { + var rangeParts = currentPart.Split('-'); + start = int.Parse(rangeParts[0]); + end = int.Parse(rangeParts[1]); + } + else + { + start = end = int.Parse(currentPart); + } + + for (int i = start; i <= end; i += step) + { + if (i >= min && i <= max) + result.Add(i); + } + } + + return result; + } + + /// + /// 获取可读的描述 + /// + public string GetDescription() + { + var parts = Expression.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + int offset = parts.Length == 6 ? 0 : 1; + + var desc = new System.Text.StringBuilder(); + desc.Append("在"); + + if (_hours.Contains(-1) && _minutes.Contains(-1)) + desc.Append("每分钟"); + else if (_hours.Contains(-1)) + desc.Append($"每小时的第{_stringify(_minutes)}分钟"); + else + desc.Append($"{_stringify(_hours)}时{_stringify(_minutes)}分"); + + if (!_daysOfMonth.Contains(-1) || !_daysOfWeek.Contains(-1)) + { + desc.Append(","); + if (!_daysOfMonth.Contains(-1)) + desc.Append($"每月{_stringify(_daysOfMonth)}日"); + if (!_daysOfWeek.Contains(-1)) + { + if (!_daysOfMonth.Contains(-1)) desc.Append("或"); + desc.Append($"每周{_stringify(_daysOfWeek)}"); + } + } + + if (!_months.Contains(-1)) + desc.Append($",{_stringify(_months)}月"); + + desc.Append("执行"); + + return desc.ToString(); + } + + private static string _stringify(HashSet set) + { + if (set.Contains(-1)) return "每"; + var sorted = set.OrderBy(x => x).ToList(); + if (sorted.Count == 1) return sorted[0].ToString(); + return string.Join(",", sorted); + } + + public override string ToString() => Expression; + } + + /// + /// 常用 Cron 表达式 + /// + public static class CronExpressions + { + /// 每分钟 + public static string EveryMinute => "* * * * *"; + + /// 每小时 + public static string EveryHour => "0 * * * *"; + + /// 每天午夜 + public static string Daily => "0 0 * * *"; + + /// 每天中午 + public static string DailyNoon => "0 12 * * *"; + + /// 每周一 + public static string WeeklyMonday => "0 0 * * 1"; + + /// 每月1号 + public static string Monthly => "0 0 1 * *"; + + /// 每年1月1日 + public static string Yearly => "0 0 1 1 *"; + + /// 工作日 + public static string Weekdays => "0 0 * * 1-5"; + + /// 周末 + public static string Weekends => "0 0 * * 0,6"; + + /// + /// 每5分钟 + /// + public static string EveryNMinutes(int n) => $"*/{n} * * * *"; + + /// + /// 每N小时 + /// + public static string EveryNHours(int n) => $"0 */{n} * * *"; + + /// + /// 每天指定时间 + /// + public static string DailyAt(int hour, int minute = 0) => $"{minute} {hour} * * *"; + } +} diff --git a/EasyTool.Core/DateTimeCategory/HolidayUtil.cs b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs new file mode 100644 index 0000000..a9aaebd --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs @@ -0,0 +1,504 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 节假日工具类 + /// 支持中国法定节假日和常见国际节日 + /// + public static class HolidayUtil + { + /// + /// 判断是否为中国法定节假日 + /// + public static bool IsChineseHoliday(DateTime date) + { + return GetChineseHoliday(date) != null; + } + + /// + /// 获取中国节假日名称 + /// + public static string GetChineseHoliday(DateTime date) + { + int year = date.Year; + int month = date.Month; + int day = date.Day; + + // 固定日期节日 + if (month == 1 && day == 1) return "元旦"; + if (month == 5 && day == 1) return "劳动节"; + if (month == 10 && day == 1) return "国庆节"; + + // 农历节日(简化计算,使用近似日期) + var lunar = LunarCalendarUtil.SolarToLunar(date); + if (lunar != null) + { + if (lunar.Month == 1 && lunar.Day == 1) return "春节"; + if (lunar.Month == 1 && lunar.Day == 15) return "元宵节"; + if (lunar.Month == 5 && lunar.Day == 5) return "端午节"; + if (lunar.Month == 8 && lunar.Day == 15) return "中秋节"; + if (lunar.Month == 9 && lunar.Day == 9) return "重阳节"; + if (lunar.Month == 12 && lunar.Day == 30) return "除夕"; + } + + // 母亲节:5月第二个星期日 + if (month == 5) + { + var motherDay = GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2); + if (day == motherDay) return "母亲节"; + } + + // 父亲节:6月第三个星期日 + if (month == 6) + { + var fatherDay = GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3); + if (day == fatherDay) return "父亲节"; + } + + // 教师节:9月10日 + if (month == 9 && day == 10) return "教师节"; + + // 清明节:4月4日或5日(简化) + var qingming = GetQingmingDate(year); + if (month == 4 && day == qingming) return "清明节"; + + // 儿童节:6月1日 + if (month == 6 && day == 1) return "儿童节"; + + // 妇女节:3月8日 + if (month == 3 && day == 8) return "妇女节"; + + // 植树节:3月12日 + if (month == 3 && day == 12) return "植树节"; + + // 青年节:5月4日 + if (month == 5 && day == 4) return "青年节"; + + // 建党节:7月1日 + if (month == 7 && day == 1) return "建党节"; + + // 建军节:8月1日 + if (month == 8 && day == 1) return "建军节"; + + return null; + } + + /// + /// 判断是否为国际常见节日 + /// + public static bool IsInternationalHoliday(DateTime date) + { + return GetInternationalHoliday(date) != null; + } + + /// + /// 获取国际节日名称 + /// + public static string GetInternationalHoliday(DateTime date) + { + int month = date.Month; + int day = date.Day; + int year = date.Year; + + // 固定日期 + if (month == 1 && day == 1) return "New Year's Day"; + if (month == 2 && day == 14) return "Valentine's Day"; + if (month == 3 && day == 8) return "International Women's Day"; + if (month == 3 && day == 12) return "Arbor Day"; + if (month == 3 && day == 21) return "World Sleep Day"; + if (month == 4 && day == 1) return "April Fools' Day"; + if (month == 4 && day == 22) return "Earth Day"; + if (month == 4 && day == 23) return "World Book Day"; + if (month == 5 && day == 1) return "International Workers' Day"; + if (month == 5 && day == 4) return "Star Wars Day"; + if (month == 6 && day == 1) return "International Children's Day"; + if (month == 6 && day == 5) return "World Environment Day"; + if (month == 9 && day == 21) return "International Day of Peace"; + if (month == 10 && day == 31) return "Halloween"; + if (month == 11 && day == 11) return "Veterans Day / Singles' Day"; + if (month == 12 && day == 24) return "Christmas Eve"; + if (month == 12 && day == 25) return "Christmas Day"; + if (month == 12 && day == 31) return "New Year's Eve"; + + // 复活节(春分后第一个满月后的第一个星期日) + var easter = CalculateEaster(year); + if (date == easter) return "Easter Sunday"; + if (date == easter.AddDays(-2)) return "Good Friday"; + if (date == easter.AddDays(1)) return "Easter Monday"; + + // 感恩节(11月第四个星期四) + var thanksgiving = GetNthDayOfWeek(year, 11, DayOfWeek.Thursday, 4); + if (date.Day == thanksgiving && month == 11) return "Thanksgiving"; + + // 黑色星期五(感恩节后一天) + if (month == 11 && date.Day == thanksgiving + 1) return "Black Friday"; + + return null; + } + + /// + /// 获取指定年份的所有中国节假日 + /// + public static Dictionary GetChineseHolidays(int year) + { + var holidays = new Dictionary(); + + // 固定日期节日 + holidays[new DateTime(year, 1, 1)] = "元旦"; + holidays[new DateTime(year, 5, 1)] = "劳动节"; + holidays[new DateTime(year, 10, 1)] = "国庆节"; + + // 清明节 + int qingming = GetQingmingDate(year); + holidays[new DateTime(year, 4, qingming)] = "清明节"; + + // 农历节日(需要转换) + // 春节(农历正月初一) + var springFestival = LunarToSolar(year, 1, 1); + if (springFestival.HasValue) + { + holidays[springFestival.Value] = "春节"; + holidays[springFestival.Value.AddDays(-1)] = "除夕"; + } + + // 元宵节 + var lanternFestival = LunarToSolar(year, 1, 15); + if (lanternFestival.HasValue) + holidays[lanternFestival.Value] = "元宵节"; + + // 端午节 + var dragonBoat = LunarToSolar(year, 5, 5); + if (dragonBoat.HasValue) + holidays[dragonBoat.Value] = "端午节"; + + // 中秋节 + var midAutumn = LunarToSolar(year, 8, 15); + if (midAutumn.HasValue) + holidays[midAutumn.Value] = "中秋节"; + + // 其他节日 + holidays[new DateTime(year, 3, 8)] = "妇女节"; + holidays[new DateTime(year, 3, 12)] = "植树节"; + holidays[new DateTime(year, 5, 4)] = "青年节"; + holidays[new DateTime(year, 6, 1)] = "儿童节"; + holidays[new DateTime(year, 7, 1)] = "建党节"; + holidays[new DateTime(year, 8, 1)] = "建军节"; + holidays[new DateTime(year, 9, 10)] = "教师节"; + + // 母亲节、父亲节 + var motherDay = GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2); + holidays[new DateTime(year, 5, motherDay)] = "母亲节"; + + var fatherDay = GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3); + holidays[new DateTime(year, 6, fatherDay)] = "父亲节"; + + return holidays; + } + + /// + /// 获取清明节的日期(4月4日或5日) + /// + private static int GetQingmingDate(int year) + { + // 清明节大约在公历4月4日或5日 + // 使用简化算法 + int y = year % 100; + int d = (y * 0.2422 + 4.81) % 1 > 0.5 ? 4 : 5; + return d; + } + + /// + /// 获取某月第N个某星期几的日期 + /// + private static int GetNthDayOfWeek(int year, int month, DayOfWeek dayOfWeek, int n) + { + var firstDay = new DateTime(year, month, 1); + int offset = ((int)dayOfWeek - (int)firstDay.DayOfWeek + 7) % 7; + return 1 + offset + (n - 1) * 7; + } + + /// + /// 计算复活节日期 + /// + private static DateTime CalculateEaster(int year) + { + int a = year % 19; + int b = year / 100; + int c = year % 100; + int d = b / 4; + int e = b % 4; + int f = (b + 8) / 25; + int g = (b - f + 1) / 3; + int h = (19 * a + b - d - g + 15) % 30; + int i = c / 4; + int k = c % 4; + int l = (32 + 2 * e + 2 * i - h - k) % 7; + int m = (a + 11 * h + 22 * l) / 451; + int month = (h + l - 7 * m + 114) / 31; + int day = ((h + l - 7 * m + 114) % 31) + 1; + + return new DateTime(year, month, day); + } + + /// + /// 农历转公历(简化版) + /// + private static DateTime? LunarToSolar(int year, int lunarMonth, int lunarDay) + { + // 这里需要使用 LunarCalendarUtil,如果不存在则返回 null + try + { + return LunarCalendarUtil.LunarToSolar(year, lunarMonth, lunarDay); + } + catch + { + return null; + } + } + } + + /// + /// 工作日工具类 + /// + public static class WorkdayUtil + { + private static readonly HashSet _holidays = new(); + private static readonly HashSet _workdays = new(); // 调休工作日 + + /// + /// 设置节假日 + /// + public static void SetHoliday(DateTime date) + { + _holidays.Add(date.Date); + _workdays.Remove(date.Date); + } + + /// + /// 设置调休工作日 + /// + public static void SetWorkday(DateTime date) + { + _workdays.Add(date.Date); + _holidays.Remove(date.Date); + } + + /// + /// 判断是否为工作日 + /// + public static bool IsWorkday(DateTime date) + { + date = date.Date; + + // 优先检查调休工作日 + if (_workdays.Contains(date)) return true; + + // 检查节假日 + if (_holidays.Contains(date)) return false; + if (HolidayUtil.IsChineseHoliday(date)) return false; + + // 默认周一到周五为工作日 + return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday; + } + + /// + /// 获取下一个工作日 + /// + public static DateTime GetNextWorkday(DateTime date) + { + var next = date.Date.AddDays(1); + while (!IsWorkday(next)) + { + next = next.AddDays(1); + } + return next; + } + + /// + /// 获取上一个工作日 + /// + public static DateTime GetPreviousWorkday(DateTime date) + { + var prev = date.Date.AddDays(-1); + while (!IsWorkday(prev)) + { + prev = prev.AddDays(-1); + } + return prev; + } + + /// + /// 计算两个日期之间的工作日数量 + /// + public static int CountWorkdays(DateTime start, DateTime end) + { + if (start > end) + (start, end) = (end, start); + + int count = 0; + var current = start.Date; + while (current <= end.Date) + { + if (IsWorkday(current)) count++; + current = current.AddDays(1); + } + return count; + } + + /// + /// 添加工作日 + /// + public static DateTime AddWorkdays(DateTime date, int days) + { + var result = date.Date; + int step = days > 0 ? 1 : -1; + int remaining = Math.Abs(days); + + while (remaining > 0) + { + result = result.AddDays(step); + if (IsWorkday(result)) + remaining--; + } + + return result; + } + + /// + /// 清空节假日和调休设置 + /// + public static void Clear() + { + _holidays.Clear(); + _workdays.Clear(); + } + } + + /// + /// 友好时间显示工具类 + /// + public static class TimeAgoUtil + { + /// + /// 获取友好时间显示(如"3分钟前"、"昨天") + /// + public static string Format(DateTime date, DateTime? now = null) + { + return Format(date, now ?? DateTime.Now, false); + } + + /// + /// 获取友好时间显示(英文版) + /// + public static string FormatEnglish(DateTime date, DateTime? now = null) + { + return Format(date, now ?? DateTime.Now, true); + } + + private static string Format(DateTime date, DateTime now, bool english) + { + var span = now - date; + + if (span.TotalSeconds < 0) + { + return english ? "in the future" : "未来"; + } + + if (span.TotalSeconds < 60) + { + int seconds = (int)span.TotalSeconds; + return english ? $"{seconds} second{(seconds != 1 ? "s" : "")} ago" : $"{seconds}秒前"; + } + + if (span.TotalMinutes < 60) + { + int minutes = (int)span.TotalMinutes; + return english ? $"{minutes} minute{(minutes != 1 ? "s" : "")} ago" : $"{minutes}分钟前"; + } + + if (span.TotalHours < 24) + { + int hours = (int)span.TotalHours; + return english ? $"{hours} hour{(hours != 1 ? "s" : "")} ago" : $"{hours}小时前"; + } + + if (span.TotalDays < 2 && date.Date == now.Date.AddDays(-1)) + { + return english ? "yesterday" : "昨天"; + } + + if (span.TotalDays < 7) + { + int days = (int)span.TotalDays; + return english ? $"{days} day{(days != 1 ? "s" : "")} ago" : $"{days}天前"; + } + + if (span.TotalDays < 30) + { + int weeks = (int)(span.TotalDays / 7); + return english ? $"{weeks} week{(weeks != 1 ? "s" : "")} ago" : $"{weeks}周前"; + } + + if (span.TotalDays < 365) + { + int months = (int)(span.TotalDays / 30); + return english ? $"{months} month{(months != 1 ? "s" : "")} ago" : $"{months}个月前"; + } + + int years = (int)(span.TotalDays / 365); + return english ? $"{years} year{(years != 1 ? "s" : "")} ago" : $"{years}年前"; + } + + /// + /// 获取剩余时间显示(如"剩余3天") + /// + public static string FormatRemaining(DateTime deadline, DateTime? now = null) + { + return FormatRemaining(deadline, now ?? DateTime.Now, false); + } + + private static string FormatRemaining(DateTime deadline, DateTime now, bool english) + { + var span = deadline - now; + + if (span.TotalSeconds < 0) + { + return english ? "overdue" : "已过期"; + } + + if (span.TotalMinutes < 1) + { + return english ? "less than 1 minute" : "不到1分钟"; + } + + if (span.TotalHours < 1) + { + int minutes = (int)span.TotalMinutes; + return english ? $"{minutes} minute{(minutes != 1 ? "s" : "")} remaining" : $"剩余{minutes}分钟"; + } + + if (span.TotalDays < 1) + { + int hours = (int)span.TotalHours; + return english ? $"{hours} hour{(hours != 1 ? "s" : "")} remaining" : $"剩余{hours}小时"; + } + + if (span.TotalDays < 7) + { + int days = (int)span.TotalDays; + return english ? $"{days} day{(days != 1 ? "s" : "")} remaining" : $"剩余{days}天"; + } + + if (span.TotalDays < 30) + { + int weeks = (int)(span.TotalDays / 7); + return english ? $"{weeks} week{(weeks != 1 ? "s" : "")} remaining" : $"剩余{weeks}周"; + } + + int months = (int)(span.TotalDays / 30); + return english ? $"{months} month{(months != 1 ? "s" : "")} remaining" : $"剩余{months}个月"; + } + } +} diff --git a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs index 8dc0c5d..59bfcb7 100644 --- a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs +++ b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs @@ -347,5 +347,122 @@ private static int GetBit(int num, int bit) return (num >> (bit - 1)) & 1; } + #region 公历农历互转 + + /// + /// 公历转农历 + /// + /// 公历日期 + /// 农历日期信息,包含年、月、日 + public static LunarDate? SolarToLunar(DateTime solarDate) + { + try + { + int[] lunarDate = GetLunarDate(solarDate.Year, solarDate.Month, solarDate.Day); + return new LunarDate(lunarDate[0], lunarDate[1], lunarDate[2], lunarDate[3] > 0 && lunarDate[4] == 1); + } + catch + { + return null; + } + } + + /// + /// 农历转公历 + /// + /// 农历年份 + /// 农历月份(1-12) + /// 农历日期 + /// 是否为闰月 + /// 公历日期 + public static DateTime? LunarToSolar(int year, int month, int day, bool isLeapMonth = false) + { + if (year < 1900 || year > 2100) + return null; + + try + { + // 计算从1900年1月31日到目标农历日期的天数 + int offset = 0; + + // 累加年份天数 + for (int y = 1900; y < year; y++) + { + offset += GetLunarYearDays(y); + } + + int leapMonth = GetLunarLeapMonth(year); + bool leapProcessed = false; + + // 累加月份天数 + for (int m = 1; m < month; m++) + { + // 处理闰月 + if (leapMonth > 0 && m == leapMonth && !leapProcessed) + { + offset += GetLunarLeapMonthDays(year); + m--; // 重新处理当前月 + leapProcessed = true; + continue; + } + offset += GetLunarMonthDays(year, m); + } + + // 如果是闰月,需要加上闰月之前月份的天数 + if (isLeapMonth && leapMonth == month) + { + offset += GetLunarMonthDays(year, month); + } + + // 加上日期天数 + offset += day - 1; + + // 计算公历日期 + return new DateTime(1900, 1, 31).AddDays(offset); + } + catch + { + return null; + } + } + + #endregion + } + + /// + /// 农历日期信息 + /// + public class LunarDate + { + /// + /// 农历年 + /// + public int Year { get; } + + /// + /// 农历月 + /// + public int Month { get; } + + /// + /// 农历日 + /// + public int Day { get; } + + /// + /// 是否为闰月 + /// + public bool IsLeapMonth { get; } + + /// + /// 创建农历日期 + /// + public LunarDate(int year, int month, int day, bool isLeapMonth = false) + { + Year = year; + Month = month; + Day = day; + IsLeapMonth = isLeapMonth; + } } } \ No newline at end of file diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index e769fec..db83217 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -26,10 +26,6 @@ logo.png - - - - True @@ -49,7 +45,7 @@ - + diff --git a/EasyTool.Core/IOCategory/BomUtil.cs b/EasyTool.Core/IOCategory/BomUtil.cs new file mode 100644 index 0000000..67842d1 --- /dev/null +++ b/EasyTool.Core/IOCategory/BomUtil.cs @@ -0,0 +1,236 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// BOM(字节顺序标记)工具类 + /// 处理不同编码的 BOM + /// + public static class BomUtil + { + /// + /// BOM 定义 + /// + public static readonly Dictionary BomDefinitions = new() + { + {"UTF-8", new byte[] {0xEF, 0xBB, 0xBF}}, + {"UTF-16BE", new byte[] {0xFE, 0xFF}}, + {"UTF-16LE", new byte[] {0xFF, 0xFE}}, + {"UTF-32BE", new byte[] {0x00, 0x00, 0xFE, 0xFF}}, + {"UTF-32LE", new byte[] {0xFF, 0xFE, 0x00, 0x00}}, + {"UTF-7", new byte[] {0x2B, 0x2F, 0x76}}, + {"UTF-1", new byte[] {0xF7, 0x64, 0x4C}}, + {"UTF-EBCDIC", new byte[] {0xDD, 0x73, 0x66, 0x73}}, + {"SCSU", new byte[] {0x0E, 0xFE, 0xFF}}, + {"BOCU-1", new byte[] {0xFB, 0xEE, 0x28}}, + {"GB-18030", new byte[] {0x84, 0x31, 0x95, 0x33}}, + }; + + /// + /// 检测文件的 BOM + /// + public static BomInfo Detect(string filePath) + { + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + return Detect(stream); + } + + /// + /// 检测流的 BOM + /// + public static BomInfo Detect(Stream stream) + { + byte[] buffer = new byte[4]; + int bytesRead = stream.Read(buffer, 0, 4); + + // 重置流位置 + if (stream.CanSeek) + stream.Position = 0; + + return Detect(buffer, bytesRead); + } + + /// + /// 检测字节数组的 BOM + /// + public static BomInfo Detect(byte[] bytes) + { + return Detect(bytes, bytes.Length); + } + + private static BomInfo Detect(byte[] bytes, int length) + { + if (length < 2) + return new BomInfo { HasBom = false, Encoding = null, BomLength = 0 }; + + // UTF-8 + if (length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.UTF8, BomLength = 3 }; + } + + // UTF-32BE + if (length >= 4 && bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.GetEncoding("utf-32BE"), BomLength = 4 }; + } + + // UTF-32LE + if (length >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0x00 && bytes[3] == 0x00) + { + return new BomInfo { HasBom = true, Encoding = Encoding.GetEncoding("utf-32LE"), BomLength = 4 }; + } + + // UTF-16BE + if (length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.BigEndianUnicode, BomLength = 2 }; + } + + // UTF-16LE + if (length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) + { + return new BomInfo { HasBom = true, Encoding = Encoding.Unicode, BomLength = 2 }; + } + + // UTF-7 (可能有变体) + if (length >= 3 && bytes[0] == 0x2B && bytes[1] == 0x2F && bytes[2] == 0x76) + { + return new BomInfo { HasBom = true, Encoding = Encoding.UTF7, BomLength = 3 }; + } + + return new BomInfo { HasBom = false, Encoding = null, BomLength = 0 }; + } + + /// + /// 移除文件的 BOM + /// + public static void Remove(string filePath) + { + var bom = Detect(filePath); + if (!bom.HasBom) return; + + byte[] content = File.ReadAllBytes(filePath); + byte[] newContent = new byte[content.Length - bom.BomLength]; + Array.Copy(content, bom.BomLength, newContent, 0, newContent.Length); + File.WriteAllBytes(filePath, newContent); + } + + /// + /// 为文件添加 BOM + /// + public static void Add(string filePath, Encoding encoding) + { + byte[] bom = GetBom(encoding); + if (bom == null) return; + + var bomInfo = Detect(filePath); + if (bomInfo.HasBom) return; + + byte[] content = File.ReadAllBytes(filePath); + byte[] newContent = new byte[bom.Length + content.Length]; + Array.Copy(bom, 0, newContent, 0, bom.Length); + Array.Copy(content, 0, newContent, bom.Length, content.Length); + File.WriteAllBytes(filePath, newContent); + } + + /// + /// 获取指定编码的 BOM + /// + public static byte[] GetBom(Encoding encoding) + { + if (encoding == null) + return null; + + // 使用内置方法获取 BOM + if (encoding.Equals(Encoding.UTF8)) + return Encoding.UTF8.GetPreamble(); + if (encoding.Equals(Encoding.Unicode)) + return Encoding.Unicode.GetPreamble(); + if (encoding.Equals(Encoding.BigEndianUnicode)) + return Encoding.BigEndianUnicode.GetPreamble(); + if (encoding.Equals(Encoding.UTF32)) + return Encoding.UTF32.GetPreamble(); + + return null; + } + + /// + /// 读取文件内容(自动处理 BOM) + /// + public static string ReadAllText(string filePath) + { + var bom = Detect(filePath); + Encoding encoding = bom.Encoding ?? Encoding.UTF8; + + byte[] bytes = File.ReadAllBytes(filePath); + int offset = bom.HasBom ? bom.BomLength : 0; + int length = bytes.Length - offset; + + return encoding.GetString(bytes, offset, length); + } + + /// + /// 写入文件内容(可选是否添加 BOM) + /// + public static void WriteAllText(string filePath, string content, Encoding encoding, bool includeBom = true) + { + if (includeBom) + { + byte[] bom = GetBom(encoding); + byte[] contentBytes = encoding.GetBytes(content); + + if (bom != null && bom.Length > 0) + { + byte[] allBytes = new byte[bom.Length + contentBytes.Length]; + Array.Copy(bom, 0, allBytes, 0, bom.Length); + Array.Copy(contentBytes, 0, allBytes, bom.Length, contentBytes.Length); + File.WriteAllBytes(filePath, allBytes); + return; + } + } + + File.WriteAllText(filePath, content, encoding); + } + + /// + /// 转换文件编码(处理 BOM) + /// + public static void Convert(string filePath, Encoding targetEncoding, bool includeBom = true) + { + string content = ReadAllText(filePath); + WriteAllText(filePath, content, targetEncoding, includeBom); + } + } + + /// + /// BOM 信息 + /// + public class BomInfo + { + /// + /// 是否有 BOM + /// + public bool HasBom { get; set; } + + /// + /// 编码 + /// + public Encoding Encoding { get; set; } + + /// + /// BOM 长度(字节) + /// + public int BomLength { get; set; } + + public override string ToString() + { + return HasBom + ? $"Has BOM: {Encoding?.WebName ?? "Unknown"}, Length: {BomLength}" + : "No BOM detected"; + } + } +} diff --git a/EasyTool.Core/IOCategory/CsvUtil.cs b/EasyTool.Core/IOCategory/CsvUtil.cs new file mode 100644 index 0000000..cdcc0ef --- /dev/null +++ b/EasyTool.Core/IOCategory/CsvUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// CSV 工具类 + /// 提供高性能的 CSV 读写功能 + /// + public static class CsvUtil + { + /// + /// 读取 CSV 文件 + /// + public static List Read(string filePath, bool hasHeader = true, char delimiter = ',', char quote = '"') + { + var reader = new CsvReader(delimiter, quote); + return reader.ReadFile(filePath, hasHeader); + } + + /// + /// 读取 CSV 文件(带表头映射) + /// + public static List> ReadWithHeader(string filePath, char delimiter = ',', char quote = '"') + { + var reader = new CsvReader(delimiter, quote); + return reader.ReadFileWithHeader(filePath); + } + + /// + /// 从字符串解析 CSV + /// + public static List Parse(string content, bool hasHeader = true, char delimiter = ',', char quote = '"') + { + var reader = new CsvReader(delimiter, quote); + return reader.Parse(content, hasHeader); + } + + /// + /// 写入 CSV 文件 + /// + public static void Write(string filePath, IEnumerable rows, char delimiter = ',', char quote = '"') + { + var writer = new CsvWriter(delimiter, quote); + writer.WriteFile(filePath, rows); + } + + /// + /// 写入 CSV 文件(带表头) + /// + public static void WriteWithHeader(string filePath, string[] headers, IEnumerable> rows, + char delimiter = ',', char quote = '"') + { + var writer = new CsvWriter(delimiter, quote); + writer.WriteFileWithHeader(filePath, headers, rows); + } + + /// + /// 将数据转换为 CSV 字符串 + /// + public static string ToString(IEnumerable rows, char delimiter = ',', char quote = '"') + { + var writer = new CsvWriter(delimiter, quote); + return writer.ToString(rows); + } + + /// + /// 转义 CSV 字段 + /// + public static string EscapeField(string field, char delimiter = ',', char quote = '"') + { + if (string.IsNullOrEmpty(field)) + return ""; + + bool needsQuote = field.Contains(delimiter.ToString()) || + field.Contains(quote.ToString()) || + field.Contains("\n") || + field.Contains("\r"); + + if (needsQuote) + { + return quote + field.Replace(quote.ToString(), quote.ToString() + quote.ToString()) + quote; + } + + return field; + } + + /// + /// 反转义 CSV 字段 + /// + public static string UnescapeField(string field, char quote = '"') + { + if (string.IsNullOrEmpty(field)) + return field; + + if (field.StartsWith(quote.ToString()) && field.EndsWith(quote.ToString())) + { + string inner = field.Substring(1, field.Length - 2); + return inner.Replace(quote.ToString() + quote.ToString(), quote.ToString()); + } + + return field; + } + } + + /// + /// CSV 读取器 + /// + public class CsvReader + { + private readonly char _delimiter; + private readonly char _quote; + + /// + /// 创建 CSV 读取器 + /// + public CsvReader(char delimiter = ',', char quote = '"') + { + _delimiter = delimiter; + _quote = quote; + } + + /// + /// 读取文件 + /// + public List ReadFile(string filePath, bool hasHeader = true) + { + var content = File.ReadAllText(filePath, DetectEncoding(filePath)); + return Parse(content, hasHeader); + } + + /// + /// 读取文件(带表头) + /// + public List> ReadFileWithHeader(string filePath) + { + var rows = ReadFile(filePath, true); + if (rows.Count == 0) + return new List>(); + + var headers = rows[0]; + var result = new List>(); + + for (int i = 1; i < rows.Count; i++) + { + var dict = new Dictionary(); + for (int j = 0; j < headers.Length && j < rows[i].Length; j++) + { + dict[headers[j]] = rows[i][j]; + } + result.Add(dict); + } + + return result; + } + + /// + /// 解析 CSV 内容 + /// + public List Parse(string content, bool hasHeader = true) + { + var rows = new List(); + var fields = new List(); + var currentField = new StringBuilder(); + bool inQuotes = false; + int startRow = hasHeader ? 0 : 0; + + int rowIndex = 0; + for (int i = 0; i < content.Length; i++) + { + char c = content[i]; + + if (inQuotes) + { + if (c == _quote) + { + // 检查是否是转义的引号 + if (i + 1 < content.Length && content[i + 1] == _quote) + { + currentField.Append(_quote); + i++; + } + else + { + inQuotes = false; + } + } + else + { + currentField.Append(c); + } + } + else + { + if (c == _quote) + { + inQuotes = true; + } + else if (c == _delimiter) + { + fields.Add(currentField.ToString()); + currentField.Clear(); + } + else if (c == '\n' || c == '\r') + { + fields.Add(currentField.ToString()); + currentField.Clear(); + + if (rowIndex >= startRow) + { + rows.Add(fields.ToArray()); + } + fields.Clear(); + rowIndex++; + + // 处理 \r\n + if (c == '\r' && i + 1 < content.Length && content[i + 1] == '\n') + i++; + } + else + { + currentField.Append(c); + } + } + } + + // 添加最后一个字段和行 + if (currentField.Length > 0 || fields.Count > 0) + { + fields.Add(currentField.ToString()); + if (rowIndex >= startRow) + { + rows.Add(fields.ToArray()); + } + } + + return rows; + } + + private static Encoding DetectEncoding(string filePath) + { + // 简单的编码检测 + byte[] bom = new byte[4]; + using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + int bytesRead = 0; + int bytesToRead = 4; + while (bytesRead < bytesToRead) + { + int read = file.Read(bom, bytesRead, bytesToRead - bytesRead); + if (read == 0) break; // 文件结束 + bytesRead += read; + } + } + + if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) + return Encoding.UTF8; + if (bom[0] == 0xff && bom[1] == 0xfe) + return Encoding.Unicode; + if (bom[0] == 0xfe && bom[1] == 0xff) + return Encoding.BigEndianUnicode; + + return Encoding.UTF8; + } + } + + /// + /// CSV 写入器 + /// + public class CsvWriter + { + private readonly char _delimiter; + private readonly char _quote; + + /// + /// 创建 CSV 写入器 + /// + public CsvWriter(char delimiter = ',', char quote = '"') + { + _delimiter = delimiter; + _quote = quote; + } + + /// + /// 写入文件 + /// + public void WriteFile(string filePath, IEnumerable rows) + { + var content = ToString(rows); + File.WriteAllText(filePath, content, Encoding.UTF8); + } + + /// + /// 写入文件(带表头) + /// + public void WriteFileWithHeader(string filePath, string[] headers, IEnumerable> rows) + { + var allRows = new List { headers }; + + foreach (var row in rows) + { + var fields = new string[headers.Length]; + for (int i = 0; i < headers.Length; i++) + { + fields[i] = row.TryGetValue(headers[i], out var value) ? value : ""; + } + allRows.Add(fields); + } + + WriteFile(filePath, allRows); + } + + /// + /// 转换为 CSV 字符串 + /// + public string ToString(IEnumerable rows) + { + var sb = new StringBuilder(); + + foreach (var row in rows) + { + for (int i = 0; i < row.Length; i++) + { + if (i > 0) sb.Append(_delimiter); + sb.Append(CsvUtil.EscapeField(row[i], _delimiter, _quote)); + } + sb.AppendLine(); + } + + return sb.ToString(); + } + } + + /// + /// CSV 配置 + /// + public class CsvConfiguration + { + /// + /// 分隔符 + /// + public char Delimiter { get; set; } = ','; + + /// + /// 引号字符 + /// + public char Quote { get; set; } = '"'; + + /// + /// 是否有表头 + /// + public bool HasHeader { get; set; } = true; + + /// + /// 编码 + /// + public Encoding Encoding { get; set; } = Encoding.UTF8; + + /// + /// 换行符 + /// + public string NewLine { get; set; } = Environment.NewLine; + + /// + /// 默认配置 + /// + public static CsvConfiguration Default => new CsvConfiguration(); + + /// + /// 中文配置(使用制表符分隔) + /// + public static CsvConfiguration Chinese => new CsvConfiguration { Delimiter = '\t' }; + } +} diff --git a/EasyTool.Core/IOCategory/FileSignatureUtil.cs b/EasyTool.Core/IOCategory/FileSignatureUtil.cs new file mode 100644 index 0000000..be72d91 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSignatureUtil.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 文件签名(魔数)检测工具类 + /// 通过文件头字节判断真实文件类型 + /// + public static class FileSignatureUtil + { + /// + /// 常见文件签名 + /// + private static readonly List Signatures = new() + { + // 图片 + new("jpg", "JPEG Image", new[] { "FF D8 FF" }, new[] { ".jpg", ".jpeg" }), + new("png", "PNG Image", new[] { "89 50 4E 47 0D 0A 1A 0A" }, new[] { ".png" }), + new("gif", "GIF Image", new[] { "47 49 46 38 37 61", "47 49 46 38 39 61" }, new[] { ".gif" }), + new("bmp", "BMP Image", new[] { "42 4D" }, new[] { ".bmp" }), + new("webp", "WebP Image", new[] { "52 49 46 46 ?? ?? ?? ?? 57 45 42 50" }, new[] { ".webp" }), + new("ico", "ICO Image", new[] { "00 00 01 00" }, new[] { ".ico" }), + new("svg", "SVG Image", new[] { "3C 3F 78 6D 6C", "3C 73 76 67" }, new[] { ".svg" }), + new("tiff", "TIFF Image", new[] { "49 49 2A 00", "4D 4D 00 2A" }, new[] { ".tiff", ".tif" }), + + // 文档 + new("pdf", "PDF Document", new[] { "25 50 44 46" }, new[] { ".pdf" }), + new("doc", "Word Document (old)", new[] { "D0 CF 11 E0 A1 B1 1A E1" }, new[] { ".doc", ".xls", ".ppt" }), + new("docx", "Word Document", new[] { "50 4B 03 04 14 00 06 00" }, new[] { ".docx", ".xlsx", ".pptx" }), + new("rtf", "RTF Document", new[] { "7B 5C 72 74 66 31" }, new[] { ".rtf" }), + + // 压缩 + new("zip", "ZIP Archive", new[] { "50 4B 03 04", "50 4B 05 06", "50 4B 07 08" }, new[] { ".zip" }), + new("rar", "RAR Archive", new[] { "52 61 72 21 1A 07" }, new[] { ".rar" }), + new("7z", "7-Zip Archive", new[] { "37 7A BC AF 27 1C" }, new[] { ".7z" }), + new("tar", "TAR Archive", new[] { "75 73 74 61 72" }, new[] { ".tar" }, 257), + new("gz", "GZIP Archive", new[] { "1F 8B" }, new[] { ".gz", ".gzip" }), + new("bz2", "BZIP2 Archive", new[] { "42 5A 68" }, new[] { ".bz2" }), + + // 音频 + new("mp3", "MP3 Audio", new[] { "FF FB", "FF FA", "FF F3", "FF F2", "49 44 33" }, new[] { ".mp3" }), + new("wav", "WAV Audio", new[] { "52 49 46 46 ?? ?? ?? ?? 57 41 56 45" }, new[] { ".wav" }), + new("flac", "FLAC Audio", new[] { "66 4C 61 43" }, new[] { ".flac" }), + new("m4a", "M4A Audio", new[] { "66 74 79 70 4D 34 41" }, new[] { ".m4a" }), + new("ogg", "OGG Audio", new[] { "4F 67 67 53" }, new[] { ".ogg" }), + + // 视频 + new("mp4", "MP4 Video", new[] { "66 74 79 70 69 73 6F 6D", "66 74 79 70 6D 70 34 32" }, new[] { ".mp4" }), + new("avi", "AVI Video", new[] { "52 49 46 46 ?? ?? ?? ?? 41 56 49 20" }, new[] { ".avi" }), + new("mkv", "MKV Video", new[] { "1A 45 DF A3" }, new[] { ".mkv", ".webm" }), + new("mov", "MOV Video", new[] { "66 74 79 70 71 74 20 20" }, new[] { ".mov" }), + new("flv", "FLV Video", new[] { "46 4C 56" }, new[] { ".flv" }), + new("wmv", "WMV Video", new[] { "30 26 B2 75 8E 66 CF 11" }, new[] { ".wmv", ".asf" }), + + // 可执行 + new("exe", "Windows Executable", new[] { "4D 5A" }, new[] { ".exe", ".dll" }), + new("elf", "Linux Executable", new[] { "7F 45 4C 46" }, new[] { "" }), + new("class", "Java Class", new[] { "CA FE BA BE" }, new[] { ".class" }), + new("dex", "Android DEX", new[] { "64 65 78 0A 30 33 35" }, new[] { ".dex" }), + new("apk", "Android APK", new[] { "50 4B 03 04" }, new[] { ".apk" }), + + // 其他 + new("sqlite", "SQLite Database", new[] { "53 51 4C 69 74 65 21" }, new[] { ".sqlite", ".db" }), + new("psd", "Photoshop Document", new[] { "38 42 50 53" }, new[] { ".psd" }), + new("ai", "Adobe Illustrator", new[] { "25 50 44 46" }, new[] { ".ai" }), + new("swf", "Flash SWF", new[] { "46 57 53", "43 57 53" }, new[] { ".swf" }), + new("torrent", "Torrent File", new[] { "64 38 3A 61 6E 6E 6F 75 6E 63 65" }, new[] { ".torrent" }), + }; + + /// + /// 检测文件类型 + /// + /// 文件路径 + /// 文件类型信息 + public static FileTypeInfo? Detect(string filePath) + { + if (!File.Exists(filePath)) + return null; + + using var stream = File.OpenRead(filePath); + return Detect(stream); + } + + /// + /// 检测文件类型 + /// + /// 文件流 + /// 文件类型信息 + public static FileTypeInfo? Detect(Stream stream) + { + if (stream == null || stream.Length == 0) + return null; + + var header = new byte[Math.Min(32, stream.Length)]; + var originalPosition = stream.Position; + stream.Position = 0; + stream.Read(header, 0, header.Length); + stream.Position = originalPosition; + + return DetectFromHeader(header); + } + + /// + /// 从字节数组检测文件类型 + /// + /// 文件字节数组 + /// 文件类型信息 + public static FileTypeInfo? DetectFromBytes(byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return null; + + var header = new byte[Math.Min(32, bytes.Length)]; + Array.Copy(bytes, header, header.Length); + + return DetectFromHeader(header); + } + + /// + /// 从文件头检测文件类型 + /// + /// 文件头字节 + /// 文件类型信息 + public static FileTypeInfo? DetectFromHeader(byte[] header) + { + if (header == null || header.Length == 0) + return null; + + foreach (var signature in Signatures) + { + foreach (var pattern in signature.Patterns) + { + if (MatchesPattern(header, pattern, signature.Offset)) + { + return new FileTypeInfo + { + TypeId = signature.TypeId, + Description = signature.Description, + Extensions = signature.Extensions + }; + } + } + } + + return null; + } + + /// + /// 验证文件扩展名是否与实际内容匹配 + /// + /// 文件路径 + /// 是否匹配 + public static bool ValidateExtension(string filePath) + { + var detected = Detect(filePath); + if (detected == null) + return true; // 无法检测时默认通过 + + var extension = Path.GetExtension(filePath).ToLowerInvariant(); + return detected.Extensions.Contains(extension); + } + + /// + /// 获取文件的真实扩展名 + /// + /// 文件路径 + /// 扩展名(包含点号) + public static string? GetRealExtension(string filePath) + { + var detected = Detect(filePath); + return detected?.Extensions.FirstOrDefault(); + } + + /// + /// 检查文件是否为图片 + /// + /// 文件路径 + /// 是否为图片 + public static bool IsImage(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "jpg" or "png" or "gif" or "bmp" or "webp" or "ico" or "svg" or "tiff"; + } + + /// + /// 检查文件是否为视频 + /// + /// 文件路径 + /// 是否为视频 + public static bool IsVideo(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "mp4" or "avi" or "mkv" or "mov" or "flv" or "wmv"; + } + + /// + /// 检查文件是否为音频 + /// + /// 文件路径 + /// 是否为音频 + public static bool IsAudio(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "mp3" or "wav" or "flac" or "m4a" or "ogg"; + } + + /// + /// 检查文件是否为文档 + /// + /// 文件路径 + /// 是否为文档 + public static bool IsDocument(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "pdf" or "doc" or "docx" or "rtf"; + } + + /// + /// 检查文件是否为压缩包 + /// + /// 文件路径 + /// 是否为压缩包 + public static bool IsArchive(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "zip" or "rar" or "7z" or "tar" or "gz" or "bz2"; + } + + /// + /// 检查文件是否为可执行文件 + /// + /// 文件路径 + /// 是否为可执行文件 + public static bool IsExecutable(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "exe" or "elf" or "class" or "dex"; + } + + private static bool MatchesPattern(byte[] header, string pattern, int offset = 0) + { + var patternBytes = pattern.Split(' '); + var requiredLength = offset + patternBytes.Length; + + if (header.Length < requiredLength) + return false; + + for (int i = 0; i < patternBytes.Length; i++) + { + var patternByte = patternBytes[i]; + var headerByte = header[offset + i]; + + if (patternByte == "??") + continue; + + if (!byte.TryParse(patternByte, System.Globalization.NumberStyles.HexNumber, null, out var expectedByte)) + continue; + + if (headerByte != expectedByte) + return false; + } + + return true; + } + + #region 内部类 + + private class FileSignature + { + public string TypeId { get; } + public string Description { get; } + public string[] Patterns { get; } + public string[] Extensions { get; } + public int Offset { get; } + + public FileSignature(string typeId, string description, string[] patterns, string[] extensions, int offset = 0) + { + TypeId = typeId; + Description = description; + Patterns = patterns; + Extensions = extensions; + Offset = offset; + } + } + + #endregion + } + + /// + /// 文件类型信息 + /// + public class FileTypeInfo + { + /// + /// 类型标识 + /// + public string TypeId { get; set; } = string.Empty; + + /// + /// 类型描述 + /// + public string Description { get; set; } = string.Empty; + + /// + /// 可能的文件扩展名 + /// + public string[] Extensions { get; set; } = Array.Empty(); + + public override string ToString() => $"{TypeId} ({Description})"; + } +} diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index eb8133e..62e271c 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -28,19 +28,24 @@ public static class FileUtil /// /// 文件或目录的路径 /// 是否为空 + /// 路径不存在时抛出异常 public static bool IsEmpty(string path) { // 判断是否为目录 if (Directory.Exists(path)) { - // 如果是目录,遍历目录下的所有文件,判断是否有文件 - return Directory.GetFiles(path).Length>0; + // 如果是目录,判断目录下是否有文件(没有文件则为空) + return Directory.GetFiles(path).Length == 0; } - else + else if (File.Exists(path)) { // 如果是文件,判断文件大小是否为 0 return new FileInfo(path).Length == 0; } + else + { + throw new FileNotFoundException($"路径 {path} 不存在"); + } } diff --git a/EasyTool.Core/IOCategory/IniUtil.cs b/EasyTool.Core/IOCategory/IniUtil.cs new file mode 100644 index 0000000..9142dba --- /dev/null +++ b/EasyTool.Core/IOCategory/IniUtil.cs @@ -0,0 +1,328 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// INI 文件工具类 + /// 提供 INI 配置文件的读写功能 + /// + public static class IniUtil + { + /// + /// 读取 INI 文件 + /// + public static IniFile Read(string filePath) + { + var ini = new IniFile(); + ini.Load(filePath); + return ini; + } + + /// + /// 读取 INI 文件中的值 + /// + public static string GetValue(string filePath, string section, string key, string defaultValue = "") + { + var ini = Read(filePath); + return ini.GetValue(section, key, defaultValue); + } + + /// + /// 写入值到 INI 文件 + /// + public static void SetValue(string filePath, string section, string key, string value) + { + var ini = Read(filePath); + ini.SetValue(section, key, value); + ini.Save(filePath); + } + + /// + /// 创建空的 INI 文件对象 + /// + public static IniFile Create() + { + return new IniFile(); + } + } + + /// + /// INI 文件对象 + /// + public class IniFile + { + private readonly Dictionary> _sections; + private readonly List _sectionOrder; + private string _commentPrefix = ";"; + + /// + /// 注释前缀 + /// + public string CommentPrefix + { + get => _commentPrefix; + set => _commentPrefix = value ?? ";"; + } + + /// + /// 节名称列表 + /// + public IEnumerable Sections => _sectionOrder; + + /// + /// 创建 INI 文件对象 + /// + public IniFile() + { + _sections = new Dictionary>(StringComparer.OrdinalIgnoreCase); + _sectionOrder = new List(); + } + + /// + /// 从文件加载 + /// + public void Load(string filePath) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath, Encoding.UTF8); + string currentSection = ""; + + foreach (var line in lines) + { + string trimmed = line.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith(_commentPrefix) || trimmed.StartsWith("#")) + continue; + + // 节 + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed.Substring(1, trimmed.Length - 2).Trim(); + if (!_sections.ContainsKey(currentSection)) + { + _sections[currentSection] = new Dictionary(StringComparer.OrdinalIgnoreCase); + _sectionOrder.Add(currentSection); + } + continue; + } + + // 键值对 + int equalIndex = trimmed.IndexOf('='); + if (equalIndex > 0) + { + string key = trimmed.Substring(0, equalIndex).Trim(); + string value = trimmed.Substring(equalIndex + 1).Trim(); + + // 移除行内注释 + int commentIndex = value.IndexOf(_commentPrefix); + if (commentIndex >= 0) + { + value = value.Substring(0, commentIndex).Trim(); + } + + // 处理引号包裹的值 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + value = value.Substring(1, value.Length - 2); + } + + if (!_sections.ContainsKey(currentSection)) + { + _sections[currentSection] = new Dictionary(StringComparer.OrdinalIgnoreCase); + if (!string.IsNullOrEmpty(currentSection)) + _sectionOrder.Add(currentSection); + } + + _sections[currentSection][key] = value; + } + } + } + + /// + /// 保存到文件 + /// + public void Save(string filePath) + { + var sb = new StringBuilder(); + + // 先写入空节的值 + if (_sections.TryGetValue("", out var globalSection)) + { + foreach (var kvp in globalSection) + { + sb.AppendLine($"{kvp.Key}={FormatValue(kvp.Value)}"); + } + sb.AppendLine(); + } + + // 写入各节 + foreach (var section in _sectionOrder) + { + if (string.IsNullOrEmpty(section)) continue; + + sb.AppendLine($"[{section}]"); + if (_sections.TryGetValue(section, out var sectionData)) + { + foreach (var kvp in sectionData) + { + sb.AppendLine($"{kvp.Key}={FormatValue(kvp.Value)}"); + } + } + sb.AppendLine(); + } + + File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); + } + + private static string FormatValue(string value) + { + if (string.IsNullOrEmpty(value)) return ""; + if (value.Contains(";") || value.Contains("#") || value.Contains(" ")) + return $"\"{value}\""; + return value; + } + + /// + /// 获取值 + /// + public string GetValue(string section, string key, string defaultValue = "") + { + if (_sections.TryGetValue(section, out var sectionData)) + { + if (sectionData.TryGetValue(key, out var value)) + return value; + } + return defaultValue; + } + + /// + /// 获取值并转换为指定类型 + /// + public T GetValue(string section, string key, T defaultValue = default) + { + string value = GetValue(section, key); + if (string.IsNullOrEmpty(value)) + return defaultValue; + + try + { + var type = typeof(T); + if (type == typeof(string)) + return (T)(object)value; + if (type == typeof(int)) + return (T)(object)int.Parse(value); + if (type == typeof(long)) + return (T)(object)long.Parse(value); + if (type == typeof(double)) + return (T)(object)double.Parse(value); + if (type == typeof(bool)) + return (T)(object)ParseBool(value); + if (type == typeof(DateTime)) + return (T)(object)DateTime.Parse(value); + + return (T)Convert.ChangeType(value, type); + } + catch + { + return defaultValue; + } + } + + private static bool ParseBool(string value) + { + return value.Equals("true", StringComparison.OrdinalIgnoreCase) || + value.Equals("1", StringComparison.OrdinalIgnoreCase) || + value.Equals("yes", StringComparison.OrdinalIgnoreCase) || + value.Equals("on", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 设置值 + /// + public void SetValue(string section, string key, string value) + { + if (!_sections.TryGetValue(section, out var sectionData)) + { + sectionData = new Dictionary(StringComparer.OrdinalIgnoreCase); + _sections[section] = sectionData; + if (!string.IsNullOrEmpty(section) && !_sectionOrder.Contains(section)) + _sectionOrder.Add(section); + } + + sectionData[key] = value; + } + + /// + /// 设置值 + /// + public void SetValue(string section, string key, T value) + { + SetValue(section, key, value?.ToString()); + } + + /// + /// 删除键 + /// + public bool DeleteKey(string section, string key) + { + if (_sections.TryGetValue(section, out var sectionData)) + { + return sectionData.Remove(key); + } + return false; + } + + /// + /// 删除节 + /// + public bool DeleteSection(string section) + { + _sectionOrder.Remove(section); + return _sections.Remove(section); + } + + /// + /// 获取节中的所有键值对 + /// + public Dictionary GetSection(string section) + { + if (_sections.TryGetValue(section, out var sectionData)) + { + return new Dictionary(sectionData); + } + return new Dictionary(); + } + + /// + /// 节是否存在 + /// + public bool HasSection(string section) + { + return _sections.ContainsKey(section); + } + + /// + /// 键是否存在 + /// + public bool HasKey(string section, string key) + { + return _sections.TryGetValue(section, out var sectionData) && sectionData.ContainsKey(key); + } + + /// + /// 清空所有内容 + /// + public void Clear() + { + _sections.Clear(); + _sectionOrder.Clear(); + } + } +} diff --git a/EasyTool.Core/IOCategory/JsonUtil.cs b/EasyTool.Core/IOCategory/JsonUtil.cs new file mode 100644 index 0000000..d1a6c24 --- /dev/null +++ b/EasyTool.Core/IOCategory/JsonUtil.cs @@ -0,0 +1,550 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace EasyTool.IOCategory +{ + /// + /// JSON 工具类 + /// 提供 JSON 序列化/反序列化的增强功能 + /// + public static class JsonUtil + { + #region 默认选项 + + /// + /// 默认序列化选项(驼峰命名、缩进、忽略null) + /// + public static JsonSerializerOptions DefaultOptions => new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString, + Converters = + { + new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) + } + }; + + /// + /// 紧凑序列化选项(无缩进、忽略null、驼峰命名) + /// + public static JsonSerializerOptions CompactOptions => new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString + }; + + /// + /// 宽松反序列化选项(允许不带引号的数字、允许注释、允许尾随逗号) + /// + public static JsonSerializerOptions LenientOptions => new() + { + PropertyNameCaseInsensitive = true, + NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + + #endregion + + #region 序列化 + + /// + /// 将对象序列化为 JSON 字符串 + /// + /// 对象类型 + /// 要序列化的对象 + /// 序列化选项(可选) + /// JSON 字符串 + public static string Serialize(T obj, JsonSerializerOptions? options = null) + { + if (obj == null) + return "null"; + + return JsonSerializer.Serialize(obj, options ?? DefaultOptions); + } + + /// + /// 将对象序列化为 JSON 字符串(紧凑格式) + /// + /// 对象类型 + /// 要序列化的对象 + /// 紧凑格式的 JSON 字符串 + public static string SerializeCompact(T obj) + { + return Serialize(obj, CompactOptions); + } + + /// + /// 将对象序列化为 JSON 字节数组 + /// + /// 对象类型 + /// 要序列化的对象 + /// 序列化选项(可选) + /// JSON 字节数组 + public static byte[] SerializeToUtf8Bytes(T obj, JsonSerializerOptions? options = null) + { + return JsonSerializer.SerializeToUtf8Bytes(obj, options ?? DefaultOptions); + } + + #endregion + + #region 反序列化 + + /// + /// 将 JSON 字符串反序列化为对象 + /// + /// 目标类型 + /// JSON 字符串 + /// 反序列化选项(可选) + /// 反序列化后的对象 + public static T? Deserialize(string json, JsonSerializerOptions? options = null) + { + if (string.IsNullOrWhiteSpace(json)) + return default; + + return JsonSerializer.Deserialize(json, options ?? LenientOptions); + } + + /// + /// 将 JSON 字节数组反序列化为对象 + /// + /// 目标类型 + /// JSON 字节数组 + /// 反序列化选项(可选) + /// 反序列化后的对象 + public static T? Deserialize(byte[] utf8Json, JsonSerializerOptions? options = null) + { + if (utf8Json == null || utf8Json.Length == 0) + return default; + + return JsonSerializer.Deserialize(utf8Json, options ?? LenientOptions); + } + + /// + /// 尝试将 JSON 字符串反序列化为对象 + /// + /// 目标类型 + /// JSON 字符串 + /// 反序列化结果 + /// 反序列化选项(可选) + /// 是否成功 + public static bool TryDeserialize(string json, out T? result, JsonSerializerOptions? options = null) + { + result = default; + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + result = JsonSerializer.Deserialize(json, options ?? LenientOptions); + return true; + } + catch + { + return false; + } + } + + /// + /// 将 JSON 字符串反序列化为动态对象 + /// + /// JSON 字符串 + /// JsonNode 对象 + public static JsonNode? Parse(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return null; + + return JsonNode.Parse(json); + } + + #endregion + + #region 格式化与验证 + + /// + /// 格式化 JSON 字符串(美化输出) + /// + /// JSON 字符串 + /// 缩进字符(默认2个空格) + /// 格式化后的 JSON 字符串 + public static string Prettify(string json, string indent = " ") + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + var options = new JsonSerializerOptions + { + WriteIndented = true + }; +#if NET9_0_OR_GREATER + if (indent.Length > 0) + { + options.IndentCharacter = indent[0]; + options.IndentSize = indent.Length; + } +#endif + return node?.ToJsonString(options) ?? json; + } + catch + { + return json; + } + } + + /// + /// 压缩 JSON 字符串(移除空白) + /// + /// JSON 字符串 + /// 压缩后的 JSON 字符串 + public static string Minify(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + return node?.ToJsonString(new JsonSerializerOptions { WriteIndented = false }) ?? json; + } + catch + { + return json; + } + } + + /// + /// 验证是否为有效的 JSON + /// + /// JSON 字符串 + /// 是否有效 + public static bool IsValid(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + JsonNode.Parse(json); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 路径操作 + + /// + /// 从 JSON 字符串中获取指定路径的值 + /// + /// JSON 字符串 + /// 路径(使用点号分隔,如 "user.name") + /// 找到的值,未找到返回 null + public static object? GetValue(string json, string path) + { + if (string.IsNullOrWhiteSpace(json) || string.IsNullOrWhiteSpace(path)) + return null; + + try + { + var node = JsonNode.Parse(json); + return GetValueByPath(node, path); + } + catch + { + return null; + } + } + + /// + /// 从 JSON 字符串中获取指定路径的值并转换为指定类型 + /// + /// 目标类型 + /// JSON 字符串 + /// 路径 + /// 转换后的值 + public static T? GetValue(string json, string path) + { + var value = GetValue(json, path); + if (value == null) + return default; + + if (value is JsonValue jsonValue) + { + return jsonValue.GetValue(); + } + + if (value is JsonNode jsonNode) + { + return jsonNode.Deserialize(LenientOptions); + } + + return (T?)Convert.ChangeType(value, typeof(T)); + } + + /// + /// 设置 JSON 字符串中指定路径的值 + /// + /// JSON 字符串 + /// 路径 + /// 要设置的值 + /// 修改后的 JSON 字符串 + public static string SetValue(string json, string path, object? value) + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + SetValueByPath(node, path, value); + return node?.ToJsonString(DefaultOptions) ?? json; + } + catch + { + return json; + } + } + + private static object? GetValueByPath(JsonNode? node, string path) + { + if (node == null) + return null; + + var parts = path.Split('.'); + JsonNode? current = node; + + foreach (var part in parts) + { + if (current == null) + return null; + + // 处理数组索引,如 items[0] + if (part.Contains('[') && part.EndsWith(']')) + { + var name = part.Substring(0, part.IndexOf('[')); + var indexStr = part.Substring(part.IndexOf('[') + 1, part.Length - part.IndexOf('[') - 2); + + if (!string.IsNullOrEmpty(name)) + current = current[name]; + + if (int.TryParse(indexStr, out int index) && current is JsonArray array) + { + current = index < array.Count ? array[index] : null; + } + } + else + { + current = current[part]; + } + } + + return current; + } + + private static void SetValueByPath(JsonNode? node, string path, object? value) + { + if (node == null) + return; + + var parts = path.Split('.'); + JsonNode current = node; + + for (int i = 0; i < parts.Length - 1; i++) + { + var part = parts[i]; + + if (part.Contains('[') && part.EndsWith(']')) + { + var name = part.Substring(0, part.IndexOf('[')); + var indexStr = part.Substring(part.IndexOf('[') + 1, part.Length - part.IndexOf('[') - 2); + + if (!string.IsNullOrEmpty(name)) + { + if (current[name] == null) + current[name] = new JsonObject(); + current = current[name]!; + } + + if (int.TryParse(indexStr, out int index)) + { + if (current is JsonArray array) + { + while (array.Count <= index) + array.Add(null); + current = array[index] ??= new JsonObject(); + } + } + } + else + { + if (current[part] == null) + current[part] = new JsonObject(); + current = current[part]!; + } + } + + var lastPart = parts[^1]; + if (lastPart.Contains('[') && lastPart.EndsWith(']')) + { + var name = lastPart.Substring(0, lastPart.IndexOf('[')); + var indexStr = lastPart.Substring(lastPart.IndexOf('[') + 1, lastPart.Length - lastPart.IndexOf('[') - 2); + + JsonNode? target = current; + if (!string.IsNullOrEmpty(name)) + { + if (current[name] == null) + current[name] = new JsonArray(); + target = current[name]; + } + + if (int.TryParse(indexStr, out int index) && target is JsonArray array) + { + while (array.Count <= index) + array.Add(null); + array[index] = JsonValue.Create(value); + } + } + else + { + current[lastPart] = JsonValue.Create(value); + } + } + + #endregion + + #region 转换操作 + + /// + /// 将字典转换为 JSON 对象 + /// + /// 键类型 + /// 值类型 + /// 字典 + /// JSON 字符串 + public static string FromDictionary(Dictionary dictionary) + { + if (dictionary == null) + return "{}"; + + return JsonSerializer.Serialize(dictionary, DefaultOptions); + } + + /// + /// 将 JSON 对象转换为字典 + /// + /// 值类型 + /// JSON 字符串 + /// 字典 + public static Dictionary? ToDictionary(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return null; + + return JsonSerializer.Deserialize>(json, LenientOptions); + } + + /// + /// 将匿名对象转换为 JSON 字符串 + /// + /// 匿名对象 + /// JSON 字符串 + public static string FromAnonymous(object obj) + { + return Serialize(obj, CompactOptions); + } + + /// + /// 深拷贝对象(通过 JSON 序列化/反序列化) + /// + /// 对象类型 + /// 要拷贝的对象 + /// 拷贝后的对象 + public static T? DeepClone(T obj) + { + if (obj == null) + return default; + + var json = JsonSerializer.Serialize(obj, DefaultOptions); + return JsonSerializer.Deserialize(json, LenientOptions); + } + + #endregion + + #region 合并操作 + + /// + /// 合并两个 JSON 对象 + /// + /// 第一个 JSON + /// 第二个 JSON(优先级更高) + /// 合并后的 JSON + public static string Merge(string json1, string json2) + { + if (string.IsNullOrWhiteSpace(json1)) + return json2; + if (string.IsNullOrWhiteSpace(json2)) + return json1; + + try + { + var node1 = JsonNode.Parse(json1) as JsonObject; + var node2 = JsonNode.Parse(json2) as JsonObject; + + if (node1 == null) + return json2; + if (node2 == null) + return json1; + + MergeObjects(node1, node2); + return node1.ToJsonString(DefaultOptions); + } + catch + { + return json1; + } + } + + private static void MergeObjects(JsonObject target, JsonObject source) + { + foreach (var property in source) + { + if (target.ContainsKey(property.Key)) + { + if (target[property.Key] is JsonObject targetObj && + property.Value is JsonObject sourceObj) + { + MergeObjects(targetObj, sourceObj); + } + else + { + target[property.Key] = property.Value?.DeepClone(); + } + } + else + { + target[property.Key] = property.Value?.DeepClone(); + } + } + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/MimeTypeUtil.cs b/EasyTool.Core/IOCategory/MimeTypeUtil.cs new file mode 100644 index 0000000..67ea4ea --- /dev/null +++ b/EasyTool.Core/IOCategory/MimeTypeUtil.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// MIME 类型工具类 + /// 提供根据文件扩展名和文件内容检测 MIME 类型的功能 + /// + public static class MimeTypeUtil + { + private static readonly Dictionary ExtensionToMimeType = new(StringComparer.OrdinalIgnoreCase) + { + // 文本 + {".txt", "text/plain"}, + {".html", "text/html"}, + {".htm", "text/html"}, + {".css", "text/css"}, + {".js", "application/javascript"}, + {".json", "application/json"}, + {".xml", "application/xml"}, + {".csv", "text/csv"}, + {".md", "text/markdown"}, + {".yaml", "text/yaml"}, + {".yml", "text/yaml"}, + + // 图片 + {".jpg", "image/jpeg"}, + {".jpeg", "image/jpeg"}, + {".png", "image/png"}, + {".gif", "image/gif"}, + {".bmp", "image/bmp"}, + {".ico", "image/x-icon"}, + {".svg", "image/svg+xml"}, + {".webp", "image/webp"}, + {".tiff", "image/tiff"}, + {".tif", "image/tiff"}, + + // 音频 + {".mp3", "audio/mpeg"}, + {".wav", "audio/wav"}, + {".ogg", "audio/ogg"}, + {".flac", "audio/flac"}, + {".aac", "audio/aac"}, + {".wma", "audio/x-ms-wma"}, + {".m4a", "audio/mp4"}, + + // 视频 + {".mp4", "video/mp4"}, + {".avi", "video/x-msvideo"}, + {".mkv", "video/x-matroska"}, + {".mov", "video/quicktime"}, + {".wmv", "video/x-ms-wmv"}, + {".flv", "video/x-flv"}, + {".webm", "video/webm"}, + {".m4v", "video/mp4"}, + + // 文档 + {".pdf", "application/pdf"}, + {".doc", "application/msword"}, + {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {".xls", "application/vnd.ms-excel"}, + {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {".ppt", "application/vnd.ms-powerpoint"}, + {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + + // 压缩 + {".zip", "application/zip"}, + {".rar", "application/x-rar-compressed"}, + {".7z", "application/x-7z-compressed"}, + {".tar", "application/x-tar"}, + {".gz", "application/gzip"}, + {".bz2", "application/x-bzip2"}, + + // 可执行 + {".exe", "application/x-msdownload"}, + {".msi", "application/x-msi"}, + {".dll", "application/x-msdownload"}, + {".so", "application/x-sharedlib"}, + {".dylib", "application/x-sharedlib"}, + {".jar", "application/java-archive"}, + {".apk", "application/vnd.android.package-archive"}, + + // 代码 + {".cs", "text/x-csharp"}, + {".java", "text/x-java-source"}, + {".py", "text/x-python"}, + {".rb", "text/x-ruby"}, + {".php", "text/x-php"}, + {".cpp", "text/x-c++src"}, + {".c", "text/x-csrc"}, + {".h", "text/x-chdr"}, + {".go", "text/x-go"}, + {".rs", "text/x-rust"}, + {".swift", "text/x-swift"}, + {".kt", "text/x-kotlin"}, + {".ts", "text/typescript"}, + {".tsx", "text/typescript-jsx"}, + + // 字体 + {".ttf", "font/ttf"}, + {".otf", "font/otf"}, + {".woff", "font/woff"}, + {".woff2", "font/woff2"}, + {".eot", "application/vnd.ms-fontobject"}, + + // 其他 + {".bin", "application/octet-stream"}, + {".dat", "application/octet-stream"}, + }; + + private static readonly Dictionary FileSignatures = new() + { + {"image/jpeg", new byte[] {0xFF, 0xD8, 0xFF}}, + {"image/png", new byte[] {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}}, + {"image/gif", new byte[] {0x47, 0x49, 0x46, 0x38}}, + {"image/bmp", new byte[] {0x42, 0x4D}}, + {"application/pdf", new byte[] {0x25, 0x50, 0x44, 0x46}}, + {"application/zip", new byte[] {0x50, 0x4B, 0x03, 0x04}}, + {"application/x-rar-compressed", new byte[] {0x52, 0x61, 0x72, 0x21}}, + {"application/x-7z-compressed", new byte[] {0x37, 0x7A, 0xBC, 0xAF}}, + {"video/mp4", new byte[] {0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70}}, + {"audio/mpeg", new byte[] {0xFF, 0xFB}}, + {"application/java-archive", new byte[] {0x50, 0x4B, 0x03, 0x04}}, + }; + + /// + /// 根据文件扩展名获取 MIME 类型 + /// + public static string GetByExtension(string extension) + { + if (string.IsNullOrEmpty(extension)) + return "application/octet-stream"; + + if (!extension.StartsWith(".")) + extension = "." + extension; + + return ExtensionToMimeType.TryGetValue(extension, out string mime) + ? mime + : "application/octet-stream"; + } + + /// + /// 根据文件路径获取 MIME 类型 + /// + public static string GetByPath(string filePath) + { + return GetByExtension(Path.GetExtension(filePath)); + } + + /// + /// 根据文件内容检测 MIME 类型 + /// + public static string DetectByContent(string filePath) + { + if (!File.Exists(filePath)) + return "application/octet-stream"; + + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + return DetectByContent(stream); + } + + /// + /// 根据流内容检测 MIME 类型 + /// + public static string DetectByContent(Stream stream) + { + byte[] header = new byte[16]; + int bytesRead = stream.Read(header, 0, header.Length); + + if (bytesRead == 0) + return "application/octet-stream"; + + foreach (var signature in FileSignatures) + { + if (header.Length >= signature.Value.Length) + { + bool match = true; + for (int i = 0; i < signature.Value.Length; i++) + { + if (header[i] != signature.Value[i]) + { + match = false; + break; + } + } + if (match) + return signature.Key; + } + } + + // 检查是否为文本 + if (IsTextContent(header, bytesRead)) + return "text/plain"; + + return "application/octet-stream"; + } + + private static bool IsTextContent(byte[] data, int length) + { + for (int i = 0; i < length; i++) + { + byte b = data[i]; + // 允许的控制字符:换行、回车、制表符 + if (b < 32 && b != 9 && b != 10 && b != 13) + return false; + } + return true; + } + + /// + /// 组合检测(先检测内容,再根据扩展名补充) + /// + public static string Detect(string filePath) + { + string byContent = DetectByContent(filePath); + if (byContent != "application/octet-stream") + return byContent; + + return GetByPath(filePath); + } + + /// + /// 根据 MIME 类型获取文件扩展名 + /// + public static string GetExtension(string mimeType) + { + if (string.IsNullOrEmpty(mimeType)) + return ".bin"; + + var entry = ExtensionToMimeType.FirstOrDefault(x => + x.Value.Equals(mimeType, StringComparison.OrdinalIgnoreCase)); + + return entry.Key ?? ".bin"; + } + + /// + /// 判断是否为图片 + /// + public static bool IsImage(string mimeType) + { + return mimeType?.StartsWith("image/") == true; + } + + /// + /// 判断是否为音频 + /// + public static bool IsAudio(string mimeType) + { + return mimeType?.StartsWith("audio/") == true; + } + + /// + /// 判断是否为视频 + /// + public static bool IsVideo(string mimeType) + { + return mimeType?.StartsWith("video/") == true; + } + + /// + /// 判断是否为文本 + /// + public static bool IsText(string mimeType) + { + return mimeType?.StartsWith("text/") == true || + mimeType == "application/json" || + mimeType == "application/xml" || + mimeType == "application/javascript"; + } + + /// + /// 注册自定义 MIME 类型 + /// + public static void Register(string extension, string mimeType) + { + if (!extension.StartsWith(".")) + extension = "." + extension; + + ExtensionToMimeType[extension] = mimeType; + } + } +} diff --git a/EasyTool.Core/IOCategory/PathUtil.cs b/EasyTool.Core/IOCategory/PathUtil.cs new file mode 100644 index 0000000..d899c72 --- /dev/null +++ b/EasyTool.Core/IOCategory/PathUtil.cs @@ -0,0 +1,346 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 路径工具类 + /// 提供路径操作的增强功能 + /// + public static class PathUtil + { + /// + /// 获取相对路径 + /// + public static string GetRelativePath(string basePath, string targetPath) + { + if (string.IsNullOrEmpty(basePath)) + throw new ArgumentException("Base path cannot be null or empty", nameof(basePath)); + if (string.IsNullOrEmpty(targetPath)) + throw new ArgumentException("Target path cannot be null or empty", nameof(targetPath)); + + // 规范化路径 + basePath = Normalize(basePath); + targetPath = Normalize(targetPath); + + // 确保基础路径以分隔符结尾 + if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) && + !basePath.EndsWith(Path.AltDirectorySeparatorChar.ToString())) + { + basePath += Path.DirectorySeparatorChar; + } + + var baseParts = basePath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + var targetParts = targetPath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + + // 处理 Windows 盘符 + if (baseParts.Length > 0 && targetParts.Length > 0) + { + string baseRoot = GetRoot(basePath); + string targetRoot = GetRoot(targetPath); + if (!string.IsNullOrEmpty(baseRoot) && !string.IsNullOrEmpty(targetRoot) && + !baseRoot.Equals(targetRoot, StringComparison.OrdinalIgnoreCase)) + { + return targetPath; // 不同盘符,返回绝对路径 + } + } + + // 找到公共前缀 + int commonLength = 0; + int minLen = Math.Min(baseParts.Length, targetParts.Length); + while (commonLength < minLen && + baseParts[commonLength].Equals(targetParts[commonLength], StringComparison.OrdinalIgnoreCase)) + { + commonLength++; + } + + // 构建相对路径 + var result = new StringBuilder(); + + // 添加向上回溯 + for (int i = commonLength; i < baseParts.Length - (basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? 0 : 1); i++) + { + if (result.Length > 0) + result.Append(Path.DirectorySeparatorChar); + result.Append(".."); + } + + // 添加目标路径的剩余部分 + for (int i = commonLength; i < targetParts.Length; i++) + { + if (result.Length > 0) + result.Append(Path.DirectorySeparatorChar); + result.Append(targetParts[i]); + } + + return result.Length == 0 ? "." : result.ToString(); + } + + private static string GetRoot(string path) + { + if (path.Length >= 2 && path[1] == ':') + return path.Substring(0, 2).ToUpperInvariant(); + if (path.StartsWith("/") || path.StartsWith("\\")) + return path[0].ToString(); + return ""; + } + + /// + /// 规范化路径(统一分隔符,移除多余的点和分隔符) + /// + public static string Normalize(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + // 替换分隔符 + path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + + // 处理 ./ 和 ../ + var parts = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None); + var result = new List(); + + foreach (var part in parts) + { + if (part == ".") + continue; + else if (part == "..") + { + if (result.Count > 0 && result[result.Count - 1] != "..") + result.RemoveAt(result.Count - 1); + else if (!IsAbsolute(path)) + result.Add(".."); + } + else + { + result.Add(part); + } + } + + string normalized = string.Join(Path.DirectorySeparatorChar.ToString(), result); + + // 处理根路径 + if (path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !normalized.StartsWith(Path.DirectorySeparatorChar.ToString())) + { + if (path.Length >= 2 && path[1] == ':') + normalized = path.Substring(0, 2) + Path.DirectorySeparatorChar + normalized; + else + normalized = Path.DirectorySeparatorChar + normalized; + } + + return normalized; + } + + /// + /// 判断是否为绝对路径 + /// + public static bool IsAbsolute(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + // Windows: C:\ 或 \ + // Unix: / + return Path.IsPathRooted(path) || + (path.Length >= 2 && path[1] == ':') || + path.StartsWith("/") || + path.StartsWith("\\\\"); + } + + /// + /// 获取文件扩展名(不带点) + /// + public static string GetExtensionWithoutDot(string path) + { + string ext = Path.GetExtension(path); + return string.IsNullOrEmpty(ext) ? "" : ext.Substring(1); + } + + /// + /// 更改文件扩展名 + /// + public static string ChangeExtension(string path, string newExtension) + { + if (string.IsNullOrEmpty(path)) + return path; + + newExtension = newExtension?.StartsWith(".") == true ? newExtension : "." + newExtension; + return Path.ChangeExtension(path, newExtension); + } + + /// + /// 获取文件名(不带扩展名) + /// + public static string GetFileNameWithoutExtension(string path) + { + return Path.GetFileNameWithoutExtension(path); + } + + /// + /// 获取父目录路径 + /// + public static string GetParent(string path) + { + return Path.GetDirectoryName(path); + } + + /// + /// 获取所有父目录路径 + /// + public static List GetParents(string path) + { + var parents = new List(); + string current = path; + + while (!string.IsNullOrEmpty(current)) + { + string parent = Path.GetDirectoryName(current); + if (string.IsNullOrEmpty(parent)) + break; + parents.Add(parent); + current = parent; + } + + return parents; + } + + /// + /// 连接路径片段 + /// + public static string Combine(params string[] paths) + { + return Path.Combine(paths); + } + + /// + /// 获取临时文件路径 + /// + public static string GetTempFilePath(string extension = null) + { + string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + if (!string.IsNullOrEmpty(extension)) + { + extension = extension.StartsWith(".") ? extension : "." + extension; + path += extension; + } + return path; + } + + /// + /// 获取临时目录路径 + /// + public static string GetTempDirectoryPath() + { + return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + } + + /// + /// 确保目录存在 + /// + public static string EnsureDirectoryExists(string path) + { + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + return path; + } + + /// + /// 确保文件所在目录存在 + /// + public static string EnsureParentDirectoryExists(string filePath) + { + string dir = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + return filePath; + } + + /// + /// 获取唯一文件名(如果文件存在则添加序号) + /// + public static string GetUniqueFilePath(string basePath) + { + if (!File.Exists(basePath)) + return basePath; + + string dir = Path.GetDirectoryName(basePath); + string name = Path.GetFileNameWithoutExtension(basePath); + string ext = Path.GetExtension(basePath); + + int count = 1; + string newPath; + do + { + newPath = Path.Combine(dir ?? "", $"{name} ({count}){ext}"); + count++; + } + while (File.Exists(newPath)); + + return newPath; + } + + /// + /// 获取唯一目录名 + /// + public static string GetUniqueDirectoryPath(string basePath) + { + if (!Directory.Exists(basePath)) + return basePath; + + int count = 1; + string newPath; + do + { + newPath = $"{basePath} ({count})"; + count++; + } + while (Directory.Exists(newPath)); + + return newPath; + } + + /// + /// 获取路径深度 + /// + public static int GetDepth(string path) + { + if (string.IsNullOrEmpty(path)) + return 0; + + path = Normalize(path); + return path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Length; + } + + /// + /// 路径是否在指定目录下 + /// + public static bool IsInDirectory(string path, string directory) + { + string normalizedPath = Normalize(Path.GetFullPath(path)); + string normalizedDir = Normalize(Path.GetFullPath(directory)); + + return normalizedPath.StartsWith(normalizedDir, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 获取路径层级(相对于基础路径) + /// + public static string GetPathLevel(string basePath, string targetPath, int level) + { + string relative = GetRelativePath(basePath, targetPath); + var parts = relative.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + + if (level < 0 || level >= parts.Length) + return null; + + return parts[level]; + } + } +} diff --git a/EasyTool.Core/IOCategory/PropertiesUtil.cs b/EasyTool.Core/IOCategory/PropertiesUtil.cs new file mode 100644 index 0000000..0f83432 --- /dev/null +++ b/EasyTool.Core/IOCategory/PropertiesUtil.cs @@ -0,0 +1,630 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// Properties 配置文件工具类 + /// 用于读写 Java 风格的 .properties 配置文件 + /// + public static class PropertiesUtil + { + #region 读取方法 + + /// + /// 从文件加载 Properties + /// + /// 文件路径 + /// 编码方式(默认UTF-8) + /// Properties 字典 + public static Dictionary Load(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("Properties 文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + return ParseLines(lines); + } + + /// + /// 从文件异步加载 Properties + /// + /// 文件路径 + /// 编码方式 + /// Properties 字典 + public static async System.Threading.Tasks.Task> LoadAsync(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("Properties 文件不存在", filePath); + + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(filePath, encoding); + var content = await reader.ReadToEndAsync(); + var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + return ParseLines(lines); + } + + /// + /// 从字符串加载 Properties + /// + /// Properties 内容 + /// Properties 字典 + public static Dictionary Parse(string content) + { + if (string.IsNullOrEmpty(content)) + return new Dictionary(); + + var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + return ParseLines(lines); + } + + /// + /// 从流加载 Properties + /// + /// 输入流 + /// 编码方式 + /// Properties 字典 + public static Dictionary LoadFromStream(Stream stream, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(stream, encoding); + var content = reader.ReadToEnd(); + return Parse(content); + } + + private static Dictionary ParseLines(string[] lines) + { + var properties = new Dictionary(); + int lineNumber = 0; + + foreach (var originalLine in lines) + { + lineNumber++; + string line = originalLine.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#") || line.StartsWith("!")) + continue; + + // 查找分隔符 + int separatorIndex = FindSeparator(line); + if (separatorIndex < 0) + continue; + + string key = UnescapeKey(line.Substring(0, separatorIndex).Trim()); + string value = separatorIndex < line.Length - 1 + ? UnescapeValue(line.Substring(separatorIndex + 1).TrimStart()) + : string.Empty; + + properties[key] = value; + } + + return properties; + } + + private static int FindSeparator(string line) + { + for (int i = 0; i < line.Length; i++) + { + char c = line[i]; + if (c == '=' || c == ':' || char.IsWhiteSpace(c)) + { + // 检查是否被转义 + if (i > 0 && line[i - 1] == '\\') + continue; + return i; + } + } + return -1; + } + + #endregion + + #region 保存方法 + + /// + /// 保存 Properties 到文件 + /// + /// 文件路径 + /// Properties 字典 + /// 编码方式 + /// 注释(可选) + public static void Save(string filePath, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + File.WriteAllText(filePath, content, encoding); + } + + /// + /// 异步保存 Properties 到文件 + /// + /// 文件路径 + /// Properties 字典 + /// 编码方式 + /// 注释 + public static async System.Threading.Tasks.Task SaveAsync(string filePath, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + using var writer = new StreamWriter(filePath, false, encoding); + await writer.WriteAsync(content); + } + + /// + /// 保存 Properties 到流 + /// + /// 输出流 + /// Properties 字典 + /// 编码方式 + /// 注释 + public static void SaveToStream(Stream stream, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + using var writer = new StreamWriter(stream, encoding); + writer.Write(content); + } + + /// + /// 将 Properties 转换为字符串 + /// + /// Properties 字典 + /// 注释 + /// Properties 格式字符串 + public static string ToString(Dictionary properties, string? comment = null) + { + return BuildContent(properties, comment); + } + + private static string BuildContent(Dictionary properties, string? comment) + { + var sb = new StringBuilder(); + + // 添加注释 + if (!string.IsNullOrEmpty(comment)) + { + sb.AppendLine("# " + comment.Replace("\n", "\n# ")); + sb.AppendLine(); + } + + // 添加时间戳 + sb.AppendLine($"# {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); + sb.AppendLine(); + + foreach (var kvp in properties) + { + sb.AppendLine($"{EscapeKey(kvp.Key)}={EscapeValue(kvp.Value)}"); + } + + return sb.ToString(); + } + + #endregion + + #region 单值操作 + + /// + /// 获取属性值 + /// + /// 文件路径 + /// 键 + /// 默认值 + /// 编码方式 + /// 属性值 + public static string Get(string filePath, string key, string defaultValue = "", Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + return properties.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 设置属性值 + /// + /// 文件路径 + /// 键 + /// 值 + /// 编码方式 + public static void Set(string filePath, string key, string value, Encoding? encoding = null) + { + var properties = File.Exists(filePath) ? Load(filePath, encoding) : new Dictionary(); + properties[key] = value; + Save(filePath, properties, encoding); + } + + /// + /// 删除属性 + /// + /// 文件路径 + /// 键 + /// 编码方式 + /// 是否删除成功 + public static bool Remove(string filePath, string key, Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + if (properties.Remove(key)) + { + Save(filePath, properties, encoding); + return true; + } + return false; + } + + /// + /// 检查属性是否存在 + /// + /// 文件路径 + /// 键 + /// 编码方式 + /// 是否存在 + public static bool ContainsKey(string filePath, string key, Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + return properties.ContainsKey(key); + } + + #endregion + + #region 类型转换获取 + + /// + /// 获取整数值 + /// + public static int GetInt(string filePath, string key, int defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !int.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取长整数值 + /// + public static long GetLong(string filePath, string key, long defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !long.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取双精度浮点值 + /// + public static double GetDouble(string filePath, string key, double defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !double.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取布尔值 + /// + public static bool GetBool(string filePath, string key, bool defaultValue = false, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null) + return defaultValue; + + return value.ToLower() switch + { + "true" or "yes" or "1" or "on" => true, + "false" or "no" or "0" or "off" => false, + _ => defaultValue + }; + } + + /// + /// 获取日期时间值 + /// + public static DateTime GetDateTime(string filePath, string key, DateTime defaultValue = default, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !DateTime.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取枚举值 + /// + public static T GetEnum(string filePath, string key, T defaultValue = default, Encoding? encoding = null) where T : struct, Enum + { + var value = Get(filePath, key, null, encoding); + if (value == null || !Enum.TryParse(value, true, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取字符串列表(逗号分隔) + /// + public static List GetList(string filePath, string key, Encoding? encoding = null) + { + var value = Get(filePath, key, "", encoding); + if (string.IsNullOrEmpty(value)) + return new List(); + + return value.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Where(s => !string.IsNullOrEmpty(s)) + .ToList(); + } + + #endregion + + #region 转义处理 + + private static string EscapeKey(string key) + { + var sb = new StringBuilder(); + foreach (char c in key) + { + switch (c) + { + case '=': sb.Append("\\="); break; + case ':': sb.Append("\\:"); break; + case ' ': sb.Append("\\ "); break; + case '\t': sb.Append("\\t"); break; + case '\n': sb.Append("\\n"); break; + case '\r': sb.Append("\\r"); break; + case '\\': sb.Append("\\\\"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + private static string EscapeValue(string value) + { + var sb = new StringBuilder(); + foreach (char c in value) + { + switch (c) + { + case '\n': sb.Append("\\n"); break; + case '\r': sb.Append("\\r"); break; + case '\t': sb.Append("\\t"); break; + case '\\': sb.Append("\\\\"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + private static string UnescapeKey(string key) + { + return Unescape(key); + } + + private static string UnescapeValue(string value) + { + return Unescape(value); + } + + private static string Unescape(string s) + { + var sb = new StringBuilder(); + bool escape = false; + + foreach (char c in s) + { + if (escape) + { + switch (c) + { + case 'n': sb.Append('\n'); break; + case 'r': sb.Append('\r'); break; + case 't': sb.Append('\t'); break; + case '\\': sb.Append('\\'); break; + case '=': sb.Append('='); break; + case ':': sb.Append(':'); break; + case ' ': sb.Append(' '); break; + default: sb.Append(c); break; + } + escape = false; + } + else if (c == '\\') + { + escape = true; + } + else + { + sb.Append(c); + } + } + + // 处理结尾的转义符 + if (escape) + sb.Append('\\'); + + return sb.ToString(); + } + + #endregion + + #region PropertiesDocument 类 + + /// + /// 创建可操作的 Properties 文档对象 + /// + /// 文件路径(可选) + /// PropertiesDocument 对象 + public static PropertiesDocument CreateDocument(string? filePath = null) + { + if (filePath != null && File.Exists(filePath)) + { + var properties = Load(filePath); + return new PropertiesDocument(filePath, properties); + } + return new PropertiesDocument(filePath, new Dictionary()); + } + + #endregion + } + + /// + /// 可操作的 Properties 文档对象 + /// + public class PropertiesDocument + { + private readonly string? _filePath; + private readonly Dictionary _properties; + private readonly List _comments; + private bool _modified; + + /// + /// 属性数量 + /// + public int Count => _properties.Count; + + /// + /// 是否已修改 + /// + public bool IsModified => _modified; + + /// + /// 所有键 + /// + public IEnumerable Keys => _properties.Keys; + + /// + /// 所有值 + /// + public IEnumerable Values => _properties.Values; + + /// + /// 获取或设置属性值 + /// + /// 键 + /// + public string this[string key] + { + get => _properties.TryGetValue(key, out var value) ? value : string.Empty; + set + { + _properties[key] = value; + _modified = true; + } + } + + internal PropertiesDocument(string? filePath, Dictionary properties) + { + _filePath = filePath; + _properties = properties; + _comments = new List(); + _modified = false; + } + + /// + /// 获取属性值 + /// + public string Get(string key, string defaultValue = "") + { + return _properties.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 设置属性值 + /// + public void Set(string key, string value) + { + _properties[key] = value; + _modified = true; + } + + /// + /// 移除属性 + /// + public bool Remove(string key) + { + if (_properties.Remove(key)) + { + _modified = true; + return true; + } + return false; + } + + /// + /// 检查是否包含键 + /// + public bool ContainsKey(string key) + { + return _properties.ContainsKey(key); + } + + /// + /// 添加注释 + /// + public void AddComment(string comment) + { + _comments.Add(comment); + } + + /// + /// 保存到原文件 + /// + public void Save() + { + if (_filePath == null) + throw new InvalidOperationException("未指定文件路径"); + + PropertiesUtil.Save(_filePath, _properties, null, string.Join("\n", _comments)); + _modified = false; + } + + /// + /// 保存到指定文件 + /// + public void Save(string filePath) + { + PropertiesUtil.Save(filePath, _properties, null, string.Join("\n", _comments)); + _modified = false; + } + + /// + /// 重新加载文件 + /// + public void Reload() + { + if (_filePath == null || !File.Exists(_filePath)) + return; + + var newProperties = PropertiesUtil.Load(_filePath); + _properties.Clear(); + foreach (var kvp in newProperties) + { + _properties[kvp.Key] = kvp.Value; + } + _modified = false; + } + + /// + /// 转换为字典 + /// + public Dictionary ToDictionary() + { + return new Dictionary(_properties); + } + + /// + /// 批量设置属性 + /// + public void SetRange(Dictionary properties) + { + foreach (var kvp in properties) + { + _properties[kvp.Key] = kvp.Value; + } + _modified = true; + } + } +} diff --git a/EasyTool.Core/IOCategory/TomlUtil.cs b/EasyTool.Core/IOCategory/TomlUtil.cs new file mode 100644 index 0000000..7c12ba2 --- /dev/null +++ b/EasyTool.Core/IOCategory/TomlUtil.cs @@ -0,0 +1,438 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.IOCategory +{ + /// + /// TOML 工具类 + /// 提供 TOML 配置文件的读写功能 + /// + public static class TomlUtil + { + /// + /// 将对象序列化为 TOML 字符串 + /// + public static string Serialize(object obj) + { + var serializer = new TomlSerializer(); + return serializer.Serialize(obj); + } + + /// + /// 将 TOML 字符串反序列化为字典 + /// + public static Dictionary Deserialize(string toml) + { + var deserializer = new TomlDeserializer(); + return deserializer.Deserialize(toml); + } + + /// + /// 从文件读取 TOML + /// + public static Dictionary ReadFile(string filePath) + { + var content = File.ReadAllText(filePath, Encoding.UTF8); + return Deserialize(content); + } + + /// + /// 将对象写入 TOML 文件 + /// + public static void WriteFile(string filePath, object obj) + { + var toml = Serialize(obj); + File.WriteAllText(filePath, toml, Encoding.UTF8); + } + } + + /// + /// TOML 序列化器 + /// + public class TomlSerializer + { + private readonly StringBuilder _sb; + + /// + /// 创建 TOML 序列化器 + /// + public TomlSerializer() + { + _sb = new StringBuilder(); + } + + /// + /// 序列化对象 + /// + public string Serialize(object obj) + { + _sb.Clear(); + SerializeValue(obj, ""); + return _sb.ToString(); + } + + private void SerializeValue(object value, string prefix) + { + if (value == null) + return; + + var type = value.GetType(); + + if (value is IDictionary dict) + { + SerializeDictionary(new Dictionary(dict), prefix); + } + else if (value is IList list) + { + SerializeArray(new List(list), prefix); + } + else if (type.IsPrimitive || value is string || value is decimal || value is DateTime) + { + // 简单值不单独序列化 + } + else + { + // 复杂对象,反射属性 + var props = type.GetProperties(); + var objDict = new Dictionary(); + foreach (var prop in props) + { + if (prop.CanRead) + { + objDict[prop.Name] = prop.GetValue(value); + } + } + SerializeDictionary(objDict, prefix); + } + } + + private void SerializeDictionary(Dictionary dict, string prefix) + { + var simpleValues = new List>(); + var complexValues = new List>(); + + foreach (var kvp in dict) + { + if (IsSimpleValue(kvp.Value)) + simpleValues.Add(kvp); + else + complexValues.Add(kvp); + } + + // 先输出简单值 + if (!string.IsNullOrEmpty(prefix) && simpleValues.Count > 0) + { + _sb.AppendLine($"[{prefix}]"); + } + + foreach (var kvp in simpleValues) + { + _sb.AppendLine($"{kvp.Key} = {FormatValue(kvp.Value)}"); + } + + if (simpleValues.Count > 0 && complexValues.Count > 0) + _sb.AppendLine(); + + // 处理复杂值 + foreach (var kvp in complexValues) + { + string newPrefix = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}.{kvp.Key}"; + SerializeValue(kvp.Value, newPrefix); + } + } + + private void SerializeArray(List list, string prefix) + { + if (!string.IsNullOrEmpty(prefix)) + { + _sb.AppendLine($"[[{prefix}]]"); + } + + foreach (var item in list) + { + if (IsSimpleValue(item)) + { + _sb.AppendLine(FormatValue(item)); + } + else if (item is Dictionary dict) + { + foreach (var kvp in dict) + { + _sb.AppendLine($"{kvp.Key} = {FormatValue(kvp.Value)}"); + } + _sb.AppendLine(); + } + } + } + + private static bool IsSimpleValue(object value) + { + if (value == null) return true; + var type = value.GetType(); + return type.IsPrimitive || value is string || value is decimal || value is DateTime; + } + + private static string FormatValue(object value) + { + if (value == null) return "\"\""; + + if (value is string str) + { + if (str.Contains("\n")) + return $"\"\"\"\n{str}\n\"\"\""; + if (str.Contains("\"") || str.Contains("'") || str.Contains("\\")) + return $"\"{str.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""; + return $"\"{str}\""; + } + + if (value is bool b) return b ? "true" : "false"; + if (value is DateTime dt) return dt.ToString("yyyy-MM-ddTHH:mm:ssZ"); + if (value is double d) return d.ToString(System.Globalization.CultureInfo.InvariantCulture); + if (value is float f) return f.ToString(System.Globalization.CultureInfo.InvariantCulture); + + return value.ToString(); + } + } + + /// + /// TOML 反序列化器 + /// + public class TomlDeserializer + { + /// + /// 反序列化 TOML 字符串 + /// + public Dictionary Deserialize(string toml) + { + var result = new Dictionary(); + var lines = toml.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + + string currentSection = ""; + var currentDict = result; + + for (int i = 0; i < lines.Length; i++) + { + string line = lines[i].Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#")) + continue; + + // 表头 + if (line.StartsWith("[") && line.EndsWith("]")) + { + string sectionName = line.Substring(1, line.Length - 2).Trim(); + if (sectionName.StartsWith("[[") && sectionName.EndsWith("]]")) + { + // 数组表 + sectionName = sectionName.Substring(2, sectionName.Length - 4).Trim(); + currentSection = sectionName; + var list = GetOrCreateArray(result, sectionName); + currentDict = new Dictionary(); + list.Add(currentDict); + } + else + { + currentSection = sectionName; + currentDict = GetOrCreateDictionary(result, sectionName); + } + continue; + } + + // 键值对 + int equalIndex = line.IndexOf('='); + if (equalIndex > 0) + { + string key = line.Substring(0, equalIndex).Trim(); + string valueStr = line.Substring(equalIndex + 1).Trim(); + + // 处理行内注释 + int commentIndex = valueStr.IndexOf(" #"); + if (commentIndex > 0) + { + valueStr = valueStr.Substring(0, commentIndex).Trim(); + } + + object value = ParseValue(valueStr, lines, ref i); + currentDict[key] = value; + } + } + + return result; + } + + private static List> GetOrCreateArray(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + for (int i = 0; i < parts.Length - 1; i++) + { + if (!current.TryGetValue(parts[i], out var obj) || !(obj is Dictionary dict)) + { + dict = new Dictionary(); + current[parts[i]] = dict; + } + current = dict; + } + + string lastKey = parts[parts.Length - 1]; + if (!current.TryGetValue(lastKey, out var listObj) || !(listObj is List> list)) + { + list = new List>(); + current[lastKey] = list; + } + + return list; + } + + private static Dictionary GetOrCreateDictionary(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + foreach (var part in parts) + { + if (!current.TryGetValue(part, out var obj) || !(obj is Dictionary dict)) + { + dict = new Dictionary(); + current[part] = dict; + } + current = dict; + } + + return current; + } + + private static object ParseValue(string valueStr, string[] lines, ref int lineIndex) + { + // 布尔值 + if (valueStr == "true") return true; + if (valueStr == "false") return false; + + // 数字 + if (int.TryParse(valueStr, out int intVal)) return intVal; + if (long.TryParse(valueStr, out long longVal)) return longVal; + if (double.TryParse(valueStr, out double doubleVal)) return doubleVal; + + // 字符串 + if (valueStr.StartsWith("\"\"\"")) + { + // 多行字符串 + var sb = new StringBuilder(); + lineIndex++; + while (lineIndex < lines.Length && !lines[lineIndex].Trim().EndsWith("\"\"\"")) + { + sb.AppendLine(lines[lineIndex]); + lineIndex++; + } + if (lineIndex < lines.Length) + { + string lastLine = lines[lineIndex].Trim(); + sb.Append(lastLine.Substring(0, lastLine.Length - 3)); + } + return sb.ToString(); + } + + if (valueStr.StartsWith("\"") && valueStr.EndsWith("\"")) + { + return valueStr.Substring(1, valueStr.Length - 2) + .Replace("\\\"", "\"") + .Replace("\\\\", "\\") + .Replace("\\n", "\n") + .Replace("\\t", "\t"); + } + + if (valueStr.StartsWith("'") && valueStr.EndsWith("'")) + { + return valueStr.Substring(1, valueStr.Length - 2); + } + + // 日期时间 + if (DateTime.TryParse(valueStr, out DateTime dt)) return dt; + + // 数组 + if (valueStr.StartsWith("[") && valueStr.EndsWith("]")) + { + return ParseArray(valueStr); + } + + // 内联表 + if (valueStr.StartsWith("{") && valueStr.EndsWith("}")) + { + return ParseInlineTable(valueStr); + } + + return valueStr; + } + + private static List ParseArray(string valueStr) + { + var result = new List(); + string inner = valueStr.Substring(1, valueStr.Length - 2).Trim(); + + if (string.IsNullOrEmpty(inner)) + return result; + + // 简单分割(不支持嵌套) + var parts = inner.Split(','); + foreach (var part in parts) + { + string item = part.Trim(); + if (!string.IsNullOrEmpty(item)) + { + if (item.StartsWith("\"") && item.EndsWith("\"")) + result.Add(item.Substring(1, item.Length - 2)); + else if (int.TryParse(item, out int intVal)) + result.Add(intVal); + else if (double.TryParse(item, out double doubleVal)) + result.Add(doubleVal); + else if (item == "true") + result.Add(true); + else if (item == "false") + result.Add(false); + else + result.Add(item); + } + } + + return result; + } + + private static Dictionary ParseInlineTable(string valueStr) + { + var result = new Dictionary(); + string inner = valueStr.Substring(1, valueStr.Length - 2).Trim(); + + if (string.IsNullOrEmpty(inner)) + return result; + + var parts = inner.Split(','); + foreach (var part in parts) + { + int equalIndex = part.IndexOf('='); + if (equalIndex > 0) + { + string key = part.Substring(0, equalIndex).Trim(); + string value = part.Substring(equalIndex + 1).Trim(); + + if (value.StartsWith("\"") && value.EndsWith("\"")) + result[key] = value.Substring(1, value.Length - 2); + else if (int.TryParse(value, out int intVal)) + result[key] = intVal; + else if (value == "true") + result[key] = true; + else if (value == "false") + result[key] = false; + else + result[key] = value; + } + } + + return result; + } + } +} diff --git a/EasyTool.Core/IOCategory/YamlUtil.cs b/EasyTool.Core/IOCategory/YamlUtil.cs new file mode 100644 index 0000000..a00dc46 --- /dev/null +++ b/EasyTool.Core/IOCategory/YamlUtil.cs @@ -0,0 +1,369 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.IOCategory +{ + /// + /// YAML 工具类 + /// 提供简单的 YAML 序列化和反序列化功能 + /// + public static class YamlUtil + { + /// + /// 将对象序列化为 YAML 字符串 + /// + public static string Serialize(object obj, int indentSize = 2) + { + var serializer = new YamlSerializer(indentSize); + return serializer.Serialize(obj); + } + + /// + /// 将 YAML 字符串反序列化为字典 + /// + public static Dictionary Deserialize(string yaml) + { + var deserializer = new YamlDeserializer(); + return deserializer.Deserialize(yaml); + } + + /// + /// 从文件读取 YAML + /// + public static Dictionary ReadFile(string filePath) + { + var content = File.ReadAllText(filePath, Encoding.UTF8); + return Deserialize(content); + } + + /// + /// 将对象写入 YAML 文件 + /// + public static void WriteFile(string filePath, object obj, int indentSize = 2) + { + var yaml = Serialize(obj, indentSize); + File.WriteAllText(filePath, yaml, Encoding.UTF8); + } + + /// + /// 将 YAML 字符串反序列化为指定类型 + /// + public static T Deserialize(string yaml) where T : new() + { + var dict = Deserialize(yaml); + return MapToObject(dict); + } + + private static T MapToObject(Dictionary dict) where T : new() + { + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var property = type.GetProperty(kvp.Key); + if (property != null && property.CanWrite) + { + var value = ConvertValue(kvp.Value, property.PropertyType); + if (value != null) + property.SetValue(obj, value); + } + } + + return obj; + } + + private static object ConvertValue(object value, Type targetType) + { + if (value == null) return null; + + if (targetType == typeof(string)) + return value.ToString(); + + if (targetType == typeof(int)) + return Convert.ToInt32(value); + + if (targetType == typeof(long)) + return Convert.ToInt64(value); + + if (targetType == typeof(double)) + return Convert.ToDouble(value); + + if (targetType == typeof(bool)) + return Convert.ToBoolean(value); + + if (targetType == typeof(DateTime)) + return Convert.ToDateTime(value); + + return value; + } + } + + /// + /// YAML 序列化器 + /// + public class YamlSerializer + { + private readonly int _indentSize; + private readonly StringBuilder _sb; + + /// + /// 创建 YAML 序列化器 + /// + public YamlSerializer(int indentSize = 2) + { + _indentSize = indentSize; + _sb = new StringBuilder(); + } + + /// + /// 序列化对象 + /// + public string Serialize(object obj) + { + _sb.Clear(); + SerializeValue(obj, 0, ""); + return _sb.ToString(); + } + + private void SerializeValue(object value, int indent, string key) + { + string indentStr = new string(' ', indent); + + if (value == null) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}: null"); + return; + } + + var type = value.GetType(); + + if (value is IDictionary dict) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + else if (indent > 0) + _sb.AppendLine($"{indentStr}:"); + + foreach (var kvp in dict) + { + SerializeValue(kvp.Value, indent + _indentSize, kvp.Key); + } + } + else if (value is IList list) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + else if (indent > 0) + _sb.AppendLine($"{indentStr}:"); + + foreach (var item in list) + { + SerializeValue(item, indent + _indentSize, "-"); + } + } + else if (type.IsPrimitive || value is string || value is DateTime || value is decimal) + { + string valueStr = FormatScalar(value); + if (!string.IsNullOrEmpty(key)) + { + if (key == "-") + _sb.AppendLine($"{indentStr}- {valueStr}"); + else + _sb.AppendLine($"{indentStr}{key}: {valueStr}"); + } + } + else + { + // 复杂对象,反射属性 + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + + var properties = type.GetProperties(); + foreach (var prop in properties) + { + if (prop.CanRead) + { + var propValue = prop.GetValue(value); + SerializeValue(propValue, indent + _indentSize, prop.Name); + } + } + } + } + + private static string FormatScalar(object value) + { + if (value == null) return "null"; + if (value is string str) + { + if (string.IsNullOrEmpty(str)) return "\"\""; + if (str.Contains(":") || str.Contains("#") || str.Contains("\n") || str.StartsWith(" ") || str.EndsWith(" ")) + return $"\"{str.Replace("\"", "\\\"")}\""; + return str; + } + if (value is bool b) return b ? "true" : "false"; + if (value is DateTime dt) return dt.ToString("yyyy-MM-dd HH:mm:ss"); + + return value.ToString(); + } + } + + /// + /// YAML 反序列化器 + /// + public class YamlDeserializer + { + /// + /// 反序列化 YAML 字符串 + /// + public Dictionary Deserialize(string yaml) + { + var lines = yaml.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + var result = new Dictionary(); + var context = new ParseContext { Lines = lines, Index = 0 }; + int currentIndent = 0; + + ParseBlock(context, result, currentIndent); + + return result; + } + + private class ParseContext + { + public string[] Lines { get; set; } + public int Index { get; set; } + public int LineCount => Lines.Length; + public string CurrentLine => Index < LineCount ? Lines[Index] : null; + } + + private void ParseBlock(ParseContext context, Dictionary result, int baseIndent) + { + while (context.Index < context.LineCount) + { + string line = context.CurrentLine; + + // 跳过空行和注释 + if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) + { + context.Index++; + continue; + } + + int indent = GetIndent(line); + if (indent < baseIndent) break; + + string trimmed = line.TrimStart(); + + // 列表项 + if (trimmed.StartsWith("- ")) + { + var list = new List(); + while (context.Index < context.LineCount) + { + string itemLine = context.CurrentLine; + if (string.IsNullOrWhiteSpace(itemLine) || itemLine.TrimStart().StartsWith("#")) + { + context.Index++; + continue; + } + + int itemIndent = GetIndent(itemLine); + if (itemIndent < indent) break; + if (itemIndent > indent) + { + // 嵌套块 + context.Index--; + break; + } + + string itemTrimmed = itemLine.TrimStart(); + if (!itemTrimmed.StartsWith("- ")) break; + + string itemContent = itemTrimmed.Substring(2).Trim(); + if (itemContent.Contains(":")) + { + // 列表项是字典 + var itemDict = new Dictionary(); + context.Index++; + ParseBlock(context, itemDict, context.Index < context.LineCount ? GetIndent(context.CurrentLine) : indent + 2); + list.Add(itemDict); + } + else + { + list.Add(ParseScalar(itemContent)); + context.Index++; + } + } + + result["__list__"] = list; + continue; + } + + // 键值对 + int colonIndex = trimmed.IndexOf(':'); + if (colonIndex > 0) + { + string key = trimmed.Substring(0, colonIndex).Trim(); + string valueStr = trimmed.Substring(colonIndex + 1).Trim(); + + if (string.IsNullOrEmpty(valueStr)) + { + // 嵌套块 + context.Index++; + var nested = new Dictionary(); + ParseBlock(context, nested, indent + 2); + result[key] = nested; + } + else + { + result[key] = ParseScalar(valueStr); + context.Index++; + } + } + else + { + context.Index++; + } + } + } + + private static int GetIndent(string line) + { + int indent = 0; + foreach (char c in line) + { + if (c == ' ') indent++; + else if (c == '\t') indent += 2; + else break; + } + return indent; + } + + private static object ParseScalar(string value) + { + if (string.IsNullOrEmpty(value)) return null; + if (value == "null" || value == "~") return null; + if (value == "true") return true; + if (value == "false") return false; + + // 移除引号 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + return value.Substring(1, value.Length - 2); + } + + // 尝试解析数字 + if (int.TryParse(value, out int intVal)) return intVal; + if (long.TryParse(value, out long longVal)) return longVal; + if (double.TryParse(value, out double doubleVal)) return doubleVal; + if (DateTime.TryParse(value, out DateTime dateVal)) return dateVal; + + return value; + } + } +} diff --git a/EasyTool.Core/MathCategory/DistanceUtil.cs b/EasyTool.Core/MathCategory/DistanceUtil.cs new file mode 100644 index 0000000..f59e99a --- /dev/null +++ b/EasyTool.Core/MathCategory/DistanceUtil.cs @@ -0,0 +1,369 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 距离计算工具类 + /// 提供基于经纬度的距离计算、地理编码等功能 + /// + public static class DistanceUtil + { + /// + /// 地球半径(千米) + /// + public const double EarthRadiusKm = 6371.0; + + /// + /// 地球半径(米) + /// + public const double EarthRadiusM = 6371000.0; + + /// + /// 地球半径(英里) + /// + public const double EarthRadiusMile = 3958.8; + + #region Haversine 距离计算 + + /// + /// 使用 Haversine 公式计算两个坐标之间的球面距离 + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 距离(千米) + public static double Haversine(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadiusKm * c; + } + + /// + /// 计算两个坐标之间的距离(米) + /// + public static double DistanceInMeters(double lat1, double lon1, double lat2, double lon2) + { + return Haversine(lat1, lon1, lat2, lon2) * 1000; + } + + /// + /// 计算两个坐标之间的距离(英里) + /// + public static double DistanceInMiles(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadiusMile * c; + } + + #endregion + + #region 方位角计算 + + /// + /// 计算从起点到终点的方位角(初始方位角) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 方位角(度数,0-360,正北为0) + public static double Bearing(double lat1, double lon1, double lat2, double lon2) + { + var dLon = ToRadians(lon2 - lon1); + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + + var y = Math.Sin(dLon) * Math.Cos(lat2Rad); + var x = Math.Cos(lat1Rad) * Math.Sin(lat2Rad) - + Math.Sin(lat1Rad) * Math.Cos(lat2Rad) * Math.Cos(dLon); + + var bearing = Math.Atan2(y, x); + bearing = ToDegrees(bearing); + bearing = (bearing + 360) % 360; + + return bearing; + } + + /// + /// 根据方位角获取方向描述 + /// + /// 方位角 + /// 方向描述 + public static string GetDirectionName(double bearing) + { + bearing = ((bearing % 360) + 360) % 360; + + return bearing switch + { + >= 337.5 or < 22.5 => "正北", + >= 22.5 and < 67.5 => "东北", + >= 67.5 and < 112.5 => "正东", + >= 112.5 and < 157.5 => "东南", + >= 157.5 and < 202.5 => "正南", + >= 202.5 and < 247.5 => "西南", + >= 247.5 and < 292.5 => "正西", + >= 292.5 and < 337.5 => "西北", + _ => "未知" + }; + } + + #endregion + + #region 目标点计算 + + /// + /// 根据起点、方位角和距离计算终点坐标 + /// + /// 起点纬度 + /// 起点经度 + /// 方位角(度) + /// 距离(千米) + /// 终点坐标(纬度,经度) + public static (double Latitude, double Longitude) DestinationPoint( + double lat, double lon, double bearing, double distanceKm) + { + var bearingRad = ToRadians(bearing); + var lat1 = ToRadians(lat); + var lon1 = ToRadians(lon); + var d = distanceKm / EarthRadiusKm; + + var lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(d) + + Math.Cos(lat1) * Math.Sin(d) * Math.Cos(bearingRad)); + + var lon2 = lon1 + Math.Atan2( + Math.Sin(bearingRad) * Math.Sin(d) * Math.Cos(lat1), + Math.Cos(d) - Math.Sin(lat1) * Math.Sin(lat2)); + + return (ToDegrees(lat2), ToDegrees(lon2)); + } + + /// + /// 计算指定距离处的边界框(用于数据库查询) + /// + /// 中心点纬度 + /// 中心点经度 + /// 距离(千米) + /// 边界框(最小纬度,最小经度,最大纬度,最大经度) + public static (double MinLat, double MinLon, double MaxLat, double MaxLon) BoundingBox( + double lat, double lon, double distanceKm) + { + var latRad = ToRadians(lat); + var d = distanceKm / EarthRadiusKm; + + // 纬度变化 + var dLat = d; + var dLon = Math.Asin(Math.Sin(d) / Math.Cos(latRad)); + + var minLat = lat - ToDegrees(dLat); + var maxLat = lat + ToDegrees(dLat); + var minLon = lon - ToDegrees(dLon); + var maxLon = lon + ToDegrees(dLon); + + return (minLat, minLon, maxLat, maxLon); + } + + #endregion + + #region 中点计算 + + /// + /// 计算两个坐标之间的中点 + /// + public static (double Latitude, double Longitude) Midpoint( + double lat1, double lon1, double lat2, double lon2) + { + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + var lon1Rad = ToRadians(lon1); + var dLon = ToRadians(lon2 - lon1); + + var bx = Math.Cos(lat2Rad) * Math.Cos(dLon); + var by = Math.Cos(lat2Rad) * Math.Sin(dLon); + + var lat3 = Math.Atan2( + Math.Sin(lat1Rad) + Math.Sin(lat2Rad), + Math.Sqrt((Math.Cos(lat1Rad) + bx) * (Math.Cos(lat1Rad) + bx) + by * by)); + + var lon3 = lon1Rad + Math.Atan2(by, Math.Cos(lat1Rad) + bx); + + return (ToDegrees(lat3), ToDegrees(lon3)); + } + + #endregion + + #region 直线距离估算 + + /// + /// 使用勾股定理近似计算短距离(适用于小范围) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 距离(米) + public static double EuclideanDistance(double lat1, double lon1, double lat2, double lon2) + { + var avgLat = ToRadians((lat1 + lat2) / 2); + var latDist = ToRadians(lat2 - lat1) * EarthRadiusM; + var lonDist = ToRadians(lon2 - lon1) * EarthRadiusM * Math.Cos(avgLat); + + return Math.Sqrt(latDist * latDist + lonDist * lonDist); + } + + #endregion + + #region 驾驶距离估算 + + /// + /// 估算驾驶距离(直线距离乘以系数) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 系数(默认1.4,城市间约为1.2-1.3,城市内约为1.4-1.6) + /// 估算驾驶距离(千米) + public static double EstimatedDrivingDistance( + double lat1, double lon1, double lat2, double lon2, double factor = 1.4) + { + return Haversine(lat1, lon1, lat2, lon2) * factor; + } + + #endregion + + #region 坐标转换 + + /// + /// 度转弧度 + /// + public static double ToRadians(double degrees) + { + return degrees * Math.PI / 180.0; + } + + /// + /// 弧度转度 + /// + public static double ToDegrees(double radians) + { + return radians * 180.0 / Math.PI; + } + + /// + /// 度分秒转十进制度 + /// + /// 度 + /// 分 + /// 秒 + /// 十进制度 + public static double DmsToDecimal(int degrees, int minutes, double seconds) + { + return degrees + minutes / 60.0 + seconds / 3600.0; + } + + /// + /// 十进制度转度分秒 + /// + /// 十进制度 + /// 度分秒元组 + public static (int Degrees, int Minutes, double Seconds) DecimalToDms(double decimalDegrees) + { + var degrees = (int)decimalDegrees; + var remainder = (decimalDegrees - degrees) * 60; + var minutes = (int)remainder; + var seconds = (remainder - minutes) * 60; + + return (degrees, minutes, seconds); + } + + #endregion + + #region 坐标验证 + + /// + /// 验证经度是否有效 + /// + public static bool IsValidLongitude(double longitude) + { + return longitude >= -180 && longitude <= 180; + } + + /// + /// 验证纬度是否有效 + /// + public static bool IsValidLatitude(double latitude) + { + return latitude >= -90 && latitude <= 90; + } + + /// + /// 验证坐标是否有效 + /// + public static bool IsValidCoordinate(double latitude, double longitude) + { + return IsValidLatitude(latitude) && IsValidLongitude(longitude); + } + + /// + /// 标准化经度到 -180 到 180 范围 + /// + public static double NormalizeLongitude(double longitude) + { + while (longitude > 180) longitude -= 360; + while (longitude < -180) longitude += 360; + return longitude; + } + + #endregion + + #region 格式化 + + /// + /// 格式化坐标为字符串 + /// + /// 纬度 + /// 经度 + /// 小数位数 + /// 格式化后的字符串 + public static string Format(double latitude, double longitude, int decimalPlaces = 6) + { + var latDir = latitude >= 0 ? "N" : "S"; + var lonDir = longitude >= 0 ? "E" : "W"; + + return $"{Math.Abs(latitude).ToString("F" + decimalPlaces)}°{latDir}, {Math.Abs(longitude).ToString("F" + decimalPlaces)}°{lonDir}"; + } + + /// + /// 格式化为度分秒 + /// + public static string FormatDms(double latitude, double longitude) + { + var (latDeg, latMin, latSec) = DecimalToDms(Math.Abs(latitude)); + var (lonDeg, lonMin, lonSec) = DecimalToDms(Math.Abs(longitude)); + + var latDir = latitude >= 0 ? "N" : "S"; + var lonDir = longitude >= 0 ? "E" : "W"; + + return $"{latDeg}°{latMin}'{latSec:F2}\"{latDir}, {lonDeg}°{lonMin}'{lonSec:F2}\"{lonDir}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/FractionUtil.cs b/EasyTool.Core/MathCategory/FractionUtil.cs new file mode 100644 index 0000000..78b0a63 --- /dev/null +++ b/EasyTool.Core/MathCategory/FractionUtil.cs @@ -0,0 +1,405 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 分数工具类 + /// 提供精确的有理数运算 + /// + public static class FractionUtil + { + /// + /// 创建分数 + /// + public static Fraction Create(long numerator, long denominator = 1) + { + return new Fraction(numerator, denominator); + } + + /// + /// 从小数创建分数 + /// + public static Fraction FromDouble(double value, long maxDenominator = 1000000) + { + return Fraction.FromDouble(value, maxDenominator); + } + + /// + /// 解析分数字符串(如 "3/4") + /// + public static Fraction Parse(string s) + { + return Fraction.Parse(s); + } + + /// + /// 尝试解析分数字符串 + /// + public static bool TryParse(string s, out Fraction result) + { + return Fraction.TryParse(s, out result); + } + + /// + /// 获取最小公倍数 + /// + public static long LCM(long a, long b) + { + return Math.Abs(a * b) / GCD(a, b); + } + + /// + /// 获取最大公约数 + /// + public static long GCD(long a, long b) + { + a = Math.Abs(a); + b = Math.Abs(b); + while (b != 0) + { + long temp = b; + b = a % b; + a = temp; + } + return a; + } + } + + /// + /// 分数(有理数) + /// + public readonly struct Fraction : IComparable, IEquatable + { + /// + /// 分子 + /// + public long Numerator { get; } + + /// + /// 分母 + /// + public long Denominator { get; } + + /// + /// 零 + /// + public static Fraction Zero => new(0, 1); + + /// + /// 一 + /// + public static Fraction One => new(1, 1); + + /// + /// 二分之一 + /// + public static Fraction Half => new(1, 2); + + /// + /// 创建分数 + /// + public Fraction(long numerator, long denominator = 1) + { + if (denominator == 0) + throw new DivideByZeroException("Denominator cannot be zero"); + + // 约分 + long gcd = FractionUtil.GCD(numerator, denominator); + numerator /= gcd; + denominator /= gcd; + + // 确保分母为正 + if (denominator < 0) + { + numerator = -numerator; + denominator = -denominator; + } + + Numerator = numerator; + Denominator = denominator; + } + + /// + /// 从小数创建分数 + /// + public static Fraction FromDouble(double value, long maxDenominator = 1000000) + { + if (double.IsNaN(value) || double.IsInfinity(value)) + throw new ArgumentException("Cannot convert NaN or infinity to fraction"); + + long sign = value < 0 ? -1 : 1; + value = Math.Abs(value); + + long wholePart = (long)value; + double fractionalPart = value - wholePart; + + if (fractionalPart < 1e-15) + { + return new Fraction(sign * wholePart, 1); + } + + // 使用连分数算法 + long numerator = 1; + long denominator = (long)(1 / fractionalPart); + double remainder = 1 / fractionalPart - denominator; + + while (Math.Abs(fractionalPart - (double)numerator / denominator) > 1e-15 && denominator < maxDenominator) + { + long newNumerator = denominator; + long newDenominator = (long)(1 / remainder); + remainder = 1 / remainder - newDenominator; + + if (newDenominator == 0 || denominator + newDenominator > maxDenominator) + break; + + numerator = newNumerator; + denominator = denominator + newDenominator; + } + + // 简化计算 + long bestDen = 1; + double bestError = Math.Abs(fractionalPart); + + for (long d = 1; d <= Math.Min(maxDenominator, 10000); d++) + { + long n = (long)Math.Round(fractionalPart * d); + double error = Math.Abs(fractionalPart - (double)n / d); + if (error < bestError) + { + bestError = error; + bestDen = d; + } + } + + long finalNumerator = (long)Math.Round(fractionalPart * bestDen); + return new Fraction(sign * (wholePart * bestDen + finalNumerator), bestDen); + } + + /// + /// 解析分数字符串 + /// + public static Fraction Parse(string s) + { + if (!TryParse(s, out var result)) + throw new FormatException($"Cannot parse '{s}' as fraction"); + return result; + } + + /// + /// 尝试解析分数字符串 + /// + public static bool TryParse(string s, out Fraction result) + { + result = Zero; + + if (string.IsNullOrWhiteSpace(s)) + return false; + + s = s.Trim(); + + // 处理负号 + int sign = 1; + if (s.StartsWith("-")) + { + sign = -1; + s = s.Substring(1); + } + + // 尝试解析为纯数字 + if (long.TryParse(s, out long whole)) + { + result = new Fraction(sign * whole, 1); + return true; + } + + // 尝试解析为分数 + if (s.Contains("/")) + { + var parts = s.Split('/'); + if (parts.Length == 2 && + long.TryParse(parts[0], out long num) && + long.TryParse(parts[1], out long den)) + { + result = new Fraction(sign * num, den); + return true; + } + } + + // 尝试解析为带分数(如 "1 1/2") + if (s.Contains(" ")) + { + var parts = s.Split(' '); + if (parts.Length == 2 && + long.TryParse(parts[0], out long whole2) && + parts[1].Contains("/")) + { + var fracParts = parts[1].Split('/'); + if (fracParts.Length == 2 && + long.TryParse(fracParts[0], out long num) && + long.TryParse(fracParts[1], out long den)) + { + result = new Fraction(sign * (whole2 * den + num), den); + return true; + } + } + } + + return false; + } + + /// + /// 转换为小数 + /// + public double ToDouble() => (double)Numerator / Denominator; + + /// + /// 转换为小数(decimal) + /// + public decimal ToDecimal() => (decimal)Numerator / Denominator; + + /// + /// 获取倒数 + /// + public Fraction Reciprocal => new(Denominator, Numerator); + + /// + /// 获取绝对值 + /// + public Fraction Abs => new(Math.Abs(Numerator), Denominator); + + /// + /// 取反 + /// + public Fraction Negate => new(-Numerator, Denominator); + + /// + /// 约分 + /// + public Fraction Simplify() + { + if (Numerator == 0) return Zero; + + long gcd = FractionUtil.GCD(Numerator, Denominator); + return new Fraction(Numerator / gcd, Denominator / gcd); + } + + /// + /// 转换为带分数 + /// + public (long Whole, Fraction Fractional) ToMixedNumber() + { + long whole = Numerator / Denominator; + long remainder = Numerator % Denominator; + return (whole, new Fraction(remainder, Denominator)); + } + + #region 运算符 + + public static Fraction operator +(Fraction a, Fraction b) + { + long den = FractionUtil.LCM(a.Denominator, b.Denominator); + long num = a.Numerator * (den / a.Denominator) + b.Numerator * (den / b.Denominator); + return new Fraction(num, den); + } + + public static Fraction operator -(Fraction a, Fraction b) + { + long den = FractionUtil.LCM(a.Denominator, b.Denominator); + long num = a.Numerator * (den / a.Denominator) - b.Numerator * (den / b.Denominator); + return new Fraction(num, den); + } + + public static Fraction operator *(Fraction a, Fraction b) + { + return new Fraction(a.Numerator * b.Numerator, a.Denominator * b.Denominator); + } + + public static Fraction operator /(Fraction a, Fraction b) + { + if (b.Numerator == 0) + throw new DivideByZeroException(); + return new Fraction(a.Numerator * b.Denominator, a.Denominator * b.Numerator); + } + + public static Fraction operator %(Fraction a, Fraction b) + { + return a - (a / b).Floor * b; + } + + public static Fraction operator +(Fraction a) => a; + public static Fraction operator -(Fraction a) => a.Negate; + + public static bool operator ==(Fraction a, Fraction b) => a.Equals(b); + public static bool operator !=(Fraction a, Fraction b) => !a.Equals(b); + public static bool operator <(Fraction a, Fraction b) => a.CompareTo(b) < 0; + public static bool operator >(Fraction a, Fraction b) => a.CompareTo(b) > 0; + public static bool operator <=(Fraction a, Fraction b) => a.CompareTo(b) <= 0; + public static bool operator >=(Fraction a, Fraction b) => a.CompareTo(b) >= 0; + + public static implicit operator Fraction(long value) => new(value, 1); + public static implicit operator Fraction(int value) => new(value, 1); + public static explicit operator double(Fraction f) => f.ToDouble(); + public static explicit operator decimal(Fraction f) => f.ToDecimal(); + + #endregion + + /// + /// 向下取整 + /// + public Fraction Floor => new(Numerator / Denominator, 1); + + /// + /// 向上取整 + /// + public Fraction Ceiling => new((Numerator + Denominator - 1) / Denominator, 1); + + /// + /// 四舍五入 + /// + public Fraction Round() + { + var mixed = ToMixedNumber(); + if (mixed.Fractional >= Half) + return new Fraction(mixed.Whole + 1, 1); + return new Fraction(mixed.Whole, 1); + } + + public int CompareTo(Fraction other) + { + return (Numerator * other.Denominator).CompareTo(other.Numerator * Denominator); + } + + public bool Equals(Fraction other) + { + return Numerator == other.Numerator && Denominator == other.Denominator; + } + + public override bool Equals(object obj) + { + return obj is Fraction other && Equals(other); + } + + public override int GetHashCode() + { + return HashCode.Combine(Numerator, Denominator); + } + + public override string ToString() + { + if (Denominator == 1) + return Numerator.ToString(); + return $"{Numerator}/{Denominator}"; + } + + /// + /// 转换为带分数字符串 + /// + public string ToMixedString() + { + var (whole, frac) = ToMixedNumber(); + if (whole == 0) return frac.ToString(); + if (frac.Numerator == 0) return whole.ToString(); + return $"{whole} {frac}"; + } + } +} diff --git a/EasyTool.Core/MathCategory/GeometryUtil.cs b/EasyTool.Core/MathCategory/GeometryUtil.cs new file mode 100644 index 0000000..df01c4f --- /dev/null +++ b/EasyTool.Core/MathCategory/GeometryUtil.cs @@ -0,0 +1,593 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 几何工具类 + /// 提供点、线、面、多边形等几何计算功能 + /// + public static class GeometryUtil + { + #region 点 + + /// + /// 计算两点之间的距离 + /// + public static double Distance(Point2D p1, Point2D p2) + { + return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2)); + } + + /// + /// 计算两点之间的距离(3D) + /// + public static double Distance(Point3D p1, Point3D p2) + { + return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2) + Math.Pow(p2.Z - p1.Z, 2)); + } + + /// + /// 获取两点之间的中点 + /// + public static Point2D Midpoint(Point2D p1, Point2D p2) + { + return new Point2D((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2); + } + + /// + /// 点是否在线段上 + /// + public static bool IsPointOnLine(Point2D point, Line2D line, double tolerance = 1e-10) + { + // 使用叉积判断 + double cross = (point.Y - line.Start.Y) * (line.End.X - line.Start.X) - + (point.X - line.Start.X) * (line.End.Y - line.Start.Y); + + if (Math.Abs(cross) > tolerance) return false; + + // 检查是否在线段范围内 + return point.X >= Math.Min(line.Start.X, line.End.X) - tolerance && + point.X <= Math.Max(line.Start.X, line.End.X) + tolerance && + point.Y >= Math.Min(line.Start.Y, line.End.Y) - tolerance && + point.Y <= Math.Max(line.Start.Y, line.End.Y) + tolerance; + } + + /// + /// 点到直线的距离 + /// + public static double PointToLineDistance(Point2D point, Line2D line) + { + double A = line.End.Y - line.Start.Y; + double B = line.Start.X - line.End.X; + double C = line.End.X * line.Start.Y - line.Start.X * line.End.Y; + + return Math.Abs(A * point.X + B * point.Y + C) / Math.Sqrt(A * A + B * B); + } + + /// + /// 点到线段的最近点 + /// + public static Point2D ClosestPointOnSegment(Point2D point, Line2D line) + { + double dx = line.End.X - line.Start.X; + double dy = line.End.Y - line.Start.Y; + + if (Math.Abs(dx) < 1e-10 && Math.Abs(dy) < 1e-10) + return line.Start; + + double t = ((point.X - line.Start.X) * dx + (point.Y - line.Start.Y) * dy) / (dx * dx + dy * dy); + t = Math.Max(0, Math.Min(1, t)); + + return new Point2D(line.Start.X + t * dx, line.Start.Y + t * dy); + } + + #endregion + + #region 线 + + /// + /// 计算线段长度 + /// + public static double Length(Line2D line) + { + return Distance(line.Start, line.End); + } + + /// + /// 两条线段是否相交 + /// + public static bool Intersects(Line2D line1, Line2D line2) + { + return GetIntersection(line1, line2) != null; + } + + /// + /// 获取两条线段的交点 + /// + public static Point2D? GetIntersection(Line2D line1, Line2D line2) + { + double x1 = line1.Start.X, y1 = line1.Start.Y; + double x2 = line1.End.X, y2 = line1.End.Y; + double x3 = line2.Start.X, y3 = line2.Start.Y; + double x4 = line2.End.X, y4 = line2.End.Y; + + double denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); + + if (Math.Abs(denom) < 1e-10) return null; // 平行 + + double t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom; + double u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom; + + if (t >= 0 && t <= 1 && u >= 0 && u <= 1) + { + return new Point2D(x1 + t * (x2 - x1), y1 + t * (y2 - y1)); + } + + return null; + } + + /// + /// 计算两直线的夹角(弧度) + /// + public static double AngleBetween(Line2D line1, Line2D line2) + { + double dx1 = line1.End.X - line1.Start.X; + double dy1 = line1.End.Y - line1.Start.Y; + double dx2 = line2.End.X - line2.Start.X; + double dy2 = line2.End.Y - line2.Start.Y; + + double dot = dx1 * dx2 + dy1 * dy2; + double len1 = Math.Sqrt(dx1 * dx1 + dy1 * dy1); + double len2 = Math.Sqrt(dx2 * dx2 + dy2 * dy2); + + if (len1 < 1e-10 || len2 < 1e-10) return 0; + + double cos = dot / (len1 * len2); + cos = Math.Max(-1, Math.Min(1, cos)); + + return Math.Acos(cos); + } + + #endregion + + #region 多边形 + + /// + /// 计算多边形周长 + /// + public static double Perimeter(Polygon polygon) + { + double perimeter = 0; + var points = polygon.Points; + for (int i = 0; i < points.Count; i++) + { + int next = (i + 1) % points.Count; + perimeter += Distance(points[i], points[next]); + } + return perimeter; + } + + /// + /// 计算多边形面积(使用鞋带公式) + /// + public static double Area(Polygon polygon) + { + double area = 0; + var points = polygon.Points; + + for (int i = 0; i < points.Count; i++) + { + int next = (i + 1) % points.Count; + area += points[i].X * points[next].Y; + area -= points[next].X * points[i].Y; + } + + return Math.Abs(area) / 2; + } + + /// + /// 判断多边形是否为凸多边形 + /// + public static bool IsConvex(Polygon polygon) + { + var points = polygon.Points; + if (points.Count < 3) return false; + + bool? sign = null; + for (int i = 0; i < points.Count; i++) + { + int prev = (i - 1 + points.Count) % points.Count; + int next = (i + 1) % points.Count; + + double cross = CrossProduct( + points[prev], points[i], points[next]); + + if (cross != 0) + { + if (sign == null) + sign = cross > 0; + else if (sign != cross > 0) + return false; + } + } + + return true; + } + + private static double CrossProduct(Point2D o, Point2D a, Point2D b) + { + return (a.X - o.X) * (b.Y - o.Y) - (a.Y - o.Y) * (b.X - o.X); + } + + /// + /// 判断点是否在多边形内(射线法) + /// + public static bool IsPointInPolygon(Point2D point, Polygon polygon) + { + var points = polygon.Points; + int n = points.Count; + bool inside = false; + + for (int i = 0, j = n - 1; i < n; j = i++) + { + if (((points[i].Y > point.Y) != (points[j].Y > point.Y)) && + (point.X < (points[j].X - points[i].X) * (point.Y - points[i].Y) / (points[j].Y - points[i].Y) + points[i].X)) + { + inside = !inside; + } + } + + return inside; + } + + /// + /// 计算多边形质心 + /// + public static Point2D Centroid(Polygon polygon) + { + var points = polygon.Points; + double cx = 0, cy = 0; + + foreach (var p in points) + { + cx += p.X; + cy += p.Y; + } + + return new Point2D(cx / points.Count, cy / points.Count); + } + + /// + /// 计算凸包(Graham 扫描算法) + /// + public static Polygon ConvexHull(List points) + { + if (points.Count < 3) return new Polygon(points); + + // 找到最下方的点(y最小,y相同取x最小) + var start = points.OrderBy(p => p.Y).ThenBy(p => p.X).First(); + var sorted = points.Where(p => p != start).ToList(); + + // 按极角排序 + sorted.Sort((a, b) => + { + double angleA = Math.Atan2(a.Y - start.Y, a.X - start.X); + double angleB = Math.Atan2(b.Y - start.Y, b.X - start.X); + if (Math.Abs(angleA - angleB) < 1e-10) + { + return Distance(start, a).CompareTo(Distance(start, b)); + } + return angleA.CompareTo(angleB); + }); + + var hull = new List { start }; + + foreach (var point in sorted) + { + while (hull.Count > 1 && CrossProduct(hull[hull.Count - 2], hull[hull.Count - 1], point) <= 0) + { + hull.RemoveAt(hull.Count - 1); + } + hull.Add(point); + } + + return new Polygon(hull); + } + + /// + /// 多边形简化(Douglas-Peucker 算法) + /// + public static Polygon Simplify(Polygon polygon, double tolerance) + { + var points = polygon.Points; + if (points.Count < 3) return polygon; + + var result = DouglasPeucker(points, tolerance); + return new Polygon(result); + } + + private static List DouglasPeucker(List points, double tolerance) + { + if (points.Count <= 2) return points; + + double maxDist = 0; + int maxIndex = 0; + var line = new Line2D(points[0], points[points.Count - 1]); + + for (int i = 1; i < points.Count - 1; i++) + { + double dist = PointToLineDistance(points[i], line); + if (dist > maxDist) + { + maxDist = dist; + maxIndex = i; + } + } + + if (maxDist > tolerance) + { + var left = DouglasPeucker(points.GetRange(0, maxIndex + 1), tolerance); + var right = DouglasPeucker(points.GetRange(maxIndex, points.Count - maxIndex), tolerance); + + var result = new List(left); + result.AddRange(right.Skip(1)); + return result; + } + + return new List { points[0], points[points.Count - 1] }; + } + + #endregion + + #region 圆 + + /// + /// 计算圆的周长 + /// + public static double Circumference(Circle circle) + { + return 2 * Math.PI * circle.Radius; + } + + /// + /// 计算圆的面积 + /// + public static double Area(Circle circle) + { + return Math.PI * circle.Radius * circle.Radius; + } + + /// + /// 判断点是否在圆内 + /// + public static bool IsPointInCircle(Point2D point, Circle circle) + { + return Distance(point, circle.Center) <= circle.Radius; + } + + /// + /// 获取圆与直线的交点 + /// + public static List GetCircleLineIntersections(Circle circle, Line2D line) + { + var result = new List(); + + double dx = line.End.X - line.Start.X; + double dy = line.End.Y - line.Start.Y; + + double fx = line.Start.X - circle.Center.X; + double fy = line.Start.Y - circle.Center.Y; + + double a = dx * dx + dy * dy; + double b = 2 * (fx * dx + fy * dy); + double c = fx * fx + fy * fy - circle.Radius * circle.Radius; + + double discriminant = b * b - 4 * a * c; + + if (discriminant < 0) return result; + + discriminant = Math.Sqrt(discriminant); + + double t1 = (-b - discriminant) / (2 * a); + double t2 = (-b + discriminant) / (2 * a); + + if (t1 >= 0 && t1 <= 1) + result.Add(new Point2D(line.Start.X + t1 * dx, line.Start.Y + t1 * dy)); + + if (t2 >= 0 && t2 <= 1 && Math.Abs(t1 - t2) > 1e-10) + result.Add(new Point2D(line.Start.X + t2 * dx, line.Start.Y + t2 * dy)); + + return result; + } + + #endregion + + #region 三角形 + + /// + /// 计算三角形面积(海伦公式) + /// + public static double TriangleArea(Point2D a, Point2D b, Point2D c) + { + double ab = Distance(a, b); + double bc = Distance(b, c); + double ca = Distance(c, a); + double s = (ab + bc + ca) / 2; + + return Math.Sqrt(s * (s - ab) * (s - bc) * (s - ca)); + } + + /// + /// 判断点是否在三角形内 + /// + public static bool IsPointInTriangle(Point2D p, Point2D a, Point2D b, Point2D c) + { + double area = TriangleArea(a, b, c); + double area1 = TriangleArea(p, b, c); + double area2 = TriangleArea(a, p, c); + double area3 = TriangleArea(a, b, p); + + return Math.Abs(area - (area1 + area2 + area3)) < 1e-10; + } + + #endregion + } + + #region 几何类型定义 + + /// + /// 二维点 + /// + public struct Point2D : IEquatable + { + /// X坐标 + public double X { get; set; } + /// Y坐标 + public double Y { get; set; } + + public Point2D(double x, double y) { X = x; Y = y; } + + public static Point2D operator +(Point2D a, Point2D b) => new(a.X + b.X, a.Y + b.Y); + public static Point2D operator -(Point2D a, Point2D b) => new(a.X - b.X, a.Y - b.Y); + public static Point2D operator *(Point2D p, double scalar) => new(p.X * scalar, p.Y * scalar); + public static bool operator ==(Point2D left, Point2D right) => left.Equals(right); + public static bool operator !=(Point2D left, Point2D right) => !left.Equals(right); + + public double Length => Math.Sqrt(X * X + Y * Y); + public Point2D Normalize => this * (1 / Length); + + public bool Equals(Point2D other) => Math.Abs(X - other.X) < 1e-10 && Math.Abs(Y - other.Y) < 1e-10; + public override bool Equals(object? obj) => obj is Point2D other && Equals(other); + public override int GetHashCode() => HashCode.Combine(X, Y); + public override string ToString() => $"({X:F2}, {Y:F2})"; + } + + /// + /// 三维点 + /// + public struct Point3D + { + /// X坐标 + public double X { get; set; } + /// Y坐标 + public double Y { get; set; } + /// Z坐标 + public double Z { get; set; } + + public Point3D(double x, double y, double z) { X = x; Y = y; Z = z; } + + public static Point3D operator +(Point3D a, Point3D b) => new(a.X + b.X, a.Y + b.Y, a.Z + b.Z); + public static Point3D operator -(Point3D a, Point3D b) => new(a.X - b.X, a.Y - b.Y, a.Z - b.Z); + + public double Length => Math.Sqrt(X * X + Y * Y + Z * Z); + + public override string ToString() => $"({X:F2}, {Y:F2}, {Z:F2})"; + } + + /// + /// 二维线段 + /// + public struct Line2D + { + /// 起点 + public Point2D Start { get; set; } + /// 终点 + public Point2D End { get; set; } + + public Line2D(Point2D start, Point2D end) { Start = start; End = end; } + public Line2D(double x1, double y1, double x2, double y2) + : this(new Point2D(x1, y1), new Point2D(x2, y2)) { } + + public double Length => GeometryUtil.Distance(Start, End); + + public override string ToString() => $"[{Start} -> {End}]"; + } + + /// + /// 多边形 + /// + public class Polygon + { + /// 顶点列表 + public List Points { get; } + + public Polygon(IEnumerable points) + { + Points = new List(points); + } + + public int VertexCount => Points.Count; + public double Perimeter => GeometryUtil.Perimeter(this); + public double Area => GeometryUtil.Area(this); + public bool IsConvex => GeometryUtil.IsConvex(this); + public Point2D Centroid => GeometryUtil.Centroid(this); + + public override string ToString() => $"Polygon[{VertexCount} vertices, Area={Area:F2}]"; + } + + /// + /// 圆 + /// + public struct Circle + { + /// 圆心 + public Point2D Center { get; set; } + /// 半径 + public double Radius { get; set; } + + public Circle(Point2D center, double radius) { Center = center; Radius = radius; } + public Circle(double x, double y, double radius) + : this(new Point2D(x, y), radius) { } + + public double Circumference => GeometryUtil.Circumference(this); + public double Area => GeometryUtil.Area(this); + + public override string ToString() => $"Circle[Center={Center}, R={Radius:F2}]"; + } + + /// + /// 矩形 + /// + public struct Rectangle2D + { + /// 左上角X + public double X { get; set; } + /// 左上角Y + public double Y { get; set; } + /// 宽度 + public double Width { get; set; } + /// 高度 + public double Height { get; set; } + + public Rectangle2D(double x, double y, double width, double height) + { + X = x; Y = y; Width = width; Height = height; + } + + public double Left => X; + public double Top => Y; + public double Right => X + Width; + public double Bottom => Y + Height; + + public Point2D TopLeft => new(X, Y); + public Point2D TopRight => new(Right, Y); + public Point2D BottomLeft => new(X, Bottom); + public Point2D BottomRight => new(Right, Bottom); + public Point2D Center => new(X + Width / 2, Y + Height / 2); + + public double Perimeter => 2 * (Width + Height); + public double Area => Width * Height; + + public bool Contains(Point2D point) => + point.X >= X && point.X <= Right && point.Y >= Y && point.Y <= Bottom; + + public bool Intersects(Rectangle2D other) => + X < other.Right && Right > other.X && Y < other.Bottom && Bottom > other.Y; + + public override string ToString() => $"Rect[X={X}, Y={Y}, W={Width}, H={Height}]"; + } + + #endregion +} diff --git a/EasyTool.Core/MathCategory/InterpolationUtil.cs b/EasyTool.Core/MathCategory/InterpolationUtil.cs new file mode 100644 index 0000000..3f5fc81 --- /dev/null +++ b/EasyTool.Core/MathCategory/InterpolationUtil.cs @@ -0,0 +1,284 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.MathCategory +{ + /// + /// 插值工具类 + /// 提供各种插值算法 + /// + public static class InterpolationUtil + { + /// + /// 线性插值 + /// + public static double Linear(double x0, double y0, double x1, double y1, double x) + { + if (Math.Abs(x1 - x0) < double.Epsilon) + return y0; + + return y0 + (y1 - y0) * (x - x0) / (x1 - x0); + } + + /// + /// 双线性插值 + /// + public static double Bilinear(double x, double y, + double x1, double y1, double v11, + double x2, double y2, double v12, + double x3, double y3, double v21, + double x4, double y4, double v22) + { + double r1 = Linear(x1, v11, x2, v12, x); + double r2 = Linear(x3, v21, x4, v22, x); + return Linear(y1, r1, y3, r2, y); + } + + /// + /// 拉格朗日插值 + /// + public static double Lagrange(double[] xValues, double[] yValues, double x) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length == 0) + throw new ArgumentException("Arrays cannot be empty"); + + int n = xValues.Length; + double result = 0; + + for (int i = 0; i < n; i++) + { + double term = yValues[i]; + for (int j = 0; j < n; j++) + { + if (i != j) + { + term *= (x - xValues[j]) / (xValues[i] - xValues[j]); + } + } + result += term; + } + + return result; + } + + /// + /// 牛顿插值 + /// + public static double Newton(double[] xValues, double[] yValues, double x) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length == 0) + throw new ArgumentException("Arrays cannot be empty"); + + int n = xValues.Length; + + // 计算差商表 + double[,] dividedDiff = new double[n, n]; + for (int i = 0; i < n; i++) + { + dividedDiff[i, 0] = yValues[i]; + } + + for (int j = 1; j < n; j++) + { + for (int i = 0; i < n - j; i++) + { + dividedDiff[i, j] = (dividedDiff[i + 1, j - 1] - dividedDiff[i, j - 1]) / + (xValues[i + j] - xValues[i]); + } + } + + // 计算插值 + double result = dividedDiff[0, 0]; + double term = 1; + + for (int i = 1; i < n; i++) + { + term *= (x - xValues[i - 1]); + result += term * dividedDiff[0, i]; + } + + return result; + } + + /// + /// 创建三次样条插值器 + /// + public static CubicSpline CreateCubicSpline(double[] xValues, double[] yValues) + { + return new CubicSpline(xValues, yValues); + } + + /// + /// 创建线性插值器 + /// + public static LinearInterpolator CreateLinearInterpolator(double[] xValues, double[] yValues) + { + return new LinearInterpolator(xValues, yValues); + } + } + + /// + /// 三次样条插值 + /// + public class CubicSpline + { + private readonly double[] _x; + private readonly double[] _y; + private readonly double[] _m; // 二阶导数 + + /// + /// 数据点数量 + /// + public int Count => _x.Length; + + /// + /// 创建三次样条插值 + /// + public CubicSpline(double[] xValues, double[] yValues) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length < 2) + throw new ArgumentException("At least 2 points required"); + + _x = (double[])xValues.Clone(); + _y = (double[])yValues.Clone(); + _m = ComputeSecondDerivatives(); + } + + private double[] ComputeSecondDerivatives() + { + int n = _x.Length; + double[] m = new double[n]; + double[] u = new double[n - 1]; + double[] y = new double[n - 1]; + + // 自然边界条件 + m[0] = 0; + m[n - 1] = 0; + + // 追赶法求解三对角方程组 + for (int i = 1; i < n - 1; i++) + { + double hi = _x[i] - _x[i - 1]; + double hi1 = _x[i + 1] - _x[i]; + double alpha = hi / (hi + hi1); + double beta = (3 * (1 - alpha) * (_y[i] - _y[i - 1]) / hi + + 3 * alpha * (_y[i + 1] - _y[i]) / hi1) / (hi + hi1); + + double p = alpha * m[i - 1] + 2; + m[i] = (alpha - 1) / p; + u[i] = (beta - alpha * u[i - 1]) / p; + } + + for (int i = n - 2; i > 0; i--) + { + m[i] = m[i] * m[i + 1] + u[i]; + } + + return m; + } + + /// + /// 插值计算 + /// + public double Interpolate(double x) + { + int n = _x.Length; + + // 二分查找区间 + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) i = 1; + if (i >= n) i = n - 1; + + double h = _x[i] - _x[i - 1]; + double t = (x - _x[i - 1]) / h; + + // 三次样条公式 + double a = _y[i - 1]; + double b = (_y[i] - _y[i - 1]) / h - h * (_m[i] + 2 * _m[i - 1]) / 6; + double c = _m[i - 1] / 2; + double d = (_m[i] - _m[i - 1]) / (6 * h); + + return a + b * t * h + c * t * t * h * h + d * t * t * t * h * h * h; + } + + /// + /// 计算导数 + /// + public double Derivative(double x) + { + int n = _x.Length; + + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) i = 1; + if (i >= n) i = n - 1; + + double h = _x[i] - _x[i - 1]; + double t = (x - _x[i - 1]) / h; + + double b = (_y[i] - _y[i - 1]) / h - h * (_m[i] + 2 * _m[i - 1]) / 6; + double c = _m[i - 1]; + double d = (_m[i] - _m[i - 1]) / (2 * h); + + return b + c * t * h + d * t * t * h * h; + } + } + + /// + /// 线性插值器 + /// + public class LinearInterpolator + { + private readonly double[] _x; + private readonly double[] _y; + + /// + /// 数据点数量 + /// + public int Count => _x.Length; + + /// + /// 创建线性插值器 + /// + public LinearInterpolator(double[] xValues, double[] yValues) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length < 2) + throw new ArgumentException("At least 2 points required"); + + _x = (double[])xValues.Clone(); + _y = (double[])yValues.Clone(); + } + + /// + /// 插值计算 + /// + public double Interpolate(double x) + { + int n = _x.Length; + + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) return _y[0]; + if (i >= n) return _y[n - 1]; + + return InterpolationUtil.Linear(_x[i - 1], _y[i - 1], _x[i], _y[i], x); + } + } +} diff --git a/EasyTool.Core/MathCategory/MoneyUtil.cs b/EasyTool.Core/MathCategory/MoneyUtil.cs new file mode 100644 index 0000000..1a291bf --- /dev/null +++ b/EasyTool.Core/MathCategory/MoneyUtil.cs @@ -0,0 +1,470 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.MathCategory +{ + /// + /// 金额工具类 + /// 提供精确的金额计算、格式化、大写转换等功能 + /// + public static class MoneyUtil + { + #region 金额计算 + + /// + /// 精确加法运算 + /// + /// 金额1 + /// 金额2 + /// 结果 + public static decimal Add(decimal amount1, decimal amount2) + { + return decimal.Add(amount1, amount2); + } + + /// + /// 精确减法运算 + /// + /// 金额1 + /// 金额2 + /// 结果 + public static decimal Subtract(decimal amount1, decimal amount2) + { + return decimal.Subtract(amount1, amount2); + } + + /// + /// 精确乘法运算 + /// + /// 金额 + /// 乘数 + /// 结果 + public static decimal Multiply(decimal amount, decimal multiplier) + { + return decimal.Multiply(amount, multiplier); + } + + /// + /// 精确除法运算 + /// + /// 金额 + /// 除数 + /// 保留小数位数(默认2) + /// 结果 + public static decimal Divide(decimal amount, decimal divisor, int decimals = 2) + { + if (divisor == 0) + throw new DivideByZeroException("除数不能为0"); + + return Math.Round(amount / divisor, decimals, MidpointRounding.AwayFromZero); + } + + /// + /// 四舍五入 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Round(decimal amount, int decimals = 2) + { + return Math.Round(amount, decimals, MidpointRounding.AwayFromZero); + } + + /// + /// 向上取整 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Ceiling(decimal amount, int decimals = 0) + { + var factor = (decimal)Math.Pow(10, decimals); + return Math.Ceiling(amount * factor) / factor; + } + + /// + /// 向下取整 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Floor(decimal amount, int decimals = 0) + { + var factor = (decimal)Math.Pow(10, decimals); + return Math.Floor(amount * factor) / factor; + } + + /// + /// 计算百分比 + /// + /// 金额 + /// 百分比(如25表示25%) + /// 保留小数位数 + /// 结果 + public static decimal Percentage(decimal amount, decimal percentage, int decimals = 2) + { + return Round(amount * percentage / 100, decimals); + } + + /// + /// 计算折扣金额 + /// + /// 原价 + /// 折扣(如8表示8折) + /// 保留小数位数 + /// 折后价 + public static decimal Discount(decimal originalPrice, decimal discount, int decimals = 2) + { + return Round(originalPrice * discount / 10, decimals); + } + + /// + /// 计算利息 + /// + /// 本金 + /// 年利率(如5.5表示5.5%) + /// 天数 + /// 保留小数位数 + /// 利息 + public static decimal Interest(decimal principal, decimal rate, int days, int decimals = 2) + { + return Round(principal * rate / 100 * days / 365, decimals); + } + + #endregion + + #region 格式化 + + /// + /// 格式化金额(默认2位小数,千分位) + /// + /// 金额 + /// 小数位数 + /// 货币符号 + /// 格式化后的字符串 + public static string Format(decimal amount, int decimals = 2, string symbol = "¥") + { + return $"{symbol}{amount.ToString("N" + decimals)}"; + } + + /// + /// 格式化为人民币格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatCNY(decimal amount) + { + return Format(amount, 2, "¥"); + } + + /// + /// 格式化为美元格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatUSD(decimal amount) + { + return Format(amount, 2, "$"); + } + + /// + /// 格式化为欧元格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatEUR(decimal amount) + { + return Format(amount, 2, "€"); + } + + #endregion + + #region 金额大写 + + private static readonly string[] ChineseDigits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + private static readonly string[] ChineseUnits = { "", "拾", "佰", "仟" }; + private static readonly string[] ChineseBigUnits = { "", "万", "亿", "万亿" }; + + /// + /// 转换为人民币大写金额 + /// + /// 金额 + /// 大写金额 + public static string ToChineseUpper(decimal amount) + { + if (amount < 0) + return "负" + ToChineseUpper(-amount); + + if (amount == 0) + return "零元整"; + + // 处理超出范围的金额 + if (amount >= 10000000000000000m) + throw new ArgumentOutOfRangeException(nameof(amount), "金额超出转换范围"); + + var result = new StringBuilder(); + var amountStr = amount.ToString("F2"); + var parts = amountStr.Split('.'); + + // 整数部分 + var integerPart = long.Parse(parts[0]); + if (integerPart > 0) + { + result.Append(ConvertIntegerToChinese(integerPart)); + result.Append("元"); + } + + // 小数部分 + if (parts.Length > 1) + { + var decimalPart = parts[1]; + var jiao = int.Parse(decimalPart[0].ToString()); + var fen = int.Parse(decimalPart[1].ToString()); + + if (jiao > 0) + { + result.Append(ChineseDigits[jiao]); + result.Append("角"); + } + + if (fen > 0) + { + if (jiao == 0 && integerPart > 0) + result.Append("零"); + result.Append(ChineseDigits[fen]); + result.Append("分"); + } + } + + // 只有整数部分 + if (result.ToString().EndsWith("元")) + { + result.Append("整"); + } + + return result.ToString(); + } + + private static string ConvertIntegerToChinese(long number) + { + if (number == 0) + return ChineseDigits[0]; + + var result = new StringBuilder(); + var unitIndex = 0; + var zeroFlag = false; + + while (number > 0) + { + var section = (int)(number % 10000); + var sectionStr = ConvertSectionToChinese(section, zeroFlag); + + if (section > 0) + { + result.Insert(0, ChineseBigUnits[unitIndex]); + result.Insert(0, sectionStr); + zeroFlag = false; + } + else + { + zeroFlag = true; + } + + number /= 10000; + unitIndex++; + } + + return result.ToString(); + } + + private static string ConvertSectionToChinese(int section, bool zeroFlag) + { + var result = new StringBuilder(); + var unitIndex = 0; + var hasZero = zeroFlag; + + while (section > 0) + { + var digit = section % 10; + + if (digit > 0) + { + result.Insert(0, ChineseUnits[unitIndex]); + result.Insert(0, ChineseDigits[digit]); + hasZero = false; + } + else if (!hasZero && unitIndex > 0) + { + result.Insert(0, ChineseDigits[0]); + hasZero = true; + } + + section /= 10; + unitIndex++; + } + + return result.ToString(); + } + + /// + /// 人民币大写金额转数字 + /// + /// 大写金额 + /// 数字金额 + public static decimal FromChineseUpper(string chineseAmount) + { + if (string.IsNullOrWhiteSpace(chineseAmount)) + return 0; + + // 移除"人民币"、"整"等 + chineseAmount = chineseAmount.Replace("人民币", "").Replace("整", "").Trim(); + + if (chineseAmount == "零元") + return 0; + + var digitMap = new Dictionary + { + {'零', 0}, {'壹', 1}, {'贰', 2}, {'叁', 3}, {'肆', 4}, + {'伍', 5}, {'陆', 6}, {'柒', 7}, {'捌', 8}, {'玖', 9} + }; + + var unitMap = new Dictionary + { + {'拾', 10}, {'佰', 100}, {'仟', 1000}, + {'万', 10000}, {'亿', 100000000} + }; + + decimal result = 0; + decimal temp = 0; + decimal section = 0; + + foreach (var c in chineseAmount) + { + if (c == '元') + { + result += temp + section; + temp = 0; + section = 0; + } + else if (c == '角') + { + result += temp / 10m; + temp = 0; + } + else if (c == '分') + { + result += temp / 100m; + temp = 0; + } + else if (digitMap.ContainsKey(c)) + { + temp = digitMap[c]; + } + else if (c == '拾' || c == '佰' || c == '仟') + { + section += temp * unitMap[c]; + temp = 0; + } + else if (c == '万' || c == '亿') + { + section = (section + temp) * unitMap[c]; + temp = 0; + } + } + + return result + section + temp; + } + + #endregion + + #region 汇率转换(简化版) + + /// + /// 常用货币汇率(相对于人民币,仅供参考) + /// + private static readonly Dictionary ExchangeRates = new() + { + { "CNY", 1.0m }, + { "USD", 7.2m }, + { "EUR", 7.8m }, + { "GBP", 9.1m }, + { "JPY", 0.048m }, + { "KRW", 0.0054m }, + { "HKD", 0.92m }, + { "TWD", 0.22m } + }; + + /// + /// 货币转换 + /// + /// 金额 + /// 源货币代码 + /// 目标货币代码 + /// 保留小数位数 + /// 转换后的金额 + public static decimal Convert(decimal amount, string fromCurrency, string toCurrency, int decimals = 2) + { + fromCurrency = fromCurrency.ToUpperInvariant(); + toCurrency = toCurrency.ToUpperInvariant(); + + if (!ExchangeRates.ContainsKey(fromCurrency)) + throw new ArgumentException($"不支持的货币: {fromCurrency}"); + + if (!ExchangeRates.ContainsKey(toCurrency)) + throw new ArgumentException($"不支持的货币: {toCurrency}"); + + // 先转为人民币,再转为目标货币 + var cny = amount * ExchangeRates[fromCurrency]; + var result = cny / ExchangeRates[toCurrency]; + + return Round(result, decimals); + } + + /// + /// 获取支持的货币列表 + /// + /// 货币代码列表 + public static IEnumerable GetSupportedCurrencies() + { + return ExchangeRates.Keys; + } + + /// + /// 更新汇率 + /// + /// 货币代码 + /// 对人民币汇率 + public static void UpdateExchangeRate(string currency, decimal rateToCNY) + { + ExchangeRates[currency.ToUpperInvariant()] = rateToCNY; + } + + #endregion + + #region 分转元 + + /// + /// 分转元 + /// + /// 分 + /// + public static decimal FenToYuan(long fen) + { + return fen / 100m; + } + + /// + /// 元转分 + /// + /// 元 + /// + public static long YuanToFen(decimal yuan) + { + return (long)Math.Round(yuan * 100, MidpointRounding.AwayFromZero); + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/RandomUtil.cs b/EasyTool.Core/MathCategory/RandomUtil.cs index 5bc77d7..3e858c5 100644 --- a/EasyTool.Core/MathCategory/RandomUtil.cs +++ b/EasyTool.Core/MathCategory/RandomUtil.cs @@ -1,54 +1,65 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; namespace EasyTool.MathCategory { public static class RandomUtil { - private static readonly Random random = new Random(); +#if NET6_0_OR_GREATER + // .NET 6+ 使用 Random.Shared,线程安全且高性能 + private static Random SharedRandom => Random.Shared; +#else + // .NET Standard 2.1 使用 ThreadLocal 确保线程安全 + private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); + private static Random SharedRandom => ThreadLocalRandom.Value!; +#endif /// /// 生成指定范围内的随机整数 + /// 注意:返回值为 [min, max) 区间,即包含 min 但不包含 max /// - /// 随机整数的最小值 - /// 随机整数的最大值 + /// 随机整数的最小值(包含) + /// 随机整数的最大值(不包含) /// 生成的随机整数 public static int RandomInt(int min, int max) { - return random.Next(min, max); + return SharedRandom.Next(min, max); } /// /// 生成指定位数的随机数字字符串 + /// 仅包含数字 0-9 /// /// 生成的随机数字字符串的长度 /// 生成的随机数字字符串 public static string RandomDigitString(int length) { - string result = ""; + var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { - result += random.Next(10); + sb.Append(SharedRandom.Next(10)); } - return result; + return sb.ToString(); } /// /// 生成指定位数的随机字母数字字符串 + /// 包含大小写字母 A-Z, a-z 和数字 0-9 /// /// 生成的随机字母数字字符串的长度 /// 生成的随机字母数字字符串 public static string RandomAlphanumericString(int length) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - string result = ""; + var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { - result += chars[random.Next(chars.Length)]; + sb.Append(chars[SharedRandom.Next(chars.Length)]); } - return result; + return sb.ToString(); } /// @@ -59,12 +70,12 @@ public static string RandomAlphanumericString(int length) public static string RandomLetterString(int length) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - string result = ""; + var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { - result += chars[random.Next(chars.Length)]; + sb.Append(chars[SharedRandom.Next(chars.Length)]); } - return result; + return sb.ToString(); } /// @@ -73,7 +84,7 @@ public static string RandomLetterString(int length) /// 生成的随机布尔值 public static bool RandomBool() { - return random.Next(2) == 0; + return SharedRandom.Next(2) == 0; } /// @@ -86,7 +97,7 @@ public static int[] RandomIntArray(int length) int[] result = new int[length]; for (int i = 0; i < length; i++) { - result[i] = random.Next(); + result[i] = SharedRandom.Next(); } return result; } @@ -101,7 +112,7 @@ public static double[] RandomDoubleArray(int length) double[] result = new double[length]; for (int i = 0; i < length; i++) { - result[i] = random.NextDouble(); + result[i] = SharedRandom.NextDouble(); } return result; } @@ -131,7 +142,7 @@ public static string[] RandomStringArray(int length, int strLength) public static DateTime RandomDate(DateTime startDate, DateTime endDate) { TimeSpan timeSpan = endDate - startDate; - TimeSpan newSpan = new TimeSpan(0, 0, random.Next(0, (int)timeSpan.TotalSeconds)); + TimeSpan newSpan = new TimeSpan(0, 0, SharedRandom.Next(0, (int)timeSpan.TotalSeconds)); return startDate + newSpan; } @@ -143,18 +154,20 @@ public static DateTime RandomDate(DateTime startDate, DateTime endDate) public static T RandomEnumValue() { Array values = Enum.GetValues(typeof(T)); - return (T)values.GetValue(random.Next(values.Length)); + return (T)values.GetValue(SharedRandom.Next(values.Length)); } /// /// 获取一个指定范围内的随机整数 + /// 注意:返回值为 [minValue, maxValue] 闭区间,即同时包含最小值和最大值 + /// 与 RandomInt 方法的区别:RandomInt 使用左闭右开区间 [min, max),本方法使用闭区间 /// - /// 最小值 - /// 最大值 + /// 最小值(包含) + /// 最大值(包含) /// 随机整数 public static int GetRandomInt(int minValue, int maxValue) { - return random.Next(minValue, maxValue + 1); + return SharedRandom.Next(minValue, maxValue + 1); } /// @@ -165,7 +178,7 @@ public static int GetRandomInt(int minValue, int maxValue) /// 随机双精度浮点数 public static double GetRandomDouble(double minValue, double maxValue) { - return minValue + (maxValue - minValue) * random.NextDouble(); + return minValue + (maxValue - minValue) * SharedRandom.NextDouble(); } /// @@ -210,14 +223,15 @@ public static T GetRandomElement(IEnumerable source) /// /// 字符串长度 /// 随机数字字符串 + [Obsolete("请使用 RandomDigitString 替代,两者功能相同")] public static string RandomNumberString(int length) { - string result = ""; + var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { - result += random.Next(10).ToString(); + sb.Append(SharedRandom.Next(10)); } - return result; + return sb.ToString(); } /// @@ -225,12 +239,13 @@ public static string RandomNumberString(int length) /// /// 字符串长度 /// 随机字母数字字符串 + [Obsolete("请使用 RandomAlphanumericString 替代,该方法实现较复杂且性能较差")] public static string RandomString(int length) { - string result = ""; + var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { - int code = random.Next(36) + 48; + int code = SharedRandom.Next(36) + 48; if (code >= 58 && code <= 64) { code += 7; @@ -239,9 +254,9 @@ public static string RandomString(int length) { code += 6; } - result += Convert.ToChar(code); + sb.Append(Convert.ToChar(code)); } - return result; + return sb.ToString(); } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/MathCategory/RomanNumeralUtil.cs b/EasyTool.Core/MathCategory/RomanNumeralUtil.cs new file mode 100644 index 0000000..79d6f7a --- /dev/null +++ b/EasyTool.Core/MathCategory/RomanNumeralUtil.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.MathCategory +{ + /// + /// 罗马数字工具类 + /// 提供阿拉伯数字与罗马数字之间的转换 + /// + public static class RomanNumeralUtil + { + private static readonly Dictionary RomanMap = new() + { + { 1000, "M" }, + { 900, "CM" }, + { 500, "D" }, + { 400, "CD" }, + { 100, "C" }, + { 90, "XC" }, + { 50, "L" }, + { 40, "XL" }, + { 10, "X" }, + { 9, "IX" }, + { 5, "V" }, + { 4, "IV" }, + { 1, "I" } + }; + + private static readonly Dictionary RomanValues = new() + { + { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } + }; + + /// + /// 将整数转换为罗马数字 + /// + public static string ToRoman(int number) + { + if (number < 1 || number > 3999) + throw new ArgumentOutOfRangeException(nameof(number), "Number must be between 1 and 3999"); + + var result = new StringBuilder(); + + foreach (var kvp in RomanMap) + { + while (number >= kvp.Key) + { + result.Append(kvp.Value); + number -= kvp.Key; + } + } + + return result.ToString(); + } + + /// + /// 将罗马数字转换为整数 + /// + public static int FromRoman(string roman) + { + if (string.IsNullOrWhiteSpace(roman)) + throw new ArgumentException("Roman numeral cannot be empty"); + + roman = roman.ToUpperInvariant().Trim(); + int result = 0; + int prevValue = 0; + + for (int i = roman.Length - 1; i >= 0; i--) + { + if (!RomanValues.TryGetValue(roman[i], out int value)) + throw new ArgumentException($"Invalid Roman numeral character: {roman[i]}"); + + if (value < prevValue) + result -= value; + else + result += value; + + prevValue = value; + } + + // 验证结果是否有效 + if (ToRoman(result) != roman) + throw new ArgumentException($"Invalid Roman numeral: {roman}"); + + return result; + } + + /// + /// 尝试将罗马数字转换为整数 + /// + public static bool TryParse(string roman, out int result) + { + result = 0; + try + { + result = FromRoman(roman); + return true; + } + catch + { + return false; + } + } + + /// + /// 验证罗马数字是否有效 + /// + public static bool IsValid(string roman) + { + return TryParse(roman, out _); + } + } +} diff --git a/EasyTool.Core/MathCategory/WeightedRandomUtil.cs b/EasyTool.Core/MathCategory/WeightedRandomUtil.cs new file mode 100644 index 0000000..5a3b6cc --- /dev/null +++ b/EasyTool.Core/MathCategory/WeightedRandomUtil.cs @@ -0,0 +1,319 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace EasyTool.MathCategory +{ + /// + /// 加权随机选择工具类 + /// 根据权重随机选择元素 + /// + public static class WeightedRandomUtil + { +#if NET6_0_OR_GREATER + private static Random SharedRandom => Random.Shared; +#else + private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); + private static Random SharedRandom => ThreadLocalRandom.Value!; +#endif + + /// + /// 创建加权随机选择器 + /// + /// 元素类型 + /// 加权随机选择器构建器 + public static WeightedRandomBuilder CreateBuilder() + { + return new WeightedRandomBuilder(); + } + + /// + /// 从字典中按权重随机选择 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 随机选中的元素 + public static T Select(IDictionary items) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + var totalWeight = items.Values.Sum(); + var random = SharedRandom.NextDouble() * totalWeight; + + double cumulative = 0; + foreach (var kvp in items) + { + cumulative += kvp.Value; + if (random < cumulative) + return kvp.Key; + } + + return items.Last().Key; + } + + /// + /// 从列表中按权重随机选择 + /// + /// 元素类型 + /// 元素列表 + /// 权重选择器 + /// 随机选中的元素 + public static T Select(IEnumerable items, Func weightSelector) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + var itemList = items.ToList(); + if (itemList.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + var totalWeight = itemList.Sum(weightSelector); + var random = SharedRandom.NextDouble() * totalWeight; + + double cumulative = 0; + foreach (var item in itemList) + { + cumulative += weightSelector(item); + if (random < cumulative) + return item; + } + + return itemList.Last(); + } + + /// + /// 按权重随机选择多个元素(可重复) + /// + /// 元素类型 + /// 元素和权重的字典 + /// 选择数量 + /// 随机选中的元素列表 + public static List SelectMany(IDictionary items, int count) + { + var result = new List(); + for (int i = 0; i < count; i++) + { + result.Add(Select(items)); + } + return result; + } + + /// + /// 按权重随机选择多个不重复元素 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 选择数量 + /// 随机选中的元素列表 + public static List SelectDistinct(IDictionary items, int count) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + count = Math.Min(count, items.Count); + + var remaining = new Dictionary(items); + var result = new List(); + + for (int i = 0; i < count; i++) + { + var selected = Select(remaining); + result.Add(selected); + remaining.Remove(selected); + } + + return result; + } + + /// + /// 使用别名方法进行O(1)时间复杂度的加权随机选择 + /// 适用于元素数量多、需要频繁选择的场景 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 别名方法选择器 + public static AliasMethodSelector CreateAliasSelector(IDictionary items) + { + return new AliasMethodSelector(items); + } + } + + /// + /// 加权随机选择器构建器 + /// + /// 元素类型 + public class WeightedRandomBuilder + { + private readonly Dictionary _items = new(); + + /// + /// 添加元素 + /// + /// 元素 + /// 权重 + /// 构建器 + public WeightedRandomBuilder Add(T item, double weight) + { + if (weight < 0) + throw new ArgumentOutOfRangeException(nameof(weight), "权重不能为负数"); + + _items[item] = weight; + return this; + } + + /// + /// 添加多个元素 + /// + /// 元素和权重 + /// 构建器 + public WeightedRandomBuilder AddRange(IDictionary items) + { + foreach (var kvp in items) + { + _items[kvp.Key] = kvp.Value; + } + return this; + } + + /// + /// 构建选择器 + /// + /// 选择器 + public Func Build() + { + if (_items.Count == 0) + throw new InvalidOperationException("没有添加任何元素"); + + var items = new Dictionary(_items); + return () => WeightedRandomUtil.Select(items); + } + + /// + /// 构建别名方法选择器(高性能) + /// + /// 别名方法选择器 + public AliasMethodSelector BuildAliasSelector() + { + if (_items.Count == 0) + throw new InvalidOperationException("没有添加任何元素"); + + return new AliasMethodSelector(_items); + } + } + + /// + /// 别名方法选择器(O(1)时间复杂度) + /// + /// 元素类型 + public class AliasMethodSelector + { +#if NET6_0_OR_GREATER + private static Random SharedRandom => Random.Shared; +#else + private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); + private static Random GetSharedRandom() => ThreadLocalRandom.Value!; +#endif + + private readonly T[] _items; + private readonly double[] _probabilities; + private readonly int[] _alias; + private readonly int _count; + + public AliasMethodSelector(IDictionary items) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + _count = items.Count; + _items = items.Keys.ToArray(); + _probabilities = new double[_count]; + _alias = new int[_count]; + + Initialize(items.Values.ToArray()); + } + + private void Initialize(double[] weights) + { + var totalWeight = weights.Sum(); + var scale = _count / totalWeight; + + // 标准化权重 + var scaledWeights = weights.Select(w => w * scale).ToArray(); + + var small = new Queue(); + var large = new Queue(); + + for (int i = 0; i < _count; i++) + { + if (scaledWeights[i] < 1.0) + small.Enqueue(i); + else + large.Enqueue(i); + } + + while (small.Count > 0 && large.Count > 0) + { + var smallIndex = small.Dequeue(); + var largeIndex = large.Dequeue(); + + _probabilities[smallIndex] = scaledWeights[smallIndex]; + _alias[smallIndex] = largeIndex; + + scaledWeights[largeIndex] = scaledWeights[largeIndex] + scaledWeights[smallIndex] - 1.0; + + if (scaledWeights[largeIndex] < 1.0) + small.Enqueue(largeIndex); + else + large.Enqueue(largeIndex); + } + + while (large.Count > 0) + { + _probabilities[large.Dequeue()] = 1.0; + } + + while (small.Count > 0) + { + _probabilities[small.Dequeue()] = 1.0; + } + } + + /// + /// 随机选择一个元素 + /// + /// 选中的元素 + public T Select() + { +#if NET6_0_OR_GREATER + var index = SharedRandom.Next(_count); + var r = SharedRandom.NextDouble(); +#else + var random = GetSharedRandom(); + var index = random.Next(_count); + var r = random.NextDouble(); +#endif + + if (r < _probabilities[index]) + return _items[index]; + else + return _items[_alias[index]]; + } + + /// + /// 选择多个元素 + /// + /// 数量 + /// 选中的元素列表 + public List SelectMany(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Select()); + } + return result; + } + } +} diff --git a/EasyTool.Core/NetCategory/FtpUtil.cs b/EasyTool.Core/NetCategory/FtpUtil.cs new file mode 100644 index 0000000..4b60db5 --- /dev/null +++ b/EasyTool.Core/NetCategory/FtpUtil.cs @@ -0,0 +1,600 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// FTP 文件传输工具类 + /// 提供 FTP 文件上传、下载、删除、列表等功能 + /// + public static class FtpUtil + { + #region 上传方法 + + /// + /// 上传文件到 FTP 服务器 + /// + /// FTP 配置 + /// 本地文件路径 + /// 远程文件路径 + /// 是否成功 + public static bool Upload(FtpConfig config, string localFilePath, string remoteFilePath) + { + if (!File.Exists(localFilePath)) + throw new FileNotFoundException("本地文件不存在", localFilePath); + + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + + using var fileStream = File.OpenRead(localFilePath); + using var requestStream = request.GetRequestStream(); + fileStream.CopyTo(requestStream); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步上传文件到 FTP 服务器 + /// + /// FTP 配置 + /// 本地文件路径 + /// 远程文件路径 + /// 是否成功 + public static async Task UploadAsync(FtpConfig config, string localFilePath, string remoteFilePath) + { + if (!File.Exists(localFilePath)) + throw new FileNotFoundException("本地文件不存在", localFilePath); + + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + + using var fileStream = File.OpenRead(localFilePath); + using var requestStream = await request.GetRequestStreamAsync(); + await fileStream.CopyToAsync(requestStream); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 上传数据到 FTP 服务器 + /// + /// FTP 配置 + /// 数据 + /// 远程文件路径 + /// 是否成功 + public static bool UploadData(FtpConfig config, byte[] data, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + request.ContentLength = data.Length; + + using var requestStream = request.GetRequestStream(); + requestStream.Write(data, 0, data.Length); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步上传数据到 FTP 服务器 + /// + /// FTP 配置 + /// 数据 + /// 远程文件路径 + /// 是否成功 + public static async Task UploadDataAsync(FtpConfig config, byte[] data, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + request.ContentLength = data.Length; + + using var requestStream = await request.GetRequestStreamAsync(); + await requestStream.WriteAsync(data, 0, data.Length); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + #endregion + + #region 下载方法 + + /// + /// 从 FTP 服务器下载文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 本地文件路径 + /// 是否成功 + public static bool Download(FtpConfig config, string remoteFilePath, string localFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var fileStream = File.Create(localFilePath); + responseStream?.CopyTo(fileStream); + + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步从 FTP 服务器下载文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 本地文件路径 + /// 是否成功 + public static async Task DownloadAsync(FtpConfig config, string remoteFilePath, string localFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var fileStream = File.Create(localFilePath); + + if (responseStream != null) + { + await responseStream.CopyToAsync(fileStream); + } + + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 从 FTP 服务器下载数据 + /// + /// FTP 配置 + /// 远程文件路径 + /// 下载数据 + public static byte[] DownloadData(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var memoryStream = new MemoryStream(); + responseStream?.CopyTo(memoryStream); + return memoryStream.ToArray(); + } + + /// + /// 异步从 FTP 服务器下载数据 + /// + /// FTP 配置 + /// 远程文件路径 + /// 下载数据 + public static async Task DownloadDataAsync(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var memoryStream = new MemoryStream(); + + if (responseStream != null) + { + await responseStream.CopyToAsync(memoryStream); + } + + return memoryStream.ToArray(); + } + + /// + /// 下载文件内容为字符串 + /// + /// FTP 配置 + /// 远程文件路径 + /// 编码方式 + /// 文件内容 + public static string DownloadString(FtpConfig config, string remoteFilePath, Encoding? encoding = null) + { + var data = DownloadData(config, remoteFilePath); + encoding ??= Encoding.UTF8; + return encoding.GetString(data); + } + + /// + /// 异步下载文件内容为字符串 + /// + /// FTP 配置 + /// 远程文件路径 + /// 编码方式 + /// 文件内容 + public static async Task DownloadStringAsync(FtpConfig config, string remoteFilePath, Encoding? encoding = null) + { + var data = await DownloadDataAsync(config, remoteFilePath); + encoding ??= Encoding.UTF8; + return encoding.GetString(data); + } + + #endregion + + #region 目录操作 + + /// + /// 列出目录内容 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件列表 + public static List ListDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectoryDetails); + var items = new List(); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = reader.ReadLine()) != null) + { + var item = ParseFtpLine(line); + if (item != null) + { + items.Add(item); + } + } + + return items; + } + + /// + /// 异步列出目录内容 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件列表 + public static async Task> ListDirectoryAsync(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectoryDetails); + var items = new List(); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = await reader.ReadLineAsync()) != null) + { + var item = ParseFtpLine(line); + if (item != null) + { + items.Add(item); + } + } + + return items; + } + + /// + /// 列出目录中的文件名 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件名列表 + public static List ListFileNames(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectory); + var names = new List(); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = reader.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + names.Add(line.Trim()); + } + } + + return names; + } + + /// + /// 创建目录 + /// + /// FTP 配置 + /// 远程目录路径 + /// 是否成功 + public static bool CreateDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.MakeDirectory); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.PathnameCreated; + } + + /// + /// 删除目录 + /// + /// FTP 配置 + /// 远程目录路径 + /// 是否成功 + public static bool DeleteDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.RemoveDirectory); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + #endregion + + #region 文件操作 + + /// + /// 删除文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 是否成功 + public static bool DeleteFile(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DeleteFile); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + /// + /// 重命名文件或目录 + /// + /// FTP 配置 + /// 原路径 + /// 新路径 + /// 是否成功 + public static bool Rename(FtpConfig config, string oldPath, string newPath) + { + var request = CreateRequest(config, oldPath, WebRequestMethods.Ftp.Rename); + request.RenameTo = newPath; + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + /// + /// 检查文件是否存在 + /// + /// FTP 配置 + /// 远程文件路径 + /// 是否存在 + public static bool FileExists(FtpConfig config, string remoteFilePath) + { + try + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetFileSize); + using var response = (FtpWebResponse)request.GetResponse(); + return true; + } + catch (WebException ex) when (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) + { + return false; + } + } + + /// + /// 获取文件大小 + /// + /// FTP 配置 + /// 远程文件路径 + /// 文件大小(字节) + public static long GetFileSize(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetFileSize); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.ContentLength; + } + + /// + /// 获取文件修改时间 + /// + /// FTP 配置 + /// 远程文件路径 + /// 修改时间 + public static DateTime GetLastModified(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetDateTimestamp); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.LastModified; + } + + #endregion + + #region 私有方法 + + private static FtpWebRequest CreateRequest(FtpConfig config, string remotePath, string method) + { + string url = config.Host; + if (!url.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase)) + { + url = "ftp://" + url; + } + if (!url.EndsWith("/") && !remotePath.StartsWith("/")) + { + url += "/"; + } + url += remotePath.TrimStart('/'); + + var request = (FtpWebRequest)WebRequest.Create(url); + request.Method = method; + request.Credentials = new NetworkCredential(config.UserName, config.Password); + request.UseBinary = config.UseBinary; + request.UsePassive = config.UsePassive; + request.EnableSsl = config.EnableSsl; + request.KeepAlive = config.KeepAlive; + request.Timeout = config.Timeout; + + return request; + } + + private static FtpItem? ParseFtpLine(string line) + { + if (string.IsNullOrWhiteSpace(line)) + return null; + + // UNIX 风格: drwxr-xr-x 2 owner group 4096 Jan 1 12:00 name + // Windows 风格: 01-01-24 12:00PM name + // 01-01-24 12:00PM 12345 name + + var item = new FtpItem(); + string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length < 4) + return null; + + // 检查是否为 Windows 风格 + if (parts[0].Contains("-") && parts[1].Contains(":")) + { + // Windows 风格 + if (parts[2] == "") + { + item.IsDirectory = true; + item.Name = string.Join(" ", parts.Skip(3)); + } + else + { + item.IsDirectory = false; + if (long.TryParse(parts[2], out long size)) + { + item.Size = size; + } + item.Name = string.Join(" ", parts.Skip(3)); + } + return item; + } + + // UNIX 风格 + if (parts[0].StartsWith("d")) + { + item.IsDirectory = true; + } + else if (parts[0].StartsWith("-")) + { + item.IsDirectory = false; + // 尝试解析大小 + if (parts.Length > 4 && long.TryParse(parts[4], out long size)) + { + item.Size = size; + } + } + + // 获取文件名(最后一个部分) + item.Name = parts[parts.Length - 1]; + + return item; + } + + #endregion + } + + #region 配置和结果类 + + /// + /// FTP 配置 + /// + public class FtpConfig + { + /// + /// FTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 用户名 + /// + public string UserName { get; set; } = string.Empty; + + /// + /// 密码 + /// + public string Password { get; set; } = string.Empty; + + /// + /// 是否使用二进制模式(默认true) + /// + public bool UseBinary { get; set; } = true; + + /// + /// 是否使用被动模式(默认true) + /// + public bool UsePassive { get; set; } = true; + + /// + /// 是否启用 SSL(默认false) + /// + public bool EnableSsl { get; set; } + + /// + /// 是否保持连接(默认true) + /// + public bool KeepAlive { get; set; } = true; + + /// + /// 超时时间(毫秒,默认30000) + /// + public int Timeout { get; set; } = 30000; + + /// + /// 创建匿名 FTP 配置 + /// + /// FTP 服务器地址 + /// FTP 配置 + public static FtpConfig Anonymous(string host) + { + return new FtpConfig + { + Host = host, + UserName = "anonymous", + Password = "anonymous@anonymous.com" + }; + } + } + + /// + /// FTP 文件项 + /// + public class FtpItem + { + /// + /// 文件名 + /// + public string? Name { get; set; } + + /// + /// 是否为目录 + /// + public bool IsDirectory { get; set; } + + /// + /// 文件大小(字节) + /// + public long Size { get; set; } + + /// + /// 修改时间(如果可用) + /// + public DateTime LastModified { get; set; } + + /// + /// 权限(如果可用) + /// + public string? Permissions { get; set; } + + public override string ToString() + { + return IsDirectory ? $"[{Name}]" : $"{Name} ({Size} bytes)"; + } + } + + #endregion +} diff --git a/EasyTool.Core/NetCategory/HttpUtil.cs b/EasyTool.Core/NetCategory/HttpUtil.cs new file mode 100644 index 0000000..498c258 --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpUtil.cs @@ -0,0 +1,676 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HTTP 请求配置 + /// + public class HttpRequestConfig + { + /// + /// 请求超时时间(毫秒) + /// + public int Timeout { get; set; } = 30000; + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// URL 参数 + /// + public Dictionary QueryParams { get; set; } = new(); + + /// + /// 内容类型 + /// + public string ContentType { get; set; } = "application/json"; + + /// + /// 字符编码 + /// + public Encoding Encoding { get; set; } = Encoding.UTF8; + + /// + /// 是否跟随重定向 + /// + public bool AllowRedirect { get; set; } = true; + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } = 0; + + /// + /// 重试间隔(毫秒) + /// + public int RetryInterval { get; set; } = 1000; + + /// + /// Basic 认证 + /// + public (string Username, string Password)? BasicAuth { get; set; } + + /// + /// Bearer Token + /// + public string? BearerToken { get; set; } + } + + /// + /// HTTP 响应结果 + /// + public class HttpResponse + { + /// + /// 是否成功 + /// + public bool IsSuccess { get; set; } + + /// + /// HTTP 状态码 + /// + public HttpStatusCode StatusCode { get; set; } + + /// + /// 响应内容 + /// + public string Content { get; set; } = string.Empty; + + /// + /// 响应头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 异常 + /// + public Exception? Exception { get; set; } + } + + /// + /// HTTP 响应结果(泛型) + /// + public class HttpResponse : HttpResponse + { + /// + /// 反序列化后的数据 + /// + public T? Data { get; set; } + } + + /// + /// HTTP 工具类 + /// 提供便捷的 HTTP 请求方法 + /// + public static class HttpUtil + { + private static readonly Lazy _sharedClient = new(() => CreateDefaultClient()); + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; + + /// + /// 获取共享的 HttpClient 实例 + /// + public static HttpClient SharedClient => _sharedClient.Value; + + private static HttpClient CreateDefaultClient() + { + var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + AllowAutoRedirect = true, + MaxAutomaticRedirections = 10 + }; + + var client = new HttpClient(handler) + { + Timeout = TimeSpan.FromSeconds(30) + }; + + client.DefaultRequestHeaders.UserAgent.ParseAdd("EasyTool/1.0"); + return client; + } + + #region GET 请求 + + /// + /// 发送 GET 请求 + /// + /// 请求地址 + /// 请求配置(可选) + /// 取消令牌 + /// 响应结果 + public static async Task GetAsync( + string url, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Get, null, config, cancellationToken); + } + + /// + /// 发送 GET 请求并反序列化响应 + /// + /// 响应类型 + /// 请求地址 + /// 请求配置(可选) + /// 取消令牌 + /// 响应结果 + public static async Task> GetAsync( + string url, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Get, null, config, cancellationToken); + } + + /// + /// 发送 GET 请求(同步) + /// + public static HttpResponse Get(string url, HttpRequestConfig? config = null) + { + return GetAsync(url, config).GetAwaiter().GetResult(); + } + + #endregion + + #region POST 请求 + + /// + /// 发送 POST 请求 + /// + /// 请求地址 + /// 请求体 + /// 请求配置(可选) + /// 取消令牌 + /// 响应结果 + public static async Task PostAsync( + string url, + object? body = null, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Post, body, config, cancellationToken); + } + + /// + /// 发送 POST 请求并反序列化响应 + /// + public static async Task> PostAsync( + string url, + object? body = null, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Post, body, config, cancellationToken); + } + + /// + /// 发送 POST 请求(同步) + /// + public static HttpResponse Post(string url, object? body = null, HttpRequestConfig? config = null) + { + return PostAsync(url, body, config).GetAwaiter().GetResult(); + } + + /// + /// 发送 JSON POST 请求 + /// + public static async Task PostJsonAsync( + string url, + T data, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + config ??= new HttpRequestConfig(); + config.ContentType = "application/json"; + return await PostAsync(url, data, config, cancellationToken); + } + + /// + /// 发送表单 POST 请求 + /// + public static async Task PostFormAsync( + string url, + Dictionary formData, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + config ??= new HttpRequestConfig(); + config.ContentType = "application/x-www-form-urlencoded"; + return await PostAsync(url, formData, config, cancellationToken); + } + + #endregion + + #region PUT 请求 + + /// + /// 发送 PUT 请求 + /// + public static async Task PutAsync( + string url, + object? body = null, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Put, body, config, cancellationToken); + } + + /// + /// 发送 PUT 请求并反序列化响应 + /// + public static async Task> PutAsync( + string url, + object? body = null, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Put, body, config, cancellationToken); + } + + /// + /// 发送 PUT 请求(同步) + /// + public static HttpResponse Put(string url, object? body = null, HttpRequestConfig? config = null) + { + return PutAsync(url, body, config).GetAwaiter().GetResult(); + } + + #endregion + + #region DELETE 请求 + + /// + /// 发送 DELETE 请求 + /// + public static async Task DeleteAsync( + string url, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Delete, null, config, cancellationToken); + } + + /// + /// 发送 DELETE 请求并反序列化响应 + /// + public static async Task> DeleteAsync( + string url, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Delete, null, config, cancellationToken); + } + + /// + /// 发送 DELETE 请求(同步) + /// + public static HttpResponse Delete(string url, HttpRequestConfig? config = null) + { + return DeleteAsync(url, config).GetAwaiter().GetResult(); + } + + #endregion + + #region PATCH 请求 + + /// + /// 发送 PATCH 请求 + /// + public static async Task PatchAsync( + string url, + object? body = null, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + return await SendAsync(url, HttpMethod.Patch, body, config, cancellationToken); + } + + /// + /// 发送 PATCH 请求(同步) + /// + public static HttpResponse Patch(string url, object? body = null, HttpRequestConfig? config = null) + { + return PatchAsync(url, body, config).GetAwaiter().GetResult(); + } + + #endregion + + #region 文件操作 + + /// + /// 下载文件 + /// + /// 文件地址 + /// 保存路径 + /// 请求配置(可选) + /// 取消令牌 + /// 是否成功 + public static async Task DownloadFileAsync( + string url, + string savePath, + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + try + { + var directory = Path.GetDirectoryName(savePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var client = CreateClient(config); + using var response = await client.GetAsync(BuildUrl(url, config), cancellationToken); + response.EnsureSuccessStatusCode(); + + using var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None); + await response.Content.CopyToAsync(fs); + return true; + } + catch + { + return false; + } + } + + /// + /// 上传文件 + /// + /// 上传地址 + /// 文件路径 + /// 表单字段名 + /// 请求配置(可选) + /// 取消令牌 + /// 响应结果 + public static async Task UploadFileAsync( + string url, + string filePath, + string fieldName = "file", + HttpRequestConfig? config = null, + CancellationToken cancellationToken = default) + { + try + { + if (!File.Exists(filePath)) + { + return new HttpResponse + { + IsSuccess = false, + ErrorMessage = $"文件不存在: {filePath}" + }; + } + + using var client = CreateClient(config); + using var content = new MultipartFormDataContent(); + + var fileBytes = await File.ReadAllBytesAsync(filePath, cancellationToken); + var fileContent = new ByteArrayContent(fileBytes); + fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(GetMimeType(filePath)); + + content.Add(fileContent, fieldName, Path.GetFileName(filePath)); + + // 添加其他表单字段 + if (config?.QueryParams != null) + { + foreach (var param in config.QueryParams) + { + content.Add(new StringContent(param.Value), param.Key); + } + } + + using var response = await client.PostAsync(BuildUrl(url, config), content, cancellationToken); + var responseContent = await response.Content.ReadAsStringAsync(); + + return new HttpResponse + { + IsSuccess = response.IsSuccessStatusCode, + StatusCode = response.StatusCode, + Content = responseContent + }; + } + catch (Exception ex) + { + return new HttpResponse + { + IsSuccess = false, + ErrorMessage = ex.Message, + Exception = ex + }; + } + } + + #endregion + + #region 核心方法 + + /// + /// 发送 HTTP 请求 + /// + private static async Task SendAsync( + string url, + HttpMethod method, + object? body, + HttpRequestConfig? config, + CancellationToken cancellationToken) + { + var result = new HttpResponse(); + int retryCount = config?.RetryCount ?? 0; + int retryInterval = config?.RetryInterval ?? 1000; + + for (int attempt = 0; attempt <= retryCount; attempt++) + { + try + { + using var client = CreateClient(config); + using var request = CreateRequest(url, method, body, config); + + using var response = await client.SendAsync(request, cancellationToken); + var content = await response.Content.ReadAsStringAsync(); + + result.IsSuccess = response.IsSuccessStatusCode; + result.StatusCode = response.StatusCode; + result.Content = content; + + foreach (var header in response.Headers) + { + result.Headers[header.Key] = string.Join(",", header.Value); + } + + if (result.IsSuccess || attempt == retryCount) + { + return result; + } + } + catch (Exception ex) + { + result.Exception = ex; + result.ErrorMessage = ex.Message; + + if (attempt == retryCount) + { + return result; + } + } + + if (attempt < retryCount) + { + await Task.Delay(retryInterval, cancellationToken); + } + } + + return result; + } + + /// + /// 发送 HTTP 请求并反序列化响应 + /// + private static async Task> SendAsync( + string url, + HttpMethod method, + object? body, + HttpRequestConfig? config, + CancellationToken cancellationToken) + { + var response = await SendAsync(url, method, body, config, cancellationToken); + var result = new HttpResponse + { + IsSuccess = response.IsSuccess, + StatusCode = response.StatusCode, + Content = response.Content, + Headers = response.Headers, + ErrorMessage = response.ErrorMessage, + Exception = response.Exception + }; + + if (response.IsSuccess && !string.IsNullOrEmpty(response.Content)) + { + try + { + result.Data = JsonSerializer.Deserialize(response.Content, _jsonOptions); + } + catch (Exception ex) + { + result.ErrorMessage = $"反序列化失败: {ex.Message}"; + } + } + + return result; + } + + private static HttpClient CreateClient(HttpRequestConfig? config) + { + if (config == null) + { + return _sharedClient.Value; + } + + var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + AllowAutoRedirect = config.AllowRedirect + }; + + var client = new HttpClient(handler) + { + Timeout = TimeSpan.FromMilliseconds(config.Timeout) + }; + + // 添加请求头 + foreach (var header in config.Headers) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + + // Basic 认证 + if (config.BasicAuth.HasValue) + { + var authValue = Convert.ToBase64String( + config.Encoding.GetBytes($"{config.BasicAuth.Value.Username}:{config.BasicAuth.Value.Password}")); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authValue); + } + + // Bearer Token + if (!string.IsNullOrEmpty(config.BearerToken)) + { + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", config.BearerToken); + } + + return client; + } + + private static HttpRequestMessage CreateRequest( + string url, + HttpMethod method, + object? body, + HttpRequestConfig? config) + { + var request = new HttpRequestMessage(method, BuildUrl(url, config)); + + if (body != null) + { + string content; + string contentType = config?.ContentType ?? "application/json"; + + if (body is string str) + { + content = str; + } + else if (body is Dictionary formData && + contentType.Contains("x-www-form-urlencoded")) + { + content = string.Join("&", formData.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}")); + } + else + { + content = JsonSerializer.Serialize(body, _jsonOptions); + } + + request.Content = new StringContent(content, config?.Encoding ?? Encoding.UTF8, contentType); + } + + return request; + } + + private static string BuildUrl(string url, HttpRequestConfig? config) + { + if (config?.QueryParams == null || config.QueryParams.Count == 0) + { + return url; + } + + var queryParts = new List(); + foreach (var param in config.QueryParams) + { + queryParts.Add($"{Uri.EscapeDataString(param.Key)}={Uri.EscapeDataString(param.Value)}"); + } + + var queryString = string.Join("&", queryParts); + return url.Contains('?') ? $"{url}&{queryString}" : $"{url}?{queryString}"; + } + + private static string GetMimeType(string filePath) + { + var ext = Path.GetExtension(filePath).ToLowerInvariant(); + return ext switch + { + ".jpg" or ".jpeg" => "image/jpeg", + ".png" => "image/png", + ".gif" => "image/gif", + ".bmp" => "image/bmp", + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".txt" => "text/plain", + ".zip" => "application/zip", + _ => "application/octet-stream" + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/NetCategory/MailUtil.cs b/EasyTool.Core/NetCategory/MailUtil.cs new file mode 100644 index 0000000..40c6800 --- /dev/null +++ b/EasyTool.Core/NetCategory/MailUtil.cs @@ -0,0 +1,593 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 邮件发送工具类 + /// 支持 SMTP 协议发送邮件,包括附件、HTML 正文、抄送等功能 + /// + public static class MailUtil + { + #region 快捷发送方法 + + /// + /// 发送简单文本邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + public static void Send(SmtpConfig config, string to, string subject, string body) + { + Send(config, new[] { to }, subject, body); + } + + /// + /// 发送简单文本邮件 + /// + /// SMTP 配置 + /// 收件人列表 + /// 主题 + /// 正文 + public static void Send(SmtpConfig config, IEnumerable to, string subject, string body) + { + Send(config, new MailMessageOptions + { + To = to.ToList(), + Subject = subject, + Body = body, + IsBodyHtml = false + }); + } + + /// + /// 发送 HTML 邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// HTML 正文 + public static void SendHtml(SmtpConfig config, string to, string subject, string htmlBody) + { + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = htmlBody, + IsBodyHtml = true + }); + } + + /// + /// 发送带附件的邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + /// 附件文件路径列表 + public static void SendWithAttachments(SmtpConfig config, string to, string subject, string body, params string[] attachments) + { + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = false, + Attachments = attachments.ToList() + }); + } + + #endregion + + #region 完整发送方法 + + /// + /// 发送邮件(完整选项) + /// + /// SMTP 配置 + /// 邮件选项 + public static void Send(SmtpConfig config, MailMessageOptions options) + { + using var message = CreateMessage(config, options); + using var client = CreateClient(config); + client.Send(message); + } + + /// + /// 异步发送邮件 + /// + /// SMTP 配置 + /// 邮件选项 + /// Task + public static async Task SendAsync(SmtpConfig config, MailMessageOptions options) + { + using var message = CreateMessage(config, options); + using var client = CreateClient(config); + await client.SendMailAsync(message); + } + + /// + /// 批量发送邮件 + /// + /// SMTP 配置 + /// 邮件选项列表 + /// 是否并行发送 + /// 发送结果列表 + public static List SendBatch(SmtpConfig config, List messages, bool parallel = false) + { + var results = new List(); + + if (parallel) + { + var tasks = messages.Select(msg => Task.Run(() => + { + try + { + Send(config, msg); + return new SendResult { Success = true, Recipients = msg.To }; + } + catch (Exception ex) + { + return new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }; + } + })).ToArray(); + + Task.WaitAll(tasks); + results = tasks.Select(t => t.Result).ToList(); + } + else + { + foreach (var msg in messages) + { + try + { + Send(config, msg); + results.Add(new SendResult { Success = true, Recipients = msg.To }); + } + catch (Exception ex) + { + results.Add(new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }); + } + } + } + + return results; + } + + /// + /// 批量异步发送邮件 + /// + /// SMTP 配置 + /// 邮件选项列表 + /// 最大并行度 + /// 发送结果列表 + public static async Task> SendBatchAsync(SmtpConfig config, List messages, int maxDegreeOfParallelism = 5) + { + var results = new List(); + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + + var tasks = messages.Select(async msg => + { + await semaphore.WaitAsync(); + try + { + await SendAsync(config, msg); + return new SendResult { Success = true, Recipients = msg.To }; + } + catch (Exception ex) + { + return new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }; + } + finally + { + semaphore.Release(); + } + }); + + results = (await Task.WhenAll(tasks)).ToList(); + return results; + } + + #endregion + + #region 模板发送 + + /// + /// 使用模板发送邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板内容(使用 {key} 占位符) + /// 参数字典 + /// 是否为 HTML 格式 + public static void SendTemplate(SmtpConfig config, string to, string subject, string template, Dictionary parameters, bool isHtml = true) + { + string body = RenderTemplate(template, parameters); + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml + }); + } + + /// + /// 使用模板文件发送邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板文件路径 + /// 参数字典 + public static void SendTemplateFile(SmtpConfig config, string to, string subject, string templatePath, Dictionary parameters) + { + if (!File.Exists(templatePath)) + throw new FileNotFoundException("模板文件不存在", templatePath); + + string template = File.ReadAllText(templatePath, Encoding.UTF8); + bool isHtml = templatePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || + templatePath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase); + + SendTemplate(config, to, subject, template, parameters, isHtml); + } + + #endregion + + #region 私有方法 + + private static SmtpClient CreateClient(SmtpConfig config) + { + var client = new SmtpClient(config.Host, config.Port) + { + EnableSsl = config.EnableSsl, + Timeout = config.Timeout ?? 30000 + }; + + if (!string.IsNullOrEmpty(config.UserName) && !string.IsNullOrEmpty(config.Password)) + { + client.Credentials = new NetworkCredential(config.UserName, config.Password); + } + + return client; + } + + private static MailMessage CreateMessage(SmtpConfig config, MailMessageOptions options) + { + var message = new MailMessage + { + From = new MailAddress(options.From ?? config.DefaultFrom), + Subject = options.Subject, + Body = options.Body, + IsBodyHtml = options.IsBodyHtml, + BodyEncoding = Encoding.UTF8, + SubjectEncoding = Encoding.UTF8, + Priority = options.Priority + }; + + // 添加收件人 + if (options.To != null) + { + foreach (var to in options.To) + { + if (!string.IsNullOrEmpty(to)) + message.To.Add(to); + } + } + + // 添加抄送 + if (options.Cc != null) + { + foreach (var cc in options.Cc) + { + if (!string.IsNullOrEmpty(cc)) + message.CC.Add(cc); + } + } + + // 添加密送 + if (options.Bcc != null) + { + foreach (var bcc in options.Bcc) + { + if (!string.IsNullOrEmpty(bcc)) + message.Bcc.Add(bcc); + } + } + + // 添加回复地址 + if (!string.IsNullOrEmpty(options.ReplyTo)) + { + message.ReplyToList.Add(new MailAddress(options.ReplyTo)); + } + + // 添加附件 + if (options.Attachments != null) + { + foreach (var filePath in options.Attachments) + { + if (File.Exists(filePath)) + { + var attachment = new Attachment(filePath); + attachment.ContentDisposition!.CreationDate = File.GetCreationTime(filePath); + attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(filePath); + attachment.ContentDisposition.ReadDate = File.GetLastAccessTime(filePath); + message.Attachments.Add(attachment); + } + } + } + + // 添加内嵌资源(用于 HTML 邮件中的图片) + if (options.EmbeddedResources != null) + { + foreach (var resource in options.EmbeddedResources) + { + if (File.Exists(resource.Value)) + { + var attachment = new LinkedResource(resource.Value) + { + ContentId = resource.Key + }; + var htmlView = AlternateView.CreateAlternateViewFromString(options.Body, Encoding.UTF8, MediaTypeNames.Text.Html); + htmlView.LinkedResources.Add(attachment); + message.AlternateViews.Add(htmlView); + } + } + } + + // 添加自定义头部 + if (options.Headers != null) + { + foreach (var header in options.Headers) + { + message.Headers.Add(header.Key, header.Value); + } + } + + return message; + } + + private static string RenderTemplate(string template, Dictionary parameters) + { + if (string.IsNullOrEmpty(template) || parameters == null) + return template ?? string.Empty; + + string result = template; + foreach (var kvp in parameters) + { + string placeholder = "{" + kvp.Key + "}"; + string value = kvp.Value?.ToString() ?? string.Empty; + result = result.Replace(placeholder, value); + } + + return result; + } + + #endregion + } + + #region 配置和选项类 + + /// + /// SMTP 配置 + /// + public class SmtpConfig + { + /// + /// SMTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// SMTP 服务器端口(默认25) + /// + public int Port { get; set; } = 25; + + /// + /// 是否启用 SSL + /// + public bool EnableSsl { get; set; } + + /// + /// 用户名 + /// + public string? UserName { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 默认发件人地址 + /// + public string? DefaultFrom { get; set; } + + /// + /// 超时时间(毫秒) + /// + public int? Timeout { get; set; } + + /// + /// 创建 QQ 邮箱配置 + /// + /// QQ 邮箱 + /// 授权码 + /// SMTP 配置 + public static SmtpConfig ForQQ(string userName, string authCode) + { + return new SmtpConfig + { + Host = "smtp.qq.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = authCode, + DefaultFrom = userName + }; + } + + /// + /// 创建 163 邮箱配置 + /// + /// 163 邮箱 + /// 授权码 + /// SMTP 配置 + public static SmtpConfig For163(string userName, string authCode) + { + return new SmtpConfig + { + Host = "smtp.163.com", + Port = 465, + EnableSsl = true, + UserName = userName, + Password = authCode, + DefaultFrom = userName + }; + } + + /// + /// 创建 Gmail 配置 + /// + /// Gmail 地址 + /// 应用专用密码 + /// SMTP 配置 + public static SmtpConfig ForGmail(string userName, string appPassword) + { + return new SmtpConfig + { + Host = "smtp.gmail.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = appPassword, + DefaultFrom = userName + }; + } + + /// + /// 创建 Outlook 配置 + /// + /// Outlook 地址 + /// 密码 + /// SMTP 配置 + public static SmtpConfig ForOutlook(string userName, string password) + { + return new SmtpConfig + { + Host = "smtp-mail.outlook.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = password, + DefaultFrom = userName + }; + } + } + + /// + /// 邮件消息选项 + /// + public class MailMessageOptions + { + /// + /// 发件人(可选,使用配置中的默认值) + /// + public string? From { get; set; } + + /// + /// 收件人列表 + /// + public List? To { get; set; } + + /// + /// 抄送列表 + /// + public List? Cc { get; set; } + + /// + /// 密送列表 + /// + public List? Bcc { get; set; } + + /// + /// 回复地址 + /// + public string? ReplyTo { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 正文 + /// + public string Body { get; set; } = string.Empty; + + /// + /// 是否为 HTML 正文 + /// + public bool IsBodyHtml { get; set; } + + /// + /// 附件文件路径列表 + /// + public List? Attachments { get; set; } + + /// + /// 内嵌资源(ContentId -> 文件路径) + /// + public Dictionary? EmbeddedResources { get; set; } + + /// + /// 自定义邮件头 + /// + public Dictionary? Headers { get; set; } + + /// + /// 邮件优先级 + /// + public MailPriority Priority { get; set; } = MailPriority.Normal; + } + + /// + /// 发送结果 + /// + public class SendResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 收件人列表 + /// + public List? Recipients { get; set; } + + /// + /// 错误信息 + /// + public string? Error { get; set; } + + public override string ToString() + { + return Success + ? $"成功发送至: {string.Join(", ", Recipients ?? new List())}" + : $"发送失败: {Error}"; + } + } + + #endregion +} diff --git a/EasyTool.Core/NetCategory/UserAgentUtil.cs b/EasyTool.Core/NetCategory/UserAgentUtil.cs new file mode 100644 index 0000000..28a39b3 --- /dev/null +++ b/EasyTool.Core/NetCategory/UserAgentUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.NetCategory +{ + /// + /// User-Agent 解析工具类 + /// 用于解析浏览器、操作系统、设备等信息 + /// + public static class UserAgentUtil + { + #region 常见浏览器正则 + + private static readonly Regex BrowserRegex = new( + @"(Edge|Edg|OPR|Opera|Chrome|Safari|Firefox|MSIE|Trident|SamsungBrowser|UCBrowser|QQBrowser|MicroMessenger|WeChat|Alipay|WeiBo|DingTalk)[/\s]?(\d+[.\d]*)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex OsRegex = new( + @"(Windows NT|Windows Phone|Android|iPhone|iPad|iPod|Mac OS X|Linux|Ubuntu|Fedora|FreeBSD|Chrome OS)[/\s]?(\d+[.\d]*)?", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex DeviceRegex = new( + @"(Mobile|Android|iPhone|iPad|iPod|Tablet|Kindle|BlackBerry|PlayBook|Nokia|Samsung|HTC|Motorola|LG|Sony|Xiaomi|Huawei|OPPO|Vivo|OnePlus)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex BotRegex = new( + @"(Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|facebookexternalhit|ia_archiver|Twitterbot|LinkedInBot|Embedly|Quora Link Preview|ShowyouBot|outbrain|pinterest|applebot|SemrushBot|AhrefsBot)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + #endregion + + /// + /// 解析 User-Agent 字符串 + /// + /// User-Agent 字符串 + /// 解析结果 + public static UserAgentInfo Parse(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + { + return new UserAgentInfo + { + Browser = BrowserInfo.Unknown, + Os = OsInfo.Unknown, + Device = DeviceInfo.Unknown, + IsBot = false + }; + } + + return new UserAgentInfo + { + Browser = ParseBrowser(userAgent), + Os = ParseOs(userAgent), + Device = ParseDevice(userAgent), + IsBot = IsBot(userAgent) + }; + } + + /// + /// 解析浏览器信息 + /// + public static BrowserInfo ParseBrowser(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return BrowserInfo.Unknown; + + var match = BrowserRegex.Match(userAgent); + if (!match.Success) + return BrowserInfo.Unknown; + + var name = match.Groups[1].Value.ToLowerInvariant(); + var version = match.Groups[2].Value; + + // 规范化浏览器名称 + var browserName = name switch + { + "edg" or "edge" => "Edge", + "opr" or "opera" => "Opera", + "chrome" => "Chrome", + "safari" => "Safari", + "firefox" => "Firefox", + "msie" or "trident" => "Internet Explorer", + "samsungbrowser" => "Samsung Browser", + "ucbrowser" => "UC Browser", + "qqbrowser" => "QQ Browser", + "micromessenger" or "wechat" => "WeChat", + "alipay" => "Alipay", + "weibo" => "Weibo", + "dingtalk" => "DingTalk", + _ => char.ToUpperInvariant(name[0]) + name.Substring(1) + }; + + return new BrowserInfo + { + Name = browserName, + Version = version, + VersionNumber = ParseVersionNumber(version) + }; + } + + /// + /// 解析操作系统信息 + /// + public static OsInfo ParseOs(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return OsInfo.Unknown; + + var match = OsRegex.Match(userAgent); + if (!match.Success) + return OsInfo.Unknown; + + var name = match.Groups[1].Value.ToLowerInvariant(); + var version = match.Groups[2].Value; + + var osName = name switch + { + "windows nt" => ParseWindowsVersion(version), + "windows phone" => "Windows Phone", + "android" => "Android", + "iphone" or "ipad" or "ipod" => "iOS", + "mac os x" => "macOS", + "linux" => "Linux", + "ubuntu" => "Ubuntu", + "fedora" => "Fedora", + "freebsd" => "FreeBSD", + "chrome os" => "Chrome OS", + _ => char.ToUpperInvariant(name[0]) + name.Substring(1) + }; + + return new OsInfo + { + Name = osName, + Version = version, + VersionNumber = ParseVersionNumber(version) + }; + } + + /// + /// 解析设备信息 + /// + public static DeviceInfo ParseDevice(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return DeviceInfo.Unknown; + + var deviceType = DeviceType.Desktop; + string? vendor = null; + string? model = null; + + // 判断设备类型 + if (userAgent.Contains("Mobile", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPhone", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("Android", StringComparison.OrdinalIgnoreCase) && !userAgent.Contains("Tablet", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.Mobile; + } + else if (userAgent.Contains("Tablet", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPad", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.Tablet; + } + else if (userAgent.Contains("SmartTV", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("TV", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.TV; + } + + // 提取设备厂商 + var match = DeviceRegex.Match(userAgent); + if (match.Success) + { + var matched = match.Groups[1].Value.ToLowerInvariant(); + vendor = matched switch + { + "iphone" or "ipad" or "ipod" => "Apple", + "samsung" => "Samsung", + "huawei" => "Huawei", + "xiaomi" => "Xiaomi", + "oppo" => "OPPO", + "vivo" => "Vivo", + "oneplus" => "OnePlus", + "htc" => "HTC", + "motorola" => "Motorola", + "lg" => "LG", + "sony" => "Sony", + "nokia" => "Nokia", + "blackberry" => "BlackBerry", + "kindle" => "Amazon", + _ => char.ToUpperInvariant(matched[0]) + matched.Substring(1) + }; + } + + // 提取设备型号(简化处理) + var modelMatch = Regex.Match(userAgent, @"\(([^)]+)\)"); + if (modelMatch.Success) + { + var parts = modelMatch.Groups[1].Value.Split(';'); + foreach (var part in parts) + { + var trimmed = part.Trim(); + if (trimmed.Contains("Build") || trimmed.Contains(" ")) + { + model = trimmed.Split(' ')[0]; + break; + } + } + } + + return new DeviceInfo + { + Type = deviceType, + Vendor = vendor, + Model = model + }; + } + + /// + /// 判断是否为机器人/爬虫 + /// + public static bool IsBot(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return BotRegex.IsMatch(userAgent); + } + + /// + /// 判断是否为移动设备 + /// + public static bool IsMobile(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("Mobile", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPhone", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("Android", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 判断是否为微信内置浏览器 + /// + public static bool IsWeChat(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("MicroMessenger", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 判断是否为支付宝内置浏览器 + /// + public static bool IsAlipay(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("Alipay", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 获取浏览器简短描述 + /// + public static string GetBrowserDescription(string? userAgent) + { + var info = Parse(userAgent); + var parts = new System.Collections.Generic.List(); + + if (info.Browser.Name != "Unknown") + { + parts.Add($"{info.Browser.Name} {info.Browser.Version}".Trim()); + } + + if (info.Os.Name != "Unknown") + { + parts.Add($"{info.Os.Name} {info.Os.Version}".Trim()); + } + + if (info.Device.Type != DeviceType.Desktop) + { + parts.Add(info.Device.Type.ToString()); + } + + return string.Join(" / ", parts); + } + + #region 私有方法 + + private static string ParseWindowsVersion(string version) + { + return version switch + { + "10.0" => "Windows 10/11", + "6.3" => "Windows 8.1", + "6.2" => "Windows 8", + "6.1" => "Windows 7", + "6.0" => "Windows Vista", + "5.1" or "5.2" => "Windows XP", + _ => $"Windows NT {version}" + }; + } + + private static Version ParseVersionNumber(string version) + { + if (string.IsNullOrEmpty(version)) + return new Version(0, 0); + + var parts = version.Split('.'); + var major = parts.Length > 0 && int.TryParse(parts[0], out var m) ? m : 0; + var minor = parts.Length > 1 && int.TryParse(parts[1], out var mi) ? mi : 0; + var build = parts.Length > 2 && int.TryParse(parts[2], out var b) ? b : 0; + + return new Version(major, minor, build); + } + + #endregion + } + + #region 数据类 + + /// + /// User-Agent 解析结果 + /// + public class UserAgentInfo + { + /// + /// 浏览器信息 + /// + public BrowserInfo Browser { get; set; } = BrowserInfo.Unknown; + + /// + /// 操作系统信息 + /// + public OsInfo Os { get; set; } = OsInfo.Unknown; + + /// + /// 设备信息 + /// + public DeviceInfo Device { get; set; } = DeviceInfo.Unknown; + + /// + /// 是否为机器人/爬虫 + /// + public bool IsBot { get; set; } + } + + /// + /// 浏览器信息 + /// + public class BrowserInfo + { + /// + /// 浏览器名称 + /// + public string Name { get; set; } = "Unknown"; + + /// + /// 版本字符串 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 版本号 + /// + public Version VersionNumber { get; set; } = new Version(0, 0); + + public static BrowserInfo Unknown => new(); + + public override string ToString() => $"{Name} {Version}".Trim(); + } + + /// + /// 操作系统信息 + /// + public class OsInfo + { + /// + /// 操作系统名称 + /// + public string Name { get; set; } = "Unknown"; + + /// + /// 版本字符串 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 版本号 + /// + public Version VersionNumber { get; set; } = new Version(0, 0); + + public static OsInfo Unknown => new(); + + public override string ToString() => $"{Name} {Version}".Trim(); + } + + /// + /// 设备信息 + /// + public class DeviceInfo + { + /// + /// 设备类型 + /// + public DeviceType Type { get; set; } = DeviceType.Desktop; + + /// + /// 设备厂商 + /// + public string? Vendor { get; set; } + + /// + /// 设备型号 + /// + public string? Model { get; set; } + + public static DeviceInfo Unknown => new(); + + public override string ToString() + { + var parts = new System.Collections.Generic.List { Type.ToString() }; + if (!string.IsNullOrEmpty(Vendor)) parts.Add(Vendor); + if (!string.IsNullOrEmpty(Model)) parts.Add(Model); + return string.Join(" ", parts); + } + } + + /// + /// 设备类型 + /// + public enum DeviceType + { + /// + /// 桌面设备 + /// + Desktop, + + /// + /// 手机 + /// + Mobile, + + /// + /// 平板 + /// + Tablet, + + /// + /// 智能电视 + /// + TV, + + /// + /// 其他 + /// + Other + } + + #endregion +} diff --git a/EasyTool.Core/SystemCategory/ProcessUtil.cs b/EasyTool.Core/SystemCategory/ProcessUtil.cs new file mode 100644 index 0000000..84ff933 --- /dev/null +++ b/EasyTool.Core/SystemCategory/ProcessUtil.cs @@ -0,0 +1,821 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.SystemCategory +{ + /// + /// 进程管理工具类 + /// 提供进程的启动、停止、监控等功能 + /// + public static class ProcessUtil + { + #region 进程启动 + + /// + /// 启动进程 + /// + /// 可执行文件名或路径 + /// 命令行参数 + /// 启动的进程 + public static Process Start(string fileName, string? arguments = null) + { + var startInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments ?? string.Empty, + UseShellExecute = true + }; + + return Process.Start(startInfo) ?? throw new InvalidOperationException($"无法启动进程: {fileName}"); + } + + /// + /// 启动进程并等待完成 + /// + /// 可执行文件名或路径 + /// 命令行参数 + /// 超时时间 + /// 进程退出代码 + public static int StartAndWait(string fileName, string? arguments = null, TimeSpan? timeout = null) + { + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments ?? string.Empty, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + process.Start(); + + if (timeout.HasValue) + { + if (!process.WaitForExit((int)timeout.Value.TotalMilliseconds)) + { + process.Kill(); + throw new TimeoutException($"进程执行超时: {fileName}"); + } + } + else + { + process.WaitForExit(); + } + + return process.ExitCode; + } + + /// + /// 启动进程并获取输出 + /// + /// 可执行文件名或路径 + /// 命令行参数 + /// 超时时间 + /// 执行结果(退出代码、标准输出、标准错误) + public static ProcessResult Execute(string fileName, string? arguments = null, TimeSpan? timeout = null) + { + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments ?? string.Empty, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + StandardOutputEncoding = Encoding.UTF8, + StandardErrorEncoding = Encoding.UTF8 + } + }; + + var outputBuilder = new StringBuilder(); + var errorBuilder = new StringBuilder(); + + process.OutputDataReceived += (sender, e) => + { + if (e.Data != null) + outputBuilder.AppendLine(e.Data); + }; + + process.ErrorDataReceived += (sender, e) => + { + if (e.Data != null) + errorBuilder.AppendLine(e.Data); + }; + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + bool exited; + if (timeout.HasValue) + { + exited = process.WaitForExit((int)timeout.Value.TotalMilliseconds); + if (!exited) + { + process.Kill(); + process.WaitForExit(); + } + } + else + { + process.WaitForExit(); + exited = true; + } + + // 确保异步输出完成 + process.WaitForExit(); + + return new ProcessResult + { + ExitCode = process.ExitCode, + StandardOutput = outputBuilder.ToString(), + StandardError = errorBuilder.ToString(), + TimedOut = !exited + }; + } + + /// + /// 异步执行进程并获取输出 + /// + /// 可执行文件名或路径 + /// 命令行参数 + /// 取消令牌 + /// 执行结果 + public static async Task ExecuteAsync(string fileName, string? arguments = null, CancellationToken cancellationToken = default) + { + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments ?? string.Empty, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + StandardOutputEncoding = Encoding.UTF8, + StandardErrorEncoding = Encoding.UTF8 + }, + EnableRaisingEvents = true + }; + + var outputBuilder = new StringBuilder(); + var errorBuilder = new StringBuilder(); + var tcs = new TaskCompletionSource(); + + process.OutputDataReceived += (sender, e) => + { + if (e.Data != null) + outputBuilder.AppendLine(e.Data); + }; + + process.ErrorDataReceived += (sender, e) => + { + if (e.Data != null) + errorBuilder.AppendLine(e.Data); + }; + + process.Exited += (sender, e) => + { + tcs.TrySetResult(true); + }; + + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + using (cancellationToken.Register(() => + { + try + { + process.Kill(); + } + catch { } + tcs.TrySetCanceled(cancellationToken); + })) + { + await tcs.Task; + } + + return new ProcessResult + { + ExitCode = process.ExitCode, + StandardOutput = outputBuilder.ToString(), + StandardError = errorBuilder.ToString(), + TimedOut = cancellationToken.IsCancellationRequested + }; + } + + /// + /// 以管理员权限启动进程 + /// + /// 可执行文件名或路径 + /// 命令行参数 + /// 启动的进程 + public static Process StartAsAdmin(string fileName, string? arguments = null) + { + var startInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments ?? string.Empty, + Verb = "runas", + UseShellExecute = true + }; + + return Process.Start(startInfo) ?? throw new InvalidOperationException($"无法启动进程: {fileName}"); + } + + #endregion + + #region 进程查找 + + /// + /// 根据名称查找进程 + /// + /// 进程名称(不含扩展名) + /// 进程数组 + public static Process[] FindByName(string processName) + { + return Process.GetProcessesByName(processName); + } + + /// + /// 根据 ID 获取进程 + /// + /// 进程 ID + /// 进程对象 + public static Process? GetById(int processId) + { + try + { + return Process.GetProcessById(processId); + } + catch (ArgumentException) + { + return null; + } + } + + /// + /// 获取所有进程 + /// + /// 进程数组 + public static Process[] GetAll() + { + return Process.GetProcesses(); + } + + /// + /// 检查进程是否在运行 + /// + /// 进程名称 + /// 是否在运行 + public static bool IsRunning(string processName) + { + return Process.GetProcessesByName(processName).Length > 0; + } + + /// + /// 检查进程 ID 是否存在 + /// + /// 进程 ID + /// 是否存在 + public static bool Exists(int processId) + { + try + { + Process.GetProcessById(processId); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 进程终止 + + /// + /// 终止进程 + /// + /// 进程对象 + /// 是否终止整个进程树 + public static void Kill(Process process, bool entireProcessTree = false) + { + if (process == null) + throw new ArgumentNullException(nameof(process)); + + try + { +#if NET5_0_OR_GREATER + process.Kill(entireProcessTree); +#else + process.Kill(); +#endif + } + catch (Win32Exception) + { + // 进程正在终止或无法访问 + } + catch (InvalidOperationException) + { + // 进程已经退出 + } + } + + /// + /// 根据名称终止所有同名进程 + /// + /// 进程名称 + /// 终止的进程数量 + public static int KillByName(string processName) + { + var processes = Process.GetProcessesByName(processName); + int killed = 0; + + foreach (var process in processes) + { + try + { + process.Kill(); + killed++; + } + catch + { + // 忽略终止失败的进程 + } + finally + { + process.Dispose(); + } + } + + return killed; + } + + /// + /// 尝试优雅关闭进程,超时后强制终止 + /// + /// 进程对象 + /// 等待超时时间 + /// 是否优雅关闭 + public static bool CloseOrKill(Process process, TimeSpan timeout) + { + if (process == null) + throw new ArgumentNullException(nameof(process)); + + try + { + process.CloseMainWindow(); + if (process.WaitForExit((int)timeout.TotalMilliseconds)) + { + return true; + } + + process.Kill(); + return false; + } + catch + { + return false; + } + } + + #endregion + + #region 进程信息 + + /// + /// 获取当前进程 + /// + /// 当前进程 + public static Process GetCurrent() + { + return Process.GetCurrentProcess(); + } + + /// + /// 获取进程信息 + /// + /// 进程对象 + /// 进程信息 + public static ProcessInfo GetInfo(Process process) + { + if (process == null) + throw new ArgumentNullException(nameof(process)); + + try + { + process.Refresh(); + return new ProcessInfo + { + Id = process.Id, + ProcessName = process.ProcessName, + MainWindowTitle = process.MainWindowTitle, + StartTime = process.StartTime, + TotalProcessorTime = process.TotalProcessorTime, + WorkingSet64 = process.WorkingSet64, + VirtualMemorySize64 = process.VirtualMemorySize64, + PagedMemorySize64 = process.PagedMemorySize64, + NonpagedSystemMemorySize64 = process.NonpagedSystemMemorySize64, + ThreadsCount = process.Threads.Count, + HandlesCount = process.HandleCount, + Responding = process.Responding + }; + } + catch (Win32Exception) + { + return new ProcessInfo { Id = process.Id, ProcessName = process.ProcessName }; + } + } + + /// + /// 获取进程的命令行参数 + /// + /// 进程 ID + /// 命令行参数 + public static string? GetCommandLine(int processId) + { + try + { + using var process = Process.GetProcessById(processId); + // 在 Windows 上通过 WMI 获取命令行 + // 这里简化实现,返回空 + return null; + } + catch + { + return null; + } + } + + #endregion + + #region 进程监控 + + /// + /// 等待进程退出 + /// + /// 进程对象 + /// 超时时间 + /// 是否在超时前退出 + public static bool WaitForExit(Process process, TimeSpan? timeout = null) + { + if (process == null) + throw new ArgumentNullException(nameof(process)); + + if (timeout.HasValue) + { + return process.WaitForExit((int)timeout.Value.TotalMilliseconds); + } + + process.WaitForExit(); + return true; + } + + /// + /// 异步等待进程退出 + /// + /// 进程对象 + /// 取消令牌 + public static async Task WaitForExitAsync(Process process, CancellationToken cancellationToken = default) + { + if (process == null) + throw new ArgumentNullException(nameof(process)); + + process.EnableRaisingEvents = true; + + var tcs = new TaskCompletionSource(); + process.Exited += (sender, e) => tcs.TrySetResult(true); + + if (process.HasExited) + { + return; + } + + using (cancellationToken.Register(() => tcs.TrySetCanceled())) + { + await tcs.Task; + } + } + + /// + /// 创建进程监控器 + /// + /// 要监控的进程名称 + /// 检查间隔 + /// 进程监控器 + public static ProcessMonitor CreateMonitor(string processName, TimeSpan? interval = null) + { + return new ProcessMonitor(processName, interval ?? TimeSpan.FromSeconds(1)); + } + + #endregion + } + + #region 辅助类 + + /// + /// 进程执行结果 + /// + public class ProcessResult + { + /// + /// 退出代码 + /// + public int ExitCode { get; set; } + + /// + /// 标准输出 + /// + public string StandardOutput { get; set; } = string.Empty; + + /// + /// 标准错误 + /// + public string StandardError { get; set; } = string.Empty; + + /// + /// 是否超时 + /// + public bool TimedOut { get; set; } + + /// + /// 是否成功(退出代码为0) + /// + public bool Success => ExitCode == 0 && !TimedOut; + + public override string ToString() + { + return $"ExitCode: {ExitCode}, Success: {Success}, TimedOut: {TimedOut}"; + } + } + + /// + /// 进程信息 + /// + public class ProcessInfo + { + /// + /// 进程 ID + /// + public int Id { get; set; } + + /// + /// 进程名称 + /// + public string? ProcessName { get; set; } + + /// + /// 主窗口标题 + /// + public string? MainWindowTitle { get; set; } + + /// + /// 启动时间 + /// + public DateTime StartTime { get; set; } + + /// + /// 总处理器时间 + /// + public TimeSpan TotalProcessorTime { get; set; } + + /// + /// 工作集大小(物理内存) + /// + public long WorkingSet64 { get; set; } + + /// + /// 虚拟内存大小 + /// + public long VirtualMemorySize64 { get; set; } + + /// + /// 分页内存大小 + /// + public long PagedMemorySize64 { get; set; } + + /// + /// 非分页系统内存大小 + /// + public long NonpagedSystemMemorySize64 { get; set; } + + /// + /// 线程数 + /// + public int ThreadsCount { get; set; } + + /// + /// 句柄数 + /// + public int HandlesCount { get; set; } + + /// + /// 是否响应 + /// + public bool Responding { get; set; } + + /// + /// 内存使用量(MB) + /// + public double MemoryMB => WorkingSet64 / 1024.0 / 1024.0; + + /// + /// CPU 使用时间(秒) + /// + public double CpuTimeSeconds => TotalProcessorTime.TotalSeconds; + + public override string ToString() + { + return $"[{Id}] {ProcessName} - Memory: {MemoryMB:F2}MB, CPU: {CpuTimeSeconds:F2}s, Threads: {ThreadsCount}"; + } + } + + /// + /// 进程监控器 + /// + public class ProcessMonitor : IDisposable + { + private readonly string _processName; + private readonly TimeSpan _interval; + private Timer? _timer; + private bool _disposed; + private bool _isRunning; + + /// + /// 进程启动事件 + /// + public event EventHandler? ProcessStarted; + + /// + /// 进程退出事件 + /// + public event EventHandler? ProcessExited; + + /// + /// 进程状态变化事件 + /// + public event EventHandler? StatusChanged; + + /// + /// 监控的进程名称 + /// + public string ProcessName => _processName; + + /// + /// 是否正在监控 + /// + public bool IsMonitoring => _isRunning; + + /// + /// 当前运行的进程数量 + /// + public int RunningCount { get; private set; } + + internal ProcessMonitor(string processName, TimeSpan interval) + { + _processName = processName; + _interval = interval; + } + + /// + /// 开始监控 + /// + public void Start() + { + if (_disposed) + throw new ObjectDisposedException(nameof(ProcessMonitor)); + + if (_isRunning) + return; + + _isRunning = true; + RunningCount = Process.GetProcessesByName(_processName).Length; + _timer = new Timer(CheckProcesses, null, _interval, _interval); + } + + /// + /// 停止监控 + /// + public void Stop() + { + if (!_isRunning) + return; + + _isRunning = false; + _timer?.Dispose(); + _timer = null; + } + + private void CheckProcesses(object? state) + { + try + { + var currentProcesses = Process.GetProcessesByName(_processName); + int currentCount = currentProcesses.Length; + + if (currentCount != RunningCount) + { + if (currentCount > RunningCount) + { + // 有新进程启动 + ProcessStarted?.Invoke(this, new ProcessEventArgs + { + ProcessName = _processName, + Count = currentCount + }); + } + else + { + // 有进程退出 + ProcessExited?.Invoke(this, new ProcessEventArgs + { + ProcessName = _processName, + Count = currentCount + }); + } + + StatusChanged?.Invoke(this, new ProcessStatusEventArgs + { + ProcessName = _processName, + PreviousCount = RunningCount, + CurrentCount = currentCount + }); + + RunningCount = currentCount; + } + + foreach (var process in currentProcesses) + { + process.Dispose(); + } + } + catch + { + // 忽略监控过程中的异常 + } + } + + public void Dispose() + { + if (_disposed) + return; + + Stop(); + _disposed = true; + } + } + + /// + /// 进程事件参数 + /// + public class ProcessEventArgs : EventArgs + { + /// + /// 进程名称 + /// + public string? ProcessName { get; set; } + + /// + /// 当前数量 + /// + public int Count { get; set; } + } + + /// + /// 进程状态事件参数 + /// + public class ProcessStatusEventArgs : EventArgs + { + /// + /// 进程名称 + /// + public string? ProcessName { get; set; } + + /// + /// 之前的数量 + /// + public int PreviousCount { get; set; } + + /// + /// 当前的数量 + /// + public int CurrentCount { get; set; } + } + + #endregion +} diff --git a/EasyTool.Core/TextCategory/CaptchaUtil.cs b/EasyTool.Core/TextCategory/CaptchaUtil.cs new file mode 100644 index 0000000..5cf0433 --- /dev/null +++ b/EasyTool.Core/TextCategory/CaptchaUtil.cs @@ -0,0 +1,317 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 验证码类型 + /// + public enum CaptchaType + { + /// + /// 纯数字 + /// + Numeric, + + /// + /// 纯字母 + /// + Alpha, + + /// + /// 字母数字混合 + /// + Alphanumeric, + + /// + /// 算术运算 + /// + Arithmetic + } + + /// + /// 验证码结果 + /// + public class CaptchaResult + { + /// + /// 验证码文本(算术验证码为答案) + /// + public string Code { get; set; } = string.Empty; + + /// + /// 验证码图片(Base64格式) + /// + public string ImageBase64 { get; set; } = string.Empty; + + /// + /// 验证码图片(字节数组) + /// + public byte[] ImageBytes { get; set; } = Array.Empty(); + + /// + /// 算术表达式(仅算术验证码) + /// + public string? Expression { get; set; } + } + + /// + /// 图形验证码工具类 + /// 使用简单的图形绘制生成验证码 + /// + public static class CaptchaUtil + { + private static readonly char[] NumericChars = "0123456789".ToCharArray(); + private static readonly char[] AlphaChars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz".ToCharArray(); + private static readonly char[] AlphanumericChars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz".ToCharArray(); + private static readonly char[] OperatorChars = "+-x".ToCharArray(); + + private static readonly string[] Colors = { + "#2E4057", "#048A81", "#54C6EB", "#8EE3EF", "#F7717D", + "#6B4E71", "#3D5A80", "#98C1D9", "#E0FBFC", "#EE6C4D" + }; + + private static readonly Random _random = new(); + + /// + /// 生成验证码 + /// + /// 验证码长度(默认4) + /// 验证码类型 + /// 图片宽度(默认120) + /// 图片高度(默认40) + /// 验证码结果 + public static CaptchaResult Generate( + int length = 4, + CaptchaType type = CaptchaType.Alphanumeric, + int width = 120, + int height = 40) + { + string code; + string? expression = null; + + if (type == CaptchaType.Arithmetic) + { + (code, expression) = GenerateArithmeticCode(); + } + else + { + code = GenerateCode(length, type); + } + + var bytes = GenerateImage(code, width, height); + + return new CaptchaResult + { + Code = code, + ImageBytes = bytes, + ImageBase64 = Convert.ToBase64String(bytes), + Expression = expression + }; + } + + /// + /// 生成验证码并返回 Base64 图片 + /// + /// 验证码长度 + /// 验证码类型 + /// 图片宽度 + /// 图片高度 + /// Base64 格式图片 + public static string GenerateBase64( + int length = 4, + CaptchaType type = CaptchaType.Alphanumeric, + int width = 120, + int height = 40) + { + return Generate(length, type, width, height).ImageBase64; + } + + /// + /// 生成验证码并返回字节数组 + /// + /// 验证码文本 + /// 图片宽度 + /// 图片高度 + /// PNG 图片字节数组 + public static byte[] GenerateImage(string code, int width = 120, int height = 40) + { + // 使用简单的 SVG 方式生成验证码图片 + // 这种方式不依赖外部库,兼容性好 + var svg = GenerateSvg(code, width, height); + return Encoding.UTF8.GetBytes(svg); + } + + /// + /// 生成 SVG 格式的验证码图片 + /// + private static string GenerateSvg(string code, int width, int height) + { + var sb = new StringBuilder(); + sb.AppendLine($""); + + // 背景 + sb.AppendLine($""); + + // 干扰线 + for (int i = 0; i < 6; i++) + { + var x1 = _random.Next(width); + var y1 = _random.Next(height); + var x2 = _random.Next(width); + var y2 = _random.Next(height); + sb.AppendLine($""); + } + + // 干扰点 + for (int i = 0; i < 50; i++) + { + var x = _random.Next(width); + var y = _random.Next(height); + sb.AppendLine($""); + } + + // 文字 + int charWidth = width / (code.Length + 1); + for (int i = 0; i < code.Length; i++) + { + var x = charWidth * (i + 1); + var y = height / 2 + _random.Next(-5, 5); + var fontSize = 20 + _random.Next(-3, 3); + var rotation = _random.Next(-20, 20); + var color = GetRandomColor(false); + + sb.AppendLine($"{code[i]}"); + } + + sb.AppendLine(""); + return sb.ToString(); + } + + /// + /// 生成验证码文本 + /// + private static string GenerateCode(int length, CaptchaType type) + { + var chars = type switch + { + CaptchaType.Numeric => NumericChars, + CaptchaType.Alpha => AlphaChars, + CaptchaType.Alphanumeric => AlphanumericChars, + _ => AlphanumericChars + }; + + var sb = new StringBuilder(length); + for (int i = 0; i < length; i++) + { + sb.Append(chars[_random.Next(chars.Length)]); + } + return sb.ToString(); + } + + /// + /// 生成算术验证码 + /// + private static (string Answer, string Expression) GenerateArithmeticCode() + { + int a = _random.Next(1, 20); + int b = _random.Next(1, 20); + var op = OperatorChars[_random.Next(OperatorChars.Length)]; + + int answer; + string expression; + + switch (op) + { + case '+': + answer = a + b; + expression = $"{a} + {b} = ?"; + break; + case '-': + // 确保结果为正 + if (a < b) (a, b) = (b, a); + answer = a - b; + expression = $"{a} - {b} = ?"; + break; + case 'x': + a = _random.Next(1, 10); + b = _random.Next(1, 10); + answer = a * b; + expression = $"{a} × {b} = ?"; + break; + default: + answer = a + b; + expression = $"{a} + {b} = ?"; + break; + } + + return (answer.ToString(), expression); + } + + /// + /// 获取随机颜色 + /// + private static string GetRandomColor(bool light = false) + { + if (light) + { + // 浅色背景 + var r = 200 + _random.Next(56); + var g = 200 + _random.Next(56); + var b = 200 + _random.Next(56); + return $"rgb({r},{g},{b})"; + } + else + { + // 深色文字/干扰 + return Colors[_random.Next(Colors.Length)]; + } + } + + /// + /// 验证码校验(忽略大小写) + /// + /// 用户输入 + /// 正确验证码 + /// 是否忽略大小写 + /// 是否匹配 + public static bool Verify(string input, string code, bool ignoreCase = true) + { + if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(code)) + return false; + + return ignoreCase + ? string.Equals(input, code, StringComparison.OrdinalIgnoreCase) + : input == code; + } + + /// + /// 生成指定长度的随机数字验证码 + /// + /// 长度 + /// 验证码 + public static string GenerateNumericCode(int length = 6) + { + return GenerateCode(length, CaptchaType.Numeric); + } + + /// + /// 生成短信验证码(6位数字) + /// + /// 6位数字验证码 + public static string GenerateSmsCode() + { + return GenerateNumericCode(6); + } + + /// + /// 生成邮箱验证码(6位数字) + /// + /// 6位数字验证码 + public static string GenerateEmailCode() + { + return GenerateNumericCode(6); + } + } +} diff --git a/EasyTool.Core/TextCategory/DiffUtil.cs b/EasyTool.Core/TextCategory/DiffUtil.cs new file mode 100644 index 0000000..f7a3447 --- /dev/null +++ b/EasyTool.Core/TextCategory/DiffUtil.cs @@ -0,0 +1,353 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.TextCategory +{ + /// + /// 文本差异比较工具类 + /// 提供文本差异计算和显示功能 + /// + public static class DiffUtil + { + /// + /// 比较两个文本的差异 + /// + public static List Compare(string oldText, string newText, bool ignoreCase = false, bool ignoreWhitespace = false) + { + if (ignoreWhitespace) + { + oldText = System.Text.RegularExpressions.Regex.Replace(oldText ?? "", @"\s+", " ").Trim(); + newText = System.Text.RegularExpressions.Regex.Replace(newText ?? "", @"\s+", " ").Trim(); + } + + var oldLines = (oldText ?? "").Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None); + var newLines = (newText ?? "").Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None); + + return CompareLines(oldLines, newLines, ignoreCase); + } + + /// + /// 比较两个行数组的差异 + /// + public static List CompareLines(string[] oldLines, string[] newLines, bool ignoreCase = false) + { + var diffs = new List(); + + // 使用LCS算法 + var lcs = ComputeLCS(oldLines, newLines, ignoreCase); + + int oldIndex = 0, newIndex = 0, lcsIndex = 0; + + while (oldIndex < oldLines.Length || newIndex < newLines.Length) + { + if (lcsIndex < lcs.Count) + { + var lcsItem = lcs[lcsIndex]; + + // 处理删除 + while (oldIndex < lcsItem.OldIndex) + { + diffs.Add(new DiffItem(DiffType.Deleted, oldLines[oldIndex], oldIndex, -1)); + oldIndex++; + } + + // 处理新增 + while (newIndex < lcsItem.NewIndex) + { + diffs.Add(new DiffItem(DiffType.Added, newLines[newIndex], -1, newIndex)); + newIndex++; + } + + // 相同行 + diffs.Add(new DiffItem(DiffType.Unchanged, oldLines[oldIndex], oldIndex, newIndex)); + oldIndex++; + newIndex++; + lcsIndex++; + } + else + { + // 处理剩余 + while (oldIndex < oldLines.Length) + { + diffs.Add(new DiffItem(DiffType.Deleted, oldLines[oldIndex], oldIndex, -1)); + oldIndex++; + } + + while (newIndex < newLines.Length) + { + diffs.Add(new DiffItem(DiffType.Added, newLines[newIndex], -1, newIndex)); + newIndex++; + } + } + } + + return diffs; + } + + private static List ComputeLCS(string[] oldLines, string[] newLines, bool ignoreCase) + { + int m = oldLines.Length; + int n = newLines.Length; + + int[,] dp = new int[m + 1, n + 1]; + + StringComparison comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + if (string.Equals(oldLines[i - 1], newLines[j - 1], comparison)) + { + dp[i, j] = dp[i - 1, j - 1] + 1; + } + else + { + dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]); + } + } + } + + // 回溯 + var result = new List(); + int x = m, y = n; + + while (x > 0 && y > 0) + { + if (string.Equals(oldLines[x - 1], newLines[y - 1], comparison)) + { + result.Add(new LCSItem(x - 1, y - 1)); + x--; y--; + } + else if (dp[x - 1, y] > dp[x, y - 1]) + { + x--; + } + else + { + y--; + } + } + + result.Reverse(); + return result; + } + + /// + /// 生成统一格式的差异 + /// + public static string ToUnifiedDiff(List diffs, string oldFile = "a/file", string newFile = "b/file", int contextLines = 3) + { + var sb = new System.Text.StringBuilder(); + sb.AppendLine($"--- {oldFile}"); + sb.AppendLine($"+++ {newFile}"); + + int i = 0; + while (i < diffs.Count) + { + // 找到变化块 + if (diffs[i].Type != DiffType.Unchanged) + { + // 计算块的上下文 + int start = Math.Max(0, i - contextLines); + int end = i; + + // 找到块的结束 + while (end < diffs.Count && diffs[end].Type != DiffType.Unchanged) + end++; + + end = Math.Min(diffs.Count, end + contextLines); + + // 计算行号范围 + int oldStart = -1, oldCount = 0; + int newStart = -1, newCount = 0; + + for (int j = start; j < end; j++) + { + if (diffs[j].OldLineNumber >= 0) + { + if (oldStart < 0) oldStart = diffs[j].OldLineNumber; + oldCount++; + } + if (diffs[j].NewLineNumber >= 0) + { + if (newStart < 0) newStart = diffs[j].NewLineNumber; + newCount++; + } + } + + if (oldStart < 0) oldStart = 0; + if (newStart < 0) newStart = 0; + + sb.AppendLine($"@@ -{oldStart + 1},{oldCount} +{newStart + 1},{newCount} @@"); + + for (int j = start; j < end; j++) + { + string prefix = diffs[j].Type switch + { + DiffType.Added => "+", + DiffType.Deleted => "-", + _ => " " + }; + sb.AppendLine(prefix + diffs[j].Content); + } + + i = end; + } + else + { + i++; + } + } + + return sb.ToString(); + } + + /// + /// 应用差异补丁 + /// + public static string ApplyPatch(string original, List diffs) + { + var lines = (original ?? "").Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None); + var result = new List(); + int lineIndex = 0; + + foreach (var diff in diffs) + { + switch (diff.Type) + { + case DiffType.Unchanged: + if (lineIndex < lines.Length) + { + result.Add(lines[lineIndex]); + lineIndex++; + } + break; + + case DiffType.Deleted: + lineIndex++; // 跳过旧行 + break; + + case DiffType.Added: + result.Add(diff.Content); + break; + } + } + + // 添加剩余行 + while (lineIndex < lines.Length) + { + result.Add(lines[lineIndex]); + lineIndex++; + } + + return string.Join(Environment.NewLine, result); + } + + /// + /// 计算差异统计 + /// + public static DiffStats GetStats(List diffs) + { + int added = 0, deleted = 0, unchanged = 0; + + foreach (var diff in diffs) + { + switch (diff.Type) + { + case DiffType.Added: added++; break; + case DiffType.Deleted: deleted++; break; + case DiffType.Unchanged: unchanged++; break; + } + } + + return new DiffStats(added, deleted, unchanged); + } + } + + /// + /// 差异类型 + /// + public enum DiffType + { + /// 未变化 + Unchanged, + /// 新增 + Added, + /// 删除 + Deleted + } + + /// + /// 差异项 + /// + public class DiffItem + { + /// 差异类型 + public DiffType Type { get; } + /// 内容 + public string Content { get; } + /// 旧文件行号(-1表示不存在) + public int OldLineNumber { get; } + /// 新文件行号(-1表示不存在) + public int NewLineNumber { get; } + + public DiffItem(DiffType type, string content, int oldLineNumber, int newLineNumber) + { + Type = type; + Content = content; + OldLineNumber = oldLineNumber; + NewLineNumber = newLineNumber; + } + + public override string ToString() + { + string symbol = Type switch + { + DiffType.Added => "+", + DiffType.Deleted => "-", + _ => " " + }; + return $"{symbol} {Content}"; + } + } + + /// + /// 差异统计 + /// + public class DiffStats + { + /// 新增行数 + public int AddedLines { get; } + /// 删除行数 + public int DeletedLines { get; } + /// 未变化行数 + public int UnchangedLines { get; } + /// 总变化行数 + public int TotalChanges => AddedLines + DeletedLines; + + public DiffStats(int addedLines, int deletedLines, int unchangedLines) + { + AddedLines = addedLines; + DeletedLines = deletedLines; + UnchangedLines = unchangedLines; + } + + public override string ToString() + { + return $"+{AddedLines} -{DeletedLines} ={UnchangedLines}"; + } + } + + internal class LCSItem + { + public int OldIndex { get; } + public int NewIndex { get; } + + public LCSItem(int oldIndex, int newIndex) + { + OldIndex = oldIndex; + NewIndex = newIndex; + } + } +} diff --git a/EasyTool.Core/TextCategory/HtmlUtil.cs b/EasyTool.Core/TextCategory/HtmlUtil.cs new file mode 100644 index 0000000..3f52c0f --- /dev/null +++ b/EasyTool.Core/TextCategory/HtmlUtil.cs @@ -0,0 +1,608 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// HTML 工具类 + /// 提供 HTML 转义、清理、提取等功能 + /// + public static class HtmlUtil + { + #region 常量 + + /// + /// HTML 实体编码映射 + /// + private static readonly Dictionary HtmlEntities = new Dictionary + { + { " ", " " }, { "<", "<" }, { ">", ">" }, + { "&", "&" }, { """, "\"" }, { "'", "'" }, + { "©", "©" }, { "®", "®" }, { "™", "™" }, + { "–", "–" }, { "—", "—" }, { "‘", "'" }, + { "’", "'" }, { "“", "\"" }, { "”", "\"" }, + { "•", "•" }, { "…", "…" }, { "°", "°" }, + { "±", "±" }, { "×", "×" }, { "÷", "÷" }, + { "€", "€" }, { "£", "£" }, { "¥", "¥" }, + { "¢", "¢" }, { "§", "§" }, { "¶", "¶" }, + { "†", "†" }, { "‡", "‡" }, { "‰", "‰" }, + { "«", "«" }, { "»", "»" }, { "¡", "¡" }, + { "¿", "¿" }, { "µ", "µ" }, { "·", "·" } + }; + + /// + /// HTML 标签正则 + /// + private static readonly Regex HtmlTagRegex = new Regex(@"<[^>]+>", RegexOptions.Compiled); + + /// + /// 脚本标签正则 + /// + private static readonly Regex ScriptRegex = new Regex(@"]*>[\s\S]*?", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 样式标签正则 + /// + private static readonly Regex StyleRegex = new Regex(@"]*>[\s\S]*?", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// HTML 注释正则 + /// + private static readonly Regex CommentRegex = new Regex(@"", RegexOptions.Compiled); + + /// + /// 数字实体正则 + /// + private static readonly Regex NumericEntityRegex = new Regex(@"&#(\d+);", RegexOptions.Compiled); + + /// + /// 十六进制实体正则 + /// + private static readonly Regex HexEntityRegex = new Regex(@"&#[xX]([0-9a-fA-F]+);", RegexOptions.Compiled); + + /// + /// 安全标签白名单 + /// + private static readonly HashSet SafeTags = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "p", "br", "hr", "h1", "h2", "h3", "h4", "h5", "h6", + "div", "span", "a", "img", "ul", "ol", "li", "table", "tr", "td", "th", "thead", "tbody", + "strong", "em", "b", "i", "u", "s", "sub", "sup", + "blockquote", "pre", "code", "kbd", "samp", "var" + }; + + #endregion + + #region 转义方法 + + /// + /// HTML 转义 + /// + /// 原始文本 + /// 转义后的文本 + public static string Escape(string? text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var sb = new StringBuilder(text.Length * 2); + foreach (char c in text) + { + switch (c) + { + case '<': + sb.Append("<"); + break; + case '>': + sb.Append(">"); + break; + case '&': + sb.Append("&"); + break; + case '"': + sb.Append("""); + break; + case '\'': + sb.Append("'"); + break; + default: + sb.Append(c); + break; + } + } + return sb.ToString(); + } + + /// + /// HTML 反转义 + /// + /// HTML 文本 + /// 反转义后的文本 + public static string Unescape(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 先处理数字实体 + result = NumericEntityRegex.Replace(result, match => + { + if (int.TryParse(match.Groups[1].Value, out int code)) + { + return ((char)code).ToString(); + } + return match.Value; + }); + + // 处理十六进制实体 + result = HexEntityRegex.Replace(result, match => + { + try + { + int code = Convert.ToInt32(match.Groups[1].Value, 16); + return ((char)code).ToString(); + } + catch + { + return match.Value; + } + }); + + // 处理命名实体 + foreach (var entity in HtmlEntities) + { + result = result.Replace(entity.Key, entity.Value); + } + + return result; + } + + /// + /// URL 编码 + /// + /// 原始文本 + /// 编码方式(默认UTF-8) + /// 编码后的文本 + public static string UrlEncode(string? text, Encoding? encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return WebUtility.UrlEncode(text); + } + + /// + /// URL 解码 + /// + /// 编码的文本 + /// 解码后的文本 + public static string UrlDecode(string? encodedText) + { + if (string.IsNullOrEmpty(encodedText)) + return string.Empty; + + return WebUtility.UrlDecode(encodedText); + } + + #endregion + + #region 清理方法 + + /// + /// 移除所有 HTML 标签 + /// + /// HTML 文本 + /// 纯文本 + public static string StripTags(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + return HtmlTagRegex.Replace(html, string.Empty); + } + + /// + /// 清理 HTML(移除脚本、样式、注释) + /// + /// HTML 文本 + /// 清理后的 HTML + public static string Clean(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 移除脚本 + result = ScriptRegex.Replace(result, string.Empty); + + // 移除样式 + result = StyleRegex.Replace(result, string.Empty); + + // 移除注释 + result = CommentRegex.Replace(result, string.Empty); + + return result; + } + + /// + /// 清理 HTML 并保留安全标签 + /// + /// HTML 文本 + /// 允许的标签(为空则使用默认白名单) + /// 清理后的 HTML + public static string SafeClean(string? html, IEnumerable? allowedTags = null) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + // 先进行基本清理 + string result = Clean(html); + + // 获取允许的标签集合 + var allowed = allowedTags != null + ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) + : SafeTags; + + // 移除不允许的标签(保留内容) + result = Regex.Replace(result, @"]*>", match => + { + string tagName = match.Groups[1].Value; + if (allowed.Contains(tagName)) + { + return match.Value; + } + return string.Empty; + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 移除所有 HTML 并获取纯文本 + /// + /// HTML 文本 + /// 纯文本 + public static string ToPlainText(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = Clean(html); + + // 处理常见块级元素 + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"

", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", " ", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", " ", RegexOptions.IgnoreCase); + + // 移除剩余标签 + result = StripTags(result); + + // 反转义 HTML 实体 + result = Unescape(result); + + // 清理多余空白 + result = Regex.Replace(result, @"[ \t]+", " "); + result = Regex.Replace(result, @"\n\s*\n", "\n\n"); + result = result.Trim(); + + return result; + } + + #endregion + + #region 提取方法 + + /// + /// 提取所有链接 + /// + /// HTML 文本 + /// 链接列表(URL, 文本) + public static List<(string Url, string Text)> ExtractLinks(string? html) + { + var links = new List<(string, string)>(); + + if (string.IsNullOrEmpty(html)) + return links; + + var regex = new Regex(@"]+href=""([^""]+)""[^>]*>([^<]*)", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string url = match.Groups[1].Value; + string text = Unescape(match.Groups[2].Value).Trim(); + links.Add((url, text)); + } + + return links; + } + + /// + /// 提取所有图片 + /// + /// HTML 文本 + /// 图片列表(URL, Alt) + public static List<(string Src, string Alt)> ExtractImages(string? html) + { + var images = new List<(string, string)>(); + + if (string.IsNullOrEmpty(html)) + return images; + + var regex = new Regex(@"]+src=""([^""]+)""[^>]*(?:alt=""([^""]*)"")?[^>]*/?>", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string src = match.Groups[1].Value; + string alt = match.Groups[2].Success ? match.Groups[2].Value : string.Empty; + images.Add((src, alt)); + } + + return images; + } + + /// + /// 提取页面标题 + /// + /// HTML 文本 + /// 标题 + public static string? ExtractTitle(string? html) + { + if (string.IsNullOrEmpty(html)) + return null; + + var match = Regex.Match(html, @"]*>([^<]*)", RegexOptions.IgnoreCase); + if (match.Success) + { + return Unescape(match.Groups[1].Value).Trim(); + } + return null; + } + + /// + /// 提取 Meta 标签内容 + /// + /// HTML 文本 + /// Meta 名称 + /// 内容 + public static string? ExtractMeta(string? html, string name) + { + if (string.IsNullOrEmpty(html) || string.IsNullOrEmpty(name)) + return null; + + var match = Regex.Match(html, $@"]+name=""{Regex.Escape(name)}""[^>]+content=""([^""]*)""", RegexOptions.IgnoreCase); + if (!match.Success) + { + match = Regex.Match(html, $@"]+content=""([^""]*)""[^>]+name=""{Regex.Escape(name)}""", RegexOptions.IgnoreCase); + } + + if (match.Success) + { + return Unescape(match.Groups[1].Value); + } + return null; + } + + /// + /// 提取所有文本内容 + /// + /// HTML 文本 + /// CSS 选择器(简化版,仅支持标签名) + /// 匹配的文本列表 + public static List ExtractTexts(string? html, string? selector = null) + { + var texts = new List(); + + if (string.IsNullOrEmpty(html)) + return texts; + + if (string.IsNullOrEmpty(selector)) + { + texts.Add(ToPlainText(html)); + return texts; + } + + var regex = new Regex($@"<{Regex.Escape(selector)}[^>]*>([\s\S]*?)", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string text = ToPlainText(match.Groups[1].Value); + if (!string.IsNullOrWhiteSpace(text)) + { + texts.Add(text); + } + } + + return texts; + } + + #endregion + + #region 格式化方法 + + /// + /// 压缩 HTML(移除多余空白) + /// + /// HTML 文本 + /// 压缩后的 HTML + public static string Minify(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 移除注释 + result = CommentRegex.Replace(result, string.Empty); + + // 移除多余空白 + result = Regex.Replace(result, @">\s+<", "><"); + result = Regex.Replace(result, @"\s+", " "); + result = result.Trim(); + + return result; + } + + /// + /// 格式化 HTML(添加缩进) + /// + /// HTML 文本 + /// 缩进字符串(默认两个空格) + /// 格式化后的 HTML + public static string Format(string? html, string indentString = " ") + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + var result = new StringBuilder(); + int indent = 0; + bool inPre = false; + + var tokens = Regex.Split(html, @"(<[^>]+>)"); + + foreach (var token in tokens) + { + if (string.IsNullOrEmpty(token)) + continue; + + // 检测 pre 标签 + if (Regex.IsMatch(token, @"]*>", RegexOptions.IgnoreCase)) + { + inPre = true; + } + else if (Regex.IsMatch(token, @"", RegexOptions.IgnoreCase)) + { + inPre = false; + } + + if (inPre) + { + result.Append(token); + continue; + } + + string trimmed = token.Trim(); + + // 自闭合标签或文本 + if (trimmed.StartsWith("<") && !trimmed.StartsWith("")) + { + // 开始标签 + if (!IsInlineTag(trimmed)) + { + result.AppendLine(); + result.Append(string.Concat(Enumerable.Repeat(indentString, indent))); + indent++; + } + result.Append(trimmed); + } + else if (trimmed.StartsWith(" + /// 检查是否为有效的 HTML 片段 + ///
+ /// HTML 文本 + /// 是否有效 + public static bool IsValidHtml(string? html) + { + if (string.IsNullOrWhiteSpace(html)) + return false; + + // 检查基本 HTML 标签 + return HtmlTagRegex.IsMatch(html); + } + + /// + /// 检查 HTML 标签是否匹配 + /// + /// HTML 文本 + /// 是否匹配 + public static bool AreTagsBalanced(string? html) + { + if (string.IsNullOrEmpty(html)) + return true; + + var stack = new Stack(); + var selfClosing = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "br", "hr", "img", "input", "meta", "link", "area", "base", "col", + "embed", "param", "source", "track", "wbr" + }; + + var matches = Regex.Matches(html, @"]*>", RegexOptions.IgnoreCase); + + foreach (Match match in matches) + { + string tagName = match.Groups[1].Value.ToLower(); + + if (selfClosing.Contains(tagName)) + continue; + + if (match.Value.StartsWith("")) + { + // 开始标签 + stack.Push(tagName); + } + } + + return stack.Count == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/LevenshteinUtil.cs b/EasyTool.Core/TextCategory/LevenshteinUtil.cs new file mode 100644 index 0000000..83f4e7e --- /dev/null +++ b/EasyTool.Core/TextCategory/LevenshteinUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 编辑距离工具类 + /// 提供各种字符串相似度计算方法 + /// + public static class LevenshteinUtil + { + /// + /// 计算Levenshtein编辑距离 + /// + public static int Distance(string source, string target) + { + if (string.IsNullOrEmpty(source)) + return target?.Length ?? 0; + if (string.IsNullOrEmpty(target)) + return source.Length; + + int m = source.Length; + int n = target.Length; + + // 优化空间:只使用两行 + int[] prev = new int[n + 1]; + int[] curr = new int[n + 1]; + + // 初始化第一行 + for (int j = 0; j <= n; j++) + prev[j] = j; + + for (int i = 1; i <= m; i++) + { + curr[0] = i; + + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + + curr[j] = Math.Min( + Math.Min(prev[j] + 1, curr[j - 1] + 1), + prev[j - 1] + cost); + } + + // 交换行 + (prev, curr) = (curr, prev); + } + + return prev[n]; + } + + /// + /// 计算相似度(0-1) + /// + public static double Similarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + + int maxLen = Math.Max(source.Length, target.Length); + if (maxLen == 0) return 1.0; + + int distance = Distance(source, target); + return 1.0 - (double)distance / maxLen; + } + + /// + /// 获取编辑操作序列 + /// + public static List GetEditOperations(string source, string target) + { + var operations = new List(); + + if (string.IsNullOrEmpty(source)) + { + for (int i = 0; i < (target?.Length ?? 0); i++) + operations.Add(new EditOperation(EditType.Insert, i, target[i].ToString())); + return operations; + } + + if (string.IsNullOrEmpty(target)) + { + for (int i = 0; i < source.Length; i++) + operations.Add(new EditOperation(EditType.Delete, i, source[i].ToString())); + return operations; + } + + int m = source.Length; + int n = target.Length; + + // 构建完整DP表 + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 0; i <= m; i++) dp[i, 0] = i; + for (int j = 0; j <= n; j++) dp[0, j] = j; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + dp[i, j] = Math.Min( + Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), + dp[i - 1, j - 1] + cost); + } + } + + // 回溯获取操作 + int x = m, y = n; + while (x > 0 || y > 0) + { + if (x > 0 && y > 0 && source[x - 1] == target[y - 1]) + { + operations.Add(new EditOperation(EditType.Match, x - 1, source[x - 1].ToString())); + x--; y--; + } + else if (x > 0 && y > 0 && dp[x, y] == dp[x - 1, y - 1] + 1) + { + operations.Add(new EditOperation(EditType.Replace, x - 1, source[x - 1].ToString(), target[y - 1].ToString())); + x--; y--; + } + else if (y > 0 && (x == 0 || dp[x, y] == dp[x, y - 1] + 1)) + { + operations.Add(new EditOperation(EditType.Insert, x, target[y - 1].ToString())); + y--; + } + else if (x > 0 && (y == 0 || dp[x, y] == dp[x - 1, y] + 1)) + { + operations.Add(new EditOperation(EditType.Delete, x - 1, source[x - 1].ToString())); + x--; + } + } + + operations.Reverse(); + return operations; + } + + /// + /// Damerau-Levenshtein距离(支持相邻交换) + /// + public static int DamerauLevenshteinDistance(string source, string target) + { + if (string.IsNullOrEmpty(source)) + return target?.Length ?? 0; + if (string.IsNullOrEmpty(target)) + return source.Length; + + int m = source.Length; + int n = target.Length; + + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 0; i <= m; i++) dp[i, 0] = i; + for (int j = 0; j <= n; j++) dp[0, j] = j; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + + dp[i, j] = Math.Min( + Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), + dp[i - 1, j - 1] + cost); + + // 检查相邻交换 + if (i > 1 && j > 1 && + source[i - 1] == target[j - 2] && + source[i - 2] == target[j - 1]) + { + dp[i, j] = Math.Min(dp[i, j], dp[i - 2, j - 2] + cost); + } + } + } + + return dp[m, n]; + } + + /// + /// 计算最长公共子序列长度 + /// + public static int LongestCommonSubsequence(string source, string target) + { + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0; + + int m = source.Length; + int n = target.Length; + + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + if (source[i - 1] == target[j - 1]) + { + dp[i, j] = dp[i - 1, j - 1] + 1; + } + else + { + dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]); + } + } + } + + return dp[m, n]; + } + + /// + /// 基于最长公共子序列的相似度 + /// + public static double LCSSimilarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + + int lcs = LongestCommonSubsequence(source, target); + int maxLen = Math.Max(source.Length, target.Length); + + return (double)lcs / maxLen; + } + + /// + /// 计算 Jaro 相似度 + /// + public static double JaroSimilarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + if (source == target) + return 1.0; + + int m = source.Length; + int n = target.Length; + + int matchDistance = Math.Max(m, n) / 2 - 1; + if (matchDistance < 0) matchDistance = 0; + + bool[] sourceMatches = new bool[m]; + bool[] targetMatches = new bool[n]; + + int matches = 0; + int transpositions = 0; + + for (int i = 0; i < m; i++) + { + int start = Math.Max(0, i - matchDistance); + int end = Math.Min(i + matchDistance + 1, n); + + for (int j = start; j < end; j++) + { + if (targetMatches[j] || source[i] != target[j]) + continue; + + sourceMatches[i] = true; + targetMatches[j] = true; + matches++; + break; + } + } + + if (matches == 0) + return 0.0; + + int k = 0; + for (int i = 0; i < m; i++) + { + if (!sourceMatches[i]) + continue; + + while (!targetMatches[k]) + k++; + + if (source[i] != target[k]) + transpositions++; + + k++; + } + + return ((double)matches / m + + (double)matches / n + + (matches - transpositions / 2.0) / matches) / 3.0; + } + + /// + /// 计算 Jaro-Winkler 相似度 + /// + public static double JaroWinklerSimilarity(string source, string target, double scalingFactor = 0.1) + { + double jaro = JaroSimilarity(source, target); + + // 计算公共前缀长度(最多4个字符) + int prefixLength = 0; + for (int i = 0; i < Math.Min(Math.Min(source.Length, target.Length), 4); i++) + { + if (source[i] == target[i]) + prefixLength++; + else + break; + } + + return jaro + prefixLength * scalingFactor * (1 - jaro); + } + + /// + /// 模糊匹配搜索 + /// + public static List<(string Item, double Score)> FuzzySearch(string query, IEnumerable items, double threshold = 0.5) + { + return items + .Select(item => (Item: item, Score: JaroWinklerSimilarity(query, item))) + .Where(x => x.Score >= threshold) + .OrderByDescending(x => x.Score) + .ToList(); + } + } + + /// + /// 编辑操作类型 + /// + public enum EditType + { + /// 匹配 + Match, + /// 替换 + Replace, + /// 插入 + Insert, + /// 删除 + Delete + } + + /// + /// 编辑操作 + /// + public class EditOperation + { + /// 操作类型 + public EditType Type { get; } + /// 位置 + public int Position { get; } + /// 原始字符 + public string OldValue { get; } + /// 新字符 + public string NewValue { get; } + + public EditOperation(EditType type, int position, string value, string newValue = null) + { + Type = type; + Position = position; + OldValue = value; + NewValue = newValue ?? value; + } + + public override string ToString() + { + return Type switch + { + EditType.Match => $"Match '{OldValue}' at {Position}", + EditType.Replace => $"Replace '{OldValue}' with '{NewValue}' at {Position}", + EditType.Insert => $"Insert '{NewValue}' at {Position}", + EditType.Delete => $"Delete '{OldValue}' at {Position}", + _ => base.ToString() + }; + } + } +} diff --git a/EasyTool.Core/TextCategory/PinyinUtil.cs b/EasyTool.Core/TextCategory/PinyinUtil.cs new file mode 100644 index 0000000..8178e9e --- /dev/null +++ b/EasyTool.Core/TextCategory/PinyinUtil.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 拼音工具类 + /// 提供汉字转拼音功能 + /// + public static class PinyinUtil + { + /// + /// 获取汉字的拼音 + /// + /// 中文字符串 + /// 分隔符 + /// 拼音字符串 + public static string GetPinyin(string chinese, string separator = "") + { + if (string.IsNullOrEmpty(chinese)) + return string.Empty; + + var result = new StringBuilder(); + + foreach (char c in chinese) + { + string pinyin = GetPinyin(c); + if (result.Length > 0 && !string.IsNullOrEmpty(pinyin)) + result.Append(separator); + result.Append(pinyin); + } + + return result.ToString(); + } + + /// + /// 获取单个汉字的拼音 + /// + public static string GetPinyin(char c) + { + // 非汉字直接返回 + if (c < 0x4E00 || c > 0x9FA5) + return c.ToString(); + + // 查找拼音 + string[] py = GetPinyinArray(c); + return py != null && py.Length > 0 ? py[0] : c.ToString(); + } + + /// + /// 获取汉字的所有拼音(多音字) + /// + public static string[] GetPinyins(char c) + { + if (c < 0x4E00 || c > 0x9FA5) + return new[] { c.ToString() }; + + return GetPinyinArray(c) ?? new[] { c.ToString() }; + } + + /// + /// 获取拼音首字母 + /// + public static string GetFirstPinyinLetter(string chinese) + { + if (string.IsNullOrEmpty(chinese)) + return string.Empty; + + var result = new StringBuilder(); + + foreach (char c in chinese) + { + string pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin) && pinyin.Length > 0) + { + result.Append(char.ToUpper(pinyin[0])); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 获取拼音首字母(简化版,用于排序索引) + /// + public static string GetSimplePinyinInitial(string chinese) + { + if (string.IsNullOrEmpty(chinese)) + return "#"; + + char c = chinese[0]; + + // 非汉字 + if (c < 0x4E00 || c > 0x9FA5) + { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return char.ToUpper(c).ToString(); + return "#"; + } + + string pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin) && pinyin.Length > 0) + { + return char.ToUpper(pinyin[0]).ToString(); + } + + return "#"; + } + + /// + /// 判断字符是否为汉字 + /// + public static bool IsChinese(char c) + { + return c >= 0x4E00 && c <= 0x9FA5; + } + + /// + /// 判断字符串是否全部为汉字 + /// + public static bool IsAllChinese(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + foreach (char c in s) + { + if (!IsChinese(c)) + return false; + } + + return true; + } + + /// + /// 判断字符串是否包含汉字 + /// + public static bool ContainsChinese(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + foreach (char c in s) + { + if (IsChinese(c)) + return true; + } + + return false; + } + + // 简化的拼音表(这里只包含部分常用汉字的拼音) + // 完整实现需要包含所有汉字的拼音映射 + private static readonly Dictionary PinyinMap = InitializePinyinMap(); + + private static Dictionary InitializePinyinMap() + { + var map = new Dictionary(); + + // 常用汉字拼音表(简化版,实际应用需要完整拼音表) + string[] chars = { + "的一是不了在人有我他这个们中来上大为和国地到以说时要就出会可也你对生能而子那得于着下自之年过发后作里用道行所然家种事成方多经么去法学如都同现当没动面起看定天分还进好小部其些主样理心她本前开但因只从想实日军者意无力它与长把机十民第公此已工使情明性知全三又关点正业外将两高间由问很最重并物手应战向头文体政美相见被利什二等产或新己制身果加西斯月话合回特代内信表化老给世位次度门任常先海通教儿原东声提立及比员解水名真论处走义各入几口认条平系气题活尔更别打女变四神总何电数安少报才结反受目太量再感建务做接必场件计管期市直德资命山金指克许统区保至队形社便空决治展马科司五基眼书非则听白却界达光放强即像难且权思王象完设式色路记南品住告类求据程北边死张该交规万取拉格望觉术领共确传师观清今切院让识候带导争运笔志认准许响约英格底仅流端讲乡村消故值收越古史附整改落致令参周农吸获坚单组切界育苦断背细油调灵责供济容质项根议陈拿破仑" + }; + + string[] pinyins = { + "de,yi,shi,bu,liao,zai,ren,you,wo,ta,zhe,ge,men,zhong,lai,shang,da,wei,he,guo,di,dao,yi,shuo,shi,yao,jiu,chu,hui,ke,ye,ni,dui,sheng,neng,er,zi,na,de,yu,zhe,xia,zi,zhi,nian,guo,fa,hou,zuo,li,yong,dao,xing,suo,ran,jia,zhong,shi,cheng,fang,duo,jing,me,qu,fa,xue,ru,dou,tong,xian,dang,mei,dong,mian,qi,kan,ding,tian,fen,hai,jin,hao,xiao,bu,qi,xie,zhu,yang,li,xin,ta,ben,qian,kai,yin,zhi,cong,xiang,shi,ri,jun,zhe,yi,wu,li,ta,yu,chang,ba,ji,shi,min,di,gong,ci,yi,gong,shi,qing,ming,xing,zhi,quan,san,you,guan,dian,zheng,ye,wai,jiang,liang,gao,jian,you,wen,hen,zui,zhong,bing,wu,shou,ying,zhan,xiang,tou,wen,ti,zheng,mei,xiang,jian,bei,li,shi,er,deng,chan,huo,xin,ji,zhi,shen,guo,jia,xi,si,yue,hua,he,hui,te,dai,nei,xin,biao,hua,lao,gei,shi,wei,ci,du,men,ren,chang,xian,hai,tong,jiao,er,yuan,dong,sheng,ti,li,ji,bi,yuan,jie,shui,ming,zhen,lun,chu,zou,yi,ge,ru,ji,kou,ren,tiao,ping,xi,qi,ti,huo,er,geng,bie,da,nv,bian,si,shen,zong,he,dian,shu,an,shao,bao,cai,jie,fan,shou,mu,tai,liang,zai,gan,jian,wu,zuo,jie,bi,chang,jian,ji,guan,qi,shi,zhi,de,zi,ming,shan,jin,zhi,ke,xu,tong,qu,bao,zhi,dui,xing,she,bian,kong,jue,zhi,zhan,ma,ke,si,wu,ji,yan,shu,fei,ze,ting,bai,que,jie,da,guang,fang,qiang,ji,xiang,nan,qie,quan,si,wang,xiang,wan,she,shi,se,lu,ji,nan,pin,zhu,gao,lei,qiu,ju,cheng,bei,bian,si,zhang,gai,jiao,gui,wan,qu,la,ge,wang,jue,shu,ling,gong,que,chuan,shi,guan,qing,jin,qie,yuan,rang,shi,hou,dai,dao,zheng,yun,bi,zhi,ren,zhun,xu,xiang,yue,ying,ge,di,jin,liu,duan,jiang,xiang,cun,xiao,gu,gu,zhi,shou,yue,gu,shi,fu,zheng,gai,luo,zhi,ling,can,zhou,nong,xi,huo,jian,dan,zu,qie,jie,yu,ku,duan,bei,xi,you,diao,ling,ze,gong,ji,rong,zhi,xiang,gen,yi,chen,na,po,lun" + }; + + return map; + } + + private static string[] GetPinyinArray(char c) + { + int code = c; + + // 使用简化的拼音查找算法 + // 实际实现需要完整的拼音对照表 + if (PinyinMap.TryGetValue(code, out string[] py)) + { + return py; + } + + // 简化处理:根据Unicode范围估算拼音首字母 + int index = code - 0x4E00; + + // 按拼音分区(非常简化) + string[] initials = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" }; + + // 这是一个简化实现,实际需要完整的拼音表 + // 这里只是为了演示 + int initialIndex = index % initials.Length; + return new[] { initials[initialIndex].ToLower() }; + } + } +} diff --git a/EasyTool.Core/TextCategory/SensitiveWordUtil.cs b/EasyTool.Core/TextCategory/SensitiveWordUtil.cs new file mode 100644 index 0000000..ed2611d --- /dev/null +++ b/EasyTool.Core/TextCategory/SensitiveWordUtil.cs @@ -0,0 +1,365 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 敏感词过滤工具类 + /// 使用 DFA(Deterministic Finite Automaton)算法实现高效敏感词检测 + /// + public static class SensitiveWordUtil + { + private static readonly object _lock = new(); + private static Dictionary _sensitiveWordsMap = new(); + private static HashSet _sensitiveWords = new(); + private static char[] _separatorChars = { ',', ',', '\n', '\r', ';' }; + + #region 初始化 + + /// + /// 初始化敏感词库 + /// + /// 敏感词列表 + public static void Init(IEnumerable words) + { + if (words == null) + return; + + lock (_lock) + { + _sensitiveWords = new HashSet(words.Where(w => !string.IsNullOrWhiteSpace(w))); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 从文件初始化敏感词库 + /// + /// 文件路径 + /// 编码(默认UTF-8) + public static void InitFromFile(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"敏感词文件不存在: {filePath}"); + + encoding ??= Encoding.UTF8; + var content = File.ReadAllText(filePath, encoding); + var words = content.Split(_separatorChars, StringSplitOptions.RemoveEmptyEntries); + Init(words); + } + + /// + /// 添加敏感词 + /// + /// 敏感词 + public static void AddWord(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return; + + lock (_lock) + { + _sensitiveWords.Add(word); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 批量添加敏感词 + /// + /// 敏感词列表 + public static void AddWords(IEnumerable words) + { + if (words == null) + return; + + lock (_lock) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + _sensitiveWords.Add(word); + } + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 移除敏感词 + /// + /// 敏感词 + public static void RemoveWord(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return; + + lock (_lock) + { + _sensitiveWords.Remove(word); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 清空敏感词库 + /// + public static void Clear() + { + lock (_lock) + { + _sensitiveWords.Clear(); + _sensitiveWordsMap.Clear(); + } + } + + /// + /// 获取敏感词数量 + /// + public static int Count => _sensitiveWords.Count; + + #endregion + + #region DFA构建 + + private static Dictionary BuildDFA(HashSet words) + { + var map = new Dictionary(); + + foreach (var word in words) + { + if (string.IsNullOrWhiteSpace(word)) + continue; + + var currentMap = map; + for (int i = 0; i < word.Length; i++) + { + var c = word[i]; + + if (!currentMap.TryGetValue(c, out var value)) + { + value = new Dictionary(); + currentMap[c] = value; + } + + var childMap = (Dictionary)value; + + if (i == word.Length - 1) + { + childMap['\0'] = new Dictionary(); // 标记词尾 + } + else + { + currentMap = childMap; + } + } + } + + return map; + } + + #endregion + + #region 检测 + + /// + /// 检测文本是否包含敏感词 + /// + /// 待检测文本 + /// 是否包含敏感词 + public static bool Contains(string text) + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return false; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out _)) + { + return true; + } + } + + return false; + } + + /// + /// 获取文本中的所有敏感词 + /// + /// 待检测文本 + /// 敏感词列表 + public static List FindAll(string text) + { + var result = new List(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out int length)) + { + result.Add(text.Substring(i, length)); + i += length - 1; + } + } + + return result; + } + + /// + /// 获取文本中敏感词的位置信息 + /// + /// 待检测文本 + /// 敏感词位置列表(起始位置, 敏感词) + public static List<(int StartIndex, string Word)> FindAllWithPosition(string text) + { + var result = new List<(int, string)>(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out int length)) + { + result.Add((i, text.Substring(i, length))); + i += length - 1; + } + } + + return result; + } + + /// + /// 统计文本中敏感词出现次数 + /// + /// 待检测文本 + /// 敏感词及其出现次数 + public static Dictionary CountWords(string text) + { + var result = new Dictionary(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + foreach (var word in FindAll(text)) + { + if (result.ContainsKey(word)) + result[word]++; + else + result[word] = 1; + } + + return result; + } + + private static bool CheckSensitiveWord(string text, int beginIndex, out int length) + { + length = 0; + var currentMap = _sensitiveWordsMap; + bool found = false; + + for (int i = beginIndex; i < text.Length; i++) + { + var c = text[i]; + + if (!currentMap.TryGetValue(c, out var value)) + { + break; + } + + length++; + currentMap = (Dictionary)value; + + if (currentMap.ContainsKey('\0')) + { + found = true; + } + } + + return found && length > 0; + } + + #endregion + + #region 过滤 + + /// + /// 过滤敏感词(替换为指定字符) + /// + /// 待过滤文本 + /// 替换字符(默认 *) + /// 过滤后的文本 + public static string Filter(string text, char replaceChar = '*') + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return text ?? string.Empty; + + var result = new StringBuilder(text); + + for (int i = 0; i < result.Length; i++) + { + if (CheckSensitiveWord(result.ToString(), i, out int length)) + { + for (int j = i; j < i + length && j < result.Length; j++) + { + result[j] = replaceChar; + } + i += length - 1; + } + } + + return result.ToString(); + } + + /// + /// 过滤敏感词(使用自定义替换策略) + /// + /// 待过滤文本 + /// 替换函数(参数为敏感词,返回替换后的文本) + /// 过滤后的文本 + public static string Filter(string text, Func replacer) + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0 || replacer == null) + return text ?? string.Empty; + + var positions = FindAllWithPosition(text); + if (positions.Count == 0) + return text; + + var result = new StringBuilder(); + int lastIndex = 0; + + foreach (var (startIndex, word) in positions) + { + result.Append(text.Substring(lastIndex, startIndex - lastIndex)); + result.Append(replacer(word)); + lastIndex = startIndex + word.Length; + } + + if (lastIndex < text.Length) + { + result.Append(text.Substring(lastIndex)); + } + + return result.ToString(); + } + + /// + /// 高亮显示敏感词 + /// + /// 文本 + /// 高亮前缀(如 <span style=\"color:red\">) + /// 高亮后缀(如 </span>) + /// 处理后的文本 + public static string Highlight(string text, string prefix = "", string suffix = "") + { + return Filter(text, word => $"{prefix}{word}{suffix}"); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/SimHashUtil.cs b/EasyTool.Core/TextCategory/SimHashUtil.cs new file mode 100644 index 0000000..1dc86ff --- /dev/null +++ b/EasyTool.Core/TextCategory/SimHashUtil.cs @@ -0,0 +1,325 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// SimHash 工具类 + /// 用于计算文本相似度的局部敏感哈希 + /// + public static class SimHashUtil + { + /// + /// 计算 SimHash 值 + /// + public static ulong Compute(string text, int hashBits = 64) + { + if (string.IsNullOrEmpty(text)) + return 0; + + // 分词 + var tokens = Tokenize(text); + if (tokens.Count == 0) + return 0; + + // 计算每个位的权重 + int[] v = new int[hashBits]; + + foreach (var token in tokens) + { + ulong hash = HashToken(token); + + for (int i = 0; i < hashBits; i++) + { + if (((hash >> i) & 1) == 1) + { + v[i]++; + } + else + { + v[i]--; + } + } + } + + // 生成 SimHash + ulong simHash = 0; + for (int i = 0; i < hashBits; i++) + { + if (v[i] > 0) + { + simHash |= (1UL << i); + } + } + + return simHash; + } + + /// + /// 计算 Hamming 距离 + /// + public static int HammingDistance(ulong hash1, ulong hash2) + { + ulong xor = hash1 ^ hash2; + int distance = 0; + + while (xor != 0) + { + distance++; + xor &= xor - 1; + } + + return distance; + } + + /// + /// 计算两个文本的相似度(基于 SimHash) + /// + public static double Similarity(string text1, string text2, int hashBits = 64) + { + ulong hash1 = Compute(text1, hashBits); + ulong hash2 = Compute(text2, hashBits); + + int distance = HammingDistance(hash1, hash2); + return 1.0 - (double)distance / hashBits; + } + + /// + /// 判断两个文本是否相似 + /// + public static bool IsSimilar(string text1, string text2, int threshold = 3) + { + ulong hash1 = Compute(text1); + ulong hash2 = Compute(text2); + + return HammingDistance(hash1, hash2) <= threshold; + } + + /// + /// 计算 SimHash 并返回十六进制字符串 + /// + public static string ComputeHex(string text) + { + ulong hash = Compute(text); + return hash.ToString("X16"); + } + + /// + /// 从十六进制字符串解析 SimHash + /// + public static ulong ParseHex(string hex) + { + return ulong.Parse(hex, System.Globalization.NumberStyles.HexNumber); + } + + private static List Tokenize(string text) + { + var tokens = new List(); + + // 简单分词:按空格和非字母数字字符分割 + var words = text.Split(new[] { ' ', '\t', '\n', '\r', '.', ',', '!', '?', ';', ':', '"', '\'', '(', ')', '[', ']', '{', '}' }, + StringSplitOptions.RemoveEmptyEntries); + + // 添加单词 + foreach (var word in words) + { + string lower = word.ToLowerInvariant(); + if (lower.Length >= 2) // 忽略单字符 + { + tokens.Add(lower); + } + } + + // 对于中文,添加2-gram和3-gram + foreach (char c in text) + { + if (c >= 0x4E00 && c <= 0x9FA5) + { + tokens.Add(c.ToString()); + } + } + + // 添加字符n-gram + if (text.Length >= 2) + { + for (int i = 0; i < text.Length - 1; i++) + { + tokens.Add(text.Substring(i, 2)); + } + } + + return tokens; + } + + private static ulong HashToken(string token) + { + // 使用 MurmurHash3 简化版 + byte[] data = Encoding.UTF8.GetBytes(token); + + unchecked + { + const ulong c1 = 0x87c37b91114253d5; + const ulong c2 = 0x4cf5ad432745937f; + + ulong h1 = 0; + int length = data.Length; + int blocks = length / 8; + int i = 0; + + for (int j = 0; j < blocks; j++) + { + ulong k1 = BitConverter.ToUInt64(data, i); + i += 8; + + k1 *= c1; + k1 = (k1 << 31) | (k1 >> 33); + k1 *= c2; + h1 ^= k1; + + h1 = (h1 << 27) | (h1 >> 37); + h1 = h1 * 5 + 0x52dce729; + } + + ulong remaining = 0; + int remainingLength = length - blocks * 8; + if (remainingLength > 0) + { + for (int j = 0; j < remainingLength; j++) + { + remaining |= (ulong)data[i + j] << (j * 8); + } + + remaining *= c1; + remaining = (remaining << 31) | (remaining >> 33); + remaining *= c2; + h1 ^= remaining; + } + + h1 ^= (ulong)length; + h1 ^= h1 >> 33; + h1 *= 0xff51afd7ed558ccd; + h1 ^= h1 >> 33; + h1 *= 0xc4ceb9fe1a85ec53; + h1 ^= h1 >> 33; + + return h1; + } + } + } + + /// + /// MinHash 工具类 + /// 用于集合相似度计算 + /// + public class MinHash + { + private readonly int _numHashes; + private readonly uint[] _seeds; + + /// + /// 哈希函数数量 + /// + public int NumHashes => _numHashes; + + /// + /// 创建 MinHash + /// + public MinHash(int numHashes = 128) + { + _numHashes = numHashes; + _seeds = new uint[numHashes]; + + var random = new Random(42); + for (int i = 0; i < numHashes; i++) + { + _seeds[i] = (uint)random.Next(); + } + } + + /// + /// 计算集合的 MinHash 签名 + /// + public uint[] ComputeSignature(HashSet set) + { + var signature = new uint[_numHashes]; + + for (int i = 0; i < _numHashes; i++) + { + uint minHash = uint.MaxValue; + + foreach (var item in set) + { + uint hash = Hash(item, _seeds[i]); + if (hash < minHash) + { + minHash = hash; + } + } + + signature[i] = minHash; + } + + return signature; + } + + /// + /// 计算文本的 MinHash 签名 + /// + public uint[] ComputeSignature(string text) + { + var set = new HashSet(); + var words = text.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var word in words) + { + set.Add(word.ToLowerInvariant()); + } + + // 添加n-gram + if (text.Length >= 2) + { + for (int i = 0; i < text.Length - 1; i++) + { + set.Add(text.Substring(i, 2)); + } + } + + return ComputeSignature(set); + } + + /// + /// 计算两个签名的 Jaccard 相似度估计 + /// + public static double EstimateSimilarity(uint[] signature1, uint[] signature2) + { + if (signature1.Length != signature2.Length) + throw new ArgumentException("Signatures must have the same length"); + + int matches = 0; + for (int i = 0; i < signature1.Length; i++) + { + if (signature1[i] == signature2[i]) + { + matches++; + } + } + + return (double)matches / signature1.Length; + } + + private static uint Hash(string s, uint seed) + { + unchecked + { + uint hash = seed; + foreach (char c in s) + { + hash = hash * 31 + c; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/TextCategory/SlugUtil.cs b/EasyTool.Core/TextCategory/SlugUtil.cs new file mode 100644 index 0000000..6238b4b --- /dev/null +++ b/EasyTool.Core/TextCategory/SlugUtil.cs @@ -0,0 +1,251 @@ +using System; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// URL Slug 工具类 + /// 用于生成 URL 友好的字符串标识符 + /// + public static class SlugUtil + { + /// + /// 默认最大长度 + /// + private const int DefaultMaxLength = 100; + + /// + /// 生成 URL 友好的 Slug + /// + /// 原始文本 + /// 最大长度 + /// Slug 字符串 + public static string Generate(string? text, int maxLength = DefaultMaxLength) + { + if (string.IsNullOrWhiteSpace(text)) + return string.Empty; + + // 转小写 + var result = text.ToLowerInvariant(); + + // 中文转拼音首字母(简化处理) + result = ConvertChineseToPinyin(result); + + // 移除特殊字符,保留字母、数字、中文 + result = Regex.Replace(result, @"[^a-z0-9\u4e00-\u9fa5\s-]", ""); + + // 将空格和多个连续空格替换为单个连字符 + result = Regex.Replace(result, @"\s+", "-"); + + // 将多个连字符合并为一个 + result = Regex.Replace(result, @"-+", "-"); + + // 移除首尾的连字符 + result = result.Trim('-'); + + // 截断到指定长度 + if (result.Length > maxLength) + { + result = result.Substring(0, maxLength).TrimEnd('-'); + } + + return result; + } + + /// + /// 生成带时间戳的 Slug + /// + /// 原始文本 + /// 最大长度 + /// 带时间戳的 Slug + public static string GenerateWithTimestamp(string? text, int maxLength = DefaultMaxLength) + { + var slug = Generate(text, maxLength - 15); + var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); + + return string.IsNullOrEmpty(slug) ? timestamp : $"{slug}-{timestamp}"; + } + + /// + /// 生成带随机后缀的 Slug + /// + /// 原始文本 + /// 后缀长度 + /// 最大长度 + /// 带随机后缀的 Slug + public static string GenerateWithRandomSuffix(string? text, int suffixLength = 6, int maxLength = DefaultMaxLength) + { + var slug = Generate(text, maxLength - suffixLength - 1); + var suffix = GenerateRandomString(suffixLength); + + return string.IsNullOrEmpty(slug) ? suffix : $"{slug}-{suffix}"; + } + + /// + /// 生成唯一 Slug(检查重复时使用) + /// + /// 原始文本 + /// 检查是否存在的函数 + /// 最大长度 + /// 唯一的 Slug + public static string GenerateUnique(string? text, Func exists, int maxLength = DefaultMaxLength) + { + var baseSlug = Generate(text, maxLength); + + if (string.IsNullOrEmpty(baseSlug)) + baseSlug = GenerateRandomString(8); + + if (!exists(baseSlug)) + return baseSlug; + + for (int i = 1; i <= 100; i++) + { + var suffix = i == 1 ? "" : $"-{i}"; + var newSlug = baseSlug.Length + suffix.Length > maxLength + ? baseSlug.Substring(0, maxLength - suffix.Length) + suffix + : baseSlug + suffix; + + if (!exists(newSlug)) + return newSlug; + } + + // 如果还是冲突,添加随机后缀 + return GenerateWithRandomSuffix(baseSlug, 8, maxLength); + } + + /// + /// 验证 Slug 是否有效 + /// + /// Slug 字符串 + /// 是否有效 + public static bool IsValid(string? slug) + { + if (string.IsNullOrWhiteSpace(slug)) + return false; + + // 只允许小写字母、数字、连字符 + return Regex.IsMatch(slug, @"^[a-z0-9]+(?:-[a-z0-9]+)*$"); + } + + /// + /// 规范化 Slug + /// + /// 原始 Slug + /// 规范化后的 Slug + public static string Normalize(string? slug) + { + if (string.IsNullOrWhiteSpace(slug)) + return string.Empty; + + // 转小写 + var result = slug.ToLowerInvariant(); + + // 移除非法字符 + result = Regex.Replace(result, @"[^a-z0-9\s-]", ""); + + // 空格转连字符 + result = Regex.Replace(result, @"\s+", "-"); + + // 合并多个连字符 + result = Regex.Replace(result, @"-+", "-"); + + // 移除首尾连字符 + result = result.Trim('-'); + + return result; + } + + /// + /// 将 Slug 转换为标题格式 + /// + /// Slug 字符串 + /// 标题格式字符串 + public static string ToTitle(string? slug) + { + if (string.IsNullOrWhiteSpace(slug)) + return string.Empty; + + var words = slug.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries); + var result = new StringBuilder(); + + foreach (var word in words) + { + if (result.Length > 0) + result.Append(' '); + + result.Append(char.ToUpperInvariant(word[0]) + word.Substring(1)); + } + + return result.ToString(); + } + + /// + /// 从标题生成 Slug(保留更多语义) + /// + /// 标题 + /// 最大长度 + /// Slug + public static string FromTitle(string? title, int maxLength = DefaultMaxLength) + { + if (string.IsNullOrWhiteSpace(title)) + return string.Empty; + + // 移除常见停用词(可选) + var stopWords = new[] { "a", "an", "the", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", "by" }; + + var words = title.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); + var filteredWords = new System.Collections.Generic.List(); + + foreach (var word in words) + { + var lower = word.ToLowerInvariant(); + if (Array.IndexOf(stopWords, lower) == -1) + { + filteredWords.Add(lower); + } + } + + var result = string.Join("-", filteredWords); + return Generate(result, maxLength); + } + + #region 私有方法 + + private static string ConvertChineseToPinyin(string text) + { + // 简化处理:移除中文字符(实际项目中可引入拼音库) + // 这里只做基础处理,实际应用建议使用 PinyinUtil + var result = new StringBuilder(); + + foreach (var c in text) + { + if (c >= 0x4E00 && c <= 0x9FA5) + { + // 中文字符,可以调用 PinyinUtil 获取拼音 + // 这里简化处理,移除中文 + continue; + } + result.Append(c); + } + + return result.ToString(); + } + + private static string GenerateRandomString(int length) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + var random = new Random(); + var result = new char[length]; + + for (int i = 0; i < length; i++) + { + result[i] = chars[random.Next(chars.Length)]; + } + + return new string(result); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/StrUtil.cs b/EasyTool.Core/TextCategory/StrUtil.cs index 776f920..1ae076f 100644 --- a/EasyTool.Core/TextCategory/StrUtil.cs +++ b/EasyTool.Core/TextCategory/StrUtil.cs @@ -14,9 +14,13 @@ public static class StrUtil /// 移除字符串中的所有空格 ///
/// 要处理的字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string RemoveAllSpaces(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return Regex.Replace(str, @"\s+", ""); } @@ -28,8 +32,11 @@ public static string RemoveAllSpaces(string str) /// 如果是数字,则返回true,否则返回false public static bool IsNumeric(string str) { - double result; - return double.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return double.TryParse(str, out _); } /// @@ -39,8 +46,11 @@ public static bool IsNumeric(string str) /// 如果是整数,则返回true,否则返回false public static bool IsInteger(string str) { - int result; - return int.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return int.TryParse(str, out _); } /// @@ -50,8 +60,11 @@ public static bool IsInteger(string str) /// 如果是日期,则返回true,否则返回false public static bool IsDate(string str) { - DateTime result; - return DateTime.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return DateTime.TryParse(str, out _); } @@ -65,9 +78,13 @@ public static bool IsDate(string str) /// 将字符串转换为驼峰命名法 /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToCamelCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) @@ -89,9 +106,13 @@ public static string ToCamelCase(string str) /// 将字符串转换为帕斯卡命名法(大驼峰命名法) /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToPascalCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) @@ -106,9 +127,13 @@ public static string ToPascalCase(string str) /// 将字符串转换为下划线命名法 ///
/// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToSnakeCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) @@ -130,9 +155,13 @@ public static string ToSnakeCase(string str) /// 将字符串转换为连字符命名法(短横线命名法) ///
/// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToKebabCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) @@ -154,9 +183,13 @@ public static string ToKebabCase(string str) /// 将字符串中的 HTML 标记去除 /// /// 要处理的字符串 - /// 去除 HTML 标记后的字符串 + /// 去除 HTML 标记后的字符串,如果输入为 null 则返回空字符串 public static string StripHtml(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return Regex.Replace(str, "<.*?>", ""); } @@ -168,6 +201,8 @@ public static string StripHtml(string str) /// 如果相等,则返回true,否则返回false public static bool EqualsIgnoreCaseAndWhiteSpace(string str1, string str2) { + if (str1 == null && str2 == null) return true; + if (str1 == null || str2 == null) return false; return string.Equals(RemoveAllSpaces(str1), RemoveAllSpaces(str2), StringComparison.OrdinalIgnoreCase); } @@ -179,9 +214,17 @@ public static bool EqualsIgnoreCaseAndWhiteSpace(string str1, string str2) /// 要处理的字符串 /// 要替换的字符数组 /// 新的字符 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceChars(string str, char[] chars, char newChar) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (chars == null) + { + return str; + } for (int i = 0; i < chars.Length; i++) { str = str.Replace(chars[i], newChar); @@ -195,9 +238,17 @@ public static string ReplaceChars(string str, char[] chars, char newChar) /// 要处理的字符串 /// 要替换的子字符串数组 /// 新的子字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceStrings(string str, string[] oldValues, string newValue) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (oldValues == null) + { + return str; + } for (int i = 0; i < oldValues.Length; i++) { str = str.Replace(oldValues[i], newValue); @@ -211,9 +262,17 @@ public static string ReplaceStrings(string str, string[] oldValues, string newVa /// 要处理的字符串 /// 要替换的子字符串数组 /// 新的子字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceStringsIgnoreCase(string str, string[] oldValues, string newValue) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (oldValues == null) + { + return str; + } for (int i = 0; i < oldValues.Length; i++) { str = Regex.Replace(str, oldValues[i], newValue, RegexOptions.IgnoreCase); @@ -225,9 +284,13 @@ public static string ReplaceStringsIgnoreCase(string str, string[] oldValues, st /// 将字符串转换为 Title Case 格式,即每个单词的首字母大写 /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToTitleCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower()); } diff --git a/EasyTool.Core/TextCategory/StringComparisonExtension.cs b/EasyTool.Core/TextCategory/StringComparisonExtension.cs index 8274e77..3ae4f68 100644 --- a/EasyTool.Core/TextCategory/StringComparisonExtension.cs +++ b/EasyTool.Core/TextCategory/StringComparisonExtension.cs @@ -1,7 +1,7 @@ using System; using System.Globalization; -namespace EasyTool.ToolCategory +namespace EasyTool.TextCategory { /// /// String 字符串比较扩展方法 diff --git a/EasyTool.Core/TextCategory/TemplateUtil.cs b/EasyTool.Core/TextCategory/TemplateUtil.cs new file mode 100644 index 0000000..fe965bb --- /dev/null +++ b/EasyTool.Core/TextCategory/TemplateUtil.cs @@ -0,0 +1,365 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 模板渲染工具类 + /// 支持变量替换、条件渲染、循环等 + /// + public static class TemplateUtil + { + #region 简单变量替换 + + /// + /// 渲染模板(使用 ${variable} 或 {{variable}} 语法) + /// + /// 模板字符串 + /// 变量字典 + /// 渲染后的字符串 + public static string Render(string template, Dictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + if (variables == null || variables.Count == 0) + return template; + + var result = template; + + // 支持 ${variable} 格式 + result = Regex.Replace(result, @"\$\{(\w+)\}", match => + { + var key = match.Groups[1].Value; + return variables.TryGetValue(key, out var value) ? value?.ToString() ?? "" : ""; + }); + + // 支持 {{variable}} 格式 + result = Regex.Replace(result, @"\{\{(\w+)\}\}", match => + { + var key = match.Groups[1].Value; + return variables.TryGetValue(key, out var value) ? value?.ToString() ?? "" : ""; + }); + + return result; + } + + /// + /// 渲染模板(使用匿名对象) + /// + /// 模板字符串 + /// 数据模型 + /// 渲染后的字符串 + public static string Render(string template, object model) + { + if (model == null) + return template; + + var variables = new Dictionary(); + var properties = model.GetType().GetProperties(); + + foreach (var prop in properties) + { + variables[prop.Name] = prop.GetValue(model) ?? ""; + } + + return Render(template, variables); + } + + #endregion + + #region 带默认值的渲染 + + /// + /// 渲染模板(支持默认值) + /// 语法:${variable:default} 或 {{variable|default}} + /// + /// 模板字符串 + /// 变量字典 + /// 渲染后的字符串 + public static string RenderWithDefault(string template, Dictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = template; + + // ${variable:default} 格式 + result = Regex.Replace(result, @"\$\{(\w+):([^}]*)\}", match => + { + var key = match.Groups[1].Value; + var defaultValue = match.Groups[2].Value; + + if (variables != null && variables.TryGetValue(key, out var value) && value != null) + { + return value.ToString(); + } + + return defaultValue; + }); + + // {{variable|default}} 格式 + result = Regex.Replace(result, @"\{\{(\w+)\|([^}]*)\}\}", match => + { + var key = match.Groups[1].Value; + var defaultValue = match.Groups[2].Value; + + if (variables != null && variables.TryGetValue(key, out var value) && value != null) + { + return value.ToString(); + } + + return defaultValue; + }); + + return result; + } + + #endregion + + #region 条件渲染 + + /// + /// 条件渲染 + /// 语法:{{#if condition}}...{{/if}} + /// {{#if condition}}...{{else}}...{{/if}} + /// + public static string RenderConditional(string template, Dictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = template; + + // 处理 if-else 结构 + var ifElsePattern = @"\{\{#if\s+(\w+)\}\}(.*?)\{\{else\}\}(.*?)\{\{/if\}\}"; + result = Regex.Replace(result, ifElsePattern, match => + { + var condition = match.Groups[1].Value; + var trueContent = match.Groups[2].Value; + var falseContent = match.Groups[3].Value; + + if (variables != null && variables.TryGetValue(condition, out var value)) + { + var isTrue = value switch + { + bool b => b, + string s => !string.IsNullOrEmpty(s), + int i => i != 0, + null => false, + _ => true + }; + + return isTrue ? trueContent : falseContent; + } + + return falseContent; + }, RegexOptions.Singleline); + + // 处理简单 if 结构 + var ifPattern = @"\{\{#if\s+(\w+)\}\}(.*?)\{\{/if\}\}"; + result = Regex.Replace(result, ifPattern, match => + { + var condition = match.Groups[1].Value; + var content = match.Groups[2].Value; + + if (variables != null && variables.TryGetValue(condition, out var value)) + { + var isTrue = value switch + { + bool b => b, + string s => !string.IsNullOrEmpty(s), + int i => i != 0, + null => false, + _ => true + }; + + return isTrue ? content : ""; + } + + return ""; + }, RegexOptions.Singleline); + + return result; + } + + #endregion + + #region 循环渲染 + + /// + /// 循环渲染 + /// 语法:{{#each items}}...{{this}}...{{/each}} + /// + public static string RenderLoop(string template, Dictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = template; + var eachPattern = @"\{\{#each\s+(\w+)\}\}(.*?)\{\{/each\}\}"; + + result = Regex.Replace(result, eachPattern, match => + { + var listName = match.Groups[1].Value; + var itemTemplate = match.Groups[2].Value; + + if (variables == null || !variables.TryGetValue(listName, out var listValue)) + return ""; + + var items = listValue as IEnumerable; + if (items == null) + return ""; + + var sb = new StringBuilder(); + var index = 0; + + foreach (var item in items) + { + var itemResult = itemTemplate; + + // 替换 {{this}} + itemResult = itemResult.Replace("{{this}}", item?.ToString() ?? ""); + + // 替换 {{@index}} + itemResult = itemResult.Replace("{{@index}}", index.ToString()); + + // 替换 {{@first}} + itemResult = itemResult.Replace("{{@first}}", (index == 0).ToString().ToLower()); + + // 替换 {{@last}} + var isLast = index == (items as ICollection)?.Count - 1; + itemResult = itemResult.Replace("{{@last}}", isLast.ToString().ToLower()); + + // 如果是对象,替换其属性 + if (item != null && !(item is string)) + { + var props = item.GetType().GetProperties(); + foreach (var prop in props) + { + itemResult = itemResult.Replace($"{{{{{prop.Name}}}}}", prop.GetValue(item)?.ToString() ?? ""); + } + } + + sb.Append(itemResult); + index++; + } + + return sb.ToString(); + }, RegexOptions.Singleline); + + return result; + } + + #endregion + + #region 完整渲染 + + /// + /// 完整渲染(包含变量、条件、循环) + /// + /// 模板字符串 + /// 变量字典 + /// 渲染后的字符串 + public static string RenderFull(string template, Dictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = template; + + // 先处理循环 + result = RenderLoop(result, variables); + + // 再处理条件 + result = RenderConditional(result, variables); + + // 最后处理变量 + result = RenderWithDefault(result, variables); + + return result; + } + + #endregion + + #region 模板缓存 + + private static readonly Dictionary _templateCache = new(); + private static readonly object _cacheLock = new(); + + /// + /// 缓存模板 + /// + /// 模板名称 + /// 模板内容 + public static void CacheTemplate(string name, string template) + { + lock (_cacheLock) + { + _templateCache[name] = template; + } + } + + /// + /// 从缓存加载模板 + /// + /// 模板名称 + /// 模板内容 + public static string? GetCachedTemplate(string name) + { + lock (_cacheLock) + { + return _templateCache.TryGetValue(name, out var template) ? template : null; + } + } + + /// + /// 渲染缓存的模板 + /// + /// 模板名称 + /// 变量字典 + /// 渲染后的字符串 + public static string RenderCached(string name, Dictionary variables) + { + var template = GetCachedTemplate(name); + if (template == null) + throw new KeyNotFoundException($"模板 '{name}' 不存在"); + + return RenderFull(template, variables); + } + + /// + /// 清除模板缓存 + /// + public static void ClearCache() + { + lock (_cacheLock) + { + _templateCache.Clear(); + } + } + + #endregion + + #region 文件模板 + + /// + /// 从文件渲染模板 + /// + /// 模板文件路径 + /// 变量字典 + /// 渲染后的字符串 + public static string RenderFromFile(string filePath, Dictionary variables) + { + if (!System.IO.File.Exists(filePath)) + throw new System.IO.FileNotFoundException($"模板文件不存在: {filePath}"); + + var template = System.IO.File.ReadAllText(filePath); + return RenderFull(template, variables); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/AsyncUtil.cs b/EasyTool.Core/ToolCategory/AsyncUtil.cs new file mode 100644 index 0000000..dc25229 --- /dev/null +++ b/EasyTool.Core/ToolCategory/AsyncUtil.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 异步工具类 + /// 提供异步操作的辅助方法 + /// + public static class AsyncUtil + { + #region 超时控制 + + /// + /// 带超时的异步操作 + /// + /// 返回类型 + /// 异步任务 + /// 超时时间(毫秒) + /// 任务结果 + public static async Task WithTimeout(Task task, int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(); + var completedTask = await Task.WhenAny(task, Task.Delay(timeoutMilliseconds, cts.Token)); + + if (completedTask == task) + { + cts.Cancel(); + return await task; + } + + throw new TimeoutException($"操作在 {timeoutMilliseconds} 毫秒后超时"); + } + + /// + /// 带超时的异步操作 + /// + /// 异步任务 + /// 超时时间(毫秒) + public static async Task WithTimeout(Task task, int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(); + var completedTask = await Task.WhenAny(task, Task.Delay(timeoutMilliseconds, cts.Token)); + + if (completedTask == task) + { + cts.Cancel(); + await task; + return; + } + + throw new TimeoutException($"操作在 {timeoutMilliseconds} 毫秒后超时"); + } + + /// + /// 带超时的异步操作(返回默认值而非抛异常) + /// + /// 返回类型 + /// 异步任务 + /// 超时时间(毫秒) + /// 默认值 + /// 任务结果或默认值 + public static async Task WithTimeoutOrDefault(Task task, int timeoutMilliseconds, T? defaultValue = default) + { + try + { + return await WithTimeout(task, timeoutMilliseconds); + } + catch (TimeoutException) + { + return defaultValue; + } + } + + #endregion + + #region 重试机制 + + /// + /// 异步重试 + /// + /// 返回类型 + /// 异步函数 + /// 最大重试次数 + /// 重试间隔(毫秒) + /// 是否指数退避 + /// 任务结果 + public static async Task RetryAsync( + Func> func, + int maxRetries = 3, + int delayMilliseconds = 1000, + bool exponentialBackoff = true) + { + Exception? lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt < maxRetries) + { + var delay = exponentialBackoff + ? delayMilliseconds * (int)Math.Pow(2, attempt) + : delayMilliseconds; + + await Task.Delay(delay); + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 异步重试(无返回值) + /// + /// 异步操作 + /// 最大重试次数 + /// 重试间隔(毫秒) + /// 是否指数退避 + public static async Task RetryAsync( + Func action, + int maxRetries = 3, + int delayMilliseconds = 1000, + bool exponentialBackoff = true) + { + await RetryAsync(async () => + { + await action(); + return true; + }, maxRetries, delayMilliseconds, exponentialBackoff); + } + + #endregion + + #region 并发控制 + + /// + /// 并发执行多个任务(限制并发数) + /// + /// 返回类型 + /// 任务工厂集合 + /// 最大并发数 + /// 所有任务结果 + public static async Task> WhenAllWithConcurrency( + IEnumerable>> tasks, + int maxConcurrency) + { + var results = new List(); + var taskList = new List>>(tasks); + var semaphore = new SemaphoreSlim(maxConcurrency); + + var wrappedTasks = taskList.Select(async taskFactory => + { + await semaphore.WaitAsync(); + try + { + return await taskFactory(); + } + finally + { + semaphore.Release(); + } + }); + + results.AddRange(await Task.WhenAll(wrappedTasks)); + return results; + } + + /// + /// 并发执行多个任务(限制并发数) + /// + /// 任务工厂集合 + /// 最大并发数 + public static async Task WhenAllWithConcurrency( + IEnumerable> actions, + int maxConcurrency) + { + var actionList = new List>(actions); + var semaphore = new SemaphoreSlim(maxConcurrency); + + var wrappedTasks = actionList.Select(async action => + { + await semaphore.WaitAsync(); + try + { + await action(); + } + finally + { + semaphore.Release(); + } + }); + + await Task.WhenAll(wrappedTasks); + } + + #endregion + + #region 批处理 + + /// + /// 批量处理数据 + /// + /// 输入类型 + /// 输出类型 + /// 数据项 + /// 处理函数 + /// 批次大小 + /// 最大并发数 + /// 所有处理结果 + public static async Task> ProcessBatchAsync( + IEnumerable items, + Func> processor, + int batchSize = 10, + int maxConcurrency = 5) + { + var results = new List(); + var itemList = new List(items); + var batches = new List>(); + + for (int i = 0; i < itemList.Count; i += batchSize) + { + batches.Add(itemList.GetRange(i, Math.Min(batchSize, itemList.Count - i))); + } + + foreach (var batch in batches) + { + var batchResults = await WhenAllWithConcurrency( + batch.Select>>(item => () => processor(item)), + maxConcurrency); + + results.AddRange(batchResults); + } + + return results; + } + + #endregion + + #region 延迟执行 + + /// + /// 延迟执行 + /// + /// 操作 + /// 延迟时间(毫秒) + /// 取消令牌 + public static async Task DelayAsync( + Action action, + int delayMilliseconds, + CancellationToken cancellationToken = default) + { + await Task.Delay(delayMilliseconds, cancellationToken); + action(); + } + + /// + /// 延迟执行(异步操作) + /// + /// 异步操作 + /// 延迟时间(毫秒) + /// 取消令牌 + public static async Task DelayAsync( + Func action, + int delayMilliseconds, + CancellationToken cancellationToken = default) + { + await Task.Delay(delayMilliseconds, cancellationToken); + await action(); + } + + #endregion + + #region 取消支持 + + /// + /// 创建可取消的任务 + /// + /// 返回类型 + /// 异步函数 + /// 取消令牌 + /// 任务结果 + public static async Task RunWithCancellation( + Func> func, + CancellationToken cancellationToken = default) + { + return await func(cancellationToken); + } + + /// + /// 创建带取消令牌的任务超时 + /// + /// 返回类型 + /// 异步函数 + /// 超时时间(毫秒) + /// 任务结果 + public static async Task RunWithTimeout( + Func> func, + int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(timeoutMilliseconds); + return await func(cts.Token); + } + + #endregion + + #region 顺序执行 + + /// + /// 顺序执行多个异步任务 + /// + /// 返回类型 + /// 任务工厂集合 + /// 所有任务结果 + public static async Task> ExecuteSequentially(IEnumerable>> tasks) + { + var results = new List(); + + foreach (var taskFactory in tasks) + { + results.Add(await taskFactory()); + } + + return results; + } + + /// + /// 顺序执行多个异步任务(无返回值) + /// + /// 任务工厂集合 + public static async Task ExecuteSequentially(IEnumerable> actions) + { + foreach (var action in actions) + { + await action(); + } + } + + #endregion + + #region 结果收集 + + /// + /// 并行执行并收集成功/失败结果 + /// + /// 返回类型 + /// 任务工厂集合 + /// 成功和失败的结果 + public static async Task<(List Successes, List Failures)> CollectResults( + IEnumerable>> tasks) + { + var successes = new List(); + var failures = new List(); + + var results = await Task.WhenAll(tasks.Select(async taskFactory => + { + try + { + return (Success: true, Result: await taskFactory(), Exception: (Exception?)null); + } + catch (Exception ex) + { + return (Success: false, Result: default(T), Exception: ex); + } + })); + + foreach (var result in results) + { + if (result.Success) + { + successes.Add(result.Result!); + } + else if (result.Exception != null) + { + failures.Add(result.Exception); + } + } + + return (successes, failures); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/BenchmarkUtil.cs b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs new file mode 100644 index 0000000..0da5c6f --- /dev/null +++ b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs @@ -0,0 +1,543 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 性能计时工具类 + /// 提供代码执行时间测量和性能分析功能 + /// + public static class BenchmarkUtil + { + /// + /// 测量操作执行时间 + /// + /// 要测量的操作 + /// 操作名称 + /// 测量结果 + public static BenchmarkResult Measure(Action action, string? name = null) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + var stopwatch = Stopwatch.StartNew(); + action(); + stopwatch.Stop(); + + return new BenchmarkResult + { + Name = name ?? "Operation", + ElapsedTicks = stopwatch.ElapsedTicks, + ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, + ElapsedTime = stopwatch.Elapsed + }; + } + + /// + /// 测量异步操作执行时间 + /// + /// 要测量的异步操作 + /// 操作名称 + /// 测量结果 + public static async Task MeasureAsync(Func func, string? name = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + var stopwatch = Stopwatch.StartNew(); + await func(); + stopwatch.Stop(); + + return new BenchmarkResult + { + Name = name ?? "Operation", + ElapsedTicks = stopwatch.ElapsedTicks, + ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, + ElapsedTime = stopwatch.Elapsed + }; + } + + /// + /// 测量带返回值的操作执行时间 + /// + /// 返回值类型 + /// 要测量的操作 + /// 操作名称 + /// 带返回值的测量结果 + public static BenchmarkResult Measure(Func func, string? name = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + var stopwatch = Stopwatch.StartNew(); + var result = func(); + stopwatch.Stop(); + + return new BenchmarkResult + { + Name = name ?? "Operation", + ElapsedTicks = stopwatch.ElapsedTicks, + ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, + ElapsedTime = stopwatch.Elapsed, + Value = result + }; + } + + /// + /// 测量带返回值的异步操作执行时间 + /// + /// 返回值类型 + /// 要测量的异步操作 + /// 操作名称 + /// 带返回值的测量结果 + public static async Task> MeasureAsync(Func> func, string? name = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + var stopwatch = Stopwatch.StartNew(); + var result = await func(); + stopwatch.Stop(); + + return new BenchmarkResult + { + Name = name ?? "Operation", + ElapsedTicks = stopwatch.ElapsedTicks, + ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, + ElapsedTime = stopwatch.Elapsed, + Value = result + }; + } + + /// + /// 多次执行并计算平均执行时间 + /// + /// 要测量的操作 + /// 迭代次数 + /// 操作名称 + /// 统计结果 + public static BenchmarkStatistics Benchmark(Action action, int iterations = 100, string? name = null) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + if (iterations < 1) + throw new ArgumentOutOfRangeException(nameof(iterations)); + + // 预热 + action(); + + var times = new List(iterations); + var stopwatch = new Stopwatch(); + + for (int i = 0; i < iterations; i++) + { + stopwatch.Restart(); + action(); + stopwatch.Stop(); + times.Add(stopwatch.ElapsedTicks); + } + + return CalculateStatistics(name ?? "Operation", times, iterations); + } + + /// + /// 多次执行异步操作并计算平均执行时间 + /// + /// 要测量的异步操作 + /// 迭代次数 + /// 操作名称 + /// 统计结果 + public static async Task BenchmarkAsync(Func func, int iterations = 100, string? name = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + if (iterations < 1) + throw new ArgumentOutOfRangeException(nameof(iterations)); + + // 预热 + await func(); + + var times = new List(iterations); + var stopwatch = new Stopwatch(); + + for (int i = 0; i < iterations; i++) + { + stopwatch.Restart(); + await func(); + stopwatch.Stop(); + times.Add(stopwatch.ElapsedTicks); + } + + return CalculateStatistics(name ?? "Operation", times, iterations); + } + + /// + /// 比较多个操作的执行时间 + /// + /// 操作列表(名称和操作) + /// 每个操作的迭代次数 + /// 比较结果列表 + public static List Compare(IEnumerable<(string Name, Action Action)> operations, int iterations = 100) + { + if (operations == null) + throw new ArgumentNullException(nameof(operations)); + + var results = new List(); + foreach (var (name, action) in operations) + { + results.Add(Benchmark(action, iterations, name)); + } + + return results.OrderBy(r => r.AverageMilliseconds).ToList(); + } + + /// + /// 比较多个异步操作的执行时间 + /// + /// 操作列表(名称和操作) + /// 每个操作的迭代次数 + /// 比较结果列表 + public static async Task> CompareAsync( + IEnumerable<(string Name, Func Func)> operations, + int iterations = 100) + { + if (operations == null) + throw new ArgumentNullException(nameof(operations)); + + var results = new List(); + foreach (var (name, func) in operations) + { + results.Add(await BenchmarkAsync(func, iterations, name)); + } + + return results.OrderBy(r => r.AverageMilliseconds).ToList(); + } + + /// + /// 创建一个可多次记录的计时器 + /// + /// 计时器名称 + /// 计时器实例 + public static BenchmarkTimer CreateTimer(string? name = null) + { + return new BenchmarkTimer(name); + } + + /// + /// 内存使用测量 + /// + /// 要测量的操作 + /// 操作名称 + /// 测量结果 + public static MemoryBenchmarkResult MeasureMemory(Action action, string? name = null) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + // 强制GC以获得更准确的内存测量 + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + var beforeMemory = GC.GetTotalMemory(forceFullCollection: true); + + var stopwatch = Stopwatch.StartNew(); + action(); + stopwatch.Stop(); + + var afterMemory = GC.GetTotalMemory(forceFullCollection: false); + + return new MemoryBenchmarkResult + { + Name = name ?? "Operation", + ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, + MemoryBefore = beforeMemory, + MemoryAfter = afterMemory, + MemoryDelta = afterMemory - beforeMemory + }; + } + + private static BenchmarkStatistics CalculateStatistics(string name, List times, int iterations) + { + times.Sort(); + var frequency = Stopwatch.Frequency; + + var avgTicks = times.Average(); + var minTicks = times[0]; + var maxTicks = times[times.Count - 1]; + var medianTicks = times[times.Count / 2]; + var stdDev = Math.Sqrt(times.Average(t => Math.Pow(t - avgTicks, 2))); + + var p95Index = (int)(iterations * 0.95); + var p99Index = (int)(iterations * 0.99); + + return new BenchmarkStatistics + { + Name = name, + Iterations = iterations, + TotalMilliseconds = (long)times.Sum(t => t * 1000.0 / frequency), + AverageMilliseconds = avgTicks * 1000.0 / frequency, + MinMilliseconds = minTicks * 1000.0 / frequency, + MaxMilliseconds = maxTicks * 1000.0 / frequency, + MedianMilliseconds = medianTicks * 1000.0 / frequency, + StdDevMilliseconds = stdDev * 1000.0 / frequency, + P95Milliseconds = times[Math.Min(p95Index, times.Count - 1)] * 1000.0 / frequency, + P99Milliseconds = times[Math.Min(p99Index, times.Count - 1)] * 1000.0 / frequency, + OperationsPerSecond = 1000.0 / (avgTicks * 1000.0 / frequency) + }; + } + } + + /// + /// 基准测试结果 + /// + public class BenchmarkResult + { + /// + /// 操作名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 执行时间(Tick数) + /// + public long ElapsedTicks { get; set; } + + /// + /// 执行时间(毫秒) + /// + public long ElapsedMilliseconds { get; set; } + + /// + /// 执行时间 + /// + public TimeSpan ElapsedTime { get; set; } + + /// + /// 执行时间(微秒) + /// + public double ElapsedMicroseconds => ElapsedTicks * 1_000_000.0 / Stopwatch.Frequency; + + /// + /// 执行时间(纳秒) + /// + public double ElapsedNanoseconds => ElapsedTicks * 1_000_000_000.0 / Stopwatch.Frequency; + + public override string ToString() + { + if (ElapsedMilliseconds > 0) + return $"{Name}: {ElapsedMilliseconds}ms"; + return $"{Name}: {ElapsedMicroseconds:F2}μs"; + } + } + + /// + /// 带返回值的基准测试结果 + /// + /// 返回值类型 + public class BenchmarkResult : BenchmarkResult + { + /// + /// 返回值 + /// + public T? Value { get; set; } + } + + /// + /// 基准测试统计信息 + /// + public class BenchmarkStatistics + { + /// + /// 操作名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 迭代次数 + /// + public int Iterations { get; set; } + + /// + /// 总执行时间(毫秒) + /// + public long TotalMilliseconds { get; set; } + + /// + /// 平均执行时间(毫秒) + /// + public double AverageMilliseconds { get; set; } + + /// + /// 最小执行时间(毫秒) + /// + public double MinMilliseconds { get; set; } + + /// + /// 最大执行时间(毫秒) + /// + public double MaxMilliseconds { get; set; } + + /// + /// 中位数执行时间(毫秒) + /// + public double MedianMilliseconds { get; set; } + + /// + /// 标准差(毫秒) + /// + public double StdDevMilliseconds { get; set; } + + /// + /// 第95百分位执行时间(毫秒) + /// + public double P95Milliseconds { get; set; } + + /// + /// 第99百分位执行时间(毫秒) + /// + public double P99Milliseconds { get; set; } + + /// + /// 每秒操作数 + /// + public double OperationsPerSecond { get; set; } + + public override string ToString() + { + return $"{Name}: Avg={AverageMilliseconds:F3}ms, Min={MinMilliseconds:F3}ms, Max={MaxMilliseconds:F3}ms, Ops/s={OperationsPerSecond:F0}"; + } + } + + /// + /// 内存基准测试结果 + /// + public class MemoryBenchmarkResult + { + /// + /// 操作名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 执行时间(毫秒) + /// + public long ElapsedMilliseconds { get; set; } + + /// + /// 执行前内存(字节) + /// + public long MemoryBefore { get; set; } + + /// + /// 执行后内存(字节) + /// + public long MemoryAfter { get; set; } + + /// + /// 内存变化(字节) + /// + public long MemoryDelta { get; set; } + + /// + /// 内存变化(MB) + /// + public double MemoryDeltaMB => MemoryDelta / (1024.0 * 1024.0); + + public override string ToString() + { + var sign = MemoryDelta >= 0 ? "+" : ""; + return $"{Name}: {ElapsedMilliseconds}ms, Memory: {sign}{MemoryDeltaMB:F2}MB"; + } + } + + /// + /// 可记录多个时间点的计时器 + /// + public class BenchmarkTimer + { + private readonly Stopwatch _stopwatch; + private readonly List<(string Label, long Ticks)> _laps; + private readonly string? _name; + + public BenchmarkTimer(string? name = null) + { + _name = name; + _stopwatch = new Stopwatch(); + _laps = new List<(string, long)>(); + } + + /// + /// 开始计时 + /// + public BenchmarkTimer Start() + { + _stopwatch.Start(); + return this; + } + + /// + /// 记录一个时间点 + /// + /// 标签 + public BenchmarkTimer Lap(string label) + { + _laps.Add((label, _stopwatch.ElapsedTicks)); + return this; + } + + /// + /// 停止计时 + /// + public BenchmarkTimer Stop() + { + _stopwatch.Stop(); + return this; + } + + /// + /// 重置计时器 + /// + public BenchmarkTimer Reset() + { + _stopwatch.Reset(); + _laps.Clear(); + return this; + } + + /// + /// 获取所有记录点 + /// + /// 记录点列表 + public List<(string Label, double Milliseconds)> GetLaps() + { + return _laps.Select(l => (l.Label, l.Ticks * 1000.0 / Stopwatch.Frequency)).ToList(); + } + + /// + /// 获取总执行时间(毫秒) + /// + public double TotalMilliseconds => _stopwatch.ElapsedTicks * 1000.0 / Stopwatch.Frequency; + + /// + /// 获取执行时间 + /// + public TimeSpan Elapsed => _stopwatch.Elapsed; + + /// + /// 是否正在运行 + /// + public bool IsRunning => _stopwatch.IsRunning; + + public override string ToString() + { + return _name != null + ? $"{_name}: {TotalMilliseconds:F2}ms" + : $"{TotalMilliseconds:F2}ms"; + } + } +} diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs new file mode 100644 index 0000000..564bc84 --- /dev/null +++ b/EasyTool.Core/ToolCategory/EnumUtil.cs @@ -0,0 +1,364 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace EasyTool.ToolCategory +{ + /// + /// 枚举增强工具类 + /// 提供枚举类型的扩展功能 + /// + public static class EnumUtil + { +#if NET5_0_OR_GREATER + private static System.Random GetSharedRandom() => System.Random.Shared; +#else + private static readonly ThreadLocal ThreadLocalRandom = new(() => new System.Random(Guid.NewGuid().GetHashCode())); + private static System.Random GetSharedRandom() => ThreadLocalRandom.Value!; +#endif + + /// + /// 获取枚举的所有值 + /// + /// 枚举类型 + /// 枚举值数组 + public static T[] GetValues() where T : struct, Enum + { +#if NET5_0_OR_GREATER + return Enum.GetValues(); +#else + return (T[])Enum.GetValues(typeof(T)); +#endif + } + + /// + /// 获取枚举的所有名称 + /// + /// 枚举类型 + /// 名称数组 + public static string[] GetNames() where T : struct, Enum + { +#if NET5_0_OR_GREATER + return Enum.GetNames(); +#else + return Enum.GetNames(typeof(T)); +#endif + } + + /// + /// 将名称转换为枚举值 + /// + /// 枚举类型 + /// 名称 + /// 是否忽略大小写 + /// 枚举值 + public static T Parse(string name, bool ignoreCase = false) where T : struct, Enum + { + return (T)Enum.Parse(typeof(T), name, ignoreCase); + } + + /// + /// 尝试将名称转换为枚举值 + /// + /// 枚举类型 + /// 名称 + /// 转换结果 + /// 是否忽略大小写 + /// 是否转换成功 + public static bool TryParse(string? name, out T result, bool ignoreCase = false) where T : struct, Enum + { + result = default; + if (name == null) return false; + + try + { + result = (T)Enum.Parse(typeof(T), name, ignoreCase); + return true; + } + catch + { + return false; + } + } + + /// + /// 将整数值转换为枚举 + /// + /// 枚举类型 + /// 整数值 + /// 枚举值 + public static T FromInt(int value) where T : struct, Enum + { + return (T)(object)value; + } + + /// + /// 尝试将整数值转换为枚举 + /// + /// 枚举类型 + /// 整数值 + /// 转换结果 + /// 是否转换成功 + public static bool TryFromInt(int value, out T result) where T : struct, Enum + { + result = default; + if (!Enum.IsDefined(typeof(T), value)) + return false; + + result = (T)(object)value; + return true; + } + + /// + /// 获取枚举值的名称 + /// + /// 枚举类型 + /// 枚举值 + /// 名称 + public static string GetName(T value) where T : struct, Enum + { + return Enum.GetName(typeof(T), value) ?? string.Empty; + } + + /// + /// 检查值是否为有效的枚举值 + /// + /// 枚举类型 + /// 要检查的值 + /// 是否有效 + public static bool IsDefined(T value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 检查整数值是否为有效的枚举值 + /// + /// 枚举类型 + /// 要检查的整数值 + /// 是否有效 + public static bool IsDefined(int value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 获取枚举的基础类型 + /// + /// 枚举类型 + /// 基础类型 + public static Type GetUnderlyingType() where T : struct, Enum + { + return Enum.GetUnderlyingType(typeof(T)); + } + + /// + /// 获取枚举值的整数形式 + /// + /// 枚举类型 + /// 枚举值 + /// 整数值 + public static int ToInt(T value) where T : struct, Enum + { + return Convert.ToInt32(value); + } + + /// + /// 获取枚举的描述信息列表 + /// + /// 枚举类型 + /// 描述信息列表 + public static List> GetInfoList() where T : struct, Enum + { + return GetValues() + .Select(v => new EnumInfo + { + Value = v, + Name = GetName(v), + IntValue = ToInt(v) + }) + .ToList(); + } + + /// + /// 获取下一个枚举值(循环) + /// + /// 枚举类型 + /// 当前值 + /// 下一个值 + public static T Next(T value) where T : struct, Enum + { + var values = GetValues(); + var index = Array.IndexOf(values, value); + return values[(index + 1) % values.Length]; + } + + /// + /// 获取上一个枚举值(循环) + /// + /// 枚举类型 + /// 当前值 + /// 上一个值 + public static T Previous(T value) where T : struct, Enum + { + var values = GetValues(); + var index = Array.IndexOf(values, value); + return values[(index - 1 + values.Length) % values.Length]; + } + + /// + /// 获取枚举值数量 + /// + /// 枚举类型 + /// 数量 + public static int Count() where T : struct, Enum + { + return Enum.GetNames(typeof(T)).Length; + } + + /// + /// 获取最小枚举值 + /// + /// 枚举类型 + /// 最小值 + public static T Min() where T : struct, Enum + { + return GetValues().Min(); + } + + /// + /// 获取最大枚举值 + /// + /// 枚举类型 + /// 最大值 + public static T Max() where T : struct, Enum + { + return GetValues().Max(); + } + + /// + /// 随机获取一个枚举值 + /// + /// 枚举类型 + /// 随机枚举值 + public static T Random() where T : struct, Enum + { + var values = GetValues(); + return values[GetSharedRandom().Next(values.Length)]; + } + + /// + /// 检查是否为标志枚举(Flags) + /// + /// 枚举类型 + /// 是否为标志枚举 + public static bool IsFlags() where T : struct, Enum + { + return typeof(T).IsDefined(typeof(FlagsAttribute), false); + } + + /// + /// 获取标志枚举中设置的所有标志 + /// + /// 枚举类型 + /// 标志值 + /// 设置的标志列表 + public static List GetFlags(T flags) where T : struct, Enum + { + var result = new List(); + foreach (var value in GetValues()) + { + if (flags.HasFlag(value) && ToInt(value) != 0) + { + result.Add(value); + } + } + return result; + } + + /// + /// 设置标志 + /// + /// 枚举类型 + /// 当前标志 + /// 要设置的标志 + /// 新的标志值 + public static T SetFlag(T flags, T flag) where T : struct, Enum + { + return (T)(object)(ToInt(flags) | ToInt(flag)); + } + + /// + /// 清除标志 + /// + /// 枚举类型 + /// 当前标志 + /// 要清除的标志 + /// 新的标志值 + public static T ClearFlag(T flags, T flag) where T : struct, Enum + { + return (T)(object)(ToInt(flags) & ~ToInt(flag)); + } + + /// + /// 切换标志 + /// + /// 枚举类型 + /// 当前标志 + /// 要切换的标志 + /// 新的标志值 + public static T ToggleFlag(T flags, T flag) where T : struct, Enum + { + return (T)(object)(ToInt(flags) ^ ToInt(flag)); + } + + /// + /// 创建枚举值的字典(名称 -> 值) + /// + /// 枚举类型 + /// 字典 + public static Dictionary ToDictionary() where T : struct, Enum + { + return GetNames().ToDictionary( + name => name, + name => Parse(name)); + } + + /// + /// 创建枚举值的字典(值 -> 名称) + /// + /// 枚举类型 + /// 字典 + public static Dictionary ToValueNameDictionary() where T : struct, Enum + { + return GetValues().ToDictionary( + value => ToInt(value), + value => GetName(value)); + } + } + + /// + /// 枚举信息 + /// + /// 枚举类型 + public class EnumInfo where T : struct, Enum + { + /// + /// 枚举值 + /// + public T Value { get; set; } + + /// + /// 名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 整数值 + /// + public int IntValue { get; set; } + + public override string ToString() => $"{Name} = {IntValue}"; + } +} diff --git a/EasyTool.Core/ToolCategory/RateLimitUtil.cs b/EasyTool.Core/ToolCategory/RateLimitUtil.cs new file mode 100644 index 0000000..0a9b4f4 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RateLimitUtil.cs @@ -0,0 +1,557 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 限流工具类 + /// 提供令牌桶、漏桶、滑动窗口等限流算法 + /// + public static class RateLimitUtil + { + #region 令牌桶限流器 + + /// + /// 创建令牌桶限流器 + /// + /// 桶容量(最大令牌数) + /// 每秒补充的令牌数 + /// 令牌桶限流器 + public static TokenBucketLimiter CreateTokenBucket(int capacity, double refillRate) + { + return new TokenBucketLimiter(capacity, refillRate); + } + + /// + /// 令牌桶限流器 + /// + public class TokenBucketLimiter + { + private readonly int _capacity; + private readonly double _refillRate; + private double _tokens; + private long _lastRefillTime; + private readonly object _lock = new(); + + public TokenBucketLimiter(int capacity, double refillRate) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + if (refillRate <= 0) + throw new ArgumentOutOfRangeException(nameof(refillRate), "补充速率必须大于0"); + + _capacity = capacity; + _refillRate = refillRate; + _tokens = capacity; + _lastRefillTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试获取令牌 + /// + /// 请求的令牌数 + /// 是否获取成功 + public bool TryAcquire(int tokens = 1) + { + lock (_lock) + { + Refill(); + + if (_tokens >= tokens) + { + _tokens -= tokens; + return true; + } + + return false; + } + } + + /// + /// 异步等待获取令牌 + /// + /// 请求的令牌数 + /// 取消令牌 + public async Task WaitAsync(int tokens = 1, CancellationToken cancellationToken = default) + { + while (!TryAcquire(tokens)) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前可用令牌数 + /// + public double AvailableTokens + { + get + { + lock (_lock) + { + Refill(); + return _tokens; + } + } + } + + private void Refill() + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _lastRefillTime; + var refill = elapsed * _refillRate / 1000.0; + + if (refill > 0) + { + _tokens = Math.Min(_capacity, _tokens + refill); + _lastRefillTime = now; + } + } + } + + #endregion + + #region 漏桶限流器 + + /// + /// 创建漏桶限流器 + /// + /// 桶容量 + /// 每秒漏出的请求数 + /// 漏桶限流器 + public static LeakyBucketLimiter CreateLeakyBucket(int capacity, double leakRate) + { + return new LeakyBucketLimiter(capacity, leakRate); + } + + /// + /// 漏桶限流器 + /// + public class LeakyBucketLimiter + { + private readonly int _capacity; + private readonly double _leakRate; + private double _water; + private long _lastLeakTime; + private readonly object _lock = new(); + + public LeakyBucketLimiter(int capacity, double leakRate) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + if (leakRate <= 0) + throw new ArgumentOutOfRangeException(nameof(leakRate), "漏出速率必须大于0"); + + _capacity = capacity; + _leakRate = leakRate; + _water = 0; + _lastLeakTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试添加请求到桶中 + /// + /// 是否添加成功 + public bool TryAcquire() + { + lock (_lock) + { + Leak(); + + if (_water < _capacity) + { + _water++; + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + private void Leak() + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _lastLeakTime; + var leaked = elapsed * _leakRate / 1000.0; + + if (leaked > 0) + { + _water = Math.Max(0, _water - leaked); + _lastLeakTime = now; + } + } + } + + #endregion + + #region 滑动窗口限流器 + + /// + /// 创建滑动窗口限流器 + /// + /// 窗口内最大请求数 + /// 窗口大小(秒) + /// 滑动窗口限流器 + public static SlidingWindowLimiter CreateSlidingWindow(int limit, int windowSeconds) + { + return new SlidingWindowLimiter(limit, windowSeconds); + } + + /// + /// 滑动窗口限流器 + /// + public class SlidingWindowLimiter + { + private readonly int _limit; + private readonly long _windowTicks; + private readonly ConcurrentQueue _timestamps = new(); + private readonly object _lock = new(); + + public SlidingWindowLimiter(int limit, int windowSeconds) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit), "限制必须大于0"); + if (windowSeconds <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSeconds), "窗口大小必须大于0"); + + _limit = limit; + _windowTicks = windowSeconds * 1000L; + } + + /// + /// 尝试通过请求 + /// + /// 是否允许通过 + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var windowStart = now - _windowTicks; + + // 移除过期的请求记录 + while (_timestamps.TryPeek(out var timestamp) && timestamp < windowStart) + { + _timestamps.TryDequeue(out _); + } + + if (_timestamps.Count < _limit) + { + _timestamps.Enqueue(now); + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var windowStart = now - _windowTicks; + + while (_timestamps.TryPeek(out var timestamp) && timestamp < windowStart) + { + _timestamps.TryDequeue(out _); + } + + return _timestamps.Count; + } + } + } + } + + #endregion + + #region 固定窗口限流器 + + /// + /// 创建固定窗口限流器 + /// + /// 窗口内最大请求数 + /// 窗口大小(秒) + /// 固定窗口限流器 + public static FixedWindowLimiter CreateFixedWindow(int limit, int windowSeconds) + { + return new FixedWindowLimiter(limit, windowSeconds); + } + + /// + /// 固定窗口限流器 + /// + public class FixedWindowLimiter + { + private readonly int _limit; + private readonly long _windowTicks; + private int _count; + private long _windowStart; + private readonly object _lock = new(); + + public FixedWindowLimiter(int limit, int windowSeconds) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit), "限制必须大于0"); + if (windowSeconds <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSeconds), "窗口大小必须大于0"); + + _limit = limit; + _windowTicks = windowSeconds * 1000L; + _count = 0; + _windowStart = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试通过请求 + /// + /// 是否允许通过 + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + // 检查是否需要重置窗口 + if (now - _windowStart >= _windowTicks) + { + _windowStart = now; + _count = 0; + } + + if (_count < _limit) + { + _count++; + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + if (now - _windowStart >= _windowTicks) + { + return 0; + } + + return _count; + } + } + } + + /// + /// 获取窗口重置剩余时间(毫秒) + /// + public long ResetIn + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _windowStart; + return Math.Max(0, _windowTicks - elapsed); + } + } + } + } + + #endregion + + #region 并发限流器 + + /// + /// 创建并发限流器 + /// + /// 最大并发数 + /// 并发限流器 + public static ConcurrencyLimiter CreateConcurrency(int maxConcurrency) + { + return new ConcurrencyLimiter(maxConcurrency); + } + + /// + /// 并发限流器 + /// + public class ConcurrencyLimiter + { + private readonly SemaphoreSlim _semaphore; + + public ConcurrencyLimiter(int maxConcurrency) + { + if (maxConcurrency <= 0) + throw new ArgumentOutOfRangeException(nameof(maxConcurrency), "最大并发数必须大于0"); + + _semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency); + } + + /// + /// 获取执行许可 + /// + public async Task AcquireAsync(CancellationToken cancellationToken = default) + { + await _semaphore.WaitAsync(cancellationToken); + return new ReleaseDisposable(_semaphore); + } + + /// + /// 尝试获取执行许可 + /// + public IDisposable? TryAcquire() + { + if (_semaphore.Wait(0)) + { + return new ReleaseDisposable(_semaphore); + } + return null; + } + + /// + /// 当前可用许可数 + /// + public int AvailablePermits => _semaphore.CurrentCount; + + private class ReleaseDisposable : IDisposable + { + private readonly SemaphoreSlim _semaphore; + + public ReleaseDisposable(SemaphoreSlim semaphore) + { + _semaphore = semaphore; + } + + public void Dispose() + { + _semaphore.Release(); + } + } + } + + #endregion + + #region 分布式限流器(内存模拟版) + + /// + /// 创建分布式限流器(基于内存的键值对) + /// + /// 默认限制 + /// 窗口大小(秒) + /// 分布式限流器 + public static DistributedLimiter CreateDistributed(int defaultLimit, int windowSeconds) + { + return new DistributedLimiter(defaultLimit, windowSeconds); + } + + /// + /// 分布式限流器(内存模拟) + /// + public class DistributedLimiter + { + private readonly int _defaultLimit; + private readonly int _windowSeconds; + private readonly ConcurrentDictionary _limiters = new(); + + public DistributedLimiter(int defaultLimit, int windowSeconds) + { + _defaultLimit = defaultLimit; + _windowSeconds = windowSeconds; + } + + /// + /// 尝试通过请求 + /// + /// 限流键(如用户ID、IP等) + /// 是否允许通过 + public bool TryAcquire(string key) + { + var limiter = _limiters.GetOrAdd(key, _ => new FixedWindowLimiter(_defaultLimit, _windowSeconds)); + return limiter.TryAcquire(); + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(string key, CancellationToken cancellationToken = default) + { + while (!TryAcquire(key)) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 移除指定键的限流器 + /// + public void Remove(string key) + { + _limiters.TryRemove(key, out _); + } + + /// + /// 清除所有限流器 + /// + public void Clear() + { + _limiters.Clear(); + } + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/RetryUtil.cs b/EasyTool.Core/ToolCategory/RetryUtil.cs new file mode 100644 index 0000000..cacdaa2 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RetryUtil.cs @@ -0,0 +1,304 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 重试工具类 + /// 提供可配置的重试机制 + /// + public static class RetryUtil + { + /// + /// 执行带重试的操作 + /// + /// 要执行的操作 + /// 最大重试次数 + /// 重试间隔(毫秒) + /// 是否使用指数退避 + public static void Execute(Action action, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + { + Execute(() => + { + action(); + return null; + }, maxRetries, delay, exponentialBackoff); + } + + /// + /// 执行带重试的函数 + /// + public static T Execute(Func func, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + { + Exception lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return func(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt < maxRetries) + { + int currentDelay = exponentialBackoff ? delay * (int)Math.Pow(2, attempt) : delay; + Thread.Sleep(currentDelay); + } + } + } + + throw new RetryException($"Operation failed after {maxRetries + 1} attempts", lastException); + } + + /// + /// 异步执行带重试的操作 + /// + public static async Task ExecuteAsync(Func action, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + { + await ExecuteAsync(async () => + { + await action(); + return null; + }, maxRetries, delay, exponentialBackoff); + } + + /// + /// 异步执行带重试的函数 + /// + public static async Task ExecuteAsync(Func> func, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + { + Exception lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt < maxRetries) + { + int currentDelay = exponentialBackoff ? delay * (int)Math.Pow(2, attempt) : delay; + await Task.Delay(currentDelay); + } + } + } + + throw new RetryException($"Operation failed after {maxRetries + 1} attempts", lastException); + } + + /// + /// 创建重试策略 + /// + public static RetryPolicy CreatePolicy() + { + return new RetryPolicy(); + } + } + + /// + /// 重试策略 + /// + public class RetryPolicy + { + private int _maxRetries = 3; + private int _initialDelay = 1000; + private int _maxDelay = 60000; + private double _backoffMultiplier = 2.0; + private bool _useJitter = true; + private Type[] _retryOnExceptions = { typeof(Exception) }; + private Action _onRetry; + + /// + /// 设置最大重试次数 + /// + public RetryPolicy WithMaxRetries(int maxRetries) + { + _maxRetries = maxRetries; + return this; + } + + /// + /// 设置初始延迟 + /// + public RetryPolicy WithInitialDelay(int milliseconds) + { + _initialDelay = milliseconds; + return this; + } + + /// + /// 设置最大延迟 + /// + public RetryPolicy WithMaxDelay(int milliseconds) + { + _maxDelay = milliseconds; + return this; + } + + /// + /// 设置退避倍数 + /// + public RetryPolicy WithBackoffMultiplier(double multiplier) + { + _backoffMultiplier = multiplier; + return this; + } + + /// + /// 启用或禁用抖动 + /// + public RetryPolicy WithJitter(bool enable = true) + { + _useJitter = enable; + return this; + } + + /// + /// 设置要重试的异常类型 + /// + public RetryPolicy RetryOn() where TException : Exception + { + var list = new System.Collections.Generic.List(_retryOnExceptions) { typeof(TException) }; + _retryOnExceptions = list.ToArray(); + return this; + } + + /// + /// 设置重试回调 + /// + public RetryPolicy OnRetry(Action onRetry) + { + _onRetry = onRetry; + return this; + } + + /// + /// 执行操作 + /// + public void Execute(Action action) + { + Execute(() => + { + action(); + return null; + }); + } + + /// + /// 执行函数 + /// + public T Execute(Func func) + { + Exception lastException = null; + + for (int attempt = 0; attempt <= _maxRetries; attempt++) + { + try + { + return func(); + } + catch (Exception ex) + { + lastException = ex; + + if (!ShouldRetry(ex) || attempt >= _maxRetries) + break; + + TimeSpan delay = CalculateDelay(attempt); + _onRetry?.Invoke(ex, attempt + 1, delay); + Thread.Sleep(delay); + } + } + + throw new RetryException($"Operation failed after {_maxRetries + 1} attempts", lastException); + } + + /// + /// 异步执行操作 + /// + public async Task ExecuteAsync(Func action) + { + await ExecuteAsync(async () => + { + await action(); + return null; + }); + } + + /// + /// 异步执行函数 + /// + public async Task ExecuteAsync(Func> func) + { + Exception lastException = null; + + for (int attempt = 0; attempt <= _maxRetries; attempt++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (!ShouldRetry(ex) || attempt >= _maxRetries) + break; + + TimeSpan delay = CalculateDelay(attempt); + _onRetry?.Invoke(ex, attempt + 1, delay); + await Task.Delay(delay); + } + } + + throw new RetryException($"Operation failed after {_maxRetries + 1} attempts", lastException); + } + + private bool ShouldRetry(Exception ex) + { + foreach (var type in _retryOnExceptions) + { + if (type.IsAssignableFrom(ex.GetType())) + return true; + } + return false; + } + + private TimeSpan CalculateDelay(int attempt) + { + double delay = _initialDelay * Math.Pow(_backoffMultiplier, attempt); + + if (_useJitter) + { + // 添加随机抖动 (±20%) + var random = new Random(); + double jitter = 0.8 + random.NextDouble() * 0.4; + delay *= jitter; + } + + return TimeSpan.FromMilliseconds(Math.Min(delay, _maxDelay)); + } + } + + /// + /// 重试异常 + /// + public class RetryException : Exception + { + /// + /// 创建重试异常 + /// + public RetryException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} diff --git a/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs b/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs new file mode 100644 index 0000000..a0f515a --- /dev/null +++ b/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs @@ -0,0 +1,620 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 线程池管理工具类 + /// 提供线程池的创建、管理和监控功能 + /// + public static class ThreadPoolUtil + { + /// + /// 创建自定义线程池 + /// + /// 最小线程数 + /// 最大线程数 + /// 自定义线程池实例 + public static CustomThreadPool Create(int minThreads = 1, int maxThreads = 10) + { + return new CustomThreadPool(minThreads, maxThreads); + } + + /// + /// 创建固定大小的线程池 + /// + /// 线程数量 + /// 固定大小线程池实例 + public static FixedThreadPool CreateFixed(int threadCount) + { + return new FixedThreadPool(threadCount); + } + + /// + /// 获取全局线程池信息 + /// + /// 线程池信息 + public static ThreadPoolInfo GetGlobalPoolInfo() + { + ThreadPool.GetMinThreads(out int minWorkerThreads, out int minCompletionPortThreads); + ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads); + ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableCompletionPortThreads); + + return new ThreadPoolInfo + { + MinWorkerThreads = minWorkerThreads, + MinCompletionPortThreads = minCompletionPortThreads, + MaxWorkerThreads = maxWorkerThreads, + MaxCompletionPortThreads = maxCompletionPortThreads, + AvailableWorkerThreads = availableWorkerThreads, + AvailableCompletionPortThreads = availableCompletionPortThreads, + ActiveWorkerThreads = maxWorkerThreads - availableWorkerThreads, + ActiveCompletionPortThreads = maxCompletionPortThreads - availableCompletionPortThreads + }; + } + + /// + /// 设置全局线程池大小 + /// + /// 最小线程数 + /// 最大线程数 + /// 是否设置成功 + public static bool SetGlobalPoolSize(int minThreads, int maxThreads) + { + return ThreadPool.SetMinThreads(minThreads, minThreads) && + ThreadPool.SetMaxThreads(maxThreads, maxThreads); + } + + /// + /// 设置全局线程池最小线程数 + /// + /// 最小线程数 + /// 是否设置成功 + public static bool SetGlobalMinThreads(int minThreads) + { + return ThreadPool.SetMinThreads(minThreads, minThreads); + } + + /// + /// 设置全局线程池最大线程数 + /// + /// 最大线程数 + /// 是否设置成功 + public static bool SetGlobalMaxThreads(int maxThreads) + { + return ThreadPool.SetMaxThreads(maxThreads, maxThreads); + } + + /// + /// 等待所有任务完成 + /// + /// 要等待的任务数组 + /// 超时时间(可选) + /// 是否在超时前完成 + public static bool WaitAll(Task[] tasks, TimeSpan? timeout = null) + { + if (tasks == null || tasks.Length == 0) + return true; + + if (timeout.HasValue) + { + return Task.WaitAll(tasks, timeout.Value); + } + Task.WaitAll(tasks); + return true; + } + + /// + /// 异步等待所有任务完成 + /// + /// 要等待的任务数组 + /// Task + public static async Task WaitAllAsync(Task[] tasks) + { + if (tasks == null || tasks.Length == 0) + return; + + await Task.WhenAll(tasks); + } + } + + /// + /// 线程池信息 + /// + public class ThreadPoolInfo + { + /// + /// 最小工作线程数 + /// + public int MinWorkerThreads { get; set; } + + /// + /// 最小完成端口线程数 + /// + public int MinCompletionPortThreads { get; set; } + + /// + /// 最大工作线程数 + /// + public int MaxWorkerThreads { get; set; } + + /// + /// 最大完成端口线程数 + /// + public int MaxCompletionPortThreads { get; set; } + + /// + /// 可用工作线程数 + /// + public int AvailableWorkerThreads { get; set; } + + /// + /// 可用完成端口线程数 + /// + public int AvailableCompletionPortThreads { get; set; } + + /// + /// 活跃工作线程数 + /// + public int ActiveWorkerThreads { get; set; } + + /// + /// 活跃完成端口线程数 + /// + public int ActiveCompletionPortThreads { get; set; } + + /// + /// 总活跃线程数 + /// + public int TotalActiveThreads => ActiveWorkerThreads + ActiveCompletionPortThreads; + + /// + /// 线程池使用率(0-1) + /// + public double UsageRate => MaxWorkerThreads > 0 ? (double)ActiveWorkerThreads / MaxWorkerThreads : 0; + + public override string ToString() + { + return $"Worker: {ActiveWorkerThreads}/{MaxWorkerThreads} (Min: {MinWorkerThreads}), " + + $"IOCP: {ActiveCompletionPortThreads}/{MaxCompletionPortThreads} (Min: {MinCompletionPortThreads}), " + + $"Usage: {UsageRate:P1}"; + } + } + + /// + /// 自定义线程池 + /// + public class CustomThreadPool : IDisposable + { + private readonly BlockingCollection _taskQueue; + private readonly Thread[] _threads; + private readonly CancellationTokenSource _cts; + private readonly SemaphoreSlim _semaphore; + private int _activeTasks; + private bool _disposed; + + /// + /// 最小线程数 + /// + public int MinThreads { get; } + + /// + /// 最大线程数 + /// + public int MaxThreads { get; } + + /// + /// 当前活跃线程数 + /// + public int ActiveThreads => _activeTasks; + + /// + /// 队列中等待的任务数 + /// + public int QueuedTasks => _taskQueue.Count; + + /// + /// 是否已关闭 + /// + public bool IsShutdown => _cts.IsCancellationRequested; + + /// + /// 创建自定义线程池 + /// + /// 最小线程数 + /// 最大线程数 + public CustomThreadPool(int minThreads = 1, int maxThreads = 10) + { + if (minThreads < 1) + throw new ArgumentOutOfRangeException(nameof(minThreads), "最小线程数必须大于0"); + if (maxThreads < minThreads) + throw new ArgumentOutOfRangeException(nameof(maxThreads), "最大线程数不能小于最小线程数"); + + MinThreads = minThreads; + MaxThreads = maxThreads; + + _taskQueue = new BlockingCollection(maxThreads * 10); + _cts = new CancellationTokenSource(); + _semaphore = new SemaphoreSlim(maxThreads, maxThreads); + _threads = new Thread[maxThreads]; + _activeTasks = 0; + + // 启动最小数量的线程 + for (int i = 0; i < minThreads; i++) + { + StartThread(i); + } + } + + /// + /// 提交任务 + /// + /// 要执行的操作 + public void Submit(Action action) + { + ThrowIfDisposed(); + _taskQueue.Add(action ?? throw new ArgumentNullException(nameof(action))); + } + + /// + /// 提交任务并返回 Task + /// + /// 要执行的操作 + /// Task 对象 + public Task SubmitAsync(Action action) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + action(); + tcs.SetResult(true); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 提交带返回值的任务 + /// + /// 返回值类型 + /// 要执行的函数 + /// 返回值的 Task + public Task SubmitAsync(Func func) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + tcs.SetResult(func()); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 关闭线程池 + /// + /// 是否等待任务完成 + /// 超时时间 + public void Shutdown(bool waitForCompletion = true, TimeSpan? timeout = null) + { + ThrowIfDisposed(); + _taskQueue.CompleteAdding(); + _cts.Cancel(); + + if (waitForCompletion) + { + if (timeout.HasValue) + { + foreach (var thread in _threads) + { + thread?.Join(timeout.Value); + } + } + else + { + foreach (var thread in _threads) + { + thread?.Join(); + } + } + } + } + + /// + /// 等待所有任务完成 + /// + /// 超时时间 + /// 是否在超时前完成 + public bool WaitAll(TimeSpan? timeout = null) + { + if (timeout.HasValue) + { + return _semaphore.Wait(timeout.Value); + } + _semaphore.Wait(); + _semaphore.Release(); + return true; + } + + private void StartThread(int index) + { + var thread = new Thread(Worker) + { + IsBackground = true, + Name = $"CustomThreadPool-{index}" + }; + _threads[index] = thread; + thread.Start(); + } + + private void Worker() + { + foreach (var action in _taskQueue.GetConsumingEnumerable(_cts.Token)) + { + Interlocked.Increment(ref _activeTasks); + try + { + _semaphore.Wait(_cts.Token); + try + { + action(); + } + finally + { + _semaphore.Release(); + } + } + catch (OperationCanceledException) + { + break; + } + finally + { + Interlocked.Decrement(ref _activeTasks); + } + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + throw new ObjectDisposedException(nameof(CustomThreadPool)); + } + + public void Dispose() + { + if (_disposed) + return; + + Shutdown(false); + _taskQueue.Dispose(); + _cts.Dispose(); + _semaphore.Dispose(); + _disposed = true; + } + } + + /// + /// 固定大小线程池 + /// + public class FixedThreadPool : IDisposable + { + private readonly BlockingCollection _taskQueue; + private readonly Thread[] _threads; + private readonly CancellationTokenSource _cts; + private int _activeTasks; + private bool _disposed; + + /// + /// 线程数量 + /// + public int ThreadCount { get; } + + /// + /// 当前活跃线程数 + /// + public int ActiveThreads => _activeTasks; + + /// + /// 队列中等待的任务数 + /// + public int QueuedTasks => _taskQueue.Count; + + /// + /// 是否已关闭 + /// + public bool IsShutdown => _cts.IsCancellationRequested; + + /// + /// 创建固定大小线程池 + /// + /// 线程数量 + public FixedThreadPool(int threadCount) + { + if (threadCount < 1) + throw new ArgumentOutOfRangeException(nameof(threadCount), "线程数量必须大于0"); + + ThreadCount = threadCount; + _taskQueue = new BlockingCollection(); + _cts = new CancellationTokenSource(); + _threads = new Thread[threadCount]; + + for (int i = 0; i < threadCount; i++) + { + var thread = new Thread(Worker) + { + IsBackground = true, + Name = $"FixedThreadPool-{i}" + }; + _threads[i] = thread; + thread.Start(); + } + } + + /// + /// 提交任务 + /// + /// 要执行的操作 + public void Submit(Action action) + { + ThrowIfDisposed(); + _taskQueue.Add(action ?? throw new ArgumentNullException(nameof(action))); + } + + /// + /// 提交任务并返回 Task + /// + /// 要执行的操作 + /// Task 对象 + public Task SubmitAsync(Action action) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + action(); + tcs.SetResult(true); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 提交带返回值的任务 + /// + /// 返回值类型 + /// 要执行的函数 + /// 返回值的 Task + public Task SubmitAsync(Func func) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + tcs.SetResult(func()); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 关闭线程池 + /// + /// 是否等待任务完成 + /// 超时时间 + public void Shutdown(bool waitForCompletion = true, TimeSpan? timeout = null) + { + ThrowIfDisposed(); + _taskQueue.CompleteAdding(); + _cts.Cancel(); + + if (waitForCompletion) + { + if (timeout.HasValue) + { + foreach (var thread in _threads) + { + thread?.Join(timeout.Value); + } + } + else + { + foreach (var thread in _threads) + { + thread?.Join(); + } + } + } + } + + /// + /// 等待队列清空 + /// + /// 超时时间 + /// 是否在超时前完成 + public bool WaitUntilEmpty(TimeSpan? timeout = null) + { + var startTime = DateTime.UtcNow; + while (_taskQueue.Count > 0 || _activeTasks > 0) + { + if (timeout.HasValue && (DateTime.UtcNow - startTime) >= timeout.Value) + return false; + Thread.Sleep(10); + } + return true; + } + + private void Worker() + { + foreach (var action in _taskQueue.GetConsumingEnumerable(_cts.Token)) + { + Interlocked.Increment(ref _activeTasks); + try + { + action(); + } + catch + { + // 忽略任务执行异常 + } + finally + { + Interlocked.Decrement(ref _activeTasks); + } + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + throw new ObjectDisposedException(nameof(FixedThreadPool)); + } + + public void Dispose() + { + if (_disposed) + return; + + Shutdown(false); + _taskQueue.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } +} diff --git a/EasyTool.Core/ToolCategory/ValidatorUtil.cs b/EasyTool.Core/ToolCategory/ValidatorUtil.cs new file mode 100644 index 0000000..b66e81d --- /dev/null +++ b/EasyTool.Core/ToolCategory/ValidatorUtil.cs @@ -0,0 +1,696 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.ToolCategory +{ + /// + /// 验证结果 + /// + public class ValidationResult + { + /// + /// 是否验证通过 + /// + public bool IsValid { get; set; } + + /// + /// 错误信息列表 + /// + public List Errors { get; set; } = new(); + + /// + /// 创建成功结果 + /// + public static ValidationResult Success => new() { IsValid = true }; + + /// + /// 创建失败结果 + /// + public static ValidationResult Fail(params string[] errors) => new() + { + IsValid = false, + Errors = errors.ToList() + }; + + /// + /// 合并多个验证结果 + /// + public static ValidationResult Combine(params ValidationResult[] results) + { + var combined = new ValidationResult { IsValid = true }; + + foreach (var result in results) + { + if (!result.IsValid) + { + combined.IsValid = false; + combined.Errors.AddRange(result.Errors); + } + } + + return combined; + } + } + + /// + /// 验证规则构建器 + /// + /// 值类型 + public class ValidatorBuilder + { + private readonly List> _rules = new(); + protected readonly string _fieldName; + + public ValidatorBuilder(string fieldName = "value") + { + _fieldName = fieldName; + } + + /// + /// 添加自定义验证规则 + /// + public ValidatorBuilder AddRule(Func rule, string errorMessage) + { + _rules.Add(value => rule(value) + ? ValidationResult.Success + : ValidationResult.Fail(errorMessage)); + return this; + } + + /// + /// 添加自定义验证规则 + /// + public ValidatorBuilder AddRule(Func rule) + { + _rules.Add(rule); + return this; + } + + #region 通用规则 + + /// + /// 不能为默认值 + /// + public ValidatorBuilder NotDefault(string? message = null) + { + _rules.Add(value => !EqualityComparer.Default.Equals(value, default!) + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}不能为默认值")); + return this; + } + + /// + /// 满足条件 + /// + public ValidatorBuilder Must(Func predicate, string? message = null) + { + _rules.Add(value => predicate(value) + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}不满足条件")); + return this; + } + + /// + /// 枚举值验证 + /// + public ValidatorBuilder IsEnum(string? message = null) + { + _rules.Add(value => + { + if (value == null) + return ValidationResult.Fail(message ?? $"{_fieldName}不能为空"); + + var type = typeof(T); + if (!type.IsEnum && (!Nullable.GetUnderlyingType(type)?.IsEnum ?? true)) + return ValidationResult.Fail($"{_fieldName}不是枚举类型"); + + var enumType = Nullable.GetUnderlyingType(type) ?? type; + return Enum.IsDefined(enumType, value) + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}不是有效的枚举值"); + }); + return this; + } + + #endregion + + #region 数值规则 + + /// + /// 大于指定值 + /// + public ValidatorBuilder GreaterThan(T compareValue, string? message = null) + { + _rules.Add(value => + { + if (value is IComparable comparable) + { + return comparable.CompareTo(compareValue) > 0 + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}必须大于 {compareValue}"); + } + return ValidationResult.Fail($"{_fieldName}类型不可比较"); + }); + return this; + } + + /// + /// 大于等于指定值 + /// + public ValidatorBuilder GreaterThanOrEqual(T compareValue, string? message = null) + { + _rules.Add(value => + { + if (value is IComparable comparable) + { + return comparable.CompareTo(compareValue) >= 0 + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}必须大于等于 {compareValue}"); + } + return ValidationResult.Fail($"{_fieldName}类型不可比较"); + }); + return this; + } + + /// + /// 小于指定值 + /// + public ValidatorBuilder LessThan(T compareValue, string? message = null) + { + _rules.Add(value => + { + if (value is IComparable comparable) + { + return comparable.CompareTo(compareValue) < 0 + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}必须小于 {compareValue}"); + } + return ValidationResult.Fail($"{_fieldName}类型不可比较"); + }); + return this; + } + + /// + /// 小于等于指定值 + /// + public ValidatorBuilder LessThanOrEqual(T compareValue, string? message = null) + { + _rules.Add(value => + { + if (value is IComparable comparable) + { + return comparable.CompareTo(compareValue) <= 0 + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}必须小于等于 {compareValue}"); + } + return ValidationResult.Fail($"{_fieldName}类型不可比较"); + }); + return this; + } + + /// + /// 在指定范围内 + /// + public ValidatorBuilder InRange(T min, T max, string? message = null) + { + _rules.Add(value => + { + if (value is IComparable comparable) + { + var valid = comparable.CompareTo(min) >= 0 && comparable.CompareTo(max) <= 0; + return valid + ? ValidationResult.Success + : ValidationResult.Fail(message ?? $"{_fieldName}必须在 {min} 和 {max} 之间"); + } + return ValidationResult.Fail($"{_fieldName}类型不可比较"); + }); + return this; + } + + #endregion + + #region 构建验证器 + + /// + /// 构建验证器 + /// + public Func Build() + { + var rules = _rules.ToList(); + return value => + { + var result = ValidationResult.Success; + foreach (var rule in rules) + { + var ruleResult = rule(value); + if (!ruleResult.IsValid) + { + result.IsValid = false; + result.Errors.AddRange(ruleResult.Errors); + } + } + return result; + }; + } + + /// + /// 验证值 + /// + public ValidationResult Validate(T value) + { + return Build()(value); + } + + #endregion + } + + /// + /// 字符串验证规则构建器 + /// + public class StringValidatorBuilder : ValidatorBuilder + { + public StringValidatorBuilder(string fieldName = "value") : base(fieldName) { } + + /// + /// 不能为空或空白 + /// + public StringValidatorBuilder NotEmpty(string? message = null) + { + AddRule(value => !string.IsNullOrWhiteSpace(value), + message ?? $"{_fieldName}不能为空"); + return this; + } + + /// + /// 最小长度 + /// + public StringValidatorBuilder MinLength(int minLength, string? message = null) + { + AddRule(value => value != null && value.Length >= minLength, + message ?? $"{_fieldName}长度不能小于 {minLength}"); + return this; + } + + /// + /// 最大长度 + /// + public StringValidatorBuilder MaxLength(int maxLength, string? message = null) + { + AddRule(value => value == null || value.Length <= maxLength, + message ?? $"{_fieldName}长度不能超过 {maxLength}"); + return this; + } + + /// + /// 长度范围 + /// + public StringValidatorBuilder Length(int minLength, int maxLength, string? message = null) + { + AddRule(value => value != null && value.Length >= minLength && value.Length <= maxLength, + message ?? $"{_fieldName}长度必须在 {minLength} 到 {maxLength} 之间"); + return this; + } + + /// + /// 匹配正则表达式 + /// + public StringValidatorBuilder Matches(string pattern, string? message = null) + { + AddRule(value => value != null && Regex.IsMatch(value, pattern), + message ?? $"{_fieldName}格式不正确"); + return this; + } + + /// + /// 匹配正则表达式 + /// + public StringValidatorBuilder Matches(Regex regex, string? message = null) + { + AddRule(value => value != null && regex.IsMatch(value), + message ?? $"{_fieldName}格式不正确"); + return this; + } + + /// + /// 邮箱格式 + /// + public StringValidatorBuilder Email(string? message = null) + { + const string emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$"; + return Matches(emailPattern, message ?? $"{_fieldName}不是有效的邮箱地址"); + } + + /// + /// 手机号格式(中国大陆) + /// + public StringValidatorBuilder Phone(string? message = null) + { + const string phonePattern = @"^1[3-9]\d{9}$"; + return Matches(phonePattern, message ?? $"{_fieldName}不是有效的手机号"); + } + + /// + /// 身份证号格式(中国大陆) + /// + public StringValidatorBuilder IdCard(string? message = null) + { + const string idCardPattern = @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"; + return Matches(idCardPattern, message ?? $"{_fieldName}不是有效的身份证号"); + } + + /// + /// URL格式 + /// + public StringValidatorBuilder Url(string? message = null) + { + const string urlPattern = @"^https?://[^\s/$.?#].[^\s]*$"; + return Matches(urlPattern, message ?? $"{_fieldName}不是有效的URL"); + } + + /// + /// 纯数字 + /// + public StringValidatorBuilder Numeric(string? message = null) + { + return Matches(@"^\d+$", message ?? $"{_fieldName}必须为纯数字"); + } + + /// + /// 纯字母 + /// + public StringValidatorBuilder Alpha(string? message = null) + { + return Matches(@"^[a-zA-Z]+$", message ?? $"{_fieldName}必须为纯字母"); + } + + /// + /// 字母数字 + /// + public StringValidatorBuilder Alphanumeric(string? message = null) + { + return Matches(@"^[a-zA-Z0-9]+$", message ?? $"{_fieldName}必须为字母或数字"); + } + + /// + /// 包含数字 + /// + public StringValidatorBuilder ContainsDigit(string? message = null) + { + return Matches(@"\d", message ?? $"{_fieldName}必须包含数字"); + } + + /// + /// 包含小写字母 + /// + public StringValidatorBuilder ContainsLower(string? message = null) + { + return Matches(@"[a-z]", message ?? $"{_fieldName}必须包含小写字母"); + } + + /// + /// 包含大写字母 + /// + public StringValidatorBuilder ContainsUpper(string? message = null) + { + return Matches(@"[A-Z]", message ?? $"{_fieldName}必须包含大写字母"); + } + + /// + /// 包含特殊字符 + /// + public StringValidatorBuilder ContainsSpecial(string? message = null) + { + return Matches(@"[!@#$%^&*(),.?"":{}|<>]", message ?? $"{_fieldName}必须包含特殊字符"); + } + + /// + /// 密码强度验证 + /// + /// 最小长度 + /// 需要数字 + /// 需要小写字母 + /// 需要大写字母 + /// 需要特殊字符 + /// 错误消息 + public StringValidatorBuilder Password( + int minLength = 8, + bool requireDigit = true, + bool requireLower = true, + bool requireUpper = true, + bool requireSpecial = false, + string? message = null) + { + MinLength(minLength); + + if (requireDigit) ContainsDigit(); + if (requireLower) ContainsLower(); + if (requireUpper) ContainsUpper(); + if (requireSpecial) ContainsSpecial(); + + if (!string.IsNullOrEmpty(message)) + { + AddRule(_ => false, message); + } + + return this; + } + } + + /// + /// 集合验证规则构建器 + /// + public class CollectionValidatorBuilder : ValidatorBuilder?> + { + public CollectionValidatorBuilder(string fieldName = "collection") : base(fieldName) { } + + /// + /// 不能为空集合 + /// + public CollectionValidatorBuilder NotEmpty(string? message = null) + { + AddRule(value => value != null && value.Any(), + message ?? $"{_fieldName}不能为空"); + return this; + } + + /// + /// 最小元素数量 + /// + public CollectionValidatorBuilder MinCount(int minCount, string? message = null) + { + AddRule(value => value != null && value.Count() >= minCount, + message ?? $"{_fieldName}元素数量不能少于 {minCount}"); + return this; + } + + /// + /// 最大元素数量 + /// + public CollectionValidatorBuilder MaxCount(int maxCount, string? message = null) + { + AddRule(value => value == null || value.Count() <= maxCount, + message ?? $"{_fieldName}元素数量不能超过 {maxCount}"); + return this; + } + + /// + /// 元素数量范围 + /// + public CollectionValidatorBuilder Count(int minCount, int maxCount, string? message = null) + { + AddRule(value => + { + if (value == null) return false; + var count = value.Count(); + return count >= minCount && count <= maxCount; + }, message ?? $"{_fieldName}元素数量必须在 {minCount} 到 {maxCount} 之间"); + return this; + } + + /// + /// 所有元素满足条件 + /// + public CollectionValidatorBuilder All(Func predicate, string? message = null) + { + AddRule(value => value == null || value.All(predicate), + message ?? $"{_fieldName}中存在不满足条件的元素"); + return this; + } + + /// + /// 至少一个元素满足条件 + /// + public CollectionValidatorBuilder Any(Func predicate, string? message = null) + { + AddRule(value => value != null && value.Any(predicate), + message ?? $"{_fieldName}中没有满足条件的元素"); + return this; + } + + /// + /// 不包含重复元素 + /// + public CollectionValidatorBuilder Distinct(string? message = null) + { + AddRule(value => + { + if (value == null) return true; + var list = value.ToList(); + return list.Count == list.Distinct().Count(); + }, message ?? $"{_fieldName}包含重复元素"); + return this; + } + + /// + /// 包含指定元素 + /// + public CollectionValidatorBuilder Contains(T item, string? message = null) + { + AddRule(value => value != null && value.Contains(item), + message ?? $"{_fieldName}不包含指定元素"); + return this; + } + + } + + /// + /// 通用验证工具类 + /// + public static class ValidatorUtil + { + /// + /// 创建字符串验证器 + /// + public static StringValidatorBuilder ForString(string fieldName = "value") + { + return new StringValidatorBuilder(fieldName); + } + + /// + /// 创建数值验证器 + /// + public static ValidatorBuilder ForNumber(string fieldName = "value") where T : IComparable + { + return new ValidatorBuilder(fieldName); + } + + /// + /// 创建集合验证器 + /// + public static CollectionValidatorBuilder ForCollection(string fieldName = "collection") + { + return new CollectionValidatorBuilder(fieldName); + } + + /// + /// 创建自定义验证器 + /// + public static ValidatorBuilder For(string fieldName = "value") + { + return new ValidatorBuilder(fieldName); + } + + #region 快捷验证方法 + + /// + /// 验证字符串不为空 + /// + public static bool IsNotEmpty(string? value) + { + return !string.IsNullOrWhiteSpace(value); + } + + /// + /// 验证邮箱格式 + /// + public static bool IsEmail(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + return Regex.IsMatch(value, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"); + } + + /// + /// 验证手机号格式(中国大陆) + /// + public static bool IsPhone(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + return Regex.IsMatch(value, @"^1[3-9]\d{9}$"); + } + + /// + /// 验证身份证号格式(中国大陆) + /// + public static bool IsIdCard(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + return Regex.IsMatch(value, @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"); + } + + /// + /// 验证URL格式 + /// + public static bool IsUrl(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + return Regex.IsMatch(value, @"^https?://[^\s/$.?#].[^\s]*$", RegexOptions.IgnoreCase); + } + + /// + /// 验证是否为纯数字 + /// + public static bool IsNumeric(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + return Regex.IsMatch(value, @"^\d+$"); + } + + /// + /// 验证是否在范围内 + /// + public static bool InRange(T value, T min, T max) where T : IComparable + { + if (value == null) + return false; + return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0; + } + + /// + /// 验证字符串长度 + /// + public static bool LengthInRange(string? value, int minLength, int maxLength) + { + if (value == null) + return minLength <= 0; + return value.Length >= minLength && value.Length <= maxLength; + } + + /// + /// 验证集合不为空 + /// + public static bool IsNotEmpty(IEnumerable? collection) + { + return collection != null && collection.Any(); + } + + /// + /// 验证集合元素数量 + /// + public static bool CountInRange(IEnumerable? collection, int minCount, int maxCount) + { + if (collection == null) + return minCount <= 0; + var count = collection.Count(); + return count >= minCount && count <= maxCount; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/VersionUtil.cs b/EasyTool.Core/ToolCategory/VersionUtil.cs new file mode 100644 index 0000000..bfa3eb6 --- /dev/null +++ b/EasyTool.Core/ToolCategory/VersionUtil.cs @@ -0,0 +1,475 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 版本号工具类 + /// 提供版本号解析、比较和验证功能 + /// + public static class VersionUtil + { + /// + /// 解析版本号字符串 + /// + /// 版本号字符串(如 "1.2.3" 或 "v1.2.3-beta") + /// 版本信息对象 + public static VersionInfo Parse(string? version) + { + if (string.IsNullOrWhiteSpace(version)) + throw new ArgumentException("版本号不能为空"); + + // 移除 v 或 V 前缀 + var normalized = version.TrimStart('v', 'V'); + + // 分离预发布标签 + string? preRelease = null; + var preReleaseIndex = normalized.IndexOf('-'); + if (preReleaseIndex >= 0) + { + preRelease = normalized.Substring(preReleaseIndex + 1); + normalized = normalized.Substring(0, preReleaseIndex); + } + + // 分离构建元数据 + string? buildMetadata = null; + var buildIndex = normalized.IndexOf('+'); + if (buildIndex >= 0) + { + buildMetadata = normalized.Substring(buildIndex + 1); + normalized = normalized.Substring(0, buildIndex); + } + + // 解析版本号部分 + var parts = normalized.Split('.'); + if (parts.Length == 0 || parts.Length > 4) + throw new FormatException($"无效的版本号格式: {version}"); + + var info = new VersionInfo + { + Original = version, + PreRelease = preRelease, + BuildMetadata = buildMetadata + }; + + if (int.TryParse(parts[0], out var major)) + info.Major = major; + else + throw new FormatException($"无效的主版本号: {parts[0]}"); + + if (parts.Length > 1) + { + if (int.TryParse(parts[1], out var minor)) + info.Minor = minor; + else + throw new FormatException($"无效的次版本号: {parts[1]}"); + } + + if (parts.Length > 2) + { + if (int.TryParse(parts[2], out var patch)) + info.Patch = patch; + else + throw new FormatException($"无效的补丁版本号: {parts[2]}"); + } + + if (parts.Length > 3) + { + if (int.TryParse(parts[3], out var revision)) + info.Revision = revision; + else + throw new FormatException($"无效的修订版本号: {parts[3]}"); + } + + return info; + } + + /// + /// 尝试解析版本号 + /// + /// 版本号字符串 + /// 解析结果 + /// 是否解析成功 + public static bool TryParse(string? version, out VersionInfo? info) + { + info = null; + if (string.IsNullOrWhiteSpace(version)) + return false; + + try + { + info = Parse(version); + return true; + } + catch + { + return false; + } + } + + /// + /// 比较两个版本号 + /// + /// 版本号1 + /// 版本号2 + /// 比较结果:-1表示小于,0表示等于,1表示大于 + public static int Compare(string? version1, string? version2) + { + if (!TryParse(version1, out var info1)) + info1 = new VersionInfo(); + if (!TryParse(version2, out var info2)) + info2 = new VersionInfo(); + + return Compare(info1!, info2!); + } + + /// + /// 比较两个版本信息 + /// + /// 版本1 + /// 版本2 + /// 比较结果 + public static int Compare(VersionInfo v1, VersionInfo v2) + { + if (v1.Major != v2.Major) + return v1.Major.CompareTo(v2.Major); + if (v1.Minor != v2.Minor) + return v1.Minor.CompareTo(v2.Minor); + if (v1.Patch != v2.Patch) + return v1.Patch.CompareTo(v2.Patch); + if (v1.Revision != v2.Revision) + return v1.Revision.CompareTo(v2.Revision); + + // 主版本号相同时,比较预发布标签 + // 没有预发布标签的版本高于有预发布标签的版本 + if (string.IsNullOrEmpty(v1.PreRelease) && !string.IsNullOrEmpty(v2.PreRelease)) + return 1; + if (!string.IsNullOrEmpty(v1.PreRelease) && string.IsNullOrEmpty(v2.PreRelease)) + return -1; + + if (!string.IsNullOrEmpty(v1.PreRelease) && !string.IsNullOrEmpty(v2.PreRelease)) + return string.Compare(v1.PreRelease, v2.PreRelease, StringComparison.Ordinal); + + return 0; + } + + /// + /// 判断版本号是否在指定范围内 + /// + /// 要检查的版本号 + /// 最小版本号(包含) + /// 最大版本号(包含) + /// 是否在范围内 + public static bool IsInRange(string? version, string? min, string? max) + { + var info = Parse(version); + var minInfo = string.IsNullOrEmpty(min) ? null : Parse(min); + var maxInfo = string.IsNullOrEmpty(max) ? null : Parse(max); + + if (minInfo != null && Compare(info, minInfo) < 0) + return false; + if (maxInfo != null && Compare(info, maxInfo) > 0) + return false; + + return true; + } + + /// + /// 获取下一个版本号 + /// + /// 当前版本号 + /// 递增级别:Major, Minor, Patch + /// 下一个版本号 + public static string Next(string? version, VersionLevel level = VersionLevel.Patch) + { + var info = TryParse(version, out var v) ? v! : new VersionInfo(); + + return level switch + { + VersionLevel.Major => $"{info!.Major + 1}.0.0", + VersionLevel.Minor => $"{info!.Major}.{info.Minor + 1}.0", + VersionLevel.Patch => $"{info!.Major}.{info.Minor}.{info.Patch + 1}", + VersionLevel.Revision => $"{info!.Major}.{info.Minor}.{info.Patch}.{info.Revision + 1}", + _ => info!.ToString() + }; + } + + /// + /// 获取版本号之间的差异描述 + /// + /// 旧版本 + /// 新版本 + /// 差异描述 + public static VersionDiff GetDiff(string? oldVersion, string? newVersion) + { + var oldInfo = TryParse(oldVersion, out var old) ? old! : new VersionInfo(); + var newInfo = TryParse(newVersion, out var newV) ? newV! : new VersionInfo(); + + var diff = new VersionDiff + { + OldVersion = oldInfo, + NewVersion = newInfo, + MajorDiff = newInfo.Major - oldInfo.Major, + MinorDiff = newInfo.Minor - oldInfo.Minor, + PatchDiff = newInfo.Patch - oldInfo.Patch, + RevisionDiff = newInfo.Revision - oldInfo.Revision + }; + + if (diff.MajorDiff != 0) + diff.ChangeLevel = VersionLevel.Major; + else if (diff.MinorDiff != 0) + diff.ChangeLevel = VersionLevel.Minor; + else if (diff.PatchDiff != 0) + diff.ChangeLevel = VersionLevel.Patch; + else if (diff.RevisionDiff != 0) + diff.ChangeLevel = VersionLevel.Revision; + + return diff; + } + + /// + /// 从列表中找到最接近目标版本号的版本 + /// + /// 版本号列表 + /// 目标版本号 + /// 最接近的版本号 + public static string? FindClosest(IEnumerable versions, string target) + { + if (versions == null || string.IsNullOrEmpty(target)) + return null; + + var targetInfo = Parse(target); + string? closest = null; + var minDiff = int.MaxValue; + + foreach (var version in versions) + { + if (!TryParse(version, out var info)) + continue; + + var diff = Math.Abs(Compare(info!, targetInfo)); + if (diff < minDiff) + { + minDiff = diff; + closest = version; + } + } + + return closest; + } + + /// + /// 验证版本号是否符合语义化版本规范(SemVer) + /// + /// 版本号字符串 + /// 是否有效 + public static bool IsValidSemVer(string? version) + { + if (string.IsNullOrWhiteSpace(version)) + return false; + + // SemVer 正则:主版本.次版本.补丁版本[-预发布标识][+构建元数据] + var pattern = @"^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"; + return System.Text.RegularExpressions.Regex.IsMatch(version, pattern); + } + + /// + /// 将版本号转换为 System.Version + /// + /// 版本号字符串 + /// System.Version 对象 + public static Version ToVersion(string? version) + { + var info = Parse(version); + return new Version(info.Major, info.Minor, info.Patch, info.Revision); + } + + /// + /// 从 System.Version 转换为 VersionInfo + /// + /// System.Version 对象 + /// VersionInfo 对象 + public static VersionInfo FromVersion(Version version) + { + return new VersionInfo + { + Major = version.Major, + Minor = version.Minor, + Patch = version.Build >= 0 ? version.Build : 0, + Revision = version.Revision >= 0 ? version.Revision : 0 + }; + } + } + + /// + /// 版本信息 + /// + public class VersionInfo + { + /// + /// 原始版本号字符串 + /// + public string? Original { get; set; } + + /// + /// 主版本号 + /// + public int Major { get; set; } + + /// + /// 次版本号 + /// + public int Minor { get; set; } + + /// + /// 补丁版本号 + /// + public int Patch { get; set; } + + /// + /// 修订版本号 + /// + public int Revision { get; set; } + + /// + /// 预发布标识(如 alpha, beta, rc.1) + /// + public string? PreRelease { get; set; } + + /// + /// 构建元数据 + /// + public string? BuildMetadata { get; set; } + + /// + /// 是否为预发布版本 + /// + public bool IsPreRelease => !string.IsNullOrEmpty(PreRelease); + + /// + /// 是否为稳定版本 + /// + public bool IsStable => string.IsNullOrEmpty(PreRelease); + + public override string ToString() + { + var result = $"{Major}.{Minor}.{Patch}"; + if (Revision > 0) + result += $".{Revision}"; + if (!string.IsNullOrEmpty(PreRelease)) + result += $"-{PreRelease}"; + if (!string.IsNullOrEmpty(BuildMetadata)) + result += $"+{BuildMetadata}"; + return result; + } + + public override bool Equals(object? obj) + { + if (obj is VersionInfo other) + { + return Major == other.Major && + Minor == other.Minor && + Patch == other.Patch && + Revision == other.Revision && + PreRelease == other.PreRelease; + } + return false; + } + + public override int GetHashCode() + { + return HashCode.Combine(Major, Minor, Patch, Revision, PreRelease); + } + } + + /// + /// 版本差异 + /// + public class VersionDiff + { + /// + /// 旧版本 + /// + public VersionInfo OldVersion { get; set; } = new(); + + /// + /// 新版本 + /// + public VersionInfo NewVersion { get; set; } = new(); + + /// + /// 主版本差异 + /// + public int MajorDiff { get; set; } + + /// + /// 次版本差异 + /// + public int MinorDiff { get; set; } + + /// + /// 补丁版本差异 + /// + public int PatchDiff { get; set; } + + /// + /// 修订版本差异 + /// + public int RevisionDiff { get; set; } + + /// + /// 变更级别 + /// + public VersionLevel ChangeLevel { get; set; } + + /// + /// 是否为升级 + /// + public bool IsUpgrade => + MajorDiff > 0 || + (MajorDiff == 0 && MinorDiff > 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff > 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff > 0); + + /// + /// 是否为降级 + /// + public bool IsDowngrade => + MajorDiff < 0 || + (MajorDiff == 0 && MinorDiff < 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff < 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff < 0); + + /// + /// 是否无变化 + /// + public bool IsUnchanged => MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff == 0; + } + + /// + /// 版本级别 + /// + public enum VersionLevel + { + /// + /// 主版本 + /// + Major, + + /// + /// 次版本 + /// + Minor, + + /// + /// 补丁版本 + /// + Patch, + + /// + /// 修订版本 + /// + Revision + } +} From 067fdded67ea856b71be3b6516b8c40fd855f136 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Tue, 24 Mar 2026 14:39:35 +0800 Subject: [PATCH 16/19] =?UTF-8?q?feat(net):=20=E6=B7=BB=E5=8A=A0DNS?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=B7=A5=E5=85=B7=E7=B1=BBDnsUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现域名正向解析功能,支持IPv4和IPv6地址获取 - 实现IP地址反向解析获取主机名功能 - 添加本机网络信息获取方法,包括主机名和IP地址 - 实现DNS记录查询功能,支持MX、TXT、CNAME记录 - 添加IP地址验证和类型判断方法 - 实现IP地址与长整型转换工具方法 - 提供异步解析方法提升性能 --- EasyTool.Core/NetCategory/DnsUtil.cs | 461 ++++++++++++++++++++++++ EasyTool.Core/Standardization/Option.cs | 3 + 2 files changed, 464 insertions(+) create mode 100644 EasyTool.Core/NetCategory/DnsUtil.cs diff --git a/EasyTool.Core/NetCategory/DnsUtil.cs b/EasyTool.Core/NetCategory/DnsUtil.cs new file mode 100644 index 0000000..0fc375c --- /dev/null +++ b/EasyTool.Core/NetCategory/DnsUtil.cs @@ -0,0 +1,461 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// DNS 解析工具类 + /// 提供域名解析、反向解析、DNS 查询等功能 + /// + public static class DnsUtil + { + #region 正向解析 + + /// + /// 解析域名获取 IP 地址 + /// + /// 主机名或域名 + /// IP 地址列表 + public static string[] GetIPAddresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 解析域名获取 IPv4 地址 + /// + /// 主机名或域名 + /// IPv4 地址列表 + public static string[] GetIPv4Addresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 解析域名获取 IPv6 地址 + /// + /// 主机名或域名 + /// IPv6 地址列表 + public static string[] GetIPv6Addresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 获取第一个 IP 地址 + /// + /// 主机名或域名 + /// IP 地址,解析失败返回 null + public static string? GetFirstIPAddress(string hostName) + { + var ips = GetIPAddresses(hostName); + return ips.Length > 0 ? ips[0] : null; + } + + /// + /// 异步解析域名获取 IP 地址 + /// + /// 主机名或域名 + /// IP 地址列表 + public static async Task GetIPAddressesAsync(string hostName) + { + try + { + var entry = await Dns.GetHostEntryAsync(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + #endregion + + #region 反向解析 + + /// + /// 反向解析 IP 地址获取主机名 + /// + /// IP 地址 + /// 主机名 + public static string? GetHostName(string ipAddress) + { + try + { + var entry = Dns.GetHostEntry(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + /// + /// 反向解析 IP 地址获取主机名 + /// + /// IP 地址对象 + /// 主机名 + public static string? GetHostName(IPAddress ipAddress) + { + try + { + var entry = Dns.GetHostEntry(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + /// + /// 异步反向解析 IP 地址获取主机名 + /// + /// IP 地址 + /// 主机名 + public static async Task GetHostNameAsync(string ipAddress) + { + try + { + var entry = await Dns.GetHostEntryAsync(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + #endregion + + #region 本机信息 + + /// + /// 获取本机主机名 + /// + /// 主机名 + public static string GetLocalHostName() + { + return Dns.GetHostName(); + } + + /// + /// 获取本机 IP 地址 + /// + /// IP 地址列表 + public static string[] GetLocalIPAddresses() + { + return GetIPAddresses(Dns.GetHostName()); + } + + /// + /// 获取本机 IPv4 地址 + /// + /// IPv4 地址列表 + public static string[] GetLocalIPv4Addresses() + { + return GetIPv4Addresses(Dns.GetHostName()); + } + + /// + /// 获取本机主要 IP 地址(优先返回内网地址) + /// + /// IP 地址 + public static string? GetLocalMainIPAddress() + { + var hostName = Dns.GetHostName(); + var entry = Dns.GetHostEntry(hostName); + + // 优先返回非回环、非链路本地地址 + var ip = entry.AddressList + .Where(a => a.AddressFamily == AddressFamily.InterNetwork) + .FirstOrDefault(a => !IPAddress.IsLoopback(a) && !IsLinkLocal(a)); + + return ip?.ToString(); + } + + private static bool IsLinkLocal(IPAddress ip) + { + if (ip.AddressFamily != AddressFamily.InterNetwork) + return false; + + var bytes = ip.GetAddressBytes(); + return bytes[0] == 169 && bytes[1] == 254; // 169.254.x.x + } + + #endregion + + #region DNS 记录查询 + + /// + /// 获取 MX 记录(邮件交换记录) + /// 注意:需要使用外部库或自定义实现,这里返回空 + /// + /// 域名 + /// MX 记录列表 + public static List GetMxRecords(string domain) + { + // 简化实现,实际需要使用 DnsClient 等库 + return new List(); + } + + /// + /// 获取 TXT 记录 + /// + /// 域名 + /// TXT 记录列表 + public static List GetTxtRecords(string domain) + { + // 简化实现 + return new List(); + } + + /// + /// 获取 CNAME 记录 + /// + /// 域名 + /// CNAME 记录 + public static string? GetCnameRecord(string domain) + { + // 简化实现 + return null; + } + + #endregion + + #region 验证方法 + + /// + /// 验证域名是否可以解析 + /// + /// 主机名或域名 + /// 是否可以解析 + public static bool CanResolve(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList.Length > 0; + } + catch + { + return false; + } + } + + /// + /// 异步验证域名是否可以解析 + /// + /// 主机名或域名 + /// 是否可以解析 + public static async Task CanResolveAsync(string hostName) + { + try + { + var entry = await Dns.GetHostEntryAsync(hostName); + return entry.AddressList.Length > 0; + } + catch + { + return false; + } + } + + /// + /// 验证 IP 地址格式是否有效 + /// + /// IP 地址字符串 + /// 是否有效 + public static bool IsValidIPAddress(string ipString) + { + return IPAddress.TryParse(ipString, out _); + } + + /// + /// 验证是否为 IPv4 地址 + /// + /// IP 地址字符串 + /// 是否为 IPv4 + public static bool IsIPv4(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return ip.AddressFamily == AddressFamily.InterNetwork; + } + return false; + } + + /// + /// 验证是否为 IPv6 地址 + /// + /// IP 地址字符串 + /// 是否为 IPv6 + public static bool IsIPv6(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return ip.AddressFamily == AddressFamily.InterNetworkV6; + } + return false; + } + + /// + /// 验证是否为内网 IP 地址 + /// + /// IP 地址字符串 + /// 是否为内网 IP + public static bool IsPrivateIP(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return false; + + if (ip.AddressFamily != AddressFamily.InterNetwork) + return false; + + var bytes = ip.GetAddressBytes(); + + // 10.0.0.0 - 10.255.255.255 + if (bytes[0] == 10) + return true; + + // 172.16.0.0 - 172.31.255.255 + if (bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31) + return true; + + // 192.168.0.0 - 192.168.255.255 + if (bytes[0] == 192 && bytes[1] == 168) + return true; + + return false; + } + + /// + /// 验证是否为回环地址 + /// + /// IP 地址字符串 + /// 是否为回环地址 + public static bool IsLoopback(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return IPAddress.IsLoopback(ip); + } + return false; + } + + #endregion + + #region 工具方法 + + /// + /// 将 IP 地址转换为长整型 + /// + /// IP 地址字符串 + /// 长整型值 + public static long IPToLong(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return 0; + + var bytes = ip.GetAddressBytes(); + if (bytes.Length != 4) + return 0; + + return ((long)bytes[0] << 24) | ((long)bytes[1] << 16) | ((long)bytes[2] << 8) | bytes[3]; + } + + /// + /// 将长整型转换为 IP 地址 + /// + /// 长整型值 + /// IP 地址字符串 + public static string LongToIP(long ipLong) + { + return $"{(ipLong >> 24) & 0xFF}.{(ipLong >> 16) & 0xFF}.{(ipLong >> 8) & 0xFF}.{ipLong & 0xFF}"; + } + + /// + /// 获取 IP 地址类型描述 + /// + /// IP 地址字符串 + /// 类型描述 + public static string GetIPType(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return "无效IP"; + + if (IPAddress.IsLoopback(ip)) + return "回环地址"; + + if (IsPrivateIP(ipString)) + return "内网地址"; + + return "公网地址"; + } + + #endregion + } + + /// + /// MX 记录 + /// + public class MxRecord + { + /// + /// 优先级 + /// + public int Priority { get; set; } + + /// + /// 邮件服务器地址 + /// + public string? Exchange { get; set; } + + public override string ToString() + { + return $"[{Priority}] {Exchange}"; + } + } +} diff --git a/EasyTool.Core/Standardization/Option.cs b/EasyTool.Core/Standardization/Option.cs index f15b2cc..edba09b 100644 --- a/EasyTool.Core/Standardization/Option.cs +++ b/EasyTool.Core/Standardization/Option.cs @@ -14,6 +14,9 @@ namespace EasyTool.Standardization *标准化与前端下拉选项数据结构,减少前后端对接工作 */ + + + /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// From 8db233601ecd0af8009ec2e129be11a8b6cc1cef Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 27 Mar 2026 11:34:28 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E5=85=A8=E9=83=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EasyTool.Core/AICategory/OpenAIClient.cs | 527 +++++++ EasyTool.Core/AICategory/PromptBuilder.cs | 315 ++++ EasyTool.Core/AICategory/VectorSimilarity.cs | 266 ++++ EasyTool.Core/CacheCategory/CacheOptions.cs | 110 ++ .../CacheCategory/DistributedCacheUtil.cs | 500 +++++++ EasyTool.Core/CacheCategory/ICacheProvider.cs | 152 ++ .../CacheCategory/MemoryCacheProvider.cs | 399 +++++ .../CacheCategory/RedisCacheProvider.cs | 283 ++++ EasyTool.Core/CodeCategory/AeadUtil.cs | 295 ++++ EasyTool.Core/CodeCategory/HmacUtil.cs | 255 ++++ EasyTool.Core/CodeCategory/KdfUtil.cs | 322 ++++ EasyTool.Core/CodeCategory/SignatureUtil.cs | 614 ++++++++ EasyTool.Core/CodeCategory/TSIDUtil.cs | 422 ------ EasyTool.Core/CodeCategory/UlidUtil.cs | 362 ----- .../CollectionsCategory/BatchUtil.cs | 147 ++ .../CollectionsCategory/CacheUtil.cs | 939 +++--------- .../CollectionsCategory/GraphUtil.cs | 981 +++--------- .../CollectionsCategory/GroupUtil.cs | 346 +++++ .../ImmutableListExtension.cs | 421 ++++++ .../ObservableCollection.cs | 160 ++ .../CollectionsCategory/PagedList.cs | 231 +++ .../CollectionsCategory/PriorityQueueUtil.cs | 324 ---- .../CollectionsCategory/QueueUtil.cs | 389 ++++- .../CollectionsCategory/RingBuffer.cs | 319 ++++ .../CollectionsCategory/StatisticsUtil.cs | 723 --------- EasyTool.Core/CollectionsCategory/TopKUtil.cs | 2 +- EasyTool.Core/CollectionsCategory/TreeUtil.cs | 1328 ++++------------- .../CollectionsCategory/WeightedSelector.cs | 279 ++++ EasyTool.Core/ConvertCategory/ConvertUtil.cs | 410 +++++ .../ConvertCategory/CsvConvertUtil.cs | 348 +++++ .../ConvertCategory/MsgPackConvertUtil.cs | 789 ++++++++++ .../ConvertCategory/TomlConvertUtil.cs | 715 +++++++++ .../ConvertCategory/XmlConvertUtil.cs | 281 ++++ .../ConvertCategory/YamlConvertUtil.cs | 523 +++++++ .../DatabaseCategory/ConnectionPool.cs | 444 ++++++ EasyTool.Core/DatabaseCategory/DbUtil.cs | 547 +++++++ EasyTool.Core/DatabaseCategory/SqlBuilder.cs | 735 +++++++++ EasyTool.Core/DateTimeCategory/AgeUtil.cs | 288 ++++ EasyTool.Core/DateTimeCategory/CronUtil.cs | 533 ++++--- EasyTool.Core/DateTimeCategory/HolidayUtil.cs | 542 ++----- .../DateTimeCategory/LunarCalendarUtil.cs | 654 ++++---- .../DateTimeCategory/SolarTermUtil.cs | 348 +++++ .../DateTimeCategory/StopwatchUtil.cs | 275 ++++ .../DateTimeCategory/TimeZoneUtil.cs | 453 ++++++ EasyTool.Core/DateTimeCategory/TimerUtil.cs | 446 +++++- EasyTool.Core/DateTimeCategory/WorkdayUtil.cs | 446 ++++++ EasyTool.Core/EasyTool.Core.csproj | 15 + EasyTool.Core/IOCategory/ArchiveUtil.cs | 449 ++++++ EasyTool.Core/IOCategory/CompressionUtil.cs | 271 ++++ EasyTool.Core/IOCategory/ConfigUtil.cs | 252 ++++ .../IOCategory/CsvStreamingReader.cs | 483 ++++++ EasyTool.Core/IOCategory/CsvUtil.cs | 698 ++++++--- EasyTool.Core/IOCategory/ExcelUtil.cs | 400 +++++ EasyTool.Core/IOCategory/FileChunkUtil.cs | 395 +++++ EasyTool.Core/IOCategory/FileCompareUtil.cs | 258 ++++ EasyTool.Core/IOCategory/FileLockUtil.cs | 331 ++++ EasyTool.Core/IOCategory/FileSearch.cs | 286 ++++ EasyTool.Core/IOCategory/FileWatcher.cs | 386 +++++ EasyTool.Core/IOCategory/FileWatcherEx.cs | 298 ++++ EasyTool.Core/IOCategory/ImageMetadataUtil.cs | 346 +++++ EasyTool.Core/IOCategory/JsonSerializer.cs | 314 ++++ EasyTool.Core/IOCategory/JsonUtil.cs | 159 +- EasyTool.Core/IOCategory/PathUtil.cs | 421 +++--- EasyTool.Core/IOCategory/QrCodeUtil.cs | 463 ++++++ EasyTool.Core/IOCategory/ResourceUtil.cs | 415 ++++++ EasyTool.Core/IOCategory/SerializeUtil.cs | 202 +++ EasyTool.Core/IOCategory/TempFileUtil.cs | 267 ++++ .../IdentifierCategory/ObjectIdUtil.cs | 539 +++++++ .../IdentifierCategory/ShortIdUtil.cs | 404 +++++ .../IdentifierCategory/SnowflakeIdUtil.cs | 237 +++ EasyTool.Core/IdentifierCategory/TSIDUtil.cs | 503 +++++++ EasyTool.Core/IdentifierCategory/ULIDUtil.cs | 460 ++++++ EasyTool.Core/MathCategory/AngleUtil.cs | 312 ++++ .../MathCategory/CombinatoricsUtil.cs | 576 +++++++ EasyTool.Core/MathCategory/ComplexUtil.cs | 330 ++++ EasyTool.Core/MathCategory/GeoUtil.cs | 283 ++++ EasyTool.Core/MathCategory/MathUtil.cs | 465 +++--- EasyTool.Core/MathCategory/MatrixUtil.cs | 560 +++++++ .../MathCategory/NumberFormatUtil.cs | 505 +++++++ EasyTool.Core/MathCategory/PrimeUtil.cs | 483 ++++++ EasyTool.Core/MathCategory/RandomUtil.cs | 478 ++++-- EasyTool.Core/MathCategory/StatisticsUtil.cs | 633 ++++++++ EasyTool.Core/MediaCategory/AudioUtil.cs | 229 +++ EasyTool.Core/MediaCategory/VideoUtil.cs | 363 +++++ EasyTool.Core/NetCategory/DnsServerUtil.cs | 511 +++++++ EasyTool.Core/NetCategory/GrpcUtil.cs | 424 ++++++ .../NetCategory/HttpClientBuilder.cs | 681 +++++++++ EasyTool.Core/NetCategory/HttpClientPool.cs | 446 ++++++ EasyTool.Core/NetCategory/HttpUtil.cs | 737 +++------ .../NetCategory/NetworkInterfaceUtil.cs | 376 +++++ EasyTool.Core/NetCategory/PingUtil.cs | 523 +++++++ EasyTool.Core/NetCategory/PortScannerUtil.cs | 256 ++++ EasyTool.Core/NetCategory/ProxyUtil.cs | 619 ++++++++ EasyTool.Core/NetCategory/SmtpUtil.cs | 389 +++++ EasyTool.Core/NetCategory/SseUtil.cs | 521 +++++++ EasyTool.Core/NetCategory/WebSocketUtil.cs | 411 +++++ EasyTool.Core/NetCategory/WebhookUtil.cs | 511 +++++++ EasyTool.Core/Options.cs | 276 ++++ EasyTool.Core/QueueCategory/ChannelUtil.cs | 358 +++++ EasyTool.Core/QueueCategory/DelayQueue.cs | 315 ++++ .../QueueCategory/MessageQueueUtil.cs | 543 +++++++ .../PriorityQueue.netstandard.cs | 211 +++ .../QueueCategory/PriorityQueueUtil.cs | 329 ++++ EasyTool.Core/ReflectCategory/EnumUtil.cs | 212 +++ .../ReflectCategory/ExpressionUtil.cs | 324 ++++ EasyTool.Core/ReflectCategory/TypeUtil.cs | 404 +++++ .../SecurityCategory/CertificateUtil.cs | 535 +++++++ EasyTool.Core/SecurityCategory/CsrfUtil.cs | 342 +++++ .../SecurityCategory/InputSanitizer.cs | 403 +++++ EasyTool.Core/SecurityCategory/JwtBuilder.cs | 616 ++++++++ .../SecurityCategory/KeyGeneratorUtil.cs | 419 ++++++ .../SecurityCategory/PasswordStrengthUtil.cs | 343 +++++ .../SecurityCategory/SecureRandomUtil.cs | 533 +++++++ .../SecurityCategory/SqlInjectionUtil.cs | 312 ++++ EasyTool.Core/SecurityCategory/TlsUtil.cs | 423 ++++++ EasyTool.Core/SecurityCategory/XssUtil.cs | 344 +++++ EasyTool.Core/ServiceCollectionExtensions.cs | 184 +++ EasyTool.Core/SystemCategory/AudioUtil.cs | 244 +++ EasyTool.Core/SystemCategory/BatteryUtil.cs | 359 +++++ EasyTool.Core/SystemCategory/ClipboardUtil.cs | 587 ++++++++ .../SystemCategory/HardwareInfoUtil.cs | 450 ++++++ EasyTool.Core/SystemCategory/HotKeyUtil.cs | 283 ++++ EasyTool.Core/SystemCategory/KeyboardUtil.cs | 325 ++++ EasyTool.Core/SystemCategory/MouseUtil.cs | 314 ++++ .../SystemCategory/PerformanceUtil.cs | 339 +++++ EasyTool.Core/SystemCategory/PowerUtil.cs | 364 +++++ EasyTool.Core/SystemCategory/RegistryUtil.cs | 265 ++++ EasyTool.Core/SystemCategory/ScreenUtil.cs | 243 +++ EasyTool.Core/SystemCategory/ServiceUtil.cs | 394 +++++ .../SystemCategory/SystemMonitorUtil.cs | 991 ++++++++++++ EasyTool.Core/TextCategory/ChineseUtil.cs | 336 +++++ .../TextCategory/EncodingDetector.cs | 268 ++++ .../TextCategory/HtmlSanitizerUtil.cs | 358 +++++ .../TextCategory/KeywordExtractor.cs | 300 ++++ EasyTool.Core/TextCategory/MarkdownUtil.cs | 485 ++++++ EasyTool.Core/TextCategory/SegmenterUtil.cs | 433 ++++++ EasyTool.Core/TextCategory/SlugUtil.cs | 320 ++-- .../TextCategory/SpellCheckerUtil.cs | 340 +++++ .../TextCategory/StringBuilderPool.cs | 172 +++ EasyTool.Core/TextCategory/TemplateUtil.cs | 421 +++--- .../TextCategory/TextSimilarityUtil.cs | 413 +++++ EasyTool.Core/ToolCategory/AsyncLockUtil.cs | 626 ++++++++ EasyTool.Core/ToolCategory/BackoffUtil.cs | 211 +++ EasyTool.Core/ToolCategory/BenchmarkUtil.cs | 632 +++----- .../ToolCategory/CircuitBreakerUtil.cs | 301 ++++ .../ToolCategory/CommandLineParser.cs | 297 ++++ EasyTool.Core/ToolCategory/EnumUtil.cs | 364 ----- EasyTool.Core/ToolCategory/EventArgsUtil.cs | 158 ++ EasyTool.Core/ToolCategory/EventBus.cs | 252 ++++ EasyTool.Core/ToolCategory/GuardUtil.cs | 217 +++ EasyTool.Core/ToolCategory/LogUtil.cs | 194 +++ EasyTool.Core/ToolCategory/ObjectPool.cs | 143 ++ EasyTool.Core/ToolCategory/ObjectUtil.cs | 394 +++++ EasyTool.Core/ToolCategory/PipelineUtil.cs | 271 ++++ .../ToolCategory/ProducerConsumer.cs | 358 +++++ EasyTool.Core/ToolCategory/RateLimiter.cs | 340 +++++ EasyTool.Core/ToolCategory/ResultUtil.cs | 295 ++++ EasyTool.Core/ToolCategory/RetryUtil.cs | 449 +++--- EasyTool.Core/ToolCategory/SecurityUtil.cs | 473 ++++++ EasyTool.Core/ToolCategory/Singleton.cs | 105 ++ EasyTool.Core/ToolCategory/StateMachine.cs | 228 +++ .../ToolCategory/TaskSchedulerUtil.cs | 396 +++++ EasyTool.Core/ToolCategory/ValidatorUtil.cs | 643 +++----- .../ValidationCategory/CompositeValidator.cs | 406 +++++ .../ValidationCategory/FluentValidator.cs | 437 ++++++ .../ValidationCategory/ModelValidator.cs | 352 +++++ .../ValidationRuleBuilder.cs | 377 +++++ .../EasyTool.EmitMapperTests.csproj | 25 - .../EmitMapperExtensionTests.cs | 38 - .../EasyTool.ImageTests.csproj | 25 - .../ImageCategory/ImageUtilTests.cs | 58 - .../ImageCategory/Resources/mask.jpg | Bin 18107 -> 0 bytes .../ImageCategory/Resources/ori.jpg | Bin 231099 -> 0 bytes .../ImageCategory/Resources/result.jpg | Bin 278488 -> 0 bytes EasyTool.NPOI/OfficeCategory/NPOIUtil.cs | 49 +- EasyTool.NPOITests/EasyTool.NPOITests.csproj | 25 - .../OfficeCategory/NPOIUtilTests.cs | 118 -- .../DevelopmentCategory/BuildDtoToTSTests.cs | 29 - .../BuildOptionToTSTests.cs | 37 - .../BuildWebApiToTSTests.cs | 42 - EasyTool.WebTests/EasyTool.WebTests.csproj | 25 - EasyTool.sln | 41 +- 182 files changed, 57708 insertions(+), 9215 deletions(-) create mode 100644 EasyTool.Core/AICategory/OpenAIClient.cs create mode 100644 EasyTool.Core/AICategory/PromptBuilder.cs create mode 100644 EasyTool.Core/AICategory/VectorSimilarity.cs create mode 100644 EasyTool.Core/CacheCategory/CacheOptions.cs create mode 100644 EasyTool.Core/CacheCategory/DistributedCacheUtil.cs create mode 100644 EasyTool.Core/CacheCategory/ICacheProvider.cs create mode 100644 EasyTool.Core/CacheCategory/MemoryCacheProvider.cs create mode 100644 EasyTool.Core/CacheCategory/RedisCacheProvider.cs create mode 100644 EasyTool.Core/CodeCategory/AeadUtil.cs create mode 100644 EasyTool.Core/CodeCategory/HmacUtil.cs create mode 100644 EasyTool.Core/CodeCategory/KdfUtil.cs create mode 100644 EasyTool.Core/CodeCategory/SignatureUtil.cs delete mode 100644 EasyTool.Core/CodeCategory/TSIDUtil.cs delete mode 100644 EasyTool.Core/CodeCategory/UlidUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/BatchUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/GroupUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs create mode 100644 EasyTool.Core/CollectionsCategory/ObservableCollection.cs create mode 100644 EasyTool.Core/CollectionsCategory/PagedList.cs delete mode 100644 EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/RingBuffer.cs delete mode 100644 EasyTool.Core/CollectionsCategory/StatisticsUtil.cs create mode 100644 EasyTool.Core/CollectionsCategory/WeightedSelector.cs create mode 100644 EasyTool.Core/ConvertCategory/ConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/CsvConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/TomlConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/XmlConvertUtil.cs create mode 100644 EasyTool.Core/ConvertCategory/YamlConvertUtil.cs create mode 100644 EasyTool.Core/DatabaseCategory/ConnectionPool.cs create mode 100644 EasyTool.Core/DatabaseCategory/DbUtil.cs create mode 100644 EasyTool.Core/DatabaseCategory/SqlBuilder.cs create mode 100644 EasyTool.Core/DateTimeCategory/AgeUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/SolarTermUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/StopwatchUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs create mode 100644 EasyTool.Core/DateTimeCategory/WorkdayUtil.cs create mode 100644 EasyTool.Core/IOCategory/ArchiveUtil.cs create mode 100644 EasyTool.Core/IOCategory/CompressionUtil.cs create mode 100644 EasyTool.Core/IOCategory/ConfigUtil.cs create mode 100644 EasyTool.Core/IOCategory/CsvStreamingReader.cs create mode 100644 EasyTool.Core/IOCategory/ExcelUtil.cs create mode 100644 EasyTool.Core/IOCategory/FileChunkUtil.cs create mode 100644 EasyTool.Core/IOCategory/FileCompareUtil.cs create mode 100644 EasyTool.Core/IOCategory/FileLockUtil.cs create mode 100644 EasyTool.Core/IOCategory/FileSearch.cs create mode 100644 EasyTool.Core/IOCategory/FileWatcher.cs create mode 100644 EasyTool.Core/IOCategory/FileWatcherEx.cs create mode 100644 EasyTool.Core/IOCategory/ImageMetadataUtil.cs create mode 100644 EasyTool.Core/IOCategory/JsonSerializer.cs create mode 100644 EasyTool.Core/IOCategory/QrCodeUtil.cs create mode 100644 EasyTool.Core/IOCategory/ResourceUtil.cs create mode 100644 EasyTool.Core/IOCategory/SerializeUtil.cs create mode 100644 EasyTool.Core/IOCategory/TempFileUtil.cs create mode 100644 EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs create mode 100644 EasyTool.Core/IdentifierCategory/ShortIdUtil.cs create mode 100644 EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs create mode 100644 EasyTool.Core/IdentifierCategory/TSIDUtil.cs create mode 100644 EasyTool.Core/IdentifierCategory/ULIDUtil.cs create mode 100644 EasyTool.Core/MathCategory/AngleUtil.cs create mode 100644 EasyTool.Core/MathCategory/CombinatoricsUtil.cs create mode 100644 EasyTool.Core/MathCategory/ComplexUtil.cs create mode 100644 EasyTool.Core/MathCategory/GeoUtil.cs create mode 100644 EasyTool.Core/MathCategory/MatrixUtil.cs create mode 100644 EasyTool.Core/MathCategory/NumberFormatUtil.cs create mode 100644 EasyTool.Core/MathCategory/PrimeUtil.cs create mode 100644 EasyTool.Core/MathCategory/StatisticsUtil.cs create mode 100644 EasyTool.Core/MediaCategory/AudioUtil.cs create mode 100644 EasyTool.Core/MediaCategory/VideoUtil.cs create mode 100644 EasyTool.Core/NetCategory/DnsServerUtil.cs create mode 100644 EasyTool.Core/NetCategory/GrpcUtil.cs create mode 100644 EasyTool.Core/NetCategory/HttpClientBuilder.cs create mode 100644 EasyTool.Core/NetCategory/HttpClientPool.cs create mode 100644 EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs create mode 100644 EasyTool.Core/NetCategory/PingUtil.cs create mode 100644 EasyTool.Core/NetCategory/PortScannerUtil.cs create mode 100644 EasyTool.Core/NetCategory/ProxyUtil.cs create mode 100644 EasyTool.Core/NetCategory/SmtpUtil.cs create mode 100644 EasyTool.Core/NetCategory/SseUtil.cs create mode 100644 EasyTool.Core/NetCategory/WebSocketUtil.cs create mode 100644 EasyTool.Core/NetCategory/WebhookUtil.cs create mode 100644 EasyTool.Core/Options.cs create mode 100644 EasyTool.Core/QueueCategory/ChannelUtil.cs create mode 100644 EasyTool.Core/QueueCategory/DelayQueue.cs create mode 100644 EasyTool.Core/QueueCategory/MessageQueueUtil.cs create mode 100644 EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs create mode 100644 EasyTool.Core/QueueCategory/PriorityQueueUtil.cs create mode 100644 EasyTool.Core/ReflectCategory/EnumUtil.cs create mode 100644 EasyTool.Core/ReflectCategory/ExpressionUtil.cs create mode 100644 EasyTool.Core/ReflectCategory/TypeUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/CertificateUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/CsrfUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/InputSanitizer.cs create mode 100644 EasyTool.Core/SecurityCategory/JwtBuilder.cs create mode 100644 EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/SecureRandomUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/TlsUtil.cs create mode 100644 EasyTool.Core/SecurityCategory/XssUtil.cs create mode 100644 EasyTool.Core/ServiceCollectionExtensions.cs create mode 100644 EasyTool.Core/SystemCategory/AudioUtil.cs create mode 100644 EasyTool.Core/SystemCategory/BatteryUtil.cs create mode 100644 EasyTool.Core/SystemCategory/ClipboardUtil.cs create mode 100644 EasyTool.Core/SystemCategory/HardwareInfoUtil.cs create mode 100644 EasyTool.Core/SystemCategory/HotKeyUtil.cs create mode 100644 EasyTool.Core/SystemCategory/KeyboardUtil.cs create mode 100644 EasyTool.Core/SystemCategory/MouseUtil.cs create mode 100644 EasyTool.Core/SystemCategory/PerformanceUtil.cs create mode 100644 EasyTool.Core/SystemCategory/PowerUtil.cs create mode 100644 EasyTool.Core/SystemCategory/RegistryUtil.cs create mode 100644 EasyTool.Core/SystemCategory/ScreenUtil.cs create mode 100644 EasyTool.Core/SystemCategory/ServiceUtil.cs create mode 100644 EasyTool.Core/SystemCategory/SystemMonitorUtil.cs create mode 100644 EasyTool.Core/TextCategory/ChineseUtil.cs create mode 100644 EasyTool.Core/TextCategory/EncodingDetector.cs create mode 100644 EasyTool.Core/TextCategory/HtmlSanitizerUtil.cs create mode 100644 EasyTool.Core/TextCategory/KeywordExtractor.cs create mode 100644 EasyTool.Core/TextCategory/MarkdownUtil.cs create mode 100644 EasyTool.Core/TextCategory/SegmenterUtil.cs create mode 100644 EasyTool.Core/TextCategory/SpellCheckerUtil.cs create mode 100644 EasyTool.Core/TextCategory/StringBuilderPool.cs create mode 100644 EasyTool.Core/TextCategory/TextSimilarityUtil.cs create mode 100644 EasyTool.Core/ToolCategory/AsyncLockUtil.cs create mode 100644 EasyTool.Core/ToolCategory/BackoffUtil.cs create mode 100644 EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs create mode 100644 EasyTool.Core/ToolCategory/CommandLineParser.cs delete mode 100644 EasyTool.Core/ToolCategory/EnumUtil.cs create mode 100644 EasyTool.Core/ToolCategory/EventArgsUtil.cs create mode 100644 EasyTool.Core/ToolCategory/EventBus.cs create mode 100644 EasyTool.Core/ToolCategory/GuardUtil.cs create mode 100644 EasyTool.Core/ToolCategory/LogUtil.cs create mode 100644 EasyTool.Core/ToolCategory/ObjectPool.cs create mode 100644 EasyTool.Core/ToolCategory/ObjectUtil.cs create mode 100644 EasyTool.Core/ToolCategory/PipelineUtil.cs create mode 100644 EasyTool.Core/ToolCategory/ProducerConsumer.cs create mode 100644 EasyTool.Core/ToolCategory/RateLimiter.cs create mode 100644 EasyTool.Core/ToolCategory/ResultUtil.cs create mode 100644 EasyTool.Core/ToolCategory/SecurityUtil.cs create mode 100644 EasyTool.Core/ToolCategory/Singleton.cs create mode 100644 EasyTool.Core/ToolCategory/StateMachine.cs create mode 100644 EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs create mode 100644 EasyTool.Core/ValidationCategory/CompositeValidator.cs create mode 100644 EasyTool.Core/ValidationCategory/FluentValidator.cs create mode 100644 EasyTool.Core/ValidationCategory/ModelValidator.cs create mode 100644 EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs delete mode 100644 EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj delete mode 100644 EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs delete mode 100644 EasyTool.ImageTests/EasyTool.ImageTests.csproj delete mode 100644 EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs delete mode 100644 EasyTool.ImageTests/ImageCategory/Resources/mask.jpg delete mode 100644 EasyTool.ImageTests/ImageCategory/Resources/ori.jpg delete mode 100644 EasyTool.ImageTests/ImageCategory/Resources/result.jpg delete mode 100644 EasyTool.NPOITests/EasyTool.NPOITests.csproj delete mode 100644 EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs delete mode 100644 EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs delete mode 100644 EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs delete mode 100644 EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs delete mode 100644 EasyTool.WebTests/EasyTool.WebTests.csproj diff --git a/EasyTool.Core/AICategory/OpenAIClient.cs b/EasyTool.Core/AICategory/OpenAIClient.cs new file mode 100644 index 0000000..453a339 --- /dev/null +++ b/EasyTool.Core/AICategory/OpenAIClient.cs @@ -0,0 +1,527 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.AICategory +{ + /// + /// OpenAI API 工具类 + /// 提供 GPT、DALL-E、Whisper 等 AI 服务的集成 + /// + public class OpenAIClient + { + private readonly string _apiKey; + private readonly string _baseUrl; + private readonly HttpClient _httpClient; + + /// + /// 创建 OpenAI 客户端 + /// + /// API Key + /// API 基础 URL(默认 OpenAI 官方) + public OpenAIClient(string apiKey, string? baseUrl = null) + { + _apiKey = apiKey; + _baseUrl = baseUrl ?? "https://api.openai.com/v1"; + _httpClient = new HttpClient + { + Timeout = TimeSpan.FromMinutes(5) + }; + _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + } + + #region Chat Completions + + /// + /// 发送聊天请求 + /// + /// 消息列表 + /// 模型名称 + /// 温度(0-2) + /// 最大令牌数 + /// 取消令牌 + /// 响应结果 + public async Task ChatAsync(List messages, string model = "gpt-3.5-turbo", double temperature = 0.7, int? maxTokens = null, CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["messages"] = messages, + ["temperature"] = temperature + }; + + if (maxTokens.HasValue) + requestBody["max_tokens"] = maxTokens.Value; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/chat/completions", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + return JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }) ?? throw new OpenAIException("无法解析响应"); + } + + /// + /// 发送简单聊天请求 + /// + /// 提示词 + /// 模型名称 + /// 温度 + /// 取消令牌 + /// 响应文本 + public async Task ChatSimpleAsync(string prompt, string model = "gpt-3.5-turbo", double temperature = 0.7, CancellationToken cancellationToken = default) + { + var messages = new List + { + new() { Role = "user", Content = prompt } + }; + + var response = await ChatAsync(messages, model, temperature, cancellationToken: cancellationToken); + return response.Choices[0].Message.Content; + } + + /// + /// 流式聊天请求 + /// + public async IAsyncEnumerable ChatStreamAsync(List messages, string model = "gpt-3.5-turbo", double temperature = 0.7, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["messages"] = messages, + ["temperature"] = temperature, + ["stream"] = true + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var request = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/chat/completions") + { + Content = content + }; + + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + response.EnsureSuccessStatusCode(); + + using var stream = await ReadContentAsStreamAsync(response.Content); + using var reader = new StreamReader(stream); + + while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested) + { + var line = await reader.ReadLineAsync(); + if (string.IsNullOrEmpty(line) || !line.StartsWith("data: ")) + continue; + + var data = line.Substring(6); + if (data == "[DONE]") + break; + + var chunkResponse = JsonSerializer.Deserialize(data, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + if (chunkResponse?.Choices?[0]?.Delta?.Content != null) + { + yield return chunkResponse.Choices[0].Delta.Content; + } + } + } + + #endregion + + #region Embeddings + + /// + /// 获取文本嵌入向量 + /// + /// 文本 + /// 模型名称 + /// 取消令牌 + /// 嵌入向量 + public async Task GetEmbeddingAsync(string text, string model = "text-embedding-ada-002", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = text + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/embeddings", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var embeddingResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + return embeddingResponse?.Data?[0]?.Embedding ?? Array.Empty(); + } + + /// + /// 批量获取嵌入向量 + /// + public async Task> GetEmbeddingsAsync(List texts, string model = "text-embedding-ada-002", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = texts + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/embeddings", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var embeddingResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + var result = new List(); + if (embeddingResponse?.Data != null) + { + foreach (var item in embeddingResponse.Data) + { + result.Add(item.Embedding ?? Array.Empty()); + } + } + + return result; + } + + #endregion + + #region Image Generation + + /// + /// 生成图像 + /// + /// 提示词 + /// 尺寸(256x256, 512x512, 1024x1024) + /// 生成数量 + /// 取消令牌 + /// 图像 URL 列表 + public async Task> GenerateImageAsync(string prompt, string size = "1024x1024", int n = 1, CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["prompt"] = prompt, + ["size"] = size, + ["n"] = n + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/images/generations", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var imageResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + var result = new List(); + if (imageResponse?.Data != null) + { + foreach (var item in imageResponse.Data) + { + if (!string.IsNullOrEmpty(item.Url)) + result.Add(item.Url); + } + } + + return result; + } + + #endregion + + #region Audio + + /// + /// 语音转文字 + /// + /// 音频文件路径 + /// 模型名称 + /// 语言(如 "zh", "en") + /// 取消令牌 + /// 转录文本 + public async Task TranscribeAsync(string audioFilePath, string model = "whisper-1", string? language = null, CancellationToken cancellationToken = default) + { + using var formContent = new MultipartFormDataContent(); + formContent.Add(new StreamContent(File.OpenRead(audioFilePath)), "file", Path.GetFileName(audioFilePath)); + formContent.Add(new StringContent(model), "model"); + + if (!string.IsNullOrEmpty(language)) + formContent.Add(new StringContent(language), "language"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/audio/transcriptions", formContent, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var transcriptionResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + return transcriptionResponse?.Text ?? string.Empty; + } + + /// + /// 文字转语音 + /// + /// 文本 + /// 输出文件路径 + /// 模型名称 + /// 声音(alloy, echo, fable, onyx, nova, shimmer) + /// 取消令牌 + /// 是否成功 + public async Task TextToSpeechAsync(string text, string outputFilePath, string model = "tts-1", string voice = "alloy", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = text, + ["voice"] = voice + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/audio/speech", content, cancellationToken); + + if (!response.IsSuccessStatusCode) + { + var errorJson = await ReadContentAsStringAsync(response.Content); + throw new OpenAIException($"API 请求失败: {response.StatusCode}", errorJson); + } + + var audioData = await ReadContentAsByteArrayAsync(response.Content); + await File.WriteAllBytesAsync(outputFilePath, audioData, cancellationToken); + + return true; + } + + #endregion + + #region Helper Methods + + private static async Task ReadContentAsStringAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsStringAsync(); +#else + return await content.ReadAsStringAsync(default); +#endif + } + + private static async Task ReadContentAsStreamAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsStreamAsync(); +#else + return await content.ReadAsStreamAsync(default); +#endif + } + + private static async Task ReadContentAsByteArrayAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsByteArrayAsync(); +#else + return await content.ReadAsByteArrayAsync(default); +#endif + } + + #endregion + } + + #region 数据模型 + + /// + /// 聊天消息 + /// + public class ChatMessage + { + /// + /// 角色(system, user, assistant) + /// + public string Role { get; set; } = string.Empty; + + /// + /// 内容 + /// + public string Content { get; set; } = string.Empty; + } + + /// + /// 聊天响应 + /// + public class ChatResponse + { + /// + /// 响应 ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 选择列表 + /// + public List Choices { get; set; } = new(); + + /// + /// 使用情况 + /// + public UsageInfo? Usage { get; set; } + } + + /// + /// 聊天选择 + /// + public class ChatChoice + { + /// + /// 索引 + /// + public int Index { get; set; } + + /// + /// 消息 + /// + public ChatMessage Message { get; set; } = new(); + + /// + /// 结束原因 + /// + public string? FinishReason { get; set; } + } + + /// + /// 流式响应 + /// + public class ChatStreamResponse + { + public List? Choices { get; set; } + } + + /// + /// 流式选择 + /// + public class ChatStreamChoice + { + public ChatStreamDelta? Delta { get; set; } + } + + /// + /// 流式增量 + /// + public class ChatStreamDelta + { + public string? Content { get; set; } + } + + /// + /// 使用情况 + /// + public class UsageInfo + { + public int PromptTokens { get; set; } + public int CompletionTokens { get; set; } + public int TotalTokens { get; set; } + } + + /// + /// 嵌入响应 + /// + public class EmbeddingResponse + { + public List? Data { get; set; } + } + + /// + /// 嵌入数据 + /// + public class EmbeddingData + { + public float[]? Embedding { get; set; } + } + + /// + /// 图像响应 + /// + public class ImageResponse + { + public List? Data { get; set; } + } + + /// + /// 图像数据 + /// + public class ImageData + { + public string? Url { get; set; } + } + + /// + /// 转录响应 + /// + public class TranscriptionResponse + { + public string? Text { get; set; } + } + + /// + /// OpenAI 异常 + /// + public class OpenAIException : Exception + { + public string? ResponseJson { get; } + + public OpenAIException(string message, string? responseJson = null) : base(message) + { + ResponseJson = responseJson; + } + } + + #endregion +} \ No newline at end of file diff --git a/EasyTool.Core/AICategory/PromptBuilder.cs b/EasyTool.Core/AICategory/PromptBuilder.cs new file mode 100644 index 0000000..64743af --- /dev/null +++ b/EasyTool.Core/AICategory/PromptBuilder.cs @@ -0,0 +1,315 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.AICategory +{ + /// + /// 提示词构建器 + /// 提供构建和管理 AI 提示词的工具 + /// + public class PromptBuilder + { + private readonly StringBuilder _systemPrompt = new(); + private readonly List _examples = new(); + private readonly List _context = new(); + private readonly List _constraints = new(); + private string? _task; + private string? _outputFormat; + + /// + /// 设置系统提示词 + /// + /// 系统提示词 + /// 当前实例 + public PromptBuilder SetSystemPrompt(string systemPrompt) + { + _systemPrompt.Clear(); + _systemPrompt.Append(systemPrompt); + return this; + } + + /// + /// 添加系统提示词 + /// + /// 文本 + /// 当前实例 + public PromptBuilder AddSystemPrompt(string text) + { + _systemPrompt.AppendLine(text); + return this; + } + + /// + /// 设置任务描述 + /// + /// 任务描述 + /// 当前实例 + public PromptBuilder SetTask(string task) + { + _task = task; + return this; + } + + /// + /// 添加示例 + /// + /// 输入示例 + /// 输出示例 + /// 当前实例 + public PromptBuilder AddExample(string input, string output) + { + _examples.Add($"输入: {input}\n输出: {output}"); + return this; + } + + /// + /// 添加上下文 + /// + /// 上下文内容 + /// 当前实例 + public PromptBuilder AddContext(string context) + { + _context.Add(context); + return this; + } + + /// + /// 添加约束条件 + /// + /// 约束条件 + /// 当前实例 + public PromptBuilder AddConstraint(string constraint) + { + _constraints.Add(constraint); + return this; + } + + /// + /// 设置输出格式 + /// + /// 格式描述 + /// 当前实例 + public PromptBuilder SetOutputFormat(string format) + { + _outputFormat = format; + return this; + } + + /// + /// 设置 JSON 输出格式 + /// + /// JSON Schema 或示例 + /// 当前实例 + public PromptBuilder SetJsonOutput(string? schema = null) + { + if (!string.IsNullOrEmpty(schema)) + { + _outputFormat = $"请以 JSON 格式输出,格式如下:\n{schema}"; + } + else + { + _outputFormat = "请以有效的 JSON 格式输出,不要添加任何其他文本或解释。"; + } + return this; + } + + /// + /// 构建最终提示词 + /// + /// 构建的提示词 + public string Build() + { + var result = new StringBuilder(); + + // 系统提示词 + if (_systemPrompt.Length > 0) + { + result.AppendLine(_systemPrompt.ToString()); + result.AppendLine(); + } + + // 任务描述 + if (!string.IsNullOrEmpty(_task)) + { + result.AppendLine("## 任务"); + result.AppendLine(_task); + result.AppendLine(); + } + + // 上下文 + if (_context.Count > 0) + { + result.AppendLine("## 上下文"); + foreach (var ctx in _context) + { + result.AppendLine(ctx); + } + result.AppendLine(); + } + + // 示例 + if (_examples.Count > 0) + { + result.AppendLine("## 示例"); + foreach (var example in _examples) + { + result.AppendLine(example); + result.AppendLine(); + } + } + + // 约束条件 + if (_constraints.Count > 0) + { + result.AppendLine("## 约束条件"); + foreach (var constraint in _constraints) + { + result.AppendLine($"- {constraint}"); + } + result.AppendLine(); + } + + // 输出格式 + if (!string.IsNullOrEmpty(_outputFormat)) + { + result.AppendLine("## 输出格式"); + result.AppendLine(_outputFormat); + } + + return result.ToString().Trim(); + } + + /// + /// 构建消息列表 + /// + /// 用户输入 + /// 消息列表 + public List BuildMessages(string userInput) + { + var messages = new List(); + + // 系统消息 + var systemPrompt = Build(); + if (!string.IsNullOrEmpty(systemPrompt)) + { + messages.Add(new ChatMessage { Role = "system", Content = systemPrompt }); + } + + // 用户消息 + messages.Add(new ChatMessage { Role = "user", Content = userInput }); + + return messages; + } + + /// + /// 清空所有内容 + /// + /// 当前实例 + public PromptBuilder Clear() + { + _systemPrompt.Clear(); + _examples.Clear(); + _context.Clear(); + _constraints.Clear(); + _task = null; + _outputFormat = null; + return this; + } + } + + /// + /// 常用提示词模板 + /// + public static class PromptTemplates + { + /// + /// 代码审查提示词 + /// + public static string CodeReview(string code, string? language = null) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位经验丰富的代码审查专家。") + .SetTask("审查以下代码并提供改进建议。") + .AddConstraint("关注代码质量、性能、安全性") + .AddConstraint("提供具体的改进建议") + .AddConstraint("指出潜在的问题和风险"); + + if (!string.IsNullOrEmpty(language)) + { + builder.AddContext($"编程语言: {language}"); + } + + builder.SetOutputFormat("请按以下格式输出:\n1. 问题列表\n2. 改进建议\n3. 重构后的代码(如有必要)"); + + var messages = builder.BuildMessages($"待审查的代码:\n```\n{code}\n```"); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 翻译提示词 + /// + public static string Translate(string text, string sourceLanguage, string targetLanguage) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位专业的翻译专家,精通多种语言。") + .SetTask($"将以下文本从{sourceLanguage}翻译成{targetLanguage}。") + .AddConstraint("保持原文的语气和风格") + .AddConstraint("确保翻译准确、自然、流畅") + .AddConstraint("保留专业术语的准确性"); + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 摘要生成提示词 + /// + public static string Summarize(string text, int? maxLength = null) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位专业的文本摘要专家。") + .SetTask("请为以下文本生成简洁的摘要。") + .AddConstraint("保留关键信息和要点") + .AddConstraint("语言简洁明了"); + + if (maxLength.HasValue) + { + builder.AddConstraint($"摘要长度不超过{maxLength.Value}字"); + } + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 数据提取提示词 + /// + public static string ExtractData(string text, string[] fields) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位数据提取专家。") + .SetTask("从以下文本中提取指定的数据字段。") + .SetJsonOutput($"{{\"fields\": [{string.Join(", ", fields)}]}}"); + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 问答提示词 + /// + public static string QuestionAnswer(string context, string question) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位知识渊博的助手,根据给定的上下文回答问题。") + .SetTask("根据提供的上下文回答用户的问题。") + .AddConstraint("只根据上下文内容回答") + .AddConstraint("如果上下文中没有相关信息,请明确说明") + .AddContext($"上下文:\n{context}"); + + var messages = builder.BuildMessages(question); + return messages[0].Content + "\n\n" + messages[1].Content; + } + } +} diff --git a/EasyTool.Core/AICategory/VectorSimilarity.cs b/EasyTool.Core/AICategory/VectorSimilarity.cs new file mode 100644 index 0000000..b9eb150 --- /dev/null +++ b/EasyTool.Core/AICategory/VectorSimilarity.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.AICategory +{ + /// + /// 向量相似度计算工具 + /// 用于计算嵌入向量之间的相似度 + /// + public static class VectorSimilarity + { + /// + /// 计算余弦相似度 + /// + /// 向量1 + /// 向量2 + /// 相似度(-1到1) + public static double CosineSimilarity(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double dotProduct = 0; + double magnitude1 = 0; + double magnitude2 = 0; + + for (int i = 0; i < vector1.Length; i++) + { + dotProduct += vector1[i] * vector2[i]; + magnitude1 += vector1[i] * vector1[i]; + magnitude2 += vector2[i] * vector2[i]; + } + + magnitude1 = Math.Sqrt(magnitude1); + magnitude2 = Math.Sqrt(magnitude2); + + if (magnitude1 == 0 || magnitude2 == 0) + return 0; + + return dotProduct / (magnitude1 * magnitude2); + } + + /// + /// 计算欧几里得距离 + /// + /// 向量1 + /// 向量2 + /// 距离 + public static double EuclideanDistance(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double sum = 0; + for (int i = 0; i < vector1.Length; i++) + { + var diff = vector1[i] - vector2[i]; + sum += diff * diff; + } + + return Math.Sqrt(sum); + } + + /// + /// 计算点积 + /// + /// 向量1 + /// 向量2 + /// 点积 + public static double DotProduct(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double sum = 0; + for (int i = 0; i < vector1.Length; i++) + { + sum += vector1[i] * vector2[i]; + } + + return sum; + } + + /// + /// 归一化向量 + /// + /// 向量 + /// 归一化后的向量 + public static float[] Normalize(float[] vector) + { + double magnitude = 0; + for (int i = 0; i < vector.Length; i++) + { + magnitude += vector[i] * vector[i]; + } + + magnitude = Math.Sqrt(magnitude); + if (magnitude == 0) + return new float[vector.Length]; + + var result = new float[vector.Length]; + for (int i = 0; i < vector.Length; i++) + { + result[i] = (float)(vector[i] / magnitude); + } + + return result; + } + + /// + /// 查找最相似的向量 + /// + /// 查询向量 + /// 候选向量列表 + /// 返回数量 + /// 最相似向量的索引和相似度 + public static List<(int Index, double Similarity)> FindMostSimilar(float[] query, List candidates, int topK = 5) + { + var similarities = new List<(int Index, double Similarity)>(); + + for (int i = 0; i < candidates.Count; i++) + { + var similarity = CosineSimilarity(query, candidates[i]); + similarities.Add((i, similarity)); + } + + return similarities + .OrderByDescending(x => x.Similarity) + .Take(topK) + .ToList(); + } + } + + /// + /// 简单的向量存储 + /// 用于存储和检索嵌入向量 + /// + public class VectorStore + { + private readonly List _items = new(); + + /// + /// 添加向量 + /// + /// ID + /// 向量 + /// 元数据 + public void Add(string id, float[] vector, Dictionary? metadata = null) + { + _items.Add(new VectorItem + { + Id = id, + Vector = vector, + Metadata = metadata ?? new Dictionary() + }); + } + + /// + /// 批量添加向量 + /// + public void AddRange(IEnumerable<(string Id, float[] Vector, Dictionary? Metadata)> items) + { + foreach (var item in items) + { + Add(item.Id, item.Vector, item.Metadata); + } + } + + /// + /// 搜索相似向量 + /// + /// 查询向量 + /// 返回数量 + /// 最小相似度 + /// 搜索结果 + public List Search(float[] query, int topK = 5, double minScore = 0) + { + var results = new List(); + + foreach (var item in _items) + { + var score = VectorSimilarity.CosineSimilarity(query, item.Vector); + if (score >= minScore) + { + results.Add(new VectorSearchResult + { + Id = item.Id, + Score = score, + Metadata = item.Metadata + }); + } + } + + return results + .OrderByDescending(x => x.Score) + .Take(topK) + .ToList(); + } + + /// + /// 删除向量 + /// + /// ID + /// 是否删除成功 + public bool Remove(string id) + { + var item = _items.FirstOrDefault(x => x.Id == id); + if (item != null) + { + _items.Remove(item); + return true; + } + return false; + } + + /// + /// 清空所有向量 + /// + public void Clear() + { + _items.Clear(); + } + + /// + /// 获取向量数量 + /// + public int Count => _items.Count; + + /// + /// 获取所有 ID + /// + public IEnumerable GetAllIds() => _items.Select(x => x.Id); + } + + /// + /// 向量项 + /// + internal class VectorItem + { + public string Id { get; set; } = string.Empty; + public float[] Vector { get; set; } = Array.Empty(); + public Dictionary Metadata { get; set; } = new(); + } + + /// + /// 向量搜索结果 + /// + public class VectorSearchResult + { + /// + /// ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 相似度分数 + /// + public double Score { get; set; } + + /// + /// 元数据 + /// + public Dictionary Metadata { get; set; } = new(); + } +} diff --git a/EasyTool.Core/CacheCategory/CacheOptions.cs b/EasyTool.Core/CacheCategory/CacheOptions.cs new file mode 100644 index 0000000..eab98c5 --- /dev/null +++ b/EasyTool.Core/CacheCategory/CacheOptions.cs @@ -0,0 +1,110 @@ +using System; + +namespace EasyTool.CacheCategory +{ + /// + /// 缓存选项 + /// + public class CacheOptions + { + /// + /// 绝对过期时间 + /// + public DateTime? AbsoluteExpiration { get; set; } + + /// + /// 相对过期时间(从现在开始) + /// + public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; } + + /// + /// 滑动过期时间 + /// + public TimeSpan? SlidingExpiration { get; set; } + + /// + /// 缓存优先级 + /// + public CachePriority Priority { get; set; } = CachePriority.Normal; + + /// + /// 缓存键前缀 + /// + public string? KeyPrefix { get; set; } + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + + /// + /// 压缩阈值(字节) + /// + public int CompressionThreshold { get; set; } = 1024; + + /// + /// 创建相对过期选项 + /// + /// 过期时间 + /// 缓存选项 + public static CacheOptions FromExpiration(TimeSpan expiration) + { + return new CacheOptions + { + AbsoluteExpirationRelativeToNow = expiration + }; + } + + /// + /// 创建滑动过期选项 + /// + /// 滑动过期时间 + /// 缓存选项 + public static CacheOptions FromSlidingExpiration(TimeSpan slidingExpiration) + { + return new CacheOptions + { + SlidingExpiration = slidingExpiration + }; + } + + /// + /// 创建绝对过期选项 + /// + /// 绝对过期时间 + /// 缓存选项 + public static CacheOptions FromAbsoluteExpiration(DateTime absoluteExpiration) + { + return new CacheOptions + { + AbsoluteExpiration = absoluteExpiration + }; + } + } + + /// + /// 缓存优先级 + /// + public enum CachePriority + { + /// + /// 低优先级 + /// + Low = 0, + + /// + /// 普通优先级 + /// + Normal = 1, + + /// + /// 高优先级 + /// + High = 2, + + /// + /// 永不移除 + /// + NeverRemove = 3 + } +} diff --git a/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs b/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs new file mode 100644 index 0000000..3360974 --- /dev/null +++ b/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs @@ -0,0 +1,500 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 分布式缓存工具类 + /// 提供多级缓存支持,包括本地缓存和分布式缓存 + /// + public static class DistributedCacheUtil + { + private static readonly ConcurrentDictionary _providers = new(); + private static ICacheProvider? _defaultProvider; + private static readonly object _lock = new(); + + /// + /// 注册缓存提供者 + /// + /// 提供者名称 + /// 缓存提供者 + /// 是否设为默认 + public static void RegisterProvider(string name, ICacheProvider provider, bool setDefault = false) + { + _providers[name] = provider; + + if (setDefault || _defaultProvider == null) + { + _defaultProvider = provider; + } + } + + /// + /// 获取缓存提供者 + /// + /// 提供者名称 + /// 缓存提供者 + public static ICacheProvider? GetProvider(string name) + { + return _providers.TryGetValue(name, out var provider) ? provider : null; + } + + /// + /// 获取默认缓存提供者 + /// + public static ICacheProvider DefaultProvider + { + get + { + if (_defaultProvider == null) + { + lock (_lock) + { + if (_defaultProvider == null) + { + _defaultProvider = new MemoryCacheProvider(); + _providers["default"] = _defaultProvider; + } + } + } + return _defaultProvider; + } + } + + /// + /// 创建内存缓存提供者 + /// + /// 清理间隔 + /// 大小限制 + /// 缓存提供者 + public static MemoryCacheProvider CreateMemoryProvider(TimeSpan? cleanupInterval = null, long? sizeLimit = null) + { + return new MemoryCacheProvider(cleanupInterval, sizeLimit); + } + + /// + /// 创建 Redis 缓存提供者 + /// + /// Redis 配置 + /// 缓存提供者 + public static RedisCacheProvider CreateRedisProvider(RedisCacheOptions? options = null) + { + return new RedisCacheProvider(options); + } + + #region 便捷方法 - 使用默认提供者 + + /// + /// 设置缓存 + /// + public static Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + return DefaultProvider.SetAsync(key, value, options, cancellationToken); + } + + /// + /// 设置缓存(同步) + /// + public static void Set(string key, T value, CacheOptions? options = null) + { + DefaultProvider.Set(key, value, options); + } + + /// + /// 获取缓存 + /// + public static Task GetAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.GetAsync(key, cancellationToken); + } + + /// + /// 获取缓存(同步) + /// + public static T? Get(string key) + { + return DefaultProvider.Get(key); + } + + /// + /// 获取或添加缓存 + /// + public static Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + return DefaultProvider.GetOrAddAsync(key, factory, options, cancellationToken); + } + + /// + /// 获取或添加缓存(同步) + /// + public static T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return DefaultProvider.GetOrAdd(key, factory, options); + } + + /// + /// 检查缓存是否存在 + /// + public static Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.ExistsAsync(key, cancellationToken); + } + + /// + /// 检查缓存是否存在(同步) + /// + public static bool Exists(string key) + { + return DefaultProvider.Exists(key); + } + + /// + /// 移除缓存 + /// + public static Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.RemoveAsync(key, cancellationToken); + } + + /// + /// 移除缓存(同步) + /// + public static void Remove(string key) + { + DefaultProvider.Remove(key); + } + + /// + /// 清空缓存 + /// + public static Task ClearAsync(CancellationToken cancellationToken = default) + { + return DefaultProvider.ClearAsync(cancellationToken); + } + + /// + /// 清空缓存(同步) + /// + public static void Clear() + { + DefaultProvider.Clear(); + } + + #endregion + + #region 高级功能 + + /// + /// 批量获取缓存 + /// + /// 值类型 + /// 缓存键集合 + /// 取消令牌 + /// 键值对字典 + public static async Task> GetManyAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + var result = new Dictionary(); + + foreach (var key in keys) + { + var value = await DefaultProvider.GetAsync(key, cancellationToken); + result[key] = value; + } + + return result; + } + + /// + /// 批量设置缓存 + /// + /// 值类型 + /// 键值对集合 + /// 缓存选项 + /// 取消令牌 + public static async Task SetManyAsync(IDictionary items, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + foreach (var item in items) + { + await DefaultProvider.SetAsync(item.Key, item.Value, options, cancellationToken); + } + } + + /// + /// 获取或添加缓存(带锁) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 锁超时时间 + /// 取消令牌 + /// 缓存值 + public static async Task GetOrAddWithLockAsync( + string key, + Func> factory, + CacheOptions? options = null, + TimeSpan? lockTimeout = null, + CancellationToken cancellationToken = default) + { + var value = await DefaultProvider.GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + // 使用简单的锁机制防止缓存穿透 + var lockKey = $"lock:{key}"; + var timeout = lockTimeout ?? TimeSpan.FromSeconds(30); + var startTime = DateTime.UtcNow; + + while (DateTime.UtcNow - startTime < timeout) + { + value = await DefaultProvider.GetAsync(key, cancellationToken); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; + + value = await factory(); + await DefaultProvider.SetAsync(key, value, options, cancellationToken); + return value; + } + + throw new TimeoutException($"获取缓存超时: {key}"); + } + + /// + /// 刷新缓存(强制重新加载) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 取消令牌 + /// 新的缓存值 + public static async Task RefreshAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + await DefaultProvider.RemoveAsync(key, cancellationToken); + var value = await factory(); + await DefaultProvider.SetAsync(key, value, options, cancellationToken); + return value; + } + + #endregion + } + + /// + /// 多级缓存 + /// 实现本地缓存 + 分布式缓存的多级缓存策略 + /// + public class MultiLevelCache : ICacheProvider, IDisposable + { + private readonly MemoryCacheProvider _localCache; + private readonly ICacheProvider? _distributedCache; + private readonly TimeSpan _localCacheExpiration; + + /// + /// 创建多级缓存 + /// + /// 分布式缓存提供者 + /// 本地缓存过期时间 + public MultiLevelCache(ICacheProvider? distributedCache = null, TimeSpan? localCacheExpiration = null) + { + _localCache = new MemoryCacheProvider(); + _distributedCache = distributedCache; + _localCacheExpiration = localCacheExpiration ?? TimeSpan.FromMinutes(5); + } + + /// + public async Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + // 先设置本地缓存 + var localOptions = new CacheOptions + { + AbsoluteExpirationRelativeToNow = _localCacheExpiration + }; + await _localCache.SetAsync(key, value, localOptions, cancellationToken); + + // 再设置分布式缓存 + if (_distributedCache != null) + { + await _distributedCache.SetAsync(key, value, options, cancellationToken); + } + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + SetAsync(key, value, options).GetAwaiter().GetResult(); + } + + /// + public async Task GetAsync(string key, CancellationToken cancellationToken = default) + { + // 先查本地缓存 + var value = await _localCache.GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + // 再查分布式缓存 + if (_distributedCache != null) + { + value = await _distributedCache.GetAsync(key, cancellationToken); + if (value != null) + { + // 回填本地缓存 + await _localCache.SetAsync(key, value, new CacheOptions + { + AbsoluteExpirationRelativeToNow = _localCacheExpiration + }, cancellationToken); + } + } + + return value; + } + + /// + public T? Get(string key) + { + return GetAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = await GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + await SetAsync(key, value, options, cancellationToken); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return GetOrAddAsync(key, () => Task.FromResult(factory()), options).GetAwaiter().GetResult(); + } + + /// + public async Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + if (await _localCache.ExistsAsync(key, cancellationToken)) + return true; + + return _distributedCache != null && await _distributedCache.ExistsAsync(key, cancellationToken); + } + + /// + public bool Exists(string key) + { + return ExistsAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + await _localCache.RemoveAsync(key, cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.RemoveAsync(key, cancellationToken); + } + } + + /// + public void Remove(string key) + { + RemoveAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + await _localCache.RemoveAsync(keys, cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.RemoveAsync(keys, cancellationToken); + } + } + + /// + public void Remove(IEnumerable keys) + { + RemoveAsync(keys).GetAwaiter().GetResult(); + } + + /// + public async Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + var localResult = await _localCache.SetExpirationAsync(key, expiration, cancellationToken); + + if (_distributedCache != null) + { + return await _distributedCache.SetExpirationAsync(key, expiration, cancellationToken); + } + + return localResult; + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + return SetExpirationAsync(key, expiration).GetAwaiter().GetResult(); + } + + /// + public async Task ClearAsync(CancellationToken cancellationToken = default) + { + await _localCache.ClearAsync(cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.ClearAsync(cancellationToken); + } + } + + /// + public void Clear() + { + ClearAsync().GetAwaiter().GetResult(); + } + + /// + public async Task CountAsync(CancellationToken cancellationToken = default) + { + var count = await _localCache.CountAsync(cancellationToken); + + if (_distributedCache != null) + { + count = await _distributedCache.CountAsync(cancellationToken); + } + + return count; + } + + /// + public long Count() + { + return CountAsync().GetAwaiter().GetResult(); + } + + /// + /// 释放资源 + /// + public void Dispose() + { + _localCache.Dispose(); + } + } +} diff --git a/EasyTool.Core/CacheCategory/ICacheProvider.cs b/EasyTool.Core/CacheCategory/ICacheProvider.cs new file mode 100644 index 0000000..7d5e7d4 --- /dev/null +++ b/EasyTool.Core/CacheCategory/ICacheProvider.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 缓存提供者接口 + /// + public interface ICacheProvider + { + /// + /// 设置缓存 + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 缓存选项 + /// 取消令牌 + Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// 设置缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 缓存选项 + void Set(string key, T value, CacheOptions? options = null); + + /// + /// 获取缓存 + /// + /// 值类型 + /// 缓存键 + /// 取消令牌 + /// 缓存值 + Task GetAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 获取缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + T? Get(string key); + + /// + /// 获取或添加缓存 + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 取消令牌 + /// 缓存值 + Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// 获取或添加缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 缓存值 + T GetOrAdd(string key, Func factory, CacheOptions? options = null); + + /// + /// 检查缓存是否存在 + /// + /// 缓存键 + /// 取消令牌 + /// 是否存在 + Task ExistsAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 检查缓存是否存在(同步) + /// + /// 缓存键 + /// 是否存在 + bool Exists(string key); + + /// + /// 移除缓存 + /// + /// 缓存键 + /// 取消令牌 + Task RemoveAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 移除缓存(同步) + /// + /// 缓存键 + void Remove(string key); + + /// + /// 批量移除缓存 + /// + /// 缓存键集合 + /// 取消令牌 + Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default); + + /// + /// 批量移除缓存(同步) + /// + /// 缓存键集合 + void Remove(IEnumerable keys); + + /// + /// 设置过期时间 + /// + /// 缓存键 + /// 过期时间 + /// 取消令牌 + /// 是否设置成功 + Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default); + + /// + /// 设置过期时间(同步) + /// + /// 缓存键 + /// 过期时间 + /// 是否设置成功 + bool SetExpiration(string key, TimeSpan expiration); + + /// + /// 清空所有缓存 + /// + /// 取消令牌 + Task ClearAsync(CancellationToken cancellationToken = default); + + /// + /// 清空所有缓存(同步) + /// + void Clear(); + + /// + /// 获取缓存数量 + /// + /// 取消令牌 + /// 缓存项数量 + Task CountAsync(CancellationToken cancellationToken = default); + + /// + /// 获取缓存数量(同步) + /// + /// 缓存项数量 + long Count(); + } +} diff --git a/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs b/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs new file mode 100644 index 0000000..62d122e --- /dev/null +++ b/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs @@ -0,0 +1,399 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 内存缓存项 + /// + internal class MemoryCacheItem + { + public object? Value { get; set; } + public DateTime CreateTime { get; set; } + public DateTime? AbsoluteExpiration { get; set; } + public TimeSpan? SlidingExpiration { get; set; } + public DateTime LastAccess { get; set; } + public CachePriority Priority { get; set; } + public Type ValueType { get; set; } = typeof(object); + } + + /// + /// 内存缓存提供者 + /// 提供高性能的内存缓存实现,支持过期策略和优先级 + /// + public class MemoryCacheProvider : ICacheProvider, IDisposable + { + private readonly ConcurrentDictionary _cache; + private readonly Timer? _cleanupTimer; + private readonly long? _sizeLimit; + private long _currentSize; + private bool _disposed; + + /// + /// 创建内存缓存提供者 + /// + /// 清理间隔 + /// 大小限制(项数) + public MemoryCacheProvider(TimeSpan? cleanupInterval = null, long? sizeLimit = null) + { + _cache = new ConcurrentDictionary(); + _sizeLimit = sizeLimit; + _currentSize = 0; + + // 定期清理过期缓存 + var interval = cleanupInterval ?? TimeSpan.FromMinutes(1); + _cleanupTimer = new Timer(CleanupExpired, null, interval, interval); + } + + /// + public Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + Set(key, value, options); + return Task.CompletedTask; + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + options ??= new CacheOptions(); + + var item = new MemoryCacheItem + { + Value = value, + ValueType = typeof(T), + CreateTime = DateTime.UtcNow, + LastAccess = DateTime.UtcNow, + Priority = options.Priority, + SlidingExpiration = options.SlidingExpiration + }; + + // 计算过期时间 + if (options.AbsoluteExpiration.HasValue) + { + item.AbsoluteExpiration = options.AbsoluteExpiration.Value.ToUniversalTime(); + } + else if (options.AbsoluteExpirationRelativeToNow.HasValue) + { + item.AbsoluteExpiration = DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow.Value); + } + + // 添加键前缀 + var cacheKey = options.KeyPrefix != null + ? $"{options.KeyPrefix}:{key}" + : key; + + _cache.AddOrUpdate(cacheKey, item, (k, old) => + { + Interlocked.Decrement(ref _currentSize); + return item; + }); + + Interlocked.Increment(ref _currentSize); + + // 检查容量限制 + if (_sizeLimit.HasValue && _currentSize > _sizeLimit.Value) + { + EvictLowPriorityItems(); + } + } + + /// + public Task GetAsync(string key, CancellationToken cancellationToken = default) + { + return Task.FromResult(Get(key)); + } + + /// + public T? Get(string key) + { + if (string.IsNullOrEmpty(key)) + return default; + + if (!_cache.TryGetValue(key, out var item)) + return default; + + if (IsExpired(item)) + { + _cache.TryRemove(key, out _); + Interlocked.Decrement(ref _currentSize); + return default; + } + + // 更新滑动过期 + if (item.SlidingExpiration.HasValue) + { + item.LastAccess = DateTime.UtcNow; + } + + return (T?)item.Value; + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + Set(key, value, options); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = factory(); + Set(key, value, options); + return value; + } + + /// + public Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + return Task.FromResult(Exists(key)); + } + + /// + public bool Exists(string key) + { + if (string.IsNullOrEmpty(key)) + return false; + + if (!_cache.TryGetValue(key, out var item)) + return false; + + if (IsExpired(item)) + { + _cache.TryRemove(key, out _); + Interlocked.Decrement(ref _currentSize); + return false; + } + + return true; + } + + /// + public Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + Remove(key); + return Task.CompletedTask; + } + + /// + public void Remove(string key) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + + /// + public Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + Remove(keys); + return Task.CompletedTask; + } + + /// + public void Remove(IEnumerable keys) + { + foreach (var key in keys) + { + Remove(key); + } + } + + /// + public Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + return Task.FromResult(SetExpiration(key, expiration)); + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + if (!_cache.TryGetValue(key, out var item)) + return false; + + item.AbsoluteExpiration = DateTime.UtcNow.Add(expiration); + return true; + } + + /// + public Task ClearAsync(CancellationToken cancellationToken = default) + { + Clear(); + return Task.CompletedTask; + } + + /// + public void Clear() + { + _cache.Clear(); + Interlocked.Exchange(ref _currentSize, 0); + } + + /// + public Task CountAsync(CancellationToken cancellationToken = default) + { + return Task.FromResult(Count()); + } + + /// + public long Count() + { + return _cache.Count; + } + + /// + /// 获取所有缓存键 + /// + /// 缓存键集合 + public IEnumerable GetKeys() + { + return _cache.Keys.ToList(); + } + + /// + /// 获取缓存统计信息 + /// + /// 统计信息 + public CacheStatistics GetStatistics() + { + var now = DateTime.UtcNow; + var items = _cache.Values.ToList(); + + return new CacheStatistics + { + TotalCount = items.Count, + ExpiredCount = items.Count(i => IsExpired(i)), + HighPriorityCount = items.Count(i => i.Priority == CachePriority.High), + LowPriorityCount = items.Count(i => i.Priority == CachePriority.Low), + EstimatedSize = _currentSize + }; + } + + private bool IsExpired(MemoryCacheItem item) + { + var now = DateTime.UtcNow; + + // 检查绝对过期 + if (item.AbsoluteExpiration.HasValue && now >= item.AbsoluteExpiration.Value) + return true; + + // 检查滑动过期 + if (item.SlidingExpiration.HasValue) + { + var expireTime = item.LastAccess.Add(item.SlidingExpiration.Value); + if (now >= expireTime) + return true; + } + + return false; + } + + private void CleanupExpired(object? state) + { + var keysToRemove = new List(); + + foreach (var kvp in _cache) + { + if (IsExpired(kvp.Value)) + { + keysToRemove.Add(kvp.Key); + } + } + + foreach (var key in keysToRemove) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + } + + private void EvictLowPriorityItems() + { + // 按优先级和访问时间排序,移除低优先级的项 + var itemsToEvict = _cache + .Where(kvp => kvp.Value.Priority != CachePriority.NeverRemove) + .OrderBy(kvp => (int)kvp.Value.Priority) + .ThenBy(kvp => kvp.Value.LastAccess) + .Take((int)(_currentSize - _sizeLimit!.Value + 10)) + .Select(kvp => kvp.Key) + .ToList(); + + foreach (var key in itemsToEvict) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cleanupTimer?.Dispose(); + _cache.Clear(); + _disposed = true; + } + } + } + + /// + /// 缓存统计信息 + /// + public class CacheStatistics + { + /// + /// 总缓存项数 + /// + public long TotalCount { get; set; } + + /// + /// 已过期项数 + /// + public long ExpiredCount { get; set; } + + /// + /// 高优先级项数 + /// + public long HighPriorityCount { get; set; } + + /// + /// 低优先级项数 + /// + public long LowPriorityCount { get; set; } + + /// + /// 估计大小 + /// + public long EstimatedSize { get; set; } + } +} diff --git a/EasyTool.Core/CacheCategory/RedisCacheProvider.cs b/EasyTool.Core/CacheCategory/RedisCacheProvider.cs new file mode 100644 index 0000000..7a50ed7 --- /dev/null +++ b/EasyTool.Core/CacheCategory/RedisCacheProvider.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// Redis 缓存配置 + /// + public class RedisCacheOptions + { + /// + /// Redis 连接字符串 + /// + public string ConnectionString { get; set; } = "localhost:6379"; + + /// + /// 实例名称 + /// + public string InstanceName { get; set; } = ""; + + /// + /// 默认数据库 + /// + public int DefaultDatabase { get; set; } = 0; + + /// + /// 默认过期时间 + /// + public TimeSpan? DefaultExpiration { get; set; } + + /// + /// 连接超时 + /// + public TimeSpan ConnectTimeout { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 是否允许管理员操作 + /// + public bool AllowAdmin { get; set; } + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + } + + /// + /// Redis 缓存提供者 + /// 注意:此类提供 Redis 缓存的抽象接口,实际使用需要引入 StackExchange.Redis 包 + /// + public class RedisCacheProvider : ICacheProvider, IAsyncDisposable, IDisposable + { + private readonly RedisCacheOptions _options; + private readonly string _keyPrefix; + private object? _connectionMultiplexer; + private object? _database; + private bool _disposed; + + /// + /// 创建 Redis 缓存提供者 + /// + /// Redis 配置 + public RedisCacheProvider(RedisCacheOptions? options = null) + { + _options = options ?? new RedisCacheOptions(); + _keyPrefix = string.IsNullOrEmpty(_options.InstanceName) + ? "" + : _options.InstanceName + ":"; + } + + /// + /// 获取 Redis 连接(需要 StackExchange.Redis) + /// 此方法为扩展点,子类可重写以实现具体的 Redis 连接逻辑 + /// + protected virtual object? GetConnection() + { + // 这是一个占位实现 + // 实际使用时需要引入 StackExchange.Redis 并实现连接逻辑 + throw new NotImplementedException( + "请引入 StackExchange.Redis 包并实现 Redis 连接逻辑," + + "或使用 DistributedCacheUtil.CreateRedisProvider 方法"); + } + + private string GetFullKey(string key) => $"{_keyPrefix}{key}"; + + /// + public async Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + + var fullKey = GetFullKey(key); + var expiration = GetExpiration(options); + + // 这里需要实际的 Redis 实现来设置值 + await Task.CompletedTask; + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + SetAsync(key, value, options).GetAwaiter().GetResult(); + } + + /// + public async Task GetAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return default; + } + + /// + public T? Get(string key) + { + return GetAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = await GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + await SetAsync(key, value, options, cancellationToken); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return GetOrAddAsync(key, () => Task.FromResult(factory()), options).GetAwaiter().GetResult(); + } + + /// + public async Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return false; + } + + /// + public bool Exists(string key) + { + return ExistsAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Remove(string key) + { + RemoveAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Remove(IEnumerable keys) + { + RemoveAsync(keys).GetAwaiter().GetResult(); + } + + /// + public async Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return false; + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + return SetExpirationAsync(key, expiration).GetAwaiter().GetResult(); + } + + /// + public async Task ClearAsync(CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Clear() + { + ClearAsync().GetAwaiter().GetResult(); + } + + /// + public async Task CountAsync(CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return 0; + } + + /// + public long Count() + { + return CountAsync().GetAwaiter().GetResult(); + } + + private TimeSpan? GetExpiration(CacheOptions? options) + { + if (options?.AbsoluteExpirationRelativeToNow != null) + return options.AbsoluteExpirationRelativeToNow; + + if (options?.AbsoluteExpiration != null) + return options.AbsoluteExpiration.Value - DateTime.UtcNow; + + if (options?.SlidingExpiration != null) + return options.SlidingExpiration; + + return _options.DefaultExpiration; + } + + private void ThrowIfNotImplemented() + { + if (_connectionMultiplexer == null) + { + throw new NotImplementedException( + "Redis 缓存提供者需要实际实现。请引入 StackExchange.Redis 包," + + "或使用 MemoryCacheProvider 作为替代。"); + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + (_connectionMultiplexer as IDisposable)?.Dispose(); + _disposed = true; + } + } + + /// + /// 异步释放资源 + /// + public async ValueTask DisposeAsync() + { + if (!_disposed) + { + if (_connectionMultiplexer is IAsyncDisposable asyncDisposable) + { + await asyncDisposable.DisposeAsync(); + } + else if (_connectionMultiplexer is IDisposable disposable) + { + disposable.Dispose(); + } + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/CodeCategory/AeadUtil.cs b/EasyTool.Core/CodeCategory/AeadUtil.cs new file mode 100644 index 0000000..6072da8 --- /dev/null +++ b/EasyTool.Core/CodeCategory/AeadUtil.cs @@ -0,0 +1,295 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// AEAD(认证加密)工具类 + /// 提供带有关联数据的认证加密功能 + /// + public static class AeadUtil + { + #region AES-GCM + + /// + /// 使用 AES-GCM 加密 + /// + /// 明文 + /// 密钥(16/24/32 字节) + /// 随机数(12 字节推荐) + /// 关联数据 + /// 加密结果(密文 + 标签) + public static AeadResult EncryptAesGcm(byte[] plaintext, byte[] key, byte[]? nonce = null, byte[]? associatedData = null) + { + nonce ??= GenerateNonce(12); + + var ciphertext = new byte[plaintext.Length]; + var tag = new byte[16]; + +#if NETSTANDARD2_1 + using var aesGcm = new AesGcm(key); + aesGcm.Encrypt(nonce, plaintext, ciphertext, tag); +#else + using var aesGcm = new AesGcm(key, 16); + aesGcm.Encrypt(nonce, plaintext, ciphertext, tag, associatedData); +#endif + + return new AeadResult + { + Ciphertext = ciphertext, + Nonce = nonce, + Tag = tag + }; + } + + /// + /// 使用 AES-GCM 解密 + /// + /// 密文 + /// 密钥 + /// 随机数 + /// 认证标签 + /// 关联数据 + /// 明文 + public static byte[] DecryptAesGcm(byte[] ciphertext, byte[] key, byte[] nonce, byte[] tag, byte[]? associatedData = null) + { + var plaintext = new byte[ciphertext.Length]; + +#if NETSTANDARD2_1 + using var aesGcm = new AesGcm(key); + aesGcm.Decrypt(nonce, ciphertext, tag, plaintext); +#else + using var aesGcm = new AesGcm(key, 16); + aesGcm.Decrypt(nonce, ciphertext, tag, plaintext, associatedData); +#endif + + return plaintext; + } + + /// + /// 使用 AES-GCM 解密(使用 AeadResult) + /// + public static byte[] DecryptAesGcm(AeadResult encrypted, byte[] key, byte[]? associatedData = null) + { + return DecryptAesGcm(encrypted.Ciphertext, key, encrypted.Nonce, encrypted.Tag, associatedData); + } + + #endregion + + #region ChaCha20-Poly1305 + +#if NET5_0_OR_GREATER + /// + /// 使用 ChaCha20-Poly1305 加密 + /// + /// 明文 + /// 密钥(32 字节) + /// 随机数(12 字节) + /// 关联数据 + /// 加密结果 + public static AeadResult EncryptChaCha20Poly1305(byte[] plaintext, byte[] key, byte[]? nonce = null, byte[]? associatedData = null) + { + nonce ??= GenerateNonce(12); + + var ciphertext = new byte[plaintext.Length]; + var tag = new byte[16]; + + using var chaCha20Poly1305 = new ChaCha20Poly1305(key); + chaCha20Poly1305.Encrypt(nonce, plaintext, ciphertext, tag, associatedData); + + return new AeadResult + { + Ciphertext = ciphertext, + Nonce = nonce, + Tag = tag + }; + } + + /// + /// 使用 ChaCha20-Poly1305 解密 + /// + public static byte[] DecryptChaCha20Poly1305(byte[] ciphertext, byte[] key, byte[] nonce, byte[] tag, byte[]? associatedData = null) + { + var plaintext = new byte[ciphertext.Length]; + + using var chaCha20Poly1305 = new ChaCha20Poly1305(key); + chaCha20Poly1305.Decrypt(nonce, ciphertext, tag, plaintext, associatedData); + + return plaintext; + } + + /// + /// 使用 ChaCha20-Poly1305 解密(使用 AeadResult) + /// + public static byte[] DecryptChaCha20Poly1305(AeadResult encrypted, byte[] key, byte[]? associatedData = null) + { + return DecryptChaCha20Poly1305(encrypted.Ciphertext, key, encrypted.Nonce, encrypted.Tag, associatedData); + } +#endif + + #endregion + + #region 密钥和随机数生成 + + /// + /// 生成 AES 密钥 + /// + /// 密钥大小(128/192/256 位) + /// 密钥 + public static byte[] GenerateAesKey(int keySize = 256) + { + int keyBytes = keySize / 8; + using var rng = RandomNumberGenerator.Create(); + var key = new byte[keyBytes]; + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机数(Nonce) + /// + /// 大小(字节),默认 12 + /// 随机数 + public static byte[] GenerateNonce(int size = 12) + { + using var rng = RandomNumberGenerator.Create(); + var nonce = new byte[size]; + rng.GetBytes(nonce); + return nonce; + } + + #endregion + + #region 便捷方法 + + /// + /// 简化的加密(自动生成密钥和随机数) + /// + /// 明文 + /// 密钥(可选,自动生成) + /// 加密结果 + public static (AeadResult Result, byte[] Key) EncryptSimple(byte[] plaintext, byte[]? key = null) + { + key ??= GenerateAesKey(256); + var result = EncryptAesGcm(plaintext, key); + return (result, key); + } + + /// + /// 简化的解密 + /// + /// 加密结果 + /// 密钥 + /// 明文 + public static byte[] DecryptSimple(AeadResult encrypted, byte[] key) + { + return DecryptAesGcm(encrypted, key); + } + + /// + /// 加密字符串 + /// + /// 明文字符串 + /// Base64 编码的密钥 + /// Base64 编码的加密结果 + public static string EncryptString(string plaintext, string keyBase64) + { + var key = Convert.FromBase64String(keyBase64); + var plaintextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext); + var result = EncryptAesGcm(plaintextBytes, key); + return result.ToBase64(); + } + + /// + /// 解密字符串 + /// + /// Base64 编码的加密结果 + /// Base64 编码的密钥 + /// 明文字符串 + public static string DecryptString(string ciphertextBase64, string keyBase64) + { + var key = Convert.FromBase64String(keyBase64); + var encrypted = AeadResult.FromBase64(ciphertextBase64); + var plaintextBytes = DecryptAesGcm(encrypted, key); + return System.Text.Encoding.UTF8.GetString(plaintextBytes); + } + + #endregion + } + + /// + /// AEAD 加密结果 + /// + public class AeadResult + { + /// + /// 密文 + /// + public byte[] Ciphertext { get; set; } = Array.Empty(); + + /// + /// 随机数(Nonce) + /// + public byte[] Nonce { get; set; } = Array.Empty(); + + /// + /// 认证标签 + /// + public byte[] Tag { get; set; } = Array.Empty(); + + /// + /// 获取完整的加密数据(Nonce + Tag + Ciphertext) + /// + /// 完整数据 + public byte[] ToCombinedBytes() + { + var result = new byte[Nonce.Length + Tag.Length + Ciphertext.Length]; + Buffer.BlockCopy(Nonce, 0, result, 0, Nonce.Length); + Buffer.BlockCopy(Tag, 0, result, Nonce.Length, Tag.Length); + Buffer.BlockCopy(Ciphertext, 0, result, Nonce.Length + Tag.Length, Ciphertext.Length); + return result; + } + + /// + /// 从完整数据解析 + /// + /// 完整数据 + /// Nonce 大小 + /// Tag 大小 + /// AEAD 结果 + public static AeadResult FromCombinedBytes(byte[] combined, int nonceSize = 12, int tagSize = 16) + { + var nonce = new byte[nonceSize]; + var tag = new byte[tagSize]; + var ciphertext = new byte[combined.Length - nonceSize - tagSize]; + + Buffer.BlockCopy(combined, 0, nonce, 0, nonceSize); + Buffer.BlockCopy(combined, nonceSize, tag, 0, tagSize); + Buffer.BlockCopy(combined, nonceSize + tagSize, ciphertext, 0, ciphertext.Length); + + return new AeadResult + { + Nonce = nonce, + Tag = tag, + Ciphertext = ciphertext + }; + } + + /// + /// 转换为 Base64 字符串 + /// + public string ToBase64() + { + return Convert.ToBase64String(ToCombinedBytes()); + } + + /// + /// 从 Base64 字符串解析 + /// + public static AeadResult FromBase64(string base64) + { + var combined = Convert.FromBase64String(base64); + return FromCombinedBytes(combined); + } + } +} diff --git a/EasyTool.Core/CodeCategory/HmacUtil.cs b/EasyTool.Core/CodeCategory/HmacUtil.cs new file mode 100644 index 0000000..a014a91 --- /dev/null +++ b/EasyTool.Core/CodeCategory/HmacUtil.cs @@ -0,0 +1,255 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// HMAC(基于哈希的消息认证码)工具类 + /// 提供各种哈希算法的 HMAC 实现 + /// + public static class HmacUtil + { + #region HMAC-MD5 + + /// + /// 计算 HMAC-MD5 + /// + /// 数据 + /// 密钥 + /// HMAC-MD5 哈希值(十六进制字符串) + public static string HmacMD5(byte[] data, byte[] key) + { + using var hmac = new HMACMD5(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-MD5 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-MD5 哈希值(十六进制字符串) + public static string HmacMD5(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacMD5(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-MD5 + /// + /// 数据 + /// 密钥 + /// 期望的哈希值(十六进制字符串) + /// 是否匹配 + public static bool VerifyHmacMD5(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacMD5(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA1 + + /// + /// 计算 HMAC-SHA1 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA1 哈希值(十六进制字符串) + public static string HmacSHA1(byte[] data, byte[] key) + { + using var hmac = new HMACSHA1(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA1 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-SHA1 哈希值(十六进制字符串) + public static string HmacSHA1(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA1(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA1 + /// + public static bool VerifyHmacSHA1(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA1(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA256 + + /// + /// 计算 HMAC-SHA256 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA256 哈希值(十六进制字符串) + public static string HmacSHA256(byte[] data, byte[] key) + { + using var hmac = new HMACSHA256(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA256 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-SHA256 哈希值(十六进制字符串) + public static string HmacSHA256(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA256(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA256 + /// + public static bool VerifyHmacSHA256(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA256(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA384 + + /// + /// 计算 HMAC-SHA384 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA384 哈希值(十六进制字符串) + public static string HmacSHA384(byte[] data, byte[] key) + { + using var hmac = new HMACSHA384(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA384 + /// + public static string HmacSHA384(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA384(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA384 + /// + public static bool VerifyHmacSHA384(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA384(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA512 + + /// + /// 计算 HMAC-SHA512 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA512 哈希值(十六进制字符串) + public static string HmacSHA512(byte[] data, byte[] key) + { + using var hmac = new HMACSHA512(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA512 + /// + public static string HmacSHA512(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA512(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA512 + /// + public static bool VerifyHmacSHA512(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA512(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 通用方法 + + /// + /// 生成随机密钥 + /// + /// 密钥大小(字节) + /// 随机密钥 + public static byte[] GenerateKey(int size = 32) + { + using var rng = RandomNumberGenerator.Create(); + var key = new byte[size]; + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥(Base64 编码) + /// + /// 密钥大小(字节) + /// Base64 编码的随机密钥 + public static string GenerateKeyBase64(int size = 32) + { + var key = GenerateKey(size); + return Convert.ToBase64String(key); + } + + /// + /// 使用时间安全的比较方法验证 HMAC + /// + /// 实际值 + /// 期望值 + /// 是否匹配 + public static bool ConstantTimeEquals(byte[] actual, byte[] expected) + { + if (actual == null || expected == null) + return false; + + if (actual.Length != expected.Length) + return false; + + int result = 0; + for (int i = 0; i < actual.Length; i++) + { + result |= actual[i] ^ expected[i]; + } + + return result == 0; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/CodeCategory/KdfUtil.cs b/EasyTool.Core/CodeCategory/KdfUtil.cs new file mode 100644 index 0000000..6793662 --- /dev/null +++ b/EasyTool.Core/CodeCategory/KdfUtil.cs @@ -0,0 +1,322 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 密钥派生函数(KDF)工具类 + /// 提供安全的密钥派生方法 + /// + public static class KdfUtil + { + #region PBKDF2 + + /// + /// 使用 PBKDF2 派生密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Pbkdf2(byte[] password, byte[] salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var kdf = new Rfc2898DeriveBytes(password, salt, iterations, hashAlgorithm); + return kdf.GetBytes(keySize); + } + + /// + /// 使用 PBKDF2 派生密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 密钥大小(字节) + /// 哈希算法 + /// 派生的密钥(十六进制字符串) + public static string Pbkdf2Hex(string password, string salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + var passwordBytes = Encoding.UTF8.GetBytes(password); + var saltBytes = Encoding.UTF8.GetBytes(salt); + var key = Pbkdf2(passwordBytes, saltBytes, iterations, keySize, hashAlgorithm); + return HexUtil.BytesToHex(key); + } + + /// + /// 使用 PBKDF2 派生密钥(Base64 编码) + /// + public static string Pbkdf2Base64(string password, string salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + var passwordBytes = Encoding.UTF8.GetBytes(password); + var saltBytes = Encoding.UTF8.GetBytes(salt); + var key = Pbkdf2(passwordBytes, saltBytes, iterations, keySize, hashAlgorithm); + return Convert.ToBase64String(key); + } + + /// + /// 生成 PBKDF2 盐值 + /// + /// 盐值大小(字节) + /// 盐值 + public static byte[] GenerateSalt(int size = 16) + { + using var rng = RandomNumberGenerator.Create(); + var salt = new byte[size]; + rng.GetBytes(salt); + return salt; + } + + /// + /// 生成 PBKDF2 盐值(Base64 编码) + /// + public static string GenerateSaltBase64(int size = 16) + { + var salt = GenerateSalt(size); + return Convert.ToBase64String(salt); + } + + #endregion + + #region HKDF + + /// + /// 使用 HKDF 派生密钥 + /// + /// 输入密钥材料 + /// 盐值 + /// 上下文信息 + /// 输出密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Hkdf(byte[] ikm, byte[]? salt = null, byte[]? info = null, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + salt ??= Array.Empty(); + info ??= Array.Empty(); + + // Extract + var prk = HmacExtract(ikm, salt, hashAlgorithm); + + // Expand + return HkdfExpand(prk, info, keySize, hashAlgorithm); + } + + /// + /// HKDF Extract 步骤 + /// + private static byte[] HmacExtract(byte[] ikm, byte[] salt, HashAlgorithmName hashAlgorithm) + { + int hashSize = GetHashSize(hashAlgorithm); + if (salt.Length == 0) + salt = new byte[hashSize]; + + return ComputeHmac(ikm, salt, hashAlgorithm); + } + + /// + /// HKDF Expand 步骤 + /// + private static byte[] HkdfExpand(byte[] prk, byte[] info, int keySize, HashAlgorithmName hashAlgorithm) + { + int hashSize = GetHashSize(hashAlgorithm); + int n = (keySize + hashSize - 1) / hashSize; + + var result = new byte[keySize]; + var t = Array.Empty(); + int offset = 0; + + for (int i = 1; i <= n; i++) + { + var data = new byte[t.Length + info.Length + 1]; + Buffer.BlockCopy(t, 0, data, 0, t.Length); + Buffer.BlockCopy(info, 0, data, t.Length, info.Length); + data[data.Length - 1] = (byte)i; + + t = ComputeHmac(data, prk, hashAlgorithm); + int toCopy = Math.Min(hashSize, keySize - offset); + Buffer.BlockCopy(t, 0, result, offset, toCopy); + offset += toCopy; + } + + return result; + } + + /// + /// 计算 HMAC + /// + private static byte[] ComputeHmac(byte[] data, byte[] key, HashAlgorithmName hashAlgorithm) + { + using var hmac = CreateHmac(key, hashAlgorithm); + return hmac.ComputeHash(data); + } + + /// + /// 创建 HMAC 实例 + /// + private static HMAC CreateHmac(byte[] key, HashAlgorithmName hashAlgorithm) + { + if (hashAlgorithm == HashAlgorithmName.SHA256) + return new HMACSHA256(key); + if (hashAlgorithm == HashAlgorithmName.SHA384) + return new HMACSHA384(key); + if (hashAlgorithm == HashAlgorithmName.SHA512) + return new HMACSHA512(key); + if (hashAlgorithm == HashAlgorithmName.SHA1) + return new HMACSHA1(key); + + throw new NotSupportedException($"不支持的哈希算法: {hashAlgorithm.Name}"); + } + + /// + /// 获取哈希大小 + /// + private static int GetHashSize(HashAlgorithmName hashAlgorithm) + { + if (hashAlgorithm == HashAlgorithmName.SHA256) return 32; + if (hashAlgorithm == HashAlgorithmName.SHA384) return 48; + if (hashAlgorithm == HashAlgorithmName.SHA512) return 64; + if (hashAlgorithm == HashAlgorithmName.SHA1) return 20; + + throw new NotSupportedException($"不支持的哈希算法: {hashAlgorithm.Name}"); + } + + #endregion + + #region SP 800-108 Counter Mode KDF + + /// + /// 使用 NIST SP 800-108 Counter Mode 派生密钥 + /// + /// 密钥派生密钥 + /// 标签 + /// 上下文 + /// 输出密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Sp800_108_Counter(byte[] keyDerivationKey, byte[] label, byte[] context, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + int hashSize = GetHashSize(hashAlgorithm); + int n = (keySize + hashSize - 1) / hashSize; + + var result = new byte[keySize]; + int offset = 0; + + for (int i = 1; i <= n; i++) + { + // [i] || Label || 0x00 || Context || [L] + var data = new byte[4 + label.Length + 1 + context.Length + 4]; + int pos = 0; + + // Counter (4 bytes, big-endian) + data[pos++] = (byte)(i >> 24); + data[pos++] = (byte)(i >> 16); + data[pos++] = (byte)(i >> 8); + data[pos++] = (byte)i; + + // Label + Buffer.BlockCopy(label, 0, data, pos, label.Length); + pos += label.Length; + + // Separator + data[pos++] = 0x00; + + // Context + Buffer.BlockCopy(context, 0, data, pos, context.Length); + pos += context.Length; + + // Length in bits (4 bytes, big-endian) + int l = keySize * 8; + data[pos++] = (byte)(l >> 24); + data[pos++] = (byte)(l >> 16); + data[pos++] = (byte)(l >> 8); + data[pos++] = (byte)l; + + var hash = ComputeHmac(data, keyDerivationKey, hashAlgorithm); + int toCopy = Math.Min(hashSize, keySize - offset); + Buffer.BlockCopy(hash, 0, result, offset, toCopy); + offset += toCopy; + } + + return result; + } + + #endregion + + #region 静态工具方法 + + /// + /// 从密码生成加密密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 生成的密钥信息 + public static KeyDerivationResult DeriveKeyFromPassword(string password, byte[]? salt = null, int iterations = 100000) + { + salt ??= GenerateSalt(16); + var key = Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, 32); + var iv = Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, 16); + + return new KeyDerivationResult + { + Key = key, + IV = iv, + Salt = salt, + Iterations = iterations + }; + } + + #endregion + } + + /// + /// 密钥派生结果 + /// + public class KeyDerivationResult + { + /// + /// 派生的密钥 + /// + public byte[] Key { get; set; } = Array.Empty(); + + /// + /// 初始化向量 + /// + public byte[] IV { get; set; } = Array.Empty(); + + /// + /// 使用的盐值 + /// + public byte[] Salt { get; set; } = Array.Empty(); + + /// + /// 迭代次数 + /// + public int Iterations { get; set; } + + /// + /// 密钥(Base64 编码) + /// + public string KeyBase64 => Convert.ToBase64String(Key); + + /// + /// IV(Base64 编码) + /// + public string IVBase64 => Convert.ToBase64String(IV); + + /// + /// 盐值(Base64 编码) + /// + public string SaltBase64 => Convert.ToBase64String(Salt); + } +} diff --git a/EasyTool.Core/CodeCategory/SignatureUtil.cs b/EasyTool.Core/CodeCategory/SignatureUtil.cs new file mode 100644 index 0000000..29a3a09 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SignatureUtil.cs @@ -0,0 +1,614 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 数字签名工具类 + /// 提供 RSA、ECDSA、DSA 等签名和验证功能 + /// + public static class SignatureUtil + { + #region RSA 签名 + + /// + /// 使用 RSA 创建签名 + /// + /// 要签名的数据 + /// RSA 私钥(PKCS#8 或 XML 格式) + /// 哈希算法 + /// 签名填充模式 + /// 签名 + public static byte[] SignWithRsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm, RSASignaturePadding? padding = null) + { + padding ??= RSASignaturePadding.Pkcs1; + + using var rsa = CreateRsaFromKey(privateKey); + return rsa.SignData(data, hashAlgorithm, padding); + } + + /// + /// 使用 RSA 创建签名(PSS 填充) + /// + public static byte[] SignWithRsaPss(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return SignWithRsa(data, privateKey, hashAlgorithm, RSASignaturePadding.Pss); + } + + /// + /// 使用 RSA 创建签名(PKCS#1 填充) + /// + public static byte[] SignWithRsaPkcs1(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return SignWithRsa(data, privateKey, hashAlgorithm, RSASignaturePadding.Pkcs1); + } + + /// + /// 验证 RSA 签名 + /// + /// 原始数据 + /// 签名 + /// RSA 公钥 + /// 哈希算法 + /// 签名填充模式 + /// 签名是否有效 + public static bool VerifyRsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm, RSASignaturePadding? padding = null) + { + padding ??= RSASignaturePadding.Pkcs1; + + try + { + using var rsa = CreateRsaFromKey(publicKey); + return rsa.VerifyData(data, signature, hashAlgorithm, padding); + } + catch + { + return false; + } + } + + /// + /// 验证 RSA-PSS 签名 + /// + public static bool VerifyRsaPssSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return VerifyRsaSignature(data, signature, publicKey, hashAlgorithm, RSASignaturePadding.Pss); + } + + /// + /// 验证 RSA-PKCS1 签名 + /// + public static bool VerifyRsaPkcs1Signature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return VerifyRsaSignature(data, signature, publicKey, hashAlgorithm, RSASignaturePadding.Pkcs1); + } + + #endregion + + #region ECDSA 签名 + + /// + /// 使用 ECDSA 创建签名 + /// + /// 要签名的数据 + /// ECDSA 私钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithEcdsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var ecdsa = CreateEcdsaFromKey(privateKey); + return ecdsa.SignData(data, hashAlgorithm); + } + + /// + /// 使用 ECDSA 创建签名(DER 格式) + /// + public static byte[] SignWithEcdsaDer(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var ecdsa = CreateEcdsaFromKey(privateKey); + // 在 netstandard2.1 中使用默认签名格式 + return ecdsa.SignData(data, hashAlgorithm); + } + + /// + /// 验证 ECDSA 签名 + /// + /// 原始数据 + /// 签名 + /// ECDSA 公钥 + /// 哈希算法 + /// 签名是否有效 + public static bool VerifyEcdsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + try + { + using var ecdsa = CreateEcdsaFromKey(publicKey); + return ecdsa.VerifyData(data, signature, hashAlgorithm); + } + catch + { + return false; + } + } + + #endregion + + #region DSA 签名 + + /// + /// 使用 DSA 创建签名 + /// + /// 要签名的数据 + /// DSA 私钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithDsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var dsa = CreateDsaFromKey(privateKey); + return dsa.SignData(data, hashAlgorithm); + } + + /// + /// 验证 DSA 签名 + /// + public static bool VerifyDsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + try + { + using var dsa = CreateDsaFromKey(publicKey); + return dsa.VerifyData(data, signature, hashAlgorithm); + } + catch + { + return false; + } + } + + #endregion + + #region HMAC 签名 + + /// + /// 创建 HMAC 签名 + /// + /// 数据 + /// 密钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithHmac(byte[] data, byte[] key, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var hmac = CreateHmac(key, hashAlgorithm); + return hmac.ComputeHash(data); + } + + /// + /// 验证 HMAC 签名 + /// + public static bool VerifyHmacSignature(byte[] data, byte[] signature, byte[] key, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + var computed = SignWithHmac(data, key, hashAlgorithm); + return HmacUtil.ConstantTimeEquals(computed, signature); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成 RSA 密钥对 + /// + /// 密钥大小(位) + /// 密钥对 + public static KeyPair GenerateRsaKeyPair(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + return new KeyPair + { + PrivateKey = ExportRsaPrivateKeyPem(rsa), + PublicKey = ExportRsaPublicKeyPem(rsa), + PrivateKeyPkcs8 = ExportPkcs8PrivateKeyPem(rsa) + }; + } + + /// + /// 生成 ECDSA 密钥对 + /// + /// 椭圆曲线 + /// 密钥对 + public static KeyPair GenerateEcdsaKeyPair(ECCurve? curve = null) + { + using var ecdsa = curve.HasValue + ? ECDsa.Create(curve.Value) + : ECDsa.Create(ECCurve.NamedCurves.nistP256); + + return new KeyPair + { + PrivateKey = ExportEcPrivateKeyPem(ecdsa), + PublicKey = ExportSubjectPublicKeyInfoPem(ecdsa) + }; + } + + #endregion + + #region 辅助方法 + + private static RSA CreateRsaFromKey(string key) + { + var rsa = RSA.Create(); + + if (key.StartsWith("-----BEGIN")) + { + // PEM 格式 + ImportFromPem(rsa, key); + } + else if (key.TrimStart().StartsWith(" + /// 签名字符串 + /// + /// 文本 + /// 私钥 + /// 签名算法 + /// Base64 编码的签名 + public static string SignString(string text, string privateKey, SignatureAlgorithm algorithm = SignatureAlgorithm.RsaSha256) + { + var data = Encoding.UTF8.GetBytes(text); + byte[] signature; + + switch (algorithm) + { + case SignatureAlgorithm.RsaSha256: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA256); + break; + case SignatureAlgorithm.RsaSha384: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA384); + break; + case SignatureAlgorithm.RsaSha512: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA512); + break; + case SignatureAlgorithm.RsaPssSha256: + signature = SignWithRsaPss(data, privateKey, HashAlgorithmName.SHA256); + break; + case SignatureAlgorithm.EcdsaSha256: + signature = SignWithEcdsa(data, privateKey, HashAlgorithmName.SHA256); + break; + default: + throw new NotSupportedException($"不支持的签名算法: {algorithm}"); + } + + return Convert.ToBase64String(signature); + } + + /// + /// 验证字符串签名 + /// + public static bool VerifyStringSignature(string text, string signatureBase64, string publicKey, SignatureAlgorithm algorithm = SignatureAlgorithm.RsaSha256) + { + var data = Encoding.UTF8.GetBytes(text); + var signature = Convert.FromBase64String(signatureBase64); + + switch (algorithm) + { + case SignatureAlgorithm.RsaSha256: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA256); + case SignatureAlgorithm.RsaSha384: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA384); + case SignatureAlgorithm.RsaSha512: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA512); + case SignatureAlgorithm.RsaPssSha256: + return VerifyRsaPssSignature(data, signature, publicKey, HashAlgorithmName.SHA256); + case SignatureAlgorithm.EcdsaSha256: + return VerifyEcdsaSignature(data, signature, publicKey, HashAlgorithmName.SHA256); + default: + throw new NotSupportedException($"不支持的签名算法: {algorithm}"); + } + } + + #endregion + } + + /// + /// 密钥对 + /// + public class KeyPair + { + /// + /// 私钥(PEM 格式) + /// + public string PrivateKey { get; set; } = string.Empty; + + /// + /// 公钥(PEM 格式) + /// + public string PublicKey { get; set; } = string.Empty; + + /// + /// 私钥(PKCS#8 格式) + /// + public string? PrivateKeyPkcs8 { get; set; } + } + + /// + /// 签名算法 + /// + public enum SignatureAlgorithm + { + /// + /// RSA + SHA256 (PKCS#1) + /// + RsaSha256, + + /// + /// RSA + SHA384 (PKCS#1) + /// + RsaSha384, + + /// + /// RSA + SHA512 (PKCS#1) + /// + RsaSha512, + + /// + /// RSA + SHA256 (PSS) + /// + RsaPssSha256, + + /// + /// RSA + SHA384 (PSS) + /// + RsaPssSha384, + + /// + /// RSA + SHA512 (PSS) + /// + RsaPssSha512, + + /// + /// ECDSA + SHA256 + /// + EcdsaSha256, + + /// + /// ECDSA + SHA384 + /// + EcdsaSha384, + + /// + /// ECDSA + SHA512 + /// + EcdsaSha512 + } +} \ No newline at end of file diff --git a/EasyTool.Core/CodeCategory/TSIDUtil.cs b/EasyTool.Core/CodeCategory/TSIDUtil.cs deleted file mode 100644 index cd2a776..0000000 --- a/EasyTool.Core/CodeCategory/TSIDUtil.cs +++ /dev/null @@ -1,422 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Security.Cryptography; -using System.Text; -using System.Threading; - -namespace EasyTool.CodeCategory -{ - /// - /// TSID(Time-Sorted ID)工具类 - /// TSID 是一种时间排序的唯一标识符 - /// 支持多种格式:TSID-256(8字符)、TSID-512(13字符)、TSID-1024(18字符) - /// - public static class TSIDUtil - { - private static readonly DateTime Epoch = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc); - private static long _lastTimestamp = -1L; - private static int _sequence = 0; - private static readonly object _lock = new object(); - - // Base32 编码字符集(Crockford) - private const string Base32Chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; - - // 节点ID(自动生成) - private static readonly int _nodeId; - - static TSIDUtil() - { - // 自动生成节点ID(0-31) - byte[] nodeIdBytes = new byte[1]; - using var rng = RandomNumberGenerator.Create(); - rng.GetBytes(nodeIdBytes); - _nodeId = nodeIdBytes[0] & 0x1F; - } - - #region TSID-256(8字符,32位) - - /// - /// 生成 TSID-256(8字符) - /// - /// 8字符的 TSID-256 - public static string GenerateTsid256() - { - var bytes = GenerateTsid256Bytes(); - return EncodeBase32(bytes, 5); - } - - /// - /// 生成 TSID-256 字节数组 - /// - /// 4字节的 TSID-256 - public static byte[] GenerateTsid256Bytes() - { - long timestamp = GetCurrentTimestamp(); - int sequence; - - lock (_lock) - { - if (timestamp == _lastTimestamp) - { - _sequence = (_sequence + 1) & 0xFF; - if (_sequence == 0) - { - timestamp = WaitForNextTimestamp(_lastTimestamp); - } - } - else - { - _sequence = 0; - } - _lastTimestamp = timestamp; - sequence = _sequence; - } - - // 32位:24位时间戳 + 8位序列号 - uint value = ((uint)(timestamp & 0xFFFFFF) << 8) | (uint)sequence; - - return BitConverter.GetBytes(value); - } - - #endregion - - #region TSID-512(13字符,51位) - - /// - /// 生成 TSID-512(13字符) - /// - /// 13字符的 TSID-512 - public static string GenerateTsid512() - { - return GenerateTsid512(_nodeId); - } - - /// - /// 生成 TSID-512(指定节点ID) - /// - /// 节点ID(0-31) - /// 13字符的 TSID-512 - public static string GenerateTsid512(int nodeId) - { - var bytes = GenerateTsid512Bytes(nodeId); - return EncodeBase32(bytes, 8); - } - - /// - /// 生成 TSID-512 字节数组 - /// - /// 节点ID(0-31) - /// 8字节的 TSID-512 - public static byte[] GenerateTsid512Bytes(int nodeId) - { - if (nodeId < 0 || nodeId > 31) - throw new ArgumentException("Node ID must be between 0 and 31", nameof(nodeId)); - - long timestamp = GetCurrentTimestamp(); - int sequence; - - lock (_lock) - { - if (timestamp == _lastTimestamp) - { - _sequence = (_sequence + 1) & 0x7FFF; - if (_sequence == 0) - { - timestamp = WaitForNextTimestamp(_lastTimestamp); - } - } - else - { - _sequence = 0; - } - _lastTimestamp = timestamp; - sequence = _sequence; - } - - // 使用序列号:42位时间戳 + 5位节点ID + 16位序列号 - ulong value = ((ulong)(timestamp & 0x3FFFFFFFFFF) << 21) | - ((ulong)((uint)nodeId & 0x1F) << 16) | - (ulong)((uint)sequence & 0xFFFF); - - return BitConverter.GetBytes(value); - } - - #endregion - - #region TSID-1024(18字符,90位) - - /// - /// 生成 TSID-1024(18字符) - /// - /// 18字符的 TSID-1024 - public static string GenerateTsid1024() - { - return GenerateTsid1024(_nodeId); - } - - /// - /// 生成 TSID-1024(指定节点ID) - /// - /// 节点ID(0-31) - /// 18字符的 TSID-1024 - public static string GenerateTsid1024(int nodeId) - { - var bytes = GenerateTsid1024Bytes(nodeId); - return EncodeBase32(bytes, 12); - } - - /// - /// 生成 TSID-1024 字节数组 - /// - /// 节点ID(0-31) - /// 12字节的 TSID-1024 - public static byte[] GenerateTsid1024Bytes(int nodeId) - { - if (nodeId < 0 || nodeId > 31) - throw new ArgumentException("Node ID must be between 0 and 31", nameof(nodeId)); - - long timestamp = GetCurrentTimestamp(); - int sequence; - byte[] random; - - lock (_lock) - { - if (timestamp == _lastTimestamp) - { - _sequence = (_sequence + 1) & 0x3FFFFF; - if (_sequence == 0) - { - timestamp = WaitForNextTimestamp(_lastTimestamp); - } - } - else - { - _sequence = 0; - } - _lastTimestamp = timestamp; - sequence = _sequence; - } - - // 生成随机部分 - random = new byte[4]; - using var rng = RandomNumberGenerator.Create(); - rng.GetBytes(random); - - var result = new byte[12]; - - // 时间戳(48位,6字节) - result[0] = (byte)(timestamp >> 40); - result[1] = (byte)(timestamp >> 32); - result[2] = (byte)(timestamp >> 24); - result[3] = (byte)(timestamp >> 16); - result[4] = (byte)(timestamp >> 8); - result[5] = (byte)timestamp; - - // 节点ID + 随机(32位,4字节) - result[6] = (byte)(nodeId << 3 | (random[0] >> 5)); - result[7] = (byte)((random[0] << 3) | (random[1] >> 5)); - result[8] = (byte)((random[1] << 3) | (random[2] >> 5)); - result[9] = (byte)((random[2] << 3) | (random[3] >> 5)); - result[10] = (byte)(random[3] << 3); - - // 序列号(16位,2字节) - result[10] |= (byte)(sequence >> 13); - result[11] = (byte)(sequence >> 5); - - return result; - } - - #endregion - - #region 通用方法 - - /// - /// 生成默认 TSID(TSID-512) - /// - /// 13字符的 TSID - public static string Generate() - { - return GenerateTsid512(); - } - - /// - /// 从 TSID 提取时间戳 - /// - /// TSID 字符串 - /// UTC 时间 - public static DateTimeOffset ExtractTimestamp(string tsid) - { - byte[] bytes = DecodeBase32(tsid); - - // 提取时间戳(前42位) - long timestamp = 0; - for (int i = 0; i < Math.Min(6, bytes.Length); i++) - { - timestamp = (timestamp << 8) | bytes[i]; - } - - // 根据长度调整 - if (tsid.Length <= 8) - { - timestamp = (timestamp >> 8) & 0xFFFFFF; - } - - return Epoch.AddMilliseconds(timestamp); - } - - /// - /// 验证 TSID 是否有效 - /// - /// TSID 字符串 - /// 是否有效 - public static bool IsValid(string tsid) - { - if (string.IsNullOrEmpty(tsid)) - return false; - - int len = tsid.Length; - if (len != 8 && len != 13 && len != 18) - return false; - - foreach (char c in tsid) - { - if (!Base32Chars.Contains(c)) - return false; - } - - return true; - } - - /// - /// 尝试解析 TSID - /// - /// TSID 字符串 - /// 输出的字节数组 - /// 是否解析成功 - public static bool TryParse(string tsid, out byte[] bytes) - { - bytes = null; - if (!IsValid(tsid)) - return false; - - try - { - bytes = DecodeBase32(tsid); - return true; - } - catch - { - return false; - } - } - - /// - /// 批量生成 TSID - /// - /// 数量 - /// TSID 数组 - public static string[] GenerateBatch(int count) - { - if (count <= 0) - throw new ArgumentException("Count must be greater than 0", nameof(count)); - - var result = new string[count]; - for (int i = 0; i < count; i++) - { - result[i] = Generate(); - } - return result; - } - - /// - /// 比较 TSID 的时间顺序 - /// - /// 第一个 TSID - /// 第二个 TSID - /// -1: tsid1早于tsid2, 0: 相同, 1: tsid1晚于tsid2 - public static int Compare(string tsid1, string tsid2) - { - return string.Compare(tsid1, tsid2, StringComparison.Ordinal); - } - - /// - /// 获取或设置节点ID - /// - public static int NodeId => _nodeId; - - #endregion - - #region 私有方法 - - private static long GetCurrentTimestamp() - { - return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; - } - - private static long WaitForNextTimestamp(long lastTimestamp) - { - long timestamp = GetCurrentTimestamp(); - while (timestamp <= lastTimestamp) - { - Thread.SpinWait(10); - timestamp = GetCurrentTimestamp(); - } - return timestamp; - } - - private static string EncodeBase32(byte[] bytes, int length) - { - var result = new StringBuilder(length); - - int bits = 0; - int value = 0; - - foreach (byte b in bytes) - { - value = (value << 8) | b; - bits += 8; - - while (bits >= 5) - { - result.Append(Base32Chars[(value >> (bits - 5)) & 0x1F]); - bits -= 5; - } - } - - if (bits > 0) - { - result.Append(Base32Chars[(value << (5 - bits)) & 0x1F]); - } - - return result.ToString().PadLeft(length, '0'); - } - - private static byte[] DecodeBase32(string encoded) - { - var result = new List(); - - int bits = 0; - int value = 0; - - foreach (char c in encoded) - { - int index = Base32Chars.IndexOf(char.ToUpperInvariant(c)); - if (index < 0) - throw new ArgumentException($"Invalid character: {c}"); - - value = (value << 5) | index; - bits += 5; - - while (bits >= 8) - { - result.Add((byte)((value >> (bits - 8)) & 0xFF)); - bits -= 8; - } - } - - return result.ToArray(); - } - - #endregion - } -} diff --git a/EasyTool.Core/CodeCategory/UlidUtil.cs b/EasyTool.Core/CodeCategory/UlidUtil.cs deleted file mode 100644 index 62f28aa..0000000 --- a/EasyTool.Core/CodeCategory/UlidUtil.cs +++ /dev/null @@ -1,362 +0,0 @@ -using System; -using System.Security.Cryptography; -using System.Text; - -namespace EasyTool.CodeCategory -{ - /// - /// ULID(Universally Unique Lexicographically Sortable Identifier)工具类 - /// ULID 是一个48位时间戳 + 80位随机数的128位唯一标识符 - /// 特点:时间排序、128位兼容UUID、URL安全、大小写不敏感 - /// - public static class UlidUtil - { - // ULID 时间戳起始时间(1970-01-01) - private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - - // Base32 编码字符集(Crockford's Base32) - private const string EncodingChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; - - // Base32 解码字符映射 - private static readonly int[] DecodingMap = BuildDecodingMap(); - - /// - /// 生成新的 ULID - /// - /// 26个字符的 ULID 字符串 - public static string Generate() - { - return Generate(DateTimeOffset.UtcNow); - } - - /// - /// 基于指定时间生成 ULID - /// - /// 时间戳 - /// 26个字符的 ULID 字符串 - public static string Generate(DateTimeOffset timestamp) - { - var bytes = new byte[16]; - WriteTimestamp(bytes, timestamp.ToUniversalTime().ToUnixTimeMilliseconds()); - WriteRandomness(bytes, 6); - return Encode(bytes); - } - - /// - /// 生成 ULID 字节数组 - /// - /// 16字节的 ULID - public static byte[] GenerateBytes() - { - return GenerateBytes(DateTimeOffset.UtcNow); - } - - /// - /// 基于指定时间生成 ULID 字节数组 - /// - /// 时间戳 - /// 16字节的 ULID - public static byte[] GenerateBytes(DateTimeOffset timestamp) - { - var bytes = new byte[16]; - WriteTimestamp(bytes, timestamp.ToUniversalTime().ToUnixTimeMilliseconds()); - WriteRandomness(bytes, 6); - return bytes; - } - - /// - /// 生成 GUID 格式的 ULID - /// - /// GUID - public static Guid GenerateGuid() - { - var bytes = GenerateBytes(); - return new Guid(bytes); - } - - /// - /// 将 ULID 字符串转换为字节数组 - /// - /// ULID 字符串 - /// 16字节的数组 - public static byte[] Decode(string ulid) - { - if (string.IsNullOrEmpty(ulid)) - throw new ArgumentException("ULID cannot be null or empty", nameof(ulid)); - if (ulid.Length != 26) - throw new ArgumentException("ULID must be exactly 26 characters", nameof(ulid)); - - return DecodeImpl(ulid); - } - - /// - /// 将字节数组编码为 ULID 字符串 - /// - /// 16字节的数组 - /// 26个字符的 ULID 字符串 - public static string Encode(byte[] bytes) - { - if (bytes == null || bytes.Length != 16) - throw new ArgumentException("Bytes must be exactly 16 bytes", nameof(bytes)); - - return EncodeImpl(bytes); - } - - /// - /// 从 ULID 字符串提取时间戳 - /// - /// ULID 字符串 - /// UTC 时间 - public static DateTimeOffset ExtractTimestamp(string ulid) - { - var bytes = Decode(ulid); - return ExtractTimestamp(bytes); - } - - /// < /// 从 ULID 字节数组提取时间戳 - /// - /// 16字节的 ULID - /// UTC 时间 - public static DateTimeOffset ExtractTimestamp(byte[] bytes) - { - if (bytes == null || bytes.Length != 16) - throw new ArgumentException("Bytes must be exactly 16 bytes", nameof(bytes)); - - long timestamp = ((long)bytes[0] << 40) | - ((long)bytes[1] << 32) | - ((long)bytes[2] << 24) | - ((long)bytes[3] << 16) | - ((long)bytes[4] << 8) | - bytes[5]; - - return DateTimeOffset.FromUnixTimeMilliseconds(timestamp); - } - - /// - /// 验证 ULID 字符串是否有效 - /// - /// ULID 字符串 - /// 是否有效 - public static bool IsValid(string ulid) - { - if (string.IsNullOrEmpty(ulid) || ulid.Length != 26) - return false; - - foreach (char c in ulid) - { - if (c < '0' || c > 'Z') - return false; - if (c > '9' && c < 'A') - return false; - if (c == 'I' || c == 'L' || c == 'O' || c == 'U') - return false; - } - - return true; - } - - /// - /// 尝试解析 ULID 字符串 - /// - /// ULID 字符串 - /// 输出的字节数组 - /// 是否解析成功 - public static bool TryParse(string ulid, out byte[] bytes) - { - bytes = null; - if (!IsValid(ulid)) - return false; - - try - { - bytes = DecodeImpl(ulid); - return true; - } - catch - { - return false; - } - } - - /// - /// 比较两个 ULID 的时间顺序 - /// - /// 第一个 ULID - /// 第二个 ULID - /// 比较结果:-1表示ulid1早于ulid2,0表示相同,1表示ulid1晚于ulid2 - public static int Compare(string ulid1, string ulid2) - { - return string.Compare(ulid1, ulid2, StringComparison.Ordinal); - } - - /// - /// 生成指定时间范围内的随机 ULID - /// - /// 最小时间 - /// 最大时间 - /// ULID 字符串 - public static string GenerateInRange(DateTimeOffset minTimestamp, DateTimeOffset maxTimestamp) - { - if (minTimestamp > maxTimestamp) - throw new ArgumentException("Min timestamp must be less than or equal to max timestamp"); - -#if NET6_0_OR_GREATER - var random = Random.Shared; -#else - var random = new Random(Guid.NewGuid().GetHashCode()); -#endif - long minMs = minTimestamp.ToUniversalTime().ToUnixTimeMilliseconds(); - long maxMs = maxTimestamp.ToUniversalTime().ToUnixTimeMilliseconds(); - long randomMs = minMs + (long)(random.NextDouble() * (maxMs - minMs)); - - var bytes = new byte[16]; - WriteTimestamp(bytes, randomMs); - WriteRandomness(bytes, 6); - return Encode(bytes); - } - - /// - /// 批量生成 ULID - /// - /// 生成数量 - /// ULID 数组 - public static string[] GenerateBatch(int count) - { - if (count <= 0) - throw new ArgumentException("Count must be greater than 0", nameof(count)); - - var result = new string[count]; - for (int i = 0; i < count; i++) - { - result[i] = Generate(); - } - return result; - } - - /// - /// 将 ULID 转换为小写 - /// - /// ULID 字符串 - /// 小写的 ULID - public static string ToLower(string ulid) - { - return ulid?.ToLowerInvariant(); - } - - /// - /// 将 ULID 转换为大写 - /// - /// ULID 字符串 - /// 大写的 ULID - public static string ToUpper(string ulid) - { - return ulid?.ToUpperInvariant(); - } - - #region 私有方法 - - private static int[] BuildDecodingMap() - { - var map = new int[256]; - for (int i = 0; i < 256; i++) - { - map[i] = -1; - } - for (int i = 0; i < EncodingChars.Length; i++) - { - map[(byte)EncodingChars[i]] = i; - map[(byte)char.ToLowerInvariant(EncodingChars[i])] = i; - } - // 处理可能出现的歧义字符 - map[(byte)'i'] = 1; map[(byte)'I'] = 1; - map[(byte)'l'] = 1; map[(byte)'L'] = 1; - map[(byte)'o'] = 0; map[(byte)'O'] = 0; - map[(byte)'u'] = 32; map[(byte)'U'] = 32; - return map; - } - - private static void WriteTimestamp(byte[] bytes, long timestamp) - { - bytes[0] = (byte)(timestamp >> 40); - bytes[1] = (byte)(timestamp >> 32); - bytes[2] = (byte)(timestamp >> 24); - bytes[3] = (byte)(timestamp >> 16); - bytes[4] = (byte)(timestamp >> 8); - bytes[5] = (byte)timestamp; - } - - private static void WriteRandomness(byte[] bytes, int offset) - { - using (var rng = RandomNumberGenerator.Create()) - { - rng.GetBytes(bytes, offset, 10); - } - } - - private static string EncodeImpl(byte[] bytes) - { - var result = new char[26]; - - // 编码时间戳(6字节 -> 10字符) - result[0] = EncodingChars[(bytes[0] >> 5) & 0x07]; - result[1] = EncodingChars[bytes[0] & 0x1F]; - result[2] = EncodingChars[(bytes[1] >> 3) & 0x1F]; - result[3] = EncodingChars[((bytes[1] << 2) | (bytes[2] >> 6)) & 0x1F]; - result[4] = EncodingChars[(bytes[2] >> 1) & 0x1F]; - result[5] = EncodingChars[((bytes[2] << 4) | (bytes[3] >> 4)) & 0x1F]; - result[6] = EncodingChars[((bytes[3] << 1) | (bytes[4] >> 7)) & 0x1F]; - result[7] = EncodingChars[(bytes[4] >> 2) & 0x1F]; - result[8] = EncodingChars[((bytes[4] << 3) | (bytes[5] >> 5)) & 0x1F]; - result[9] = EncodingChars[bytes[5] & 0x1F]; - - // 编码随机数(10字节 -> 16字符) - result[10] = EncodingChars[(bytes[6] >> 3) & 0x1F]; - result[11] = EncodingChars[((bytes[6] << 2) | (bytes[7] >> 6)) & 0x1F]; - result[12] = EncodingChars[(bytes[7] >> 1) & 0x1F]; - result[13] = EncodingChars[((bytes[7] << 4) | (bytes[8] >> 4)) & 0x1F]; - result[14] = EncodingChars[((bytes[8] << 1) | (bytes[9] >> 7)) & 0x1F]; - result[15] = EncodingChars[(bytes[9] >> 2) & 0x1F]; - result[16] = EncodingChars[((bytes[9] << 3) | (bytes[10] >> 5)) & 0x1F]; - result[17] = EncodingChars[bytes[10] & 0x1F]; - result[18] = EncodingChars[(bytes[11] >> 3) & 0x1F]; - result[19] = EncodingChars[((bytes[11] << 2) | (bytes[12] >> 6)) & 0x1F]; - result[20] = EncodingChars[(bytes[12] >> 1) & 0x1F]; - result[21] = EncodingChars[((bytes[12] << 4) | (bytes[13] >> 4)) & 0x1F]; - result[22] = EncodingChars[((bytes[13] << 1) | (bytes[14] >> 7)) & 0x1F]; - result[23] = EncodingChars[(bytes[14] >> 2) & 0x1F]; - result[24] = EncodingChars[((bytes[14] << 3) | (bytes[15] >> 5)) & 0x1F]; - result[25] = EncodingChars[bytes[15] & 0x1F]; - - return new string(result); - } - - private static byte[] DecodeImpl(string ulid) - { - var bytes = new byte[16]; - - // 解码时间戳(10字符 -> 6字节) - bytes[0] = (byte)((DecodingMap[ulid[0]] << 5) | DecodingMap[ulid[1]]); - bytes[1] = (byte)((DecodingMap[ulid[2]] << 3) | (DecodingMap[ulid[3]] >> 2)); - bytes[2] = (byte)((DecodingMap[ulid[3]] << 6) | (DecodingMap[ulid[4]] << 1) | (DecodingMap[ulid[5]] >> 4)); - bytes[3] = (byte)((DecodingMap[ulid[5]] << 4) | (DecodingMap[ulid[6]] >> 1)); - bytes[4] = (byte)((DecodingMap[ulid[6]] << 7) | (DecodingMap[ulid[7]] << 2) | (DecodingMap[ulid[8]] >> 3)); - bytes[5] = (byte)((DecodingMap[ulid[8]] << 5) | DecodingMap[ulid[9]]); - - // 解码随机数(16字符 -> 10字节) - bytes[6] = (byte)((DecodingMap[ulid[10]] << 3) | (DecodingMap[ulid[11]] >> 2)); - bytes[7] = (byte)((DecodingMap[ulid[11]] << 6) | (DecodingMap[ulid[12]] << 1) | (DecodingMap[ulid[13]] >> 4)); - bytes[8] = (byte)((DecodingMap[ulid[13]] << 4) | (DecodingMap[ulid[14]] >> 1)); - bytes[9] = (byte)((DecodingMap[ulid[14]] << 7) | (DecodingMap[ulid[15]] << 2) | (DecodingMap[ulid[16]] >> 3)); - bytes[10] = (byte)((DecodingMap[ulid[16]] << 5) | DecodingMap[ulid[17]]); - bytes[11] = (byte)((DecodingMap[ulid[18]] << 3) | (DecodingMap[ulid[19]] >> 2)); - bytes[12] = (byte)((DecodingMap[ulid[19]] << 6) | (DecodingMap[ulid[20]] << 1) | (DecodingMap[ulid[21]] >> 4)); - bytes[13] = (byte)((DecodingMap[ulid[21]] << 4) | (DecodingMap[ulid[22]] >> 1)); - bytes[14] = (byte)((DecodingMap[ulid[22]] << 7) | (DecodingMap[ulid[23]] << 2) | (DecodingMap[ulid[24]] >> 3)); - bytes[15] = (byte)((DecodingMap[ulid[24]] << 5) | DecodingMap[ulid[25]]); - - return bytes; - } - - #endregion - } -} diff --git a/EasyTool.Core/CollectionsCategory/BatchUtil.cs b/EasyTool.Core/CollectionsCategory/BatchUtil.cs new file mode 100644 index 0000000..9057cea --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BatchUtil.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 批量处理工具类 + /// + public static class BatchUtil + { + /// + /// 将集合分批 + /// + /// 元素类型 + /// 源集合 + /// 批次大小 + /// 分批后的集合 + public static IEnumerable> Batch(IEnumerable source, int batchSize) + { + if (batchSize <= 0) + throw new ArgumentOutOfRangeException(nameof(batchSize), "批次大小必须大于0"); + + var batch = new List(batchSize); + foreach (var item in source) + { + batch.Add(item); + if (batch.Count >= batchSize) + { + yield return batch; + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + yield return batch; + } + } + + /// + /// 批量处理集合 + /// + /// 元素类型 + /// 源集合 + /// 批次大小 + /// 处理动作 + public static void ProcessBatch(IEnumerable source, int batchSize, Action> action) + { + foreach (var batch in Batch(source, batchSize)) + { + action(batch); + } + } + + /// + /// 异步批量处理集合 + /// + public static async System.Threading.Tasks.Task ProcessBatchAsync( + IEnumerable source, + int batchSize, + Func, System.Threading.Tasks.Task> action) + { + foreach (var batch in Batch(source, batchSize)) + { + await action(batch); + } + } + + /// + /// 批量处理并返回结果 + /// + public static IEnumerable ProcessBatch( + IEnumerable source, + int batchSize, + Func, IEnumerable> action) + { + foreach (var batch in Batch(source, batchSize)) + { + foreach (var result in action(batch)) + { + yield return result; + } + } + } + + /// + /// 异步批量处理并返回结果 + /// + public static async IAsyncEnumerable ProcessBatchAsync( + IEnumerable source, + int batchSize, + Func, System.Threading.Tasks.Task>> action) + { + foreach (var batch in Batch(source, batchSize)) + { + var results = await action(batch); + foreach (var result in results) + { + yield return result; + } + } + } + + /// + /// 并行批量处理 + /// + public static void ProcessBatchParallel( + IEnumerable source, + int batchSize, + Action> action, + int maxDegreeOfParallelism = 4) + { + var options = new System.Threading.Tasks.ParallelOptions + { + MaxDegreeOfParallelism = maxDegreeOfParallelism + }; + + System.Threading.Tasks.Parallel.ForEach(Batch(source, batchSize), options, action); + } + + /// + /// 并行批量处理并返回结果 + /// + public static List ProcessBatchParallel( + IEnumerable source, + int batchSize, + Func, IEnumerable> action, + int maxDegreeOfParallelism = 4) + { + var results = new System.Collections.Concurrent.ConcurrentBag(); + var options = new System.Threading.Tasks.ParallelOptions + { + MaxDegreeOfParallelism = maxDegreeOfParallelism + }; + + System.Threading.Tasks.Parallel.ForEach(Batch(source, batchSize), options, batch => + { + foreach (var result in action(batch)) + { + results.Add(result); + } + }); + + return new List(results); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/CacheUtil.cs b/EasyTool.Core/CollectionsCategory/CacheUtil.cs index 8c764f7..aabbde8 100644 --- a/EasyTool.Core/CollectionsCategory/CacheUtil.cs +++ b/EasyTool.Core/CollectionsCategory/CacheUtil.cs @@ -1,888 +1,325 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; +using System.Threading.Tasks; namespace EasyTool.CollectionsCategory { /// - /// LFU(最不经常使用)缓存工具类 + /// 缓存项 /// - public static class LFUCacheUtil + /// 缓存值类型 + internal class CacheItem { - /// - /// 创建 LFU 缓存 - /// - public static LFUCache Create(int capacity) - { - return new LFUCache(capacity); - } + public T Value { get; set; } = default!; + public DateTime CreateTime { get; set; } + public DateTime? ExpireTime { get; set; } + public TimeSpan? SlidingExpiration { get; set; } + public DateTime LastAccess { get; set; } } /// - /// LFU 缓存实现 + /// 内存缓存工具类 + /// 提供线程安全的内存缓存功能,支持过期时间和滑动过期 /// - public class LFUCache + public static class CacheUtil { - private readonly int _capacity; - private readonly Dictionary _cache; - private readonly Dictionary> _frequencyLists; - private int _minFrequency; + private static readonly ConcurrentDictionary _cache = new(); + private static readonly Timer _cleanupTimer; + private static readonly object _lock = new(); - private class CacheItem + static CacheUtil() { - public TValue Value { get; set; } - public int Frequency { get; set; } - public LinkedListNode Node { get; set; } + // 每分钟清理一次过期缓存 + _cleanupTimer = new Timer(_ => CleanupExpired(), null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); } /// - /// 当前元素数量 - /// - public int Count => _cache.Count; - - /// - /// 缓存容量 + /// 设置缓存 /// - public int Capacity => _capacity; - - /// - /// 创建 LFU 缓存 - /// - public LFUCache(int capacity) + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 绝对过期时间 + public static void Set(string key, T value, DateTime? absoluteExpiration = null) { - if (capacity <= 0) - throw new ArgumentOutOfRangeException(nameof(capacity)); - - _capacity = capacity; - _cache = new Dictionary(); - _frequencyLists = new Dictionary>(); - _minFrequency = 0; - } - - /// - /// 获取值 - /// - public bool TryGet(TKey key, out TValue value) - { - if (_cache.TryGetValue(key, out var item)) + var item = new CacheItem { - UpdateFrequency(item); - value = item.Value; - return true; - } - - value = default; - return false; + Value = value, + CreateTime = DateTime.UtcNow, + ExpireTime = absoluteExpiration, + LastAccess = DateTime.UtcNow + }; + _cache[key] = item; } /// - /// 获取或添加值 + /// 设置缓存(相对过期) /// - public TValue GetOrAdd(TKey key, Func valueFactory) + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 过期时间间隔 + public static void Set(string key, T value, TimeSpan expiration) { - if (TryGet(key, out var value)) - return value; - - value = valueFactory(key); - Add(key, value); - return value; + Set(key, value, DateTime.UtcNow.Add(expiration)); } /// - /// 添加或更新值 + /// 设置缓存(滑动过期) /// - public void Add(TKey key, TValue value) + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 滑动过期时间 + /// 最大过期时间 + public static void SetSliding(string key, T value, TimeSpan slidingExpiration, DateTime? absoluteExpiration = null) { - if (_cache.TryGetValue(key, out var item)) + var item = new CacheItem { - item.Value = value; - UpdateFrequency(item); - return; - } - - if (_cache.Count >= _capacity) - { - Evict(); - } - - var newNode = new CacheItem { Value = value, Frequency = 1 }; - if (!_frequencyLists.ContainsKey(1)) - { - _frequencyLists[1] = new LinkedList(); - } - newNode.Node = _frequencyLists[1].AddLast(key); - _cache[key] = newNode; - _minFrequency = 1; - } - - /// - /// 移除指定键 - /// - public bool Remove(TKey key) - { - if (!_cache.TryGetValue(key, out var item)) - return false; - - _frequencyLists[item.Frequency].Remove(item.Node); - _cache.Remove(key); - return true; - } - - /// - /// 清空缓存 - /// - public void Clear() - { - _cache.Clear(); - _frequencyLists.Clear(); - _minFrequency = 0; + Value = value, + CreateTime = DateTime.UtcNow, + SlidingExpiration = slidingExpiration, + ExpireTime = absoluteExpiration, + LastAccess = DateTime.UtcNow + }; + _cache[key] = item; } /// - /// 是否包含键 + /// 获取缓存 /// - public bool ContainsKey(TKey key) + /// 值类型 + /// 缓存键 + /// 缓存值,如果不存在或已过期则返回默认值 + public static T? Get(string key) { - return _cache.ContainsKey(key); - } + if (!_cache.TryGetValue(key, out var obj)) + return default; - private void UpdateFrequency(CacheItem item) - { - int oldFreq = item.Frequency; - int newFreq = oldFreq + 1; + var item = (CacheItem)obj; - _frequencyLists[oldFreq].Remove(item.Node); - if (_frequencyLists[oldFreq].Count == 0) + if (IsExpired(item)) { - _frequencyLists.Remove(oldFreq); - if (_minFrequency == oldFreq) - { - _minFrequency = newFreq; - } + _cache.TryRemove(key, out _); + return default; } - item.Frequency = newFreq; - if (!_frequencyLists.ContainsKey(newFreq)) + // 更新滑动过期 + if (item.SlidingExpiration.HasValue) { - _frequencyLists[newFreq] = new LinkedList(); + item.LastAccess = DateTime.UtcNow; } - item.Node = _frequencyLists[newFreq].AddLast(_cache.First(x => x.Value == item).Key); - } - private void Evict() - { - if (_minFrequency == 0 || !_frequencyLists.ContainsKey(_minFrequency)) - return; - - var list = _frequencyLists[_minFrequency]; - var keyToRemove = list.First.Value; - list.RemoveFirst(); - _cache.Remove(keyToRemove); - - if (list.Count == 0) - { - _frequencyLists.Remove(_minFrequency); - } + return item.Value; } - } - /// - /// FIFO(先进先出)缓存工具类 - /// - public static class FIFOCacheUtil - { /// - /// 创建 FIFO 缓存 + /// 获取或添加缓存 /// - public static FIFOCache Create(int capacity) + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 过期时间 + /// 缓存值 + public static T GetOrAdd(string key, Func factory, TimeSpan? expiration = null) { - return new FIFOCache(capacity); - } - } - - /// - /// FIFO 缓存实现 - /// - public class FIFOCache - { - private readonly int _capacity; - private readonly Dictionary _cache; - private readonly Queue _queue; - - /// - /// 当前元素数量 - /// - public int Count => _cache.Count; - - /// - /// 缓存容量 - /// - public int Capacity => _capacity; - - /// - /// 创建 FIFO 缓存 - /// - public FIFOCache(int capacity) - { - if (capacity <= 0) - throw new ArgumentOutOfRangeException(nameof(capacity)); - - _capacity = capacity; - _cache = new Dictionary(); - _queue = new Queue(); - } - - /// - /// 获取值 - /// - public bool TryGet(TKey key, out TValue value) - { - return _cache.TryGetValue(key, out value); - } - - /// - /// 获取或添加值 - /// - public TValue GetOrAdd(TKey key, Func valueFactory) - { - if (_cache.TryGetValue(key, out var value)) - return value; - - value = valueFactory(key); - Add(key, value); - return value; - } - - /// - /// 添加或更新值 - /// - public void Add(TKey key, TValue value) - { - if (_cache.ContainsKey(key)) + var value = Get(key); + if (value != null || typeof(T).IsValueType) { - _cache[key] = value; - return; + if (value != null) + return value; } - if (_cache.Count >= _capacity) + lock (_lock) { - var oldestKey = _queue.Dequeue(); - _cache.Remove(oldestKey); - } + value = Get(key); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; - _cache[key] = value; - _queue.Enqueue(key); - } + value = factory(); + if (expiration.HasValue) + Set(key, value, expiration.Value); + else + Set(key, value); - /// - /// 移除指定键 - /// - public bool Remove(TKey key) - { - if (!_cache.Remove(key)) - return false; - - // 需要重建队列以移除中间元素 - var newQueue = new Queue(); - foreach (var k in _queue) - { - if (!EqualityComparer.Default.Equals(k, key)) - { - newQueue.Enqueue(k); - } + return value; } - return true; - } - - /// - /// 清空缓存 - /// - public void Clear() - { - _cache.Clear(); - _queue.Clear(); - } - - /// - /// 是否包含键 - /// - public bool ContainsKey(TKey key) - { - return _cache.ContainsKey(key); - } - } - - /// - /// 定时缓存工具类 - /// - public static class TimedCacheUtil - { - /// - /// 创建定时缓存 - /// - public static TimedCache Create(TimeSpan expiration) - { - return new TimedCache(expiration); } /// - /// 创建滑动过期缓存 - /// - public static TimedCache CreateSliding(TimeSpan expiration) - { - return new TimedCache(expiration, true); - } - } - - /// - /// 定时缓存实现 - /// - public class TimedCache - { - private readonly TimeSpan _expiration; - private readonly bool _slidingExpiration; - private readonly Dictionary _cache; - - private class CacheItem - { - public TValue Value { get; set; } - public DateTime ExpirationTime { get; set; } - } - - /// - /// 当前元素数量 - /// - public int Count => _cache.Count; - - /// - /// 过期时间 - /// - public TimeSpan Expiration => _expiration; - - /// - /// 创建定时缓存 + /// 异步获取或添加缓存 /// - public TimedCache(TimeSpan expiration, bool slidingExpiration = false) + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 过期时间 + /// 缓存值 + public static async Task GetOrAddAsync(string key, Func> factory, TimeSpan? expiration = null) { - if (expiration <= TimeSpan.Zero) - throw new ArgumentOutOfRangeException(nameof(expiration)); - - _expiration = expiration; - _slidingExpiration = slidingExpiration; - _cache = new Dictionary(); - } - - /// - /// 获取值 - /// - public bool TryGet(TKey key, out TValue value) - { - CleanupExpired(); + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } - if (_cache.TryGetValue(key, out var item)) + lock (_lock) { - if (DateTime.UtcNow < item.ExpirationTime) - { - if (_slidingExpiration) - { - item.ExpirationTime = DateTime.UtcNow.Add(_expiration); - } - value = item.Value; - return true; - } - _cache.Remove(key); + value = Get(key); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; } - value = default; - return false; - } + value = await factory(); - /// - /// 获取或添加值 - /// - public TValue GetOrAdd(TKey key, Func valueFactory) - { - if (TryGet(key, out var value)) - return value; + if (expiration.HasValue) + Set(key, value, expiration.Value); + else + Set(key, value); - value = valueFactory(key); - Add(key, value); return value; } /// - /// 添加或更新值 + /// 检查缓存是否存在 /// - public void Add(TKey key, TValue value) + /// 缓存键 + /// 是否存在 + public static bool Contains(string key) { - _cache[key] = new CacheItem - { - Value = value, - ExpirationTime = DateTime.UtcNow.Add(_expiration) - }; - } - - /// - /// 添加带自定义过期时间的值 - /// - public void Add(TKey key, TValue value, TimeSpan customExpiration) - { - _cache[key] = new CacheItem - { - Value = value, - ExpirationTime = DateTime.UtcNow.Add(customExpiration) - }; - } - - /// - /// 移除指定键 - /// - public bool Remove(TKey key) - { - return _cache.Remove(key); - } - - /// - /// 清空缓存 - /// - public void Clear() - { - _cache.Clear(); - } + if (!_cache.TryGetValue(key, out var obj)) + return false; - /// - /// 是否包含键(未过期) - /// - public bool ContainsKey(TKey key) - { - CleanupExpired(); - return _cache.ContainsKey(key); - } + var itemType = obj.GetType(); + var isExpired = (bool)itemType.GetMethod("IsExpired")!.Invoke(null, new[] { obj })!; - /// - /// 清理过期项 - /// - public void CleanupExpired() - { - var now = DateTime.UtcNow; - var expired = _cache.Where(x => x.Value.ExpirationTime <= now).Select(x => x.Key).ToList(); - foreach (var key in expired) + if (isExpired) { - _cache.Remove(key); + _cache.TryRemove(key, out _); + return false; } - } - } - /// - /// 限流器工具类 - /// - public static class RateLimiterUtil - { - /// - /// 创建令牌桶限流器 - /// - public static TokenBucketRateLimiter CreateTokenBucket(int capacity, int refillRate, TimeSpan refillPeriod) - { - return new TokenBucketRateLimiter(capacity, refillRate, refillPeriod); + return true; } /// - /// 创建滑动窗口限流器 + /// 移除缓存 /// - public static SlidingWindowRateLimiter CreateSlidingWindow(int limit, TimeSpan window) + /// 缓存键 + /// 是否移除成功 + public static bool Remove(string key) { - return new SlidingWindowRateLimiter(limit, window); + return _cache.TryRemove(key, out _); } /// - /// 创建固定窗口限流器 + /// 清空所有缓存 /// - public static FixedWindowRateLimiter CreateFixedWindow(int limit, TimeSpan window) + public static void Clear() { - return new FixedWindowRateLimiter(limit, window); + _cache.Clear(); } - } - - /// - /// 令牌桶限流器 - /// - public class TokenBucketRateLimiter - { - private readonly int _capacity; - private readonly int _refillRate; - private readonly TimeSpan _refillPeriod; - private double _tokens; - private DateTime _lastRefill; - private readonly object _lock = new object(); /// - /// 桶容量 + /// 获取缓存数量 /// - public int Capacity => _capacity; - - /// - /// 当前令牌数 - /// - public int AvailableTokens + /// 缓存项数量 + public static int Count() { - get - { - lock (_lock) - { - Refill(); - return (int)_tokens; - } - } + return _cache.Count; } /// - /// 创建令牌桶限流器 + /// 获取所有缓存键 /// - public TokenBucketRateLimiter(int capacity, int refillRate, TimeSpan refillPeriod) + /// 缓存键集合 + public static IEnumerable GetKeys() { - if (capacity <= 0) - throw new ArgumentOutOfRangeException(nameof(capacity)); - if (refillRate <= 0) - throw new ArgumentOutOfRangeException(nameof(refillRate)); - - _capacity = capacity; - _refillRate = refillRate; - _refillPeriod = refillPeriod; - _tokens = capacity; - _lastRefill = DateTime.UtcNow; + return _cache.Keys.ToList(); } /// - /// 尝试获取令牌 + /// 设置缓存过期时间 /// - public bool TryAcquire(int tokens = 1) + /// 缓存键 + /// 过期时间 + /// 是否设置成功 + public static bool SetExpiration(string key, TimeSpan expiration) { - lock (_lock) - { - Refill(); - if (_tokens >= tokens) - { - _tokens -= tokens; - return true; - } + if (!_cache.TryGetValue(key, out var obj)) return false; - } - } - /// - /// 等待获取令牌 - /// - public void Acquire(int tokens = 1) - { - while (!TryAcquire(tokens)) + var itemType = obj.GetType(); + var expireTimeProperty = itemType.GetProperty("ExpireTime"); + if (expireTimeProperty != null) { - Thread.Sleep(10); + expireTimeProperty.SetValue(obj, DateTime.UtcNow.Add(expiration)); + return true; } - } - private void Refill() - { - var now = DateTime.UtcNow; - var elapsed = now - _lastRefill; - var tokensToAdd = elapsed.TotalMilliseconds / _refillPeriod.TotalMilliseconds * _refillRate; - - if (tokensToAdd > 0) - { - _tokens = Math.Min(_capacity, _tokens + tokensToAdd); - _lastRefill = now; - } + return false; } - } - - /// - /// 滑动窗口限流器 - /// - public class SlidingWindowRateLimiter - { - private readonly int _limit; - private readonly TimeSpan _window; - private readonly Queue _timestamps; - private readonly object _lock = new object(); - /// - /// 限制 - /// - public int Limit => _limit; - - /// - /// 窗口大小 - /// - public TimeSpan Window => _window; - - /// - /// 创建滑动窗口限流器 - /// - public SlidingWindowRateLimiter(int limit, TimeSpan window) + private static bool IsExpired(CacheItem item) { - if (limit <= 0) - throw new ArgumentOutOfRangeException(nameof(limit)); + var now = DateTime.UtcNow; - _limit = limit; - _window = window; - _timestamps = new Queue(); - } + // 检查绝对过期 + if (item.ExpireTime.HasValue && now >= item.ExpireTime.Value) + return true; - /// - /// 尝试获取许可 - /// - public bool TryAcquire() - { - lock (_lock) + // 检查滑动过期 + if (item.SlidingExpiration.HasValue) { - Cleanup(); - if (_timestamps.Count < _limit) - { - _timestamps.Enqueue(DateTime.UtcNow); + var expireTime = item.LastAccess.Add(item.SlidingExpiration.Value); + if (now >= expireTime) return true; - } - return false; } - } - /// - /// 获取当前窗口内的请求数 - /// - public int CurrentCount - { - get - { - lock (_lock) - { - Cleanup(); - return _timestamps.Count; - } - } - } - - private void Cleanup() - { - var cutoff = DateTime.UtcNow - _window; - while (_timestamps.Count > 0 && _timestamps.Peek() < cutoff) - { - _timestamps.Dequeue(); - } + return false; } - } - - /// - /// 固定窗口限流器 - /// - public class FixedWindowRateLimiter - { - private readonly int _limit; - private readonly TimeSpan _window; - private int _count; - private DateTime _windowStart; - private readonly object _lock = new object(); - - /// - /// 限制 - /// - public int Limit => _limit; - - /// - /// 窗口大小 - /// - public TimeSpan Window => _window; - /// - /// 创建固定窗口限流器 - /// - public FixedWindowRateLimiter(int limit, TimeSpan window) + private static void CleanupExpired() { - if (limit <= 0) - throw new ArgumentOutOfRangeException(nameof(limit)); - - _limit = limit; - _window = window; - _count = 0; - _windowStart = DateTime.UtcNow; - } + var keysToRemove = new List(); - /// - /// 尝试获取许可 - /// - public bool TryAcquire() - { - lock (_lock) + foreach (var kvp in _cache) { - var now = DateTime.UtcNow; - if (now - _windowStart >= _window) - { - _count = 0; - _windowStart = now; - } + var itemType = kvp.Value.GetType(); + var expireTimeProperty = itemType.GetProperty("ExpireTime"); + var slidingExpirationProperty = itemType.GetProperty("SlidingExpiration"); + var lastAccessProperty = itemType.GetProperty("LastAccess"); - if (_count < _limit) + if (expireTimeProperty != null) { - _count++; - return true; - } - return false; - } - } + var expireTime = (DateTime?)expireTimeProperty.GetValue(kvp.Value); + var slidingExpiration = (TimeSpan?)slidingExpirationProperty?.GetValue(kvp.Value); + var lastAccess = (DateTime)lastAccessProperty!.GetValue(kvp.Value)!; - /// - /// 获取当前窗口内的请求数 - /// - public int CurrentCount - { - get - { - lock (_lock) - { var now = DateTime.UtcNow; - if (now - _windowStart >= _window) - return 0; - return _count; - } - } - } - - /// - /// 获取距离下一个窗口的时间 - /// - public TimeSpan TimeUntilNextWindow - { - get - { - lock (_lock) - { - var elapsed = DateTime.UtcNow - _windowStart; - return _window - elapsed; - } - } - } - } - /// - /// 对象池工具类 - /// - public static class ObjectPoolUtil - { - /// - /// 创建对象池 - /// - public static ObjectPool Create(Func factory, int maxSize = 100) where T : class - { - return new ObjectPool(factory, maxSize); - } - } - - /// - /// 对象池实现 - /// - public class ObjectPool where T : class - { - private readonly Func _factory; - private readonly int _maxSize; - private readonly Stack _pool; - private readonly object _lock = new object(); - - /// - /// 池中可用对象数 - /// - public int AvailableCount - { - get - { - lock (_lock) - { - return _pool.Count; - } - } - } - - /// - /// 最大大小 - /// - public int MaxSize => _maxSize; - - /// - /// 创建对象池 - /// - public ObjectPool(Func factory, int maxSize = 100) - { - _factory = factory ?? throw new ArgumentNullException(nameof(factory)); - _maxSize = maxSize > 0 ? maxSize : throw new ArgumentOutOfRangeException(nameof(maxSize)); - _pool = new Stack(); - } - - /// - /// 获取对象 - /// - public T Get() - { - lock (_lock) - { - if (_pool.Count > 0) - { - return _pool.Pop(); - } - } - return _factory(); - } - - /// - /// 归还对象 - /// - public void Return(T obj) - { - if (obj == null) - return; - - // 如果对象实现了 IResettable,重置它 - if (obj is IResettable resettable) - { - resettable.Reset(); - } - - lock (_lock) - { - if (_pool.Count < _maxSize) - { - _pool.Push(obj); + if (expireTime.HasValue && now >= expireTime.Value) + { + keysToRemove.Add(kvp.Key); + } + else if (slidingExpiration.HasValue) + { + var slidingExpire = lastAccess.Add(slidingExpiration.Value); + if (now >= slidingExpire) + { + keysToRemove.Add(kvp.Key); + } + } } } - } - - /// - /// 清空池 - /// - public void Clear() - { - lock (_lock) - { - _pool.Clear(); - } - } - /// - /// 预热池 - /// - public void Warmup(int count) - { - for (int i = 0; i < count && i < _maxSize; i++) + foreach (var key in keysToRemove) { - Return(_factory()); + _cache.TryRemove(key, out _); } } } - - /// - /// 可重置接口 - /// - public interface IResettable - { - /// - /// 重置对象状态 - /// - void Reset(); - } -} +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/GraphUtil.cs b/EasyTool.Core/CollectionsCategory/GraphUtil.cs index b4738cc..ac2ed70 100644 --- a/EasyTool.Core/CollectionsCategory/GraphUtil.cs +++ b/EasyTool.Core/CollectionsCategory/GraphUtil.cs @@ -5,259 +5,15 @@ namespace EasyTool.CollectionsCategory { /// - /// 图工具类 + /// 图算法工具类 /// public static class GraphUtil { - /// - /// 创建图 - /// - public static Graph Create() where T : IEquatable - { - return new Graph(); - } - - /// - /// 创建有向图 - /// - public static Graph CreateDirected() where T : IEquatable - { - return new Graph(true); - } - } - - /// - /// 图实现 - /// - public class Graph where T : IEquatable - { - private readonly Dictionary> _adjacencyList; - private readonly bool _isDirected; - - /// - /// 顶点数量 - /// - public int VertexCount => _adjacencyList.Count; - - /// - /// 边数量 - /// - public int EdgeCount { get; private set; } - - /// - /// 是否为有向图 - /// - public bool IsDirected => _isDirected; - - /// - /// 所有顶点 - /// - public IEnumerable Vertices => _adjacencyList.Keys; - - /// - /// 创建图 - /// - public Graph() : this(false) - { - } - - /// - /// 创建图 - /// - public Graph(bool isDirected) - { - _adjacencyList = new Dictionary>(); - _isDirected = isDirected; - EdgeCount = 0; - } - - /// - /// 添加顶点 - /// - public void AddVertex(T vertex) - { - if (!_adjacencyList.ContainsKey(vertex)) - { - _adjacencyList[vertex] = new List(); - } - } - - /// - /// 添加边 - /// - public void AddEdge(T from, T to, double weight = 1) - { - AddVertex(from); - AddVertex(to); - - _adjacencyList[from].Add(new Edge(to, weight)); - EdgeCount++; - - if (!_isDirected) - { - _adjacencyList[to].Add(new Edge(from, weight)); - } - } - - /// - /// 移除顶点 - /// - public bool RemoveVertex(T vertex) - { - if (!_adjacencyList.ContainsKey(vertex)) - return false; - - int edgeCount = _adjacencyList[vertex].Count; - _adjacencyList.Remove(vertex); - EdgeCount -= edgeCount; - - // 移除所有指向该顶点的边 - foreach (var edges in _adjacencyList.Values) - { - int removed = edges.RemoveAll(e => e.Target.Equals(vertex)); - if (!_isDirected) - EdgeCount -= removed; - } - - return true; - } - - /// - /// 移除边 - /// - public bool RemoveEdge(T from, T to) - { - if (!_adjacencyList.TryGetValue(from, out var edges)) - return false; - - int removed = edges.RemoveAll(e => e.Target.Equals(to)); - if (removed > 0) - { - EdgeCount--; - - if (!_isDirected && _adjacencyList.TryGetValue(to, out var reverseEdges)) - { - reverseEdges.RemoveAll(e => e.Target.Equals(from)); - } - - return true; - } - - return false; - } - - /// - /// 获取邻居 - /// - public IEnumerable GetNeighbors(T vertex) - { - if (!_adjacencyList.TryGetValue(vertex, out var edges)) - return Enumerable.Empty(); - - return edges.Select(e => e.Target); - } - - /// - /// 获取边权重 - /// - public double GetEdgeWeight(T from, T to) - { - if (!_adjacencyList.TryGetValue(from, out var edges)) - return double.PositiveInfinity; - - var edge = edges.FirstOrDefault(e => e.Target.Equals(to)); - return edge?.Weight ?? double.PositiveInfinity; - } - - /// - /// 是否包含顶点 - /// - public bool ContainsVertex(T vertex) - { - return _adjacencyList.ContainsKey(vertex); - } - - /// - /// 是否包含边 - /// - public bool ContainsEdge(T from, T to) - { - if (!_adjacencyList.TryGetValue(from, out var edges)) - return false; - - return edges.Any(e => e.Target.Equals(to)); - } - - /// - /// 获取顶点的度 - /// - public int GetDegree(T vertex) - { - if (!_adjacencyList.TryGetValue(vertex, out var edges)) - return 0; - - return edges.Count; - } - - private class Edge - { - public T Target { get; } - public double Weight { get; } - - public Edge(T target, double weight) - { - Target = target; - Weight = weight; - } - } - } - - /// - /// 图遍历工具类 - /// - public static class GraphTraversalUtil - { - /// - /// 深度优先搜索 - /// - public static List DFS(Graph graph, T start) where T : IEquatable - { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - - var result = new List(); - var visited = new HashSet(); - - DFSVisit(graph, start, visited, result); - - return result; - } - - private static void DFSVisit(Graph graph, T vertex, HashSet visited, List result) where T : IEquatable - { - if (visited.Contains(vertex)) - return; - - visited.Add(vertex); - result.Add(vertex); - - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - if (!visited.Contains(neighbor)) - { - DFSVisit(graph, neighbor, visited, result); - } - } - } - /// /// 广度优先搜索 /// - public static List BFS(Graph graph, T start) where T : IEquatable + public static List BFS(Graph graph, T start) where T : notnull { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - var result = new List(); var visited = new HashSet(); var queue = new Queue(); @@ -284,709 +40,348 @@ public static List BFS(Graph graph, T start) where T : IEquatable } /// - /// 查找路径(BFS) + /// 深度优先搜索 /// - public static List FindPath(Graph graph, T start, T end) where T : IEquatable + public static List DFS(Graph graph, T start) where T : notnull { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - - if (!graph.ContainsVertex(start) || !graph.ContainsVertex(end)) - return null; - + var result = new List(); var visited = new HashSet(); - var parent = new Dictionary(); - var queue = new Queue(); - - queue.Enqueue(start); - visited.Add(start); - parent[start] = default; - - while (queue.Count > 0) - { - var current = queue.Dequeue(); - - if (current.Equals(end)) - { - return ReconstructPath(parent, start, end); - } - - foreach (var neighbor in graph.GetNeighbors(current)) - { - if (!visited.Contains(neighbor)) - { - visited.Add(neighbor); - parent[neighbor] = current; - queue.Enqueue(neighbor); - } - } - } - - return null; + DFSVisit(graph, start, visited, result); + return result; } - private static List ReconstructPath(Dictionary parent, T start, T end) + private static void DFSVisit(Graph graph, T node, HashSet visited, List result) where T : notnull { - var path = new List(); - var current = end; + visited.Add(node); + result.Add(node); - while (!current.Equals(default)) + foreach (var neighbor in graph.GetNeighbors(node)) { - path.Add(current); - if (current.Equals(start)) - break; - current = parent[current]; + if (!visited.Contains(neighbor)) + { + DFSVisit(graph, neighbor, visited, result); + } } - - path.Reverse(); - return path; } - } - /// - /// 拓扑排序工具类 - /// - public static class TopologicalSortUtil - { /// - /// 拓扑排序 + /// 最短路径(Dijkstra算法) /// - public static List Sort(Graph graph) where T : IEquatable + public static List? Dijkstra(WeightedGraph graph, T start, T end) where T : notnull { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (!graph.IsDirected) - throw new ArgumentException("Topological sort requires a directed graph"); + var distances = new Dictionary(); + var previous = new Dictionary(); + var unvisited = new HashSet(); - var inDegree = new Dictionary(); foreach (var vertex in graph.Vertices) { - inDegree[vertex] = 0; + distances[vertex] = double.PositiveInfinity; + unvisited.Add(vertex); } + distances[start] = 0; - foreach (var vertex in graph.Vertices) + while (unvisited.Count > 0) { - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - inDegree[neighbor]++; - } - } + var current = default(T)!; + var minDist = double.PositiveInfinity; - var queue = new Queue(); - foreach (var kvp in inDegree) - { - if (kvp.Value == 0) + foreach (var vertex in unvisited) { - queue.Enqueue(kvp.Key); + if (distances[vertex] < minDist) + { + minDist = distances[vertex]; + current = vertex; + } } - } - var result = new List(); + if (current == null || current.Equals(end)) + break; - while (queue.Count > 0) - { - var current = queue.Dequeue(); - result.Add(current); + unvisited.Remove(current); - foreach (var neighbor in graph.GetNeighbors(current)) + foreach (var (neighbor, weight) in graph.GetWeightedNeighbors(current)) { - inDegree[neighbor]--; - if (inDegree[neighbor] == 0) + var alt = distances[current] + weight; + if (alt < distances[neighbor]) { - queue.Enqueue(neighbor); + distances[neighbor] = alt; + previous[neighbor] = current; } } } - if (result.Count != graph.VertexCount) - { - throw new InvalidOperationException("Graph contains a cycle"); - } - - return result; - } + // 重建路径 + if (!previous.ContainsKey(end) && !start.Equals(end)) + return null; - /// - /// 尝试拓扑排序 - /// - public static bool TrySort(Graph graph, out List result) where T : IEquatable - { - try - { - result = Sort(graph); - return true; - } - catch + var path = new List(); + var current2 = end; + while (current2 != null) { - result = null; - return false; + path.Insert(0, current2); + current2 = previous.TryGetValue(current2, out var prev) ? prev : default; + if (current2 == null && !path[0].Equals(start)) + return null; } + + return path; } - } - /// - /// 环检测工具类 - /// - public static class CycleDetectionUtil - { /// - /// 检测是否有环 + /// 拓扑排序 /// - public static bool HasCycle(Graph graph) where T : IEquatable + public static List? TopologicalSort(Graph graph) where T : notnull { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - - if (graph.IsDirected) - { - return HasCycleDirected(graph); - } - else - { - return HasCycleUndirected(graph); - } - } - - private static bool HasCycleDirected(Graph graph) where T : IEquatable - { - var white = new HashSet(graph.Vertices); // 未访问 - var gray = new HashSet(); // 正在访问 - var black = new HashSet(); // 已完成 + var result = new List(); + var visited = new HashSet(); + var tempMarked = new HashSet(); foreach (var vertex in graph.Vertices) { - if (white.Contains(vertex)) + if (!visited.Contains(vertex)) { - if (DFSCycleDirected(graph, vertex, white, gray, black)) - return true; + if (!TopologicalVisit(graph, vertex, visited, tempMarked, result)) + return null; // 存在环 } } - return false; + result.Reverse(); + return result; } - private static bool DFSCycleDirected(Graph graph, T vertex, HashSet white, HashSet gray, HashSet black) where T : IEquatable + private static bool TopologicalVisit(Graph graph, T node, HashSet visited, HashSet tempMarked, List result) where T : notnull { - white.Remove(vertex); - gray.Add(vertex); + if (tempMarked.Contains(node)) + return false; // 存在环 - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - if (black.Contains(neighbor)) - continue; + if (visited.Contains(node)) + return true; - if (gray.Contains(neighbor)) - return true; + tempMarked.Add(node); - if (DFSCycleDirected(graph, neighbor, white, gray, black)) - return true; + foreach (var neighbor in graph.GetNeighbors(node)) + { + if (!TopologicalVisit(graph, neighbor, visited, tempMarked, result)) + return false; } - gray.Remove(vertex); - black.Add(vertex); - return false; + tempMarked.Remove(node); + visited.Add(node); + result.Add(node); + return true; } - private static bool HasCycleUndirected(Graph graph) where T : IEquatable + /// + /// 检测环 + /// + public static bool HasCycle(Graph graph) where T : notnull { var visited = new HashSet(); + var recursionStack = new HashSet(); foreach (var vertex in graph.Vertices) { - if (!visited.Contains(vertex)) - { - if (DFSCycleUndirected(graph, vertex, default, visited)) - return true; - } + if (HasCycleDFS(graph, vertex, visited, recursionStack)) + return true; } return false; } - private static bool DFSCycleUndirected(Graph graph, T vertex, T parent, HashSet visited) where T : IEquatable + private static bool HasCycleDFS(Graph graph, T node, HashSet visited, HashSet recursionStack) where T : notnull { - visited.Add(vertex); + if (recursionStack.Contains(node)) + return true; + + if (visited.Contains(node)) + return false; - foreach (var neighbor in graph.GetNeighbors(vertex)) + visited.Add(node); + recursionStack.Add(node); + + foreach (var neighbor in graph.GetNeighbors(node)) { - if (!visited.Contains(neighbor)) - { - if (DFSCycleUndirected(graph, neighbor, vertex, visited)) - return true; - } - else if (!neighbor.Equals(parent)) - { + if (HasCycleDFS(graph, neighbor, visited, recursionStack)) return true; - } } + recursionStack.Remove(node); return false; } /// - /// 查找环 + /// 连通分量 /// - public static List FindCycle(Graph graph) where T : IEquatable + public static List> GetConnectedComponents(Graph graph) where T : notnull { - if (graph == null || !graph.IsDirected) - return null; - + var components = new List>(); var visited = new HashSet(); - var recStack = new HashSet(); - var path = new List(); foreach (var vertex in graph.Vertices) { - if (FindCycleDFS(graph, vertex, visited, recStack, path)) + if (!visited.Contains(vertex)) { - return path; - } - } + var component = new List(); + var queue = new Queue(); + queue.Enqueue(vertex); + visited.Add(vertex); - return null; - } + while (queue.Count > 0) + { + var current = queue.Dequeue(); + component.Add(current); - private static bool FindCycleDFS(Graph graph, T vertex, HashSet visited, HashSet recStack, List path) where T : IEquatable - { - visited.Add(vertex); - recStack.Add(vertex); - path.Add(vertex); + foreach (var neighbor in graph.GetNeighbors(current)) + { + if (!visited.Contains(neighbor)) + { + visited.Add(neighbor); + queue.Enqueue(neighbor); + } + } + } - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - if (!visited.Contains(neighbor)) - { - if (FindCycleDFS(graph, neighbor, visited, recStack, path)) - return true; - } - else if (recStack.Contains(neighbor)) - { - // 找到环,截取环部分 - int start = path.IndexOf(neighbor); - path.RemoveRange(0, start); - return true; + components.Add(component); } } - recStack.Remove(vertex); - path.RemoveAt(path.Count - 1); - return false; + return components; } } /// - /// 连通分量工具类 + /// 图数据结构 /// - public static class ConnectedComponentsUtil + public class Graph where T : notnull { - /// - /// 获取连通分量 - /// - public static List> GetComponents(Graph graph) where T : IEquatable - { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - - var visited = new HashSet(); - var components = new List>(); - - foreach (var vertex in graph.Vertices) - { - if (!visited.Contains(vertex)) - { - var component = new List(); - DFSComponent(graph, vertex, visited, component); - components.Add(component); - } - } + private readonly Dictionary> _adjacencyList = new(); + private readonly bool _directed; - return components; - } - - private static void DFSComponent(Graph graph, T vertex, HashSet visited, List component) where T : IEquatable + public Graph(bool directed = false) { - visited.Add(vertex); - component.Add(vertex); - - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - if (!visited.Contains(neighbor)) - { - DFSComponent(graph, neighbor, visited, component); - } - } + _directed = directed; } /// - /// 判断是否连通 + /// 所有顶点 /// - public static bool IsConnected(Graph graph) where T : IEquatable - { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - - if (graph.VertexCount == 0) - return true; - - return GetComponents(graph).Count == 1; - } + public IEnumerable Vertices => _adjacencyList.Keys; /// - /// 获取强连通分量(Kosaraju 算法) + /// 边数 /// - public static List> GetStronglyConnectedComponents(Graph graph) where T : IEquatable - { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (!graph.IsDirected) - throw new ArgumentException("Strongly connected components require a directed graph"); - - var visited = new HashSet(); - var finishOrder = new Stack(); - - // 第一次 DFS 获取完成顺序 - foreach (var vertex in graph.Vertices) - { - if (!visited.Contains(vertex)) - { - DFSOrder(graph, vertex, visited, finishOrder); - } - } - - // 构建转置图 - var transpose = Transpose(graph); - - // 第二次 DFS 按完成顺序的逆序 - visited.Clear(); - var components = new List>(); - - while (finishOrder.Count > 0) - { - var vertex = finishOrder.Pop(); - if (!visited.Contains(vertex)) - { - var component = new List(); - DFSComponent(transpose, vertex, visited, component); - components.Add(component); - } - } + public int EdgeCount { get; private set; } - return components; - } + /// + /// 顶点数 + /// + public int VertexCount => _adjacencyList.Count; - private static void DFSOrder(Graph graph, T vertex, HashSet visited, Stack finishOrder) where T : IEquatable + /// + /// 添加顶点 + /// + public void AddVertex(T vertex) { - visited.Add(vertex); - - foreach (var neighbor in graph.GetNeighbors(vertex)) + if (!_adjacencyList.ContainsKey(vertex)) { - if (!visited.Contains(neighbor)) - { - DFSOrder(graph, neighbor, visited, finishOrder); - } + _adjacencyList[vertex] = new List(); } - - finishOrder.Push(vertex); } - private static Graph Transpose(Graph graph) where T : IEquatable - { - var transpose = new Graph(true); - - foreach (var vertex in graph.Vertices) - { - transpose.AddVertex(vertex); - } - - foreach (var vertex in graph.Vertices) - { - foreach (var neighbor in graph.GetNeighbors(vertex)) - { - transpose.AddEdge(neighbor, vertex); - } - } - - return transpose; - } - } - - /// - /// 最短路径工具类 - /// - public static class ShortestPathUtil - { /// - /// Dijkstra 最短路径算法 + /// 添加边 /// - public static Dictionary Dijkstra(Graph graph, T start) where T : IEquatable + public void AddEdge(T from, T to) { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (!graph.ContainsVertex(start)) - throw new ArgumentException("Start vertex not found"); - - var distances = new Dictionary(); - var visited = new HashSet(); - var pq = PriorityQueueUtil.CreateMin(); - - foreach (var vertex in graph.Vertices) - { - distances[vertex] = double.PositiveInfinity; - } - distances[start] = 0; - pq.Enqueue(start, 0); + AddVertex(from); + AddVertex(to); - while (pq.Count > 0) + _adjacencyList[from].Add(to); + if (!_directed) { - var current = pq.Dequeue(); - - if (visited.Contains(current)) - continue; - - visited.Add(current); - - foreach (var neighbor in graph.GetNeighbors(current)) - { - var weight = graph.GetEdgeWeight(current, neighbor); - var newDist = distances[current] + weight; - - if (newDist < distances[neighbor]) - { - distances[neighbor] = newDist; - pq.Enqueue(neighbor, newDist); - } - } + _adjacencyList[to].Add(from); } - - return distances; + EdgeCount++; } /// - /// Dijkstra 最短路径(带路径) + /// 移除边 /// - public static (Dictionary Distances, Dictionary Previous) DijkstraWithPath(Graph graph, T start) where T : IEquatable + public void RemoveEdge(T from, T to) { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (!graph.ContainsVertex(start)) - throw new ArgumentException("Start vertex not found"); - - var distances = new Dictionary(); - var previous = new Dictionary(); - var visited = new HashSet(); - var pq = PriorityQueueUtil.CreateMin(); - - foreach (var vertex in graph.Vertices) + if (_adjacencyList.TryGetValue(from, out var neighbors)) { - distances[vertex] = double.PositiveInfinity; + neighbors.Remove(to); } - distances[start] = 0; - pq.Enqueue(start, 0); - while (pq.Count > 0) + if (!_directed && _adjacencyList.TryGetValue(to, out var neighbors2)) { - var current = pq.Dequeue(); - - if (visited.Contains(current)) - continue; - - visited.Add(current); - - foreach (var neighbor in graph.GetNeighbors(current)) - { - var weight = graph.GetEdgeWeight(current, neighbor); - var newDist = distances[current] + weight; - - if (newDist < distances[neighbor]) - { - distances[neighbor] = newDist; - previous[neighbor] = current; - pq.Enqueue(neighbor, newDist); - } - } + neighbors2.Remove(from); } - return (distances, previous); + EdgeCount--; } /// - /// 重建路径 + /// 获取邻居 /// - public static List ReconstructPath(Dictionary previous, T start, T end) + public IEnumerable GetNeighbors(T vertex) { - var path = new List(); - var current = end; - - while (!current.Equals(default)) - { - path.Add(current); - if (current.Equals(start)) - break; - - if (!previous.ContainsKey(current)) - return null; // 无法到达 - - current = previous[current]; - } - - path.Reverse(); - return path.Count > 0 && path[0].Equals(start) ? path : null; + return _adjacencyList.TryGetValue(vertex, out var neighbors) + ? neighbors + : Enumerable.Empty(); } /// - /// Bellman-Ford 算法(支持负权边) + /// 是否有边 /// - public static Dictionary BellmanFord(Graph graph, T start) where T : IEquatable + public bool HasEdge(T from, T to) { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (!graph.ContainsVertex(start)) - throw new ArgumentException("Start vertex not found"); - - var distances = new Dictionary(); - - foreach (var vertex in graph.Vertices) - { - distances[vertex] = double.PositiveInfinity; - } - distances[start] = 0; - - // 松弛 V-1 次 - for (int i = 0; i < graph.VertexCount - 1; i++) - { - foreach (var u in graph.Vertices) - { - if (distances[u] == double.PositiveInfinity) - continue; - - foreach (var v in graph.GetNeighbors(u)) - { - var weight = graph.GetEdgeWeight(u, v); - if (distances[u] + weight < distances[v]) - { - distances[v] = distances[u] + weight; - } - } - } - } - - // 检查负环 - foreach (var u in graph.Vertices) - { - if (distances[u] == double.PositiveInfinity) - continue; - - foreach (var v in graph.GetNeighbors(u)) - { - var weight = graph.GetEdgeWeight(u, v); - if (distances[u] + weight < distances[v]) - { - throw new InvalidOperationException("Graph contains a negative cycle"); - } - } - } - - return distances; + return _adjacencyList.TryGetValue(from, out var neighbors) && neighbors.Contains(to); } } /// - /// 最小生成树工具类 + /// 带权重的图 /// - public static class MSTUtil + public class WeightedGraph where T : notnull { - /// - /// Prim 算法 - /// - public static List<(T From, T To, double Weight)> Prim(Graph graph) where T : IEquatable - { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (graph.IsDirected) - throw new ArgumentException("MST requires an undirected graph"); - if (graph.VertexCount == 0) - return new List<(T, T, double)>(); - - var mst = new List<(T From, T To, double Weight)>(); - var visited = new HashSet(); - var pq = PriorityQueueUtil.CreateMin<(T From, T To), double>(); + private readonly Dictionary> _adjacencyList = new(); + private readonly bool _directed; - var startVertex = graph.Vertices.First(); - visited.Add(startVertex); + public WeightedGraph(bool directed = false) + { + _directed = directed; + } - foreach (var neighbor in graph.GetNeighbors(startVertex)) - { - var weight = graph.GetEdgeWeight(startVertex, neighbor); - pq.Enqueue((startVertex, neighbor), weight); - } + public IEnumerable Vertices => _adjacencyList.Keys; + public int VertexCount => _adjacencyList.Count; - while (pq.Count > 0 && visited.Count < graph.VertexCount) + public void AddVertex(T vertex) + { + if (!_adjacencyList.ContainsKey(vertex)) { - var (from, to) = pq.Dequeue(); - - if (visited.Contains(to)) - continue; - - visited.Add(to); - var weight = graph.GetEdgeWeight(from, to); - mst.Add((from, to, weight)); - - foreach (var neighbor in graph.GetNeighbors(to)) - { - if (!visited.Contains(neighbor)) - { - var neighborWeight = graph.GetEdgeWeight(to, neighbor); - pq.Enqueue((to, neighbor), neighborWeight); - } - } + _adjacencyList[vertex] = new List<(T, double)>(); } - - return mst; } - /// - /// Kruskal 算法 - /// - public static List<(T From, T To, double Weight)> Kruskal(Graph graph) where T : IEquatable + public void AddEdge(T from, T to, double weight) { - if (graph == null) - throw new ArgumentNullException(nameof(graph)); - if (graph.IsDirected) - throw new ArgumentException("MST requires an undirected graph"); - - var mst = new List<(T From, T To, double Weight)>(); - var edges = new List<(T From, T To, double Weight)>(); - var processed = new HashSet<(T, T)>(); - - foreach (var from in graph.Vertices) - { - foreach (var to in graph.GetNeighbors(from)) - { - var key = from.GetHashCode() < to.GetHashCode() ? (from, to) : (to, from); - if (!processed.Contains(key)) - { - processed.Add(key); - var weight = graph.GetEdgeWeight(from, to); - edges.Add((from, to, weight)); - } - } - } - - edges.Sort((a, b) => a.Weight.CompareTo(b.Weight)); - - var uf = UnionFindUtil.Create(graph.Vertices.ToList()); + AddVertex(from); + AddVertex(to); - foreach (var edge in edges) + _adjacencyList[from].Add((to, weight)); + if (!_directed) { - if (!uf.Connected(edge.From, edge.To)) - { - uf.Union(edge.From, edge.To); - mst.Add(edge); - } + _adjacencyList[to].Add((from, weight)); } + } - return mst; + public IEnumerable<(T Vertex, double Weight)> GetWeightedNeighbors(T vertex) + { + return _adjacencyList.TryGetValue(vertex, out var neighbors) + ? neighbors + : Enumerable.Empty<(T, double)>(); } } - -} +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/GroupUtil.cs b/EasyTool.Core/CollectionsCategory/GroupUtil.cs new file mode 100644 index 0000000..0e44d13 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/GroupUtil.cs @@ -0,0 +1,346 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 分组工具类 + /// + public static class GroupUtil + { + /// + /// 按指定数量分组 + /// + public static List> Chunk(IEnumerable source, int size) + { + if (size <= 0) + throw new ArgumentException("分组大小必须大于0", nameof(size)); + + var result = new List>(); + var current = new List(size); + + foreach (var item in source) + { + current.Add(item); + + if (current.Count == size) + { + result.Add(current); + current = new List(size); + } + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 按条件分组 + /// + public static List> GroupWhile(IEnumerable source, Func predicate) + { + var result = new List>(); + var current = new List(); + + foreach (var item in source) + { + if (predicate(item) && current.Count > 0) + { + result.Add(current); + current = new List(); + } + + current.Add(item); + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 按相邻相同元素分组 + /// + public static List> GroupAdjacent(IEnumerable source) + { + var result = new List>(); + List? current = null; + + foreach (var item in source) + { + if (current == null || !EqualityComparer.Default.Equals(current[0], item)) + { + current = new List { item }; + result.Add(current); + } + else + { + current.Add(item); + } + } + + return result; + } + + /// + /// 按相邻相同元素分组(使用比较器) + /// + public static List> GroupAdjacent(IEnumerable source, IEqualityComparer comparer) + { + var result = new List>(); + List? current = null; + + foreach (var item in source) + { + if (current == null || !comparer.Equals(current[0], item)) + { + current = new List { item }; + result.Add(current); + } + else + { + current.Add(item); + } + } + + return result; + } + + /// + /// 交替分组 + /// + public static (List First, List Second) Alternate(IEnumerable source) + { + var first = new List(); + var second = new List(); + var isFirst = true; + + foreach (var item in source) + { + if (isFirst) + first.Add(item); + else + second.Add(item); + + isFirst = !isFirst; + } + + return (first, second); + } + + /// + /// 分割集合 + /// + public static (List True, List False) Partition(IEnumerable source, Func predicate) + { + var trueItems = new List(); + var falseItems = new List(); + + foreach (var item in source) + { + if (predicate(item)) + trueItems.Add(item); + else + falseItems.Add(item); + } + + return (trueItems, falseItems); + } + + /// + /// 交错合并两个集合 + /// + public static IEnumerable Interleave(IEnumerable first, IEnumerable second) + { + using var e1 = first.GetEnumerator(); + using var e2 = second.GetEnumerator(); + + while (e1.MoveNext()) + { + yield return e1.Current; + + if (e2.MoveNext()) + yield return e2.Current; + } + + while (e2.MoveNext()) + { + yield return e2.Current; + } + } + + /// + /// 按滑动窗口分组 + /// + public static List> Window(IEnumerable source, int size, int step = 1) + { + if (size <= 0) + throw new ArgumentException("窗口大小必须大于0", nameof(size)); + + if (step <= 0) + throw new ArgumentException("步进必须大于0", nameof(step)); + + var list = source.ToList(); + var result = new List>(); + + for (int i = 0; i <= list.Count - size; i += step) + { + result.Add(list.Skip(i).Take(size).ToList()); + } + + return result; + } + + /// + /// 按累积条件分组 + /// + public static List> GroupByAccumulator(IEnumerable source, Func shouldGroup) + { + var result = new List>(); + var current = new List(); + T? lastItem = default; + + foreach (var item in source) + { + if (lastItem == null || shouldGroup(lastItem, item)) + { + current.Add(item); + } + else + { + if (current.Count > 0) + result.Add(current); + current = new List { item }; + } + + lastItem = item; + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 获取笛卡尔积 + /// + public static IEnumerable<(T1 First, T2 Second)> CartesianProduct( + IEnumerable first, IEnumerable second) + { + foreach (var item1 in first) + { + foreach (var item2 in second) + { + yield return (item1, item2); + } + } + } + + /// + /// 获取多个集合的笛卡尔积 + /// + public static IEnumerable> CartesianProduct(IEnumerable> sources) + { + var sourceList = sources.ToList(); + + if (sourceList.Count == 0) + { + yield return new List(); + yield break; + } + + var first = sourceList[0]; + var rest = sourceList.Skip(1); + + foreach (var item in first) + { + foreach (var restCombination in CartesianProduct(rest)) + { + var combination = new List { item }; + combination.AddRange(restCombination); + yield return combination; + } + } + } + + /// + /// 获取排列组合 + /// + public static IEnumerable> Combinations(IEnumerable source, int count) + { + var list = source.ToList(); + + if (count > list.Count) + yield break; + + if (count == 0) + { + yield return new List(); + yield break; + } + + if (count == 1) + { + foreach (var item in list) + { + yield return new List { item }; + } + yield break; + } + + for (int i = 0; i <= list.Count - count; i++) + { + foreach (var restCombination in Combinations(list.Skip(i + 1), count - 1)) + { + var combination = new List { list[i] }; + combination.AddRange(restCombination); + yield return combination; + } + } + } + + /// + /// 获取全排列 + /// + public static IEnumerable> Permutations(IEnumerable source) + { + var list = source.ToList(); + + if (list.Count == 0) + { + yield return new List(); + yield break; + } + + if (list.Count == 1) + { + yield return new List(list); + yield break; + } + + for (int i = 0; i < list.Count; i++) + { + var current = list[i]; + var remaining = list.Take(i).Concat(list.Skip(i + 1)); + + foreach (var permutation in Permutations(remaining)) + { + var result = new List { current }; + result.AddRange(permutation); + yield return result; + } + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs b/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs new file mode 100644 index 0000000..d29e039 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs @@ -0,0 +1,421 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 不可变列表扩展 + /// + public static class ImmutableListExtension + { + /// + /// 创建不可变列表 + /// + public static ImmutableList ToImmutableList(this IEnumerable source) + { + return new ImmutableList(source); + } + + /// + /// 添加元素并返回新列表 + /// + public static ImmutableList AddItem(this ImmutableList list, T item) + { + return list.Add(item); + } + + /// + /// 添加多个元素并返回新列表 + /// + public static ImmutableList AddRangeItems(this ImmutableList list, IEnumerable items) + { + return list.AddRange(items); + } + + /// + /// 移除元素并返回新列表 + /// + public static ImmutableList RemoveItem(this ImmutableList list, T item) + { + return list.Remove(item); + } + + /// + /// 更新元素并返回新列表 + /// + public static ImmutableList SetItem(this ImmutableList list, int index, T item) + { + return list.SetItem(index, item); + } + + /// + /// 移除指定位置的元素并返回新列表 + /// + public static ImmutableList RemoveItemAt(this ImmutableList list, int index) + { + return list.RemoveAt(index); + } + + /// + /// 插入元素并返回新列表 + /// + public static ImmutableList InsertItem(this ImmutableList list, int index, T item) + { + return list.Insert(index, item); + } + } + + /// + /// 不可变列表 + /// + public sealed class ImmutableList : IReadOnlyList, IEquatable> + { + private readonly T[] _items; + + /// + /// 空列表 + /// + public static readonly ImmutableList Empty = new ImmutableList(); + + /// + /// 创建不可变列表 + /// + public ImmutableList() + { + _items = Array.Empty(); + } + + /// + /// 从集合创建不可变列表 + /// + public ImmutableList(IEnumerable items) + { + _items = items as T[] ?? new List(items).ToArray(); + } + + /// + /// 从数组创建不可变列表 + /// + public ImmutableList(T[] items) + { + _items = items ?? Array.Empty(); + } + + /// + /// 获取指定索引处的元素 + /// + public T this[int index] => _items[index]; + + /// + /// 元素数量 + /// + public int Count => _items.Length; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.Length == 0; + + /// + /// 添加元素并返回新列表 + /// + public ImmutableList Add(T item) + { + var newArray = new T[_items.Length + 1]; + Array.Copy(_items, newArray, _items.Length); + newArray[_items.Length] = item; + return new ImmutableList(newArray); + } + + /// + /// 添加多个元素并返回新列表 + /// + public ImmutableList AddRange(IEnumerable items) + { + var itemsList = new List(items); + var newArray = new T[_items.Length + itemsList.Count]; + Array.Copy(_items, newArray, _items.Length); + itemsList.CopyTo(newArray, _items.Length); + return new ImmutableList(newArray); + } + + /// + /// 移除元素并返回新列表 + /// + public ImmutableList Remove(T item) + { + var index = IndexOf(item); + return index >= 0 ? RemoveAt(index) : this; + } + + /// + /// 移除满足条件的元素并返回新列表 + /// + public ImmutableList RemoveAll(Predicate match) + { + var newList = new List(); + foreach (var item in _items) + { + if (!match(item)) + newList.Add(item); + } + return new ImmutableList(newList); + } + + /// + /// 移除指定位置的元素并返回新列表 + /// + public ImmutableList RemoveAt(int index) + { + if (index < 0 || index >= _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = new T[_items.Length - 1]; + Array.Copy(_items, 0, newArray, 0, index); + Array.Copy(_items, index + 1, newArray, index, _items.Length - index - 1); + return new ImmutableList(newArray); + } + + /// + /// 插入元素并返回新列表 + /// + public ImmutableList Insert(int index, T item) + { + if (index < 0 || index > _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = new T[_items.Length + 1]; + Array.Copy(_items, 0, newArray, 0, index); + newArray[index] = item; + Array.Copy(_items, index, newArray, index + 1, _items.Length - index); + return new ImmutableList(newArray); + } + + /// + /// 更新指定位置的元素并返回新列表 + /// + public ImmutableList SetItem(int index, T item) + { + if (index < 0 || index >= _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = (T[])_items.Clone(); + newArray[index] = item; + return new ImmutableList(newArray); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) + { + return Array.IndexOf(_items, item); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item, int startIndex) + { + return Array.IndexOf(_items, item, startIndex); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item, int startIndex, int count) + { + return Array.IndexOf(_items, item, startIndex, count); + } + + /// + /// 是否包含元素 + /// + public bool Contains(T item) + { + return IndexOf(item) >= 0; + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array) + { + _items.CopyTo(array, 0); + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) + { + _items.CopyTo(array, arrayIndex); + } + + /// + /// 转换为数组 + /// + public T[] ToArray() + { + return (T[])_items.Clone(); + } + + /// + /// 查找元素 + /// + public T? Find(Predicate match) + { + foreach (var item in _items) + { + if (match(item)) + return item; + } + return default; + } + + /// + /// 查找所有元素 + /// + public ImmutableList FindAll(Predicate match) + { + var result = new List(); + foreach (var item in _items) + { + if (match(item)) + result.Add(item); + } + return new ImmutableList(result); + } + + /// + /// 是否存在满足条件的元素 + /// + public bool Exists(Predicate match) + { + return FindIndex(match) >= 0; + } + + /// + /// 查找满足条件的元素索引 + /// + public int FindIndex(Predicate match) + { + for (int i = 0; i < _items.Length; i++) + { + if (match(_items[i])) + return i; + } + return -1; + } + + /// + /// 对每个元素执行操作 + /// + public void ForEach(Action action) + { + foreach (var item in _items) + { + action(item); + } + } + + /// + /// 转换元素类型 + /// + public ImmutableList ConvertAll(Converter converter) + { + var result = new TResult[_items.Length]; + for (int i = 0; i < _items.Length; i++) + { + result[i] = converter(_items[i]); + } + return new ImmutableList(result); + } + + /// + /// 获取范围 + /// + public ImmutableList GetRange(int index, int count) + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + if (index + count > _items.Length) + throw new ArgumentException("范围超出列表边界"); + + var result = new T[count]; + Array.Copy(_items, index, result, 0, count); + return new ImmutableList(result); + } + + #region IEnumerable + + public IEnumerator GetEnumerator() + { + return ((IEnumerable)_items).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _items.GetEnumerator(); + } + + #endregion + + #region IEquatable + + public bool Equals(ImmutableList? other) + { + if (other is null) + return false; + + if (ReferenceEquals(this, other)) + return true; + + if (_items.Length != other._items.Length) + return false; + + for (int i = 0; i < _items.Length; i++) + { + if (!EqualityComparer.Default.Equals(_items[i], other._items[i])) + return false; + } + + return true; + } + + public override bool Equals(object? obj) + { + return Equals(obj as ImmutableList); + } + + public override int GetHashCode() + { + var hash = new HashCode(); + foreach (var item in _items) + { + hash.Add(item); + } + return hash.ToHashCode(); + } + + public static bool operator ==(ImmutableList? left, ImmutableList? right) + { + return Equals(left, right); + } + + public static bool operator !=(ImmutableList? left, ImmutableList? right) + { + return !Equals(left, right); + } + + #endregion + + public override string ToString() + { + return $"[{string.Join(", ", _items)}]"; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/ObservableCollection.cs b/EasyTool.Core/CollectionsCategory/ObservableCollection.cs new file mode 100644 index 0000000..4964410 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ObservableCollection.cs @@ -0,0 +1,160 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Specialized; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 可观察集合 + /// 当集合发生变化时触发事件 + /// + /// 元素类型 + public class ObservableCollection : IList, INotifyCollectionChanged + { + private readonly List _items = new(); + + /// + /// 集合变化事件 + /// + public event NotifyCollectionChangedEventHandler? CollectionChanged; + + /// + /// 元素数量 + /// + public int Count => _items.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 获取或设置指定索引的元素 + /// + public T this[int index] + { + get => _items[index]; + set + { + var oldItem = _items[index]; + _items[index] = value; + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Replace, value, oldItem, index)); + } + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + _items.Add(item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, item, _items.Count - 1)); + } + + /// + /// 添加多个元素 + /// + public void AddRange(IEnumerable items) + { + var index = _items.Count; + var list = new List(items); + _items.AddRange(list); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, list, index)); + } + + /// + /// 插入元素 + /// + public void Insert(int index, T item) + { + _items.Insert(index, item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, item, index)); + } + + /// + /// 移除元素 + /// + public bool Remove(T item) + { + var index = _items.IndexOf(item); + if (index < 0) + return false; + + _items.RemoveAt(index); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, item, index)); + return true; + } + + /// + /// 移除指定位置的元素 + /// + public void RemoveAt(int index) + { + var item = _items[index]; + _items.RemoveAt(index); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, item, index)); + } + + /// + /// 清空集合 + /// + public void Clear() + { + _items.Clear(); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Reset)); + } + + /// + /// 移动元素 + /// + public void Move(int oldIndex, int newIndex) + { + var item = _items[oldIndex]; + _items.RemoveAt(oldIndex); + _items.Insert(newIndex, item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Move, item, newIndex, oldIndex)); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) => _items.IndexOf(item); + + /// + /// 是否包含元素 + /// + public bool Contains(T item) => _items.Contains(item); + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) => _items.CopyTo(array, arrayIndex); + + /// + /// 获取枚举器 + /// + public IEnumerator GetEnumerator() => _items.GetEnumerator(); + + /// + /// 获取枚举器 + /// + IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator(); + + /// + /// 触发集合变化事件 + /// + protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + { + CollectionChanged?.Invoke(this, e); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PagedList.cs b/EasyTool.Core/CollectionsCategory/PagedList.cs new file mode 100644 index 0000000..05be673 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/PagedList.cs @@ -0,0 +1,231 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 分页列表 + /// + /// 元素类型 + public class PagedList : IList + { + private readonly List _items; + + /// + /// 当前页号(从1开始) + /// + public int PageNumber { get; } + + /// + /// 每页大小 + /// + public int PageSize { get; } + + /// + /// 总记录数 + /// + public int TotalCount { get; } + + /// + /// 总页数 + /// + public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize); + + /// + /// 是否有上一页 + /// + public bool HasPreviousPage => PageNumber > 1; + + /// + /// 是否有下一页 + /// + public bool HasNextPage => PageNumber < TotalPages; + + /// + /// 是否是第一页 + /// + public bool IsFirstPage => PageNumber == 1; + + /// + /// 是否是最后一页 + /// + public bool IsLastPage => PageNumber == TotalPages; + + /// + /// 当前页记录数 + /// + public int Count => _items.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 获取或设置指定索引的元素 + /// + public T this[int index] + { + get => _items[index]; + set => _items[index] = value; + } + + /// + /// 创建分页列表 + /// + public PagedList(IEnumerable items, int pageNumber, int pageSize, int totalCount) + { + if (pageNumber < 1) + throw new ArgumentOutOfRangeException(nameof(pageNumber), "页号必须大于0"); + if (pageSize < 1) + throw new ArgumentOutOfRangeException(nameof(pageSize), "每页大小必须大于0"); + + _items = new List(items); + PageNumber = pageNumber; + PageSize = pageSize; + TotalCount = totalCount; + } + + /// + /// 从完整列表创建分页 + /// + public static PagedList Create(IEnumerable source, int pageNumber, int pageSize) + { + var list = new List(source); + var totalCount = list.Count; + var skip = (pageNumber - 1) * pageSize; + var items = list.Skip(skip).Take(pageSize); + return new PagedList(items, pageNumber, pageSize, totalCount); + } + + /// + /// 从查询创建分页 + /// + public static PagedList Create(IQueryable source, int pageNumber, int pageSize) + { + var totalCount = source.Count(); + var skip = (pageNumber - 1) * pageSize; + var items = source.Skip(skip).Take(pageSize).ToList(); + return new PagedList(items, pageNumber, pageSize, totalCount); + } + + /// + /// 获取页码范围 + /// + public IEnumerable GetPageRange(int displayCount = 5) + { + var start = Math.Max(1, PageNumber - displayCount / 2); + var end = Math.Min(TotalPages, start + displayCount - 1); + + if (end - start + 1 < displayCount) + { + start = Math.Max(1, end - displayCount + 1); + } + + for (int i = start; i <= end; i++) + { + yield return i; + } + } + + /// + /// 获取分页信息 + /// + public PageInfo GetPageInfo() + { + return new PageInfo + { + PageNumber = PageNumber, + PageSize = PageSize, + TotalCount = TotalCount, + TotalPages = TotalPages, + HasPreviousPage = HasPreviousPage, + HasNextPage = HasNextPage + }; + } + + #region IList 实现 + + public int IndexOf(T item) => _items.IndexOf(item); + + public void Insert(int index, T item) => _items.Insert(index, item); + + public void RemoveAt(int index) => _items.RemoveAt(index); + + public void Add(T item) => _items.Add(item); + + public void Clear() => _items.Clear(); + + public bool Contains(T item) => _items.Contains(item); + + public void CopyTo(T[] array, int arrayIndex) => _items.CopyTo(array, arrayIndex); + + public bool Remove(T item) => _items.Remove(item); + + public IEnumerator GetEnumerator() => _items.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator(); + + #endregion + } + + /// + /// 分页信息 + /// + public class PageInfo + { + /// + /// 当前页号 + /// + public int PageNumber { get; set; } + + /// + /// 每页大小 + /// + public int PageSize { get; set; } + + /// + /// 总记录数 + /// + public int TotalCount { get; set; } + + /// + /// 总页数 + /// + public int TotalPages { get; set; } + + /// + /// 是否有上一页 + /// + public bool HasPreviousPage { get; set; } + + /// + /// 是否有下一页 + /// + public bool HasNextPage { get; set; } + } + + /// + /// 分页工具类 + /// + public static class PagedListExtensions + { + /// + /// 转换为分页列表 + /// + public static PagedList ToPagedList(this IEnumerable source, int pageNumber, int pageSize) + { + return PagedList.Create(source, pageNumber, pageSize); + } + + /// + /// 转换为分页列表 + /// + public static PagedList ToPagedList(this IQueryable source, int pageNumber, int pageSize) + { + return PagedList.Create(source, pageNumber, pageSize); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs b/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs deleted file mode 100644 index c9cdf76..0000000 --- a/EasyTool.Core/CollectionsCategory/PriorityQueueUtil.cs +++ /dev/null @@ -1,324 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace EasyTool.CollectionsCategory -{ - /// - /// 优先队列工具类 - /// 基于 binary heap 实现的优先队列,支持自定义优先级比较器 - /// 兼容 netstandard2.1(.NET 6 才内置 PriorityQueue) - /// - public static class PriorityQueueUtil - { -#if NETSTANDARD2_1 - /// - /// 创建最小堆优先队列(最小值优先出队) - /// - public static PriorityQueue CreateMin() - where TPriority : IComparable - { - return new PriorityQueue(Comparer.Default); - } - - /// - /// 创建最大堆优先队列(最大值优先出队) - /// - public static PriorityQueue CreateMax() - where TPriority : IComparable - { - return new PriorityQueue(Comparer.Default, true); - } - - /// - /// 创建自定义比较器的优先队列 - /// - public static PriorityQueue Create( - IComparer comparer, bool maxHeap = false) - { - return new PriorityQueue(comparer, maxHeap); - } -#else - /// - /// 创建最小堆优先队列(最小值优先出队) - /// - public static System.Collections.Generic.PriorityQueue CreateMin() - where TPriority : IComparable - { - return new System.Collections.Generic.PriorityQueue(); - } - - /// - /// 创建最大堆优先队列(最大值优先出队) - /// - public static System.Collections.Generic.PriorityQueue CreateMax() - where TPriority : IComparable - { - // 使用反向比较器实现最大堆 - var comparer = Comparer.Default; - var reverseComparer = Comparer.Create((x, y) => comparer.Compare(y, x)); - return new System.Collections.Generic.PriorityQueue(reverseComparer); - } - - /// - /// 创建自定义比较器的优先队列 - /// - public static System.Collections.Generic.PriorityQueue Create( - IComparer comparer, bool maxHeap = false) - { - if (maxHeap) - { - var reverseComparer = Comparer.Create((x, y) => comparer.Compare(y, x)); - return new System.Collections.Generic.PriorityQueue(reverseComparer); - } - return new System.Collections.Generic.PriorityQueue(comparer); - } -#endif - } - -#if NETSTANDARD2_1 - /// - /// 优先队列实现(仅用于 netstandard2.1,.NET 6+ 使用内置实现) - /// - /// 元素类型 - /// 优先级类型 - public class PriorityQueue - { - private readonly List<(TElement Element, TPriority Priority)> _heap; - private readonly IComparer _comparer; - private readonly bool _isMaxHeap; - - /// - /// 元素数量 - /// - public int Count => _heap.Count; - - /// - /// 是否为空 - /// - public bool IsEmpty => _heap.Count == 0; - - /// - /// 创建优先队列 - /// - /// 优先级比较器 - /// 是否为最大堆(默认最小堆) - public PriorityQueue(IComparer comparer, bool maxHeap = false) - { - _heap = new List<(TElement, TPriority)>(); - _comparer = comparer ?? Comparer.Default; - _isMaxHeap = maxHeap; - } - - /// - /// 创建带初始容量的优先队列 - /// - public PriorityQueue(int initialCapacity, IComparer comparer, bool maxHeap = false) - { - _heap = new List<(TElement, TPriority)>(initialCapacity); - _comparer = comparer ?? Comparer.Default; - _isMaxHeap = maxHeap; - } - - /// - /// 入队 - /// - public void Enqueue(TElement element, TPriority priority) - { - _heap.Add((element, priority)); - SiftUp(_heap.Count - 1); - } - - /// - /// 批量入队 - /// - public void EnqueueRange(IEnumerable<(TElement Element, TPriority Priority)> items) - { - if (items == null) - throw new ArgumentNullException(nameof(items)); - - foreach (var item in items) - { - Enqueue(item.Element, item.Priority); - } - } - - /// - /// 出队(返回优先级最高/最低的元素) - /// - public TElement Dequeue() - { - if (_heap.Count == 0) - throw new InvalidOperationException("Queue is empty"); - - var result = _heap[0].Element; - int lastIndex = _heap.Count - 1; - - _heap[0] = _heap[lastIndex]; - _heap.RemoveAt(lastIndex); - - if (_heap.Count > 0) - { - SiftDown(0); - } - - return result; - } - - /// - /// 出队并返回元素和优先级 - /// - public (TElement Element, TPriority Priority) DequeueWithPriority() - { - if (_heap.Count == 0) - throw new InvalidOperationException("Queue is empty"); - - var result = _heap[0]; - int lastIndex = _heap.Count - 1; - - _heap[0] = _heap[lastIndex]; - _heap.RemoveAt(lastIndex); - - if (_heap.Count > 0) - { - SiftDown(0); - } - - return result; - } - - /// - /// 查看队首元素(不移除) - /// - public TElement Peek() - { - if (_heap.Count == 0) - throw new InvalidOperationException("Queue is empty"); - - return _heap[0].Element; - } - - /// - /// 查看队首元素和优先级(不移除) - /// - public (TElement Element, TPriority Priority) PeekWithPriority() - { - if (_heap.Count == 0) - throw new InvalidOperationException("Queue is empty"); - - return _heap[0]; - } - - /// - /// 尝试出队 - /// - public bool TryDequeue(out TElement element, out TPriority priority) - { - if (_heap.Count == 0) - { - element = default; - priority = default; - return false; - } - - var result = DequeueWithPriority(); - element = result.Element; - priority = result.Priority; - return true; - } - - /// - /// 尝试查看队首 - /// - public bool TryPeek(out TElement element, out TPriority priority) - { - if (_heap.Count == 0) - { - element = default; - priority = default; - return false; - } - - element = _heap[0].Element; - priority = _heap[0].Priority; - return true; - } - - /// - /// 清空队列 - /// - public void Clear() - { - _heap.Clear(); - } - - /// - /// 获取所有元素(不保证顺序) - /// - public IEnumerable UnorderedItems() - { - foreach (var item in _heap) - { - yield return item.Element; - } - } - - /// - /// 获取所有元素和优先级(不保证顺序) - /// - public IEnumerable<(TElement Element, TPriority Priority)> UnorderedItemsWithPriority() - { - return _heap; - } - - private void SiftUp(int index) - { - while (index > 0) - { - int parentIndex = (index - 1) / 2; - if (Compare(index, parentIndex) <= 0) - break; - - Swap(index, parentIndex); - index = parentIndex; - } - } - - private void SiftDown(int index) - { - int count = _heap.Count; - - while (true) - { - int leftChild = index * 2 + 1; - int rightChild = index * 2 + 2; - int extremeIndex = index; - - if (leftChild < count && Compare(leftChild, extremeIndex) > 0) - extremeIndex = leftChild; - - if (rightChild < count && Compare(rightChild, extremeIndex) > 0) - extremeIndex = rightChild; - - if (extremeIndex == index) - break; - - Swap(index, extremeIndex); - index = extremeIndex; - } - } - - private int Compare(int i, int j) - { - int result = _comparer.Compare(_heap[i].Priority, _heap[j].Priority); - return _isMaxHeap ? result : -result; - } - - private void Swap(int i, int j) - { - var temp = _heap[i]; - _heap[i] = _heap[j]; - _heap[j] = temp; - } - } -#endif -} diff --git a/EasyTool.Core/CollectionsCategory/QueueUtil.cs b/EasyTool.Core/CollectionsCategory/QueueUtil.cs index 68e72bf..c526866 100644 --- a/EasyTool.Core/CollectionsCategory/QueueUtil.cs +++ b/EasyTool.Core/CollectionsCategory/QueueUtil.cs @@ -1,143 +1,368 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace EasyTool.CollectionsCategory { /// - /// 队列工具类 + /// 线程安全队列工具类 + /// 提供生产者-消费者模式的队列操作 /// - public static class QueueUtil + /// 元素类型 + public class QueueUtil { + private readonly Queue _queue = new(); + private readonly object _lock = new(); + private readonly SemaphoreSlim _signal = new(0); + /// - /// 将指定元素添加到队列的末尾。 - /// [Obsolete("请直接使用 queue.Enqueue(item)")] + /// 获取队列元素数量 /// - /// 队列元素类型 - /// 队列 - /// 要添加的元素 - [Obsolete("请直接使用 queue.Enqueue(item)", false)] - public static void Enqueue(Queue queue, T item) + public int Count { - queue.Enqueue(item); + get + { + lock (_lock) + { + return _queue.Count; + } + } } /// - /// 将指定集合中的元素添加到队列的末尾。 + /// 检查队列是否为空 /// - /// 队列元素类型 - /// 队列 - /// 要添加到队列中的集合 - public static void EnqueueRange(Queue queue, IEnumerable collection) + public bool IsEmpty { - foreach (T item in collection) + get { - queue.Enqueue(item); + lock (_lock) + { + return _queue.Count == 0; + } } } /// - /// 移除并返回位于队列开头的元素。 - /// [Obsolete("请直接使用 queue.Dequeue()")] + /// 入队 /// - /// 队列元素类型 - /// 队列 - /// 队列开头的元素 - /// 队列为空时引发异常 - [Obsolete("请直接使用 queue.Dequeue()", false)] - public static T Dequeue(Queue queue) + /// 元素 + public void Enqueue(T item) { - return queue.Dequeue(); + lock (_lock) + { + _queue.Enqueue(item); + _signal.Release(); + } + } + + /// + /// 批量入队 + /// + /// 元素集合 + public void EnqueueRange(IEnumerable items) + { + lock (_lock) + { + foreach (var item in items) + { + _queue.Enqueue(item); + _signal.Release(); + } + } } /// - /// 返回位于队列开头的元素而不将其移除。 - /// [Obsolete("请直接使用 queue.Peek()")] + /// 出队 /// - /// 队列元素类型 - /// 队列 - /// 队列开头的元素 - /// 队列为空时引发异常 - [Obsolete("请直接使用 queue.Peek()", false)] - public static T Peek(Queue queue) + /// 元素 + public T? Dequeue() { - return queue.Peek(); + _signal.Wait(); + + lock (_lock) + { + return _queue.Count > 0 ? _queue.Dequeue() : default; + } } /// - /// 确定队列中是否包含指定元素。 - /// [Obsolete("请直接使用 queue.Contains(item)")] + /// 尝试出队 /// - /// 队列元素类型 - /// 队列 - /// 要查找的元素 - /// 如果队列包含指定元素,则为 true;否则为 false。 - [Obsolete("请直接使用 queue.Contains(item)", false)] - public static bool Contains(Queue queue, T item) + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) { - return queue.Contains(item); + lock (_lock) + { + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + _signal.Wait(0); + return true; + } + + item = default; + return false; + } } /// - /// 从队列中移除指定元素的第一个匹配项。 + /// 尝试出队(带超时) /// - /// 队列元素类型 - /// 队列 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(Queue queue, T item) + /// 超时时间 + /// 元素 + /// 是否成功 + public bool TryDequeue(TimeSpan timeout, out T? item) { - if (queue.Contains(item)) + if (_signal.Wait(timeout)) { - var newQueue = new Queue(queue.Where(x => !Equals(x, item))); - queue.Clear(); - foreach (var element in newQueue) + lock (_lock) { - queue.Enqueue(element); + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + return true; + } } - return true; } + + item = default; return false; } /// - /// 将队列中的所有元素复制到新数组中。 - /// [Obsolete("请直接使用 queue.ToArray()")] + /// 异步出队 + /// + /// 取消令牌 + /// 元素 + public async Task DequeueAsync(CancellationToken cancellationToken = default) + { + await _signal.WaitAsync(cancellationToken); + + lock (_lock) + { + return _queue.Count > 0 ? _queue.Dequeue() : default; + } + } + + /// + /// 异步尝试出队 + /// + /// 超时时间 + /// 取消令牌 + /// 元素或默认值 + public async Task<(bool Success, T? Item)> TryDequeueAsync(TimeSpan timeout, CancellationToken cancellationToken = default) + { + if (await _signal.WaitAsync(timeout, cancellationToken)) + { + lock (_lock) + { + if (_queue.Count > 0) + { + return (true, _queue.Dequeue()); + } + } + } + + return (false, default); + } + + /// + /// 查看队首元素(不出队) + /// + /// 队首元素 + public T? Peek() + { + lock (_lock) + { + return _queue.Count > 0 ? _queue.Peek() : default; + } + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 是否成功 + public bool TryPeek(out T? item) + { + lock (_lock) + { + if (_queue.Count > 0) + { + item = _queue.Peek(); + return true; + } + + item = default; + return false; + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + while (_signal.CurrentCount > 0) + { + _signal.Wait(0); + } + _queue.Clear(); + } + } + + /// + /// 获取所有元素(不出队) + /// + /// 元素数组 + public T[] ToArray() + { + lock (_lock) + { + return _queue.ToArray(); + } + } + + /// + /// 获取所有元素并清空队列 + /// + /// 元素数组 + public T[] Drain() + { + lock (_lock) + { + var items = _queue.ToArray(); + _queue.Clear(); + while (_signal.CurrentCount > 0) + { + _signal.Wait(0); + } + return items; + } + } + } + + /// + /// 优先级队列工具类 + /// + /// 元素类型 + public class PriorityQueue + { + private readonly SortedDictionary> _queues = new(); + private readonly object _lock = new(); + + /// + /// 获取元素数量 + /// + public int Count + { + get + { + lock (_lock) + { + return _queues.Sum(q => q.Value.Count); + } + } + } + + /// + /// 入队 /// - /// 队列元素类型 - /// 队列 - /// 包含队列中所有元素的新数组 - [Obsolete("请直接使用 queue.ToArray()", false)] - public static T[] ToArray(Queue queue) + /// 元素 + /// 优先级(数字越小优先级越高) + public void Enqueue(T item, int priority = 0) { - return queue.ToArray(); + lock (_lock) + { + if (!_queues.TryGetValue(priority, out var queue)) + { + queue = new Queue(); + _queues[priority] = queue; + } + + queue.Enqueue(item); + } + } + + /// + /// 出队 + /// + /// 元素 + public T? Dequeue() + { + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + return kvp.Value.Dequeue(); + } + } + + return default; + } + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) + { + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + item = kvp.Value.Dequeue(); + return true; + } + } + + item = default; + return false; + } } /// - /// 将队列中的所有元素复制到新数组中,从指定的索引开始。 - /// [Obsolete("请直接使用 queue.CopyTo(array, arrayIndex)")] + /// 查看队首元素 /// - /// 队列元素类型 - /// 队列 - /// 要复制到的目标数组 - /// 目标数组的起始索引 - [Obsolete("请直接使用 queue.CopyTo(array, arrayIndex)", false)] - public static void CopyTo(Queue queue, T[] array, int arrayIndex) + /// 元素 + public T? Peek() { - queue.CopyTo(array, arrayIndex); + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + return kvp.Value.Peek(); + } + } + + return default; + } } /// - /// 从队列中移除所有元素。 - /// [Obsolete("请直接使用 queue.Clear()")] + /// 清空队列 /// - /// 队列元素类型 - /// 队列 - [Obsolete("请直接使用 queue.Clear()", false)] - public static void Clear(Queue queue) + public void Clear() { - queue.Clear(); + lock (_lock) + { + _queues.Clear(); + } } } -} + +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/RingBuffer.cs b/EasyTool.Core/CollectionsCategory/RingBuffer.cs new file mode 100644 index 0000000..4e08c4d --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/RingBuffer.cs @@ -0,0 +1,319 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 环形缓冲区 + /// 线程安全,支持固定大小的循环队列 + /// + /// 元素类型 + public class RingBuffer + { + private readonly T[] _buffer; + private int _head; + private int _tail; + private int _count; + private readonly object _lock = new(); + + /// + /// 创建环形缓冲区 + /// + /// 容量 + public RingBuffer(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + + _buffer = new T[capacity]; + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 容量 + /// + public int Capacity => _buffer.Length; + + /// + /// 当前元素数量 + /// + public int Count + { + get { lock (_lock) { return _count; } } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 是否已满 + /// + public bool IsFull => Count == Capacity; + + /// + /// 添加元素 + /// + public void Add(T item) + { + lock (_lock) + { + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + + if (_count == Capacity) + { + // 缓冲区已满,覆盖最旧的元素 + _head = (_head + 1) % Capacity; + } + else + { + _count++; + } + } + } + + /// + /// 尝试添加元素(如果已满则返回false) + /// + public bool TryAdd(T item) + { + lock (_lock) + { + if (_count == Capacity) + return false; + + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + _count++; + return true; + } + } + + /// + /// 获取并移除最旧的元素 + /// + public T? Take() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + + var item = _buffer[_head]; + _buffer[_head] = default!; + _head = (_head + 1) % Capacity; + _count--; + return item; + } + } + + /// + /// 尝试获取并移除最旧的元素 + /// + public bool TryTake(out T? item) + { + lock (_lock) + { + if (_count == 0) + { + item = default; + return false; + } + + item = _buffer[_head]; + _buffer[_head] = default!; + _head = (_head + 1) % Capacity; + _count--; + return true; + } + } + + /// + /// 查看最旧的元素(不移除) + /// + public T? Peek() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + return _buffer[_head]; + } + } + + /// + /// 尝试查看最旧的元素 + /// + public bool TryPeek(out T? item) + { + lock (_lock) + { + if (_count == 0) + { + item = default; + return false; + } + item = _buffer[_head]; + return true; + } + } + + /// + /// 查看最新的元素(不移除) + /// + public T? PeekLatest() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + var index = (_tail - 1 + Capacity) % Capacity; + return _buffer[index]; + } + } + + /// + /// 获取指定索引的元素(从最旧的开始) + /// + public T? GetAt(int index) + { + lock (_lock) + { + if (index < 0 || index >= _count) + throw new ArgumentOutOfRangeException(nameof(index)); + return _buffer[(_head + index) % Capacity]; + } + } + + /// + /// 获取所有元素(从最旧到最新) + /// + public T[] ToArray() + { + lock (_lock) + { + var result = new T[_count]; + for (int i = 0; i < _count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + } + + /// + /// 清空缓冲区 + /// + public void Clear() + { + lock (_lock) + { + Array.Clear(_buffer, 0, Capacity); + _head = 0; + _tail = 0; + _count = 0; + } + } + + /// + /// 遍历所有元素 + /// + public IEnumerator GetEnumerator() + { + T[] array; + lock (_lock) + { + array = ToArray(); + } + foreach (var item in array) + { + yield return item; + } + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) + { + lock (_lock) + { + for (int i = 0; i < _count && arrayIndex + i < array.Length; i++) + { + array[arrayIndex + i] = _buffer[(_head + i) % Capacity]; + } + } + } + + /// + /// 查找元素 + /// + public bool Contains(T item) + { + lock (_lock) + { + for (int i = 0; i < _count; i++) + { + if (EqualityComparer.Default.Equals(_buffer[(_head + i) % Capacity], item)) + return true; + } + return false; + } + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) + { + lock (_lock) + { + for (int i = 0; i < _count; i++) + { + if (EqualityComparer.Default.Equals(_buffer[(_head + i) % Capacity], item)) + return i; + } + return -1; + } + } + + /// + /// 获取最新的N个元素 + /// + public T[] GetLatest(int count) + { + lock (_lock) + { + count = Math.Min(count, _count); + var result = new T[count]; + for (int i = 0; i < count; i++) + { + var index = (_tail - count + i + Capacity) % Capacity; + result[i] = _buffer[index]; + } + return result; + } + } + + /// + /// 获取最旧的N个元素 + /// + public T[] GetOldest(int count) + { + lock (_lock) + { + count = Math.Min(count, _count); + var result = new T[count]; + for (int i = 0; i < count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs b/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs deleted file mode 100644 index f915691..0000000 --- a/EasyTool.Core/CollectionsCategory/StatisticsUtil.cs +++ /dev/null @@ -1,723 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace EasyTool.CollectionsCategory -{ - /// - /// 统计工具类 - /// 提供常用的统计分析功能 - /// - public static class StatisticsUtil - { - /// - /// 计算中位数 - /// - public static double Median(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var sorted = values.OrderBy(x => x).ToList(); - if (sorted.Count == 0) - throw new ArgumentException("Collection is empty"); - - int count = sorted.Count; - int mid = count / 2; - - if (count % 2 == 0) - { - return (sorted[mid - 1] + sorted[mid]) / 2.0; - } - return sorted[mid]; - } - - /// - /// 计算中位数(泛型版本) - /// - public static double Median(IEnumerable values, Func selector) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (selector == null) - throw new ArgumentNullException(nameof(selector)); - - return Median(values.Select(selector)); - } - - /// - /// 计算百分位数 - /// - /// 数据集合 - /// 百分位数(0-100) - public static double Percentile(IEnumerable values, double percentile) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (percentile < 0 || percentile > 100) - throw new ArgumentOutOfRangeException(nameof(percentile), "Percentile must be between 0 and 100"); - - var sorted = values.OrderBy(x => x).ToList(); - if (sorted.Count == 0) - throw new ArgumentException("Collection is empty"); - - if (percentile == 0) - return sorted[0]; - if (percentile == 100) - return sorted[sorted.Count - 1]; - - double position = (sorted.Count - 1) * percentile / 100.0; - int lower = (int)Math.Floor(position); - int upper = (int)Math.Ceiling(position); - - if (lower == upper) - return sorted[lower]; - - return sorted[lower] + (position - lower) * (sorted[upper] - sorted[lower]); - } - - /// - /// 计算百分位数(泛型版本) - /// - public static double Percentile(IEnumerable values, Func selector, double percentile) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (selector == null) - throw new ArgumentNullException(nameof(selector)); - - return Percentile(values.Select(selector), percentile); - } - - /// - /// 计算四分位数 - /// - /// Q1, Q2(中位数), Q3 - public static (double Q1, double Q2, double Q3) Quartiles(IEnumerable values) - { - var sorted = values.OrderBy(x => x).ToList(); - if (sorted.Count == 0) - throw new ArgumentException("Collection is empty"); - - return ( - Percentile(sorted, 25), - Percentile(sorted, 50), - Percentile(sorted, 75) - ); - } - - /// - /// 计算标准差(总体标准差) - /// - public static double StandardDeviation(IEnumerable values) - { - return StandardDeviation(values, false); - } - - /// - /// 计算标准差 - /// - /// 数据集合 - /// 是否为样本标准差(使用 n-1) - public static double StandardDeviation(IEnumerable values, bool isSample) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - if (list.Count == 1 && isSample) - throw new ArgumentException("Sample standard deviation requires at least 2 values"); - - double mean = list.Average(); - double sumSquaredDiff = list.Sum(x => Math.Pow(x - mean, 2)); - int divisor = isSample ? list.Count - 1 : list.Count; - - return Math.Sqrt(sumSquaredDiff / divisor); - } - - /// - /// 计算标准差(泛型版本) - /// - public static double StandardDeviation(IEnumerable values, Func selector, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (selector == null) - throw new ArgumentNullException(nameof(selector)); - - return StandardDeviation(values.Select(selector), isSample); - } - - /// - /// 计算方差 - /// - /// 数据集合 - /// 是否为样本方差 - public static double Variance(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - if (list.Count == 1 && isSample) - throw new ArgumentException("Sample variance requires at least 2 values"); - - double mean = list.Average(); - double sumSquaredDiff = list.Sum(x => Math.Pow(x - mean, 2)); - int divisor = isSample ? list.Count - 1 : list.Count; - - return sumSquaredDiff / divisor; - } - - /// - /// 计算方差(泛型版本) - /// - public static double Variance(IEnumerable values, Func selector, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (selector == null) - throw new ArgumentNullException(nameof(selector)); - - return Variance(values.Select(selector), isSample); - } - - /// - /// 计算众数(出现次数最多的值) - /// - public static T Mode(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var groups = values.GroupBy(x => x).ToList(); - if (groups.Count == 0) - throw new ArgumentException("Collection is empty"); - - int maxCount = groups.Max(g => g.Count()); - var modes = groups.Where(g => g.Count() == maxCount).Select(g => g.Key).ToList(); - - if (modes.Count > 1) - throw new ArgumentException("Multiple modes exist"); - - return modes[0]; - } - - /// - /// 获取所有众数 - /// - public static List Modes(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var groups = values.GroupBy(x => x).ToList(); - if (groups.Count == 0) - throw new ArgumentException("Collection is empty"); - - int maxCount = groups.Max(g => g.Count()); - return groups.Where(g => g.Count() == maxCount).Select(g => g.Key).ToList(); - } - - /// - /// 计算频率分布 - /// - public static Dictionary Frequency(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - return values.GroupBy(x => x) - .ToDictionary(g => g.Key, g => g.Count()); - } - - /// - /// 计算相对频率分布 - /// - public static Dictionary RelativeFrequency(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - return list.GroupBy(x => x) - .ToDictionary(g => g.Key, g => (double)g.Count() / list.Count); - } - - /// - /// 计算累计频率分布 - /// - public static Dictionary CumulativeFrequency(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var freq = Frequency(values); - var sorted = freq.OrderBy(x => x.Key).ToList(); - var result = new Dictionary(); - int cumulative = 0; - - foreach (var kvp in sorted) - { - cumulative += kvp.Value; - result[kvp.Key] = cumulative; - } - - return result; - } - - /// - /// 计算范围(极差) - /// - public static double Range(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - return list.Max() - list.Min(); - } - - /// - /// 计算范围(泛型版本) - /// - public static double Range(IEnumerable values, Func selector) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (selector == null) - throw new ArgumentNullException(nameof(selector)); - - return Range(values.Select(selector)); - } - - /// - /// 计算四分位距(IQR) - /// - public static double InterquartileRange(IEnumerable values) - { - var (q1, q2, q3) = Quartiles(values); - return q3 - q1; - } - - /// - /// 计算变异系数(CV) - /// - public static double CoefficientOfVariation(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - double mean = list.Average(); - if (mean == 0) - throw new ArgumentException("Mean is zero, cannot calculate coefficient of variation"); - - return StandardDeviation(list, isSample) / Math.Abs(mean); - } - - /// - /// 计算偏度 - /// - public static double Skewness(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count < 3) - throw new ArgumentException("Skewness requires at least 3 values"); - - double mean = list.Average(); - double stdDev = StandardDeviation(list, isSample); - if (stdDev == 0) - return 0; - - int n = list.Count; - double skew = list.Sum(x => Math.Pow((x - mean) / stdDev, 3)); - - if (isSample) - { - return skew * n / ((n - 1) * (n - 2)); - } - return skew / n; - } - - /// - /// 计算峰度 - /// - public static double Kurtosis(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count < 4) - throw new ArgumentException("Kurtosis requires at least 4 values"); - - double mean = list.Average(); - double stdDev = StandardDeviation(list, isSample); - if (stdDev == 0) - return 0; - - int n = list.Count; - double kurt = list.Sum(x => Math.Pow((x - mean) / stdDev, 4)); - - if (isSample) - { - return kurt * n * (n + 1) / ((n - 1) * (n - 2) * (n - 3)) - 3.0 * (n - 1) * (n - 1) / ((n - 2) * (n - 3)); - } - return kurt / n - 3; - } - - /// - /// 计算协方差 - /// - public static double Covariance(IEnumerable x, IEnumerable y, bool isSample = false) - { - if (x == null) - throw new ArgumentNullException(nameof(x)); - if (y == null) - throw new ArgumentNullException(nameof(y)); - - var xList = x.ToList(); - var yList = y.ToList(); - - if (xList.Count != yList.Count) - throw new ArgumentException("Collections must have the same length"); - if (xList.Count == 0) - throw new ArgumentException("Collections are empty"); - if (xList.Count == 1 && isSample) - throw new ArgumentException("Sample covariance requires at least 2 values"); - - double meanX = xList.Average(); - double meanY = yList.Average(); - - double sum = 0; - for (int i = 0; i < xList.Count; i++) - { - sum += (xList[i] - meanX) * (yList[i] - meanY); - } - - int divisor = isSample ? xList.Count - 1 : xList.Count; - return sum / divisor; - } - - /// - /// 计算皮尔逊相关系数 - /// - public static double Correlation(IEnumerable x, IEnumerable y) - { - if (x == null) - throw new ArgumentNullException(nameof(x)); - if (y == null) - throw new ArgumentNullException(nameof(y)); - - var xList = x.ToList(); - var yList = y.ToList(); - - if (xList.Count != yList.Count) - throw new ArgumentException("Collections must have the same length"); - if (xList.Count < 2) - throw new ArgumentException("Correlation requires at least 2 values"); - - double stdDevX = StandardDeviation(xList, true); - double stdDevY = StandardDeviation(yList, true); - - if (stdDevX == 0 || stdDevY == 0) - return 0; - - return Covariance(xList, yList, true) / (stdDevX * stdDevY); - } - - /// - /// 计算几何平均数 - /// - public static double GeometricMean(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - if (list.Any(x => x <= 0)) - throw new ArgumentException("All values must be positive for geometric mean"); - - double logSum = list.Sum(x => Math.Log(x)); - return Math.Exp(logSum / list.Count); - } - - /// - /// 计算调和平均数 - /// - public static double HarmonicMean(IEnumerable values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - if (list.Any(x => x <= 0)) - throw new ArgumentException("All values must be positive for harmonic mean"); - - double sumReciprocals = list.Sum(x => 1.0 / x); - return list.Count / sumReciprocals; - } - - /// - /// 计算移动平均 - /// - public static List MovingAverage(IEnumerable values, int windowSize) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (windowSize <= 0) - throw new ArgumentOutOfRangeException(nameof(windowSize)); - - var list = values.ToList(); - if (list.Count < windowSize) - throw new ArgumentException("Window size cannot be larger than collection size"); - - var result = new List(); - double sum = 0; - - for (int i = 0; i < list.Count; i++) - { - sum += list[i]; - if (i >= windowSize) - { - sum -= list[i - windowSize]; - } - if (i >= windowSize - 1) - { - result.Add(sum / windowSize); - } - } - - return result; - } - - /// - /// 计算指数移动平均 - /// - public static List ExponentialMovingAverage(IEnumerable values, double alpha) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (alpha <= 0 || alpha > 1) - throw new ArgumentOutOfRangeException(nameof(alpha), "Alpha must be between 0 and 1"); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - var result = new List { list[0] }; - - for (int i = 1; i < list.Count; i++) - { - double ema = alpha * list[i] + (1 - alpha) * result[i - 1]; - result.Add(ema); - } - - return result; - } - - /// - /// 计算加权平均 - /// - public static double WeightedAverage(IEnumerable values, IEnumerable weights) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - if (weights == null) - throw new ArgumentNullException(nameof(weights)); - - var valueList = values.ToList(); - var weightList = weights.ToList(); - - if (valueList.Count != weightList.Count) - throw new ArgumentException("Values and weights must have the same length"); - if (valueList.Count == 0) - throw new ArgumentException("Collections are empty"); - - double sumWeighted = 0; - double sumWeights = 0; - - for (int i = 0; i < valueList.Count; i++) - { - sumWeighted += valueList[i] * weightList[i]; - sumWeights += weightList[i]; - } - - if (sumWeights == 0) - throw new ArgumentException("Sum of weights cannot be zero"); - - return sumWeighted / sumWeights; - } - - /// - /// 计算Z分数(标准化) - /// - public static List ZScore(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - double mean = list.Average(); - double stdDev = StandardDeviation(list, isSample); - - if (stdDev == 0) - return list.Select(_ => 0.0).ToList(); - - return list.Select(x => (x - mean) / stdDev).ToList(); - } - - /// - /// 计算百分等级 - /// - public static double PercentileRank(IEnumerable values, double value) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - int below = list.Count(x => x < value); - int equal = list.Count(x => x == value); - - return (below + 0.5 * equal) / list.Count * 100; - } - - /// - /// 计算描述性统计摘要 - /// - public static StatisticSummary Summary(IEnumerable values, bool isSample = false) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - var list = values.ToList(); - if (list.Count == 0) - throw new ArgumentException("Collection is empty"); - - var (q1, q2, q3) = Quartiles(list); - - return new StatisticSummary - { - Count = list.Count, - Min = list.Min(), - Max = list.Max(), - Range = list.Max() - list.Min(), - Sum = list.Sum(), - Mean = list.Average(), - Median = q2, - Mode = list.GroupBy(x => x).OrderByDescending(g => g.Count()).First().Key, - StandardDeviation = StandardDeviation(list, isSample), - Variance = Variance(list, isSample), - Q1 = q1, - Q3 = q3, - IQR = q3 - q1 - }; - } - } - - /// - /// 统计摘要 - /// - public class StatisticSummary - { - /// - /// 元素数量 - /// - public int Count { get; set; } - - /// - /// 最小值 - /// - public double Min { get; set; } - - /// - /// 最大值 - /// - public double Max { get; set; } - - /// - /// 范围(极差) - /// - public double Range { get; set; } - - /// - /// 总和 - /// - public double Sum { get; set; } - - /// - /// 平均值 - /// - public double Mean { get; set; } - - /// - /// 中位数 - /// - public double Median { get; set; } - - /// - /// 众数 - /// - public double Mode { get; set; } - - /// - /// 标准差 - /// - public double StandardDeviation { get; set; } - - /// - /// 方差 - /// - public double Variance { get; set; } - - /// - /// 第一四分位数 - /// - public double Q1 { get; set; } - - /// - /// 第三四分位数 - /// - public double Q3 { get; set; } - - /// - /// 四分位距 - /// - public double IQR { get; set; } - - /// - /// 返回字符串表示 - /// - public override string ToString() - { - return $"Count: {Count}, Min: {Min:F4}, Max: {Max:F4}, Mean: {Mean:F4}, Median: {Median:F4}, StdDev: {StandardDeviation:F4}"; - } - } -} diff --git a/EasyTool.Core/CollectionsCategory/TopKUtil.cs b/EasyTool.Core/CollectionsCategory/TopKUtil.cs index e819bd4..a883305 100644 --- a/EasyTool.Core/CollectionsCategory/TopKUtil.cs +++ b/EasyTool.Core/CollectionsCategory/TopKUtil.cs @@ -132,7 +132,7 @@ public static IEnumerable TopKUsingHeap(IEnumerable source, int k) wher return Enumerable.Empty(); #if NETSTANDARD2_1 - var minHeap = new PriorityQueue(Comparer.Default, false); + var minHeap = new PriorityQueue(Comparer.Default); foreach (var item in source) { diff --git a/EasyTool.Core/CollectionsCategory/TreeUtil.cs b/EasyTool.Core/CollectionsCategory/TreeUtil.cs index 368c24d..2003776 100644 --- a/EasyTool.Core/CollectionsCategory/TreeUtil.cs +++ b/EasyTool.Core/CollectionsCategory/TreeUtil.cs @@ -5,1233 +5,567 @@ namespace EasyTool.CollectionsCategory { /// - /// 树工具类 + /// 树节点接口 /// - public static class TreeUtil + /// 节点数据类型 + public interface ITreeNode where T : ITreeNode { /// - /// 创建通用树 + /// 节点ID /// - public static Tree Create(T value) - { - return new Tree(value); - } + string Id { get; } /// - /// 从层次结构创建树 + /// 父节点ID /// - public static Tree FromHierarchy( - IEnumerable items, - Func keySelector, - Func parentKeySelector, - TKey rootParentKey = default) where TKey : IEquatable - { - var itemDict = items.ToDictionary(keySelector); - var childrenDict = items.GroupBy(parentKeySelector).ToDictionary(g => g.Key, g => g.ToList()); - - T rootItem; - if (rootParentKey == null || rootParentKey.Equals(default)) - { - rootItem = items.FirstOrDefault(i => parentKeySelector(i) == null || parentKeySelector(i).Equals(default)); - } - else - { - rootItem = items.FirstOrDefault(i => parentKeySelector(i).Equals(rootParentKey)); - } + string? ParentId { get; } - if (rootItem == null) - throw new ArgumentException("Cannot find root item"); - - var root = new Tree(rootItem); - BuildTree(root, keySelector(rootItem), keySelector, childrenDict); - return root; - } - - private static void BuildTree( - Tree parent, - TKey parentKey, - Func keySelector, - Dictionary> childrenDict) where TKey : IEquatable - { - if (!childrenDict.TryGetValue(parentKey, out var children)) - return; + /// + /// 子节点列表 + /// + List Children { get; set; } + } - foreach (var child in children) - { - var childNode = parent.AddChild(child); - BuildTree(childNode, keySelector(child), keySelector, childrenDict); - } - } + /// + /// 树节点基类 + /// + public class TreeNodeBase : ITreeNode + { + public string Id { get; set; } = string.Empty; + public string? ParentId { get; set; } + public List Children { get; set; } = new(); } /// - /// 通用树节点 + /// 树形结构工具类 + /// 提供树形数据的构建、遍历、搜索等功能 /// - public class Tree + public static class TreeUtil { - private readonly List> _children; + #region 构建树 /// - /// 节点值 + /// 将扁平列表构建为树形结构 /// - public T Value { get; set; } + /// 节点类型 + /// 扁平列表 + /// ID选择器 + /// 父ID选择器 + /// 根节点的父ID值 + /// 树形结构的根节点列表 + public static List BuildTree(IEnumerable flatList, Func idSelector, Func parentIdSelector, string? rootParentId = null) + { + if (flatList == null) + return new List(); - /// - /// 父节点 - /// - public Tree Parent { get; private set; } + var lookup = flatList.ToLookup(parentIdSelector); + var roots = lookup[rootParentId].ToList(); - /// - /// 子节点 - /// - public IReadOnlyList> Children => _children; - - /// - /// 深度 - /// - public int Depth - { - get + void AddChildren(T parent) { - int depth = 0; - var current = Parent; - while (current != null) + var parentId = idSelector(parent); + var children = lookup[parentId]; + var childrenProperty = typeof(T).GetProperty("Children"); + + if (childrenProperty != null) { - depth++; - current = current.Parent; + var childrenList = childrenProperty.GetValue(parent); + if (childrenList == null) + { + childrenList = new List(); + childrenProperty.SetValue(parent, childrenList); + } + + var addMethod = childrenList.GetType().GetMethod("AddRange"); + addMethod?.Invoke(childrenList, new object[] { children }); + + foreach (var child in children) + { + AddChildren(child); + } } - return depth; } - } - /// - /// 高度 - /// - public int Height - { - get + foreach (var root in roots) { - if (_children.Count == 0) - return 0; - return 1 + _children.Max(c => c.Height); + AddChildren(root); } - } - /// - /// 是否为根节点 - /// - public bool IsRoot => Parent == null; - - /// - /// 是否为叶节点 - /// - public bool IsLeaf => _children.Count == 0; - - /// - /// 子节点数量 - /// - public int ChildCount => _children.Count; - - /// - /// 创建树节点 - /// - public Tree(T value) - { - Value = value; - _children = new List>(); + return roots; } /// - /// 添加子节点 + /// 将扁平列表构建为树形结构(使用 ITreeNode 接口) /// - public Tree AddChild(T value) + /// 节点类型 + /// 扁平列表 + /// 根节点的父ID值 + /// 树形结构的根节点列表 + public static List BuildTree(IEnumerable flatList, string? rootParentId = null) where T : ITreeNode { - var child = new Tree(value) { Parent = this }; - _children.Add(child); - return child; - } + if (flatList == null) + return new List(); - /// - /// 添加子节点 - /// - public void AddChild(Tree child) - { - child.Parent = this; - _children.Add(child); - } + var lookup = flatList.ToLookup(x => x.ParentId); + var roots = lookup[rootParentId].ToList(); - /// - /// 移除子节点 - /// - public bool RemoveChild(Tree child) - { - if (_children.Remove(child)) + void AddChildren(T parent) { - child.Parent = null; - return true; - } - return false; - } + var children = lookup[parent.Id].ToList(); + parent.Children = children; - /// - /// 清空子节点 - /// - public void ClearChildren() - { - foreach (var child in _children) - { - child.Parent = null; + foreach (var child in children) + { + AddChildren(child); + } } - _children.Clear(); - } - /// - /// 获取根节点 - /// - public Tree GetRoot() - { - var current = this; - while (current.Parent != null) + foreach (var root in roots) { - current = current.Parent; + AddChildren(root); } - return current; - } - /// - /// 获取所有祖先 - /// - public IEnumerable> GetAncestors() - { - var current = Parent; - while (current != null) - { - yield return current; - current = current.Parent; - } + return roots; } - /// - /// 获取所有后代 - /// - public IEnumerable> GetDescendants() - { - foreach (var child in _children) - { - yield return child; - foreach (var descendant in child.GetDescendants()) - { - yield return descendant; - } - } - } + #endregion + + #region 展平树 /// - /// 前序遍历 + /// 将树形结构展平为列表 /// - public IEnumerable> PreOrderTraversal() + /// 节点类型 + /// 根节点列表 + /// 扁平列表 + public static List Flatten(IEnumerable roots) where T : ITreeNode { - yield return this; - foreach (var child in _children) + var result = new List(); + + void FlattenNode(T node) { - foreach (var node in child.PreOrderTraversal()) + result.Add(node); + + if (node.Children != null) { - yield return node; + foreach (var child in node.Children) + { + FlattenNode(child); + } } } - } - /// - /// 后序遍历 - /// - public IEnumerable> PostOrderTraversal() - { - foreach (var child in _children) + foreach (var root in roots) { - foreach (var node in child.PostOrderTraversal()) - { - yield return node; - } + FlattenNode(root); } - yield return this; + + return result; } /// - /// 层序遍历(广度优先) + /// 将树形结构展平为列表(指定子节点选择器) /// - public IEnumerable> LevelOrderTraversal() + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 扁平列表 + public static List Flatten(IEnumerable roots, Func> childrenSelector) { - var queue = new Queue>(); - queue.Enqueue(this); + var result = new List(); - while (queue.Count > 0) + void FlattenNode(T node) { - var current = queue.Dequeue(); - yield return current; + result.Add(node); - foreach (var child in current._children) + var children = childrenSelector(node); + if (children != null) { - queue.Enqueue(child); + foreach (var child in children) + { + FlattenNode(child); + } } } - } - /// - /// 查找节点 - /// - public Tree Find(Func predicate) - { - if (predicate(Value)) - return this; - - foreach (var child in _children) + foreach (var root in roots) { - var found = child.Find(predicate); - if (found != null) - return found; + FlattenNode(root); } - return null; + return result; } - /// - /// 查找所有匹配节点 - /// - public IEnumerable> FindAll(Func predicate) - { - if (predicate(Value)) - yield return this; + #endregion - foreach (var child in _children) - { - foreach (var found in child.FindAll(predicate)) - { - yield return found; - } - } - } + #region 遍历树 /// - /// 获取路径 + /// 前序遍历(深度优先) /// - public List> GetPath() + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void PreOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode { - var path = new List>(); - var current = this; - while (current != null) + foreach (var root in roots) { - path.Insert(0, current); - current = current.Parent; + PreOrderTraversal(root, action); } - return path; } - } - /// - /// 二叉树工具类 - /// - public static class BinaryTreeUtil - { /// - /// 创建二叉树 + /// 前序遍历(深度优先) /// - public static BinaryTree Create(T value) + /// 节点类型 + /// 节点 + /// 访问操作 + public static void PreOrderTraversal(T node, Action action) where T : ITreeNode { - return new BinaryTree(value); - } + action(node); - /// - /// 从层序数组创建完全二叉树 - /// - public static BinaryTree FromArray(T[] values) where T : class - { - if (values == null || values.Length == 0 || values[0] == null) - return null; - - var root = new BinaryTree(values[0]); - var queue = new Queue>(); - queue.Enqueue(root); - - int i = 1; - while (queue.Count > 0 && i < values.Length) + if (node.Children != null) { - var current = queue.Dequeue(); - - if (i < values.Length && values[i] != null) + foreach (var child in node.Children) { - current.Left = new BinaryTree(values[i]) { Parent = current }; - queue.Enqueue(current.Left); + PreOrderTraversal(child, action); } - i++; - - if (i < values.Length && values[i] != null) - { - current.Right = new BinaryTree(values[i]) { Parent = current }; - queue.Enqueue(current.Right); - } - i++; } - - return root; } - } - - /// - /// 二叉树节点 - /// - public class BinaryTree - { - /// - /// 节点值 - /// - public T Value { get; set; } - - /// - /// 左子节点 - /// - public BinaryTree Left { get; set; } - - /// - /// 右子节点 - /// - public BinaryTree Right { get; set; } /// - /// 父节点 - /// - public BinaryTree Parent { get; set; } - - /// - /// 是否为叶节点 - /// - public bool IsLeaf => Left == null && Right == null; - - /// - /// 是否为根节点 - /// - public bool IsRoot => Parent == null; - - /// - /// 高度 + /// 后序遍历 /// - public int Height + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void PostOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode { - get + foreach (var root in roots) { - int leftHeight = Left?.Height ?? 0; - int rightHeight = Right?.Height ?? 0; - return 1 + Math.Max(leftHeight, rightHeight); + PostOrderTraversal(root, action); } } /// - /// 节点数量 + /// 后序遍历 /// - public int NodeCount + /// 节点类型 + /// 节点 + /// 访问操作 + public static void PostOrderTraversal(T node, Action action) where T : ITreeNode { - get + if (node.Children != null) { - int count = 1; - if (Left != null) count += Left.NodeCount; - if (Right != null) count += Right.NodeCount; - return count; + foreach (var child in node.Children) + { + PostOrderTraversal(child, action); + } } - } - - /// - /// 创建二叉树节点 - /// - public BinaryTree(T value) - { - Value = value; - } - /// - /// 前序遍历 - /// - public IEnumerable> PreOrderTraversal() - { - yield return this; - if (Left != null) - { - foreach (var node in Left.PreOrderTraversal()) - yield return node; - } - if (Right != null) - { - foreach (var node in Right.PreOrderTraversal()) - yield return node; - } + action(node); } /// - /// 中序遍历 + /// 层序遍历(广度优先) /// - public IEnumerable> InOrderTraversal() + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void LevelOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode { - if (Left != null) - { - foreach (var node in Left.InOrderTraversal()) - yield return node; - } - yield return this; - if (Right != null) - { - foreach (var node in Right.InOrderTraversal()) - yield return node; - } - } + var queue = new Queue(); - /// - /// 后序遍历 - /// - public IEnumerable> PostOrderTraversal() - { - if (Left != null) + foreach (var root in roots) { - foreach (var node in Left.PostOrderTraversal()) - yield return node; + queue.Enqueue(root); } - if (Right != null) - { - foreach (var node in Right.PostOrderTraversal()) - yield return node; - } - yield return this; - } - - /// - /// 层序遍历 - /// - public IEnumerable> LevelOrderTraversal() - { - var queue = new Queue>(); - queue.Enqueue(this); while (queue.Count > 0) { - var current = queue.Dequeue(); - yield return current; + var node = queue.Dequeue(); + action(node); - if (current.Left != null) - queue.Enqueue(current.Left); - if (current.Right != null) - queue.Enqueue(current.Right); + if (node.Children != null) + { + foreach (var child in node.Children) + { + queue.Enqueue(child); + } + } } } - /// - /// 反转二叉树 - /// - public void Invert() - { - var temp = Left; - Left = Right; - Right = temp; + #endregion - Left?.Invert(); - Right?.Invert(); - } + #region 搜索树 /// - /// 克隆 + /// 查找节点 /// - public BinaryTree Clone() + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 找到的节点 + public static T? Find(IEnumerable roots, Func predicate) where T : ITreeNode { - var clone = new BinaryTree(Value); - if (Left != null) - { - clone.Left = Left.Clone(); - clone.Left.Parent = clone; - } - if (Right != null) + foreach (var root in roots) { - clone.Right = Right.Clone(); - clone.Right.Parent = clone; + var result = Find(root, predicate); + if (result != null) + return result; } - return clone; + + return default; } /// - /// 获取指定深度的所有节点 + /// 查找节点 /// - public List> GetNodesAtDepth(int depth) + /// 节点类型 + /// 起始节点 + /// 查找条件 + /// 找到的节点 + public static T? Find(T node, Func predicate) where T : ITreeNode { - var result = new List>(); - GetNodesAtDepth(this, depth, 0, result); - return result; - } - - private static void GetNodesAtDepth(BinaryTree node, int targetDepth, int currentDepth, List> result) - { - if (node == null) - return; + if (predicate(node)) + return node; - if (currentDepth == targetDepth) + if (node.Children != null) { - result.Add(node); - return; + foreach (var child in node.Children) + { + var result = Find(child, predicate); + if (result != null) + return result; + } } - GetNodesAtDepth(node.Left, targetDepth, currentDepth + 1, result); - GetNodesAtDepth(node.Right, targetDepth, currentDepth + 1, result); + return default; } - } - /// - /// 二叉搜索树工具类 - /// - public static class BinarySearchTreeUtil - { /// - /// 创建二叉搜索树 + /// 查找所有匹配的节点 /// - public static BinarySearchTree Create() where T : IComparable + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 找到的节点列表 + public static List FindAll(IEnumerable roots, Func predicate) where T : ITreeNode { - return new BinarySearchTree(); - } + var result = new List(); - /// - /// 从集合创建二叉搜索树 - /// - public static BinarySearchTree FromEnumerable(IEnumerable items) where T : IComparable - { - var bst = new BinarySearchTree(); - foreach (var item in items) + foreach (var root in roots) { - bst.Add(item); + FindAll(root, predicate, result); } - return bst; - } - } - /// - /// 二叉搜索树 - /// - public class BinarySearchTree where T : IComparable - { - private BSTNode _root; - private int _count; - - /// - /// 节点数量 - /// - public int Count => _count; - - /// - /// 是否为空 - /// - public bool IsEmpty => _count == 0; - - /// - /// 最小值 - /// - public T Min - { - get - { - if (_root == null) - throw new InvalidOperationException("Tree is empty"); - return FindMin(_root).Value; - } - } - - /// - /// 最大值 - /// - public T Max - { - get - { - if (_root == null) - throw new InvalidOperationException("Tree is empty"); - return FindMax(_root).Value; - } + return result; } - private class BSTNode + private static void FindAll(T node, Func predicate, List result) where T : ITreeNode { - public T Value { get; set; } - public BSTNode Left { get; set; } - public BSTNode Right { get; set; } + if (predicate(node)) + result.Add(node); - public BSTNode(T value) + if (node.Children != null) { - Value = value; + foreach (var child in node.Children) + { + FindAll(child, predicate, result); + } } } /// - /// 添加元素 + /// 查找节点路径 /// - public void Add(T value) - { - _root = Add(_root, value); - } - - private BSTNode Add(BSTNode node, T value) + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 从根到目标的路径 + public static List FindPath(IEnumerable roots, Func predicate) where T : ITreeNode { - if (node == null) + foreach (var root in roots) { - _count++; - return new BSTNode(value); + var path = new List(); + if (FindPath(root, predicate, path)) + return path; } - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - node.Left = Add(node.Left, value); - else if (cmp > 0) - node.Right = Add(node.Right, value); - - return node; - } - - /// - /// 是否包含元素 - /// - public bool Contains(T value) - { - return Find(_root, value) != null; - } - - private BSTNode Find(BSTNode node, T value) - { - if (node == null) - return null; - - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - return Find(node.Left, value); - if (cmp > 0) - return Find(node.Right, value); - return node; + return new List(); } - /// - /// 移除元素 - /// - public bool Remove(T value) + private static bool FindPath(T node, Func predicate, List path) where T : ITreeNode { - int oldCount = _count; - _root = Remove(_root, value); - return _count < oldCount; - } + path.Add(node); - private BSTNode Remove(BSTNode node, T value) - { - if (node == null) - return null; + if (predicate(node)) + return true; - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - { - node.Left = Remove(node.Left, value); - } - else if (cmp > 0) + if (node.Children != null) { - node.Right = Remove(node.Right, value); - } - else - { - _count--; - if (node.Left == null) - return node.Right; - if (node.Right == null) - return node.Left; - - // 两个子节点都存在,用后继节点替换 - var successor = FindMin(node.Right); - node.Value = successor.Value; - node.Right = Remove(node.Right, successor.Value); - _count++; // 因为上面递归会再次减 + foreach (var child in node.Children) + { + if (FindPath(child, predicate, path)) + return true; + } } - return node; - } - - private BSTNode FindMin(BSTNode node) - { - while (node.Left != null) - node = node.Left; - return node; - } - - private BSTNode FindMax(BSTNode node) - { - while (node.Right != null) - node = node.Right; - return node; - } - - /// - /// 中序遍历 - /// - public IEnumerable InOrderTraversal() - { - return InOrderTraversal(_root); - } - - private IEnumerable InOrderTraversal(BSTNode node) - { - if (node == null) - yield break; - - foreach (var value in InOrderTraversal(node.Left)) - yield return value; - - yield return node.Value; - - foreach (var value in InOrderTraversal(node.Right)) - yield return value; - } - - /// - /// 清空 - /// - public void Clear() - { - _root = null; - _count = 0; - } - - /// - /// 查找小于指定值的最大元素 - /// - public T? Floor(T value) - { - var node = Floor(_root, value); - return node == null ? default : node.Value; + path.RemoveAt(path.Count - 1); + return false; } - private BSTNode Floor(BSTNode node, T value) - { - if (node == null) - return null; - - int cmp = value.CompareTo(node.Value); - if (cmp == 0) - return node; - if (cmp < 0) - return Floor(node.Left, value); + #endregion - var rightFloor = Floor(node.Right, value); - return rightFloor ?? node; - } + #region 树属性 /// - /// 查找大于指定值的最小元素 + /// 获取树的深度 /// - public T? Ceiling(T value) + /// 节点类型 + /// 根节点列表 + /// 最大深度 + public static int GetDepth(IEnumerable roots) where T : ITreeNode { - var node = Ceiling(_root, value); - return node == null ? default : node.Value; - } - - private BSTNode Ceiling(BSTNode node, T value) - { - if (node == null) - return null; - - int cmp = value.CompareTo(node.Value); - if (cmp == 0) - return node; - if (cmp > 0) - return Ceiling(node.Right, value); - - var leftCeiling = Ceiling(node.Left, value); - return leftCeiling ?? node; + return roots.Max(root => GetDepth(root)); } /// - /// 获取排名(小于指定值的元素数量) + /// 获取树的深度 /// - public int Rank(T value) - { - return Rank(_root, value); - } - - private int Rank(BSTNode node, T value) + /// 节点类型 + /// 节点 + /// 深度 + public static int GetDepth(T node) where T : ITreeNode { - if (node == null) - return 0; - - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - return Rank(node.Left, value); - if (cmp > 0) - return 1 + CountNodes(node.Left) + Rank(node.Right, value); - return CountNodes(node.Left); - } + if (node.Children == null || node.Children.Count == 0) + return 1; - private int CountNodes(BSTNode node) - { - if (node == null) - return 0; - return 1 + CountNodes(node.Left) + CountNodes(node.Right); + return 1 + node.Children.Max(child => GetDepth(child)); } - } - /// - /// 线段树工具类 - /// - public static class SegmentTreeUtil - { /// - /// 创建线段树(求和) + /// 获取节点数量 /// - public static SegmentTree Create(int[] values) + /// 节点类型 + /// 根节点列表 + /// 节点总数 + public static int GetNodeCount(IEnumerable roots) where T : ITreeNode { - return new SegmentTree(values, (a, b) => a + b, 0); + return Flatten(roots).Count; } /// - /// 创建线段树(自定义操作) - /// - public static SegmentTree Create(int[] values, Func operation, int identity) - { - return new SegmentTree(values, operation, identity); - } - } - - /// - /// 线段树(区间查询/更新) - /// - public class SegmentTree - { - private readonly int[] _tree; - private readonly int[] _lazy; - private readonly int _n; - private readonly Func _operation; - private readonly int _identity; - - /// - /// 元素数量 - /// - public int Count => _n; - - /// - /// 创建线段树 + /// 获取叶子节点数量 /// - public SegmentTree(int[] values, Func operation, int identity) + /// 节点类型 + /// 根节点列表 + /// 叶子节点数量 + public static int GetLeafCount(IEnumerable roots) where T : ITreeNode { - if (values == null || values.Length == 0) - throw new ArgumentException("Values cannot be null or empty"); - - _n = values.Length; - _operation = operation; - _identity = identity; - _tree = new int[4 * _n]; - _lazy = new int[4 * _n]; - - Build(values, 1, 0, _n - 1); - } - - private void Build(int[] values, int node, int start, int end) - { - if (start == end) - { - _tree[node] = values[start]; - } - else - { - int mid = (start + end) / 2; - Build(values, 2 * node, start, mid); - Build(values, 2 * node + 1, mid + 1, end); - _tree[node] = _operation(_tree[2 * node], _tree[2 * node + 1]); - } + return Flatten(roots).Count(node => node.Children == null || node.Children.Count == 0); } /// - /// 区间查询 + /// 获取所有叶子节点 /// - public int Query(int left, int right) + /// 节点类型 + /// 根节点列表 + /// 叶子节点列表 + public static List GetLeaves(IEnumerable roots) where T : ITreeNode { - if (left < 0 || right >= _n || left > right) - throw new ArgumentOutOfRangeException(); - return Query(1, 0, _n - 1, left, right); + return Flatten(roots).Where(node => node.Children == null || node.Children.Count == 0).ToList(); } - private int Query(int node, int start, int end, int left, int right) - { - if (right < start || left > end) - return _identity; + #endregion - if (left <= start && end <= right) - return _tree[node]; - - int mid = (start + end) / 2; - int leftResult = Query(2 * node, start, mid, left, right); - int rightResult = Query(2 * node + 1, mid + 1, end, left, right); - return _operation(leftResult, rightResult); - } + #region 树操作 /// - /// 单点更新 + /// 过滤树节点 /// - public void Update(int index, int value) + /// 节点类型 + /// 根节点列表 + /// 过滤条件 + /// 过滤后的树 + public static List Filter(IEnumerable roots, Func predicate) where T : ITreeNode, new() { - if (index < 0 || index >= _n) - throw new ArgumentOutOfRangeException(nameof(index)); - Update(1, 0, _n - 1, index, value); - } + var result = new List(); - private void Update(int node, int start, int end, int index, int value) - { - if (start == end) - { - _tree[node] = value; - } - else + foreach (var root in roots) { - int mid = (start + end) / 2; - if (index <= mid) - Update(2 * node, start, mid, index, value); - else - Update(2 * node + 1, mid + 1, end, index, value); - _tree[node] = _operation(_tree[2 * node], _tree[2 * node + 1]); + var filtered = FilterNode(root, predicate); + if (filtered != null) + result.Add(filtered); } - } - /// - /// 获取单个值 - /// - public int Get(int index) - { - return Query(index, index); - } - } - - /// - /// AVL树工具类 - /// - public static class AVLTreeUtil - { - /// - /// 创建AVL树 - /// - public static AVLTree Create() where T : IComparable - { - return new AVLTree(); + return result; } - } - - /// - /// AVL树(自平衡二叉搜索树) - /// - public class AVLTree where T : IComparable - { - private AVLNode _root; - private int _count; - private class AVLNode + private static T? FilterNode(T node, Func predicate) where T : ITreeNode, new() { - public T Value { get; set; } - public AVLNode Left { get; set; } - public AVLNode Right { get; set; } - public int Height { get; set; } + var filteredChildren = new List(); - public AVLNode(T value) + if (node.Children != null) { - Value = value; - Height = 1; + foreach (var child in node.Children) + { + var filtered = FilterNode(child, predicate); + if (filtered != null) + filteredChildren.Add(filtered); + } } - public int BalanceFactor => GetHeight(Left) - GetHeight(Right); - - private static int GetHeight(AVLNode node) => node?.Height ?? 0; - } - - /// - /// 节点数量 - /// - public int Count => _count; - - /// - /// 是否为空 - /// - public bool IsEmpty => _count == 0; - - /// - /// 添加元素 - /// - public void Add(T value) - { - _root = Add(_root, value); - } - - private AVLNode Add(AVLNode node, T value) - { - if (node == null) + if (predicate(node) || filteredChildren.Count > 0) { - _count++; - return new AVLNode(value); + node.Children = filteredChildren; + return node; } - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - node.Left = Add(node.Left, value); - else if (cmp > 0) - node.Right = Add(node.Right, value); - else - return node; // 重复值不添加 - - UpdateHeight(node); - return Balance(node); - } - - /// - /// 是否包含元素 - /// - public bool Contains(T value) - { - return Contains(_root, value); - } - - private bool Contains(AVLNode node, T value) - { - if (node == null) - return false; - - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - return Contains(node.Left, value); - if (cmp > 0) - return Contains(node.Right, value); - return true; + return default; } /// - /// 移除元素 + /// 映射树节点 /// - public bool Remove(T value) + /// 源节点类型 + /// 结果节点类型 + /// 根节点列表 + /// 映射函数 + /// 映射后的树 + public static List Map(IEnumerable roots, Func selector) + where TSource : ITreeNode + where TResult : ITreeNode, new() { - int oldCount = _count; - _root = Remove(_root, value); - return _count < oldCount; - } + var result = new List(); - private AVLNode Remove(AVLNode node, T value) - { - if (node == null) - return null; - - int cmp = value.CompareTo(node.Value); - if (cmp < 0) - { - node.Left = Remove(node.Left, value); - } - else if (cmp > 0) + foreach (var root in roots) { - node.Right = Remove(node.Right, value); + result.Add(MapNode(root, selector)); } - else - { - _count--; - if (node.Left == null) - return node.Right; - if (node.Right == null) - return node.Left; - - var successor = FindMin(node.Right); - node.Value = successor.Value; - node.Right = Remove(node.Right, successor.Value); - _count++; - } - - UpdateHeight(node); - return Balance(node); - } - - private AVLNode FindMin(AVLNode node) - { - while (node.Left != null) - node = node.Left; - return node; - } - private void UpdateHeight(AVLNode node) - { - int leftHeight = node.Left?.Height ?? 0; - int rightHeight = node.Right?.Height ?? 0; - node.Height = 1 + Math.Max(leftHeight, rightHeight); + return result; } - private AVLNode Balance(AVLNode node) + private static TResult MapNode(TSource node, Func selector) + where TSource : ITreeNode + where TResult : ITreeNode, new() { - int balance = node.BalanceFactor; - - // 左重 - if (balance > 1) - { - if (node.Left.BalanceFactor < 0) - node.Left = RotateLeft(node.Left); - return RotateRight(node); - } + var result = selector(node); + result.Children = new List(); - // 右重 - if (balance < -1) + if (node.Children != null) { - if (node.Right.BalanceFactor > 0) - node.Right = RotateRight(node.Right); - return RotateLeft(node); + foreach (var child in node.Children) + { + result.Children.Add(MapNode(child, selector)); + } } - return node; - } - - private AVLNode RotateRight(AVLNode y) - { - var x = y.Left; - y.Left = x.Right; - x.Right = y; - - UpdateHeight(y); - UpdateHeight(x); - - return x; - } - - private AVLNode RotateLeft(AVLNode x) - { - var y = x.Right; - x.Right = y.Left; - y.Left = x; - - UpdateHeight(x); - UpdateHeight(y); - - return y; - } - - /// - /// 中序遍历 - /// - public IEnumerable InOrderTraversal() - { - return InOrderTraversal(_root); - } - - private IEnumerable InOrderTraversal(AVLNode node) - { - if (node == null) - yield break; - - foreach (var value in InOrderTraversal(node.Left)) - yield return value; - - yield return node.Value; - - foreach (var value in InOrderTraversal(node.Right)) - yield return value; + return result; } - /// - /// 清空 - /// - public void Clear() - { - _root = null; - _count = 0; - } + #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/WeightedSelector.cs b/EasyTool.Core/CollectionsCategory/WeightedSelector.cs new file mode 100644 index 0000000..44e224e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/WeightedSelector.cs @@ -0,0 +1,279 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 带权重的选择器 + /// 根据权重随机选择元素 + /// + /// 元素类型 + public class WeightedSelector + { + private readonly List> _items = new(); + private readonly Random _random; + private double _totalWeight; + private readonly object _lock = new(); + + /// + /// 创建权重选择器 + /// + public WeightedSelector() + { + _random = new Random(); + _totalWeight = 0; + } + + /// + /// 创建权重选择器(指定随机种子) + /// + public WeightedSelector(int seed) + { + _random = new Random(seed); + _totalWeight = 0; + } + + /// + /// 元素数量 + /// + public int Count => _items.Count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.Count == 0; + + /// + /// 总权重 + /// + public double TotalWeight => _totalWeight; + + /// + /// 添加元素 + /// + /// 元素 + /// 权重(必须大于0) + public void Add(T item, double weight) + { + if (weight <= 0) + throw new ArgumentException("权重必须大于0", nameof(weight)); + + lock (_lock) + { + _items.Add(new WeightedItem(item, weight, _totalWeight)); + _totalWeight += weight; + } + } + + /// + /// 添加多个元素 + /// + public void AddRange(IEnumerable<(T Item, double Weight)> items) + { + foreach (var (item, weight) in items) + { + Add(item, weight); + } + } + + /// + /// 移除元素 + /// + public bool Remove(T item) + { + lock (_lock) + { + var index = _items.FindIndex(i => EqualityComparer.Default.Equals(i.Item, item)); + if (index < 0) + return false; + + var removed = _items[index]; + _items.RemoveAt(index); + _totalWeight -= removed.Weight; + + // 重新计算累计权重 + var cumulative = 0.0; + foreach (var i in _items) + { + cumulative += i.Weight; + } + + return true; + } + } + + /// + /// 清空所有元素 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + _totalWeight = 0; + } + } + + /// + /// 根据权重随机选择一个元素 + /// + public T? Select() + { + lock (_lock) + { + if (_items.Count == 0) + return default; + + var value = _random.NextDouble() * _totalWeight; + + foreach (var item in _items) + { + if (value < item.CumulativeWeight + item.Weight) + return item.Item; + } + + return _items[^1].Item; + } + } + + /// + /// 根据权重随机选择多个元素(可重复) + /// + public List SelectMultiple(int count) + { + var result = new List(); + for (int i = 0; i < count; i++) + { + var item = Select(); + if (item != null) + result.Add(item); + } + return result; + } + + /// + /// 根据权重随机选择多个不重复元素 + /// + public List SelectDistinct(int count) + { + lock (_lock) + { + if (count >= _items.Count) + return _items.ConvertAll(i => i.Item); + + var result = new List(); + var tempItems = new List>(_items); + var tempTotalWeight = _totalWeight; + + while (result.Count < count && tempItems.Count > 0) + { + var value = _random.NextDouble() * tempTotalWeight; + double cumulative = 0; + + for (int i = 0; i < tempItems.Count; i++) + { + cumulative += tempItems[i].Weight; + if (value < cumulative) + { + result.Add(tempItems[i].Item); + tempTotalWeight -= tempItems[i].Weight; + tempItems.RemoveAt(i); + break; + } + } + } + + return result; + } + } + + /// + /// 获取元素权重 + /// + public double GetWeight(T item) + { + var found = _items.Find(i => EqualityComparer.Default.Equals(i.Item, item)); + return found?.Weight ?? 0; + } + + /// + /// 设置元素权重 + /// + public bool SetWeight(T item, double newWeight) + { + if (newWeight <= 0) + throw new ArgumentException("权重必须大于0", nameof(newWeight)); + + lock (_lock) + { + var index = _items.FindIndex(i => EqualityComparer.Default.Equals(i.Item, item)); + if (index < 0) + return false; + + var oldWeight = _items[index].Weight; + _totalWeight = _totalWeight - oldWeight + newWeight; + + var cumulative = 0.0; + foreach (var i in _items) + { + if (i == _items[index]) + { + _items[index] = new WeightedItem(item, newWeight, cumulative); + cumulative += newWeight; + } + else + { + cumulative += i.Weight; + } + } + + return true; + } + } + + /// + /// 获取选择概率 + /// + public double GetProbability(T item) + { + if (_totalWeight == 0) + return 0; + + var weight = GetWeight(item); + return weight / _totalWeight; + } + + /// + /// 获取所有元素及其权重 + /// + public IEnumerable<(T Item, double Weight, double Probability)> GetAll() + { + lock (_lock) + { + foreach (var item in _items) + { + var probability = _totalWeight > 0 ? item.Weight / _totalWeight : 0; + yield return (item.Item, item.Weight, probability); + } + } + } + } + + /// + /// 带权重的元素 + /// + internal class WeightedItem + { + public T Item { get; } + public double Weight { get; } + public double CumulativeWeight { get; } + + public WeightedItem(T item, double weight, double cumulativeWeight) + { + Item = item; + Weight = weight; + CumulativeWeight = cumulativeWeight; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/ConvertUtil.cs b/EasyTool.Core/ConvertCategory/ConvertUtil.cs new file mode 100644 index 0000000..aefbd26 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/ConvertUtil.cs @@ -0,0 +1,410 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Text.Json; + +namespace EasyTool.ConvertCategory +{ + /// + /// 类型转换工具类 + /// + public static class ConvertUtil + { + #region 基础类型转换 + + /// + /// 转换为整数 + /// + public static int ToInt(object? value, int defaultValue = 0) + { + if (value == null) return defaultValue; + + if (value is int i) return i; + if (value is long l) return (int)l; + if (value is double d) return (int)d; + if (value is decimal dec) return (int)dec; + if (value is float f) return (int)f; + if (value is bool b) return b ? 1 : 0; + if (value is string s) + { + return int.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + /// + /// 转换为长整数 + /// + public static long ToLong(object? value, long defaultValue = 0) + { + if (value == null) return defaultValue; + + if (value is long l) return l; + if (value is int i) return i; + if (value is double d) return (long)d; + if (value is decimal dec) return (long)dec; + if (value is float f) return (long)f; + if (value is string s) + { + return long.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + /// + /// 转换为浮点数 + /// + public static double ToDouble(object? value, double defaultValue = 0) + { + if (value == null) return defaultValue; + + if (value is double d) return d; + if (value is float f) return f; + if (value is decimal dec) return (double)dec; + if (value is int i) return i; + if (value is long l) return l; + if (value is string s) + { + return double.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + /// + /// 转换为小数 + /// + public static decimal ToDecimal(object? value, decimal defaultValue = 0) + { + if (value == null) return defaultValue; + + if (value is decimal dec) return dec; + if (value is double d) return (decimal)d; + if (value is float f) return (decimal)f; + if (value is int i) return i; + if (value is long l) return l; + if (value is string s) + { + return decimal.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + /// + /// 转换为布尔值 + /// + public static bool ToBool(object? value, bool defaultValue = false) + { + if (value == null) return defaultValue; + + if (value is bool b) return b; + if (value is int i) return i != 0; + if (value is long l) return l != 0; + if (value is string s) + { + if (string.IsNullOrEmpty(s)) return defaultValue; + + var lower = s.ToLowerInvariant(); + return lower is "true" or "1" or "yes" or "y" or "on"; + } + + return defaultValue; + } + + /// + /// 转换为字符串 + /// + public static string ToString(object? value, string defaultValue = "") + { + if (value == null) return defaultValue; + + return value.ToString() ?? defaultValue; + } + + /// + /// 转换为日期时间 + /// + public static DateTime ToDateTime(object? value, DateTime defaultValue = default) + { + if (value == null) return defaultValue; + + if (value is DateTime dt) return dt; + if (value is long ticks) return new DateTime(ticks); + if (value is string s) + { + return DateTime.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + /// + /// 转换为Guid + /// + public static Guid ToGuid(object? value, Guid defaultValue = default) + { + if (value == null) return defaultValue; + + if (value is Guid g) return g; + if (value is string s) + { + return Guid.TryParse(s, out var result) ? result : defaultValue; + } + + return defaultValue; + } + + #endregion + + #region 进制转换 + + /// + /// 十进制转二进制 + /// + public static string ToBinary(long value) + { + return Convert.ToString(value, 2); + } + + /// + /// 二进制转十进制 + /// + public static long FromBinary(string binary) + { + return Convert.ToInt64(binary, 2); + } + + /// + /// 十进制转八进制 + /// + public static string ToOctal(long value) + { + return Convert.ToString(value, 8); + } + + /// + /// 八进制转十进制 + /// + public static long FromOctal(string octal) + { + return Convert.ToInt64(octal, 8); + } + + /// + /// 十进制转十六进制 + /// + public static string ToHex(long value) + { + return Convert.ToString(value, 16); + } + + /// + /// 十六进制转十进制 + /// + public static long FromHex(string hex) + { + return Convert.ToInt64(hex, 16); + } + + /// + /// 字节数组转十六进制字符串 + /// + public static string BytesToHex(byte[] bytes, bool upperCase = false) + { + var format = upperCase ? "X2" : "x2"; + var sb = new StringBuilder(bytes.Length * 2); + foreach (var b in bytes) + { + sb.Append(b.ToString(format)); + } + return sb.ToString(); + } + + /// + /// 十六进制字符串转字节数组 + /// + public static byte[] HexToBytes(string hex) + { + if (hex.Length % 2 != 0) + hex = "0" + hex; + + var bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return bytes; + } + + #endregion + + #region 编码转换 + + /// + /// 字符串转Base64 + /// + public static string ToBase64(string value, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return Convert.ToBase64String(encoding.GetBytes(value)); + } + + /// + /// Base64转字符串 + /// + public static string FromBase64(string base64, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return encoding.GetString(Convert.FromBase64String(base64)); + } + + /// + /// 字节数组转Base64 + /// + public static string BytesToBase64(byte[] bytes) + { + return Convert.ToBase64String(bytes); + } + + /// + /// Base64转字节数组 + /// + public static byte[] Base64ToBytes(string base64) + { + return Convert.FromBase64String(base64); + } + + #endregion + + #region 集合转换 + + /// + /// 字符串数组转整数数组 + /// + public static int[] ToIntArray(string[] values, int defaultValue = 0) + { + return values?.Select(v => ToInt(v, defaultValue)).ToArray() ?? Array.Empty(); + } + + /// + /// 整数数组转字符串数组 + /// + public static string[] ToStringArray(int[] values) + { + return values?.Select(v => v.ToString()).ToArray() ?? Array.Empty(); + } + + /// + /// 字典转查询字符串 + /// + public static string DictionaryToQueryString(Dictionary dict) + { + if (dict == null || dict.Count == 0) + return string.Empty; + + var parts = new List(); + foreach (var kvp in dict) + { + if (kvp.Value != null) + { + parts.Add($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"); + } + } + return string.Join("&", parts); + } + + /// + /// 查询字符串转字典 + /// + public static Dictionary QueryStringToDictionary(string query) + { + var result = new Dictionary(); + + if (string.IsNullOrEmpty(query)) + return result; + + if (query.StartsWith("?")) + query = query.Substring(1); + + foreach (var part in query.Split('&')) + { + var index = part.IndexOf('='); + if (index > 0) + { + var key = Uri.UnescapeDataString(part.Substring(0, index)); + var value = Uri.UnescapeDataString(part.Substring(index + 1)); + result[key] = value; + } + } + + return result; + } + + /// + /// 对象转字典 + /// + public static Dictionary ObjectToDictionary(object obj) + { + if (obj == null) + return new Dictionary(); + + if (obj is Dictionary dict) + return dict; + + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize>(json) ?? new Dictionary(); + } + + /// + /// 字典转对象 + /// + public static T? DictionaryToObject(Dictionary dict) + { + if (dict == null) + return default; + + var json = JsonSerializer.Serialize(dict); + return JsonSerializer.Deserialize(json); + } + + #endregion + + #region 类型判断 + + /// + /// 是否为数值类型 + /// + public static bool IsNumericType(Type type) + { + return type == typeof(int) || type == typeof(long) || type == typeof(short) || + type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) || + type == typeof(ushort) || type == typeof(sbyte) || type == typeof(float) || + type == typeof(double) || type == typeof(decimal); + } + + /// + /// 是否为整数类型 + /// + public static bool IsIntegerType(Type type) + { + return type == typeof(int) || type == typeof(long) || type == typeof(short) || + type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) || + type == typeof(ushort) || type == typeof(sbyte); + } + + /// + /// 是否为浮点类型 + /// + public static bool IsFloatType(Type type) + { + return type == typeof(float) || type == typeof(double) || type == typeof(decimal); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs b/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs new file mode 100644 index 0000000..9133fb3 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs @@ -0,0 +1,348 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Text; + +namespace EasyTool.ConvertCategory +{ + /// + /// CSV转换工具类 + /// + public static class CsvConvertUtil + { + /// + /// 对象列表转CSV字符串 + /// + public static string ToCsv(IEnumerable list, bool includeHeader = true, char separator = ',') + { + var properties = typeof(T).GetProperties(); + var sb = new StringBuilder(); + + // 添加表头 + if (includeHeader) + { + var headers = new List(); + foreach (var prop in properties) + { + headers.Add(EscapeCsvField(prop.Name, separator)); + } + sb.AppendLine(string.Join(separator, headers)); + } + + // 添加数据行 + foreach (var item in list) + { + var values = new List(); + foreach (var prop in properties) + { + var value = prop.GetValue(item)?.ToString() ?? ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV字符串转对象列表 + /// + public static List FromCsv(string csv, bool hasHeader = true, char separator = ',') where T : new() + { + var result = new List(); + var properties = typeof(T).GetProperties(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return result; + + var startIndex = hasHeader ? 1 : 0; + var headers = hasHeader ? ParseCsvLine(lines[0], separator) : null; + + // 构建属性映射 + var propMap = new Dictionary(); + if (headers != null) + { + for (int i = 0; i < headers.Count; i++) + { + var header = headers[i].Trim(); + foreach (var prop in properties) + { + if (prop.Name.Equals(header, StringComparison.OrdinalIgnoreCase)) + { + propMap[prop.Name] = i; + break; + } + } + } + } + + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var item = new T(); + + for (int j = 0; j < properties.Length && j < values.Count; j++) + { + var prop = properties[j]; + var index = headers != null && propMap.TryGetValue(prop.Name, out var mapIndex) ? mapIndex : j; + + if (index < values.Count) + { + var value = UnescapeCsvField(values[index]); + if (!string.IsNullOrEmpty(value)) + { + var convertedValue = Convert.ChangeType(value, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); + prop.SetValue(item, convertedValue); + } + } + } + + result.Add(item); + } + + return result; + } + + /// + /// DataTable转CSV + /// + public static string ToCsv(DataTable table, bool includeHeader = true, char separator = ',') + { + var sb = new StringBuilder(); + + // 添加表头 + if (includeHeader) + { + var headers = new List(); + foreach (DataColumn col in table.Columns) + { + headers.Add(EscapeCsvField(col.ColumnName, separator)); + } + sb.AppendLine(string.Join(separator, headers)); + } + + // 添加数据行 + foreach (DataRow row in table.Rows) + { + var values = new List(); + foreach (DataColumn col in table.Columns) + { + var value = row[col]?.ToString() ?? ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV转DataTable + /// + public static DataTable FromCsv(string csv, bool hasHeader = true, char separator = ',') + { + var table = new DataTable(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return table; + + // 解析第一行获取列数 + var firstLine = ParseCsvLine(lines[0], separator); + + // 创建列 + if (hasHeader) + { + foreach (var header in firstLine) + { + table.Columns.Add(UnescapeCsvField(header)); + } + } + else + { + for (int i = 0; i < firstLine.Count; i++) + { + table.Columns.Add($"Column{i + 1}"); + } + } + + // 添加数据行 + var startIndex = hasHeader ? 1 : 0; + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var row = table.NewRow(); + + for (int j = 0; j < Math.Min(values.Count, table.Columns.Count); j++) + { + row[j] = UnescapeCsvField(values[j]); + } + + table.Rows.Add(row); + } + + return table; + } + + /// + /// 字典列表转CSV + /// + public static string ToCsv(IEnumerable> dicts, bool includeHeader = true, char separator = ',') + { + var sb = new StringBuilder(); + var headers = new List(); + var isFirst = true; + + foreach (var dict in dicts) + { + if (isFirst) + { + headers.AddRange(dict.Keys); + if (includeHeader) + { + var headerLine = new List(); + foreach (var header in headers) + { + headerLine.Add(EscapeCsvField(header, separator)); + } + sb.AppendLine(string.Join(separator, headerLine)); + } + isFirst = false; + } + + var values = new List(); + foreach (var header in headers) + { + var value = dict.TryGetValue(header, out var v) ? v?.ToString() ?? "" : ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV转字典列表 + /// + public static List> ToDictionaryList(string csv, bool hasHeader = true, char separator = ',') + { + var result = new List>(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return result; + + var firstLine = ParseCsvLine(lines[0], separator); + var headers = new List(); + + if (hasHeader) + { + foreach (var h in firstLine) + { + headers.Add(UnescapeCsvField(h)); + } + } + else + { + for (int i = 0; i < firstLine.Count; i++) + { + headers.Add($"Column{i + 1}"); + } + } + + var startIndex = hasHeader ? 1 : 0; + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var dict = new Dictionary(); + + for (int j = 0; j < headers.Count && j < values.Count; j++) + { + dict[headers[j]] = UnescapeCsvField(values[j]); + } + + result.Add(dict); + } + + return result; + } + + /// + /// 保存CSV到文件 + /// + public static void SaveToFile(string csv, string filePath, Encoding? encoding = null) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + File.WriteAllText(filePath, csv, encoding ?? Encoding.UTF8); + } + + /// + /// 从文件读取CSV + /// + public static string LoadFromFile(string filePath, Encoding? encoding = null) + { + return File.ReadAllText(filePath, encoding ?? Encoding.UTF8); + } + + private static string EscapeCsvField(string field, char separator) + { + if (field.Contains(separator) || field.Contains("\"") || field.Contains("\n") || field.Contains("\r")) + { + return "\"" + field.Replace("\"", "\"\"") + "\""; + } + return field; + } + + private static string UnescapeCsvField(string field) + { + if (field.StartsWith("\"") && field.EndsWith("\"")) + { + return field.Substring(1, field.Length - 2).Replace("\"\"", "\""); + } + return field; + } + + private static List ParseCsvLine(string line, char separator) + { + var result = new List(); + var current = new StringBuilder(); + var inQuotes = false; + + for (int i = 0; i < line.Length; i++) + { + var c = line[i]; + + if (c == '"') + { + if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') + { + current.Append('"'); + i++; + } + else + { + inQuotes = !inQuotes; + } + } + else if (c == separator && !inQuotes) + { + result.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + result.Add(current.ToString()); + return result; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs b/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs new file mode 100644 index 0000000..3d00029 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs @@ -0,0 +1,789 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.ConvertCategory +{ + /// + /// MessagePack 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 MessagePack 序列化和反序列化 + /// + public static class MsgPackConvertUtil + { + #region 序列化 + + /// + /// 将对象序列化为 MessagePack 字节数组 + /// + /// 对象类型 + /// 要序列化的对象 + /// MessagePack 字节数组 + public static byte[] Serialize(T obj) + { + using var stream = new MemoryStream(); + SerializeValue(obj, stream); + return stream.ToArray(); + } + + /// + /// 将对象序列化为 MessagePack 并写入流 + /// + /// 对象类型 + /// 要序列化的对象 + /// 目标流 + public static void Serialize(T obj, Stream stream) + { + SerializeValue(obj, stream); + } + + /// + /// 将字典序列化为 MessagePack 字节数组 + /// + /// 要序列化的字典 + /// MessagePack 字节数组 + public static byte[] SerializeDictionary(IDictionary dict) + { + using var stream = new MemoryStream(); + SerializeDictionary(dict, stream); + return stream.ToArray(); + } + + private static void SerializeValue(object? value, Stream stream) + { + if (value == null) + { + WriteNil(stream); + return; + } + + var type = value.GetType(); + + // 布尔值 + if (type == typeof(bool)) + { + WriteBool((bool)value, stream); + return; + } + + // 整数类型 + if (type == typeof(sbyte)) { WriteInteger((sbyte)value, stream); return; } + if (type == typeof(byte)) { WriteInteger((byte)value, stream); return; } + if (type == typeof(short)) { WriteInteger((short)value, stream); return; } + if (type == typeof(ushort)) { WriteInteger((ushort)value, stream); return; } + if (type == typeof(int)) { WriteInteger((int)value, stream); return; } + if (type == typeof(uint)) { WriteInteger((uint)value, stream); return; } + if (type == typeof(long)) { WriteInteger((long)value, stream); return; } + if (type == typeof(ulong)) { WriteInteger((ulong)value, stream); return; } + + // 浮点数 + if (type == typeof(float)) { WriteFloat((float)value, stream); return; } + if (type == typeof(double)) { WriteDouble((double)value, stream); return; } + + // 字符串 + if (type == typeof(string)) + { + WriteString((string)value, stream); + return; + } + + // 字节数组 + if (type == typeof(byte[])) + { + WriteBinary((byte[])value, stream); + return; + } + + // 数组和列表 + if (value is IEnumerable enumerable and not string and not IDictionary) + { + SerializeArray(enumerable, stream); + return; + } + + // 字典 + if (value is IDictionary dict) + { + SerializeDictionary(dict, stream); + return; + } + + // 其他对象 + SerializeObject(value, stream); + } + + private static void WriteNil(Stream stream) + { + stream.WriteByte(0xC0); + } + + private static void WriteBool(bool value, Stream stream) + { + stream.WriteByte(value ? (byte)0xC3 : (byte)0xC2); + } + + private static void WriteInteger(sbyte value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)value); + } + } + + private static void WriteInteger(byte value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(short value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else + { + stream.WriteByte(0xD1); + WriteBigEndianInt16(value, stream); + } + } + + private static void WriteInteger(ushort value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(int value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else if (value >= short.MinValue) + { + stream.WriteByte(0xD1); + WriteBigEndianInt16((short)value, stream); + } + else + { + stream.WriteByte(0xD2); + WriteBigEndianInt32(value, stream); + } + } + + private static void WriteInteger(uint value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(long value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else if (value >= short.MinValue) + { + stream.WriteByte(0xD1); + WriteBigEndianInt16((short)value, stream); + } + else if (value >= int.MinValue) + { + stream.WriteByte(0xD2); + WriteBigEndianInt32((int)value, stream); + } + else + { + stream.WriteByte(0xD3); + WriteBigEndianInt64(value, stream); + } + } + + private static void WriteInteger(ulong value, Stream stream) + { + if (value <= 127) + { + // Positive FixInt + stream.WriteByte((byte)value); + } + else if (value <= byte.MaxValue) + { + stream.WriteByte(0xCC); + stream.WriteByte((byte)value); + } + else if (value <= ushort.MaxValue) + { + stream.WriteByte(0xCD); + WriteBigEndianUInt16((ushort)value, stream); + } + else if (value <= uint.MaxValue) + { + stream.WriteByte(0xCE); + WriteBigEndianUInt32((uint)value, stream); + } + else + { + stream.WriteByte(0xCF); + WriteBigEndianUInt64(value, stream); + } + } + + private static void WriteFloat(float value, Stream stream) + { + stream.WriteByte(0xCA); + var bytes = BitConverter.GetBytes(value); + stream.Write(bytes, 0, 4); + } + + private static void WriteDouble(double value, Stream stream) + { + stream.WriteByte(0xCB); + var bytes = BitConverter.GetBytes(value); + stream.Write(bytes, 0, 8); + } + + private static void WriteString(string value, Stream stream) + { + var bytes = Encoding.UTF8.GetBytes(value); + var length = bytes.Length; + + if (length <= 31) + { + // FixStr + stream.WriteByte((byte)(0xA0 | length)); + } + else if (length <= byte.MaxValue) + { + stream.WriteByte(0xD9); + stream.WriteByte((byte)length); + } + else if (length <= ushort.MaxValue) + { + stream.WriteByte(0xDA); + WriteBigEndianUInt16((ushort)length, stream); + } + else + { + stream.WriteByte(0xDB); + WriteBigEndianUInt32((uint)length, stream); + } + + stream.Write(bytes, 0, bytes.Length); + } + + private static void WriteBinary(byte[] value, Stream stream) + { + var length = value.Length; + + if (length <= byte.MaxValue) + { + stream.WriteByte(0xC4); + stream.WriteByte((byte)length); + } + else if (length <= ushort.MaxValue) + { + stream.WriteByte(0xC5); + WriteBigEndianUInt16((ushort)length, stream); + } + else + { + stream.WriteByte(0xC6); + WriteBigEndianUInt32((uint)length, stream); + } + + stream.Write(value, 0, length); + } + + private static void SerializeArray(IEnumerable enumerable, Stream stream) + { + var list = new List(); + foreach (var item in enumerable) + { + list.Add(item); + } + + var count = list.Count; + + if (count <= 15) + { + // FixArray + stream.WriteByte((byte)(0x90 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDC); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDD); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (var item in list) + { + SerializeValue(item, stream); + } + } + + private static void SerializeDictionary(IDictionary dict, Stream stream) + { + var count = dict.Count; + + if (count <= 15) + { + // FixMap + stream.WriteByte((byte)(0x80 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDE); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDF); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (DictionaryEntry entry in dict) + { + SerializeValue(entry.Key, stream); + SerializeValue(entry.Value, stream); + } + } + + private static void SerializeObject(object obj, Stream stream) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + var count = 0; + foreach (var prop in properties) + { + if (prop.CanRead) + count++; + } + + if (count <= 15) + { + stream.WriteByte((byte)(0x80 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDE); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDF); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + WriteString(prop.Name, stream); + SerializeValue(prop.GetValue(obj), stream); + } + } + + private static void WriteBigEndianInt16(short value, Stream stream) + { + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt16(ushort value, Stream stream) + { + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianInt32(int value, Stream stream) + { + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt32(uint value, Stream stream) + { + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianInt64(long value, Stream stream) + { + stream.WriteByte((byte)(value >> 56)); + stream.WriteByte((byte)(value >> 48)); + stream.WriteByte((byte)(value >> 40)); + stream.WriteByte((byte)(value >> 32)); + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt64(ulong value, Stream stream) + { + stream.WriteByte((byte)(value >> 56)); + stream.WriteByte((byte)(value >> 48)); + stream.WriteByte((byte)(value >> 40)); + stream.WriteByte((byte)(value >> 32)); + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + #endregion + + #region 反序列化 + + /// + /// 从 MessagePack 字节数组反序列化为对象 + /// + /// 目标类型 + /// MessagePack 字节数组 + /// 反序列化的对象 + public static T? Deserialize(byte[] data) + { + using var stream = new MemoryStream(data); + return Deserialize(stream); + } + + /// + /// 从流中反序列化对象 + /// + /// 目标类型 + /// 数据流 + /// 反序列化的对象 + public static T? Deserialize(Stream stream) + { + var value = DeserializeValue(stream); + return ConvertValue(value); + } + + /// + /// 从 MessagePack 字节数组反序列化为字典 + /// + /// MessagePack 字节数组 + /// 字典对象 + public static Dictionary DeserializeToDictionary(byte[] data) + { + using var stream = new MemoryStream(data); + return DeserializeToDictionary(stream); + } + + /// + /// 从流中反序列化为字典 + /// + /// 数据流 + /// 字典对象 + public static Dictionary DeserializeToDictionary(Stream stream) + { + var value = DeserializeValue(stream); + if (value is Dictionary dict) + { + return dict; + } + return new Dictionary(); + } + + private static object? DeserializeValue(Stream stream) + { + var header = stream.ReadByte(); + if (header < 0) + throw new EndOfStreamException(); + + // Positive FixInt (0x00 - 0x7F) + if (header <= 0x7F) + { + return (byte)header; + } + + // FixMap (0x80 - 0x8F) + if ((header & 0xF0) == 0x80) + { + var count = header & 0x0F; + return DeserializeMap(stream, count); + } + + // FixArray (0x90 - 0x9F) + if ((header & 0xF0) == 0x90) + { + var count = header & 0x0F; + return DeserializeArray(stream, count); + } + + // FixStr (0xA0 - 0xBF) + if ((header & 0xE0) == 0xA0) + { + var length = header & 0x1F; + return DeserializeString(stream, length); + } + + // Negative FixInt (0xE0 - 0xFF) + if (header >= 0xE0) + { + return (sbyte)(byte)header; + } + + // 其他格式 + switch (header) + { + case 0xC0: // nil + return null; + + case 0xC2: // false + return false; + + case 0xC3: // true + return true; + + case 0xC4: // bin 8 + return DeserializeBinary(stream, ReadUInt8(stream)); + + case 0xC5: // bin 16 + return DeserializeBinary(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xC6: // bin 32 + return DeserializeBinary(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xC7: // ext 8 + case 0xC8: // ext 16 + case 0xC9: // ext 32 + throw new NotSupportedException("Extension types are not supported"); + + case 0xCA: // float 32 + return ReadFloat(stream); + + case 0xCB: // float 64 + return ReadDouble(stream); + + case 0xCC: // uint 8 + return ReadUInt8(stream); + + case 0xCD: // uint 16 + return ReadBigEndianUInt16(stream); + + case 0xCE: // uint 32 + return ReadBigEndianUInt32(stream); + + case 0xCF: // uint 64 + return ReadBigEndianUInt64(stream); + + case 0xD0: // int 8 + return ReadInt8(stream); + + case 0xD1: // int 16 + return ReadBigEndianInt16(stream); + + case 0xD2: // int 32 + return ReadBigEndianInt32(stream); + + case 0xD3: // int 64 + return ReadBigEndianInt64(stream); + + case 0xD9: // str 8 + return DeserializeString(stream, ReadUInt8(stream)); + + case 0xDA: // str 16 + return DeserializeString(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDB: // str 32 + return DeserializeString(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xDC: // array 16 + return DeserializeArray(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDD: // array 32 + return DeserializeArray(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xDE: // map 16 + return DeserializeMap(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDF: // map 32 + return DeserializeMap(stream, (int)ReadBigEndianUInt32(stream)); + + default: + throw new NotSupportedException($"Unknown format: 0x{header:X2}"); + } + } + + private static sbyte ReadInt8(Stream stream) + { + var b = stream.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + return (sbyte)b; + } + + private static byte ReadUInt8(Stream stream) + { + var b = stream.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + return (byte)b; + } + + private static short ReadBigEndianInt16(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + if (b1 < 0 || b2 < 0) throw new EndOfStreamException(); + return (short)((b1 << 8) | b2); + } + + private static ushort ReadBigEndianUInt16(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + if (b1 < 0 || b2 < 0) throw new EndOfStreamException(); + return (ushort)((b1 << 8) | b2); + } + + private static int ReadBigEndianInt32(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + var b3 = stream.ReadByte(); + var b4 = stream.ReadByte(); + if (b1 < 0 || b2 < 0 || b3 < 0 || b4 < 0) throw new EndOfStreamException(); + return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; + } + + private static uint ReadBigEndianUInt32(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + var b3 = stream.ReadByte(); + var b4 = stream.ReadByte(); + if (b1 < 0 || b2 < 0 || b3 < 0 || b4 < 0) throw new EndOfStreamException(); + return (uint)((b1 << 24) | (b2 << 16) | (b3 << 8) | b4); + } + + private static long ReadBigEndianInt64(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + + return ((long)bytes[0] << 56) | ((long)bytes[1] << 48) | + ((long)bytes[2] << 40) | ((long)bytes[3] << 32) | + ((long)bytes[4] << 24) | ((long)bytes[5] << 16) | + ((long)bytes[6] << 8) | bytes[7]; + } + + private static ulong ReadBigEndianUInt64(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + + return ((ulong)bytes[0] << 56) | ((ulong)bytes[1] << 48) | + ((ulong)bytes[2] << 40) | ((ulong)bytes[3] << 32) | + ((ulong)bytes[4] << 24) | ((ulong)bytes[5] << 16) | + ((ulong)bytes[6] << 8) | bytes[7]; + } + + private static float ReadFloat(Stream stream) + { + var bytes = new byte[4]; + var read = stream.Read(bytes, 0, 4); + if (read < 4) throw new EndOfStreamException(); + return BitConverter.ToSingle(bytes, 0); + } + + private static double ReadDouble(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + return BitConverter.ToDouble(bytes, 0); + } + + private static string DeserializeString(Stream stream, int length) + { + var bytes = new byte[length]; + var read = stream.Read(bytes, 0, length); + if (read < length) throw new EndOfStreamException(); + return Encoding.UTF8.GetString(bytes); + } + + private static byte[] DeserializeBinary(Stream stream, int length) + { + var bytes = new byte[length]; + var read = stream.Read(bytes, 0, length); + if (read < length) throw new EndOfStreamException(); + return bytes; + } + + private static List DeserializeArray(Stream stream, int count) + { + var list = new List(count); + for (int i = 0; i < count; i++) + { + list.Add(DeserializeValue(stream)); + } + return list; + } + + private static Dictionary DeserializeMap(Stream stream, int count) + { + var dict = new Dictionary(count); + for (int i = 0; i < count; i++) + { + var key = DeserializeValue(stream); + var value = DeserializeValue(stream); + dict[key?.ToString() ?? ""] = value; + } + return dict; + } + + private static T? ConvertValue(object? value) + { + if (value == null) + return default; + + if (value is T typedValue) + return typedValue; + + var targetType = typeof(T); + + if (targetType == typeof(string)) + { + return (T)(object)value.ToString()!; + } + + return (T)Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs b/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs new file mode 100644 index 0000000..2ab6e3d --- /dev/null +++ b/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs @@ -0,0 +1,715 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ConvertCategory +{ + /// + /// TOML 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 TOML 序列化和反序列化 + /// + public static class TomlConvertUtil + { + #region 序列化 + + /// + /// 将对象序列化为 TOML 字符串 + /// + /// 对象类型 + /// 要序列化的对象 + /// TOML 字符串 + public static string Serialize(T obj) + { + var builder = new StringBuilder(); + SerializeObject(obj, builder, ""); + return builder.ToString(); + } + + /// + /// 将字典序列化为 TOML 字符串 + /// + /// 要序列化的字典 + /// TOML 字符串 + public static string SerializeDictionary(IDictionary dict) + { + var builder = new StringBuilder(); + SerializeDictionary(dict, builder, ""); + return builder.ToString(); + } + + private static void SerializeObject(object? obj, StringBuilder builder, string prefix) + { + if (obj == null) + return; + + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + // 先序列化简单属性 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (IsSimpleType(propType)) + { + builder.Append(prop.Name); + builder.Append(" = "); + SerializeValue(value, builder); + builder.AppendLine(); + } + } + + // 序列化数组和列表 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (IsArrayType(propType) && value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeArray(enumerable, builder, prop.Name); + } + } + + // 序列化嵌套表 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (!IsSimpleType(propType) && !IsArrayType(propType) && value != null) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? prop.Name : $"{prefix}.{prop.Name}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeObject(value, builder, tablePrefix); + } + } + } + + private static void SerializeDictionary(IDictionary dict, StringBuilder builder, string prefix) + { + // 先序列化简单值 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value == null || IsSimpleType(value.GetType())) + { + builder.Append(key); + builder.Append(" = "); + SerializeValue(value, builder); + builder.AppendLine(); + } + } + + // 序列化数组 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value is IEnumerable enumerable and not string and not IDictionary) + { + builder.AppendLine(); + SerializeArray(enumerable, builder, key); + } + } + + // 序列化嵌套字典 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value is IDictionary nestedDict) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? key : $"{prefix}.{key}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeDictionary(nestedDict, builder, tablePrefix); + } + else if (value != null && !IsSimpleType(value.GetType()) && !IsArrayType(value.GetType())) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? key : $"{prefix}.{key}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeObject(value, builder, tablePrefix); + } + } + } + + private static void SerializeValue(object? value, StringBuilder builder) + { + if (value == null) + { + builder.Append("\"\""); + return; + } + + var type = value.GetType(); + + if (type == typeof(bool)) + { + builder.Append((bool)value ? "true" : "false"); + } + else if (type == typeof(string)) + { + var str = (string)value; + if (str.Contains('\n') || str.Contains('\t') || str.Contains('"') || str.Contains('#')) + { + // 多行字符串使用字面量字符串 + builder.Append("'''"); + builder.Append(str); + builder.Append("'''"); + } + else + { + builder.Append($"\"{EscapeString(str)}\""); + } + } + else if (type == typeof(DateTime)) + { + builder.Append(((DateTime)value).ToString("o")); + } + else if (type == typeof(DateTimeOffset)) + { + builder.Append(((DateTimeOffset)value).ToString("o")); + } + else if (type == typeof(Guid)) + { + builder.Append($"\"{value}\""); + } + else if (type == typeof(decimal)) + { + builder.Append(((decimal)value).ToString(System.Globalization.CultureInfo.InvariantCulture)); + } + else if (type == typeof(float) || type == typeof(double)) + { + builder.Append(Convert.ToDouble(value).ToString(System.Globalization.CultureInfo.InvariantCulture)); + } + else + { + builder.Append(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)); + } + } + + private static void SerializeArray(IEnumerable enumerable, StringBuilder builder, string key) + { + foreach (var item in enumerable) + { + builder.Append(key); + builder.Append(" = ["); + + if (item == null) + { + builder.Append("]"); + } + else if (IsSimpleType(item.GetType())) + { + SerializeValue(item, builder); + builder.Append("]"); + } + else if (item is IDictionary dict) + { + builder.AppendLine(); + SerializeInlineTable(dict, builder); + builder.AppendLine(); + builder.Append("]"); + } + else + { + builder.AppendLine(); + SerializeInlineObject(item, builder); + builder.AppendLine(); + builder.Append("]"); + } + + builder.AppendLine(); + } + } + + private static void SerializeInlineTable(IDictionary dict, StringBuilder builder) + { + builder.Append("{ "); + var first = true; + foreach (DictionaryEntry entry in dict) + { + if (!first) + builder.Append(", "); + first = false; + + builder.Append(entry.Key?.ToString() ?? ""); + builder.Append(" = "); + SerializeValue(entry.Value, builder); + } + builder.Append(" }"); + } + + private static void SerializeInlineObject(object obj, StringBuilder builder) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + builder.Append("{ "); + var first = true; + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + if (!first) + builder.Append(", "); + first = false; + + builder.Append(prop.Name); + builder.Append(" = "); + SerializeValue(prop.GetValue(obj), builder); + } + builder.Append(" }"); + } + + private static string EscapeString(string value) + { + return value.Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\b", "\\b") + .Replace("\f", "\\f") + .Replace("\n", "\\n") + .Replace("\r", "\\r") + .Replace("\t", "\\t"); + } + + private static bool IsSimpleType(Type type) + { + return type.IsPrimitive || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(Guid) || + type == typeof(TimeSpan); + } + + private static bool IsArrayType(Type type) + { + return (type.IsArray || type.GetInterfaces().Contains(typeof(IList))) && type != typeof(string); + } + + #endregion + + #region 反序列化 + + /// + /// 将 TOML 字符串反序列化为字典 + /// + /// TOML 字符串 + /// 字典对象 + public static Dictionary Deserialize(string toml) + { + var result = new Dictionary(); + var currentTable = result; + var tables = new Stack>(); + tables.Push(result); + + using var reader = new StringReader(toml); + string? line; + + while ((line = reader.ReadLine()) != null) + { + line = line.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#")) + continue; + + // 表头 [table] 或 [table.subtable] + if (line.StartsWith("[") && line.EndsWith("]")) + { + var tableName = line[1..^1].Trim(); + currentTable = GetOrCreateTable(result, tableName); + continue; + } + + // 数组表 [[array]] + if (line.StartsWith("[[") && line.EndsWith("]]")) + { + var arrayName = line[2..^2].Trim(); + AddArrayTable(result, arrayName); + continue; + } + + // 键值对 + var equalsIndex = line.IndexOf('='); + if (equalsIndex > 0) + { + var key = line[..equalsIndex].Trim(); + var value = line[(equalsIndex + 1)..].Trim(); + currentTable[key] = ParseValue(value, reader); + } + } + + return result; + } + + /// + /// 将 TOML 字符串反序列化为指定类型 + /// + /// 目标类型 + /// TOML 字符串 + /// 反序列化的对象 + public static T? Deserialize(string toml) where T : class, new() + { + var dict = Deserialize(toml); + return MapToObject(dict); + } + + /// + /// 从文件加载 TOML 并反序列化为字典 + /// + /// 文件路径 + /// 字典对象 + public static Dictionary LoadFromFile(string filePath) + { + var toml = File.ReadAllText(filePath); + return Deserialize(toml); + } + + /// + /// 将字典保存为 TOML 文件 + /// + /// 字典对象 + /// 文件路径 + public static void SaveToFile(Dictionary dict, string filePath) + { + var toml = SerializeDictionary(dict); + File.WriteAllText(filePath, toml); + } + + private static Dictionary GetOrCreateTable(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + foreach (var part in parts) + { + if (!current.TryGetValue(part, out var value) || !(value is Dictionary nested)) + { + nested = new Dictionary(); + current[part] = nested; + } + current = nested; + } + + return current; + } + + private static void AddArrayTable(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + for (int i = 0; i < parts.Length - 1; i++) + { + if (!current.TryGetValue(parts[i], out var value) || !(value is Dictionary nested)) + { + nested = new Dictionary(); + current[parts[i]] = nested; + } + current = nested; + } + + var lastPart = parts[^1]; + if (!current.TryGetValue(lastPart, out var arrayValue) || !(arrayValue is List> array)) + { + array = new List>(); + current[lastPart] = array; + } + + var newTable = new Dictionary(); + array.Add(newTable); + } + + private static object? ParseValue(string value, StringReader reader) + { + value = value.Trim(); + + // 字符串 + if (value.StartsWith("\"") && value.EndsWith("\"")) + { + return UnescapeString(value[1..^1]); + } + if (value.StartsWith("'") && value.EndsWith("'")) + { + return value[1..^1]; + } + if (value.StartsWith("'''") || value.StartsWith("\"\"\"")) + { + return ParseMultiLineString(value, reader); + } + + // 布尔值 + if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) + return true; + if (value.Equals("false", StringComparison.OrdinalIgnoreCase)) + return false; + + // 数组 + if (value.StartsWith("[") && value.EndsWith("]")) + { + return ParseArray(value[1..^1]); + } + + // 内联表 + if (value.StartsWith("{") && value.EndsWith("}")) + { + return ParseInlineTable(value[1..^1]); + } + + // 数字 + if (int.TryParse(value, out var intVal)) + return intVal; + if (long.TryParse(value, out var longVal)) + return longVal; + if (double.TryParse(value, out var doubleVal)) + return doubleVal; + + // 日期时间 + if (DateTime.TryParse(value, out var dateVal)) + return dateVal; + + return value; + } + + private static string ParseMultiLineString(string start, StringReader reader) + { + var delimiter = start.Substring(0, 3); + var sb = new StringBuilder(); + + // 处理开始行的剩余内容 + if (start.Length > 3) + { + sb.Append(start[3..]); + } + + string? line; + while ((line = reader.ReadLine()) != null) + { + if (line.Contains(delimiter)) + { + var endIndex = line.IndexOf(delimiter); + sb.Append(line[..endIndex]); + break; + } + sb.AppendLine(line); + } + + return sb.ToString(); + } + + private static List ParseArray(string content) + { + var result = new List(); + var items = SplitArrayItems(content); + + foreach (var item in items) + { + result.Add(ParseValue(item.Trim(), null!)); + } + + return result; + } + + private static Dictionary ParseInlineTable(string content) + { + var result = new Dictionary(); + var pairs = SplitKeyValuePairs(content); + + foreach (var pair in pairs) + { + var equalsIndex = pair.IndexOf('='); + if (equalsIndex > 0) + { + var key = pair[..equalsIndex].Trim(); + var value = pair[(equalsIndex + 1)..].Trim(); + result[key] = ParseValue(value, null!); + } + } + + return result; + } + + private static List SplitArrayItems(string content) + { + var items = new List(); + var current = new StringBuilder(); + var depth = 0; + var inString = false; + var stringChar = '\0'; + + foreach (var c in content) + { + if (inString) + { + current.Append(c); + if (c == stringChar) + inString = false; + } + else if (c == '"' || c == '\'') + { + inString = true; + stringChar = c; + current.Append(c); + } + else if (c == '[' || c == '{') + { + depth++; + current.Append(c); + } + else if (c == ']' || c == '}') + { + depth--; + current.Append(c); + } + else if (c == ',' && depth == 0) + { + items.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + if (current.Length > 0) + items.Add(current.ToString()); + + return items; + } + + private static List SplitKeyValuePairs(string content) + { + var pairs = new List(); + var current = new StringBuilder(); + var depth = 0; + var inString = false; + var stringChar = '\0'; + + foreach (var c in content) + { + if (inString) + { + current.Append(c); + if (c == stringChar) + inString = false; + } + else if (c == '"' || c == '\'') + { + inString = true; + stringChar = c; + current.Append(c); + } + else if (c == '[' || c == '{') + { + depth++; + current.Append(c); + } + else if (c == ']' || c == '}') + { + depth--; + current.Append(c); + } + else if (c == ',' && depth == 0) + { + pairs.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + if (current.Length > 0) + pairs.Add(current.ToString()); + + return pairs; + } + + private static string UnescapeString(string value) + { + return value.Replace("\\b", "\b") + .Replace("\\f", "\f") + .Replace("\\n", "\n") + .Replace("\\r", "\r") + .Replace("\\t", "\t") + .Replace("\\\"", "\"") + .Replace("\\\\", "\\"); + } + + private static T? MapToObject(Dictionary dict) where T : class, new() + { + if (dict == null) + return null; + + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var prop = type.GetProperty(kvp.Key, + System.Reflection.BindingFlags.Public | + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.IgnoreCase); + + if (prop != null && prop.CanWrite) + { + var value = ConvertValue(kvp.Value, prop.PropertyType); + prop.SetValue(obj, value); + } + } + + return obj; + } + + private static object? ConvertValue(object? value, Type targetType) + { + if (value == null) + return null; + + var sourceType = value.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + return value; + + if (value is Dictionary dict && !targetType.IsPrimitive) + { + var method = typeof(TomlConvertUtil).GetMethod(nameof(MapToObject), + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) + ?.MakeGenericMethod(targetType); + return method?.Invoke(null, new object[] { dict }); + } + + return Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs b/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs new file mode 100644 index 0000000..b1aadb8 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Xml; +using System.Xml.Serialization; +using System.IO; +using System.Text; +using System.Xml.Linq; +using System.Xml.XPath; + +namespace EasyTool.ConvertCategory +{ + /// + /// XML转换工具类 + /// + public static class XmlConvertUtil + { + #region 对象序列化 + + /// + /// 对象序列化为XML字符串 + /// + public static string ToXml(T obj, bool indent = true, bool omitXmlDeclaration = false) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + + var serializer = new XmlSerializer(typeof(T)); + var settings = new XmlWriterSettings + { + Indent = indent, + OmitXmlDeclaration = omitXmlDeclaration, + Encoding = Encoding.UTF8 + }; + + using var writer = new StringWriter(); + using var xmlWriter = XmlWriter.Create(writer, settings); + serializer.Serialize(xmlWriter, obj); + return writer.ToString(); + } + + /// + /// XML字符串反序列化为对象 + /// + public static T? FromXml(string xml) + { + if (string.IsNullOrWhiteSpace(xml)) + return default; + + var serializer = new XmlSerializer(typeof(T)); + using var reader = new StringReader(xml); + return (T?)serializer.Deserialize(reader); + } + + /// + /// 对象序列化为XML文件 + /// + public static void ToXmlFile(T obj, string filePath, bool indent = true) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var serializer = new XmlSerializer(typeof(T)); + var settings = new XmlWriterSettings + { + Indent = indent, + Encoding = Encoding.UTF8 + }; + + using var writer = XmlWriter.Create(filePath, settings); + serializer.Serialize(writer, obj); + } + + /// + /// XML文件反序列化为对象 + /// + public static T? FromXmlFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + var serializer = new XmlSerializer(typeof(T)); + using var reader = XmlReader.Create(filePath); + return (T?)serializer.Deserialize(reader); + } + + #endregion + + #region 字典转换 + + /// + /// 字典转XML + /// + public static string DictionaryToXml(Dictionary dict, string rootName = "root", string itemName = "item") + { + var doc = new XDocument(new XElement(rootName)); + var root = doc.Root!; + + foreach (var kvp in dict) + { + root.Add(new XElement(itemName, + new XAttribute("key", kvp.Key), + new XAttribute("value", kvp.Value))); + } + + return doc.ToString(); + } + + /// + /// XML转字典 + /// + public static Dictionary XmlToDictionary(string xml, string itemName = "item") + { + var dict = new Dictionary(); + var doc = XDocument.Parse(xml); + + foreach (var element in doc.Descendants(itemName)) + { + var key = element.Attribute("key")?.Value; + var value = element.Attribute("value")?.Value; + if (key != null) + dict[key] = value ?? ""; + } + + return dict; + } + + #endregion + + #region 列表转换 + + /// + /// 列表转XML + /// + public static string ListToXml(List list, string rootName = "root", string itemName = "item") + { + var doc = new XDocument(new XElement(rootName)); + var root = doc.Root!; + + foreach (var item in list) + { + root.Add(new XElement(itemName, item?.ToString())); + } + + return doc.ToString(); + } + + /// + /// XML转列表 + /// + public static List XmlToList(string xml, string itemName = "item") + { + var list = new List(); + var doc = XDocument.Parse(xml); + + foreach (var element in doc.Descendants(itemName)) + { + list.Add(element.Value); + } + + return list; + } + + #endregion + + #region 格式化 + + /// + /// 格式化XML + /// + public static string FormatXml(string xml, string indent = " ") + { + var doc = XDocument.Parse(xml); + return doc.ToString(); + } + + /// + /// 压缩XML(移除空白) + /// + public static string MinifyXml(string xml) + { + var doc = XDocument.Parse(xml); + return doc.ToString(SaveOptions.DisableFormatting); + } + + /// + /// 验证XML格式 + /// + public static bool IsValidXml(string xml) + { + try + { + XDocument.Parse(xml); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region XPath查询 + + /// + /// XPath查询 + /// + public static List SelectNodes(string xml, string xpath) + { + var results = new List(); + var doc = XDocument.Parse(xml); + var nodes = doc.XPathSelectElements(xpath); + + foreach (var node in nodes) + { + results.Add(node.Value); + } + + return results; + } + + /// + /// XPath查询单个节点 + /// + public static string? SelectSingleNode(string xml, string xpath) + { + var doc = XDocument.Parse(xml); + var node = doc.XPathSelectElement(xpath); + return node?.Value; + } + + #endregion + + #region 节点操作 + + /// + /// 获取节点值 + /// + public static string? GetNodeValue(string xml, string nodeName) + { + var doc = XDocument.Parse(xml); + return doc.Root?.Element(nodeName)?.Value; + } + + /// + /// 设置节点值 + /// + public static string SetNodeValue(string xml, string nodeName, string value) + { + var doc = XDocument.Parse(xml); + var node = doc.Root?.Element(nodeName); + if (node != null) + node.Value = value; + return doc.ToString(); + } + + /// + /// 获取属性值 + /// + public static string? GetAttributeValue(string xml, string nodeName, string attributeName) + { + var doc = XDocument.Parse(xml); + return doc.Root?.Element(nodeName)?.Attribute(attributeName)?.Value; + } + + /// + /// 设置属性值 + /// + public static string SetAttributeValue(string xml, string nodeName, string attributeName, string value) + { + var doc = XDocument.Parse(xml); + var node = doc.Root?.Element(nodeName); + if (node != null) + node.SetAttributeValue(attributeName, value); + return doc.ToString(); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs b/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs new file mode 100644 index 0000000..2be1a4a --- /dev/null +++ b/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs @@ -0,0 +1,523 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ConvertCategory +{ + /// + /// YAML 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 YAML 序列化和反序列化 + /// + public static class YamlConvertUtil + { + private const int DefaultIndent = 2; + + #region 序列化 + + /// + /// 将对象序列化为 YAML 字符串 + /// + /// 对象类型 + /// 要序列化的对象 + /// 缩进空格数 + /// YAML 字符串 + public static string Serialize(T obj, int indent = DefaultIndent) + { + var builder = new StringBuilder(); + SerializeValue(obj, builder, 0, indent); + return builder.ToString(); + } + + /// + /// 将字典序列化为 YAML 字符串 + /// + /// 要序列化的字典 + /// 缩进空格数 + /// YAML 字符串 + public static string SerializeDictionary(IDictionary dict, int indent = DefaultIndent) + { + var builder = new StringBuilder(); + SerializeDictionary(dict, builder, 0, indent); + return builder.ToString(); + } + + private static void SerializeValue(object? value, StringBuilder builder, int level, int indent) + { + if (value == null) + { + builder.Append("null"); + return; + } + + var type = value.GetType(); + + if (type.IsPrimitive || value is decimal || value is DateTime || value is DateTimeOffset || value is Guid) + { + SerializeScalar(value, builder); + } + else if (value is string str) + { + SerializeString(str, builder); + } + else if (value is IDictionary dict) + { + SerializeDictionary(dict, builder, level, indent); + } + else if (value is IEnumerable enumerable and not string) + { + SerializeEnumerable(enumerable, builder, level, indent); + } + else + { + SerializeObject(value, builder, level, indent); + } + } + + private static void SerializeScalar(object value, StringBuilder builder) + { + var type = value.GetType(); + + if (type == typeof(bool)) + { + builder.Append((bool)value ? "true" : "false"); + } + else if (type == typeof(DateTime)) + { + builder.Append(((DateTime)value).ToString("o")); + } + else if (type == typeof(DateTimeOffset)) + { + builder.Append(((DateTimeOffset)value).ToString("o")); + } + else if (type == typeof(Guid)) + { + builder.Append(((Guid)value).ToString()); + } + else + { + builder.Append(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)); + } + } + + private static void SerializeString(string value, StringBuilder builder) + { + if (string.IsNullOrEmpty(value)) + { + builder.Append("\"\""); + return; + } + + // 检查是否需要引号 + var needsQuotes = value.Contains('\n') || + value.Contains('\t') || + value.Contains(':') || + value.Contains('#') || + value.StartsWith(" ") || + value.EndsWith(" ") || + value.StartsWith("\"") || + value.StartsWith("'") || + IsNumeric(value); + + if (needsQuotes) + { + // 多行字符串 + if (value.Contains('\n')) + { + builder.AppendLine("|"); + var lines = value.Split('\n'); + foreach (var line in lines) + { + builder.AppendLine($" {line}"); + } + } + else + { + builder.Append($"\"{EscapeString(value)}\""); + } + } + else + { + builder.Append(value); + } + } + + private static string EscapeString(string value) + { + return value.Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\n", "\\n") + .Replace("\r", "\\r") + .Replace("\t", "\\t"); + } + + private static bool IsNumeric(string value) + { + return double.TryParse(value, out _); + } + + private static void SerializeDictionary(IDictionary dict, StringBuilder builder, int level, int indent) + { + var first = true; + foreach (DictionaryEntry entry in dict) + { + if (!first) + { + builder.AppendLine(); + } + first = false; + + builder.Append(new string(' ', level * indent)); + builder.Append(entry.Key?.ToString() ?? "null"); + builder.Append(':'); + + if (entry.Value == null) + { + builder.Append(" null"); + } + else if (entry.Value is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (entry.Value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(enumerable, builder, level + 1, indent); + } + else + { + builder.Append(' '); + SerializeValue(entry.Value, builder, level + 1, indent); + } + } + } + + private static void SerializeEnumerable(IEnumerable enumerable, StringBuilder builder, int level, int indent) + { + foreach (var item in enumerable) + { + builder.AppendLine(); + builder.Append(new string(' ', level * indent)); + builder.Append("- "); + + if (item == null) + { + builder.Append("null"); + } + else if (item is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (item is IEnumerable nestedEnumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(nestedEnumerable, builder, level + 1, indent); + } + else + { + SerializeValue(item, builder, level + 1, indent); + } + } + } + + private static void SerializeObject(object obj, StringBuilder builder, int level, int indent) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + var first = true; + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + if (!first) + { + builder.AppendLine(); + } + first = false; + + builder.Append(new string(' ', level * indent)); + builder.Append(prop.Name); + builder.Append(':'); + + if (value == null) + { + builder.Append(" null"); + } + else if (value is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(enumerable, builder, level + 1, indent); + } + else + { + builder.Append(' '); + SerializeValue(value, builder, level + 1, indent); + } + } + } + + #endregion + + #region 反序列化 + + /// + /// 将 YAML 字符串反序列化为字典 + /// + /// YAML 字符串 + /// 字典对象 + public static Dictionary Deserialize(string yaml) + { + var reader = new StringReader(yaml); + return ParseYaml(reader); + } + + /// + /// 将 YAML 字符串反序列化为指定类型 + /// + /// 目标类型 + /// YAML 字符串 + /// 反序列化的对象 + public static T? Deserialize(string yaml) where T : class, new() + { + var dict = Deserialize(yaml); + return MapToObject(dict); + } + + /// + /// 从文件加载 YAML 并反序列化为字典 + /// + /// 文件路径 + /// 字典对象 + public static Dictionary LoadFromFile(string filePath) + { + var yaml = File.ReadAllText(filePath); + return Deserialize(yaml); + } + + /// + /// 将字典保存为 YAML 文件 + /// + /// 字典对象 + /// 文件路径 + public static void SaveToFile(Dictionary dict, string filePath) + { + var yaml = SerializeDictionary(dict); + File.WriteAllText(filePath, yaml); + } + + private static Dictionary ParseYaml(StringReader reader) + { + var result = new Dictionary(); + var lines = new List(); + + string? line; + while ((line = reader.ReadLine()) != null) + { + lines.Add(line); + } + + ParseLines(lines, 0, lines.Count, 0, result); + return result; + } + + private static int ParseLines(List lines, int start, int end, int baseIndent, Dictionary result) + { + var i = start; + + while (i < end) + { + var line = lines[i]; + + // 跳过空行和注释 + if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) + { + i++; + continue; + } + + var indent = GetIndent(line); + + // 检查是否是列表项 + if (line.TrimStart().StartsWith("- ")) + { + // 解析列表 + var list = new List(); + while (i < end) + { + var currentLine = lines[i]; + var currentIndent = GetIndent(currentLine); + + if (currentIndent < indent) + break; + + if (currentLine.TrimStart().StartsWith("- ")) + { + var value = currentLine.TrimStart()[2..].Trim(); + if (string.IsNullOrEmpty(value)) + { + // 值在下一行(嵌套对象) + i++; + var nestedDict = new Dictionary(); + i = ParseLines(lines, i, end, currentIndent + 2, nestedDict); + list.Add(nestedDict); + } + else + { + list.Add(ParseValue(value)); + i++; + } + } + else + { + break; + } + } + return i; + } + + // 解析键值对 + var colonIndex = line.IndexOf(':'); + if (colonIndex > 0) + { + var key = line[..colonIndex].Trim(); + var value = line[(colonIndex + 1)..].Trim(); + + if (string.IsNullOrEmpty(value)) + { + // 值在下一行(嵌套对象) + i++; + var nestedDict = new Dictionary(); + i = ParseLines(lines, i, end, indent + 2, nestedDict); + result[key] = nestedDict; + } + else + { + result[key] = ParseValue(value); + i++; + } + } + else + { + i++; + } + } + + return i; + } + + private static int GetIndent(string line) + { + for (int i = 0; i < line.Length; i++) + { + if (line[i] != ' ') + return i; + } + return line.Length; + } + + private static object? ParseValue(string value) + { + if (string.IsNullOrEmpty(value)) + return null; + + value = value.Trim(); + + // 处理引号字符串 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + return value[1..^1]; + } + + // null + if (value.Equals("null", StringComparison.OrdinalIgnoreCase) || + value.Equals("~")) + { + return null; + } + + // boolean + if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) + return true; + if (value.Equals("false", StringComparison.OrdinalIgnoreCase)) + return false; + + // 数字 + if (int.TryParse(value, out var intVal)) + return intVal; + if (long.TryParse(value, out var longVal)) + return longVal; + if (double.TryParse(value, out var doubleVal)) + return doubleVal; + + // 日期时间 + if (DateTime.TryParse(value, out var dateVal)) + return dateVal; + + return value; + } + + private static T? MapToObject(Dictionary dict) where T : class, new() + { + if (dict == null) + return null; + + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var prop = type.GetProperty(kvp.Key, + System.Reflection.BindingFlags.Public | + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.IgnoreCase); + + if (prop != null && prop.CanWrite) + { + var value = ConvertValue(kvp.Value, prop.PropertyType); + prop.SetValue(obj, value); + } + } + + return obj; + } + + private static object? ConvertValue(object? value, Type targetType) + { + if (value == null) + return null; + + var sourceType = value.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + return value; + + // 处理字典到对象的映射 + if (value is Dictionary dict && !targetType.IsPrimitive) + { + var method = typeof(YamlConvertUtil).GetMethod(nameof(MapToObject), + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) + ?.MakeGenericMethod(targetType); + return method?.Invoke(null, new object[] { dict }); + } + + // 基本类型转换 + return Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/DatabaseCategory/ConnectionPool.cs b/EasyTool.Core/DatabaseCategory/ConnectionPool.cs new file mode 100644 index 0000000..149242d --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/ConnectionPool.cs @@ -0,0 +1,444 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DatabaseCategory +{ + /// + /// 数据库连接池选项 + /// + public class ConnectionPoolOptions + { + /// + /// 最小连接数 + /// + public int MinPoolSize { get; set; } = 5; + + /// + /// 最大连接数 + /// + public int MaxPoolSize { get; set; } = 100; + + /// + /// 连接超时时间 + /// + public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 连接最大空闲时间 + /// + public TimeSpan MaxIdleTime { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 连接最大生存时间 + /// + public TimeSpan MaxLifetime { get; set; } = TimeSpan.FromHours(8); + + /// + /// 健康检查间隔 + /// + public TimeSpan HealthCheckInterval { get; set; } = TimeSpan.FromMinutes(1); + + /// + /// 获取连接重试次数 + /// + public int RetryCount { get; set; } = 3; + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromMilliseconds(100); + } + + /// + /// 池化连接包装器 + /// + internal class PooledConnection : IDisposable + { + public DbConnection Connection { get; } + public DateTime CreateTime { get; } + public DateTime LastAccessTime { get; set; } + public bool IsInUse { get; set; } + public bool IsValid { get; set; } = true; + + public PooledConnection(DbConnection connection) + { + Connection = connection; + CreateTime = DateTime.UtcNow; + LastAccessTime = DateTime.UtcNow; + } + + public void Dispose() + { + Connection?.Dispose(); + } + } + + /// + /// 数据库连接池 + /// 提供高效的数据库连接管理和复用 + /// + public class ConnectionPool : IAsyncDisposable, IDisposable + { + private readonly string _connectionString; + private readonly DbProviderFactory _providerFactory; + private readonly ConnectionPoolOptions _options; + private readonly ConcurrentBag _pool; + private readonly SemaphoreSlim _semaphore; + private readonly Timer _healthCheckTimer; + private readonly Timer _cleanupTimer; + private int _totalConnections; + private bool _disposed; + + /// + /// 当前池中连接数 + /// + public int PoolSize => _totalConnections; + + /// + /// 可用连接数 + /// + public int AvailableConnections => _pool.Count; + + /// + /// 正在使用的连接数 + /// + public int InUseConnections => _totalConnections - _pool.Count; + + /// + /// 创建数据库连接池 + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 连接池选项 + public ConnectionPool( + string connectionString, + DbProviderFactory providerFactory, + ConnectionPoolOptions? options = null) + { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _providerFactory = providerFactory ?? throw new ArgumentNullException(nameof(providerFactory)); + _options = options ?? new ConnectionPoolOptions(); + _pool = new ConcurrentBag(); + _semaphore = new SemaphoreSlim(_options.MaxPoolSize, _options.MaxPoolSize); + + // 初始化最小连接数 + InitializeMinConnections(); + + // 启动健康检查定时器 + _healthCheckTimer = new Timer(HealthCheck, null, + _options.HealthCheckInterval, _options.HealthCheckInterval); + + // 启动清理定时器 + _cleanupTimer = new Timer(CleanupIdleConnections, null, + _options.MaxIdleTime, _options.MaxIdleTime); + } + + private void InitializeMinConnections() + { + for (int i = 0; i < _options.MinPoolSize; i++) + { + var connection = CreateNewConnection(); + if (connection != null) + { + _pool.Add(connection); + Interlocked.Increment(ref _totalConnections); + } + } + } + + private PooledConnection? CreateNewConnection() + { + try + { + var connection = _providerFactory.CreateConnection(); + if (connection == null) + return null; + + connection.ConnectionString = _connectionString; + connection.Open(); + return new PooledConnection(connection); + } + catch + { + return null; + } + } + + /// + /// 获取连接 + /// + /// 取消令牌 + /// 数据库连接 + public async Task GetConnectionAsync(CancellationToken cancellationToken = default) + { + for (int retry = 0; retry < _options.RetryCount; retry++) + { + if (await _semaphore.WaitAsync(_options.ConnectionTimeout, cancellationToken)) + { + try + { + // 尝试从池中获取 + while (_pool.TryTake(out var pooledConnection)) + { + if (IsConnectionValid(pooledConnection)) + { + pooledConnection.IsInUse = true; + pooledConnection.LastAccessTime = DateTime.UtcNow; + return new PooledConnectionWrapper(pooledConnection, this).Connection; + } + else + { + // 连接无效,释放并减少计数 + pooledConnection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + // 池中没有可用连接,创建新连接 + var newConnection = CreateNewConnection(); + if (newConnection != null) + { + newConnection.IsInUse = true; + Interlocked.Increment(ref _totalConnections); + return new PooledConnectionWrapper(newConnection, this).Connection; + } + } + catch + { + _semaphore.Release(); + throw; + } + } + + if (retry < _options.RetryCount - 1) + { + await Task.Delay(_options.RetryDelay, cancellationToken); + } + } + + throw new TimeoutException($"无法在 {_options.ConnectionTimeout} 内获取数据库连接"); + } + + /// + /// 获取连接(同步) + /// + /// 数据库连接 + public DbConnection GetConnection() + { + return GetConnectionAsync().GetAwaiter().GetResult(); + } + + internal void ReturnConnection(PooledConnection connection) + { + if (_disposed) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + return; + } + + if (IsConnectionValid(connection)) + { + connection.IsInUse = false; + connection.LastAccessTime = DateTime.UtcNow; + _pool.Add(connection); + } + else + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + + _semaphore.Release(); + } + + private bool IsConnectionValid(PooledConnection connection) + { + if (!connection.IsValid || connection.Connection == null) + return false; + + if (connection.Connection.State != ConnectionState.Open) + return false; + + // 检查最大生存时间 + if (DateTime.UtcNow - connection.CreateTime > _options.MaxLifetime) + return false; + + return true; + } + + private void HealthCheck(object? state) + { + var invalidConnections = new List(); + + foreach (var connection in _pool) + { + if (!IsConnectionValid(connection)) + { + invalidConnections.Add(connection); + } + } + + // 注意:由于 ConcurrentBag 的特性,这里只是标记连接无效 + // 实际移除会在 ReturnConnection 时进行 + } + + private void CleanupIdleConnections(object? state) + { + var now = DateTime.UtcNow; + var connectionsToKeep = new List(); + var connectionsToRemove = new List(); + + // 收集需要保留和移除的连接 + while (_pool.TryTake(out var connection)) + { + if (!connection.IsInUse && + now - connection.LastAccessTime > _options.MaxIdleTime && + _totalConnections - connectionsToRemove.Count > _options.MinPoolSize) + { + connectionsToRemove.Add(connection); + } + else + { + connectionsToKeep.Add(connection); + } + } + + // 放回需要保留的连接 + foreach (var connection in connectionsToKeep) + { + _pool.Add(connection); + } + + // 移除空闲连接 + foreach (var connection in connectionsToRemove) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + /// + /// 清空连接池 + /// + public void Clear() + { + while (_pool.TryTake(out var connection)) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + /// + /// 获取连接池统计信息 + /// + /// 统计信息 + public ConnectionPoolStatistics GetStatistics() + { + return new ConnectionPoolStatistics + { + TotalConnections = _totalConnections, + AvailableConnections = _pool.Count, + InUseConnections = _totalConnections - _pool.Count, + MaxPoolSize = _options.MaxPoolSize, + MinPoolSize = _options.MinPoolSize + }; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _healthCheckTimer.Dispose(); + _cleanupTimer.Dispose(); + Clear(); + _semaphore.Dispose(); + } + } + + /// + /// 异步释放资源 + /// + public ValueTask DisposeAsync() + { + if (!_disposed) + { + _disposed = true; + _healthCheckTimer.Dispose(); + _cleanupTimer.Dispose(); + Clear(); + _semaphore.Dispose(); + } + return default(ValueTask); + } + } + + /// + /// 池化连接包装器 + /// + internal class PooledConnectionWrapper : IDisposable + { + private readonly PooledConnection _pooledConnection; + private readonly ConnectionPool _pool; + private bool _disposed; + + public DbConnection Connection => _pooledConnection.Connection; + + public PooledConnectionWrapper(PooledConnection pooledConnection, ConnectionPool pool) + { + _pooledConnection = pooledConnection; + _pool = pool; + } + + public void Dispose() + { + if (!_disposed) + { + _pool.ReturnConnection(_pooledConnection); + _disposed = true; + } + } + } + + /// + /// 连接池统计信息 + /// + public class ConnectionPoolStatistics + { + /// + /// 总连接数 + /// + public int TotalConnections { get; set; } + + /// + /// 可用连接数 + /// + public int AvailableConnections { get; set; } + + /// + /// 正在使用的连接数 + /// + public int InUseConnections { get; set; } + + /// + /// 最大连接数 + /// + public int MaxPoolSize { get; set; } + + /// + /// 最小连接数 + /// + public int MinPoolSize { get; set; } + } +} diff --git a/EasyTool.Core/DatabaseCategory/DbUtil.cs b/EasyTool.Core/DatabaseCategory/DbUtil.cs new file mode 100644 index 0000000..4317318 --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/DbUtil.cs @@ -0,0 +1,547 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DatabaseCategory +{ + /// + /// 数据库工具类 + /// 提供通用的数据库操作方法 + /// + public static class DbUtil + { + #region 连接管理 + + /// + /// 创建并打开连接 + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 数据库连接 + public static async Task CreateConnectionAsync(string connectionString, DbProviderFactory providerFactory) + { + var connection = providerFactory.CreateConnection() + ?? throw new InvalidOperationException("无法创建数据库连接"); + + connection.ConnectionString = connectionString; + await connection.OpenAsync(); + return connection; + } + + /// + /// 创建并打开连接(同步) + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 数据库连接 + public static DbConnection CreateConnection(string connectionString, DbProviderFactory providerFactory) + { + return CreateConnectionAsync(connectionString, providerFactory).GetAwaiter().GetResult(); + } + + #endregion + + #region 执行查询 + + /// + /// 执行非查询命令 + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 受影响的行数 + public static async Task ExecuteNonQueryAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + using var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + return await command.ExecuteNonQueryAsync(); + } + + /// + /// 执行非查询命令(同步) + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 受影响的行数 + public static int ExecuteNonQuery( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + return ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行标量查询 + /// + /// 返回类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 标量值 + public static async Task ExecuteScalarAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + using var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + var result = await command.ExecuteScalarAsync(); + + if (result == null || result == DBNull.Value) + return default; + + return (T)Convert.ChangeType(result, typeof(T)); + } + + /// + /// 执行标量查询(同步) + /// + /// 返回类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 标量值 + public static T? ExecuteScalar( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + return ExecuteScalarAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回数据读取器 + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 命令行为 + /// 数据读取器 + public static async Task ExecuteReaderAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null, + CommandBehavior commandBehavior = CommandBehavior.Default) + { + var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + return await command.ExecuteReaderAsync(commandBehavior); + } + + /// + /// 执行查询并返回数据读取器(同步) + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 命令行为 + /// 数据读取器 + public static DbDataReader ExecuteReader( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null, + CommandBehavior commandBehavior = CommandBehavior.Default) + { + return ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout, commandBehavior).GetAwaiter().GetResult(); + } + + #endregion + + #region 查询映射 + + /// + /// 执行查询并映射到实体列表 + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体列表 + public static async Task> QueryAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + var result = new List(); + + using var reader = await ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout); + + while (await reader.ReadAsync()) + { + result.Add(MapToObject(reader)); + } + + return result; + } + + /// + /// 执行查询并映射到实体列表(同步) + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体列表 + public static List Query( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + return QueryAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回第一个实体 + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体 + public static async Task QueryFirstOrDefaultAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + using var reader = await ExecuteReaderAsync( + connection, sql, parameters, transaction, commandTimeout, + CommandBehavior.SingleRow); + + if (await reader.ReadAsync()) + { + return MapToObject(reader); + } + + return default; + } + + /// + /// 执行查询并返回第一个实体(同步) + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体 + public static T? QueryFirstOrDefault( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + return QueryFirstOrDefaultAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回单列值列表 + /// + /// 值类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 值列表 + public static async Task> QueryColumnAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + var result = new List(); + + using var reader = await ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout); + + while (await reader.ReadAsync()) + { + var value = reader.GetValue(0); + if (value != null && value != DBNull.Value) + { + result.Add((T)Convert.ChangeType(value, typeof(T))); + } + } + + return result; + } + + #endregion + + #region 批量操作 + + /// + /// 批量插入 + /// + /// 实体类型 + /// 数据库连接 + /// 表名 + /// 实体列表 + /// 事务 + /// 批次大小 + /// 命令超时时间 + /// 插入行数 + public static async Task BulkInsertAsync( + DbConnection connection, + string table, + IEnumerable entities, + DbTransaction? transaction = null, + int batchSize = 1000, + int? commandTimeout = null) where T : class + { + var totalRows = 0; + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + var entityList = entities.ToList(); + + for (int i = 0; i < entityList.Count; i += batchSize) + { + var batch = entityList.Skip(i).Take(batchSize); + + var columns = string.Join(", ", properties.Select(p => p.Name)); + var paramNames = string.Join(", ", properties.Select((p, idx) => $"@p{idx}")); + + var sql = $"INSERT INTO {table} ({columns}) VALUES ({paramNames})"; + + foreach (var entity in batch) + { + var parameters = new Dictionary(); + for (int j = 0; j < properties.Length; j++) + { + parameters[$"@p{j}"] = properties[j].GetValue(entity); + } + + totalRows += await ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout); + } + } + + return totalRows; + } + + /// + /// 批量更新 + /// + /// 实体类型 + /// 数据库连接 + /// 表名 + /// 实体列表 + /// 主键列名 + /// 要更新的列(null 表示更新所有非主键列) + /// 事务 + /// 命令超时时间 + /// 更新行数 + public static async Task BulkUpdateAsync( + DbConnection connection, + string table, + IEnumerable entities, + string keyColumn, + string[]? updateColumns = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : class + { + var totalRows = 0; + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + var columnsToUpdate = updateColumns ?? properties + .Select(p => p.Name) + .Where(n => n != keyColumn) + .ToArray(); + + foreach (var entity in entities) + { + var setClauses = columnsToUpdate.Select((c, i) => $"{c} = @p{i}"); + var sql = $"UPDATE {table} SET {string.Join(", ", setClauses)} WHERE {keyColumn} = @key"; + + var parameters = new Dictionary(); + for (int i = 0; i < columnsToUpdate.Length; i++) + { + var prop = properties.First(p => p.Name == columnsToUpdate[i]); + parameters[$"@p{i}"] = prop.GetValue(entity); + } + + var keyProp = properties.First(p => p.Name == keyColumn); + parameters["@key"] = keyProp.GetValue(entity); + + totalRows += await ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout); + } + + return totalRows; + } + + #endregion + + #region 事务 + + /// + /// 执行事务 + /// + /// 数据库连接 + /// 事务操作 + /// 隔离级别 + public static async Task ExecuteTransactionAsync( + DbConnection connection, + Func action, + IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) + { + if (connection.State != ConnectionState.Open) + { + await connection.OpenAsync(); + } + + using var transaction = await connection.BeginTransactionAsync(isolationLevel); + + try + { + await action(transaction); + await transaction.CommitAsync(); + } + catch + { + await transaction.RollbackAsync(); + throw; + } + } + + /// + /// 执行事务并返回结果 + /// + /// 返回类型 + /// 数据库连接 + /// 事务操作 + /// 隔离级别 + /// 结果 + public static async Task ExecuteTransactionAsync( + DbConnection connection, + Func> func, + IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) + { + if (connection.State != ConnectionState.Open) + { + await connection.OpenAsync(); + } + + using var transaction = await connection.BeginTransactionAsync(isolationLevel); + + try + { + var result = await func(transaction); + await transaction.CommitAsync(); + return result; + } + catch + { + await transaction.RollbackAsync(); + throw; + } + } + + #endregion + + #region 辅助方法 + + private static DbCommand CreateCommand( + DbConnection connection, + string sql, + Dictionary? parameters, + DbTransaction? transaction, + int? commandTimeout) + { + var command = connection.CreateCommand(); + command.CommandText = sql; + command.Transaction = transaction; + + if (commandTimeout.HasValue) + { + command.CommandTimeout = commandTimeout.Value; + } + + if (parameters != null) + { + foreach (var kvp in parameters) + { + var parameter = command.CreateParameter(); + parameter.ParameterName = kvp.Key; + parameter.Value = kvp.Value ?? DBNull.Value; + command.Parameters.Add(parameter); + } + } + + return command; + } + + private static T MapToObject(DbDataReader reader) where T : new() + { + var obj = new T(); + var type = typeof(T); + + for (int i = 0; i < reader.FieldCount; i++) + { + var name = reader.GetName(i); + var property = type.GetProperty(name, + BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + if (property != null && property.CanWrite) + { + var value = reader.GetValue(i); + + if (value != null && value != DBNull.Value) + { + if (property.PropertyType != value.GetType()) + { + value = Convert.ChangeType(value, property.PropertyType); + } + property.SetValue(obj, value); + } + } + } + + return obj; + } + + #endregion + } +} diff --git a/EasyTool.Core/DatabaseCategory/SqlBuilder.cs b/EasyTool.Core/DatabaseCategory/SqlBuilder.cs new file mode 100644 index 0000000..41d7b48 --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/SqlBuilder.cs @@ -0,0 +1,735 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.DatabaseCategory +{ + /// + /// SQL 构建器 + /// 提供流畅的 SQL 语句构建接口 + /// + public class SqlBuilder + { + private readonly StringBuilder _sql; + private readonly List _selectColumns; + private readonly List _fromTables; + private readonly List _joins; + private readonly List _whereConditions; + private readonly List _groupByColumns; + private readonly List _havingConditions; + private readonly List _orderByColumns; + private readonly Dictionary _parameters; + private string? _insertTable; + private string? _updateTable; + private string? _deleteTable; + private readonly List _insertColumns; + private readonly List _updateSets; + private int _skip; + private int _take; + private bool _distinct; + private int _paramIndex; + + /// + /// 创建 SQL 构建器 + /// + public SqlBuilder() + { + _sql = new StringBuilder(); + _selectColumns = new List(); + _fromTables = new List(); + _joins = new List(); + _whereConditions = new List(); + _groupByColumns = new List(); + _havingConditions = new List(); + _orderByColumns = new List(); + _parameters = new Dictionary(); + _insertColumns = new List(); + _updateSets = new List(); + _paramIndex = 0; + } + + #region SELECT + + /// + /// SELECT 语句 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder Select(params string[] columns) + { + _selectColumns.AddRange(columns); + return this; + } + + /// + /// SELECT DISTINCT + /// + /// 列名 + /// SqlBuilder + public SqlBuilder SelectDistinct(params string[] columns) + { + _distinct = true; + return Select(columns); + } + + /// + /// SELECT COUNT(*) + /// + /// SqlBuilder + public SqlBuilder SelectCount() + { + return Select("COUNT(*)"); + } + + /// + /// SELECT COUNT(column) + /// + /// 列名 + /// SqlBuilder + public SqlBuilder SelectCount(string column) + { + return Select($"COUNT({column})"); + } + + #endregion + + #region FROM + + /// + /// FROM 语句 + /// + /// 表名 + /// 别名 + /// SqlBuilder + public SqlBuilder From(string table, string? alias = null) + { + var from = string.IsNullOrEmpty(alias) ? table : $"{table} AS {alias}"; + _fromTables.Add(from); + return this; + } + + /// + /// FROM 子查询 + /// + /// 子查询 + /// 别名 + /// SqlBuilder + public SqlBuilder FromSubQuery(SqlBuilder subQuery, string alias) + { + var sql = subQuery.Build(); + foreach (var param in subQuery.GetParameters()) + { + _parameters[param.Key] = param.Value; + } + _fromTables.Add($"({sql}) AS {alias}"); + return this; + } + + #endregion + + #region JOIN + + /// + /// INNER JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder InnerJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"INNER JOIN {table} ON {on}" + : $"INNER JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + /// + /// LEFT JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder LeftJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"LEFT JOIN {table} ON {on}" + : $"LEFT JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + /// + /// RIGHT JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder RightJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"RIGHT JOIN {table} ON {on}" + : $"RIGHT JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + #endregion + + #region WHERE + + /// + /// WHERE 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Where(string condition) + { + _whereConditions.Add(condition); + return this; + } + + /// + /// WHERE 等于条件 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder WhereEquals(string column, object? value) + { + var paramName = AddParameter(value); + return Where($"{column} = {paramName}"); + } + + /// + /// WHERE IN 条件 + /// + /// 列名 + /// 值集合 + /// SqlBuilder + public SqlBuilder WhereIn(string column, IEnumerable values) + { + var paramNames = values.Select(v => AddParameter(v)); + return Where($"{column} IN ({string.Join(", ", paramNames)})"); + } + + /// + /// WHERE BETWEEN 条件 + /// + /// 列名 + /// 开始值 + /// 结束值 + /// SqlBuilder + public SqlBuilder WhereBetween(string column, object start, object end) + { + var startParam = AddParameter(start); + var endParam = AddParameter(end); + return Where($"{column} BETWEEN {startParam} AND {endParam}"); + } + + /// + /// WHERE LIKE 条件 + /// + /// 列名 + /// 模式 + /// SqlBuilder + public SqlBuilder WhereLike(string column, string pattern) + { + var paramName = AddParameter(pattern); + return Where($"{column} LIKE {paramName}"); + } + + /// + /// WHERE IS NULL + /// + /// 列名 + /// SqlBuilder + public SqlBuilder WhereIsNull(string column) + { + return Where($"{column} IS NULL"); + } + + /// + /// WHERE IS NOT NULL + /// + /// 列名 + /// SqlBuilder + public SqlBuilder WhereIsNotNull(string column) + { + return Where($"{column} IS NOT NULL"); + } + + /// + /// AND 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder And(string condition) + { + if (_whereConditions.Count > 0) + { + _whereConditions[_whereConditions.Count - 1] = $"({string.Join(" AND ", _whereConditions)})"; + } + _whereConditions.Add(condition); + return this; + } + + /// + /// OR 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Or(string condition) + { + if (_whereConditions.Count > 0) + { + _whereConditions[_whereConditions.Count - 1] = $"({string.Join(" OR ", _whereConditions)})"; + } + _whereConditions.Add(condition); + return this; + } + + #endregion + + #region GROUP BY / HAVING + + /// + /// GROUP BY + /// + /// 列名 + /// SqlBuilder + public SqlBuilder GroupBy(params string[] columns) + { + _groupByColumns.AddRange(columns); + return this; + } + + /// + /// HAVING 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Having(string condition) + { + _havingConditions.Add(condition); + return this; + } + + #endregion + + #region ORDER BY + + /// + /// ORDER BY 升序 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder OrderBy(params string[] columns) + { + _orderByColumns.AddRange(columns.Select(c => $"{c} ASC")); + return this; + } + + /// + /// ORDER BY 降序 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder OrderByDescending(params string[] columns) + { + _orderByColumns.AddRange(columns.Select(c => $"{c} DESC")); + return this; + } + + #endregion + + #region LIMIT / OFFSET + + /// + /// LIMIT + /// + /// 数量 + /// SqlBuilder + public SqlBuilder Take(int count) + { + _take = count; + return this; + } + + /// + /// OFFSET + /// + /// 偏移量 + /// SqlBuilder + public SqlBuilder Skip(int count) + { + _skip = count; + return this; + } + + /// + /// 分页 + /// + /// 页码(从1开始) + /// 每页大小 + /// SqlBuilder + public SqlBuilder Page(int page, int pageSize) + { + _skip = (page - 1) * pageSize; + _take = pageSize; + return this; + } + + #endregion + + #region INSERT + + /// + /// INSERT INTO + /// + /// 表名 + /// SqlBuilder + public SqlBuilder InsertInto(string table) + { + _insertTable = table; + return this; + } + + /// + /// 添加列值 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder Value(string column, object? value) + { + var paramName = AddParameter(value); + _insertColumns.Add(column); + return this; + } + + /// + /// 批量添加列值 + /// + /// 列值字典 + /// SqlBuilder + public SqlBuilder Values(Dictionary values) + { + foreach (var kvp in values) + { + var paramName = AddParameter(kvp.Value); + _insertColumns.Add(kvp.Key); + } + return this; + } + + #endregion + + #region UPDATE + + /// + /// UPDATE + /// + /// 表名 + /// SqlBuilder + public SqlBuilder Update(string table) + { + _updateTable = table; + return this; + } + + /// + /// SET 列值 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder Set(string column, object? value) + { + var paramName = AddParameter(value); + _updateSets.Add($"{column} = {paramName}"); + return this; + } + + /// + /// 批量 SET + /// + /// 列值字典 + /// SqlBuilder + public SqlBuilder SetMany(Dictionary values) + { + foreach (var kvp in values) + { + Set(kvp.Key, kvp.Value); + } + return this; + } + + #endregion + + #region DELETE + + /// + /// DELETE FROM + /// + /// 表名 + /// SqlBuilder + public SqlBuilder DeleteFrom(string table) + { + _deleteTable = table; + return this; + } + + #endregion + + #region Build + + /// + /// 构建 SQL 语句 + /// + /// SQL 字符串 + public string Build() + { + _sql.Clear(); + + // INSERT + if (_insertTable != null) + { + BuildInsert(); + } + // UPDATE + else if (_updateTable != null) + { + BuildUpdate(); + } + // DELETE + else if (_deleteTable != null) + { + BuildDelete(); + } + // SELECT + else + { + BuildSelect(); + } + + return _sql.ToString(); + } + + private void BuildSelect() + { + _sql.Append("SELECT "); + + if (_distinct) + { + _sql.Append("DISTINCT "); + } + + if (_selectColumns.Count == 0) + { + _sql.Append("*"); + } + else + { + _sql.Append(string.Join(", ", _selectColumns)); + } + + if (_fromTables.Count > 0) + { + _sql.Append(" FROM "); + _sql.Append(string.Join(", ", _fromTables)); + } + + if (_joins.Count > 0) + { + _sql.Append(" "); + _sql.Append(string.Join(" ", _joins)); + } + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + + if (_groupByColumns.Count > 0) + { + _sql.Append(" GROUP BY "); + _sql.Append(string.Join(", ", _groupByColumns)); + } + + if (_havingConditions.Count > 0) + { + _sql.Append(" HAVING "); + _sql.Append(string.Join(" AND ", _havingConditions)); + } + + if (_orderByColumns.Count > 0) + { + _sql.Append(" ORDER BY "); + _sql.Append(string.Join(", ", _orderByColumns)); + } + + if (_take > 0) + { + _sql.Append($" LIMIT {_take}"); + } + + if (_skip > 0) + { + _sql.Append($" OFFSET {_skip}"); + } + } + + private void BuildInsert() + { + var paramNames = _parameters.Keys.Take(_insertColumns.Count).ToList(); + + _sql.Append($"INSERT INTO {_insertTable} "); + _sql.Append($"({string.Join(", ", _insertColumns)}) "); + _sql.Append($"VALUES ({string.Join(", ", paramNames)})"); + } + + private void BuildUpdate() + { + _sql.Append($"UPDATE {_updateTable} "); + _sql.Append($"SET {string.Join(", ", _updateSets)}"); + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + } + + private void BuildDelete() + { + _sql.Append($"DELETE FROM {_deleteTable}"); + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + } + + /// + /// 获取参数 + /// + /// 参数字典 + public Dictionary GetParameters() + { + return new Dictionary(_parameters); + } + + private string AddParameter(object? value) + { + var paramName = $"@p{_paramIndex++}"; + _parameters[paramName] = value; + return paramName; + } + + /// + /// 重置构建器 + /// + /// SqlBuilder + public SqlBuilder Reset() + { + _sql.Clear(); + _selectColumns.Clear(); + _fromTables.Clear(); + _joins.Clear(); + _whereConditions.Clear(); + _groupByColumns.Clear(); + _havingConditions.Clear(); + _orderByColumns.Clear(); + _parameters.Clear(); + _insertColumns.Clear(); + _updateSets.Clear(); + _insertTable = null; + _updateTable = null; + _deleteTable = null; + _skip = 0; + _take = 0; + _distinct = false; + _paramIndex = 0; + return this; + } + + #endregion + } + + /// + /// SQL 构建工具类 + /// + public static class SqlBuilderUtil + { + /// + /// 创建 SQL 构建器 + /// + /// SqlBuilder + public static SqlBuilder Create() + { + return new SqlBuilder(); + } + + /// + /// 快速创建 SELECT 查询 + /// + /// 表名 + /// 列名 + /// SqlBuilder + public static SqlBuilder SelectFrom(string table, params string[] columns) + { + return new SqlBuilder().Select(columns).From(table); + } + + /// + /// 快速创建 INSERT 语句 + /// + /// 表名 + /// 列值字典 + /// SqlBuilder + public static SqlBuilder Insert(string table, Dictionary values) + { + return new SqlBuilder().InsertInto(table).Values(values); + } + + /// + /// 快速创建 UPDATE 语句 + /// + /// 表名 + /// 列值字典 + /// WHERE 条件 + /// SqlBuilder + public static SqlBuilder Update(string table, Dictionary values, string? where = null) + { + var builder = new SqlBuilder().Update(table).SetMany(values); + if (!string.IsNullOrEmpty(where)) + { + builder.Where(where); + } + return builder; + } + + /// + /// 快速创建 DELETE 语句 + /// + /// 表名 + /// WHERE 条件 + /// SqlBuilder + public static SqlBuilder Delete(string table, string? where = null) + { + var builder = new SqlBuilder().DeleteFrom(table); + if (!string.IsNullOrEmpty(where)) + { + builder.Where(where); + } + return builder; + } + } +} diff --git a/EasyTool.Core/DateTimeCategory/AgeUtil.cs b/EasyTool.Core/DateTimeCategory/AgeUtil.cs new file mode 100644 index 0000000..55c0c8b --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/AgeUtil.cs @@ -0,0 +1,288 @@ +using System; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 年龄计算工具类 + /// + public static class AgeUtil + { + /// + /// 计算年龄(周岁) + /// + public static int CalculateAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + var age = today.Year - birthDate.Year; + + // 如果生日还没到,减1岁 + if (birthDate.Date > today.AddYears(-age)) + { + age--; + } + + return Math.Max(0, age); + } + + /// + /// 计算精确年龄(岁、月、日) + /// + public static Age CalculateExactAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + + var years = today.Year - birthDate.Year; + var months = today.Month - birthDate.Month; + var days = today.Day - birthDate.Day; + + if (days < 0) + { + months--; + days += DateTime.DaysInMonth(today.Year, today.Month == 1 ? 12 : today.Month - 1); + } + + if (months < 0) + { + years--; + months += 12; + } + + return new Age + { + Years = Math.Max(0, years), + Months = Math.Max(0, months), + Days = Math.Max(0, days) + }; + } + + /// + /// 计算虚岁 + /// + public static int CalculateNominalAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return today.Year - birthDate.Year + 1; + } + + /// + /// 获取下一个生日 + /// + public static DateTime GetNextBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + var birthday = new DateTime(today.Year, birthDate.Month, birthDate.Day); + + if (birthday < today) + { + birthday = birthday.AddYears(1); + } + + return birthday; + } + + /// + /// 获取距离下一个生日的天数 + /// + public static int GetDaysUntilNextBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var nextBirthday = GetNextBirthday(birthDate, currentDate); + return (nextBirthday - (currentDate ?? DateTime.Today)).Days; + } + + /// + /// 判断今天是否是生日 + /// + public static bool IsBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return birthDate.Month == today.Month && birthDate.Day == today.Day; + } + + /// + /// 判断是否成年(默认18岁) + /// + public static bool IsAdult(DateTime birthDate, int adultAge = 18, DateTime? currentDate = null) + { + return CalculateAge(birthDate, currentDate) >= adultAge; + } + + /// + /// 获取生肖 + /// + public static string GetChineseZodiac(DateTime birthDate) + { + var zodiacs = new[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; + var index = (birthDate.Year - 1900) % 12; + return zodiacs[index >= 0 ? index : index + 12]; + } + + /// + /// 获取星座 + /// + public static string GetZodiacSign(DateTime birthDate) + { + var month = birthDate.Month; + var day = birthDate.Day; + + return (month, day) switch + { + (1, >= 20) or (2, <= 18) => "水瓶座", + (2, >= 19) or (3, <= 20) => "双鱼座", + (3, >= 21) or (4, <= 19) => "白羊座", + (4, >= 20) or (5, <= 20) => "金牛座", + (5, >= 21) or (6, <= 21) => "双子座", + (6, >= 22) or (7, <= 22) => "巨蟹座", + (7, >= 23) or (8, <= 22) => "狮子座", + (8, >= 23) or (9, <= 22) => "处女座", + (9, >= 23) or (10, <= 23) => "天秤座", + (10, >= 24) or (11, <= 22) => "天蝎座", + (11, >= 23) or (12, <= 21) => "射手座", + _ => "摩羯座" + }; + } + + /// + /// 获取星座英文 + /// + public static string GetZodiacSignEnglish(DateTime birthDate) + { + var month = birthDate.Month; + var day = birthDate.Day; + + return (month, day) switch + { + (1, >= 20) or (2, <= 18) => "Aquarius", + (2, >= 19) or (3, <= 20) => "Pisces", + (3, >= 21) or (4, <= 19) => "Aries", + (4, >= 20) or (5, <= 20) => "Taurus", + (5, >= 21) or (6, <= 21) => "Gemini", + (6, >= 22) or (7, <= 22) => "Cancer", + (7, >= 23) or (8, <= 22) => "Leo", + (8, >= 23) or (9, <= 22) => "Virgo", + (9, >= 23) or (10, <= 23) => "Libra", + (10, >= 24) or (11, <= 22) => "Scorpio", + (11, >= 23) or (12, <= 21) => "Sagittarius", + _ => "Capricorn" + }; + } + + /// + /// 计算退休年龄(男60,女干部55,女工人50) + /// + public static DateTime CalculateRetirementDate(DateTime birthDate, Gender gender, bool isCadre = false) + { + var retirementAge = gender switch + { + Gender.Male => 60, + Gender.Female when isCadre => 55, + Gender.Female => 50, + _ => 60 + }; + + return birthDate.AddYears(retirementAge); + } + + /// + /// 计算总存活天数 + /// + public static int CalculateTotalDays(DateTime birthDate, DateTime? currentDate = null) + { + return (int)((currentDate ?? DateTime.Today) - birthDate.Date).TotalDays; + } + + /// + /// 计算总存活周数 + /// + public static int CalculateTotalWeeks(DateTime birthDate, DateTime? currentDate = null) + { + return CalculateTotalDays(birthDate, currentDate) / 7; + } + + /// + /// 计算总存活月数 + /// + public static int CalculateTotalMonths(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return (today.Year - birthDate.Year) * 12 + today.Month - birthDate.Month; + } + + /// + /// 格式化年龄显示 + /// + public static string FormatAge(DateTime birthDate, DateTime? currentDate = null) + { + var age = CalculateExactAge(birthDate, currentDate); + if (age.Years > 0) + return $"{age.Years}岁{age.Months}个月"; + if (age.Months > 0) + return $"{age.Months}个月{age.Days}天"; + return $"{age.Days}天"; + } + + /// + /// 格式化年龄(简短格式) + /// + public static string FormatAgeShort(DateTime birthDate, DateTime? currentDate = null) + { + var age = CalculateExactAge(birthDate, currentDate); + if (age.Years > 0) + return $"{age.Years}岁"; + if (age.Months > 0) + return $"{age.Months}个月"; + return $"{age.Days}天"; + } + } + + /// + /// 年龄信息 + /// + public class Age + { + /// + /// 岁 + /// + public int Years { get; set; } + + /// + /// 月 + /// + public int Months { get; set; } + + /// + /// 日 + /// + public int Days { get; set; } + + /// + /// 总天数 + /// + public int TotalDays => Years * 365 + Months * 30 + Days; + + /// + /// 总月数 + /// + public int TotalMonths => Years * 12 + Months; + + public override string ToString() + { + return $"{Years}岁{Months}个月{Days}天"; + } + } + + /// + /// 性别 + /// + public enum Gender + { + /// + /// 男性 + /// + Male, + + /// + /// 女性 + /// + Female + } +} diff --git a/EasyTool.Core/DateTimeCategory/CronUtil.cs b/EasyTool.Core/DateTimeCategory/CronUtil.cs index 0123aac..2dd7a90 100644 --- a/EasyTool.Core/DateTimeCategory/CronUtil.cs +++ b/EasyTool.Core/DateTimeCategory/CronUtil.cs @@ -7,332 +7,419 @@ namespace EasyTool.DateTimeCategory { /// /// Cron 表达式工具类 - /// 支持 Cron 表达式解析、验证和下一次执行时间计算 + /// 提供 Cron 表达式的解析和计算下次执行时间 /// public static class CronUtil { - /// - /// 解析 Cron 表达式 - /// - public static CronExpression Parse(string cronExpression) - { - return new CronExpression(cronExpression); - } + private static readonly Regex CronRegex = new(@"^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$", RegexOptions.Compiled); /// /// 验证 Cron 表达式是否有效 /// + /// Cron 表达式 + /// 是否有效 public static bool IsValid(string cronExpression) { - try - { - new CronExpression(cronExpression); - return true; - } - catch - { + if (string.IsNullOrWhiteSpace(cronExpression)) return false; - } - } - /// - /// 获取下一次执行时间 - /// - public static DateTime? GetNextExecution(string cronExpression, DateTime from) - { - return Parse(cronExpression).GetNextExecution(from); + var match = CronRegex.Match(cronExpression); + if (!match.Success) + return false; + + return IsValidField(match.Groups[1].Value, 0, 59) && // 秒 + IsValidField(match.Groups[2].Value, 0, 59) && // 分 + IsValidField(match.Groups[3].Value, 0, 23) && // 时 + IsValidField(match.Groups[4].Value, 1, 31) && // 日 + IsValidField(match.Groups[5].Value, 1, 12); // 月 } - /// - /// 获取接下来的N次执行时间 - /// - public static IEnumerable GetNextExecutions(string cronExpression, DateTime from, int count) + private static bool IsValidField(string field, int min, int max) { - return Parse(cronExpression).GetNextExecutions(from, count); - } - } + if (field == "*") + return true; - /// - /// Cron 表达式 - /// - public class CronExpression - { - private static readonly int[] MonthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + foreach (var part in field.Split(',')) + { + var trimmedPart = part.Trim(); - private readonly HashSet _seconds; - private readonly HashSet _minutes; - private readonly HashSet _hours; - private readonly HashSet _daysOfMonth; - private readonly HashSet _months; - private readonly HashSet _daysOfWeek; + if (trimmedPart == "*") + continue; - /// - /// 原始表达式 - /// - public string Expression { get; } + if (trimmedPart.Contains('/')) + { + var slashParts = trimmedPart.Split('/'); + if (slashParts.Length != 2) + return false; - /// - /// 创建 Cron 表达式 - /// - public CronExpression(string expression) + if (slashParts[0] != "*" && !IsValidRangeOrNumber(slashParts[0], min, max)) + return false; + + if (!int.TryParse(slashParts[1], out var step) || step <= 0) + return false; + } + else if (!IsValidRangeOrNumber(trimmedPart, min, max)) + { + return false; + } + } + + return true; + } + + private static bool IsValidRangeOrNumber(string value, int min, int max) { - if (string.IsNullOrWhiteSpace(expression)) - throw new ArgumentException("Cron expression cannot be empty"); + if (value.Contains('-')) + { + var rangeParts = value.Split('-'); + if (rangeParts.Length != 2) + return false; - Expression = expression; + if (!int.TryParse(rangeParts[0], out var start) || !int.TryParse(rangeParts[1], out var end)) + return false; - var parts = expression.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length != 5 && parts.Length != 6) - throw new ArgumentException("Cron expression must have 5 or 6 fields"); + return start >= min && start <= max && end >= min && end <= max && start <= end; + } - int offset = parts.Length == 6 ? 0 : 1; + if (int.TryParse(value, out var num)) + return num >= min && num <= max; - _seconds = parts.Length == 6 ? ParseField(parts[0], 0, 59) : new HashSet { 0 }; - _minutes = ParseField(parts[offset], 0, 59); - _hours = ParseField(parts[offset + 1], 0, 23); - _daysOfMonth = ParseField(parts[offset + 2], 1, 31); - _months = ParseField(parts[offset + 3], 1, 12); - _daysOfWeek = ParseField(parts[offset + 4], 0, 6); + return false; } /// - /// 获取下一次执行时间 + /// 获取下次执行时间 /// - public DateTime? GetNextExecution(DateTime from) + /// Cron 表达式(秒 分 时 日 月) + /// 起始时间 + /// 下次执行时间 + public static DateTime GetNextExecutionTime(string cronExpression, DateTime? fromTime = null) { - return GetNextExecutions(from, 1).FirstOrDefault(); - } + if (!IsValid(cronExpression)) + throw new ArgumentException("无效的 Cron 表达式", nameof(cronExpression)); - /// - /// 获取接下来的N次执行时间 - /// - public IEnumerable GetNextExecutions(DateTime from, int count) - { - var current = from.AddSeconds(1); - int found = 0; - int maxIterations = 366 * 24 * 60 * 60; // 最多查找一年 + var parts = cronExpression.Split(' '); + var secondField = parts[0]; + var minuteField = parts[1]; + var hourField = parts[2]; + var dayField = parts[3]; + var monthField = parts[4]; - while (found < count && maxIterations-- > 0) + var currentTime = fromTime ?? DateTime.Now; + var nextTime = currentTime.AddSeconds(1); + + while (true) { - if (Matches(current)) + // 检查月份 + if (!IsFieldMatch(monthField, nextTime.Month, 1, 12)) { - yield return current; - found++; - current = current.AddSeconds(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, 1).AddMonths(1); + continue; } - else + + // 检查日期 + var daysInMonth = DateTime.DaysInMonth(nextTime.Year, nextTime.Month); + if (!IsFieldMatch(dayField, nextTime.Day, 1, daysInMonth)) + { + nextTime = nextTime.AddDays(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day); + continue; + } + + // 检查小时 + if (!IsFieldMatch(hourField, nextTime.Hour, 0, 23)) + { + nextTime = nextTime.AddHours(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day, nextTime.Hour, 0, 0); + continue; + } + + // 检查分钟 + if (!IsFieldMatch(minuteField, nextTime.Minute, 0, 59)) + { + nextTime = nextTime.AddMinutes(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day, nextTime.Hour, nextTime.Minute, 0); + continue; + } + + // 检查秒 + if (!IsFieldMatch(secondField, nextTime.Second, 0, 59)) { - current = SkipToNextCandidate(current); + nextTime = nextTime.AddSeconds(1); + continue; } + + return nextTime; } } /// - /// 判断指定时间是否匹配 + /// 获取接下来的多次执行时间 /// - public bool Matches(DateTime time) + /// Cron 表达式 + /// 获取次数 + /// 起始时间 + /// 执行时间列表 + public static List GetNextExecutionTimes(string cronExpression, int count, DateTime? fromTime = null) { - if (!_seconds.Contains(time.Second)) return false; - if (!_minutes.Contains(time.Minute)) return false; - if (!_hours.Contains(time.Hour)) return false; - if (!_months.Contains(time.Month)) return false; - - // 日和周是"或"关系 - bool dayMatch = _daysOfMonth.Contains(time.Day); - bool weekMatch = _daysOfWeek.Contains((int)time.DayOfWeek); - - // 如果两者都设置了,只要有一个匹配即可 - // 如果其中一个设置为*,则另一个起作用 - if (_daysOfMonth.Contains(-1) && _daysOfWeek.Contains(-1)) - return true; - if (_daysOfMonth.Contains(-1)) - return weekMatch; - if (_daysOfWeek.Contains(-1)) - return dayMatch; + var result = new List(); + var nextTime = fromTime ?? DateTime.Now; + + for (int i = 0; i < count; i++) + { + nextTime = GetNextExecutionTime(cronExpression, nextTime); + result.Add(nextTime); + } - return dayMatch || weekMatch; + return result; } - private DateTime SkipToNextCandidate(DateTime current) + private static bool IsFieldMatch(string field, int value, int min, int max) { - // 优化:跳过不可能匹配的时间 - if (!_months.Contains(current.Month)) - { - // 跳到下个月 - return new DateTime(current.Year, current.Month, 1).AddMonths(1); - } + if (field == "*") + return true; - if (!_hours.Contains(current.Hour)) + foreach (var part in field.Split(',')) { - // 跳到下一个小时 - return current.AddHours(1).AddMinutes(-current.Minute).AddSeconds(-current.Second); - } + var trimmedPart = part.Trim(); - if (!_minutes.Contains(current.Minute)) - { - // 跳到下一分钟 - return current.AddMinutes(1).AddSeconds(-current.Second); + if (trimmedPart == "*") + return true; + + if (trimmedPart.Contains('/')) + { + var slashParts = trimmedPart.Split('/'); + var step = int.Parse(slashParts[1]); + + if (slashParts[0] == "*") + { + if ((value - min) % step == 0) + return true; + } + else + { + var start = int.Parse(slashParts[0]); + if (value >= start && (value - start) % step == 0) + return true; + } + } + else if (trimmedPart.Contains('-')) + { + var rangeParts = trimmedPart.Split('-'); + var start = int.Parse(rangeParts[0]); + var end = int.Parse(rangeParts[1]); + + if (value >= start && value <= end) + return true; + } + else + { + if (int.Parse(trimmedPart) == value) + return true; + } } - // 逐秒查找 - return current.AddSeconds(1); + return false; } - private static HashSet ParseField(string field, int min, int max) + /// + /// 获取字段匹配的所有值 + /// + /// 字段表达式 + /// 最小值 + /// 最大值 + /// 匹配的值列表 + public static List GetFieldValues(string field, int min, int max) { var result = new HashSet(); - int wildcard = -1; - - // 处理 L (Last) - if (field == "L") - { - result.Add(max); - return result; - } - // 处理 * 或 ? - if (field == "*" || field == "?") + if (field == "*") { - result.Add(wildcard); - return result; + for (int i = min; i <= max; i++) + result.Add(i); + return result.OrderBy(x => x).ToList(); } - // 分割逗号分隔的部分 foreach (var part in field.Split(',')) { - string currentPart = part.Trim(); + var trimmedPart = part.Trim(); - // 处理步长 - int step = 1; - if (currentPart.Contains('/')) + if (trimmedPart == "*") { - var stepParts = currentPart.Split('/'); - currentPart = stepParts[0]; - step = int.Parse(stepParts[1]); + for (int i = min; i <= max; i++) + result.Add(i); } - - // 处理范围 - int start, end; - if (currentPart == "*") + else if (trimmedPart.Contains('/')) { - start = min; - end = max; + var slashParts = trimmedPart.Split('/'); + var step = int.Parse(slashParts[1]); + int start; + + if (slashParts[0] == "*") + { + start = min; + } + else + { + start = int.Parse(slashParts[0]); + } + + for (int i = start; i <= max; i += step) + result.Add(i); } - else if (currentPart.Contains('-')) + else if (trimmedPart.Contains('-')) { - var rangeParts = currentPart.Split('-'); - start = int.Parse(rangeParts[0]); - end = int.Parse(rangeParts[1]); + var rangeParts = trimmedPart.Split('-'); + var start = int.Parse(rangeParts[0]); + var end = int.Parse(rangeParts[1]); + + for (int i = start; i <= end; i++) + result.Add(i); } else { - start = end = int.Parse(currentPart); - } - - for (int i = start; i <= end; i += step) - { - if (i >= min && i <= max) - result.Add(i); + result.Add(int.Parse(trimmedPart)); } } - return result; + return result.Where(x => x >= min && x <= max).OrderBy(x => x).ToList(); } /// - /// 获取可读的描述 + /// 解析 Cron 表达式为可读文本 /// - public string GetDescription() + /// Cron 表达式 + /// 可读文本 + public static string ToDescription(string cronExpression) { - var parts = Expression.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - int offset = parts.Length == 6 ? 0 : 1; + if (!IsValid(cronExpression)) + throw new ArgumentException("无效的 Cron 表达式", nameof(cronExpression)); - var desc = new System.Text.StringBuilder(); - desc.Append("在"); + var parts = cronExpression.Split(' '); + var secondField = parts[0]; + var minuteField = parts[1]; + var hourField = parts[2]; + var dayField = parts[3]; + var monthField = parts[4]; - if (_hours.Contains(-1) && _minutes.Contains(-1)) - desc.Append("每分钟"); - else if (_hours.Contains(-1)) - desc.Append($"每小时的第{_stringify(_minutes)}分钟"); - else - desc.Append($"{_stringify(_hours)}时{_stringify(_minutes)}分"); + var descriptions = new List(); - if (!_daysOfMonth.Contains(-1) || !_daysOfWeek.Contains(-1)) - { - desc.Append(","); - if (!_daysOfMonth.Contains(-1)) - desc.Append($"每月{_stringify(_daysOfMonth)}日"); - if (!_daysOfWeek.Contains(-1)) - { - if (!_daysOfMonth.Contains(-1)) desc.Append("或"); - desc.Append($"每周{_stringify(_daysOfWeek)}"); - } - } + // 秒 + if (secondField != "*") + descriptions.Add($"第 {FieldToDescription(secondField)} 秒"); - if (!_months.Contains(-1)) - desc.Append($",{_stringify(_months)}月"); + // 分 + if (minuteField != "*") + descriptions.Add($"第 {FieldToDescription(minuteField)} 分钟"); - desc.Append("执行"); + // 时 + if (hourField != "*") + descriptions.Add($"第 {FieldToDescription(hourField)} 小时"); - return desc.ToString(); + // 日 + if (dayField != "*") + descriptions.Add($"每月 {FieldToDescription(dayField)} 日"); + + // 月 + if (monthField != "*") + descriptions.Add($"{FieldToDescription(monthField)} 月"); + + if (descriptions.Count == 0) + return "每秒执行"; + + return string.Join(",", descriptions) + " 执行"; } - private static string _stringify(HashSet set) + private static string FieldToDescription(string field) { - if (set.Contains(-1)) return "每"; - var sorted = set.OrderBy(x => x).ToList(); - if (sorted.Count == 1) return sorted[0].ToString(); - return string.Join(",", sorted); - } + if (field == "*") + return "每"; - public override string ToString() => Expression; - } + if (field.Contains('/')) + { + var parts = field.Split('/'); + return parts[0] == "*" ? $"每隔 {parts[1]}" : $"从 {parts[0]} 开始每隔 {parts[1]}"; + } - /// - /// 常用 Cron 表达式 - /// - public static class CronExpressions - { - /// 每分钟 - public static string EveryMinute => "* * * * *"; + if (field.Contains('-')) + { + var parts = field.Split('-'); + return $"{parts[0]} 到 {parts[1]}"; + } - /// 每小时 - public static string EveryHour => "0 * * * *"; + return field; + } - /// 每天午夜 - public static string Daily => "0 0 * * *"; + #region 常用 Cron 表达式 - /// 每天中午 - public static string DailyNoon => "0 12 * * *"; + /// + /// 每秒执行 + /// + public static string EverySecond => "* * * * *"; - /// 每周一 - public static string WeeklyMonday => "0 0 * * 1"; + /// + /// 每分钟执行(每分钟的第 0 秒) + /// + public static string EveryMinute => "0 * * * *"; - /// 每月1号 - public static string Monthly => "0 0 1 * *"; + /// + /// 每小时执行(每小时的第 0 分 0 秒) + /// + public static string EveryHour => "0 0 * * *"; - /// 每年1月1日 - public static string Yearly => "0 0 1 1 *"; + /// + /// 每天执行(每天的 00:00:00) + /// + public static string EveryDay => "0 0 0 * *"; - /// 工作日 - public static string Weekdays => "0 0 * * 1-5"; + /// + /// 每月执行(每月 1 日的 00:00:00) + /// + public static string EveryMonth => "0 0 0 1 *"; - /// 周末 - public static string Weekends => "0 0 * * 0,6"; + /// + /// 每隔 N 秒执行 + /// + public static string EveryNSeconds(int n) => $"*/{n} * * * *"; + + /// + /// 每隔 N 分钟执行 + /// + public static string EveryNMinutes(int n) => $"0 */{n} * * *"; + + /// + /// 每隔 N 小时执行 + /// + public static string EveryNHours(int n) => $"0 0 */{n} * *"; /// - /// 每5分钟 + /// 每天指定时间执行 /// - public static string EveryNMinutes(int n) => $"*/{n} * * * *"; + /// 小时 + /// 分钟 + /// 秒 + public static string DailyAt(int hour, int minute = 0, int second = 0) => $"{second} {minute} {hour} * *"; /// - /// 每N小时 + /// 每周指定时间执行(周一为 1,周日为 7) /// - public static string EveryNHours(int n) => $"0 */{n} * * *"; + /// 星期几(1-7) + /// 小时 + /// 分钟 + /// 秒 + public static string WeeklyAt(int dayOfWeek, int hour = 0, int minute = 0, int second = 0) + => $"{second} {minute} {hour} * *"; /// - /// 每天指定时间 + /// 每月指定日期时间执行 /// - public static string DailyAt(int hour, int minute = 0) => $"{minute} {hour} * * *"; + /// 日期 + /// 小时 + /// 分钟 + /// 秒 + public static string MonthlyAt(int day, int hour = 0, int minute = 0, int second = 0) + => $"{second} {minute} {hour} {day} *"; + + #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/HolidayUtil.cs b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs index a9aaebd..da02e26 100644 --- a/EasyTool.Core/DateTimeCategory/HolidayUtil.cs +++ b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs @@ -5,311 +5,96 @@ namespace EasyTool.DateTimeCategory { /// /// 节假日工具类 - /// 支持中国法定节假日和常见国际节日 /// public static class HolidayUtil { /// - /// 判断是否为中国法定节假日 + /// 获取指定年份的中国法定节假日 /// - public static bool IsChineseHoliday(DateTime date) + public static List GetChineseHolidays(int year) { - return GetChineseHoliday(date) != null; - } - - /// - /// 获取中国节假日名称 - /// - public static string GetChineseHoliday(DateTime date) - { - int year = date.Year; - int month = date.Month; - int day = date.Day; - - // 固定日期节日 - if (month == 1 && day == 1) return "元旦"; - if (month == 5 && day == 1) return "劳动节"; - if (month == 10 && day == 1) return "国庆节"; - - // 农历节日(简化计算,使用近似日期) - var lunar = LunarCalendarUtil.SolarToLunar(date); - if (lunar != null) - { - if (lunar.Month == 1 && lunar.Day == 1) return "春节"; - if (lunar.Month == 1 && lunar.Day == 15) return "元宵节"; - if (lunar.Month == 5 && lunar.Day == 5) return "端午节"; - if (lunar.Month == 8 && lunar.Day == 15) return "中秋节"; - if (lunar.Month == 9 && lunar.Day == 9) return "重阳节"; - if (lunar.Month == 12 && lunar.Day == 30) return "除夕"; - } - - // 母亲节:5月第二个星期日 - if (month == 5) - { - var motherDay = GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2); - if (day == motherDay) return "母亲节"; - } - - // 父亲节:6月第三个星期日 - if (month == 6) - { - var fatherDay = GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3); - if (day == fatherDay) return "父亲节"; - } - - // 教师节:9月10日 - if (month == 9 && day == 10) return "教师节"; - - // 清明节:4月4日或5日(简化) - var qingming = GetQingmingDate(year); - if (month == 4 && day == qingming) return "清明节"; - - // 儿童节:6月1日 - if (month == 6 && day == 1) return "儿童节"; - - // 妇女节:3月8日 - if (month == 3 && day == 8) return "妇女节"; - - // 植树节:3月12日 - if (month == 3 && day == 12) return "植树节"; - - // 青年节:5月4日 - if (month == 5 && day == 4) return "青年节"; + var holidays = new List(); - // 建党节:7月1日 - if (month == 7 && day == 1) return "建党节"; + // 元旦 + holidays.Add(new DateTime(year, 1, 1)); - // 建军节:8月1日 - if (month == 8 && day == 1) return "建军节"; + // 春节(简化处理,实际需要根据农历计算) + holidays.Add(new DateTime(year, 1, 1)); + holidays.Add(new DateTime(year, 1, 2)); + holidays.Add(new DateTime(year, 1, 3)); - return null; - } + // 清明节(4月4日或5日) + holidays.Add(GetQingmingDate(year)); - /// - /// 判断是否为国际常见节日 - /// - public static bool IsInternationalHoliday(DateTime date) - { - return GetInternationalHoliday(date) != null; - } + // 劳动节 + holidays.Add(new DateTime(year, 5, 1)); + holidays.Add(new DateTime(year, 5, 2)); + holidays.Add(new DateTime(year, 5, 3)); - /// - /// 获取国际节日名称 - /// - public static string GetInternationalHoliday(DateTime date) - { - int month = date.Month; - int day = date.Day; - int year = date.Year; - - // 固定日期 - if (month == 1 && day == 1) return "New Year's Day"; - if (month == 2 && day == 14) return "Valentine's Day"; - if (month == 3 && day == 8) return "International Women's Day"; - if (month == 3 && day == 12) return "Arbor Day"; - if (month == 3 && day == 21) return "World Sleep Day"; - if (month == 4 && day == 1) return "April Fools' Day"; - if (month == 4 && day == 22) return "Earth Day"; - if (month == 4 && day == 23) return "World Book Day"; - if (month == 5 && day == 1) return "International Workers' Day"; - if (month == 5 && day == 4) return "Star Wars Day"; - if (month == 6 && day == 1) return "International Children's Day"; - if (month == 6 && day == 5) return "World Environment Day"; - if (month == 9 && day == 21) return "International Day of Peace"; - if (month == 10 && day == 31) return "Halloween"; - if (month == 11 && day == 11) return "Veterans Day / Singles' Day"; - if (month == 12 && day == 24) return "Christmas Eve"; - if (month == 12 && day == 25) return "Christmas Day"; - if (month == 12 && day == 31) return "New Year's Eve"; - - // 复活节(春分后第一个满月后的第一个星期日) - var easter = CalculateEaster(year); - if (date == easter) return "Easter Sunday"; - if (date == easter.AddDays(-2)) return "Good Friday"; - if (date == easter.AddDays(1)) return "Easter Monday"; - - // 感恩节(11月第四个星期四) - var thanksgiving = GetNthDayOfWeek(year, 11, DayOfWeek.Thursday, 4); - if (date.Day == thanksgiving && month == 11) return "Thanksgiving"; - - // 黑色星期五(感恩节后一天) - if (month == 11 && date.Day == thanksgiving + 1) return "Black Friday"; - - return null; - } + // 端午节(简化处理) + holidays.Add(new DateTime(year, 6, 1)); - /// - /// 获取指定年份的所有中国节假日 - /// - public static Dictionary GetChineseHolidays(int year) - { - var holidays = new Dictionary(); - - // 固定日期节日 - holidays[new DateTime(year, 1, 1)] = "元旦"; - holidays[new DateTime(year, 5, 1)] = "劳动节"; - holidays[new DateTime(year, 10, 1)] = "国庆节"; + // 中秋节(简化处理) + holidays.Add(new DateTime(year, 9, 15)); - // 清明节 - int qingming = GetQingmingDate(year); - holidays[new DateTime(year, 4, qingming)] = "清明节"; - - // 农历节日(需要转换) - // 春节(农历正月初一) - var springFestival = LunarToSolar(year, 1, 1); - if (springFestival.HasValue) - { - holidays[springFestival.Value] = "春节"; - holidays[springFestival.Value.AddDays(-1)] = "除夕"; - } - - // 元宵节 - var lanternFestival = LunarToSolar(year, 1, 15); - if (lanternFestival.HasValue) - holidays[lanternFestival.Value] = "元宵节"; - - // 端午节 - var dragonBoat = LunarToSolar(year, 5, 5); - if (dragonBoat.HasValue) - holidays[dragonBoat.Value] = "端午节"; - - // 中秋节 - var midAutumn = LunarToSolar(year, 8, 15); - if (midAutumn.HasValue) - holidays[midAutumn.Value] = "中秋节"; - - // 其他节日 - holidays[new DateTime(year, 3, 8)] = "妇女节"; - holidays[new DateTime(year, 3, 12)] = "植树节"; - holidays[new DateTime(year, 5, 4)] = "青年节"; - holidays[new DateTime(year, 6, 1)] = "儿童节"; - holidays[new DateTime(year, 7, 1)] = "建党节"; - holidays[new DateTime(year, 8, 1)] = "建军节"; - holidays[new DateTime(year, 9, 10)] = "教师节"; - - // 母亲节、父亲节 - var motherDay = GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2); - holidays[new DateTime(year, 5, motherDay)] = "母亲节"; - - var fatherDay = GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3); - holidays[new DateTime(year, 6, fatherDay)] = "父亲节"; + // 国庆节 + holidays.Add(new DateTime(year, 10, 1)); + holidays.Add(new DateTime(year, 10, 2)); + holidays.Add(new DateTime(year, 10, 3)); + holidays.Add(new DateTime(year, 10, 4)); + holidays.Add(new DateTime(year, 10, 5)); + holidays.Add(new DateTime(year, 10, 6)); + holidays.Add(new DateTime(year, 10, 7)); return holidays; } /// - /// 获取清明节的日期(4月4日或5日) + /// 获取清明节日期 /// - private static int GetQingmingDate(int year) + private static DateTime GetQingmingDate(int year) { - // 清明节大约在公历4月4日或5日 - // 使用简化算法 - int y = year % 100; - int d = (y * 0.2422 + 4.81) % 1 > 0.5 ? 4 : 5; - return d; - } - - /// - /// 获取某月第N个某星期几的日期 - /// - private static int GetNthDayOfWeek(int year, int month, DayOfWeek dayOfWeek, int n) - { - var firstDay = new DateTime(year, month, 1); - int offset = ((int)dayOfWeek - (int)firstDay.DayOfWeek + 7) % 7; - return 1 + offset + (n - 1) * 7; - } - - /// - /// 计算复活节日期 - /// - private static DateTime CalculateEaster(int year) - { - int a = year % 19; - int b = year / 100; - int c = year % 100; - int d = b / 4; - int e = b % 4; - int f = (b + 8) / 25; - int g = (b - f + 1) / 3; - int h = (19 * a + b - d - g + 15) % 30; - int i = c / 4; - int k = c % 4; - int l = (32 + 2 * e + 2 * i - h - k) % 7; - int m = (a + 11 * h + 22 * l) / 451; - int month = (h + l - 7 * m + 114) / 31; - int day = ((h + l - 7 * m + 114) % 31) + 1; - - return new DateTime(year, month, day); - } - - /// - /// 农历转公历(简化版) - /// - private static DateTime? LunarToSolar(int year, int lunarMonth, int lunarDay) - { - // 这里需要使用 LunarCalendarUtil,如果不存在则返回 null - try + // 清明节通常在4月4日或5日 + var day = 5; + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { - return LunarCalendarUtil.LunarToSolar(year, lunarMonth, lunarDay); - } - catch - { - return null; + day = 4; } + return new DateTime(year, 4, day); } - } - - /// - /// 工作日工具类 - /// - public static class WorkdayUtil - { - private static readonly HashSet _holidays = new(); - private static readonly HashSet _workdays = new(); // 调休工作日 /// - /// 设置节假日 + /// 判断是否为工作日 /// - public static void SetHoliday(DateTime date) + public static bool IsWorkday(DateTime date, List? holidays = null, List? workdays = null) { - _holidays.Add(date.Date); - _workdays.Remove(date.Date); - } + // 检查是否为调休工作日 + if (workdays != null && workdays.Contains(date.Date)) + return true; - /// - /// 设置调休工作日 - /// - public static void SetWorkday(DateTime date) - { - _workdays.Add(date.Date); - _holidays.Remove(date.Date); + // 检查是否为假日 + if (holidays != null && holidays.Contains(date.Date)) + return false; + + // 周一至周五为工作日 + return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday; } /// - /// 判断是否为工作日 + /// 判断是否为周末 /// - public static bool IsWorkday(DateTime date) + public static bool IsWeekend(DateTime date) { - date = date.Date; - - // 优先检查调休工作日 - if (_workdays.Contains(date)) return true; - - // 检查节假日 - if (_holidays.Contains(date)) return false; - if (HolidayUtil.IsChineseHoliday(date)) return false; - - // 默认周一到周五为工作日 - return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday; + return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; } /// /// 获取下一个工作日 /// - public static DateTime GetNextWorkday(DateTime date) + public static DateTime GetNextWorkday(DateTime date, List? holidays = null, List? workdays = null) { - var next = date.Date.AddDays(1); - while (!IsWorkday(next)) + var next = date.AddDays(1); + while (!IsWorkday(next, holidays, workdays)) { next = next.AddDays(1); } @@ -319,10 +104,10 @@ public static DateTime GetNextWorkday(DateTime date) /// /// 获取上一个工作日 /// - public static DateTime GetPreviousWorkday(DateTime date) + public static DateTime GetPreviousWorkday(DateTime date, List? holidays = null, List? workdays = null) { - var prev = date.Date.AddDays(-1); - while (!IsWorkday(prev)) + var prev = date.AddDays(-1); + while (!IsWorkday(prev, holidays, workdays)) { prev = prev.AddDays(-1); } @@ -330,36 +115,36 @@ public static DateTime GetPreviousWorkday(DateTime date) } /// - /// 计算两个日期之间的工作日数量 + /// 计算工作日数量 /// - public static int CountWorkdays(DateTime start, DateTime end) + public static int CountWorkdays(DateTime start, DateTime end, List? holidays = null, List? workdays = null) { - if (start > end) - (start, end) = (end, start); - - int count = 0; + var count = 0; var current = start.Date; + while (current <= end.Date) { - if (IsWorkday(current)) count++; + if (IsWorkday(current, holidays, workdays)) + count++; current = current.AddDays(1); } + return count; } /// /// 添加工作日 /// - public static DateTime AddWorkdays(DateTime date, int days) + public static DateTime AddWorkdays(DateTime date, int days, List? holidays = null, List? workdays = null) { - var result = date.Date; - int step = days > 0 ? 1 : -1; - int remaining = Math.Abs(days); + var result = date; + var increment = days > 0 ? 1 : -1; + var remaining = Math.Abs(days); while (remaining > 0) { - result = result.AddDays(step); - if (IsWorkday(result)) + result = result.AddDays(increment); + if (IsWorkday(result, holidays, workdays)) remaining--; } @@ -367,138 +152,119 @@ public static DateTime AddWorkdays(DateTime date, int days) } /// - /// 清空节假日和调休设置 + /// 获取西式节日 /// - public static void Clear() + public static DateTime GetWesternHoliday(int year, WesternHoliday holiday) { - _holidays.Clear(); - _workdays.Clear(); + return holiday switch + { + WesternHoliday.NewYear => new DateTime(year, 1, 1), + WesternHoliday.ValentinesDay => new DateTime(year, 2, 14), + WesternHoliday.StPatricksDay => new DateTime(year, 3, 17), + WesternHoliday.AprilFools => new DateTime(year, 4, 1), + WesternHoliday.IndependenceDay => new DateTime(year, 7, 4), + WesternHoliday.Halloween => new DateTime(year, 10, 31), + WesternHoliday.VeteransDay => new DateTime(year, 11, 11), + WesternHoliday.Christmas => new DateTime(year, 12, 25), + WesternHoliday.Thanksgiving => GetNthDayOfWeek(year, 11, DayOfWeek.Thursday, 4), + WesternHoliday.MothersDay => GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2), + WesternHoliday.FathersDay => GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3), + WesternHoliday.LaborDay => GetNthDayOfWeek(year, 9, DayOfWeek.Monday, 1), + WesternHoliday.MemorialDay => GetLastDayOfWeek(year, 5, DayOfWeek.Monday), + _ => throw new ArgumentOutOfRangeException(nameof(holiday)) + }; } - } - /// - /// 友好时间显示工具类 - /// - public static class TimeAgoUtil - { /// - /// 获取友好时间显示(如"3分钟前"、"昨天") + /// 获取某月第N个星期几 /// - public static string Format(DateTime date, DateTime? now = null) + private static DateTime GetNthDayOfWeek(int year, int month, DayOfWeek dayOfWeek, int n) { - return Format(date, now ?? DateTime.Now, false); + var firstDay = new DateTime(year, month, 1); + var daysToAdd = ((int)dayOfWeek - (int)firstDay.DayOfWeek + 7) % 7; + var result = firstDay.AddDays(daysToAdd + (n - 1) * 7); + return result; } /// - /// 获取友好时间显示(英文版) + /// 获取某月最后一个星期几 /// - public static string FormatEnglish(DateTime date, DateTime? now = null) + private static DateTime GetLastDayOfWeek(int year, int month, DayOfWeek dayOfWeek) { - return Format(date, now ?? DateTime.Now, true); + var lastDay = new DateTime(year, month, DateTime.DaysInMonth(year, month)); + var daysToSubtract = ((int)lastDay.DayOfWeek - (int)dayOfWeek + 7) % 7; + return lastDay.AddDays(-daysToSubtract); } + } - private static string Format(DateTime date, DateTime now, bool english) - { - var span = now - date; - - if (span.TotalSeconds < 0) - { - return english ? "in the future" : "未来"; - } - - if (span.TotalSeconds < 60) - { - int seconds = (int)span.TotalSeconds; - return english ? $"{seconds} second{(seconds != 1 ? "s" : "")} ago" : $"{seconds}秒前"; - } - - if (span.TotalMinutes < 60) - { - int minutes = (int)span.TotalMinutes; - return english ? $"{minutes} minute{(minutes != 1 ? "s" : "")} ago" : $"{minutes}分钟前"; - } - - if (span.TotalHours < 24) - { - int hours = (int)span.TotalHours; - return english ? $"{hours} hour{(hours != 1 ? "s" : "")} ago" : $"{hours}小时前"; - } - - if (span.TotalDays < 2 && date.Date == now.Date.AddDays(-1)) - { - return english ? "yesterday" : "昨天"; - } - - if (span.TotalDays < 7) - { - int days = (int)span.TotalDays; - return english ? $"{days} day{(days != 1 ? "s" : "")} ago" : $"{days}天前"; - } + /// + /// 西式节日 + /// + public enum WesternHoliday + { + /// + /// 元旦 + /// + NewYear, - if (span.TotalDays < 30) - { - int weeks = (int)(span.TotalDays / 7); - return english ? $"{weeks} week{(weeks != 1 ? "s" : "")} ago" : $"{weeks}周前"; - } + /// + /// 情人节 + /// + ValentinesDay, - if (span.TotalDays < 365) - { - int months = (int)(span.TotalDays / 30); - return english ? $"{months} month{(months != 1 ? "s" : "")} ago" : $"{months}个月前"; - } + /// + /// 圣帕特里克节 + /// + StPatricksDay, - int years = (int)(span.TotalDays / 365); - return english ? $"{years} year{(years != 1 ? "s" : "")} ago" : $"{years}年前"; - } + /// + /// 愚人节 + /// + AprilFools, /// - /// 获取剩余时间显示(如"剩余3天") + /// 美国独立日 /// - public static string FormatRemaining(DateTime deadline, DateTime? now = null) - { - return FormatRemaining(deadline, now ?? DateTime.Now, false); - } + IndependenceDay, - private static string FormatRemaining(DateTime deadline, DateTime now, bool english) - { - var span = deadline - now; + /// + /// 万圣节 + /// + Halloween, - if (span.TotalSeconds < 0) - { - return english ? "overdue" : "已过期"; - } + /// + /// 退伍军人节 + /// + VeteransDay, - if (span.TotalMinutes < 1) - { - return english ? "less than 1 minute" : "不到1分钟"; - } + /// + /// 圣诞节 + /// + Christmas, - if (span.TotalHours < 1) - { - int minutes = (int)span.TotalMinutes; - return english ? $"{minutes} minute{(minutes != 1 ? "s" : "")} remaining" : $"剩余{minutes}分钟"; - } + /// + /// 感恩节 + /// + Thanksgiving, - if (span.TotalDays < 1) - { - int hours = (int)span.TotalHours; - return english ? $"{hours} hour{(hours != 1 ? "s" : "")} remaining" : $"剩余{hours}小时"; - } + /// + /// 母亲节 + /// + MothersDay, - if (span.TotalDays < 7) - { - int days = (int)span.TotalDays; - return english ? $"{days} day{(days != 1 ? "s" : "")} remaining" : $"剩余{days}天"; - } + /// + /// 父亲节 + /// + FathersDay, - if (span.TotalDays < 30) - { - int weeks = (int)(span.TotalDays / 7); - return english ? $"{weeks} week{(weeks != 1 ? "s" : "")} remaining" : $"剩余{weeks}周"; - } + /// + /// 劳动节(美国) + /// + LaborDay, - int months = (int)(span.TotalDays / 30); - return english ? $"{months} month{(months != 1 ? "s" : "")} remaining" : $"剩余{months}个月"; - } + /// + /// 阵亡将士纪念日(美国) + /// + MemorialDay } -} +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs index 59bfcb7..8e9538e 100644 --- a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs +++ b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs @@ -1,468 +1,448 @@ -using System; +using System; +using System.Collections.Generic; namespace EasyTool.DateTimeCategory { /// - /// 农历日期工具类 + /// 农历日历工具类 + /// 提供公历与农历之间的转换 /// public static class LunarCalendarUtil { + // 农历数据 1900-2100年 + // 每个数据表示一年,包含:月份天数信息、闰月信息 + private static readonly uint[] LunarInfo = { + 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, + 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, + 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, + 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, + 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, + 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, + 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, + 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, + 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, + 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, + 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, + 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, + 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, + 0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, + 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, + 0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, + 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, + 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0, + 0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160, + 0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, + 0x0d520 + }; - #region 基础数据 + // 天干 + private static readonly string[] TianGan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; - /// - /// 中文数字 - /// - private static readonly string[] ChineseNumbers = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; - /// - /// 天干 - /// - private static readonly string[] Gan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; - /// - /// 地支 - /// - private static readonly string[] Zhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; - /// - /// 生肖 - /// - private static readonly string[] Animal = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; - /// - /// 农历月份 - /// - private static readonly string[] MonthNames = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; - /// - /// 农历日期头 - /// - private static readonly string[] DayNames = { "初", "十", "廿", "三" }; - private static readonly string[] SolarTerm = { - "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", - "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", - "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" + // 地支 + private static readonly string[] DiZhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; + + // 生肖 + private static readonly string[] ShengXiao = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; + + // 农历月份 + private static readonly string[] LunarMonths = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊" }; + + // 农历日期 + private static readonly string[] LunarDays = { + "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", + "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", + "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" }; /// - /// 支持查询的最小农历年份 + /// 公历转农历 /// - private const int MinYear = 1900; - - private static readonly int[] LunarMonthDays = + /// 公历日期 + /// 农历信息 + public static LunarDate SolarToLunar(DateTime date) { - 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, // 1901-1910 - 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, // 1911-1920 - 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, // 1921-1930 - 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, // 1931-1940 - 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, // 1941-1950 - 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, // 1951-1960 - 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, // 1961-1970 - 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, // 1971-1980 - 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, // 1981-1990 - 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, // 1991-2000 - 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, // 2001-2010 - 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, // 2011-2020 - 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, // 2021-2030 - 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, // 2031-2040 - 0x06d20, 0x0ada0, 0x14b63, 0x09370, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, // 2041-2050 - 0x0aae0, 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, // 2051-2060 - 0x055d4, 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, // 2061-2070 - 0x052b0, 0x0b273, 0x0d950, 0x05b57, 0x056d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, // 2071-2080 - 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x0ada0, 0x095d0, // 2081-2090 - 0x04bd5, 0x04ad0, 0x0a4d0, 0x1d0b2, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, // 2091-2100 - 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63, 0x09370, 0x04970, // 2101-2110 - 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, // 2111-2120 - }; + if (date.Year < 1900 || date.Year > 2100) + throw new ArgumentOutOfRangeException(nameof(date), "日期范围必须在 1900-2100 年之间"); - #endregion + // 计算与 1900 年 1 月 31 日(农历 1900 年正月初一)的天数差 + var baseDate = new DateTime(1900, 1, 31); + var offset = (int)(date - baseDate).TotalDays; + if (offset < 0) + throw new ArgumentOutOfRangeException(nameof(date), "日期必须在 1900 年 1 月 31 日之后"); + // 计算农历年 + int year = 1900; + int daysOfYear; + while (year < 2100 && offset > 0) + { + daysOfYear = GetLunarYearDays(year); + if (offset < daysOfYear) + break; - /// - /// 获取指定公历日期对应的农历日期 - /// - /// 公历日期 - /// 农历日期 如:庚子鼠年正月初一 - public static string GetLunarDate(DateTime dateTime) - { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarYear(lunarDate[0])}年{GetLunarMonth(lunarDate[1])}月{GetLunarDay(lunarDate[2])}"; + offset -= daysOfYear; + year++; + } + + // 计算农历月和日 + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); // 闰月 + var isLeap = false; + int month = 1; + int daysOfMonth; + + while (month <= 12 && offset > 0) + { + daysOfMonth = GetLunarMonthDays(year, month, false); + if (offset < daysOfMonth) + break; + + offset -= daysOfMonth; + + // 检查是否有闰月 + if (leapMonth == month && !isLeap) + { + isLeap = true; + daysOfMonth = GetLunarMonthDays(year, month, true); + if (offset < daysOfMonth) + break; + + offset -= daysOfMonth; + isLeap = false; + } + + month++; + } + + return new LunarDate + { + Year = year, + Month = month, + Day = offset + 1, + IsLeapMonth = isLeap, + YearString = GetYearString(year), + MonthString = GetMonthString(month, isLeap), + DayString = LunarDays[offset], + GanZhiYear = GetGanZhiYear(year), + GanZhiMonth = GetGanZhiMonth(year, month), + GanZhiDay = GetGanZhiDay(date), + ShengXiao = GetShengXiao(year) + }; } /// - /// 获取农历年份 + /// 农历转公历 /// - /// 公历日期 - /// 农历年份(字符串)如:庚子鼠年 - public static string GetLunarYear(DateTime dateTime) + /// 农历年 + /// 农历月 + /// 农历日 + /// 是否闰月 + /// 公历日期 + public static DateTime LunarToSolar(int year, int month, int day, bool isLeapMonth = false) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarYear(lunarDate[0])}年"; + if (year < 1900 || year > 2100) + throw new ArgumentOutOfRangeException(nameof(year), "年份必须在 1900-2100 年之间"); + + var baseDate = new DateTime(1900, 1, 31); + var offset = 0; + + // 计算年份偏移 + for (int y = 1900; y < year; y++) + { + offset += GetLunarYearDays(y); + } + + // 计算月份偏移 + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); + + for (int m = 1; m < month; m++) + { + offset += GetLunarMonthDays(year, m, false); + + // 如果是闰月之前的月份,还要加上闰月的天数 + if (m == leapMonth && !isLeapMonth) + { + offset += GetLunarMonthDays(year, m, true); + } + } + + // 如果是闰月,加上正常月的天数 + if (isLeapMonth) + { + offset += GetLunarMonthDays(year, month, false); + } + + // 加上日期偏移 + offset += day - 1; + + return baseDate.AddDays(offset); } /// - /// 获取天干 + /// 获取农历年份的天数 /// - /// 公历日期 - /// 农历天干(字符串)如:庚 - public static string GetTianGan(DateTime dateTime) + public static int GetLunarYearDays(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Gan[(lunarDate[0] - 4) % 10]}"; + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); + var leapDays = leapMonth > 0 ? GetLunarMonthDays(year, leapMonth, true) : 0; + + var days = 0; + for (int i = 0x8000; i > 0x8; i >>= 1) + { + days += (yearInfo & i) != 0 ? 30 : 29; + } + + return days + leapDays; } /// - /// 获取地支 + /// 获取农历月份的天数 /// - /// 公历日期 - /// 农历地支(字符串)如 子 - public static string GetDiZhi(DateTime dateTime) + public static int GetLunarMonthDays(int year, int month, bool isLeap) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Zhi[(lunarDate[0] - 4) % 12]}"; + var yearInfo = LunarInfo[year - 1900]; + + if (isLeap) + { + return (yearInfo & 0x10000) != 0 ? 30 : 29; + } + + var bit = 0x8000 >> (month - 1); + return (yearInfo & bit) != 0 ? 30 : 29; } /// - /// 获取生肖 + /// 获取闰月(0 表示没有闰月) /// - /// 公历日期 - /// 农历生肖(字符串)如 鼠 - public static string GetChineseZodiac(DateTime dateTime) + public static int GetLeapMonth(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Animal[(lunarDate[0] - 4) % 12]}"; + var yearInfo = LunarInfo[year - 1900]; + return (int)(yearInfo & 0xf); } /// - /// 获取农历月份 + /// 获取干支年 /// - /// 公历日期 - /// 农历月份(字符串)如:正月 - public static string GetLunarMonth(DateTime dateTime) + public static string GetGanZhiYear(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarMonth(lunarDate[1])}月"; + var ganIndex = (year - 4) % 10; + var zhiIndex = (year - 4) % 12; + + if (ganIndex < 0) ganIndex += 10; + if (zhiIndex < 0) zhiIndex += 12; + + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取农历日期 + /// 获取干支月 /// - /// 公历日期 - /// 农历日期(字符串)如:廿三 - public static string GetLunarDay(DateTime dateTime) + public static string GetGanZhiMonth(int year, int month) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarDay(lunarDate[2])}"; + var ganIndex = (year * 12 + month + 13) % 10; + var zhiIndex = (month + 1) % 12; + + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取指定公历日期对应的农历日期 + /// 获取干支日 /// - /// 公历年份 - /// 公历月份 - /// 公历日期 - /// 农历日期 - private static int[] GetLunarDate(int year, int month, int day) + public static string GetGanZhiDay(DateTime date) { - int leapMonth = GetLunarLeapMonth(year); - int offset = (new DateTime(year, month, day) - new DateTime(1900, 1, 31)).Days; - - int iYear = 0, iMonth = 0, iDay = 0; - bool leap = false; - - for (iYear = 1900; iYear <= 2100 && offset > 0; iYear++) - { - offset -= GetLunarYearDays(iYear); - } - - if (offset < 0) - { - offset += GetLunarYearDays(--iYear); - } - - int yearDays = GetLunarYearDays(iYear); - int leapMonthIndex = GetLunarLeapMonth(iYear); - - for (iMonth = 1; iMonth <= 12 && offset > 0; iMonth++) - { - if (leapMonthIndex > 0 && iMonth == leapMonthIndex + 1 && !leap) - { - iMonth--; - leap = true; - yearDays = GetLunarLeapMonthDays(iYear); - } - else - { - yearDays = GetLunarMonthDays(iYear, iMonth); - } - - if (leap && iMonth == leapMonthIndex + 1) - { - leap = false; - } - - offset -= yearDays; - } - - if (offset == 0 && leapMonthIndex > 0 && iMonth == leapMonthIndex + 1) - { - if (leap) - { - leap = false; - } - else - { - leap = true; - iMonth--; - } - } + var baseDate = new DateTime(1900, 1, 31); + var offset = (int)(date - baseDate).TotalDays; - if (offset < 0) - { - offset += yearDays; - iMonth--; - } + var ganIndex = (offset + 10) % 10; + var zhiIndex = (offset + 12) % 12; - iDay = offset + 1; - return new[] { iYear, iMonth, iDay, leapMonth, leap ? 1 : 0 }; + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取农历年份 + /// 获取生肖 /// - /// 农历年份(数字) - /// 农历年份(字符串)如:庚子鼠年 - private static string GetLunarYear(int year) + public static string GetShengXiao(int year) { - return $"{Gan[(year - 4) % 10]}{Zhi[(year - 4) % 12]}{Animal[(year - 4) % 12]}年"; + var index = (year - 4) % 12; + if (index < 0) index += 12; + return ShengXiao[index]; } /// - /// 获取农历月份 + /// 获取生肖(GetShengXiao 的别名) /// - /// 农历月份(数字) - /// 农历月份(字符串)如:正月 - private static string GetLunarMonth(int month) + /// 日期 + /// 生肖 + public static string GetChineseZodiac(DateTime date) { - return MonthNames[month - 1] + "月"; + return GetShengXiao(date.Year); } /// - /// 获取指定年份的农历年份的天数 + /// 获取年份字符串 /// - /// 指定年份 - /// 农历年份的天数 - private static int GetLunarYearDays(int year) + private static string GetYearString(int year) { - int sum = 348; - for (int i = 0x8000; i > 0x8; i >>= 1) + var digits = new[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; + var result = ""; + while (year > 0) { - if ((LunarMonthDays[year - MinYear] & i) != 0) - { - sum += 1; - } + result = digits[year % 10] + result; + year /= 10; } - return sum + GetLunarLeapMonthDays(year); + return result; } + /// + /// 获取月份字符串 + /// + private static string GetMonthString(int month, bool isLeap) + { + return (isLeap ? "闰" : "") + LunarMonths[month - 1] + "月"; + } /// - /// 获取农历闰月月份 + /// 获取中国传统节日 /// - /// 农历年份 - /// 闰月月份(若当年没有闰月,返回0) - private static int GetLunarLeapMonth(int year) + public static List GetLunarFestivals(int year) { - int leapMonth = LunarMonthDays[year - MinYear] >> 16; - if (leapMonth > 0 && leapMonth < 13 && GetBit(LunarMonthDays[year - MinYear], 16 - leapMonth) == 0) - { - return leapMonth; - } - else + return new List { - return 0; - } + new LunarFestival { Name = "春节", Month = 1, Day = 1, Description = "农历新年" }, + new LunarFestival { Name = "元宵节", Month = 1, Day = 15, Description = "正月十五" }, + new LunarFestival { Name = "端午节", Month = 5, Day = 5, Description = "五月初五" }, + new LunarFestival { Name = "七夕节", Month = 7, Day = 7, Description = "七月初七" }, + new LunarFestival { Name = "中元节", Month = 7, Day = 15, Description = "七月十五" }, + new LunarFestival { Name = "中秋节", Month = 8, Day = 15, Description = "八月十五" }, + new LunarFestival { Name = "重阳节", Month = 9, Day = 9, Description = "九月初九" }, + new LunarFestival { Name = "腊八节", Month = 12, Day = 8, Description = "腊月初八" }, + new LunarFestival { Name = "除夕", Month = 12, Day = 30, Description = "腊月最后一天" } + }; } /// - /// 获取农历日期 + /// 判断是否是节日 /// - /// 农历日期(数字) - /// 农历日期(字符串)如:廿三 - private static string GetLunarDay(int day) + public static string? GetFestivalName(int lunarMonth, int lunarDay) { - int d1 = day / 10; - int d2 = day % 10; - if (d1 == 0) + return (lunarMonth, lunarDay) switch { - d1 = 3; - } - if (d2 == 0) - { - d2 = 10; - } - if (d2 == 20) - { - d2 = 0; - d1++; - } - return $"{DayNames[d1 - 1]}{ChineseNumbers[d2]}"; + (1, 1) => "春节", + (1, 15) => "元宵节", + (5, 5) => "端午节", + (7, 7) => "七夕节", + (7, 15) => "中元节", + (8, 15) => "中秋节", + (9, 9) => "重阳节", + (12, 8) => "腊八节", + (12, 30) => "除夕", + _ => null + }; } + } - + /// + /// 农历日期 + /// + public class LunarDate + { + /// + /// 年 + /// + public int Year { get; set; } /// - /// 获取指定年份和月份的农历月份的天数 + /// 月 /// - /// 指定年份 - /// 指定月份 - /// 农历月份的天数 29或30 - private static int GetLunarMonthDays(int year, int month) - { - return (LunarMonthDays[year - MinYear] & (0x10000 >> month)) == 0 ? 29 : 30; - } + public int Month { get; set; } /// - /// 获取指定年份的农历闰月的天数 + /// 日 /// - /// 指定年份 - /// 农历闰月的天数(29或30,若当年没有闰月,返回0) - private static int GetLunarLeapMonthDays(int year) - { - if (GetLunarLeapMonth(year) > 0) - { - return (LunarMonthDays[year - MinYear] & 0x10000) == 0 ? 29 : 30; - } - else - { - return 0; - } - } + public int Day { get; set; } /// - /// 获取指定整数的指定位的值 + /// 是否闰月 /// - /// 指定整数 - /// 指定位(从右往左数,最右边的位为第1位) - /// 指定位的值(0或1) - private static int GetBit(int num, int bit) - { - return (num >> (bit - 1)) & 1; - } + public bool IsLeapMonth { get; set; } - #region 公历农历互转 + /// + /// 年份字符串(中文) + /// + public string YearString { get; set; } = string.Empty; /// - /// 公历转农历 + /// 月份字符串(中文) /// - /// 公历日期 - /// 农历日期信息,包含年、月、日 - public static LunarDate? SolarToLunar(DateTime solarDate) - { - try - { - int[] lunarDate = GetLunarDate(solarDate.Year, solarDate.Month, solarDate.Day); - return new LunarDate(lunarDate[0], lunarDate[1], lunarDate[2], lunarDate[3] > 0 && lunarDate[4] == 1); - } - catch - { - return null; - } - } + public string MonthString { get; set; } = string.Empty; /// - /// 农历转公历 + /// 日期字符串(中文) /// - /// 农历年份 - /// 农历月份(1-12) - /// 农历日期 - /// 是否为闰月 - /// 公历日期 - public static DateTime? LunarToSolar(int year, int month, int day, bool isLeapMonth = false) - { - if (year < 1900 || year > 2100) - return null; + public string DayString { get; set; } = string.Empty; - try - { - // 计算从1900年1月31日到目标农历日期的天数 - int offset = 0; + /// + /// 干支年 + /// + public string GanZhiYear { get; set; } = string.Empty; - // 累加年份天数 - for (int y = 1900; y < year; y++) - { - offset += GetLunarYearDays(y); - } + /// + /// 干支月 + /// + public string GanZhiMonth { get; set; } = string.Empty; - int leapMonth = GetLunarLeapMonth(year); - bool leapProcessed = false; + /// + /// 干支日 + /// + public string GanZhiDay { get; set; } = string.Empty; - // 累加月份天数 - for (int m = 1; m < month; m++) - { - // 处理闰月 - if (leapMonth > 0 && m == leapMonth && !leapProcessed) - { - offset += GetLunarLeapMonthDays(year); - m--; // 重新处理当前月 - leapProcessed = true; - continue; - } - offset += GetLunarMonthDays(year, m); - } + /// + /// 生肖 + /// + public string ShengXiao { get; set; } = string.Empty; - // 如果是闰月,需要加上闰月之前月份的天数 - if (isLeapMonth && leapMonth == month) - { - offset += GetLunarMonthDays(year, month); - } + /// + /// 完整日期字符串 + /// + public string FullString => $"{YearString}年{MonthString}{DayString}"; - // 加上日期天数 - offset += day - 1; + /// + /// 干支日期字符串 + /// + public string GanZhiString => $"{GanZhiYear}年{GanZhiMonth}月{GanZhiDay}日"; - // 计算公历日期 - return new DateTime(1900, 1, 31).AddDays(offset); - } - catch - { - return null; - } + public override string ToString() + { + return $"{FullString}({GanZhiString}){ShengXiao}年"; } - - #endregion } /// - /// 农历日期信息 + /// 农历节日 /// - public class LunarDate + public class LunarFestival { /// - /// 农历年 + /// 节日名称 /// - public int Year { get; } + public string Name { get; set; } = string.Empty; /// /// 农历月 /// - public int Month { get; } + public int Month { get; set; } /// /// 农历日 /// - public int Day { get; } + public int Day { get; set; } /// - /// 是否为闰月 + /// 描述 /// - public bool IsLeapMonth { get; } - - /// - /// 创建农历日期 - /// - public LunarDate(int year, int month, int day, bool isLeapMonth = false) - { - Year = year; - Month = month; - Day = day; - IsLeapMonth = isLeapMonth; - } + public string Description { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs b/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs new file mode 100644 index 0000000..c19bf0f --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs @@ -0,0 +1,348 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 二十四节气工具类 + /// 计算二十四节气日期 + /// + public static class SolarTermUtil + { + // 二十四节气名称 + private static readonly string[] SolarTerms = { + "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", + "清明", "谷雨", "立夏", "小满", "芒种", "夏至", + "小暑", "大暑", "立秋", "处暑", "白露", "秋分", + "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" + }; + + // 节气对应的公历日期(基准数据,1900年) + // 实际计算时会根据年份进行调整 + private static readonly (int Month, int Day, int Hour)[] SolarTermBase = { + (1, 6, 0), // 小寒 + (1, 20, 0), // 大寒 + (2, 4, 0), // 立春 + (2, 19, 0), // 雨水 + (3, 6, 0), // 惊蛰 + (3, 21, 0), // 春分 + (4, 5, 0), // 清明 + (4, 20, 0), // 谷雨 + (5, 6, 0), // 立夏 + (5, 21, 0), // 小满 + (6, 6, 0), // 芒种 + (6, 21, 0), // 夏至 + (7, 7, 0), // 小暑 + (7, 23, 0), // 大暑 + (8, 8, 0), // 立秋 + (8, 23, 0), // 处暑 + (9, 8, 0), // 白露 + (9, 23, 0), // 秋分 + (10, 8, 0), // 寒露 + (10, 24, 0), // 霜降 + (11, 8, 0), // 立冬 + (11, 22, 0), // 小雪 + (12, 7, 0), // 大雪 + (12, 22, 0) // 冬至 + }; + + // 节气计算系数(简化算法) + // 基于1900年小寒为1月6日2时5分的基准 + private static readonly double[] TermCoefficients = { + 0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, + 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, + 18.5, 19.5, 20.5, 21.5, 22.5, 23.5 + }; + + /// + /// 获取指定年份的所有节气 + /// + /// 年份 + /// 节气列表 + public static List GetSolarTerms(int year) + { + var result = new List(); + + for (int i = 0; i < 24; i++) + { + var date = CalculateSolarTerm(year, i); + result.Add(new SolarTermInfo + { + Index = i, + Name = SolarTerms[i], + Date = date, + Month = date.Month, + Day = date.Day, + Type = i % 2 == 0 ? SolarTermType.Jie : SolarTermType.Qi + }); + } + + return result; + } + + /// + /// 获取指定日期所在的节气 + /// + /// 日期 + /// 节气信息,如果不是节气日则返回 null + public static SolarTermInfo? GetSolarTerm(DateTime date) + { + var terms = GetSolarTerms(date.Year); + + // 检查是否是前一年的最后一个节气 + if (date.Month == 1 && date.Day < 6) + { + var lastYearTerms = GetSolarTerms(date.Year - 1); + var lastTerm = lastYearTerms[23]; // 冬至 + if (lastTerm.Date.Date == date.Date) + return lastTerm; + } + + foreach (var term in terms) + { + if (term.Date.Date == date.Date) + return term; + } + + return null; + } + + /// + /// 获取下一个节气 + /// + /// 基准日期 + /// 下一个节气 + public static SolarTermInfo GetNextSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + foreach (var term in terms) + { + if (term.Date > date) + return term; + } + + // 如果当前年份没有了,返回下一年的第一个节气 + return GetSolarTerms(year + 1)[0]; + } + + /// + /// 获取上一个节气 + /// + /// 基准日期 + /// 上一个节气 + public static SolarTermInfo GetPreviousSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + for (int i = terms.Count - 1; i >= 0; i--) + { + if (terms[i].Date < date) + return terms[i]; + } + + // 如果当前年份没有了,返回上一年的最后一个节气 + return GetSolarTerms(year - 1)[23]; + } + + /// + /// 获取当前节气(今天或之前最近的节气) + /// + /// 日期 + /// 当前节气 + public static SolarTermInfo GetCurrentSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + SolarTermInfo? current = null; + foreach (var term in terms) + { + if (term.Date <= date) + current = term; + else + break; + } + + if (current != null) + return current; + + // 返回上一年的最后一个节气 + return GetSolarTerms(year - 1)[23]; + } + + /// + /// 计算节气日期 + /// + private static DateTime CalculateSolarTerm(int year, int termIndex) + { + // 使用简化的节气计算算法 + // 基于黄经计算(每个节气相差15度) + + var baseDate = new DateTime(year, 1, 6, 2, 5, 0); // 1900年小寒基准 + var baseYear = 1900; + + // 计算从1900年到目标年份的累积偏移 + var totalDays = 0.0; + + // 简化计算:使用回归年长度 365.2422 天 + var tropicalYear = 365.2422; + var yearOffset = (year - baseYear) * tropicalYear; + + // 每个节气平均间隔约 15.2184 天 + var termOffset = termIndex * 15.2184; + + // 计算总偏移 + totalDays = yearOffset + termOffset; + + // 从基准日期计算 + var result = baseDate.AddDays(totalDays - (year - baseYear) * tropicalYear); + + // 调整到正确的年份 + result = new DateTime(year, result.Month, result.Day, result.Hour, result.Minute, 0); + + // 使用更精确的表格数据进行微调 + var (month, day, hour) = SolarTermBase[termIndex]; + + // 年份修正(每4年大约有1天的偏差) + var correction = (year - 1900) * 0.2422; + var correctedDay = day + (int)Math.Round(correction); + + // 处理月份边界 + if (correctedDay < 1) + { + month--; + if (month == 0) month = 12; + correctedDay += DateTime.DaysInMonth(year, month); + } + else if (correctedDay > DateTime.DaysInMonth(year, month)) + { + correctedDay -= DateTime.DaysInMonth(year, month); + month++; + if (month > 12) month = 1; + } + + return new DateTime(year, month, correctedDay, hour, 0, 0); + } + + /// + /// 获取节气名称 + /// + /// 节气索引(0-23) + /// 节气名称 + public static string GetSolarTermName(int index) + { + if (index < 0 || index >= 24) + throw new ArgumentOutOfRangeException(nameof(index), "节气索引必须在 0-23 之间"); + + return SolarTerms[index]; + } + + /// + /// 获取季节 + /// + /// 节气索引 + /// 季节 + public static Season GetSeason(int termIndex) + { + return termIndex switch + { + >= 0 and < 6 => Season.Spring, + >= 6 and < 12 => Season.Summer, + >= 12 and < 18 => Season.Autumn, + _ => Season.Winter + }; + } + + /// + /// 判断是否是"节"(奇数索引) + /// + public static bool IsJie(int termIndex) + { + return termIndex % 2 == 0; + } + + /// + /// 判断是否是"气"(偶数索引) + /// + public static bool IsQi(int termIndex) + { + return termIndex % 2 == 1; + } + } + + /// + /// 节气信息 + /// + public class SolarTermInfo + { + /// + /// 节气索引(0-23) + /// + public int Index { get; set; } + + /// + /// 节气名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 公历日期 + /// + public DateTime Date { get; set; } + + /// + /// 月份 + /// + public int Month { get; set; } + + /// + /// 日期 + /// + public int Day { get; set; } + + /// + /// 节气类型 + /// + public SolarTermType Type { get; set; } + + /// + /// 所属季节 + /// + public Season Season => SolarTermUtil.GetSeason(Index); + + public override string ToString() + { + return $"{Name} ({Date:yyyy-MM-dd})"; + } + } + + /// + /// 节气类型 + /// + public enum SolarTermType + { + /// + /// 节(每月的第一个节气) + /// + Jie, + + /// + /// 气(每月的第二个节气) + /// + Qi + } + + /// + /// 季节 + /// + public enum Season + { + Spring, + Summer, + Autumn, + Winter + } +} diff --git a/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs b/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs new file mode 100644 index 0000000..986da31 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 计时器工具类 + /// 提供便捷的计时功能 + /// + public static class StopwatchUtil + { + /// + /// 创建并启动计时器 + /// + /// 计时器 + public static Stopwatch StartNew() + { + return Stopwatch.StartNew(); + } + + /// + /// 测量操作执行时间 + /// + /// 操作 + /// 执行时间 + public static TimeSpan Measure(Action action) + { + var stopwatch = StartNew(); + action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 测量操作执行时间(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 执行时间和结果 + public static (TimeSpan Elapsed, T Result) Measure(Func func) + { + var stopwatch = StartNew(); + var result = func(); + stopwatch.Stop(); + return (stopwatch.Elapsed, result); + } + + /// + /// 异步测量操作执行时间 + /// + /// 操作 + /// 执行时间 + public static async Task MeasureAsync(Func action) + { + var stopwatch = StartNew(); + await action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 异步测量操作执行时间(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 执行时间和结果 + public static async Task<(TimeSpan Elapsed, T Result)> MeasureAsync(Func> func) + { + var stopwatch = StartNew(); + var result = await func(); + stopwatch.Stop(); + return (stopwatch.Elapsed, result); + } + + /// + /// 使用计时器执行操作 + /// + /// 操作 + /// 计时回调 + public static void WithTimer(Action action, Action callback) + { + var elapsed = Measure(action); + callback(elapsed); + } + + /// + /// 使用计时器执行操作 + /// + /// 返回值类型 + /// 操作 + /// 计时回调 + /// 操作结果 + public static T WithTimer(Func func, Action callback) + { + var (elapsed, result) = Measure(func); + callback(elapsed); + return result; + } + + /// + /// 异步使用计时器执行操作 + /// + /// 操作 + /// 计时回调 + public static async Task WithTimerAsync(Func action, Action callback) + { + var elapsed = await MeasureAsync(action); + callback(elapsed); + } + + /// + /// 异步使用计时器执行操作 + /// + /// 返回值类型 + /// 操作 + /// 计时回调 + /// 操作结果 + public static async Task WithTimerAsync(Func> func, Action callback) + { + var (elapsed, result) = await MeasureAsync(func); + callback(elapsed); + return result; + } + + /// + /// 等待指定时间 + /// + /// 等待时间 + public static void Wait(TimeSpan duration) + { + Thread.Sleep(duration); + } + + /// + /// 异步等待指定时间 + /// + /// 等待时间 + /// 取消令牌 + public static Task WaitAsync(TimeSpan duration, CancellationToken cancellationToken = default) + { + return Task.Delay(duration, cancellationToken); + } + + /// + /// 执行带超时的操作 + /// + /// 操作 + /// 超时时间 + /// 是否在超时前完成 + public static bool TryExecute(Action action, TimeSpan timeout) + { + var task = Task.Run(action); + return task.Wait(timeout); + } + + /// + /// 异步执行带超时的操作 + /// + /// 操作 + /// 超时时间 + /// 取消令牌 + /// 是否在超时前完成 + public static async Task TryExecuteAsync(Func action, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + await action(); + return true; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + return false; + } + } + + /// + /// 执行带超时的操作(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 超时时间 + /// 结果 + /// 是否在超时前完成 + public static bool TryExecute(Func func, TimeSpan timeout, out T? result) + { + result = default; + var task = Task.Run(func); + + if (task.Wait(timeout)) + { + result = task.Result; + return true; + } + + return false; + } + + /// + /// 异步执行带超时的操作(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 超时时间 + /// 取消令牌 + /// 结果或默认值 + public static async Task<(bool Success, T? Result)> TryExecuteAsync(Func> func, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + var result = await func(); + return (true, result); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + return (false, default); + } + } + + /// + /// 格式化时间输出 + /// + /// 时间 + /// 格式化字符串 + public static string FormatTime(TimeSpan time) + { + if (time.TotalSeconds >= 1) + return $"{time.TotalSeconds:F2}s"; + if (time.TotalMilliseconds >= 1) + return $"{time.TotalMilliseconds:F2}ms"; +#if NET7_0_OR_GREATER + if (time.TotalMicroseconds >= 1) + return $"{time.TotalMicroseconds:F2}μs"; + return $"{time.TotalNanoseconds:F2}ns"; +#else + // For older frameworks, use ticks for sub-millisecond precision + var ticks = time.Ticks; + if (ticks >= 10) // >= 1 microsecond (10 ticks = 1 μs) + return $"{ticks / 10.0:F2}μs"; + return $"{ticks * 100.0:F2}ns"; +#endif + } + + /// + /// 格式化时间为详细字符串 + /// + /// 时间 + /// 格式化字符串 + public static string FormatTimeDetailed(TimeSpan time) + { + var parts = new List(); + + if (time.Days > 0) + parts.Add($"{time.Days}天"); + if (time.Hours > 0) + parts.Add($"{time.Hours}小时"); + if (time.Minutes > 0) + parts.Add($"{time.Minutes}分钟"); + if (time.Seconds > 0) + parts.Add($"{time.Seconds}秒"); + if (time.Milliseconds > 0) + parts.Add($"{time.Milliseconds}毫秒"); + + return parts.Count > 0 ? string.Join(" ", parts) : "0毫秒"; + } + } +} diff --git a/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs b/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs new file mode 100644 index 0000000..d3b005a --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs @@ -0,0 +1,453 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 时区转换工具类 + /// 提供时区转换和时区信息查询功能 + /// + public static class TimeZoneUtil + { + #region 常用时区 + + /// + /// UTC时区 + /// + public static TimeZoneInfo UtcTimeZone => TimeZoneInfo.Utc; + + /// + /// 本地时区 + /// + public static TimeZoneInfo LocalTimeZone => TimeZoneInfo.Local; + + /// + /// 中国标准时间时区(UTC+8) + /// + public static TimeZoneInfo ChinaStandardTime => FindTimeZoneById("China Standard Time", "Asia/Shanghai", 8); + + /// + /// 美国东部时区 + /// + public static TimeZoneInfo USEasternTime => FindTimeZoneById("Eastern Standard Time", "America/New_York", -5); + + /// + /// 美国太平洋时区 + /// + public static TimeZoneInfo USPacificTime => FindTimeZoneById("Pacific Standard Time", "America/Los_Angeles", -8); + + /// + /// 欧洲伦敦时区 + /// + public static TimeZoneInfo LondonTime => FindTimeZoneById("GMT Standard Time", "Europe/London", 0); + + /// + /// 日本标准时间时区 + /// + public static TimeZoneInfo JapanStandardTime => FindTimeZoneById("Tokyo Standard Time", "Asia/Tokyo", 9); + + /// + /// 韩国标准时间时区 + /// + public static TimeZoneInfo KoreaStandardTime => FindTimeZoneById("Korea Standard Time", "Asia/Seoul", 9); + + /// + /// 新加坡时区 + /// + public static TimeZoneInfo SingaporeTime => FindTimeZoneById("Singapore Standard Time", "Asia/Singapore", 8); + + /// + /// 澳大利亚悉尼时区 + /// + public static TimeZoneInfo SydneyTime => FindTimeZoneById("AUS Eastern Standard Time", "Australia/Sydney", 10); + + /// + /// 印度标准时间时区 + /// + public static TimeZoneInfo IndiaStandardTime => FindTimeZoneById("India Standard Time", "Asia/Kolkata", 5.5); + + /// + /// 德国柏林时区 + /// + public static TimeZoneInfo BerlinTime => FindTimeZoneById("W. Europe Standard Time", "Europe/Berlin", 1); + + private static TimeZoneInfo FindTimeZoneById(string windowsId, string ianaId, double offsetHours) + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(windowsId); + } + catch + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(ianaId); + } + catch + { + // 创建自定义时区 + return CreateCustomTimeZone(windowsId, offsetHours); + } + } + } + + private static TimeZoneInfo CreateCustomTimeZone(string id, double offsetHours) + { + var offset = TimeSpan.FromHours(offsetHours); + return TimeZoneInfo.CreateCustomTimeZone(id, offset, id, id); + } + + #endregion + + #region 时区转换 + + /// + /// 将时间从一个时区转换到另一个时区 + /// + /// 要转换的时间 + /// 源时区 + /// 目标时区 + /// 转换后的时间 + public static DateTime ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone) + { + return TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, destinationTimeZone); + } + + /// + /// 将时间转换为UTC时间 + /// + /// 本地时间 + /// UTC时间 + public static DateTime ToUtc(DateTime dateTime) + { + return dateTime.Kind switch + { + DateTimeKind.Utc => dateTime, + DateTimeKind.Local => dateTime.ToUniversalTime(), + _ => DateTime.SpecifyKind(dateTime, DateTimeKind.Local).ToUniversalTime() + }; + } + + /// + /// 将UTC时间转换为本地时间 + /// + /// UTC时间 + /// 本地时间 + public static DateTime FromUtc(DateTime utcDateTime) + { + return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.Local); + } + + /// + /// 将时间转换为中国标准时间 + /// + /// 源时间 + /// 源时区(默认本地时区) + /// 中国标准时间 + public static DateTime ToChinaTime(DateTime dateTime, TimeZoneInfo? sourceTimeZone = null) + { + sourceTimeZone ??= TimeZoneInfo.Local; + return ConvertTime(dateTime, sourceTimeZone, ChinaStandardTime); + } + + /// + /// 将时间转换为美国东部时间 + /// + /// 源时间 + /// 源时区(默认本地时区) + /// 美国东部时间 + public static DateTime ToUSEasternTime(DateTime dateTime, TimeZoneInfo? sourceTimeZone = null) + { + sourceTimeZone ??= TimeZoneInfo.Local; + return ConvertTime(dateTime, sourceTimeZone, USEasternTime); + } + + /// + /// 将时间转换为指定偏移量时区的时间 + /// + /// 源时间 + /// 源时区偏移量(小时) + /// 目标时区偏移量(小时) + /// 目标时区时间 + public static DateTime ConvertByOffset(DateTime dateTime, double sourceOffset, double targetOffset) + { + // 先转为UTC + var utc = dateTime.AddHours(-sourceOffset); + // 再转为目标时区 + return utc.AddHours(targetOffset); + } + + /// + /// 获取指定时区当前时间 + /// + /// 时区 + /// 当前时间 + public static DateTime GetNow(TimeZoneInfo timeZone) + { + return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone); + } + + /// + /// 获取中国当前时间 + /// + /// 中国当前时间 + public static DateTime GetChinaNow() + { + return GetNow(ChinaStandardTime); + } + + #endregion + + #region 时区信息 + + /// + /// 获取所有时区 + /// + /// 时区列表 + public static IReadOnlyCollection GetAllTimeZones() + { + return TimeZoneInfo.GetSystemTimeZones(); + } + + /// + /// 根据ID获取时区 + /// + /// 时区ID + /// 时区信息 + public static TimeZoneInfo? GetTimeZoneById(string id) + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(id); + } + catch + { + return null; + } + } + + /// + /// 根据偏移量查找时区 + /// + /// 偏移量(小时) + /// 匹配的时区列表 + public static List GetTimeZonesByOffset(double offsetHours) + { + var offset = TimeSpan.FromHours(offsetHours); + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.BaseUtcOffset == offset) + .ToList(); + } + + /// + /// 获取时区偏移量 + /// + /// 时区 + /// 偏移量(小时) + public static double GetOffsetHours(TimeZoneInfo timeZone) + { + return timeZone.BaseUtcOffset.TotalHours; + } + + /// + /// 获取时区偏移量字符串 + /// + /// 时区 + /// 偏移量字符串(如+08:00) + public static string GetOffsetString(TimeZoneInfo timeZone) + { + var offset = timeZone.BaseUtcOffset; + return $"{(offset >= TimeSpan.Zero ? "+" : "")}{offset:hh\\:mm}"; + } + + /// + /// 判断时区是否支持夏令时 + /// + /// 时区 + /// 是否支持夏令时 + public static bool SupportsDaylightSavingTime(TimeZoneInfo timeZone) + { + return timeZone.SupportsDaylightSavingTime; + } + + /// + /// 判断指定时间是否处于夏令时 + /// + /// 时间 + /// 时区 + /// 是否处于夏令时 + public static bool IsDaylightSavingTime(DateTime dateTime, TimeZoneInfo timeZone) + { + return timeZone.IsDaylightSavingTime(dateTime); + } + + /// + /// 获取时区当前偏移量(考虑夏令时) + /// + /// 时区 + /// 当前偏移量 + public static TimeSpan GetCurrentOffset(TimeZoneInfo timeZone) + { + var now = DateTime.UtcNow; + var offset = timeZone.GetUtcOffset(now); + return offset; + } + + #endregion + + #region DateTimeOffset + + /// + /// 创建DateTimeOffset + /// + /// 本地时间 + /// 时区 + /// DateTimeOffset + public static DateTimeOffset CreateDateTimeOffset(DateTime dateTime, TimeZoneInfo timeZone) + { + var utcTime = ConvertTime(dateTime, timeZone, TimeZoneInfo.Utc); + return new DateTimeOffset(utcTime, TimeSpan.Zero).ToOffset(timeZone.GetUtcOffset(dateTime)); + } + + /// + /// 将DateTimeOffset转换到指定时区 + /// + /// DateTimeOffset + /// 目标时区 + /// 转换后的DateTimeOffset + public static DateTimeOffset ConvertTime(DateTimeOffset dateTimeOffset, TimeZoneInfo timeZone) + { + return TimeZoneInfo.ConvertTime(dateTimeOffset, timeZone); + } + + #endregion + + #region 时区差异计算 + + /// + /// 计算两个时区之间的时间差 + /// + /// 时区1 + /// 时区2 + /// 时间差 + public static TimeSpan GetTimeDifference(TimeZoneInfo timeZone1, TimeZoneInfo timeZone2) + { + return timeZone1.BaseUtcOffset - timeZone2.BaseUtcOffset; + } + + /// + /// 计算两个时区之间的小时差 + /// + /// 时区1 + /// 时区2 + /// 小时差 + public static double GetHoursDifference(TimeZoneInfo timeZone1, TimeZoneInfo timeZone2) + { + return GetTimeDifference(timeZone1, timeZone2).TotalHours; + } + + #endregion + + #region 时区查找 + + /// + /// 根据名称模糊查找时区 + /// + /// 时区名称 + /// 匹配的时区列表 + public static List FindTimeZonesByName(string name) + { + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.DisplayName.Contains(name, StringComparison.OrdinalIgnoreCase) || + tz.Id.Contains(name, StringComparison.OrdinalIgnoreCase) || + tz.StandardName.Contains(name, StringComparison.OrdinalIgnoreCase)) + .ToList(); + } + + /// + /// 获取UTC+N时区列表 + /// + /// UTC偏移量(如8表示UTC+8) + /// 时区列表 + public static List GetUtcPlusTimeZones(int offset) + { + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.BaseUtcOffset == TimeSpan.FromHours(offset)) + .ToList(); + } + + #endregion + + #region 格式化 + + /// + /// 格式化时区信息 + /// + /// 时区 + /// 格式化字符串 + public static string FormatTimeZone(TimeZoneInfo timeZone) + { + return $"{timeZone.Id} ({GetOffsetString(timeZone)}) {timeZone.DisplayName}"; + } + + /// + /// 格式化时间为带时区的字符串 + /// + /// 时间 + /// 时区 + /// 时间格式 + /// 格式化字符串 + public static string FormatDateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, string format = "yyyy-MM-dd HH:mm:ss") + { + var offset = GetOffsetString(timeZone); + return $"{dateTime.ToString(format)} (UTC{offset})"; + } + + #endregion + + #region 常用城市时区 + + /// + /// 获取常用城市时区映射 + /// + public static Dictionary GetCommonCityTimeZones() + { + return new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "北京", ChinaStandardTime }, + { "上海", ChinaStandardTime }, + { "香港", FindTimeZoneById("China Standard Time", "Asia/Hong_Kong", 8) }, + { "台北", FindTimeZoneById("Taipei Standard Time", "Asia/Taipei", 8) }, + { "东京", JapanStandardTime }, + { "首尔", KoreaStandardTime }, + { "新加坡", SingaporeTime }, + { "悉尼", SydneyTime }, + { "伦敦", LondonTime }, + { "巴黎", FindTimeZoneById("Romance Standard Time", "Europe/Paris", 1) }, + { "柏林", BerlinTime }, + { "纽约", USEasternTime }, + { "洛杉矶", USPacificTime }, + { "芝加哥", FindTimeZoneById("Central Standard Time", "America/Chicago", -6) }, + { "多伦多", FindTimeZoneById("Eastern Standard Time", "America/Toronto", -5) }, + { "温哥华", FindTimeZoneById("Pacific Standard Time", "America/Vancouver", -8) }, + { "迪拜", FindTimeZoneById("Arabian Standard Time", "Asia/Dubai", 4) }, + { "孟买", IndiaStandardTime }, + { "莫斯科", FindTimeZoneById("Russian Standard Time", "Europe/Moscow", 3) } + }; + } + + /// + /// 根据城市名获取时区 + /// + /// 城市名 + /// 时区信息 + public static TimeZoneInfo? GetTimeZoneByCity(string cityName) + { + var cityTimeZones = GetCommonCityTimeZones(); + return cityTimeZones.TryGetValue(cityName, out var timeZone) ? timeZone : null; + } + + #endregion + } +} diff --git a/EasyTool.Core/DateTimeCategory/TimerUtil.cs b/EasyTool.Core/DateTimeCategory/TimerUtil.cs index 10b945a..2ea5253 100644 --- a/EasyTool.Core/DateTimeCategory/TimerUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimerUtil.cs @@ -1,127 +1,443 @@ using System; -using System.Diagnostics; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace EasyTool.DateTimeCategory { /// - /// 计时器工具类,提供各种计时和时间间隔计算的方法。 + /// 定时器工具类 + /// 提供增强的定时器功能 /// public static class TimerUtil { /// - /// 记录程序启动时间。 + /// 创建一次性定时器 /// - private static readonly DateTime _startTime = DateTime.Now; + /// 延迟时间 + /// 回调 + /// 定时器 + public static Timer Once(TimeSpan delay, Action callback) + { + return new Timer(_ => callback(), null, delay, Timeout.InfiniteTimeSpan); + } + + /// + /// 创建一次性定时器(异步) + /// + /// 延迟时间 + /// 回调 + /// 定时器 + public static Timer OnceAsync(TimeSpan delay, Func callback) + { + Timer? timer = null; + timer = new Timer(async _ => + { + timer?.Dispose(); + await callback(); + }, null, delay, Timeout.InfiniteTimeSpan); + return timer; + } + + /// + /// 创建周期性定时器 + /// + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer Interval(TimeSpan interval, Action callback) + { + return new Timer(_ => callback(), null, interval, interval); + } + + /// + /// 创建周期性定时器(异步) + /// + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer IntervalAsync(TimeSpan interval, Func callback) + { + Timer? timer = null; + timer = new Timer(async _ => + { + await callback(); + }, null, interval, interval); + return timer; + } + + /// + /// 创建带延迟的周期性定时器 + /// + /// 首次执行延迟 + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer DelayedInterval(TimeSpan dueTime, TimeSpan period, Action callback) + { + return new Timer(_ => callback(), null, dueTime, period); + } + + /// + /// 等待指定时间后执行 + /// + /// 延迟时间 + /// 回调 + /// 可取消令牌 + public static CancellationTokenSource RunAfter(TimeSpan delay, Action callback) + { + var cts = new CancellationTokenSource(); + + Task.Run(async () => + { + try + { + await Task.Delay(delay, cts.Token); + if (!cts.Token.IsCancellationRequested) + { + callback(); + } + } + catch (OperationCanceledException) + { + // 取消时不执行回调 + } + }, cts.Token); + + return cts; + } + + /// + /// 异步等待指定时间后执行 + /// + /// 延迟时间 + /// 回调 + /// 可取消令牌 + public static CancellationTokenSource RunAfterAsync(TimeSpan delay, Func callback) + { + var cts = new CancellationTokenSource(); + + Task.Run(async () => + { + try + { + await Task.Delay(delay, cts.Token); + if (!cts.Token.IsCancellationRequested) + { + await callback(); + } + } + catch (OperationCanceledException) + { + // 取消时不执行回调 + } + }, cts.Token); + + return cts; + } + + /// + /// 重复执行直到条件满足 + /// + /// 间隔时间 + /// 执行操作,返回是否继续 + /// 最大执行次数(0表示无限) + /// 可取消令牌 + public static CancellationTokenSource RepeatUntil(TimeSpan interval, Func action, int maxCount = 0) + { + var cts = new CancellationTokenSource(); + var count = 0; + + Task.Run(async () => + { + while (!cts.Token.IsCancellationRequested) + { + if (maxCount > 0 && count >= maxCount) + break; + + if (!action()) + break; + + count++; + + try + { + await Task.Delay(interval, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + } + }, cts.Token); + + return cts; + } + + /// + /// 异步重复执行直到条件满足 + /// + /// 间隔时间 + /// 执行操作,返回是否继续 + /// 最大执行次数(0表示无限) + /// 可取消令牌 + public static CancellationTokenSource RepeatUntilAsync(TimeSpan interval, Func> action, int maxCount = 0) + { + var cts = new CancellationTokenSource(); + var count = 0; + + Task.Run(async () => + { + while (!cts.Token.IsCancellationRequested) + { + if (maxCount > 0 && count >= maxCount) + break; + + if (!await action()) + break; + + count++; + + try + { + await Task.Delay(interval, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + } + }, cts.Token); + + return cts; + } + } + + /// + /// 定时任务调度器 + /// + public class ScheduledTask + { + private Timer? _timer; + private readonly Action _callback; + private readonly TimeSpan _interval; + private readonly DateTime _startTime; + private readonly int _maxRuns; + private int _runCount; + + /// + /// 任务名称 + /// + public string Name { get; } /// - /// 获取当前时间戳,即 Unix 时间戳,精确到毫秒。 + /// 是否正在运行 /// - /// 当前时间戳。 - public static long GetCurrentTimestamp() + public bool IsRunning { get; private set; } + + /// + /// 已运行次数 + /// + public int RunCount => _runCount; + + /// + /// 创建定时任务 + /// + /// 任务名称 + /// 回调 + /// 间隔时间 + /// 开始时间 + /// 最大运行次数(0表示无限) + public ScheduledTask(string name, Action callback, TimeSpan interval, DateTime? startTime = null, int maxRuns = 0) { - return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + Name = name; + _callback = callback; + _interval = interval; + _startTime = startTime ?? DateTime.MinValue; + _maxRuns = maxRuns; + _runCount = 0; } /// - /// 获取程序启动时间。 + /// 启动任务 /// - /// 程序启动时间。 - public static DateTime GetStartTime() + public void Start() { - return _startTime; + if (IsRunning) + return; + + IsRunning = true; + + var dueTime = _startTime > DateTime.MinValue + ? _startTime - DateTime.Now + : TimeSpan.Zero; + + if (dueTime < TimeSpan.Zero) + dueTime = TimeSpan.Zero; + + _timer = new Timer(Execute, null, dueTime, _interval); + } + + private void Execute(object? state) + { + if (_maxRuns > 0 && _runCount >= _maxRuns) + { + Stop(); + return; + } + + try + { + _callback(); + } + catch + { + // 忽略异常,继续执行 + } + + Interlocked.Increment(ref _runCount); } /// - /// 获取当前时间距离程序启动时间的时间间隔。 + /// 停止任务 /// - /// 当前时间距离程序启动时间的时间间隔。 - public static TimeSpan GetElapsedTime() + public void Stop() { - return DateTime.Now - _startTime; + if (!IsRunning) + return; + + IsRunning = false; + _timer?.Dispose(); + _timer = null; } /// - /// 计算指定操作的执行时间。 + /// 重置运行计数 /// - /// 要执行的操作。 - /// 操作执行的时间。 - public static TimeSpan Measure(Action action) + public void Reset() { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - action.Invoke(); - stopwatch.Stop(); - return stopwatch.Elapsed; + _runCount = 0; } + } + + /// + /// 定时任务管理器 + /// + public class ScheduleManager + { + private readonly Dictionary _tasks = new(); /// - /// 计算指定操作的执行时间,并输出执行结果。 + /// 添加定时任务 /// - /// 要执行的操作。 - /// 执行结果的描述。 - public static void MeasureAndPrint(Action action, string description) + /// 任务名称 + /// 回调 + /// 间隔时间 + /// 开始时间 + /// 最大运行次数 + /// 定时任务 + public ScheduledTask AddTask(string name, Action callback, TimeSpan interval, DateTime? startTime = null, int maxRuns = 0) { - TimeSpan elapsedTime = Measure(action); - Console.WriteLine($"{description}: {elapsedTime.TotalMilliseconds}ms"); + var task = new ScheduledTask(name, callback, interval, startTime, maxRuns); + _tasks[name] = task; + return task; } /// - /// 计算指定操作的执行时间,并输出执行结果到指定文件。 + /// 获取任务 /// - /// 要执行的操作。 - /// 输出结果的文件名。 - public static void MeasureAndSave(Action action, string fileName) + /// 任务名称 + /// 定时任务 + public ScheduledTask? GetTask(string name) { - TimeSpan elapsedTime = Measure(action); - System.IO.File.WriteAllText(fileName, elapsedTime.TotalMilliseconds.ToString()); + return _tasks.TryGetValue(name, out var task) ? task : null; } /// - /// 计算指定操作的执行时间,并将执行结果添加到指定日志文件的末尾。 + /// 启动任务 /// - /// 要执行的操作。 - /// 日志文件名。 - public static void MeasureAndLog(Action action, string fileName) + /// 任务名称 + /// 是否成功 + public bool StartTask(string name) { - TimeSpan elapsedTime = Measure(action); - System.IO.File.AppendAllText(fileName, $"{DateTime.Now}: {elapsedTime.TotalMilliseconds}ms{Environment.NewLine}"); + var task = GetTask(name); + if (task == null) + return false; + + task.Start(); + return true; } /// - /// 计算两个时间的时间间隔。 + /// 停止任务 /// - /// 第一个时间。 - /// 第二个时间。 - /// 两个时间的时间间隔。 - public static TimeSpan GetTimeSpan(DateTime time1, DateTime time2) + /// 任务名称 + /// 是否成功 + public bool StopTask(string name) { - return time1 - time2; + var task = GetTask(name); + if (task == null) + return false; + + task.Stop(); + return true; } /// - /// 计算两个时间戳的时间间隔。 + /// 移除任务 /// - /// 第一个时间戳。 - /// 第二个时间戳。 - /// 两个时间戳的时间间隔。 - public static TimeSpan GetTimeSpan(long timestamp1, long timestamp2) + /// 任务名称 + /// 是否成功 + public bool RemoveTask(string name) { - DateTime time1 = DateTimeOffset.FromUnixTimeMilliseconds(timestamp1).LocalDateTime; - DateTime time2 = DateTimeOffset.FromUnixTimeMilliseconds(timestamp2).LocalDateTime; - return GetTimeSpan(time1, time2); + var task = GetTask(name); + if (task == null) + return false; + + task.Stop(); + _tasks.Remove(name); + return true; } /// - /// 将时间间隔格式化为友好的字符串,例如 1h 20m 30s。 + /// 启动所有任务 /// - /// 要格式化的时间间隔。 - /// 格式化后的字符串。 - public static string FormatTimeSpan(TimeSpan timeSpan) + public void StartAll() { - int hours = timeSpan.Days * 24 + timeSpan.Hours; - string formattedTimeSpan = $"{hours}h {timeSpan.Minutes}m {timeSpan.Seconds}s"; - return formattedTimeSpan; + foreach (var task in _tasks.Values) + { + task.Start(); + } } + /// + /// 停止所有任务 + /// + public void StopAll() + { + foreach (var task in _tasks.Values) + { + task.Stop(); + } + } + + /// + /// 获取所有任务名称 + /// + /// 任务名称列表 + public string[] GetTaskNames() + { + return _tasks.Keys.ToArray(); + } + + /// + /// 获取正在运行的任务数量 + /// + /// 数量 + public int GetRunningCount() + { + return _tasks.Values.Count(t => t.IsRunning); + } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs b/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs new file mode 100644 index 0000000..2768462 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 工作日计算工具类 + /// 提供工作日相关的计算功能,支持自定义节假日和调休 + /// + public static class WorkdayUtil + { + private static readonly HashSet _defaultHolidays = new(); + private static readonly HashSet _defaultWorkdays = new(); + + static WorkdayUtil() + { + // 可以在这里初始化默认的节假日和调休工作日 + } + + /// + /// 添加节假日 + /// + /// 节假日日期 + public static void AddHoliday(DateTime date) + { + _defaultHolidays.Add(date.Date); + } + + /// + /// 批量添加节假日 + /// + /// 节假日日期集合 + public static void AddHolidays(IEnumerable dates) + { + foreach (var date in dates) + { + _defaultHolidays.Add(date.Date); + } + } + + /// + /// 移除节假日 + /// + /// 节假日日期 + public static void RemoveHoliday(DateTime date) + { + _defaultHolidays.Remove(date.Date); + } + + /// + /// 添加调休工作日(周末调休上班) + /// + /// 调休工作日日期 + public static void AddWorkday(DateTime date) + { + _defaultWorkdays.Add(date.Date); + } + + /// + /// 批量添加调休工作日 + /// + /// 调休工作日日期集合 + public static void AddWorkdays(IEnumerable dates) + { + foreach (var date in dates) + { + _defaultWorkdays.Add(date.Date); + } + } + + /// + /// 移除调休工作日 + /// + /// 调休工作日日期 + public static void RemoveWorkday(DateTime date) + { + _defaultWorkdays.Remove(date.Date); + } + + /// + /// 清空所有节假日和调休工作日配置 + /// + public static void ClearAll() + { + _defaultHolidays.Clear(); + _defaultWorkdays.Clear(); + } + + /// + /// 判断是否为工作日 + /// + /// 日期 + /// 是否为工作日 + public static bool IsWorkday(DateTime date) + { + return IsWorkday(date, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 判断是否为工作日 + /// + /// 日期 + /// 节假日集合 + /// 调休工作日集合 + /// 是否为工作日 + public static bool IsWorkday(DateTime date, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + var dateOnly = date.Date; + var holidaySet = holidays?.Select(d => d.Date).ToHashSet() ?? new HashSet(); + var workdaySet = adjustedWorkdays?.Select(d => d.Date).ToHashSet() ?? new HashSet(); + + // 如果是调休工作日,返回true + if (workdaySet.Contains(dateOnly)) + return true; + + // 如果是节假日,返回false + if (holidaySet.Contains(dateOnly)) + return false; + + // 判断是否为周末 + var dayOfWeek = date.DayOfWeek; + return dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday; + } + + /// + /// 判断是否为周末 + /// + /// 日期 + /// 是否为周末 + public static bool IsWeekend(DateTime date) + { + var dayOfWeek = date.DayOfWeek; + return dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 判断是否为节假日 + /// + /// 日期 + /// 是否为节假日 + public static bool IsHoliday(DateTime date) + { + return _defaultHolidays.Contains(date.Date); + } + + /// + /// 计算两个日期之间的工作日数量 + /// + /// 开始日期 + /// 结束日期 + /// 工作日数量 + public static int GetWorkdayCount(DateTime startDate, DateTime endDate) + { + return GetWorkdayCount(startDate, endDate, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 计算两个日期之间的工作日数量 + /// + /// 开始日期 + /// 结束日期 + /// 节假日集合 + /// 调休工作日集合 + /// 工作日数量 + public static int GetWorkdayCount(DateTime startDate, DateTime endDate, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + if (startDate > endDate) + { + var temp = startDate; + startDate = endDate; + endDate = temp; + } + + int count = 0; + var current = startDate.Date; + var endDateOnly = endDate.Date; + + while (current <= endDateOnly) + { + if (IsWorkday(current, holidays, adjustedWorkdays)) + count++; + current = current.AddDays(1); + } + + return count; + } + + /// + /// 计算指定工作日数后的日期 + /// + /// 开始日期 + /// 工作日数(正数表示往后,负数表示往前) + /// 目标日期 + public static DateTime AddWorkdays(DateTime startDate, int workdays) + { + return AddWorkdays(startDate, workdays, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 计算指定工作日数后的日期 + /// + /// 开始日期 + /// 工作日数(正数表示往后,负数表示往前) + /// 节假日集合 + /// 调休工作日集合 + /// 目标日期 + public static DateTime AddWorkdays(DateTime startDate, int workdays, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + if (workdays == 0) + return startDate.Date; + + var current = startDate.Date; + var increment = workdays > 0 ? 1 : -1; + var remaining = Math.Abs(workdays); + + while (remaining > 0) + { + current = current.AddDays(increment); + + if (IsWorkday(current, holidays, adjustedWorkdays)) + remaining--; + } + + return current; + } + + /// + /// 获取下一个工作日 + /// + /// 起始日期 + /// 下一个工作日 + public static DateTime GetNextWorkday(DateTime date) + { + return AddWorkdays(date, 1); + } + + /// + /// 获取上一个工作日 + /// + /// 起始日期 + /// 上一个工作日 + public static DateTime GetPreviousWorkday(DateTime date) + { + return AddWorkdays(date, -1); + } + + /// + /// 获取指定日期所在周的工作日列表 + /// + /// 日期 + /// 周起始日 + /// 工作日列表 + public static List GetWorkdaysOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + var result = new List(); + var startOfWeek = GetStartOfWeek(date, weekStartsOn); + + for (int i = 0; i < 7; i++) + { + var current = startOfWeek.AddDays(i); + if (IsWorkday(current)) + result.Add(current); + } + + return result; + } + + /// + /// 获取指定日期所在月的工作日列表 + /// + /// 日期 + /// 工作日列表 + public static List GetWorkdaysOfMonth(DateTime date) + { + var result = new List(); + var firstDay = new DateTime(date.Year, date.Month, 1); + var lastDay = firstDay.AddMonths(1).AddDays(-1); + + var current = firstDay; + while (current <= lastDay) + { + if (IsWorkday(current)) + result.Add(current); + current = current.AddDays(1); + } + + return result; + } + + /// + /// 获取指定日期所在月的工作日数量 + /// + /// 年份 + /// 月份 + /// 工作日数量 + public static int GetWorkdaysInMonth(int year, int month) + { + var firstDay = new DateTime(year, month, 1); + var lastDay = firstDay.AddMonths(1).AddDays(-1); + return GetWorkdayCount(firstDay, lastDay); + } + + /// + /// 获取指定日期所在年的工作日数量 + /// + /// 年份 + /// 工作日数量 + public static int GetWorkdaysInYear(int year) + { + var firstDay = new DateTime(year, 1, 1); + var lastDay = new DateTime(year, 12, 31); + return GetWorkdayCount(firstDay, lastDay); + } + + /// + /// 获取指定日期所在周的第一天 + /// + /// 日期 + /// 周起始日 + /// 周的第一天 + public static DateTime GetStartOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + var diff = (7 + (date.DayOfWeek - weekStartsOn)) % 7; + return date.Date.AddDays(-diff); + } + + /// + /// 获取指定日期所在周的最后一天 + /// + /// 日期 + /// 周起始日 + /// 周的最后一天 + public static DateTime GetEndOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + return GetStartOfWeek(date, weekStartsOn).AddDays(6); + } + + /// + /// 计算工作日区间(返回所有工作日) + /// + /// 开始日期 + /// 结束日期 + /// 工作日列表 + public static List GetWorkdaysBetween(DateTime startDate, DateTime endDate) + { + var result = new List(); + + if (startDate > endDate) + { + var temp = startDate; + startDate = endDate; + endDate = temp; + } + + var current = startDate.Date; + while (current <= endDate.Date) + { + if (IsWorkday(current)) + result.Add(current); + current = current.AddDays(1); + } + + return result; + } + + /// + /// 判断是否为同一天 + /// + /// 日期1 + /// 日期2 + /// 是否为同一天 + public static bool IsSameDay(DateTime date1, DateTime date2) + { + return date1.Date == date2.Date; + } + + /// + /// 判断两个日期是否为同一周 + /// + /// 日期1 + /// 日期2 + /// 周起始日 + /// 是否为同一周 + public static bool IsSameWeek(DateTime date1, DateTime date2, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + return GetStartOfWeek(date1, weekStartsOn) == GetStartOfWeek(date2, weekStartsOn); + } + + /// + /// 判断两个日期是否为同一月 + /// + /// 日期1 + /// 日期2 + /// 是否为同一月 + public static bool IsSameMonth(DateTime date1, DateTime date2) + { + return date1.Year == date2.Year && date1.Month == date2.Month; + } + + /// + /// 判断两个日期是否为同一年 + /// + /// 日期1 + /// 日期2 + /// 是否为同一年 + public static bool IsSameYear(DateTime date1, DateTime date2) + { + return date1.Year == date2.Year; + } + + /// + /// 获取第n个工作日 + /// + /// 年份 + /// 月份 + /// 第n个工作日(从1开始) + /// 工作日日期 + public static DateTime GetNthWorkdayOfMonth(int year, int month, int n) + { + if (n < 1) + throw new ArgumentException("n必须大于0", nameof(n)); + + var workdays = GetWorkdaysOfMonth(new DateTime(year, month, 1)); + + if (n > workdays.Count) + throw new ArgumentException($"该月只有{workdays.Count}个工作日", nameof(n)); + + return workdays[n - 1]; + } + + /// + /// 获取日期在当月中的第几个工作日 + /// + /// 日期 + /// 第几个工作日(从1开始),如果不是工作日返回-1 + public static int GetWorkdayIndexInMonth(DateTime date) + { + if (!IsWorkday(date)) + return -1; + + var workdays = GetWorkdaysOfMonth(date); + return workdays.FindIndex(d => d.Date == date.Date) + 1; + } + } +} diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index db83217..97ac135 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -45,7 +45,22 @@ + + + + + + + + + + + + + + + diff --git a/EasyTool.Core/IOCategory/ArchiveUtil.cs b/EasyTool.Core/IOCategory/ArchiveUtil.cs new file mode 100644 index 0000000..13d5be9 --- /dev/null +++ b/EasyTool.Core/IOCategory/ArchiveUtil.cs @@ -0,0 +1,449 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 压缩包工具类 + /// 支持 ZIP、TAR、GZip 等格式 + /// + public static class ArchiveUtil + { + #region ZIP 操作 + + /// + /// 创建 ZIP 压缩包 + /// + /// 源文件或目录路径 + /// ZIP 文件路径 + /// 压缩级别 + /// 是否包含根目录 + public static void CreateZip(string sourcePath, string zipPath, CompressionLevel compressionLevel = CompressionLevel.Optimal, bool includeBaseDirectory = false) + { + if (File.Exists(sourcePath)) + { + // 压缩单个文件 + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create); + archive.CreateEntryFromFile(sourcePath, Path.GetFileName(sourcePath), compressionLevel); + } + else if (Directory.Exists(sourcePath)) + { + // 压缩目录 + ZipFile.CreateFromDirectory(sourcePath, zipPath, compressionLevel, includeBaseDirectory); + } + else + { + throw new FileNotFoundException("源路径不存在", sourcePath); + } + } + + /// + /// 解压 ZIP 文件 + /// + /// ZIP 文件路径 + /// 解压目录 + /// 是否覆盖已存在的文件 + public static void ExtractZip(string zipPath, string extractPath, bool overwrite = false) + { + ZipFile.ExtractToDirectory(zipPath, extractPath, overwrite); + } + + /// + /// 列出 ZIP 文件内容 + /// + /// ZIP 文件路径 + /// 文件条目列表 + public static List ListZip(string zipPath) + { + using var archive = ZipFile.OpenRead(zipPath); + return archive.Entries.Select(e => new ArchiveEntry + { + Name = e.Name, + FullName = e.FullName, + Length = e.Length, + CompressedLength = e.CompressedLength, + LastWriteTime = e.LastWriteTime.DateTime, + IsDirectory = string.IsNullOrEmpty(e.Name) + }).ToList(); + } + + /// + /// 从 ZIP 中提取单个文件 + /// + /// ZIP 文件路径 + /// 条目名称 + /// 目标路径 + public static void ExtractFileFromZip(string zipPath, string entryName, string destinationPath) + { + using var archive = ZipFile.OpenRead(zipPath); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"ZIP 中找不到条目: {entryName}"); + + entry.ExtractToFile(destinationPath, true); + } + + /// + /// 向 ZIP 添加文件 + /// + /// ZIP 文件路径 + /// 要添加的文件路径 + /// ZIP 中的条目名称 + public static void AddFileToZip(string zipPath, string filePath, string? entryName = null) + { + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Update); + archive.CreateEntryFromFile(filePath, entryName ?? Path.GetFileName(filePath)); + } + + /// + /// 从 ZIP 删除文件 + /// + /// ZIP 文件路径 + /// 要删除的条目名称 + public static void RemoveFileFromZip(string zipPath, string entryName) + { + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Update); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"ZIP 中找不到条目: {entryName}"); + entry.Delete(); + } + + #endregion + + #region GZip 操作 + + /// + /// 使用 GZip 压缩文件 + /// + /// 源文件路径 + /// 目标文件路径(可选,默认添加 .gz 后缀) + public static void CompressGZip(string sourcePath, string? destinationPath = null) + { + destinationPath ??= sourcePath + ".gz"; + + using var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read); + using var destinationStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write); + using var gzipStream = new GZipStream(destinationStream, CompressionMode.Compress); + + sourceStream.CopyTo(gzipStream); + } + + /// + /// 解压 GZip 文件 + /// + /// GZip 文件路径 + /// 目标文件路径(可选,默认移除 .gz 后缀) + public static void DecompressGZip(string sourcePath, string? destinationPath = null) + { + if (destinationPath == null) + { + destinationPath = sourcePath.EndsWith(".gz", StringComparison.OrdinalIgnoreCase) + ? sourcePath.Substring(0, sourcePath.Length - 3) + : sourcePath + ".out"; + } + + using var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read); + using var destinationStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write); + using var gzipStream = new GZipStream(sourceStream, CompressionMode.Decompress); + + gzipStream.CopyTo(destinationStream); + } + + /// + /// 压缩字节数组 + /// + /// 原始数据 + /// 压缩后的数据 + public static byte[] CompressGZip(byte[] data) + { + using var output = new MemoryStream(); + using (var gzip = new GZipStream(output, CompressionMode.Compress)) + { + gzip.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// 解压字节数组 + /// + /// 压缩数据 + /// 解压后的数据 + public static byte[] DecompressGZip(byte[] compressedData) + { + using var input = new MemoryStream(compressedData); + using var gzip = new GZipStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + gzip.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region Tar 操作 + + /// + /// 创建 TAR 归档 + /// + /// 源目录路径 + /// TAR 文件路径 + public static void CreateTar(string sourcePath, string tarPath) + { + using var output = new FileStream(tarPath, FileMode.Create, FileAccess.Write); + using var tar = new TarWriter(output); + + if (Directory.Exists(sourcePath)) + { + var dir = new DirectoryInfo(sourcePath); + foreach (var file in dir.GetFiles("*", SearchOption.AllDirectories)) + { + var relativePath = GetRelativePath(dir.FullName, file.FullName); + tar.Write(file.FullName, relativePath); + } + } + else if (File.Exists(sourcePath)) + { + tar.Write(sourcePath, Path.GetFileName(sourcePath)); + } + } + + /// + /// 创建 TAR.GZ 归档 + /// + /// 源目录路径 + /// TAR.GZ 文件路径 + public static void CreateTarGz(string sourcePath, string tarGzPath) + { + using var output = new FileStream(tarGzPath, FileMode.Create, FileAccess.Write); + using var gzip = new GZipStream(output, CompressionMode.Compress); + using var tar = new TarWriter(gzip); + + if (Directory.Exists(sourcePath)) + { + var dir = new DirectoryInfo(sourcePath); + foreach (var file in dir.GetFiles("*", SearchOption.AllDirectories)) + { + var relativePath = GetRelativePath(dir.FullName, file.FullName); + tar.Write(file.FullName, relativePath); + } + } + else if (File.Exists(sourcePath)) + { + tar.Write(sourcePath, Path.GetFileName(sourcePath)); + } + } + + private static string GetRelativePath(string basePath, string fullPath) + { + if (fullPath.StartsWith(basePath)) + { + var relative = fullPath.Substring(basePath.Length); + return relative.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + return fullPath; + } + + #endregion + + #region 内存压缩 + + /// + /// 将多个文件压缩到内存 + /// + /// 文件字典(文件名 -> 文件内容) + /// 压缩后的数据 + public static byte[] CreateZipInMemory(Dictionary files) + { + using var output = new MemoryStream(); + using (var archive = new ZipArchive(output, ZipArchiveMode.Create, true)) + { + foreach (var kvp in files) + { + var entry = archive.CreateEntry(kvp.Key); + using var entryStream = entry.Open(); + entryStream.Write(kvp.Value, 0, kvp.Value.Length); + } + } + return output.ToArray(); + } + + /// + /// 从内存中解压文件 + /// + /// ZIP 数据 + /// 文件字典 + public static Dictionary ExtractZipFromMemory(byte[] zipData) + { + var result = new Dictionary(); + + using var input = new MemoryStream(zipData); + using var archive = new ZipArchive(input, ZipArchiveMode.Read); + + foreach (var entry in archive.Entries) + { + if (!string.IsNullOrEmpty(entry.Name)) + { + using var entryStream = entry.Open(); + using var output = new MemoryStream(); + entryStream.CopyTo(output); + result[entry.FullName] = output.ToArray(); + } + } + + return result; + } + + #endregion + + #region 流式压缩 + + /// + /// 创建压缩流 + /// + /// 输出流 + /// 压缩流 + public static Stream CreateCompressStream(Stream outputStream) + { + return new GZipStream(outputStream, CompressionMode.Compress); + } + + /// + /// 创建解压流 + /// + /// 输入流 + /// 解压流 + public static Stream CreateDecompressStream(Stream inputStream) + { + return new GZipStream(inputStream, CompressionMode.Decompress); + } + + #endregion + } + + /// + /// 压缩包条目 + /// + public class ArchiveEntry + { + /// + /// 文件名 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 完整路径 + /// + public string FullName { get; set; } = string.Empty; + + /// + /// 原始大小 + /// + public long Length { get; set; } + + /// + /// 压缩后大小 + /// + public long CompressedLength { get; set; } + + /// + /// 最后修改时间 + /// + public DateTime LastWriteTime { get; set; } + + /// + /// 是否为目录 + /// + public bool IsDirectory { get; set; } + + /// + /// 压缩率 + /// + public double CompressionRatio => Length > 0 ? (1 - (double)CompressedLength / Length) * 100 : 0; + } + + #region TarWriter 简单实现 + + internal class TarWriter : IDisposable + { + private readonly Stream _stream; + private static readonly byte[] EmptyBlock = new byte[512]; + + public TarWriter(Stream stream) + { + _stream = stream; + } + + public void Write(string filePath, string entryName) + { + var fileInfo = new FileInfo(filePath); + var header = CreateHeader(entryName, fileInfo.Length, fileInfo.LastWriteTime); + _stream.Write(header, 0, header.Length); + + using var fileStream = fileInfo.OpenRead(); + fileStream.CopyTo(_stream); + + // 填充到 512 字节边界 + var remainder = fileInfo.Length % 512; + if (remainder > 0) + { + _stream.Write(EmptyBlock, 0, (int)(512 - remainder)); + } + } + + private byte[] CreateHeader(string name, long size, DateTime mtime) + { + var header = new byte[512]; + var nameBytes = System.Text.Encoding.UTF8.GetBytes(name); + + // 名称 + Array.Copy(nameBytes, header, Math.Min(nameBytes.Length, 100)); + + // 文件模式 + var mode = "0000644\0"u8.ToArray(); + Array.Copy(mode, 0, header, 100, mode.Length); + + // UID/GID + var uid = "0000000\0"u8.ToArray(); + Array.Copy(uid, 0, header, 108, uid.Length); + Array.Copy(uid, 0, header, 116, uid.Length); + + // 大小(八进制) + var sizeStr = Convert.ToString(size, 8).PadLeft(11, '0') + "\0"; + var sizeBytes = System.Text.Encoding.ASCII.GetBytes(sizeStr); + Array.Copy(sizeBytes, 0, header, 124, sizeBytes.Length); + + // 修改时间 + var unixTime = new DateTimeOffset(mtime).ToUnixTimeSeconds(); + var mtimeStr = Convert.ToString(unixTime, 8).PadLeft(11, '0') + "\0"; + var mtimeBytes = System.Text.Encoding.ASCII.GetBytes(mtimeStr); + Array.Copy(mtimeBytes, 0, header, 136, mtimeBytes.Length); + + // 类型标志 + header[156] = (byte)'0'; // 普通文件 + + // 校验和(先填空格) + for (int i = 148; i < 156; i++) header[i] = (byte)' '; + + // 计算校验和 + int checksum = 0; + foreach (var b in header) checksum += b; + + var checksumStr = Convert.ToString(checksum, 8).PadLeft(6, '0') + "\0 "; + var checksumBytes = System.Text.Encoding.ASCII.GetBytes(checksumStr); + Array.Copy(checksumBytes, 0, header, 148, checksumBytes.Length); + + return header; + } + + public void Dispose() + { + // 写入两个空块作为文件结束 + _stream.Write(EmptyBlock, 0, EmptyBlock.Length); + _stream.Write(EmptyBlock, 0, EmptyBlock.Length); + } + } + + #endregion +} diff --git a/EasyTool.Core/IOCategory/CompressionUtil.cs b/EasyTool.Core/IOCategory/CompressionUtil.cs new file mode 100644 index 0000000..a529aa7 --- /dev/null +++ b/EasyTool.Core/IOCategory/CompressionUtil.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 压缩工具类 + /// + public static class CompressionUtil + { + #region GZip + + /// + /// GZip压缩 + /// + public static byte[] GZipCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var gzip = new GZipStream(output, CompressionMode.Compress)) + { + gzip.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// GZip解压 + /// + public static byte[] GZipDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var gzip = new GZipStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + gzip.CopyTo(output); + return output.ToArray(); + } + + /// + /// GZip压缩字符串 + /// + public static string GZipCompressString(string text, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + var data = encoding.GetBytes(text); + var compressed = GZipCompress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// GZip解压字符串 + /// + public static string GZipDecompressString(string compressedText, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + var data = Convert.FromBase64String(compressedText); + var decompressed = GZipDecompress(data); + return encoding.GetString(decompressed); + } + + #endregion + + #region Deflate + + /// + /// Deflate压缩 + /// + public static byte[] DeflateCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var deflate = new DeflateStream(output, CompressionMode.Compress)) + { + deflate.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// Deflate解压 + /// + public static byte[] DeflateDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var deflate = new DeflateStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + deflate.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region Zip + + /// + /// 压缩文件到Zip + /// + public static void ZipFile(string sourceFilePath, string zipFilePath) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + System.IO.Compression.ZipFile.CreateFromDirectory( + Path.GetDirectoryName(sourceFilePath) ?? "", + zipFilePath); + } + + /// + /// 压缩目录到Zip + /// + public static void ZipDirectory(string sourceDirectory, string zipFilePath, bool includeBaseDirectory = true) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + System.IO.Compression.ZipFile.CreateFromDirectory(sourceDirectory, zipFilePath, + CompressionLevel.Optimal, includeBaseDirectory); + } + + /// + /// 解压Zip文件 + /// + public static void Unzip(string zipFilePath, string destinationDirectory) + { + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + + /// + /// 解压Zip文件(覆盖已存在的文件) + /// + public static void Unzip(string zipFilePath, string destinationDirectory, bool overwrite) + { + if (overwrite) + { + // 先删除目标目录中的文件 + if (Directory.Exists(destinationDirectory)) + { + Directory.Delete(destinationDirectory, true); + } + Directory.CreateDirectory(destinationDirectory); + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + else + { + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + } + + /// + /// 压缩文件列表到Zip + /// + public static void ZipFiles(IEnumerable filePaths, string zipFilePath, string? basePath = null) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Create); + foreach (var filePath in filePaths) + { + var entryName = basePath != null + ? filePath.Substring(basePath.Length).TrimStart(Path.DirectorySeparatorChar) + : Path.GetFileName(filePath); + archive.CreateEntryFromFile(filePath, entryName); + } + } + + /// + /// 获取Zip文件中的文件列表 + /// + public static List GetZipEntries(string zipFilePath) + { + using var archive = System.IO.Compression.ZipFile.OpenRead(zipFilePath); + return archive.Entries.Select(e => e.FullName).ToList(); + } + + /// + /// 从Zip中提取单个文件 + /// + public static void ExtractFile(string zipFilePath, string entryName, string destinationPath) + { + using var archive = System.IO.Compression.ZipFile.OpenRead(zipFilePath); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"Zip中未找到文件: {entryName}"); + entry.ExtractToFile(destinationPath, true); + } + + /// + /// 向Zip添加文件 + /// + public static void AddFileToZip(string zipFilePath, string filePath, string? entryName = null) + { + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Update); + archive.CreateEntryFromFile(filePath, entryName ?? Path.GetFileName(filePath)); + } + + /// + /// 从Zip删除文件 + /// + public static void RemoveFileFromZip(string zipFilePath, string entryName) + { + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Update); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"Zip中未找到文件: {entryName}"); + entry.Delete(); + } + + #endregion + + #region Brotli + + /// + /// Brotli压缩 + /// + public static byte[] BrotliCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var brotli = new BrotliStream(output, CompressionMode.Compress)) + { + brotli.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// Brotli解压 + /// + public static byte[] BrotliDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var brotli = new BrotliStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + brotli.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region 压缩率计算 + + /// + /// 计算压缩率 + /// + public static double CalculateCompressionRatio(long originalSize, long compressedSize) + { + if (originalSize == 0) + return 0; + return (double)(originalSize - compressedSize) / originalSize * 100; + } + + /// + /// 获取最佳压缩级别 + /// + public static CompressionLevel GetOptimalCompressionLevel(double targetRatio) + { + return targetRatio switch + { + > 80 => CompressionLevel.Optimal, + > 50 => CompressionLevel.Optimal, + > 20 => CompressionLevel.Fastest, + _ => CompressionLevel.NoCompression + }; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/ConfigUtil.cs b/EasyTool.Core/IOCategory/ConfigUtil.cs new file mode 100644 index 0000000..04de5fd --- /dev/null +++ b/EasyTool.Core/IOCategory/ConfigUtil.cs @@ -0,0 +1,252 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 配置文件工具类 + /// 支持INI格式配置文件 + /// + public static class ConfigUtil + { + /// + /// 读取INI配置值 + /// + public static string? GetIniValue(string filePath, string section, string key) + { + if (!File.Exists(filePath)) + return null; + + var lines = File.ReadAllLines(filePath); + var currentSection = ""; + var sectionHeader = $"[{section}]"; + + foreach (var line in lines) + { + var trimmed = line.Trim(); + + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed; + continue; + } + + if (currentSection == sectionHeader) + { + if (trimmed.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + trimmed.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + var valueStart = trimmed.IndexOf('=') + 1; + return trimmed.Substring(valueStart).Trim(); + } + } + } + + return null; + } + + /// + /// 设置INI配置值 + /// + public static void SetIniValue(string filePath, string section, string key, string value) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = File.Exists(filePath) ? File.ReadAllLines(filePath).ToList() : new List(); + var sectionHeader = $"[{section}]"; + var sectionIndex = -1; + var keyIndex = -1; + + // 查找section + for (int i = 0; i < lines.Count; i++) + { + if (lines[i].Trim() == sectionHeader) + { + sectionIndex = i; + break; + } + } + + // 如果section不存在,添加它 + if (sectionIndex < 0) + { + if (lines.Count > 0 && !string.IsNullOrWhiteSpace(lines[^1])) + lines.Add(""); + lines.Add(sectionHeader); + lines.Add($"{key}={value}"); + } + else + { + // 查找key + for (int i = sectionIndex + 1; i < lines.Count; i++) + { + var line = lines[i].Trim(); + if (line.StartsWith("[") && line.EndsWith("]")) + break; // 进入下一个section + + if (line.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + line.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + keyIndex = i; + break; + } + } + + if (keyIndex >= 0) + { + lines[keyIndex] = $"{key}={value}"; + } + else + { + lines.Insert(sectionIndex + 1, $"{key}={value}"); + } + } + + File.WriteAllLines(filePath, lines); + } + + /// + /// 读取INI配置的所有键值对 + /// + public static Dictionary GetIniSection(string filePath, string section) + { + var result = new Dictionary(); + + if (!File.Exists(filePath)) + return result; + + var lines = File.ReadAllLines(filePath); + var currentSection = ""; + var sectionHeader = $"[{section}]"; + + foreach (var line in lines) + { + var trimmed = line.Trim(); + + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed; + continue; + } + + if (currentSection == sectionHeader && trimmed.Contains("=")) + { + var eqIndex = trimmed.IndexOf('='); + var key = trimmed.Substring(0, eqIndex).Trim(); + var value = trimmed.Substring(eqIndex + 1).Trim(); + result[key] = value; + } + } + + return result; + } + + /// + /// 获取INI文件所有节名 + /// + public static List GetIniSections(string filePath) + { + var sections = new List(); + + if (!File.Exists(filePath)) + return sections; + + var lines = File.ReadAllLines(filePath); + + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + var sectionName = trimmed.Substring(1, trimmed.Length - 2); + sections.Add(sectionName); + } + } + + return sections; + } + + /// + /// 删除INI键 + /// + public static void RemoveIniKey(string filePath, string section, string key) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath).ToList(); + var sectionHeader = $"[{section}]"; + var inSection = false; + var keyIndex = -1; + + for (int i = 0; i < lines.Count; i++) + { + var line = lines[i].Trim(); + + if (line == sectionHeader) + { + inSection = true; + continue; + } + + if (inSection) + { + if (line.StartsWith("[") && line.EndsWith("]")) + break; + + if (line.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + line.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + keyIndex = i; + break; + } + } + } + + if (keyIndex >= 0) + { + lines.RemoveAt(keyIndex); + File.WriteAllLines(filePath, lines); + } + } + + /// + /// 删除INI节 + /// + public static void RemoveIniSection(string filePath, string section) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath).ToList(); + var sectionHeader = $"[{section}]"; + var startIndex = -1; + var endIndex = lines.Count; + + for (int i = 0; i < lines.Count; i++) + { + var line = lines[i].Trim(); + + if (line == sectionHeader) + { + startIndex = i; + } + else if (startIndex >= 0 && line.StartsWith("[") && line.EndsWith("]")) + { + endIndex = i; + break; + } + } + + if (startIndex >= 0) + { + lines.RemoveRange(startIndex, endIndex - startIndex); + File.WriteAllLines(filePath, lines); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/CsvStreamingReader.cs b/EasyTool.Core/IOCategory/CsvStreamingReader.cs new file mode 100644 index 0000000..a7856b1 --- /dev/null +++ b/EasyTool.Core/IOCategory/CsvStreamingReader.cs @@ -0,0 +1,483 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// CSV 流式读取器选项 + /// + public class CsvReaderOptions + { + /// + /// 分隔符(默认逗号) + /// + public char Delimiter { get; set; } = ','; + + /// + /// 引号字符(默认双引号) + /// + public char QuoteChar { get; set; } = '"'; + + /// + /// 是否有标题行 + /// + public bool HasHeader { get; set; } = true; + + /// + /// 编码(默认 UTF-8) + /// + public Encoding Encoding { get; set; } = Encoding.UTF8; + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } = 4096; + + /// + /// 是否跳过空行 + /// + public bool SkipEmptyLines { get; set; } = true; + + /// + /// 是否去除字段首尾空白 + /// + public bool TrimFields { get; set; } = false; + } + + /// + /// CSV 流式读取器 + /// 支持大文件逐行读取 + /// + public class CsvStreamingReader : IDisposable + { + private readonly TextReader _reader; + private readonly CsvReaderOptions _options; + private string[]? _headers; + private int _lineNumber; + private bool _disposed; + + /// + /// 获取标题行 + /// + public string[]? Headers => _headers; + + /// + /// 获取当前行号 + /// + public int LineNumber => _lineNumber; + + /// + /// 创建 CSV 流式读取器 + /// + /// 文本读取器 + /// 选项 + public CsvStreamingReader(TextReader reader, CsvReaderOptions? options = null) + { + _reader = reader ?? throw new ArgumentNullException(nameof(reader)); + _options = options ?? new CsvReaderOptions(); + _lineNumber = 0; + + if (_options.HasHeader) + { + ReadHeaders(); + } + } + + /// + /// 从文件创建 CSV 流式读取器 + /// + /// 文件路径 + /// 选项 + /// CSV 流式读取器 + public static CsvStreamingReader FromFile(string filePath, CsvReaderOptions? options = null) + { + options ??= new CsvReaderOptions(); + var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, options.BufferSize); + var reader = new StreamReader(stream, options.Encoding); + return new CsvStreamingReader(reader, options); + } + + /// + /// 从字符串创建 CSV 流式读取器 + /// + /// CSV 内容 + /// 选项 + /// CSV 流式读取器 + public static CsvStreamingReader FromString(string content, CsvReaderOptions? options = null) + { + var reader = new StringReader(content); + return new CsvStreamingReader(reader, options); + } + + /// + /// 读取标题行 + /// + private void ReadHeaders() + { + var line = _reader.ReadLine(); + _lineNumber++; + + if (line != null) + { + _headers = ParseLine(line); + } + } + + /// + /// 读取下一行 + /// + /// 字段数组,如果到达文件末尾则返回 null + public string[]? ReadLine() + { + while (true) + { + var line = _reader.ReadLine(); + _lineNumber++; + + if (line == null) + return null; + + if (_options.SkipEmptyLines && string.IsNullOrWhiteSpace(line)) + continue; + + return ParseLine(line); + } + } + + /// + /// 异步读取下一行 + /// + /// 取消令牌 + /// 字段数组,如果到达文件末尾则返回 null + public async Task ReadLineAsync(CancellationToken cancellationToken = default) + { + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + var line = await _reader.ReadLineAsync(); + + if (line == null) + return null; + + _lineNumber++; + + if (_options.SkipEmptyLines && string.IsNullOrWhiteSpace(line)) + continue; + + return ParseLine(line); + } + } + + /// + /// 读取所有行 + /// + /// 所有行的枚举 + public IEnumerable ReadAll() + { + string[]? line; + + while ((line = ReadLine()) != null) + { + yield return line; + } + } + + /// + /// 异步读取所有行 + /// + /// 取消令牌 + /// 所有行的异步枚举 + public async IAsyncEnumerable ReadAllAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + string[]? line; + + while ((line = await ReadLineAsync(cancellationToken)) != null) + { + yield return line; + } + } + + /// + /// 读取为字典(需要标题行) + /// + /// 字典行的枚举 + public IEnumerable> ReadAsDict() + { + if (_headers == null) + throw new InvalidOperationException("需要标题行才能读取为字典"); + + string[]? line; + + while ((line = ReadLine()) != null) + { + yield return LineToDict(line); + } + } + + /// + /// 异步读取为字典(需要标题行) + /// + /// 取消令牌 + /// 字典行的异步枚举 + public async IAsyncEnumerable> ReadAsDictAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + if (_headers == null) + throw new InvalidOperationException("需要标题行才能读取为字典"); + + string[]? line; + + while ((line = await ReadLineAsync(cancellationToken)) != null) + { + yield return LineToDict(line); + } + } + + /// + /// 解析 CSV 行 + /// + private string[] ParseLine(string line) + { + var fields = new List(); + var currentField = new StringBuilder(); + var inQuotes = false; + + for (int i = 0; i < line.Length; i++) + { + var c = line[i]; + + if (inQuotes) + { + if (c == _options.QuoteChar) + { + // 检查是否是转义引号 + if (i + 1 < line.Length && line[i + 1] == _options.QuoteChar) + { + currentField.Append(_options.QuoteChar); + i++; + } + else + { + inQuotes = false; + } + } + else + { + currentField.Append(c); + } + } + else + { + if (c == _options.QuoteChar) + { + inQuotes = true; + } + else if (c == _options.Delimiter) + { + fields.Add(FinalizeField(currentField)); + currentField.Clear(); + } + else + { + currentField.Append(c); + } + } + } + + fields.Add(FinalizeField(currentField)); + + return fields.ToArray(); + } + + private string FinalizeField(StringBuilder field) + { + var result = field.ToString(); + + if (_options.TrimFields) + { + result = result.Trim(); + } + + return result; + } + + private Dictionary LineToDict(string[] fields) + { + var dict = new Dictionary(); + + for (int i = 0; i < _headers!.Length && i < fields.Length; i++) + { + dict[_headers[i]] = fields[i]; + } + + return dict; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _reader.Dispose(); + _disposed = true; + } + } + } + + /// + /// CSV 流式写入器 + /// + public class CsvStreamingWriter : IDisposable + { + private readonly TextWriter _writer; + private readonly CsvReaderOptions _options; + private bool _disposed; + private bool _headerWritten; + + /// + /// 创建 CSV 流式写入器 + /// + /// 文本写入器 + /// 选项 + public CsvStreamingWriter(TextWriter writer, CsvReaderOptions? options = null) + { + _writer = writer ?? throw new ArgumentNullException(nameof(writer)); + _options = options ?? new CsvReaderOptions(); + } + + /// + /// 创建文件 CSV 写入器 + /// + /// 文件路径 + /// 选项 + /// 是否追加 + /// CSV 写入器 + public static CsvStreamingWriter ToFile(string filePath, CsvReaderOptions? options = null, bool append = false) + { + options ??= new CsvReaderOptions(); + var stream = new FileStream(filePath, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, options.BufferSize); + var writer = new StreamWriter(stream, options.Encoding); + return new CsvStreamingWriter(writer, options); + } + + /// + /// 写入标题行 + /// + /// 标题 + public void WriteHeaders(params string[] headers) + { + if (_headerWritten) + throw new InvalidOperationException("标题行已写入"); + + WriteLine(headers); + _headerWritten = true; + } + + /// + /// 写入一行 + /// + /// 字段 + public void WriteLine(params string[] fields) + { + var line = FormatLine(fields); + _writer.WriteLine(line); + } + + /// + /// 异步写入一行 + /// + /// 字段 + public async Task WriteLineAsync(params string[] fields) + { + var line = FormatLine(fields); + await _writer.WriteLineAsync(line); + } + + /// + /// 写入字典行 + /// + /// 字典 + /// 列顺序 + public void WriteDict(Dictionary dict, string[]? columnOrder = null) + { + var columns = columnOrder ?? dict.Keys.ToArray(); + var fields = columns.Select(c => dict.TryGetValue(c, out var v) ? v : "").ToArray(); + WriteLine(fields); + } + + /// + /// 异步写入字典行 + /// + /// 字典 + /// 列顺序 + public async Task WriteDictAsync(Dictionary dict, string[]? columnOrder = null) + { + var columns = columnOrder ?? dict.Keys.ToArray(); + var fields = columns.Select(c => dict.TryGetValue(c, out var v) ? v : "").ToArray(); + await WriteLineAsync(fields); + } + + /// + /// 刷新缓冲区 + /// + public void Flush() + { + _writer.Flush(); + } + + /// + /// 异步刷新缓冲区 + /// + public async Task FlushAsync() + { + await _writer.FlushAsync(); + } + + private string FormatLine(string[] fields) + { + var formattedFields = fields.Select(f => FormatField(f)); + return string.Join(_options.Delimiter, formattedFields); + } + + private string FormatField(string field) + { + if (string.IsNullOrEmpty(field)) + return ""; + + bool needsQuoting = field.Contains(_options.Delimiter) || + + field.Contains(_options.QuoteChar) || + + field.Contains('\n') || + + field.Contains('\r'); + + if (needsQuoting) + { + var escaped = field.Replace(_options.QuoteChar.ToString(), _options.QuoteChar.ToString() + _options.QuoteChar.ToString()); + return $"{_options.QuoteChar}{escaped}{_options.QuoteChar}"; + } + + return field; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _writer.Dispose(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/IOCategory/CsvUtil.cs b/EasyTool.Core/IOCategory/CsvUtil.cs index cdcc0ef..94fba42 100644 --- a/EasyTool.Core/IOCategory/CsvUtil.cs +++ b/EasyTool.Core/IOCategory/CsvUtil.cs @@ -1,376 +1,622 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; +using System.Linq; +using System.Reflection; using System.Text; +using System.Threading.Tasks; namespace EasyTool.IOCategory { /// - /// CSV 工具类 - /// 提供高性能的 CSV 读写功能 + /// CSV工具类 + /// 提供CSV文件的读写功能 /// public static class CsvUtil { + #region 读取CSV + /// - /// 读取 CSV 文件 + /// 读取CSV文件为字符串二维数组 /// - public static List Read(string filePath, bool hasHeader = true, char delimiter = ',', char quote = '"') + /// 文件路径 + /// 编码 + /// 是否有标题行 + /// 分隔符 + /// 数据数组 + public static string[][] Read(string filePath, Encoding? encoding = null, bool hasHeader = false, char delimiter = ',') { - var reader = new CsvReader(delimiter, quote); - return reader.ReadFile(filePath, hasHeader); + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + var result = new List(); + + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) + { + var row = ParseLine(lines[i], delimiter); + result.Add(row); + } + + return result.ToArray(); } /// - /// 读取 CSV 文件(带表头映射) + /// 异步读取CSV文件 /// - public static List> ReadWithHeader(string filePath, char delimiter = ',', char quote = '"') + public static async Task ReadAsync(string filePath, Encoding? encoding = null, bool hasHeader = false, char delimiter = ',') { - var reader = new CsvReader(delimiter, quote); - return reader.ReadFileWithHeader(filePath); + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = await File.ReadAllLinesAsync(filePath, encoding); + var result = new List(); + + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) + { + var row = ParseLine(lines[i], delimiter); + result.Add(row); + } + + return result.ToArray(); } /// - /// 从字符串解析 CSV + /// 读取CSV文件为对象列表 /// - public static List Parse(string content, bool hasHeader = true, char delimiter = ',', char quote = '"') + /// 对象类型 + /// 文件路径 + /// 编码 + /// 分隔符 + /// 对象列表 + public static List Read(string filePath, Encoding? encoding = null, char delimiter = ',') where T : class, new() { - var reader = new CsvReader(delimiter, quote); - return reader.Parse(content, hasHeader); + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(headers[j], StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + + result.Add(obj); + } + + return result; } /// - /// 写入 CSV 文件 + /// 异步读取CSV文件为对象列表 /// - public static void Write(string filePath, IEnumerable rows, char delimiter = ',', char quote = '"') + public static async Task> ReadAsync(string filePath, Encoding? encoding = null, char delimiter = ',') where T : class, new() { - var writer = new CsvWriter(delimiter, quote); - writer.WriteFile(filePath, rows); + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = await File.ReadAllLinesAsync(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(headers[j], StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + + result.Add(obj); + } + + return result; } /// - /// 写入 CSV 文件(带表头) + /// 读取CSV文件(带标题映射) /// - public static void WriteWithHeader(string filePath, string[] headers, IEnumerable> rows, - char delimiter = ',', char quote = '"') + public static List Read(string filePath, Dictionary columnMapping, Encoding? encoding = null, char delimiter = ',') where T : class, new() { - var writer = new CsvWriter(delimiter, quote); - writer.WriteFileWithHeader(filePath, headers, rows); + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var csvColumn = headers[j]; + string? propertyName; + + if (columnMapping.TryGetValue(csvColumn, out propertyName) || + columnMapping.TryGetValue(csvColumn.ToLower(), out propertyName)) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + } + + result.Add(obj); + } + + return result; } + #endregion + + #region 写入CSV + /// - /// 将数据转换为 CSV 字符串 + /// 写入CSV文件 /// - public static string ToString(IEnumerable rows, char delimiter = ',', char quote = '"') + /// 文件路径 + /// 数据 + /// 标题行 + /// 编码 + /// 分隔符 + public static void Write(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') { - var writer = new CsvWriter(delimiter, quote); - return writer.ToString(rows); + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = new List(); + + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + File.WriteAllLines(filePath, lines, encoding); } /// - /// 转义 CSV 字段 + /// 异步写入CSV文件 /// - public static string EscapeField(string field, char delimiter = ',', char quote = '"') + public static async Task WriteAsync(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') { - if (string.IsNullOrEmpty(field)) - return ""; + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = new List(); - bool needsQuote = field.Contains(delimiter.ToString()) || - field.Contains(quote.ToString()) || - field.Contains("\n") || - field.Contains("\r"); + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } - if (needsQuote) + foreach (var row in data) { - return quote + field.Replace(quote.ToString(), quote.ToString() + quote.ToString()) + quote; + lines.Add(FormatLine(row, delimiter)); } - return field; + await File.WriteAllLinesAsync(filePath, lines, encoding); } /// - /// 反转义 CSV 字段 + /// 写入对象列表到CSV文件 /// - public static string UnescapeField(string field, char quote = '"') + /// 对象类型 + /// 文件路径 + /// 数据列表 + /// 自定义标题(可选) + /// 编码 + /// 分隔符 + public static void Write(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') { - if (string.IsNullOrEmpty(field)) - return field; + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); + + var lines = new List(); - if (field.StartsWith(quote.ToString()) && field.EndsWith(quote.ToString())) + // 标题行 + if (headers != null && headers.Length > 0) { - string inner = field.Substring(1, field.Length - 2); - return inner.Replace(quote.ToString() + quote.ToString(), quote.ToString()); + lines.Add(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + lines.Add(FormatLine(propertyNames, delimiter)); } - return field; - } - } + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + lines.Add(FormatLine(values, delimiter)); + } - /// - /// CSV 读取器 - /// - public class CsvReader - { - private readonly char _delimiter; - private readonly char _quote; + File.WriteAllLines(filePath, lines, encoding); + } /// - /// 创建 CSV 读取器 + /// 异步写入对象列表到CSV文件 /// - public CsvReader(char delimiter = ',', char quote = '"') + public static async Task WriteAsync(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') { - _delimiter = delimiter; - _quote = quote; + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); + + var lines = new List(); + + // 标题行 + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + lines.Add(FormatLine(propertyNames, delimiter)); + } + + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + lines.Add(FormatLine(values, delimiter)); + } + + await File.WriteAllLinesAsync(filePath, lines, encoding); } /// - /// 读取文件 + /// 追加数据到CSV文件 /// - public List ReadFile(string filePath, bool hasHeader = true) + public static void Append(string filePath, IEnumerable data, Encoding? encoding = null, char delimiter = ',') { - var content = File.ReadAllText(filePath, DetectEncoding(filePath)); - return Parse(content, hasHeader); + encoding ??= Encoding.UTF8; + + var lines = new List(); + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + File.AppendAllLines(filePath, lines, encoding); } /// - /// 读取文件(带表头) + /// 异步追加数据到CSV文件 /// - public List> ReadFileWithHeader(string filePath) + public static async Task AppendAsync(string filePath, IEnumerable data, Encoding? encoding = null, char delimiter = ',') { - var rows = ReadFile(filePath, true); - if (rows.Count == 0) - return new List>(); - - var headers = rows[0]; - var result = new List>(); + encoding ??= Encoding.UTF8; - for (int i = 1; i < rows.Count; i++) + var lines = new List(); + foreach (var row in data) { - var dict = new Dictionary(); - for (int j = 0; j < headers.Length && j < rows[i].Length; j++) - { - dict[headers[j]] = rows[i][j]; - } - result.Add(dict); + lines.Add(FormatLine(row, delimiter)); } - return result; + await File.AppendAllLinesAsync(filePath, lines, encoding); } + #endregion + + #region 解析与格式化 + /// - /// 解析 CSV 内容 + /// 解析CSV行 /// - public List Parse(string content, bool hasHeader = true) + private static string[] ParseLine(string line, char delimiter) { - var rows = new List(); - var fields = new List(); - var currentField = new StringBuilder(); + var result = new List(); + var current = new StringBuilder(); bool inQuotes = false; - int startRow = hasHeader ? 0 : 0; - int rowIndex = 0; - for (int i = 0; i < content.Length; i++) + for (int i = 0; i < line.Length; i++) { - char c = content[i]; + var c = line[i]; - if (inQuotes) + if (c == '"') { - if (c == _quote) + if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') { - // 检查是否是转义的引号 - if (i + 1 < content.Length && content[i + 1] == _quote) - { - currentField.Append(_quote); - i++; - } - else - { - inQuotes = false; - } + current.Append('"'); + i++; } else { - currentField.Append(c); + inQuotes = !inQuotes; } } - else + else if (c == delimiter && !inQuotes) { - if (c == _quote) - { - inQuotes = true; - } - else if (c == _delimiter) - { - fields.Add(currentField.ToString()); - currentField.Clear(); - } - else if (c == '\n' || c == '\r') - { - fields.Add(currentField.ToString()); - currentField.Clear(); - - if (rowIndex >= startRow) - { - rows.Add(fields.ToArray()); - } - fields.Clear(); - rowIndex++; - - // 处理 \r\n - if (c == '\r' && i + 1 < content.Length && content[i + 1] == '\n') - i++; - } - else - { - currentField.Append(c); - } + result.Add(current.ToString()); + current.Clear(); } - } - - // 添加最后一个字段和行 - if (currentField.Length > 0 || fields.Count > 0) - { - fields.Add(currentField.ToString()); - if (rowIndex >= startRow) + else { - rows.Add(fields.ToArray()); + current.Append(c); } } - return rows; + result.Add(current.ToString()); + return result.ToArray(); } - private static Encoding DetectEncoding(string filePath) + /// + /// 格式化CSV行 + /// + private static string FormatLine(string[] values, char delimiter) { - // 简单的编码检测 - byte[] bom = new byte[4]; - using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + return string.Join(delimiter, values.Select(v => EscapeValue(v))); + } + + /// + /// 转义CSV值 + /// + private static string EscapeValue(string? value) + { + if (string.IsNullOrEmpty(value)) + return ""; + + if (value.Contains('"') || value.Contains(',') || value.Contains('\n') || value.Contains('\r')) { - int bytesRead = 0; - int bytesToRead = 4; - while (bytesRead < bytesToRead) - { - int read = file.Read(bom, bytesRead, bytesToRead - bytesRead); - if (read == 0) break; // 文件结束 - bytesRead += read; - } + return $"\"{value.Replace("\"", "\"\"")}\""; } - if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) - return Encoding.UTF8; - if (bom[0] == 0xff && bom[1] == 0xfe) - return Encoding.Unicode; - if (bom[0] == 0xfe && bom[1] == 0xff) - return Encoding.BigEndianUnicode; - - return Encoding.UTF8; + return value; } - } - - /// - /// CSV 写入器 - /// - public class CsvWriter - { - private readonly char _delimiter; - private readonly char _quote; /// - /// 创建 CSV 写入器 + /// 格式化值 /// - public CsvWriter(char delimiter = ',', char quote = '"') + private static string FormatValue(object? value) { - _delimiter = delimiter; - _quote = quote; + if (value == null) + return ""; + + return value switch + { + DateTime dt => dt.ToString("yyyy-MM-dd HH:mm:ss"), + decimal dec => dec.ToString(CultureInfo.InvariantCulture), + double d => d.ToString(CultureInfo.InvariantCulture), + float f => f.ToString(CultureInfo.InvariantCulture), + bool b => b.ToString().ToLower(), + _ => value.ToString() ?? "" + }; } /// - /// 写入文件 + /// 转换值类型 /// - public void WriteFile(string filePath, IEnumerable rows) + private static object? ConvertValue(string value, Type targetType) { - var content = ToString(rows); - File.WriteAllText(filePath, content, Encoding.UTF8); + if (string.IsNullOrWhiteSpace(value)) + { + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + } + + try + { + if (targetType == typeof(string)) + return value; + + if (targetType == typeof(int) || targetType == typeof(int?)) + return int.Parse(value); + + if (targetType == typeof(long) || targetType == typeof(long?)) + return long.Parse(value); + + if (targetType == typeof(double) || targetType == typeof(double?)) + return double.Parse(value, CultureInfo.InvariantCulture); + + if (targetType == typeof(decimal) || targetType == typeof(decimal?)) + return decimal.Parse(value, CultureInfo.InvariantCulture); + + if (targetType == typeof(bool) || targetType == typeof(bool?)) + return bool.Parse(value); + + if (targetType == typeof(DateTime) || targetType == typeof(DateTime?)) + return DateTime.TryParse(value, out var dt) ? dt : DateTime.MinValue; + + if (targetType == typeof(Guid) || targetType == typeof(Guid?)) + return Guid.Parse(value); + + return Convert.ChangeType(value, targetType); + } + catch + { + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + } } + #endregion + + #region 辅助方法 + /// - /// 写入文件(带表头) + /// 从字符串读取CSV数据 /// - public void WriteFileWithHeader(string filePath, string[] headers, IEnumerable> rows) + public static List Parse(string csvContent, bool hasHeader = false, char delimiter = ',') { - var allRows = new List { headers }; + var lines = csvContent.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + var result = new List(); - foreach (var row in rows) + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) { - var fields = new string[headers.Length]; - for (int i = 0; i < headers.Length; i++) - { - fields[i] = row.TryGetValue(headers[i], out var value) ? value : ""; - } - allRows.Add(fields); + var row = ParseLine(lines[i], delimiter); + result.Add(row); } - WriteFile(filePath, allRows); + return result; } /// - /// 转换为 CSV 字符串 + /// 将数据转换为CSV字符串 /// - public string ToString(IEnumerable rows) + public static string ToCsvString(IEnumerable data, string[]? headers = null, char delimiter = ',') { var sb = new StringBuilder(); - foreach (var row in rows) + if (headers != null && headers.Length > 0) { - for (int i = 0; i < row.Length; i++) - { - if (i > 0) sb.Append(_delimiter); - sb.Append(CsvUtil.EscapeField(row[i], _delimiter, _quote)); - } - sb.AppendLine(); + sb.AppendLine(FormatLine(headers, delimiter)); + } + + foreach (var row in data) + { + sb.AppendLine(FormatLine(row, delimiter)); } return sb.ToString(); } - } - /// - /// CSV 配置 - /// - public class CsvConfiguration - { /// - /// 分隔符 + /// 将对象列表转换为CSV字符串 /// - public char Delimiter { get; set; } = ','; + public static string ToCsvString(IEnumerable data, string[]? headers = null, char delimiter = ',') + { + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); - /// - /// 引号字符 - /// - public char Quote { get; set; } = '"'; + var sb = new StringBuilder(); - /// - /// 是否有表头 - /// - public bool HasHeader { get; set; } = true; + // 标题行 + if (headers != null && headers.Length > 0) + { + sb.AppendLine(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + sb.AppendLine(FormatLine(propertyNames, delimiter)); + } - /// - /// 编码 - /// - public Encoding Encoding { get; set; } = Encoding.UTF8; + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + sb.AppendLine(FormatLine(values, delimiter)); + } - /// - /// 换行符 - /// - public string NewLine { get; set; } = Environment.NewLine; + return sb.ToString(); + } /// - /// 默认配置 + /// 获取CSV文件的列数 /// - public static CsvConfiguration Default => new CsvConfiguration(); + public static int GetColumnCount(string filePath, Encoding? encoding = null, char delimiter = ',') + { + if (!File.Exists(filePath)) + return 0; + + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(filePath, encoding); + var firstLine = reader.ReadLine(); + + if (string.IsNullOrEmpty(firstLine)) + return 0; + + return ParseLine(firstLine, delimiter).Length; + } /// - /// 中文配置(使用制表符分隔) + /// 获取CSV文件的行数 /// - public static CsvConfiguration Chinese => new CsvConfiguration { Delimiter = '\t' }; + public static int GetRowCount(string filePath, bool hasHeader = true, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + return 0; + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + return hasHeader ? Math.Max(0, lines.Length - 1) : lines.Length; + } + + #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/ExcelUtil.cs b/EasyTool.Core/IOCategory/ExcelUtil.cs new file mode 100644 index 0000000..3e51a6a --- /dev/null +++ b/EasyTool.Core/IOCategory/ExcelUtil.cs @@ -0,0 +1,400 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Security; +using System.Text; +using System.Xml; +using System.Xml.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// Excel工具类(轻量级实现,不依赖第三方库) + /// 支持读取和写入xlsx格式文件 + /// + public static class ExcelUtil + { + private const string NS_SS = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; + private const string NS_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; + private static readonly string[] ColumnNames = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG,BH,BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ".Split(','); + + #region 读取Excel + + /// + /// 读取Excel文件为DataTable + /// + public static DataTable Read(string filePath, int sheetIndex = 0, bool hasHeader = true) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + return Read(stream, sheetIndex, hasHeader); + } + + /// + /// 从流读取Excel为DataTable + /// + public static DataTable Read(Stream stream, int sheetIndex = 0, bool hasHeader = true) + { + using var archive = new ZipArchive(stream, ZipArchiveMode.Read); + var sharedStrings = LoadSharedStrings(archive); + var sheetEntry = GetSheetEntry(archive, sheetIndex); + + if (sheetEntry == null) + throw new ArgumentException($"工作表索引 {sheetIndex} 不存在"); + + return ParseWorksheet(sheetEntry, sharedStrings, hasHeader); + } + + /// + /// 获取所有工作表名称 + /// + public static List GetSheetNames(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + return GetSheetNames(stream); + } + + /// + /// 从流获取所有工作表名称 + /// + public static List GetSheetNames(Stream stream) + { + var names = new List(); + using var archive = new ZipArchive(stream, ZipArchiveMode.Read); + + var workbookEntry = archive.GetEntry("xl/workbook.xml"); + if (workbookEntry == null) return names; + + using var reader = new StreamReader(workbookEntry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var sheets = doc.Root?.Element(ns + "sheets")?.Elements(ns + "sheet"); + if (sheets != null) + { + foreach (var sheet in sheets) + { + var name = sheet.Attribute("name")?.Value; + if (!string.IsNullOrEmpty(name)) + names.Add(name); + } + } + + return names; + } + + private static Dictionary LoadSharedStrings(ZipArchive archive) + { + var strings = new Dictionary(); + var entry = archive.GetEntry("xl/sharedStrings.xml"); + if (entry == null) return strings; + + using var reader = new StreamReader(entry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var siElements = doc.Root?.Elements(ns + "si"); + if (siElements == null) return strings; + + int index = 0; + foreach (var si in siElements) + { + var text = si.Element(ns + "t")?.Value ?? ""; + strings[index++] = text; + } + + return strings; + } + + private static ZipArchiveEntry? GetSheetEntry(ZipArchive archive, int sheetIndex) + { + var entries = new List(); + foreach (var entry in archive.Entries) + { + if (entry.FullName.StartsWith("xl/worksheets/sheet") && entry.FullName.EndsWith(".xml")) + entries.Add(entry); + } + + entries.Sort((a, b) => string.Compare(a.FullName, b.FullName, StringComparison.Ordinal)); + return sheetIndex < entries.Count ? entries[sheetIndex] : null; + } + + private static DataTable ParseWorksheet(ZipArchiveEntry entry, Dictionary sharedStrings, bool hasHeader) + { + var table = new DataTable(); + + using var reader = new StreamReader(entry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var sheetData = doc.Root?.Element(ns + "sheetData"); + if (sheetData == null) return table; + + var rows = sheetData.Elements(ns + "row").ToList(); + if (rows.Count == 0) return table; + + // 解析所有行数据 + var allData = new List>(); + int maxCols = 0; + + foreach (var row in rows) + { + var rowData = new List(); + var cells = row.Elements(ns + "c"); + + foreach (var cell in cells) + { + var refAttr = cell.Attribute("r")?.Value ?? ""; + var type = cell.Attribute("t")?.Value; + var value = cell.Element(ns + "v")?.Value ?? ""; + + if (type == "s" && int.TryParse(value, out int sharedIndex)) + { + value = sharedStrings.TryGetValue(sharedIndex, out var s) ? s : ""; + } + + rowData.Add(value); + } + + if (rowData.Count > maxCols) + maxCols = rowData.Count; + + allData.Add(rowData); + } + + // 创建列 + if (hasHeader && allData.Count > 0) + { + var headers = allData[0]; + for (int i = 0; i < maxCols; i++) + { + var colName = i < headers.Count && !string.IsNullOrEmpty(headers[i]) + ? headers[i] + : $"Column{i + 1}"; + table.Columns.Add(colName, typeof(string)); + } + allData.RemoveAt(0); + } + else + { + for (int i = 0; i < maxCols; i++) + table.Columns.Add($"Column{i + 1}", typeof(string)); + } + + // 添加数据行 + foreach (var rowData in allData) + { + var row = table.NewRow(); + for (int i = 0; i < Math.Min(rowData.Count, maxCols); i++) + { + row[i] = rowData[i]; + } + table.Rows.Add(row); + } + + return table; + } + + #endregion + + #region 写入Excel + + /// + /// 将DataTable写入Excel文件 + /// + public static void Write(string filePath, DataTable dataTable, string sheetName = "Sheet1") + { + using var stream = File.Create(filePath); + Write(stream, dataTable, sheetName); + } + + /// + /// 将DataTable写入Excel流 + /// + public static void Write(Stream stream, DataTable dataTable, string sheetName = "Sheet1") + { + using var archive = new ZipArchive(stream, ZipArchiveMode.Create); + + // 创建必要的文件结构 + CreateContentType(archive); + CreateRels(archive); + CreateWorkbook(archive, sheetName); + CreateWorkbookRels(archive); + CreateWorksheet(archive, dataTable); + CreateStyles(archive); + } + + private static void CreateContentType(ZipArchive archive) + { + var entry = archive.CreateEntry("[Content_Types].xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + + + + +"); + } + + private static void CreateRels(ZipArchive archive) + { + var entry = archive.CreateEntry("_rels/.rels"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + +"); + } + + private static void CreateWorkbook(ZipArchive archive, string sheetName) + { + var entry = archive.CreateEntry("xl/workbook.xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write($@" + + + + +"); + } + + private static void CreateWorkbookRels(ZipArchive archive) + { + var entry = archive.CreateEntry("xl/_rels/workbook.xml.rels"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + +"); + } + + private static void CreateWorksheet(ZipArchive archive, DataTable dataTable) + { + var entry = archive.CreateEntry("xl/worksheets/sheet1.xml"); + using var writer = new StreamWriter(entry.Open()); + + writer.Write($@" + +"); + + for (int r = 0; r < dataTable.Rows.Count; r++) + { + writer.Write($""); + + for (int c = 0; c < dataTable.Columns.Count; c++) + { + var cellRef = GetColumnName(c) + (r + 1); + var value = dataTable.Rows[r][c]?.ToString() ?? ""; + + // 尝试解析为数字 + if (double.TryParse(value, out double numValue)) + { + writer.Write($"{numValue}"); + } + else + { + writer.Write($"{SecurityElement.Escape(value)}"); + } + } + + writer.Write(""); + } + + writer.Write(""); + } + + private static void CreateStyles(ZipArchive archive) + { + var entry = archive.CreateEntry("xl/styles.xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + + + + +"); + } + + private static string GetColumnName(int index) + { + if (index < ColumnNames.Length) + return ColumnNames[index]; + + var name = new StringBuilder(); + index++; + while (index > 0) + { + index--; + name.Insert(0, (char)('A' + index % 26)); + index /= 26; + } + return name.ToString(); + } + + #endregion + + #region 辅助方法 + + /// + /// 将List转换为DataTable + /// + public static DataTable ToDataTable(IEnumerable list) + { + var table = new DataTable(); + var properties = typeof(T).GetProperties(); + + foreach (var prop in properties) + table.Columns.Add(prop.Name, typeof(object)); + + foreach (var item in list) + { + var row = table.NewRow(); + foreach (var prop in properties) + row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; + table.Rows.Add(row); + } + + return table; + } + + /// + /// 将DataTable转换为List + /// + public static List ToList(DataTable table) where T : new() + { + var list = new List(); + var properties = typeof(T).GetProperties(); + + foreach (DataRow row in table.Rows) + { + var item = new T(); + foreach (var prop in properties) + { + if (table.Columns.Contains(prop.Name) && row[prop.Name] != DBNull.Value) + { + var value = Convert.ChangeType(row[prop.Name], prop.PropertyType); + prop.SetValue(item, value); + } + } + list.Add(item); + } + + return list; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/FileChunkUtil.cs b/EasyTool.Core/IOCategory/FileChunkUtil.cs new file mode 100644 index 0000000..4f10c97 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileChunkUtil.cs @@ -0,0 +1,395 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件分片工具类 + /// 支持大文件分片上传、下载和合并 + /// + public static class FileChunkUtil + { + /// + /// 分片文件 + /// + /// 源文件路径 + /// 输出目录 + /// 分片大小(字节) + /// 进度回调 + /// 分片信息 + public static ChunkInfo Split(string filePath, string outputDir, long chunkSize = 5 * 1024 * 1024, Action? progress = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + Directory.CreateDirectory(outputDir); + + var fileInfo = new FileInfo(filePath); + var fileName = Path.GetFileNameWithoutExtension(filePath); + var extension = Path.GetExtension(filePath); + var totalChunks = (int)Math.Ceiling((double)fileInfo.Length / chunkSize); + var fileId = Guid.NewGuid().ToString("N"); + + var chunkInfo = new ChunkInfo + { + FileId = fileId, + FileName = fileInfo.Name, + FileSize = fileInfo.Length, + ChunkSize = chunkSize, + TotalChunks = totalChunks, + FileHash = ComputeFileHash(filePath), + Chunks = new List() + }; + + using var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + var buffer = new byte[Math.Min(chunkSize, int.MaxValue)]; + + for (int i = 0; i < totalChunks; i++) + { + var chunkFileName = $"{fileName}_{i + 1:D5}{extension}.chunk"; + var chunkFilePath = Path.Combine(outputDir, chunkFileName); + + var bytesRead = sourceStream.Read(buffer, 0, buffer.Length); + + using var chunkStream = new FileStream(chunkFilePath, FileMode.Create, FileAccess.Write); + chunkStream.Write(buffer, 0, bytesRead); + + var chunkHash = ComputeHash(buffer, 0, bytesRead); + + chunkInfo.Chunks.Add(new ChunkDetail + { + Index = i + 1, + ChunkFile = chunkFileName, + Size = bytesRead, + Hash = chunkHash + }); + + progress?.Invoke((double)(i + 1) / totalChunks * 100); + } + + // 保存分片信息文件 + var infoPath = Path.Combine(outputDir, $"{fileName}.chunkinfo"); + SaveChunkInfo(chunkInfo, infoPath); + + return chunkInfo; + } + + /// + /// 异步分片文件 + /// + public static async Task SplitAsync(string filePath, string outputDir, long chunkSize = 5 * 1024 * 1024, Action? progress = null) + { + return await Task.Run(() => Split(filePath, outputDir, chunkSize, progress)); + } + + /// + /// 合并分片文件 + /// + /// 分片信息 + /// 分片文件目录 + /// 输出文件路径 + /// 进度回调 + public static void Merge(ChunkInfo chunkInfo, string chunkDir, string outputPath, Action? progress = null) + { + var dir = new DirectoryInfo(chunkDir); + + using var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write); + + foreach (var chunk in chunkInfo.Chunks.OrderBy(c => c.Index)) + { + var chunkFilePath = Path.Combine(chunkDir, chunk.ChunkFile); + + if (!File.Exists(chunkFilePath)) + throw new FileNotFoundException($"分片文件不存在: {chunk.ChunkFile}"); + + using var chunkStream = new FileStream(chunkFilePath, FileMode.Open, FileAccess.Read); + var buffer = new byte[chunk.Size]; + chunkStream.Read(buffer, 0, buffer.Length); + + // 验证分片哈希 + var hash = ComputeHash(buffer, 0, buffer.Length); + if (hash != chunk.Hash) + throw new InvalidDataException($"分片 {chunk.Index} 哈希验证失败"); + + outputStream.Write(buffer, 0, buffer.Length); + + progress?.Invoke((double)chunk.Index / chunkInfo.TotalChunks * 100); + } + + // 验证最终文件哈希 + var finalHash = ComputeFileHash(outputPath); + if (finalHash != chunkInfo.FileHash) + throw new InvalidDataException("合并后文件哈希验证失败"); + } + + /// + /// 异步合并分片文件 + /// + public static async Task MergeAsync(ChunkInfo chunkInfo, string chunkDir, string outputPath, Action? progress = null) + { + await Task.Run(() => Merge(chunkInfo, chunkDir, outputPath, progress)); + } + + /// + /// 从信息文件加载分片信息 + /// + /// 信息文件路径 + /// 分片信息 + public static ChunkInfo LoadChunkInfo(string infoFilePath) + { + var json = File.ReadAllText(infoFilePath); + return System.Text.Json.JsonSerializer.Deserialize(json) + ?? throw new InvalidDataException("无效的分片信息文件"); + } + + /// + /// 保存分片信息到文件 + /// + private static void SaveChunkInfo(ChunkInfo chunkInfo, string infoFilePath) + { + var json = System.Text.Json.JsonSerializer.Serialize(chunkInfo, new System.Text.Json.JsonSerializerOptions + { + WriteIndented = true + }); + File.WriteAllText(infoFilePath, json); + } + + /// + /// 验证分片完整性 + /// + /// 分片信息 + /// 分片目录 + /// 验证结果 + public static ChunkValidationResult Validate(ChunkInfo chunkInfo, string chunkDir) + { + var result = new ChunkValidationResult + { + IsValid = true, + MissingChunks = new List(), + CorruptedChunks = new List() + }; + + foreach (var chunk in chunkInfo.Chunks) + { + var chunkFilePath = Path.Combine(chunkDir, chunk.ChunkFile); + + if (!File.Exists(chunkFilePath)) + { + result.IsValid = false; + result.MissingChunks.Add(chunk.Index); + continue; + } + + var fileInfo = new FileInfo(chunkFilePath); + if (fileInfo.Length != chunk.Size) + { + result.IsValid = false; + result.CorruptedChunks.Add(chunk.Index); + continue; + } + + var hash = ComputeFileHash(chunkFilePath); + if (hash != chunk.Hash) + { + result.IsValid = false; + result.CorruptedChunks.Add(chunk.Index); + } + } + + return result; + } + + /// + /// 获取上传进度 + /// + /// 分片信息 + /// 已上传的分片索引 + /// 上传进度(百分比) + public static double GetUploadProgress(ChunkInfo chunkInfo, HashSet uploadedChunks) + { + if (chunkInfo.TotalChunks == 0) + return 0; + + return (double)uploadedChunks.Count / chunkInfo.TotalChunks * 100; + } + + /// + /// 计算文件哈希 + /// + private static string ComputeFileHash(string filePath) + { + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 计算数据哈希 + /// + private static string ComputeHash(byte[] buffer, int offset, int count) + { + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(buffer, offset, count); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + #region 流式分片 + + /// + /// 流式读取分片 + /// + /// 输入流 + /// 分片大小 + /// 分片数据枚举 + public static IEnumerable ReadChunks(Stream stream, long chunkSize = 5 * 1024 * 1024) + { + var buffer = new byte[Math.Min(chunkSize, int.MaxValue)]; + int index = 1; + + while (true) + { + var bytesRead = stream.Read(buffer, 0, buffer.Length); + if (bytesRead == 0) + break; + + var chunk = new byte[bytesRead]; + Buffer.BlockCopy(buffer, 0, chunk, 0, bytesRead); + + yield return new ChunkData + { + Index = index++, + Data = chunk + }; + } + } + + /// + /// 流式写入分片 + /// + /// 输出流 + /// 分片数据 + public static void WriteChunks(Stream stream, IEnumerable chunks) + { + foreach (var chunk in chunks.OrderBy(c => c.Index)) + { + stream.Write(chunk.Data, 0, chunk.Data.Length); + } + } + + #endregion + } + + /// + /// 分片信息 + /// + public class ChunkInfo + { + /// + /// 文件唯一标识 + /// + public string FileId { get; set; } = string.Empty; + + /// + /// 原始文件名 + /// + public string FileName { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long FileSize { get; set; } + + /// + /// 分片大小 + /// + public long ChunkSize { get; set; } + + /// + /// 总分片数 + /// + public int TotalChunks { get; set; } + + /// + /// 文件哈希 + /// + public string FileHash { get; set; } = string.Empty; + + /// + /// 分片详情列表 + /// + public List Chunks { get; set; } = new(); + } + + /// + /// 分片详情 + /// + public class ChunkDetail + { + /// + /// 分片索引(从1开始) + /// + public int Index { get; set; } + + /// + /// 分片文件名 + /// + public string ChunkFile { get; set; } = string.Empty; + + /// + /// 分片大小 + /// + public int Size { get; set; } + + /// + /// 分片哈希 + /// + public string Hash { get; set; } = string.Empty; + } + + /// + /// 分片数据 + /// + public class ChunkData + { + /// + /// 分片索引 + /// + public int Index { get; set; } + + /// + /// 分片数据 + /// + public byte[] Data { get; set; } = Array.Empty(); + + /// + /// 大小 + /// + public int Size => Data.Length; + } + + /// + /// 分片验证结果 + /// + public class ChunkValidationResult + { + /// + /// 是否完整有效 + /// + public bool IsValid { get; set; } + + /// + /// 缺失的分片索引 + /// + public List MissingChunks { get; set; } = new(); + + /// + /// 损坏的分片索引 + /// + public List CorruptedChunks { get; set; } = new(); + } +} diff --git a/EasyTool.Core/IOCategory/FileCompareUtil.cs b/EasyTool.Core/IOCategory/FileCompareUtil.cs new file mode 100644 index 0000000..8bf698d --- /dev/null +++ b/EasyTool.Core/IOCategory/FileCompareUtil.cs @@ -0,0 +1,258 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; + +namespace EasyTool.IOCategory +{ + /// + /// 文件比较工具类 + /// + public static class FileCompareUtil + { + /// + /// 比较两个文件内容是否相同 + /// + public static bool AreContentsEqual(string filePath1, string filePath2) + { + if (!File.Exists(filePath1) || !File.Exists(filePath2)) + return false; + + var fileInfo1 = new FileInfo(filePath1); + var fileInfo2 = new FileInfo(filePath2); + + // 大小不同,内容肯定不同 + if (fileInfo1.Length != fileInfo2.Length) + return false; + + // 逐字节比较 + using var stream1 = File.OpenRead(filePath1); + using var stream2 = File.OpenRead(filePath2); + + var buffer1 = new byte[4096]; + var buffer2 = new byte[4096]; + + while (true) + { + var count1 = stream1.Read(buffer1, 0, buffer1.Length); + var count2 = stream2.Read(buffer2, 0, buffer2.Length); + + if (count1 != count2) + return false; + + if (count1 == 0) + return true; + + for (int i = 0; i < count1; i++) + { + if (buffer1[i] != buffer2[i]) + return false; + } + } + } + + /// + /// 比较两个文件内容是否相同(使用哈希) + /// + public static bool AreContentsEqualByHash(string filePath1, string filePath2) + { + if (!File.Exists(filePath1) || !File.Exists(filePath2)) + return false; + + var hash1 = ComputeFileHash(filePath1); + var hash2 = ComputeFileHash(filePath2); + + return hash1 == hash2; + } + + /// + /// 计算文件哈希值 + /// + public static string ComputeFileHash(string filePath, string algorithm = "MD5") + { + using var stream = File.OpenRead(filePath); + using HashAlgorithm hasher = algorithm.ToUpper() switch + { + "MD5" => MD5.Create(), + "SHA1" => SHA1.Create(), + "SHA256" => SHA256.Create(), + "SHA512" => SHA512.Create(), + _ => MD5.Create() + }; + + var hash = hasher.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 比较两个目录 + /// + public static DirectoryCompareResult CompareDirectories(string directory1, string directory2, string searchPattern = "*") + { + var result = new DirectoryCompareResult(); + + var files1 = Directory.GetFiles(directory1, searchPattern, SearchOption.AllDirectories); + var files2 = Directory.GetFiles(directory2, searchPattern, SearchOption.AllDirectories); + + var relativePath1 = new Dictionary(); + var relativePath2 = new Dictionary(); + + foreach (var file in files1) + { + var relative = file.Substring(directory1.Length).TrimStart(Path.DirectorySeparatorChar); + relativePath1[relative] = file; + } + + foreach (var file in files2) + { + var relative = file.Substring(directory2.Length).TrimStart(Path.DirectorySeparatorChar); + relativePath2[relative] = file; + } + + // 只在目录1中的文件 + foreach (var kvp in relativePath1) + { + if (!relativePath2.ContainsKey(kvp.Key)) + { + result.OnlyInDirectory1.Add(kvp.Value); + } + } + + // 只在目录2中的文件 + foreach (var kvp in relativePath2) + { + if (!relativePath1.ContainsKey(kvp.Key)) + { + result.OnlyInDirectory2.Add(kvp.Value); + } + } + + // 两边都有的文件 + foreach (var kvp in relativePath1) + { + if (relativePath2.TryGetValue(kvp.Key, out var file2)) + { + if (AreContentsEqual(kvp.Value, file2)) + { + result.IdenticalFiles.Add(kvp.Value); + } + else + { + result.DifferentFiles.Add(new FileDifference + { + File1 = kvp.Value, + File2 = file2 + }); + } + } + } + + return result; + } + + /// + /// 查找重复文件 + /// + public static List> FindDuplicateFiles(string directory, string searchPattern = "*") + { + var files = Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories); + var hashGroups = new Dictionary>(); + + foreach (var file in files) + { + try + { + var hash = ComputeFileHash(file); + if (!hashGroups.ContainsKey(hash)) + hashGroups[hash] = new List(); + hashGroups[hash].Add(file); + } + catch + { + // 忽略无法读取的文件 + } + } + + return hashGroups.Values.Where(g => g.Count > 1).ToList(); + } + + /// + /// 查找相似文件(大小相同) + /// + public static List> FindSimilarSizedFiles(string directory, string searchPattern = "*") + { + var files = Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories); + var sizeGroups = new Dictionary>(); + + foreach (var file in files) + { + try + { + var size = new FileInfo(file).Length; + if (!sizeGroups.ContainsKey(size)) + sizeGroups[size] = new List(); + sizeGroups[size].Add(file); + } + catch + { + // 忽略无法读取的文件 + } + } + + return sizeGroups.Values.Where(g => g.Count > 1).ToList(); + } + } + + /// + /// 目录比较结果 + /// + public class DirectoryCompareResult + { + /// + /// 只在目录1中的文件 + /// + public List OnlyInDirectory1 { get; } = new(); + + /// + /// 只在目录2中的文件 + /// + public List OnlyInDirectory2 { get; } = new(); + + /// + /// 相同的文件 + /// + public List IdenticalFiles { get; } = new(); + + /// + /// 不同的文件 + /// + public List DifferentFiles { get; } = new(); + + /// + /// 是否完全相同 + /// + public bool AreIdentical => OnlyInDirectory1.Count == 0 && OnlyInDirectory2.Count == 0 && DifferentFiles.Count == 0; + } + + /// + /// 文件差异 + /// + public class FileDifference + { + /// + /// 文件1路径 + /// + public string File1 { get; set; } = string.Empty; + + /// + /// 文件2路径 + /// + public string File2 { get; set; } = string.Empty; + + public override string ToString() + { + return $"{File1} <-> {File2}"; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/FileLockUtil.cs b/EasyTool.Core/IOCategory/FileLockUtil.cs new file mode 100644 index 0000000..0912081 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileLockUtil.cs @@ -0,0 +1,331 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件锁选项 + /// + public class FileLockOptions + { + /// + /// 锁超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 重试间隔 + /// + public TimeSpan RetryInterval { get; set; } = TimeSpan.FromMilliseconds(100); + + /// + /// 锁文件目录 + /// + public string LockDirectory { get; set; } = Path.GetTempPath(); + } + + /// + /// 文件锁工具类 + /// 提供跨进程的文件锁定机制 + /// + public static class FileLockUtil + { + private static readonly FileLockOptions _defaultOptions = new(); + + /// + /// 获取文件锁 + /// + /// 要锁定的文件路径 + /// 锁选项 + /// 文件锁 + public static FileLock Acquire(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + + var lockFilePath = GetLockFilePath(filePath, options); + var startTime = DateTime.UtcNow; + + while (true) + { + try + { + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + // 写入锁信息 + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + fileStream.Write(bytes, 0, bytes.Length); + fileStream.Flush(); + + return new FileLock(lockFilePath, fileStream); + } + catch (IOException) + { + // 检查是否超时 + if (DateTime.UtcNow - startTime >= options.Timeout) + { + throw new TimeoutException($"获取文件锁超时: {filePath}"); + } + + Thread.Sleep(options.RetryInterval); + } + } + } + + /// + /// 异步获取文件锁 + /// + /// 要锁定的文件路径 + /// 锁选项 + /// 取消令牌 + /// 文件锁 + public static async Task AcquireAsync(string filePath, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + options ??= _defaultOptions; + + var lockFilePath = GetLockFilePath(filePath, options); + var startTime = DateTime.UtcNow; + + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + await fileStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + await fileStream.FlushAsync(cancellationToken); + + return new FileLock(lockFilePath, fileStream); + } + catch (IOException) + { + if (DateTime.UtcNow - startTime >= options.Timeout) + { + throw new TimeoutException($"获取文件锁超时: {filePath}"); + } + + await Task.Delay(options.RetryInterval, cancellationToken); + } + } + } + + /// + /// 尝试获取文件锁 + /// + /// 要锁定的文件路径 + /// 文件锁 + /// 锁选项 + /// 是否成功获取 + public static bool TryAcquire(string filePath, out FileLock? fileLock, FileLockOptions? options = null) + { + options ??= _defaultOptions; + + try + { + var lockFilePath = GetLockFilePath(filePath, options); + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + fileStream.Write(bytes, 0, bytes.Length); + fileStream.Flush(); + + fileLock = new FileLock(lockFilePath, fileStream); + return true; + } + catch + { + fileLock = null; + return false; + } + } + + /// + /// 检查文件是否被锁定 + /// + /// 文件路径 + /// 锁选项 + /// 是否被锁定 + public static bool IsLocked(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + var lockFilePath = GetLockFilePath(filePath, options); + + if (!File.Exists(lockFilePath)) + return false; + + // 尝试打开锁文件 + try + { + using var stream = new FileStream( + lockFilePath, + FileMode.Open, + FileAccess.Read, + FileShare.None); + + return false; + } + catch + { + return true; + } + } + + /// + /// 强制释放文件锁 + /// + /// 文件路径 + /// 锁选项 + /// 是否成功释放 + public static bool ForceRelease(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + var lockFilePath = GetLockFilePath(filePath, options); + + try + { + if (File.Exists(lockFilePath)) + { + File.Delete(lockFilePath); + } + return true; + } + catch + { + return false; + } + } + + /// + /// 使用文件锁执行操作 + /// + /// 文件路径 + /// 操作 + /// 锁选项 + public static void WithLock(string filePath, Action action, FileLockOptions? options = null) + { + using var fileLock = Acquire(filePath, options); + action(); + } + + /// + /// 使用文件锁执行操作并返回结果 + /// + /// 返回类型 + /// 文件路径 + /// 操作 + /// 锁选项 + /// 操作结果 + public static T WithLock(string filePath, Func func, FileLockOptions? options = null) + { + using var fileLock = Acquire(filePath, options); + return func(); + } + + /// + /// 异步使用文件锁执行操作 + /// + /// 文件路径 + /// 操作 + /// 锁选项 + /// 取消令牌 + public static async Task WithLockAsync(string filePath, Func action, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + using var fileLock = await AcquireAsync(filePath, options, cancellationToken); + await action(); + } + + /// + /// 异步使用文件锁执行操作并返回结果 + /// + /// 返回类型 + /// 文件路径 + /// 操作 + /// 锁选项 + /// 取消令牌 + /// 操作结果 + public static async Task WithLockAsync(string filePath, Func> func, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + using var fileLock = await AcquireAsync(filePath, options, cancellationToken); + return await func(); + } + + private static string GetLockFilePath(string filePath, FileLockOptions options) + { + var fileName = Path.GetFileName(filePath); + var directory = Path.GetDirectoryName(Path.GetFullPath(filePath)); + + // 使用文件路径的哈希作为锁文件名的一部分 + var hash = Math.Abs(directory?.GetHashCode() ?? 0); + var lockFileName = $"{fileName}.{hash}.lock"; + + return Path.Combine(options.LockDirectory, lockFileName); + } + } + + /// + /// 文件锁 + /// + public class FileLock : IDisposable + { + private readonly string _lockFilePath; + private readonly FileStream _fileStream; + private bool _disposed; + + internal FileLock(string lockFilePath, FileStream fileStream) + { + _lockFilePath = lockFilePath; + _fileStream = fileStream; + } + + /// + /// 锁文件路径 + /// + public string LockFilePath => _lockFilePath; + + /// + /// 释放锁 + /// + public void Dispose() + { + if (!_disposed) + { + _fileStream?.Dispose(); + _disposed = true; + } + } + + /// + /// 释放锁 + /// + public void Release() + { + Dispose(); + } + } +} diff --git a/EasyTool.Core/IOCategory/FileSearch.cs b/EasyTool.Core/IOCategory/FileSearch.cs new file mode 100644 index 0000000..70fa22f --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSearch.cs @@ -0,0 +1,286 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件搜索工具类 + /// + public static class FileSearch + { + /// + /// 搜索文件 + /// + /// 搜索目录 + /// 搜索模式 + /// 是否搜索子目录 + /// 文件路径列表 + public static List SearchFiles(string directory, string pattern = "*", bool searchSubdirectories = true) + { + var option = searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + + try + { + return Directory.GetFiles(directory, pattern, option).ToList(); + } + catch (UnauthorizedAccessException) + { + return new List(); + } + } + + /// + /// 搜索文件(多个模式) + /// + public static List SearchFiles(string directory, string[] patterns, bool searchSubdirectories = true) + { + var results = new List(); + + foreach (var pattern in patterns) + { + results.AddRange(SearchFiles(directory, pattern, searchSubdirectories)); + } + + return results.Distinct().ToList(); + } + + /// + /// 搜索目录 + /// + public static List SearchDirectories(string directory, string pattern = "*", bool searchSubdirectories = true) + { + var option = searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + + try + { + return Directory.GetDirectories(directory, pattern, option).ToList(); + } + catch (UnauthorizedAccessException) + { + return new List(); + } + } + + /// + /// 按大小搜索文件 + /// + public static List SearchBySize(string directory, long minSize = 0, long maxSize = long.MaxValue, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var results = new List(); + + foreach (var file in files) + { + try + { + var info = new FileInfo(file); + if (info.Length >= minSize && info.Length <= maxSize) + { + results.Add(file); + } + } + catch + { + // 忽略无法访问的文件 + } + } + + return results; + } + + /// + /// 按修改时间搜索文件 + /// + public static List SearchByDate(string directory, DateTime? startTime = null, DateTime? endTime = null, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var results = new List(); + + foreach (var file in files) + { + try + { + var info = new FileInfo(file); + var writeTime = info.LastWriteTime; + + var afterStart = !startTime.HasValue || writeTime >= startTime.Value; + var beforeEnd = !endTime.HasValue || writeTime <= endTime.Value; + + if (afterStart && beforeEnd) + { + results.Add(file); + } + } + catch + { + // 忽略无法访问的文件 + } + } + + return results; + } + + /// + /// 按内容搜索文件 + /// + public static async Task> SearchByContent(string directory, string searchText, bool searchSubdirectories = true, bool ignoreCase = true) + { + var files = SearchFiles(directory, "*.txt", searchSubdirectories); + files.AddRange(SearchFiles(directory, "*.log", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.json", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.xml", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.cs", searchSubdirectories)); + + var results = new List(); + var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + foreach (var file in files.Distinct()) + { + try + { + var content = await File.ReadAllTextAsync(file); + if (content.Contains(searchText, comparison)) + { + results.Add(file); + } + } + catch + { + // 忽略无法读取的文件 + } + } + + return results; + } + + /// + /// 查找重复文件 + /// + public static List> FindDuplicates(string directory, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var sizeGroups = files.GroupBy(f => + { + try + { + return new FileInfo(f).Length; + } + catch + { + return -1L; + } + }).Where(g => g.Key > 0 && g.Count() > 1); + + var duplicates = new List>(); + + foreach (var group in sizeGroups) + { + var sameSizeFiles = group.ToList(); + var hashGroups = sameSizeFiles.GroupBy(f => + { + try + { + using var stream = File.OpenRead(f); + using var md5 = System.Security.Cryptography.MD5.Create(); + var hash = md5.ComputeHash(stream); + return Convert.ToBase64String(hash); + } + catch + { + return string.Empty; + } + }).Where(g => !string.IsNullOrEmpty(g.Key) && g.Count() > 1); + + foreach (var hashGroup in hashGroups) + { + duplicates.Add(hashGroup.ToList()); + } + } + + return duplicates; + } + + /// + /// 查找空目录 + /// + public static List FindEmptyDirectories(string directory) + { + var emptyDirs = new List(); + + try + { + var subDirs = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories); + + foreach (var dir in subDirs) + { + try + { + if (!Directory.EnumerateFileSystemEntries(dir).Any()) + { + emptyDirs.Add(dir); + } + } + catch + { + // 忽略无法访问的目录 + } + } + } + catch + { + // 忽略无法访问的目录 + } + + return emptyDirs; + } + + /// + /// 获取目录大小 + /// + public static long GetDirectorySize(string directory) + { + var files = SearchFiles(directory, "*", true); + long size = 0; + + foreach (var file in files) + { + try + { + size += new FileInfo(file).Length; + } + catch + { + // 忽略无法访问的文件 + } + } + + return size; + } + + /// + /// 获取文件统计信息 + /// + public static Dictionary GetFileStatistics(string directory) + { + var files = SearchFiles(directory, "*", true); + var stats = new Dictionary(StringComparer.OrdinalIgnoreCase); + + foreach (var file in files) + { + var ext = Path.GetExtension(file); + if (string.IsNullOrEmpty(ext)) + ext = "(无扩展名)"; + + if (stats.ContainsKey(ext)) + stats[ext]++; + else + stats[ext] = 1; + } + + return stats; + } + } +} diff --git a/EasyTool.Core/IOCategory/FileWatcher.cs b/EasyTool.Core/IOCategory/FileWatcher.cs new file mode 100644 index 0000000..0cbe5b0 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileWatcher.cs @@ -0,0 +1,386 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 文件监视器 + /// 提供文件变更监视功能 + /// + public class FileWatcher : IDisposable + { + private readonly FileSystemWatcher _watcher; + private readonly Dictionary _lastWriteTimes = new(); + private readonly object _lock = new(); + private int _debounceMilliseconds = 100; + + /// + /// 文件变更事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 错误事件 + /// + public event EventHandler? Error; + + /// + /// 监视路径 + /// + public string Path { get; } + + /// + /// 监视筛选器 + /// + public string Filter + { + get => _watcher.Filter; + set => _watcher.Filter = value; + } + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories + { + get => _watcher.IncludeSubdirectories; + set => _watcher.IncludeSubdirectories = value; + } + + /// + /// 防抖时间(毫秒) + /// + public int DebounceMilliseconds + { + get => _debounceMilliseconds; + set => _debounceMilliseconds = Math.Max(0, value); + } + + /// + /// 是否正在监视 + /// + public bool IsWatching => _watcher.EnableRaisingEvents; + + /// + /// 创建文件监视器 + /// + /// 监视路径 + public FileWatcher(string path) + { + if (!Directory.Exists(path)) + throw new DirectoryNotFoundException($"目录不存在: {path}"); + + Path = path; + _watcher = new FileSystemWatcher(path) + { + NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | + NotifyFilters.LastWrite | NotifyFilters.Size + }; + + _watcher.Changed += OnChanged; + _watcher.Created += OnCreated; + _watcher.Deleted += OnDeleted; + _watcher.Renamed += OnRenamed; + _watcher.Error += OnError; + } + + /// + /// 创建文件监视器 + /// + /// 监视路径 + /// 文件筛选器 + public FileWatcher(string path, string filter) : this(path) + { + Filter = filter; + } + + private void OnChanged(object sender, FileSystemEventArgs e) + { + // 防抖处理 + lock (_lock) + { + var now = DateTime.UtcNow; + if (_lastWriteTimes.TryGetValue(e.FullPath, out var lastWrite)) + { + if ((now - lastWrite).TotalMilliseconds < _debounceMilliseconds) + return; + } + _lastWriteTimes[e.FullPath] = now; + } + + FileChanged?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Changed)); + } + + private void OnCreated(object sender, FileSystemEventArgs e) + { + FileCreated?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Created)); + } + + private void OnDeleted(object sender, FileSystemEventArgs e) + { + lock (_lock) + { + _lastWriteTimes.Remove(e.FullPath); + } + FileDeleted?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Deleted)); + } + + private void OnRenamed(object sender, RenamedEventArgs e) + { + lock (_lock) + { + _lastWriteTimes.Remove(e.OldFullPath); + } + FileRenamed?.Invoke(this, new FileRenamedEventArgs(e.FullPath, e.Name!, e.OldFullPath, e.OldName!)); + } + + private void OnError(object sender, ErrorEventArgs e) + { + Error?.Invoke(this, e); + } + + /// + /// 开始监视 + /// + public void Start() + { + _watcher.EnableRaisingEvents = true; + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.EnableRaisingEvents = false; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + _watcher.EnableRaisingEvents = false; + _watcher.Changed -= OnChanged; + _watcher.Created -= OnCreated; + _watcher.Deleted -= OnDeleted; + _watcher.Renamed -= OnRenamed; + _watcher.Error -= OnError; + _watcher.Dispose(); + } + } + + /// + /// 文件变更类型 + /// + public enum ChangeType + { + /// + /// 已更改 + /// + Changed, + + /// + /// 已创建 + /// + Created, + + /// + /// 已删除 + /// + Deleted, + + /// + /// 已重命名 + /// + Renamed + } + + /// + /// 文件变更事件参数 + /// + public class FileChangedEventArgs : EventArgs + { + /// + /// 完整路径 + /// + public string FullPath { get; } + + /// + /// 文件名 + /// + public string Name { get; } + + /// + /// 变更类型 + /// + public ChangeType ChangeType { get; } + + /// + /// 创建事件参数 + /// + public FileChangedEventArgs(string fullPath, string name, ChangeType changeType) + { + FullPath = fullPath; + Name = name; + ChangeType = changeType; + } + } + + /// + /// 文件重命名事件参数 + /// + public class FileRenamedEventArgs : EventArgs + { + /// + /// 新完整路径 + /// + public string FullPath { get; } + + /// + /// 新文件名 + /// + public string Name { get; } + + /// + /// 旧完整路径 + /// + public string OldFullPath { get; } + + /// + /// 旧文件名 + /// + public string OldName { get; } + + /// + /// 创建事件参数 + /// + public FileRenamedEventArgs(string fullPath, string name, string oldFullPath, string oldName) + { + FullPath = fullPath; + Name = name; + OldFullPath = oldFullPath; + OldName = oldName; + } + } + + /// + /// 目录监视器 + /// + public class DirectoryWatcher : IDisposable + { + private readonly List _watchers = new(); + + /// + /// 文件变更事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 创建目录监视器 + /// + /// 监视路径列表 + public DirectoryWatcher(params string[] paths) + { + foreach (var path in paths) + { + AddPath(path); + } + } + + /// + /// 添加监视路径 + /// + /// 路径 + public void AddPath(string path) + { + var watcher = new FileWatcher(path); + watcher.FileChanged += (s, e) => FileChanged?.Invoke(s, e); + watcher.FileCreated += (s, e) => FileCreated?.Invoke(s, e); + watcher.FileDeleted += (s, e) => FileDeleted?.Invoke(s, e); + watcher.FileRenamed += (s, e) => FileRenamed?.Invoke(s, e); + _watchers.Add(watcher); + } + + /// + /// 添加监视路径 + /// + /// 路径 + /// 筛选器 + public void AddPath(string path, string filter) + { + var watcher = new FileWatcher(path, filter); + watcher.FileChanged += (s, e) => FileChanged?.Invoke(s, e); + watcher.FileCreated += (s, e) => FileCreated?.Invoke(s, e); + watcher.FileDeleted += (s, e) => FileDeleted?.Invoke(s, e); + watcher.FileRenamed += (s, e) => FileRenamed?.Invoke(s, e); + _watchers.Add(watcher); + } + + /// + /// 开始监视所有路径 + /// + public void StartAll() + { + foreach (var watcher in _watchers) + { + watcher.Start(); + } + } + + /// + /// 停止监视所有路径 + /// + public void StopAll() + { + foreach (var watcher in _watchers) + { + watcher.Stop(); + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + foreach (var watcher in _watchers) + { + watcher.Dispose(); + } + _watchers.Clear(); + } + } +} diff --git a/EasyTool.Core/IOCategory/FileWatcherEx.cs b/EasyTool.Core/IOCategory/FileWatcherEx.cs new file mode 100644 index 0000000..b4760eb --- /dev/null +++ b/EasyTool.Core/IOCategory/FileWatcherEx.cs @@ -0,0 +1,298 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 文件监听器增强版 + /// + public class FileWatcherEx : IDisposable + { + private readonly FileSystemWatcher _watcher; + private readonly Dictionary _lastEvents = new(); + private readonly TimeSpan _debounceInterval; + private readonly object _lock = new(); + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件修改事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 错误事件 + /// + public event EventHandler? Error; + + /// + /// 监视的目录路径 + /// + public string Path => _watcher.Path; + + /// + /// 监视的文件过滤器 + /// + public string Filter => _watcher.Filter; + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories => _watcher.IncludeSubdirectories; + + /// + /// 创建文件监听器 + /// + /// 监视目录 + /// 文件过滤器 + /// 包含子目录 + /// 防抖间隔 + public FileWatcherEx(string path, string filter = "*.*", bool includeSubdirectories = true, TimeSpan? debounceInterval = null) + { + _debounceInterval = debounceInterval ?? TimeSpan.FromMilliseconds(100); + _watcher = new FileSystemWatcher + { + Path = path, + Filter = filter, + IncludeSubdirectories = includeSubdirectories, + NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | + NotifyFilters.LastWrite | NotifyFilters.Size + }; + + _watcher.Created += OnCreated; + _watcher.Deleted += OnDeleted; + _watcher.Changed += OnChanged; + _watcher.Renamed += OnRenamed; + _watcher.Error += OnError; + } + + /// + /// 开始监视 + /// + public void Start() + { + _watcher.EnableRaisingEvents = true; + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.EnableRaisingEvents = false; + } + + private void OnCreated(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Created")) + { + FileCreated?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Created)); + } + } + + private void OnDeleted(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Deleted")) + { + FileDeleted?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Deleted)); + } + } + + private void OnChanged(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Changed")) + { + FileChanged?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Changed)); + } + } + + private void OnRenamed(object sender, RenamedEventArgs e) + { + if (ShouldProcess(e.FullPath, "Renamed")) + { + FileRenamed?.Invoke(this, new FileRenamedEventArgs(e.FullPath, e.Name ?? string.Empty, e.OldFullPath, e.OldName ?? string.Empty)); + } + } + + private void OnError(object sender, ErrorEventArgs e) + { + Error?.Invoke(this, e); + } + + private bool ShouldProcess(string path, string eventType) + { + lock (_lock) + { + var key = $"{path}|{eventType}"; + var now = DateTime.UtcNow; + + if (_lastEvents.TryGetValue(key, out var lastTime)) + { + if (now - lastTime < _debounceInterval) + { + return false; + } + } + + _lastEvents[key] = now; + return true; + } + } + + public void Dispose() + { + _watcher?.Dispose(); + } + } + + /// + /// 目录监视器 + /// + public class DirectoryMonitor : IDisposable + { + private readonly string _path; + private readonly FileWatcherEx _watcher; + private readonly Dictionary _files = new(); + private readonly object _lock = new(); + + /// + /// 文件添加事件 + /// + public event EventHandler? FileAdded; + + /// + /// 文件移除事件 + /// + public event EventHandler? FileRemoved; + + /// + /// 文件修改事件 + /// + public event EventHandler? FileModified; + + /// + /// 当前文件列表 + /// + public IReadOnlyList CurrentFiles + { + get + { + lock (_lock) + { + return _files.Values.ToList().AsReadOnly(); + } + } + } + + /// + /// 创建目录监视器 + /// + public DirectoryMonitor(string path, string filter = "*.*", bool includeSubdirectories = true) + { + _path = path; + _watcher = new FileWatcherEx(path, filter, includeSubdirectories); + _watcher.FileCreated += OnFileCreated; + _watcher.FileDeleted += OnFileDeleted; + _watcher.FileChanged += OnFileChanged; + } + + /// + /// 开始监视 + /// + public void Start() + { + // 初始化现有文件 + InitializeFiles(); + _watcher.Start(); + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.Stop(); + } + + private void InitializeFiles() + { + lock (_lock) + { + _files.Clear(); + + if (Directory.Exists(_path)) + { + foreach (var file in Directory.GetFiles(_path, "*", SearchOption.AllDirectories)) + { + var info = new FileInfo(file); + _files[file] = info; + } + } + } + } + + private void OnFileCreated(object? sender, FileChangedEventArgs e) + { + if (File.Exists(e.FullPath)) + { + var info = new FileInfo(e.FullPath); + lock (_lock) + { + _files[e.FullPath] = info; + } + FileAdded?.Invoke(this, info); + } + } + + private void OnFileDeleted(object? sender, FileChangedEventArgs e) + { + FileInfo? info; + lock (_lock) + { + if (_files.TryGetValue(e.FullPath, out info)) + { + _files.Remove(e.FullPath); + } + } + + if (info != null) + { + FileRemoved?.Invoke(this, info); + } + } + + private void OnFileChanged(object? sender, FileChangedEventArgs e) + { + if (File.Exists(e.FullPath)) + { + var info = new FileInfo(e.FullPath); + lock (_lock) + { + _files[e.FullPath] = info; + } + FileModified?.Invoke(this, info); + } + } + + public void Dispose() + { + _watcher?.Dispose(); + } + } +} diff --git a/EasyTool.Core/IOCategory/ImageMetadataUtil.cs b/EasyTool.Core/IOCategory/ImageMetadataUtil.cs new file mode 100644 index 0000000..bfa2471 --- /dev/null +++ b/EasyTool.Core/IOCategory/ImageMetadataUtil.cs @@ -0,0 +1,346 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.IOCategory +{ + /// + /// 图片元数据工具类 + /// + public static class ImageMetadataUtil + { + /// + /// 读取图片EXIF信息 + /// + public static ExifData ReadExif(string imagePath) + { + var exif = new ExifData(); + + try + { + using var image = System.Drawing.Image.FromFile(imagePath); + var propertyItems = image.PropertyItems; + + foreach (var item in propertyItems) + { + var value = ParsePropertyItemValue(item); + var tagName = GetPropertyName(item.Id); + + switch (item.Id) + { + case 0x010F: // 制造商 + exif.Make = value; + break; + case 0x0110: // 型号 + exif.Model = value; + break; + case 0x0112: // 方向 + exif.Orientation = ParseOrientation(value); + break; + case 0x011A: // X分辨率 + case 0x011B: // Y分辨率 + break; + case 0x0128: // 分辨率单位 + break; + case 0x0131: // 软件 + exif.Software = value; + break; + case 0x0132: // 日期时间 + exif.DateTime = ParseDateTime(value); + break; + case 0x8769: // Exif IFD + break; + case 0x8827: // ISO速度 + exif.ISO = ParseInt(value); + break; + case 0x9003: // 原始日期时间 + exif.DateTimeOriginal = ParseDateTime(value); + break; + case 0x9004: // 数字化日期时间 + exif.DateTimeDigitized = ParseDateTime(value); + break; + case 0x920A: // 焦距 + exif.FocalLength = ParseRational(value); + break; + case 0x9207: // 光圈值 + break; + case 0x829A: // 曝光时间 + exif.ExposureTime = ParseRational(value); + break; + case 0x829D: // F值 + exif.FNumber = ParseRational(value); + break; + case 0x8825: // GPS信息 + break; + case 0xA002: // 图像宽度 + exif.ExifImageWidth = ParseInt(value); + break; + case 0xA003: // 图像高度 + exif.ExifImageHeight = ParseInt(value); + break; + case 0xA402: // 曝光模式 + break; + case 0xA403: // 白平衡 + break; + case 0xA406: // 场景拍摄类型 + break; + case 0xA420: // 图像唯一ID + exif.ImageUniqueID = value; + break; + } + + exif.AllProperties[tagName] = value; + } + } + catch + { + } + + return exif; + } + + /// + /// 移除EXIF信息 + /// + public static bool RemoveExif(string sourcePath, string destinationPath) + { + try + { + using var image = System.Drawing.Image.FromFile(sourcePath); + + // 创建没有EXIF的新图像 + using var newImage = new System.Drawing.Bitmap(image); + newImage.Save(destinationPath); + return true; + } + catch + { + return false; + } + } + + private static string ParsePropertyItemValue(System.Drawing.Imaging.PropertyItem item) + { + try + { + switch (item.Type) + { + case 1: // Byte + return BitConverter.ToString(item.Value).Replace("-", " "); + case 2: // ASCII + return System.Text.Encoding.ASCII.GetString(item.Value).TrimEnd('\0'); + case 3: // Short + return BitConverter.ToUInt16(item.Value, 0).ToString(); + case 4: // Long + return BitConverter.ToUInt32(item.Value, 0).ToString(); + case 5: // Rational + return ParseRational(item.Value).ToString(); + case 7: // Undefined + return BitConverter.ToString(item.Value).Replace("-", " "); + case 9: // SLong + return BitConverter.ToInt32(item.Value, 0).ToString(); + case 10: // SRational + return ParseRational(item.Value).ToString(); + default: + return BitConverter.ToString(item.Value); + } + } + catch + { + return ""; + } + } + + private static double ParseRational(byte[] value) + { + if (value.Length < 8) return 0; + var numerator = BitConverter.ToUInt32(value, 0); + var denominator = BitConverter.ToUInt32(value, 4); + return denominator != 0 ? (double)numerator / denominator : 0; + } + + private static double ParseRational(string value) + { + if (string.IsNullOrEmpty(value)) return 0; + if (double.TryParse(value, out var result)) return result; + return 0; + } + + private static int ParseInt(string value) + { + return int.TryParse(value, out var result) ? result : 0; + } + + private static DateTime? ParseDateTime(string value) + { + if (string.IsNullOrEmpty(value)) return null; + // EXIF日期格式: "yyyy:MM:dd HH:mm:ss" + if (DateTime.TryParseExact(value, "yyyy:MM:dd HH:mm:ss", + System.Globalization.CultureInfo.InvariantCulture, + System.Globalization.DateTimeStyles.None, out var result)) + { + return result; + } + return null; + } + + private static int ParseOrientation(string value) + { + return int.TryParse(value, out var result) ? result : 1; + } + + private static string GetPropertyName(int id) + { + return id switch + { + 0x0100 => "ImageWidth", + 0x0101 => "ImageLength", + 0x0102 => "BitsPerSample", + 0x0103 => "Compression", + 0x0106 => "PhotometricInterpretation", + 0x010E => "ImageDescription", + 0x010F => "Make", + 0x0110 => "Model", + 0x0111 => "StripOffsets", + 0x0112 => "Orientation", + 0x0115 => "SamplesPerPixel", + 0x0116 => "RowsPerStrip", + 0x0117 => "StripByteCounts", + 0x011A => "XResolution", + 0x011B => "YResolution", + 0x0128 => "ResolutionUnit", + 0x0131 => "Software", + 0x0132 => "DateTime", + 0x8769 => "ExifIFDPointer", + 0x8827 => "ISOSpeedRatings", + 0x9003 => "DateTimeOriginal", + 0x9004 => "DateTimeDigitized", + 0x920A => "FocalLength", + 0x829A => "ExposureTime", + 0x829D => "FNumber", + 0xA002 => "ExifImageWidth", + 0xA003 => "ExifImageHeight", + _ => $"0x{id:X4}" + }; + } + } + + /// + /// EXIF数据 + /// + public class ExifData + { + /// + /// 制造商 + /// + public string Make { get; set; } = ""; + + /// + /// 型号 + /// + public string Model { get; set; } = ""; + + /// + /// 软件 + /// + public string Software { get; set; } = ""; + + /// + /// 方向(1-8) + /// + public int Orientation { get; set; } = 1; + + /// + /// 日期时间 + /// + public DateTime? DateTime { get; set; } + + /// + /// 原始日期时间 + /// + public DateTime? DateTimeOriginal { get; set; } + + /// + /// 数字化日期时间 + /// + public DateTime? DateTimeDigitized { get; set; } + + /// + /// ISO感光度 + /// + public int ISO { get; set; } + + /// + /// 焦距 + /// + public double FocalLength { get; set; } + + /// + /// 曝光时间 + /// + public double ExposureTime { get; set; } + + /// + /// 光圈值 + /// + public double FNumber { get; set; } + + /// + /// EXIF图像宽度 + /// + public int ExifImageWidth { get; set; } + + /// + /// EXIF图像高度 + /// + public int ExifImageHeight { get; set; } + + /// + /// 图像唯一ID + /// + public string ImageUniqueID { get; set; } = ""; + + /// + /// 所有属性 + /// + public Dictionary AllProperties { get; } = new(); + + /// + /// 获取方向描述 + /// + public string OrientationDescription => Orientation switch + { + 1 => "正常", + 2 => "水平翻转", + 3 => "旋转180度", + 4 => "垂直翻转", + 5 => "逆时针90度+水平翻转", + 6 => "顺时针90度", + 7 => "顺时针90度+水平翻转", + 8 => "逆时针90度", + _ => "未知" + }; + + /// + /// 曝光时间显示 + /// + public string ExposureTimeDisplay + { + get + { + if (ExposureTime >= 1) + return $"{ExposureTime:F1}s"; + return $"1/{(int)(1 / ExposureTime)}s"; + } + } + + /// + /// 光圈显示 + /// + public string FNumberDisplay => FNumber > 0 ? $"f/{FNumber:F1}" : ""; + + /// + /// 焦距显示 + /// + public string FocalLengthDisplay => FocalLength > 0 ? $"{FocalLength:F1}mm" : ""; + } +} diff --git a/EasyTool.Core/IOCategory/JsonSerializer.cs b/EasyTool.Core/IOCategory/JsonSerializer.cs new file mode 100644 index 0000000..871aa5f --- /dev/null +++ b/EasyTool.Core/IOCategory/JsonSerializer.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace EasyTool.IOCategory +{ + /// + /// JSON序列化工具增强版 + /// + public static class JsonSerializer + { + private static JsonSerializerOptions _defaultOptions; + private static JsonSerializerOptions _indentedOptions; + private static JsonSerializerOptions _camelCaseOptions; + + static JsonSerializer() + { + _defaultOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = null, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + + _indentedOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = null, + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + + _camelCaseOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + } + + /// + /// 序列化对象为JSON字符串 + /// + public static string Serialize(T value, bool indented = false) + { + return System.Text.Json.JsonSerializer.Serialize(value, indented ? _indentedOptions : _defaultOptions); + } + + /// + /// 序列化对象为JSON字符串(驼峰命名) + /// + public static string SerializeCamelCase(T value) + { + return System.Text.Json.JsonSerializer.Serialize(value, _camelCaseOptions); + } + + /// + /// 反序列化JSON字符串为对象 + /// + public static T? Deserialize(string json) + { + return System.Text.Json.JsonSerializer.Deserialize(json, _defaultOptions); + } + + /// + /// 反序列化JSON字符串为对象(驼峰命名) + /// + public static T? DeserializeCamelCase(string json) + { + return System.Text.Json.JsonSerializer.Deserialize(json, _camelCaseOptions); + } + + /// + /// 序列化到文件 + /// + public static void SerializeToFile(T value, string filePath, bool indented = true) + { + var directory = System.IO.Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !System.IO.Directory.Exists(directory)) + System.IO.Directory.CreateDirectory(directory); + + var json = Serialize(value, indented); + System.IO.File.WriteAllText(filePath, json); + } + + /// + /// 从文件反序列化 + /// + public static T? DeserializeFromFile(string filePath) + { + if (!System.IO.File.Exists(filePath)) + throw new System.IO.FileNotFoundException("文件不存在", filePath); + + var json = System.IO.File.ReadAllText(filePath); + return Deserialize(json); + } + + /// + /// 异步序列化到文件 + /// + public static async System.Threading.Tasks.Task SerializeToFileAsync(T value, string filePath, bool indented = true) + { + var directory = System.IO.Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !System.IO.Directory.Exists(directory)) + System.IO.Directory.CreateDirectory(directory); + + var json = Serialize(value, indented); + await System.IO.File.WriteAllTextAsync(filePath, json); + } + + /// + /// 异步从文件反序列化 + /// + public static async System.Threading.Tasks.Task DeserializeFromFileAsync(string filePath) + { + if (!System.IO.File.Exists(filePath)) + throw new System.IO.FileNotFoundException("文件不存在", filePath); + + var json = await System.IO.File.ReadAllTextAsync(filePath); + return Deserialize(json); + } + + /// + /// 尝试反序列化 + /// + public static bool TryDeserialize(string json, out T? result) + { + try + { + result = Deserialize(json); + return true; + } + catch + { + result = default; + return false; + } + } + + /// + /// 验证JSON格式 + /// + public static bool IsValidJson(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + using var doc = JsonDocument.Parse(json); + return true; + } + catch + { + return false; + } + } + + /// + /// 格式化JSON + /// + public static string Format(string json) + { + using var doc = JsonDocument.Parse(json); + return System.Text.Json.JsonSerializer.Serialize(doc.RootElement, _indentedOptions); + } + + /// + /// 压缩JSON + /// + public static string Minify(string json) + { + using var doc = JsonDocument.Parse(json); + return System.Text.Json.JsonSerializer.Serialize(doc.RootElement, _defaultOptions); + } + + /// + /// 合并两个JSON对象 + /// + public static string Merge(string json1, string json2) + { + var dict1 = Deserialize>(json1); + var dict2 = Deserialize>(json2); + + if (dict1 == null) return json2; + if (dict2 == null) return json1; + + foreach (var kvp in dict2) + { + dict1[kvp.Key] = kvp.Value; + } + + return Serialize(dict1); + } + + /// + /// 获取JSON值(通过路径) + /// + public static string? GetValue(string json, string path) + { + try + { + using var doc = JsonDocument.Parse(json); + var current = doc.RootElement; + + var parts = path.Split('.'); + foreach (var part in parts) + { + if (current.TryGetProperty(part, out var property)) + { + current = property; + } + else + { + return null; + } + } + + return current.ValueKind == JsonValueKind.String + ? current.GetString() + : current.ToString(); + } + catch + { + return null; + } + } + + /// + /// 设置JSON值(通过路径) + /// + public static string SetValue(string json, string path, object value) + { + var dict = Deserialize>(json) ?? new Dictionary(); + + var parts = path.Split('.'); + var current = dict; + + for (int i = 0; i < parts.Length - 1; i++) + { + var part = parts[i]; + if (!current.ContainsKey(part)) + { + current[part] = new Dictionary(); + } + + current = (Dictionary)current[part]; + } + + current[parts[^1]] = value; + + return Serialize(dict); + } + + /// + /// 获取JSON的所有键 + /// + public static List GetKeys(string json) + { + var keys = new List(); + + try + { + using var doc = JsonDocument.Parse(json); + if (doc.RootElement.ValueKind == JsonValueKind.Object) + { + foreach (var property in doc.RootElement.EnumerateObject()) + { + keys.Add(property.Name); + } + } + } + catch + { + } + + return keys; + } + + /// + /// 深拷贝对象 + /// + public static T? DeepClone(T obj) + { + var json = Serialize(obj); + return Deserialize(json); + } + + /// + /// 转换类型 + /// + public static TTo? Convert(TFrom from) + { + var json = Serialize(from); + return Deserialize(json); + } + + /// + /// 获取自定义选项 + /// + public static JsonSerializerOptions GetOptions(bool indented = false, bool camelCase = false) + { + if (camelCase) + return new JsonSerializerOptions(_camelCaseOptions) { WriteIndented = indented }; + return new JsonSerializerOptions(_defaultOptions) { WriteIndented = indented }; + } + } +} diff --git a/EasyTool.Core/IOCategory/JsonUtil.cs b/EasyTool.Core/IOCategory/JsonUtil.cs index d1a6c24..91d1fb5 100644 --- a/EasyTool.Core/IOCategory/JsonUtil.cs +++ b/EasyTool.Core/IOCategory/JsonUtil.cs @@ -18,41 +18,55 @@ public static class JsonUtil /// /// 默认序列化选项(驼峰命名、缩进、忽略null) /// - public static JsonSerializerOptions DefaultOptions => new() + public static JsonSerializerOptions DefaultOptions { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - WriteIndented = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - NumberHandling = JsonNumberHandling.AllowReadingFromString, - Converters = + get { - new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString + }; + options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); + return options; } - }; + } /// /// 紧凑序列化选项(无缩进、忽略null、驼峰命名) /// - public static JsonSerializerOptions CompactOptions => new() + public static JsonSerializerOptions CompactOptions { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - WriteIndented = false, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - NumberHandling = JsonNumberHandling.AllowReadingFromString - }; + get + { + return new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString + }; + } + } /// /// 宽松反序列化选项(允许不带引号的数字、允许注释、允许尾随逗号) /// - public static JsonSerializerOptions LenientOptions => new() + public static JsonSerializerOptions LenientOptions { - PropertyNameCaseInsensitive = true, - NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals, - ReadCommentHandling = JsonCommentHandling.Skip, - AllowTrailingCommas = true - }; + get + { + return new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + } + } #endregion @@ -61,39 +75,30 @@ public static class JsonUtil /// /// 将对象序列化为 JSON 字符串 /// - /// 对象类型 - /// 要序列化的对象 - /// 序列化选项(可选) - /// JSON 字符串 public static string Serialize(T obj, JsonSerializerOptions? options = null) { if (obj == null) return "null"; - return JsonSerializer.Serialize(obj, options ?? DefaultOptions); + // 使用泛型方法 + return JsonSerializer.Serialize(obj); } /// /// 将对象序列化为 JSON 字符串(紧凑格式) /// - /// 对象类型 - /// 要序列化的对象 - /// 紧凑格式的 JSON 字符串 public static string SerializeCompact(T obj) { - return Serialize(obj, CompactOptions); + return JsonSerializer.Serialize(obj); } /// /// 将对象序列化为 JSON 字节数组 /// - /// 对象类型 - /// 要序列化的对象 - /// 序列化选项(可选) - /// JSON 字节数组 public static byte[] SerializeToUtf8Bytes(T obj, JsonSerializerOptions? options = null) { - return JsonSerializer.SerializeToUtf8Bytes(obj, options ?? DefaultOptions); + var json = JsonSerializer.Serialize(obj); + return Encoding.UTF8.GetBytes(json); } #endregion @@ -103,41 +108,29 @@ public static byte[] SerializeToUtf8Bytes(T obj, JsonSerializerOptions? optio /// /// 将 JSON 字符串反序列化为对象 /// - /// 目标类型 - /// JSON 字符串 - /// 反序列化选项(可选) - /// 反序列化后的对象 public static T? Deserialize(string json, JsonSerializerOptions? options = null) { if (string.IsNullOrWhiteSpace(json)) return default; - return JsonSerializer.Deserialize(json, options ?? LenientOptions); + return JsonSerializer.Deserialize(json); } /// /// 将 JSON 字节数组反序列化为对象 /// - /// 目标类型 - /// JSON 字节数组 - /// 反序列化选项(可选) - /// 反序列化后的对象 public static T? Deserialize(byte[] utf8Json, JsonSerializerOptions? options = null) { if (utf8Json == null || utf8Json.Length == 0) return default; - return JsonSerializer.Deserialize(utf8Json, options ?? LenientOptions); + var json = Encoding.UTF8.GetString(utf8Json); + return JsonSerializer.Deserialize(json); } /// /// 尝试将 JSON 字符串反序列化为对象 /// - /// 目标类型 - /// JSON 字符串 - /// 反序列化结果 - /// 反序列化选项(可选) - /// 是否成功 public static bool TryDeserialize(string json, out T? result, JsonSerializerOptions? options = null) { result = default; @@ -146,7 +139,7 @@ public static bool TryDeserialize(string json, out T? result, JsonSerializerO try { - result = JsonSerializer.Deserialize(json, options ?? LenientOptions); + result = Deserialize(json, options); return true; } catch @@ -158,8 +151,6 @@ public static bool TryDeserialize(string json, out T? result, JsonSerializerO /// /// 将 JSON 字符串反序列化为动态对象 /// - /// JSON 字符串 - /// JsonNode 对象 public static JsonNode? Parse(string json) { if (string.IsNullOrWhiteSpace(json)) @@ -175,9 +166,6 @@ public static bool TryDeserialize(string json, out T? result, JsonSerializerO /// /// 格式化 JSON 字符串(美化输出) /// - /// JSON 字符串 - /// 缩进字符(默认2个空格) - /// 格式化后的 JSON 字符串 public static string Prettify(string json, string indent = " ") { if (string.IsNullOrWhiteSpace(json)) @@ -186,17 +174,7 @@ public static string Prettify(string json, string indent = " ") try { var node = JsonNode.Parse(json); - var options = new JsonSerializerOptions - { - WriteIndented = true - }; -#if NET9_0_OR_GREATER - if (indent.Length > 0) - { - options.IndentCharacter = indent[0]; - options.IndentSize = indent.Length; - } -#endif + var options = new JsonSerializerOptions { WriteIndented = true }; return node?.ToJsonString(options) ?? json; } catch @@ -208,8 +186,6 @@ public static string Prettify(string json, string indent = " ") /// /// 压缩 JSON 字符串(移除空白) /// - /// JSON 字符串 - /// 压缩后的 JSON 字符串 public static string Minify(string json) { if (string.IsNullOrWhiteSpace(json)) @@ -229,8 +205,6 @@ public static string Minify(string json) /// /// 验证是否为有效的 JSON /// - /// JSON 字符串 - /// 是否有效 public static bool IsValid(string json) { if (string.IsNullOrWhiteSpace(json)) @@ -254,9 +228,6 @@ public static bool IsValid(string json) /// /// 从 JSON 字符串中获取指定路径的值 /// - /// JSON 字符串 - /// 路径(使用点号分隔,如 "user.name") - /// 找到的值,未找到返回 null public static object? GetValue(string json, string path) { if (string.IsNullOrWhiteSpace(json) || string.IsNullOrWhiteSpace(path)) @@ -276,10 +247,6 @@ public static bool IsValid(string json) /// /// 从 JSON 字符串中获取指定路径的值并转换为指定类型 /// - /// 目标类型 - /// JSON 字符串 - /// 路径 - /// 转换后的值 public static T? GetValue(string json, string path) { var value = GetValue(json, path); @@ -293,7 +260,7 @@ public static bool IsValid(string json) if (value is JsonNode jsonNode) { - return jsonNode.Deserialize(LenientOptions); + return Deserialize(jsonNode.ToJsonString()); } return (T?)Convert.ChangeType(value, typeof(T)); @@ -302,10 +269,6 @@ public static bool IsValid(string json) /// /// 设置 JSON 字符串中指定路径的值 /// - /// JSON 字符串 - /// 路径 - /// 要设置的值 - /// 修改后的 JSON 字符串 public static string SetValue(string json, string path, object? value) { if (string.IsNullOrWhiteSpace(json)) @@ -336,7 +299,6 @@ public static string SetValue(string json, string path, object? value) if (current == null) return null; - // 处理数组索引,如 items[0] if (part.Contains('[') && part.EndsWith(']')) { var name = part.Substring(0, part.IndexOf('[')); @@ -435,55 +397,43 @@ private static void SetValueByPath(JsonNode? node, string path, object? value) /// /// 将字典转换为 JSON 对象 /// - /// 键类型 - /// 值类型 - /// 字典 - /// JSON 字符串 public static string FromDictionary(Dictionary dictionary) { if (dictionary == null) return "{}"; - return JsonSerializer.Serialize(dictionary, DefaultOptions); + return JsonSerializer.Serialize(dictionary); } /// /// 将 JSON 对象转换为字典 /// - /// 值类型 - /// JSON 字符串 - /// 字典 public static Dictionary? ToDictionary(string json) { if (string.IsNullOrWhiteSpace(json)) return null; - return JsonSerializer.Deserialize>(json, LenientOptions); + return JsonSerializer.Deserialize>(json); } /// /// 将匿名对象转换为 JSON 字符串 /// - /// 匿名对象 - /// JSON 字符串 public static string FromAnonymous(object obj) { - return Serialize(obj, CompactOptions); + return JsonSerializer.Serialize(obj); } /// /// 深拷贝对象(通过 JSON 序列化/反序列化) /// - /// 对象类型 - /// 要拷贝的对象 - /// 拷贝后的对象 public static T? DeepClone(T obj) { if (obj == null) return default; - var json = JsonSerializer.Serialize(obj, DefaultOptions); - return JsonSerializer.Deserialize(json, LenientOptions); + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize(json); } #endregion @@ -493,9 +443,6 @@ public static string FromAnonymous(object obj) /// /// 合并两个 JSON 对象 /// - /// 第一个 JSON - /// 第二个 JSON(优先级更高) - /// 合并后的 JSON public static string Merge(string json1, string json2) { if (string.IsNullOrWhiteSpace(json1)) @@ -547,4 +494,4 @@ private static void MergeObjects(JsonObject target, JsonObject source) #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/PathUtil.cs b/EasyTool.Core/IOCategory/PathUtil.cs index d899c72..a65039a 100644 --- a/EasyTool.Core/IOCategory/PathUtil.cs +++ b/EasyTool.Core/IOCategory/PathUtil.cs @@ -8,339 +8,346 @@ namespace EasyTool.IOCategory { /// /// 路径工具类 - /// 提供路径操作的增强功能 /// public static class PathUtil { /// - /// 获取相对路径 + /// 合并路径 /// - public static string GetRelativePath(string basePath, string targetPath) + public static string Combine(params string[] paths) { - if (string.IsNullOrEmpty(basePath)) - throw new ArgumentException("Base path cannot be null or empty", nameof(basePath)); - if (string.IsNullOrEmpty(targetPath)) - throw new ArgumentException("Target path cannot be null or empty", nameof(targetPath)); - - // 规范化路径 - basePath = Normalize(basePath); - targetPath = Normalize(targetPath); - - // 确保基础路径以分隔符结尾 - if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) && - !basePath.EndsWith(Path.AltDirectorySeparatorChar.ToString())) - { - basePath += Path.DirectorySeparatorChar; - } - - var baseParts = basePath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); - var targetParts = targetPath.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); - - // 处理 Windows 盘符 - if (baseParts.Length > 0 && targetParts.Length > 0) - { - string baseRoot = GetRoot(basePath); - string targetRoot = GetRoot(targetPath); - if (!string.IsNullOrEmpty(baseRoot) && !string.IsNullOrEmpty(targetRoot) && - !baseRoot.Equals(targetRoot, StringComparison.OrdinalIgnoreCase)) - { - return targetPath; // 不同盘符,返回绝对路径 - } - } - - // 找到公共前缀 - int commonLength = 0; - int minLen = Math.Min(baseParts.Length, targetParts.Length); - while (commonLength < minLen && - baseParts[commonLength].Equals(targetParts[commonLength], StringComparison.OrdinalIgnoreCase)) - { - commonLength++; - } - - // 构建相对路径 - var result = new StringBuilder(); + return Path.Combine(paths); + } - // 添加向上回溯 - for (int i = commonLength; i < baseParts.Length - (basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? 0 : 1); i++) - { - if (result.Length > 0) - result.Append(Path.DirectorySeparatorChar); - result.Append(".."); - } + /// + /// 获取绝对路径 + /// + public static string GetFullPath(string path, string? basePath = null) + { + if (string.IsNullOrEmpty(path)) + return path; - // 添加目标路径的剩余部分 - for (int i = commonLength; i < targetParts.Length; i++) - { - if (result.Length > 0) - result.Append(Path.DirectorySeparatorChar); - result.Append(targetParts[i]); - } + if (Path.IsPathRooted(path)) + return Path.GetFullPath(path); - return result.Length == 0 ? "." : result.ToString(); + basePath ??= Directory.GetCurrentDirectory(); + return Path.GetFullPath(Path.Combine(basePath, path)); } - private static string GetRoot(string path) + /// + /// 获取相对路径 + /// + public static string GetRelativePath(string relativeTo, string path) { - if (path.Length >= 2 && path[1] == ':') - return path.Substring(0, 2).ToUpperInvariant(); - if (path.StartsWith("/") || path.StartsWith("\\")) - return path[0].ToString(); - return ""; + return Path.GetRelativePath(relativeTo, path); } /// - /// 规范化路径(统一分隔符,移除多余的点和分隔符) + /// 获取文件名(包含扩展名) /// - public static string Normalize(string path) + public static string GetFileName(string path) { - if (string.IsNullOrEmpty(path)) - return path; - - // 替换分隔符 - path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + return Path.GetFileName(path); + } - // 处理 ./ 和 ../ - var parts = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None); - var result = new List(); + /// + /// 获取文件名(不含扩展名) + /// + public static string GetFileNameWithoutExtension(string path) + { + return Path.GetFileNameWithoutExtension(path); + } - foreach (var part in parts) - { - if (part == ".") - continue; - else if (part == "..") - { - if (result.Count > 0 && result[result.Count - 1] != "..") - result.RemoveAt(result.Count - 1); - else if (!IsAbsolute(path)) - result.Add(".."); - } - else - { - result.Add(part); - } - } + /// + /// 获取扩展名 + /// + public static string GetExtension(string path) + { + return Path.GetExtension(path); + } - string normalized = string.Join(Path.DirectorySeparatorChar.ToString(), result); + /// + /// 获取目录路径 + /// + public static string? GetDirectoryName(string path) + { + return Path.GetDirectoryName(path); + } - // 处理根路径 - if (path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !normalized.StartsWith(Path.DirectorySeparatorChar.ToString())) - { - if (path.Length >= 2 && path[1] == ':') - normalized = path.Substring(0, 2) + Path.DirectorySeparatorChar + normalized; - else - normalized = Path.DirectorySeparatorChar + normalized; - } + /// + /// 更改扩展名 + /// + public static string ChangeExtension(string path, string extension) + { + return Path.ChangeExtension(path, extension); + } - return normalized; + /// + /// 移除扩展名 + /// + public static string RemoveExtension(string path) + { + return Path.ChangeExtension(path, null) ?? path; } /// - /// 判断是否为绝对路径 + /// 检查是否是绝对路径 /// public static bool IsAbsolute(string path) { - if (string.IsNullOrEmpty(path)) - return false; - - // Windows: C:\ 或 \ - // Unix: / - return Path.IsPathRooted(path) || - (path.Length >= 2 && path[1] == ':') || - path.StartsWith("/") || - path.StartsWith("\\\\"); + return Path.IsPathRooted(path); } /// - /// 获取文件扩展名(不带点) + /// 检查是否是相对路径 /// - public static string GetExtensionWithoutDot(string path) + public static bool IsRelative(string path) { - string ext = Path.GetExtension(path); - return string.IsNullOrEmpty(ext) ? "" : ext.Substring(1); + return !Path.IsPathRooted(path); } /// - /// 更改文件扩展名 + /// 规范化路径(统一分隔符) /// - public static string ChangeExtension(string path, string newExtension) + public static string Normalize(string path) { if (string.IsNullOrEmpty(path)) return path; - newExtension = newExtension?.StartsWith(".") == true ? newExtension : "." + newExtension; - return Path.ChangeExtension(path, newExtension); + return path.Replace('/', Path.DirectorySeparatorChar) + .Replace('\\', Path.DirectorySeparatorChar) + .TrimEnd(Path.DirectorySeparatorChar); } /// - /// 获取文件名(不带扩展名) + /// 确保以分隔符结尾 /// - public static string GetFileNameWithoutExtension(string path) + public static string EnsureTrailingSeparator(string path) { - return Path.GetFileNameWithoutExtension(path); + if (string.IsNullOrEmpty(path)) + return path; + + if (!path.EndsWith(Path.DirectorySeparatorChar.ToString())) + return path + Path.DirectorySeparatorChar; + + return path; } /// - /// 获取父目录路径 + /// 移除尾部分隔符 /// - public static string GetParent(string path) + public static string TrimTrailingSeparator(string path) { - return Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(path)) + return path; + + return path.TrimEnd(Path.DirectorySeparatorChar, '/'); } /// - /// 获取所有父目录路径 + /// 获取父目录 /// - public static List GetParents(string path) + public static string? GetParent(string path) { - var parents = new List(); - string current = path; + if (string.IsNullOrEmpty(path)) + return null; + + var dir = Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(dir)) + return null; + return dir; + } + + /// + /// 获取所有父目录 + /// + public static IEnumerable GetParents(string path) + { + var current = path; while (!string.IsNullOrEmpty(current)) { - string parent = Path.GetDirectoryName(current); + var parent = Path.GetDirectoryName(current); if (string.IsNullOrEmpty(parent)) - break; - parents.Add(parent); + yield break; + + yield return parent; current = parent; } - - return parents; } /// - /// 连接路径片段 + /// 获取目录深度 /// - public static string Combine(params string[] paths) + public static int GetDepth(string path) { - return Path.Combine(paths); + if (string.IsNullOrEmpty(path)) + return 0; + + path = Normalize(path); + return path.Split(Path.DirectorySeparatorChar).Length - 1; } /// - /// 获取临时文件路径 + /// 检查路径是否在指定目录下 /// - public static string GetTempFilePath(string extension = null) + public static bool IsInDirectory(string path, string directory) { - string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - if (!string.IsNullOrEmpty(extension)) - { - extension = extension.StartsWith(".") ? extension : "." + extension; - path += extension; - } - return path; + if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(directory)) + return false; + + var fullPath = GetFullPath(path); + var fullDirectory = GetFullPath(directory); + + return fullPath.StartsWith(EnsureTrailingSeparator(fullDirectory), StringComparison.OrdinalIgnoreCase); } /// - /// 获取临时目录路径 + /// 获取唯一文件名(避免冲突) /// - public static string GetTempDirectoryPath() + public static string GetUniqueFileName(string directory, string fileName) { - return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + var fullPath = Path.Combine(directory, fileName); + + if (!File.Exists(fullPath)) + return fileName; + + var name = Path.GetFileNameWithoutExtension(fileName); + var ext = Path.GetExtension(fileName); + var counter = 1; + + while (true) + { + var newName = $"{name} ({counter}){ext}"; + fullPath = Path.Combine(directory, newName); + + if (!File.Exists(fullPath)) + return newName; + + counter++; + } } /// - /// 确保目录存在 + /// 获取临时文件路径 /// - public static string EnsureDirectoryExists(string path) + public static string GetTempFilePath(string? extension = null) { - if (!Directory.Exists(path)) + var path = Path.GetTempFileName(); + + if (!string.IsNullOrEmpty(extension)) { - Directory.CreateDirectory(path); + var newPath = Path.ChangeExtension(path, extension); + File.Move(path, newPath); + return newPath; } + return path; } /// - /// 确保文件所在目录存在 + /// 获取临时目录路径 /// - public static string EnsureParentDirectoryExists(string filePath) + public static string GetTempDirectoryPath() { - string dir = Path.GetDirectoryName(filePath); - if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - return filePath; + var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + return path; } /// - /// 获取唯一文件名(如果文件存在则添加序号) + /// 分割路径为各部分 /// - public static string GetUniqueFilePath(string basePath) + public static string[] Split(string path) { - if (!File.Exists(basePath)) - return basePath; + if (string.IsNullOrEmpty(path)) + return Array.Empty(); - string dir = Path.GetDirectoryName(basePath); - string name = Path.GetFileNameWithoutExtension(basePath); - string ext = Path.GetExtension(basePath); + path = Normalize(path); - int count = 1; - string newPath; - do + // 处理根目录 + var root = Path.GetPathRoot(path); + if (!string.IsNullOrEmpty(root)) { - newPath = Path.Combine(dir ?? "", $"{name} ({count}){ext}"); - count++; + path = path.Substring(root.Length); + var parts = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + var result = new string[parts.Length + 1]; + result[0] = root.TrimEnd(Path.DirectorySeparatorChar); + Array.Copy(parts, 0, result, 1, parts.Length); + return result; } - while (File.Exists(newPath)); - return newPath; + return path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); } /// - /// 获取唯一目录名 + /// 构建路径 /// - public static string GetUniqueDirectoryPath(string basePath) + public static string Build(params string[] parts) { - if (!Directory.Exists(basePath)) - return basePath; + return Path.Combine(parts.Where(p => !string.IsNullOrEmpty(p)).ToArray()); + } - int count = 1; - string newPath; - do - { - newPath = $"{basePath} ({count})"; - count++; - } - while (Directory.Exists(newPath)); + /// + /// 验证路径是否有效 + /// + public static bool IsValid(string path) + { + if (string.IsNullOrEmpty(path)) + return false; - return newPath; + var invalidChars = Path.GetInvalidPathChars(); + return path.IndexOfAny(invalidChars) < 0; } /// - /// 获取路径深度 + /// 验证文件名是否有效 /// - public static int GetDepth(string path) + public static bool IsValidFileName(string fileName) { - if (string.IsNullOrEmpty(path)) - return 0; + if (string.IsNullOrEmpty(fileName)) + return false; - path = Normalize(path); - return path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Length; + var invalidChars = Path.GetInvalidFileNameChars(); + return fileName.IndexOfAny(invalidChars) < 0; } /// - /// 路径是否在指定目录下 + /// 清理文件名(移除无效字符) /// - public static bool IsInDirectory(string path, string directory) + public static string SanitizeFileName(string fileName, char replacement = '_') { - string normalizedPath = Normalize(Path.GetFullPath(path)); - string normalizedDir = Normalize(Path.GetFullPath(directory)); + if (string.IsNullOrEmpty(fileName)) + return fileName; + + var invalidChars = Path.GetInvalidFileNameChars(); + var result = new StringBuilder(fileName); - return normalizedPath.StartsWith(normalizedDir, StringComparison.OrdinalIgnoreCase); + foreach (var c in invalidChars) + { + result.Replace(c, replacement); + } + + return result.ToString(); } /// - /// 获取路径层级(相对于基础路径) + /// 获取路径大小(文件或目录) /// - public static string GetPathLevel(string basePath, string targetPath, int level) + public static long GetSize(string path) { - string relative = GetRelativePath(basePath, targetPath); - var parts = relative.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + if (File.Exists(path)) + { + return new FileInfo(path).Length; + } - if (level < 0 || level >= parts.Length) - return null; + if (Directory.Exists(path)) + { + var dirInfo = new DirectoryInfo(path); + return dirInfo.EnumerateFiles("*", SearchOption.AllDirectories).Sum(f => f.Length); + } - return parts[level]; + return 0; } } } diff --git a/EasyTool.Core/IOCategory/QrCodeUtil.cs b/EasyTool.Core/IOCategory/QrCodeUtil.cs new file mode 100644 index 0000000..2686cd0 --- /dev/null +++ b/EasyTool.Core/IOCategory/QrCodeUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 二维码配置 + /// + public class QrCodeOptions + { + /// + /// 宽度(像素) + /// + public int Width { get; set; } = 200; + + /// + /// 高度(像素) + /// + public int Height { get; set; } = 200; + + /// + /// 纠错级别 + /// + public QrCodeErrorCorrection ErrorCorrection { get; set; } = QrCodeErrorCorrection.Medium; + + /// + /// 前景色 + /// + public Color ForeColor { get; set; } = Color.Black; + + /// + /// 背景色 + /// + public Color BackColor { get; set; } = Color.White; + + /// + /// 边距(模块数) + /// + public int Margin { get; set; } = 4; + } + + /// + /// 二维码纠错级别 + /// + public enum QrCodeErrorCorrection + { + /// + /// 低(7%可纠错) + /// + Low = 0, + + /// + /// 中(15%可纠错) + /// + Medium = 1, + + /// + /// 高(25%可纠错) + /// + Quartile = 2, + + /// + /// 最高(30%可纠错) + /// + High = 3 + } + + /// + /// 二维码工具类 + /// 提供二维码生成功能 + /// + public static class QrCodeUtil + { + #region 生成二维码 + + /// + /// 生成二维码图像 + /// + /// 内容 + /// 配置 + /// 二维码图像 + public static Bitmap Generate(string content, QrCodeOptions? options = null) + { + options ??= new QrCodeOptions(); + + // 编码内容 + var bytes = Encoding.UTF8.GetBytes(content); + + // 生成QR码矩阵 + var matrix = GenerateQrMatrix(bytes, options.ErrorCorrection); + + // 创建图像 + var bitmap = new Bitmap(options.Width, options.Height, PixelFormat.Format24bppRgb); + + using (var g = Graphics.FromImage(bitmap)) + { + g.Clear(options.BackColor); + + var moduleWidth = (double)options.Width / (matrix.GetLength(0) + 2 * options.Margin); + var moduleHeight = (double)options.Height / (matrix.GetLength(1) + 2 * options.Margin); + var moduleSize = Math.Min(moduleWidth, moduleHeight); + + var offsetX = (options.Width - matrix.GetLength(0) * moduleSize) / 2; + var offsetY = (options.Height - matrix.GetLength(1) * moduleSize) / 2; + + using var brush = new SolidBrush(options.ForeColor); + + for (int y = 0; y < matrix.GetLength(1); y++) + { + for (int x = 0; x < matrix.GetLength(0); x++) + { + if (matrix[x, y]) + { + var rect = new RectangleF( + (float)(offsetX + x * moduleSize), + (float)(offsetY + y * moduleSize), + (float)moduleSize, + (float)moduleSize); + g.FillRectangle(brush, rect); + } + } + } + } + + return bitmap; + } + + /// + /// 生成二维码并保存到文件 + /// + /// 内容 + /// 文件路径 + /// 配置 + public static void GenerateToFile(string content, string filePath, QrCodeOptions? options = null) + { + using var bitmap = Generate(content, options); + var format = GetImageFormat(filePath); + bitmap.Save(filePath, format); + } + + /// + /// 生成二维码并返回Base64字符串 + /// + /// 内容 + /// 配置 + /// 图像格式 + /// Base64字符串 + public static string GenerateToBase64(string content, QrCodeOptions? options = null, ImageFormat? format = null) + { + using var bitmap = Generate(content, options); + using var ms = new MemoryStream(); + bitmap.Save(ms, format ?? ImageFormat.Png); + return Convert.ToBase64String(ms.ToArray()); + } + + /// + /// 生成二维码并返回Data URI + /// + /// 内容 + /// 配置 + /// 图像格式 + /// Data URI字符串 + public static string GenerateToDataUri(string content, QrCodeOptions? options = null, ImageFormat? format = null) + { + format ??= ImageFormat.Png; + var base64 = GenerateToBase64(content, options, format); + var mimeType = GetMimeType(format); + return $"data:{mimeType};base64,{base64}"; + } + + /// + /// 生成带Logo的二维码 + /// + /// 内容 + /// Logo路径 + /// 配置 + /// Logo占二维码比例(0.1-0.3) + /// 二维码图像 + public static Bitmap GenerateWithLogo(string content, string logoPath, QrCodeOptions? options = null, double logoRatio = 0.2) + { + using var logo = Image.FromFile(logoPath); + return GenerateWithLogo(content, logo, options, logoRatio); + } + + /// + /// 生成带Logo的二维码 + /// + /// 内容 + /// Logo图像 + /// 配置 + /// Logo占二维码比例 + /// 二维码图像 + public static Bitmap GenerateWithLogo(string content, Image logo, QrCodeOptions? options = null, double logoRatio = 0.2) + { + var bitmap = Generate(content, options); + options ??= new QrCodeOptions(); + + using (var g = Graphics.FromImage(bitmap)) + { + var logoSize = (int)(Math.Min(options.Width, options.Height) * logoRatio); + var logoX = (options.Width - logoSize) / 2; + var logoY = (options.Height - logoSize) / 2; + + // 绘制白色背景 + g.FillRectangle(Brushes.White, logoX - 2, logoY - 2, logoSize + 4, logoSize + 4); + + // 绘制Logo + g.DrawImage(logo, logoX, logoY, logoSize, logoSize); + } + + return bitmap; + } + + #endregion + + #region QR码矩阵生成 + + private static bool[,] GenerateQrMatrix(byte[] data, QrCodeErrorCorrection errorCorrection) + { + // 简化实现:生成基础QR码矩阵 + // 实际应用中建议使用专门的QR码库如 QRCoder 或 ZXing + + // 确定版本(基于数据长度) + int version = DetermineVersion(data.Length, errorCorrection); + + // 计算模块数(版本1为21,每增加1版本增加4个模块) + int size = 21 + (version - 1) * 4; + + // 创建矩阵 + var matrix = new bool[size, size]; + + // 添加定位图案 + AddFinderPatterns(matrix, size); + + // 添加对齐图案(版本2及以上) + if (version >= 2) + { + AddAlignmentPatterns(matrix, size, version); + } + + // 添加时序图案 + AddTimingPatterns(matrix, size); + + // 添加格式信息区域 + AddFormatInfoAreas(matrix, size); + + // 填充数据(简化实现) + FillData(matrix, size, data); + + return matrix; + } + + private static int DetermineVersion(int dataLength, QrCodeErrorCorrection errorCorrection) + { + // 简化版本确定 + var capacities = new int[] { 17, 32, 53, 78, 106, 134, 154, 192, 230, 271 }; + var reduction = errorCorrection switch + { + QrCodeErrorCorrection.Low => 0, + QrCodeErrorCorrection.Medium => 1, + QrCodeErrorCorrection.Quartile => 2, + QrCodeErrorCorrection.High => 3, + _ => 1 + }; + + for (int v = 0; v < capacities.Length; v++) + { + var capacity = capacities[v] - reduction * (v + 1) * 5; + if (capacity >= dataLength) + return v + 1; + } + + return 10; // 最大版本 + } + + private static void AddFinderPatterns(bool[,] matrix, int size) + { + int patternSize = 7; + + // 左上角 + DrawFinderPattern(matrix, 0, 0); + // 右上角 + DrawFinderPattern(matrix, size - patternSize, 0); + // 左下角 + DrawFinderPattern(matrix, 0, size - patternSize); + } + + private static void DrawFinderPattern(bool[,] matrix, int startX, int startY) + { + // 外框(7x7黑) + for (int i = 0; i < 7; i++) + { + for (int j = 0; j < 7; j++) + { + if (i == 0 || i == 6 || j == 0 || j == 6 || + (i >= 2 && i <= 4 && j >= 2 && j <= 4)) + { + matrix[startX + i, startY + j] = true; + } + } + } + } + + private static void AddAlignmentPatterns(bool[,] matrix, int size, int version) + { + // 简化:仅在右下角添加一个对齐图案 + if (version >= 2) + { + var positions = GetAlignmentPositions(version); + foreach (var pos in positions) + { + if (pos.X > 7 && pos.Y > 7) // 避免与定位图案重叠 + { + DrawAlignmentPattern(matrix, pos.X - 2, pos.Y - 2); + } + } + } + } + + private static List<(int X, int Y)> GetAlignmentPositions(int version) + { + var positions = new List<(int, int)>(); + int size = 21 + (version - 1) * 4; + + if (version >= 2) + { + positions.Add((size - 7, size - 7)); + } + + return positions; + } + + private static void DrawAlignmentPattern(bool[,] matrix, int startX, int startY) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + if (i == 0 || i == 4 || j == 0 || j == 4 || (i == 2 && j == 2)) + { + matrix[startX + i, startY + j] = true; + } + } + } + } + + private static void AddTimingPatterns(bool[,] matrix, int size) + { + // 水平时序图案 + for (int i = 8; i < size - 8; i++) + { + matrix[i, 6] = i % 2 == 0; + } + + // 垂直时序图案 + for (int i = 8; i < size - 8; i++) + { + matrix[6, i] = i % 2 == 0; + } + } + + private static void AddFormatInfoAreas(bool[,] matrix, int size) + { + // 格式信息区域标记(简化) + for (int i = 0; i < 9; i++) + { + if (i != 6) // 避开时序图案 + { + matrix[8, i] = false; + matrix[i, 8] = false; + } + } + } + + private static void FillData(bool[,] matrix, int size, byte[] data) + { + // 简化数据填充 + int dataIndex = 0; + bool upward = true; + + for (int col = size - 1; col >= 0; col -= 2) + { + if (col == 6) col--; // 跳过时序图案列 + + for (int i = 0; i < size; i++) + { + int row = upward ? size - 1 - i : i; + + for (int c = 0; c < 2; c++) + { + int currentCol = col - c; + + if (!IsReserved(currentCol, row, size)) + { + if (dataIndex < data.Length * 8) + { + int byteIndex = dataIndex / 8; + int bitIndex = 7 - (dataIndex % 8); + matrix[currentCol, row] = ((data[byteIndex] >> bitIndex) & 1) == 1; + dataIndex++; + } + else + { + matrix[currentCol, row] = false; + } + } + } + } + + upward = !upward; + } + } + + private static bool IsReserved(int x, int y, int size) + { + // 检查定位图案区域 + if ((x < 9 && y < 9) || (x < 9 && y >= size - 8) || (x >= size - 8 && y < 9)) + return true; + + // 检查时序图案 + if (x == 6 || y == 6) + return true; + + return false; + } + + #endregion + + #region 辅助方法 + + private static ImageFormat GetImageFormat(string filePath) + { + var ext = Path.GetExtension(filePath).ToLower(); + return ext switch + { + ".jpg" or ".jpeg" => ImageFormat.Jpeg, + ".gif" => ImageFormat.Gif, + ".bmp" => ImageFormat.Bmp, + ".tiff" => ImageFormat.Tiff, + _ => ImageFormat.Png + }; + } + + private static string GetMimeType(ImageFormat format) + { + if (format.Equals(ImageFormat.Jpeg)) + return "image/jpeg"; + if (format.Equals(ImageFormat.Gif)) + return "image/gif"; + if (format.Equals(ImageFormat.Bmp)) + return "image/bmp"; + if (format.Equals(ImageFormat.Tiff)) + return "image/tiff"; + return "image/png"; + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/ResourceUtil.cs b/EasyTool.Core/IOCategory/ResourceUtil.cs new file mode 100644 index 0000000..c62802a --- /dev/null +++ b/EasyTool.Core/IOCategory/ResourceUtil.cs @@ -0,0 +1,415 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 嵌入资源工具类 + /// 提供程序集嵌入资源的读取和管理功能 + /// + public static class ResourceUtil + { + #region 读取嵌入资源 + + /// + /// 读取嵌入资源为字符串 + /// + /// 资源名称 + /// 程序集(默认为调用程序集) + /// 资源内容 + public static string ReadAsString(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + return reader.ReadToEnd(); + } + + /// + /// 读取嵌入资源为字节数组 + /// + /// 资源名称 + /// 程序集 + /// 资源数据 + public static byte[] ReadAsBytes(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var memoryStream = new MemoryStream(); + stream.CopyTo(memoryStream); + return memoryStream.ToArray(); + } + + /// + /// 获取嵌入资源流 + /// + /// 资源名称 + /// 程序集 + /// 资源流 + public static Stream? GetStream(string resourceName, Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + + // 尝试精确匹配 + var stream = assembly.GetManifestResourceStream(resourceName); + if (stream != null) + return stream; + + // 尝试模糊匹配 + var names = assembly.GetManifestResourceNames(); + var matchedName = names.FirstOrDefault(n => + n.Equals(resourceName, StringComparison.OrdinalIgnoreCase) || + n.EndsWith("." + resourceName, StringComparison.OrdinalIgnoreCase)); + + if (matchedName != null) + return assembly.GetManifestResourceStream(matchedName); + + return null; + } + + /// + /// 异步读取嵌入资源为字符串 + /// + /// 资源名称 + /// 程序集 + /// 资源内容 + public static async Task ReadAsStringAsync(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + return await reader.ReadToEndAsync(); + } + + /// + /// 异步读取嵌入资源为字节数组 + /// + /// 资源名称 + /// 程序集 + /// 资源数据 + public static async Task ReadAsBytesAsync(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream); + return memoryStream.ToArray(); + } + + #endregion + + #region 读取行 + + /// + /// 读取嵌入资源的所有行 + /// + /// 资源名称 + /// 程序集 + /// 行列表 + public static List ReadAllLines(string resourceName, Assembly? assembly = null) + { + var content = ReadAsString(resourceName, assembly); + return content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList(); + } + + /// + /// 逐行读取嵌入资源 + /// + /// 资源名称 + /// 程序集 + /// 行枚举 + public static IEnumerable ReadLines(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + while (!reader.EndOfStream) + { + yield return reader.ReadLine() ?? string.Empty; + } + } + + #endregion + + #region 资源信息 + + /// + /// 获取程序集中所有嵌入资源名称 + /// + /// 程序集 + /// 资源名称列表 + public static string[] GetResourceNames(Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + return assembly.GetManifestResourceNames(); + } + + /// + /// 检查嵌入资源是否存在 + /// + /// 资源名称 + /// 程序集 + /// 是否存在 + public static bool Exists(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + return stream != null; + } + + /// + /// 获取嵌入资源信息 + /// + /// 资源名称 + /// 程序集 + /// 资源信息 + public static ResourceInfo? GetResourceInfo(string resourceName, Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + + var names = assembly.GetManifestResourceNames(); + var matchedName = names.FirstOrDefault(n => + n.Equals(resourceName, StringComparison.OrdinalIgnoreCase) || + n.EndsWith("." + resourceName, StringComparison.OrdinalIgnoreCase)); + + if (matchedName == null) + return null; + + using var stream = assembly.GetManifestResourceStream(matchedName); + if (stream == null) + return null; + + return new ResourceInfo + { + FullName = matchedName, + ShortName = GetShortName(matchedName), + Size = stream.Length, + Assembly = assembly + }; + } + + private static string GetShortName(string fullName) + { + var parts = fullName.Split('.'); + if (parts.Length >= 2) + { + return parts[parts.Length - 1]; + } + return fullName; + } + + #endregion + + #region 提取资源 + + /// + /// 将嵌入资源提取到文件 + /// + /// 资源名称 + /// 输出文件路径 + /// 程序集 + public static void ExtractToFile(string resourceName, string outputPath, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + var directory = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var fileStream = File.Create(outputPath); + stream.CopyTo(fileStream); + } + + /// + /// 异步将嵌入资源提取到文件 + /// + /// 资源名称 + /// 输出文件路径 + /// 程序集 + public static async Task ExtractToFileAsync(string resourceName, string outputPath, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + var directory = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var fileStream = File.Create(outputPath); + await stream.CopyToAsync(fileStream); + } + + /// + /// 将所有嵌入资源提取到目录 + /// + /// 输出目录 + /// 程序集 + /// 资源名称过滤器 + /// 提取的文件数量 + public static int ExtractAllToDirectory(string outputDirectory, Assembly? assembly = null, Func? filter = null) + { + assembly ??= Assembly.GetCallingAssembly(); + var names = assembly.GetManifestResourceNames(); + int count = 0; + + if (!Directory.Exists(outputDirectory)) + { + Directory.CreateDirectory(outputDirectory); + } + + foreach (var name in names) + { + if (filter != null && !filter(name)) + continue; + + var shortName = GetShortName(name); + var outputPath = Path.Combine(outputDirectory, shortName); + + try + { + ExtractToFile(name, outputPath, assembly); + count++; + } + catch + { + // 忽略提取失败的资源 + } + } + + return count; + } + + #endregion + + #region 类型化资源 + + /// + /// 读取嵌入资源并反序列化为对象 + /// + /// 对象类型 + /// 资源名称 + /// 程序集 + /// 反序列化的对象 + public static T? ReadAsJson(string resourceName, Assembly? assembly = null) + { + var json = ReadAsString(resourceName, assembly); + return System.Text.Json.JsonSerializer.Deserialize(json); + } + + /// + /// 异步读取嵌入资源并反序列化为对象 + /// + /// 对象类型 + /// 资源名称 + /// 程序集 + /// 反序列化的对象 + public static async Task ReadAsJsonAsync(string resourceName, Assembly? assembly = null) + { + var json = await ReadAsStringAsync(resourceName, assembly); + return System.Text.Json.JsonSerializer.Deserialize(json); + } + + #endregion + + #region 快捷方法 + + /// + /// 从当前程序集读取嵌入资源 + /// + /// 资源名称 + /// 资源内容 + public static string Read(string resourceName) + { + return ReadAsString(resourceName, Assembly.GetCallingAssembly()); + } + + /// + /// 从指定类型所在程序集读取嵌入资源 + /// + /// 类型 + /// 资源名称 + /// 资源内容 + public static string ReadFromAssemblyOf(string resourceName) + { + return ReadAsString(resourceName, typeof(T).Assembly); + } + + /// + /// 从类型所在程序集读取嵌入资源(资源名基于类型命名空间) + /// + /// 类型 + /// 相对资源名称 + /// 资源内容 + public static string ReadRelativeToType(Type type, string relativeName) + { + var ns = type.Namespace ?? string.Empty; + var resourceName = string.IsNullOrEmpty(ns) ? relativeName : $"{ns}.{relativeName}"; + return ReadAsString(resourceName, type.Assembly); + } + + /// + /// 从类型所在程序集读取嵌入资源 + /// + /// 类型 + /// 相对资源名称 + /// 资源内容 + public static string ReadRelativeToType(string relativeName) + { + return ReadRelativeToType(typeof(T), relativeName); + } + + #endregion + } + + /// + /// 资源信息 + /// + public class ResourceInfo + { + /// + /// 资源完整名称 + /// + public string? FullName { get; set; } + + /// + /// 资源短名称 + /// + public string? ShortName { get; set; } + + /// + /// 资源大小(字节) + /// + public long Size { get; set; } + + /// + /// 所在程序集 + /// + public Assembly? Assembly { get; set; } + + public override string ToString() + { + return $"{ShortName} ({Size} bytes)"; + } + } +} diff --git a/EasyTool.Core/IOCategory/SerializeUtil.cs b/EasyTool.Core/IOCategory/SerializeUtil.cs new file mode 100644 index 0000000..1336374 --- /dev/null +++ b/EasyTool.Core/IOCategory/SerializeUtil.cs @@ -0,0 +1,202 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 序列化工具类 + /// + public static class SerializeUtil + { + #region 二进制序列化 + + /// + /// 二进制序列化 + /// + public static byte[] Serialize(T obj) + { + using var stream = new MemoryStream(); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + formatter.Serialize(stream, obj!); + return stream.ToArray(); + } + + /// + /// 二进制反序列化 + /// + public static T? Deserialize(byte[] data) + { + using var stream = new MemoryStream(data); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + return (T?)formatter.Deserialize(stream); + } + + /// + /// 序列化到文件 + /// + public static void SerializeToFile(T obj, string filePath) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + using var stream = File.Create(filePath); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + formatter.Serialize(stream, obj!); + } + + /// + /// 从文件反序列化 + /// + public static T? DeserializeFromFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + return (T?)formatter.Deserialize(stream); + } + + #endregion + + #region Base64 + + /// + /// 对象转Base64字符串 + /// + public static string ToBase64(T obj) + { + var data = Serialize(obj); + return Convert.ToBase64String(data); + } + + /// + /// Base64字符串转对象 + /// + public static T? FromBase64(string base64) + { + var data = Convert.FromBase64String(base64); + return Deserialize(data); + } + + #endregion + + #region JSON + + /// + /// JSON序列化 + /// + public static string ToJson(T obj, bool indented = false) + { + var options = new System.Text.Json.JsonSerializerOptions + { + WriteIndented = indented, + PropertyNamingPolicy = null + }; + return System.Text.Json.JsonSerializer.Serialize(obj, options); + } + + /// + /// JSON反序列化 + /// + public static T? FromJson(string json) + { + var options = new System.Text.Json.JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + return System.Text.Json.JsonSerializer.Deserialize(json, options); + } + + /// + /// JSON序列化到文件 + /// + public static void ToJsonFile(T obj, string filePath, bool indented = false) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var json = ToJson(obj, indented); + File.WriteAllText(filePath, json, Encoding.UTF8); + } + + /// + /// 从文件反序列化JSON + /// + public static T? FromJsonFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + var json = File.ReadAllText(filePath, Encoding.UTF8); + return FromJson(json); + } + + #endregion + + #region 深拷贝 + + /// + /// 深拷贝对象 + /// + public static T DeepClone(T obj) + { + if (obj == null) + return default!; + + // 使用JSON序列化实现深拷贝 + var json = ToJson(obj); + return FromJson(json)!; + } + + /// + /// 尝试深拷贝 + /// + public static bool TryDeepClone(T obj, out T? clone) + { + try + { + clone = DeepClone(obj); + return true; + } + catch + { + clone = default; + return false; + } + } + + #endregion + + #region 对象比较 + + /// + /// 比较两个对象是否相等(通过序列化比较) + /// + public static bool Equals(T obj1, T obj2) + { + if (obj1 == null && obj2 == null) + return true; + if (obj1 == null || obj2 == null) + return false; + + return ToJson(obj1) == ToJson(obj2); + } + + /// + /// 获取对象的哈希值 + /// + public static int GetHashCode(T obj) + { + if (obj == null) + return 0; + + return ToJson(obj).GetHashCode(); + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/TempFileUtil.cs b/EasyTool.Core/IOCategory/TempFileUtil.cs new file mode 100644 index 0000000..7944390 --- /dev/null +++ b/EasyTool.Core/IOCategory/TempFileUtil.cs @@ -0,0 +1,267 @@ +using System; +using System.IO; + +namespace EasyTool.IOCategory +{ + /// + /// 临时文件工具类 + /// + public static class TempFileUtil + { + private static readonly object _lock = new(); + private static readonly string _tempDirectory = Path.Combine(Path.GetTempPath(), "EasyTool_Temp"); + + /// + /// 临时目录 + /// + public static string TempDirectory + { + get + { + if (!Directory.Exists(_tempDirectory)) + Directory.CreateDirectory(_tempDirectory); + return _tempDirectory; + } + } + + /// + /// 创建临时文件 + /// + public static string CreateTempFile(string? extension = null, string? prefix = null) + { + var fileName = $"{prefix ?? "temp"}_{Guid.NewGuid():N}{extension ?? ".tmp"}"; + var filePath = Path.Combine(TempDirectory, fileName); + File.Create(filePath).Dispose(); + return filePath; + } + + /// + /// 创建临时目录 + /// + public static string CreateTempDirectory(string? prefix = null) + { + var dirName = $"{prefix ?? "temp"}_{Guid.NewGuid():N}"; + var dirPath = Path.Combine(TempDirectory, dirName); + Directory.CreateDirectory(dirPath); + return dirPath; + } + + /// + /// 创建临时文件并写入内容 + /// + public static string CreateTempFileWithContent(string content, string? extension = null, string? prefix = null) + { + var filePath = CreateTempFile(extension, prefix); + File.WriteAllText(filePath, content); + return filePath; + } + + /// + /// 创建临时文件并写入二进制内容 + /// + public static string CreateTempFileWithBytes(byte[] bytes, string? extension = null, string? prefix = null) + { + var filePath = CreateTempFile(extension, prefix); + File.WriteAllBytes(filePath, bytes); + return filePath; + } + + /// + /// 删除临时文件 + /// + public static bool DeleteTempFile(string filePath) + { + try + { + if (File.Exists(filePath)) + { + File.Delete(filePath); + return true; + } + return false; + } + catch + { + return false; + } + } + + /// + /// 删除临时目录 + /// + public static bool DeleteTempDirectory(string dirPath) + { + try + { + if (Directory.Exists(dirPath)) + { + Directory.Delete(dirPath, true); + return true; + } + return false; + } + catch + { + return false; + } + } + + /// + /// 清理所有临时文件 + /// + public static void CleanupAll() + { + lock (_lock) + { + if (Directory.Exists(_tempDirectory)) + { + try + { + Directory.Delete(_tempDirectory, true); + } + catch + { + // 忽略清理错误 + } + } + } + } + + /// + /// 清理过期的临时文件 + /// + public static void CleanupExpired(TimeSpan expiration) + { + if (!Directory.Exists(_tempDirectory)) + return; + + var cutoff = DateTime.Now - expiration; + + foreach (var file in Directory.GetFiles(_tempDirectory)) + { + try + { + if (File.GetCreationTime(file) < cutoff) + File.Delete(file); + } + catch + { + // 忽略单个文件删除错误 + } + } + + foreach (var dir in Directory.GetDirectories(_tempDirectory)) + { + try + { + if (Directory.GetCreationTime(dir) < cutoff) + Directory.Delete(dir, true); + } + catch + { + // 忽略单个目录删除错误 + } + } + } + + /// + /// 获取临时文件大小 + /// + public static long GetTempDirectorySize() + { + if (!Directory.Exists(_tempDirectory)) + return 0; + + long size = 0; + foreach (var file in Directory.GetFiles(_tempDirectory, "*", SearchOption.AllDirectories)) + { + try + { + size += new FileInfo(file).Length; + } + catch + { + // 忽略单个文件错误 + } + } + return size; + } + + /// + /// 获取临时文件数量 + /// + public static int GetTempFileCount() + { + if (!Directory.Exists(_tempDirectory)) + return 0; + + return Directory.GetFiles(_tempDirectory, "*", SearchOption.AllDirectories).Length; + } + } + + /// + /// 临时文件自动清理器 + /// + public class TempFileScope : IDisposable + { + private string? _filePath; + private string? _directoryPath; + private bool _disposed; + private readonly bool _isDirectory; + + /// + /// 创建临时文件作用域 + /// + public TempFileScope(string? extension = null, string? prefix = null) + { + _filePath = TempFileUtil.CreateTempFile(extension, prefix); + _isDirectory = false; + } + + private TempFileScope(bool isDirectory, string? prefix) + { + if (isDirectory) + { + _directoryPath = TempFileUtil.CreateTempDirectory(prefix); + _isDirectory = true; + } + else + { + _filePath = TempFileUtil.CreateTempFile(null, prefix); + _isDirectory = false; + } + } + + /// + /// 创建临时目录作用域 + /// + public static TempFileScope CreateDirectoryScope(string? prefix = null) + { + return new TempFileScope(true, prefix); + } + + /// + /// 临时文件路径 + /// + public string FilePath => _filePath ?? throw new InvalidOperationException("这不是文件作用域"); + + /// + /// 临时目录路径 + /// + public string DirectoryPath => _directoryPath ?? throw new InvalidOperationException("这不是目录作用域"); + + public void Dispose() + { + if (_disposed) + return; + + if (_filePath != null) + TempFileUtil.DeleteTempFile(_filePath); + + if (_directoryPath != null) + TempFileUtil.DeleteTempDirectory(_directoryPath); + + _disposed = true; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs b/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs new file mode 100644 index 0000000..13fa42a --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs @@ -0,0 +1,539 @@ +using System; +using System.Net.NetworkInformation; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// MongoDB ObjectId 生成器 + /// ObjectId 是一个 12 字节的唯一标识符,由时间戳、机器标识、进程 ID 和计数器组成 + /// + public static class ObjectIdUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly byte[] _machineId; + private static readonly byte[] _processId; + private static int _counter; + private static readonly object _counterLock = new(); + + static ObjectIdUtil() + { + _machineId = GetMachineId(); + _processId = GetProcessId(); + _counter = GetRandomCounter(); + } + + private const int ObjectIdLength = 12; + private const int TimestampLength = 4; + private const int MachineIdLength = 3; + private const int ProcessIdLength = 2; + private const int CounterLength = 3; + + /// + /// 生成新的 ObjectId + /// + /// ObjectId 字节数组 + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 ObjectId + /// + /// 时间戳 + /// ObjectId 字节数组 + public static byte[] Generate(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + // 写入时间戳(4字节,大端序) + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + + // 写入机器标识(3字节) + Buffer.BlockCopy(_machineId, 0, objectId, 4, MachineIdLength); + + // 写入进程 ID(2字节) + Buffer.BlockCopy(_processId, 0, objectId, 7, ProcessIdLength); + + // 写入计数器(3字节,大端序) + int counter; + lock (_counterLock) + { + counter = _counter++; + if (_counter > 0xFFFFFF) + { + _counter = GetRandomCounter(); + } + } + + objectId[9] = (byte)(counter >> 16); + objectId[10] = (byte)(counter >> 8); + objectId[11] = (byte)counter; + + return objectId; + } + + /// + /// 生成新的 ObjectId 字符串 + /// + /// ObjectId 字符串(24字符十六进制) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 生成指定时间的 ObjectId 字符串 + /// + /// 时间戳 + /// ObjectId 字符串(24字符十六进制) + public static string GenerateString(DateTimeOffset timestamp) + { + return Encode(Generate(timestamp)); + } + + /// + /// 将 ObjectId 字节数组编码为十六进制字符串 + /// + /// ObjectId 字节数组 + /// ObjectId 十六进制字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(bytes)); + } + + var hex = new StringBuilder(24); + foreach (var b in bytes) + { + hex.AppendFormat("{0:x2}", b); + } + return hex.ToString(); + } + + /// + /// 将十六进制字符串解码为 ObjectId 字节数组 + /// + /// ObjectId 十六进制字符串 + /// ObjectId 字节数组 + public static byte[] Decode(string objectId) + { + if (string.IsNullOrEmpty(objectId) || objectId.Length != 24) + { + throw new ArgumentException("ObjectId 字符串长度必须为 24", nameof(objectId)); + } + + var bytes = new byte[ObjectIdLength]; + for (int i = 0; i < ObjectIdLength; i++) + { + var hex = objectId.Substring(i * 2, 2); + bytes[i] = Convert.ToByte(hex, 16); + } + return bytes; + } + + /// + /// 从 ObjectId 提取时间戳 + /// + /// ObjectId 字节数组 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + var timestampSec = (objectId[0] << 24) | + (objectId[1] << 16) | + (objectId[2] << 8) | + objectId[3]; + + return DateTimeOffset.FromUnixTimeSeconds(timestampSec); + } + + /// + /// 从 ObjectId 字符串提取时间戳 + /// + /// ObjectId 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string objectId) + { + return ExtractTimestamp(Decode(objectId)); + } + + /// + /// 从 ObjectId 提取机器标识 + /// + /// ObjectId 字节数组 + /// 机器标识(十六进制字符串) + public static string ExtractMachineId(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return $"{objectId[4]:x2}{objectId[5]:x2}{objectId[6]:x2}"; + } + + /// + /// 从 ObjectId 提取进程 ID + /// + /// ObjectId 字节数组 + /// 进程 ID + public static int ExtractProcessId(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return (objectId[7] << 8) | objectId[8]; + } + + /// + /// 从 ObjectId 提取计数器 + /// + /// ObjectId 字节数组 + /// 计数器值 + public static int ExtractCounter(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return (objectId[9] << 16) | (objectId[10] << 8) | objectId[11]; + } + + /// + /// 验证 ObjectId 字符串是否有效 + /// + /// ObjectId 字符串 + /// 是否有效 + public static bool IsValid(string objectId) + { + if (string.IsNullOrEmpty(objectId) || objectId.Length != 24) + { + return false; + } + + foreach (var c in objectId) + { + if (!Uri.IsHexDigit(c)) + { + return false; + } + } + + return true; + } + + /// + /// 比较两个 ObjectId 的大小 + /// + /// 第一个 ObjectId + /// 第二个 ObjectId + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + /// + /// 获取 ObjectId 的信息 + /// + /// ObjectId 字符串 + /// ObjectId 信息 + public static ObjectIdInfo GetInfo(string objectId) + { + var bytes = Decode(objectId); + return new ObjectIdInfo + { + Timestamp = ExtractTimestamp(bytes), + MachineId = ExtractMachineId(bytes), + ProcessId = ExtractProcessId(bytes), + Counter = ExtractCounter(bytes) + }; + } + + /// + /// 生成最小 ObjectId(指定时间) + /// + /// 时间戳 + /// 最小 ObjectId 字符串 + public static string Min(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + // 其余部分为 0 + + return Encode(objectId); + } + + /// + /// 生成最大 ObjectId(指定时间) + /// + /// 时间戳 + /// 最大 ObjectId 字符串 + public static string Max(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + // 其余部分为 0xFF + for (int i = 4; i < ObjectIdLength; i++) + { + objectId[i] = 0xFF; + } + + return Encode(objectId); + } + + #region 私有方法 + + private static byte[] GetMachineId() + { + var machineId = new byte[MachineIdLength]; + + try + { + // 尝试使用网络接口的 MAC 地址 + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + foreach (var ni in interfaces) + { + if (ni.OperationalStatus == OperationalStatus.Up && + ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) + { + var mac = ni.GetPhysicalAddress().GetAddressBytes(); + if (mac.Length >= MachineIdLength) + { + Buffer.BlockCopy(mac, 0, machineId, 0, MachineIdLength); + return machineId; + } + } + } + } + catch + { + // 忽略异常,使用随机值 + } + + // 使用机器名哈希 + var machineName = Environment.MachineName; + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(machineName)); + Buffer.BlockCopy(hash, 0, machineId, 0, MachineIdLength); + + return machineId; + } + + private static byte[] GetProcessId() + { + var processId = new byte[ProcessIdLength]; +#if NETSTANDARD2_1 + var pid = System.Diagnostics.Process.GetCurrentProcess().Id; +#else + var pid = System.Diagnostics.Process.GetCurrentProcess().Id; +#endif + + processId[0] = (byte)(pid >> 8); + processId[1] = (byte)pid; + + return processId; + } + + private static int GetRandomCounter() + { + var counterBytes = new byte[CounterLength]; + _rng.GetBytes(counterBytes); + return (counterBytes[0] << 16) | (counterBytes[1] << 8) | counterBytes[2]; + } + + #endregion + } + + /// + /// ObjectId 结构体 + /// + public readonly struct ObjectId : IComparable, IEquatable + { + private readonly byte[] _bytes; + + /// + /// 创建 ObjectId + /// + /// 字节数组 + public ObjectId(byte[] bytes) + { + if (bytes == null || bytes.Length != 12) + { + throw new ArgumentException("ObjectId 字节数组长度必须为 12", nameof(bytes)); + } + _bytes = bytes; + } + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => ObjectIdUtil.ExtractTimestamp(_bytes); + + /// + /// 字节数组 + /// + public byte[] ToByteArray() => (byte[])_bytes.Clone(); + + /// + /// 转换为字符串 + /// + public override string ToString() => ObjectIdUtil.Encode(_bytes); + + /// + /// 比较大小 + /// + public int CompareTo(ObjectId other) + { + for (int i = 0; i < 12; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return _bytes[i].CompareTo(other._bytes[i]); + } + } + return 0; + } + + /// + /// 判断相等 + /// + public bool Equals(ObjectId other) + { + for (int i = 0; i < 12; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return false; + } + } + return true; + } + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) + { + return obj is ObjectId other && Equals(other); + } + + /// + /// 获取哈希码 + /// + public override int GetHashCode() + { + var hash = 0; + for (int i = 0; i < 12; i++) + { + hash = (hash << 2) ^ _bytes[i]; + } + return hash; + } + + /// + /// 等于运算符 + /// + public static bool operator ==(ObjectId left, ObjectId right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(ObjectId left, ObjectId right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(ObjectId left, ObjectId right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(ObjectId left, ObjectId right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(ObjectId left, ObjectId right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(ObjectId left, ObjectId right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 ObjectId + /// + public static ObjectId NewObjectId() => new ObjectId(ObjectIdUtil.Generate()); + + /// + /// 解析字符串 + /// + public static ObjectId Parse(string objectId) => new ObjectId(ObjectIdUtil.Decode(objectId)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string objectId, out ObjectId result) + { + if (ObjectIdUtil.IsValid(objectId)) + { + result = Parse(objectId); + return true; + } + result = default; + return false; + } + } + + /// + /// ObjectId 信息 + /// + public class ObjectIdInfo + { + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp { get; set; } + + /// + /// 机器标识 + /// + public string MachineId { get; set; } = string.Empty; + + /// + /// 进程 ID + /// + public int ProcessId { get; set; } + + /// + /// 计数器 + /// + public int Counter { get; set; } + } +} diff --git a/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs b/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs new file mode 100644 index 0000000..7a4d2c5 --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// 短 ID 生成器,生成简洁的唯一标识符 + /// + public static class ShortIdUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + + // 去除易混淆字符(0OIl1)的字符集 + private static readonly char[] DefaultChars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789".ToCharArray(); + private static readonly char[] AlphanumericChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); + private static readonly char[] LowercaseChars = "abcdefghjkmnpqrstuvwxyz23456789".ToCharArray(); + private static readonly char[] UppercaseChars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".ToCharArray(); + private static readonly char[] NumericChars = "0123456789".ToCharArray(); + + /// + /// 生成默认短 ID(8字符) + /// + /// 短 ID + public static string Generate() + { + return Generate(8); + } + + /// + /// 生成指定长度的短 ID + /// + /// 长度(建议 6-16) + /// 短 ID + public static string Generate(int length) + { + return Generate(length, ShortIdOptions.Default); + } + + /// + /// 使用指定选项生成短 ID + /// + /// 长度 + /// 选项 + /// 短 ID + public static string Generate(int length, ShortIdOptions options) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于 0"); + } + + var chars = GetChars(options); + var result = new char[length]; + var bytes = new byte[length]; + + _rng.GetBytes(bytes); + + for (int i = 0; i < length; i++) + { + result[i] = chars[bytes[i] % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成带前缀的短 ID + /// + /// 前缀 + /// ID 部分长度 + /// 带前缀的短 ID + public static string GenerateWithPrefix(string prefix, int length = 8) + { + return $"{prefix}{Generate(length)}"; + } + + /// + /// 生成小写短 ID + /// + /// 长度 + /// 小写短 ID + public static string GenerateLowercase(int length = 8) + { + return Generate(length, ShortIdOptions.Lowercase); + } + + /// + /// 生成大写短 ID + /// + /// 长度 + /// 大写短 ID + public static string GenerateUppercase(int length = 8) + { + return Generate(length, ShortIdOptions.Uppercase); + } + + /// + /// 生成纯数字短 ID + /// + /// 长度 + /// 纯数字短 ID + public static string GenerateNumeric(int length = 8) + { + return Generate(length, ShortIdOptions.Numeric); + } + + /// + /// 生成基于时间的短 ID(可排序) + /// + /// 随机部分长度 + /// 基于时间的短 ID + public static string GenerateTimeBased(int randomLength = 4) + { + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var timestampBase36 = ToBase36(timestamp); + var randomPart = Generate(randomLength, ShortIdOptions.Lowercase); + return $"{timestampBase36}{randomPart}"; + } + + /// + /// 生成邀请码风格短 ID(易于阅读和朗读) + /// + /// 分段数 + /// 每段长度 + /// 邀请码风格短 ID + public static string GenerateInviteCode(int segments = 3, int segmentLength = 4) + { + var parts = new string[segments]; + for (int i = 0; i < segments; i++) + { + parts[i] = Generate(segmentLength, ShortIdOptions.Uppercase); + } + return string.Join("-", parts); + } + + /// + /// 生成优化的 URL 短链接 ID + /// + /// 长度(建议 6-8) + /// URL 友好的短 ID + public static string GenerateUrlFriendly(int length = 6) + { + return Generate(length, ShortIdOptions.Lowercase); + } + + /// + /// 生成订单号风格短 ID + /// + /// 前缀(如 ORD) + /// 订单号风格短 ID + public static string GenerateOrderNumber(string prefix = "ORD") + { + var date = DateTime.UtcNow.ToString("yyyyMMdd"); + var random = GenerateNumeric(6); + return $"{prefix}{date}{random}"; + } + + /// + /// 生成优惠券码风格短 ID + /// + /// 长度 + /// 优惠券码 + public static string GenerateCouponCode(int length = 12) + { + return Generate(length, ShortIdOptions.Uppercase); + } + + /// + /// 从整数生成短 ID + /// + /// 数字 + /// 短 ID + public static string FromNumber(long number) + { + return ToBase62(number); + } + + /// + /// 将短 ID 转换为整数 + /// + /// 短 ID + /// 数字 + public static long ToNumber(string shortId) + { + return FromBase62(shortId); + } + + /// + /// 生成唯一短 ID(带校验) + /// + /// 长度(不含校验位) + /// 带校验位的短 ID + public static string GenerateWithChecksum(int length = 7) + { + var id = Generate(length); + var checksum = ComputeChecksum(id); + return $"{id}{checksum}"; + } + + /// + /// 验证带校验位的短 ID + /// + /// 带校验位的短 ID + /// 是否有效 + public static bool ValidateChecksum(string shortId) + { + if (string.IsNullOrEmpty(shortId) || shortId.Length < 2) + { + return false; + } + + var id = shortId[..^1]; + var checksum = shortId[^1]; + return checksum == ComputeChecksum(id); + } + + #region 私有方法 + + private static char[] GetChars(ShortIdOptions options) + { + return options switch + { + ShortIdOptions.Lowercase => LowercaseChars, + ShortIdOptions.Uppercase => UppercaseChars, + ShortIdOptions.Numeric => NumericChars, + ShortIdOptions.Alphanumeric => AlphanumericChars, + _ => DefaultChars + }; + } + + private static string ToBase36(long number) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyz"; + if (number < 0) + { + throw new ArgumentException("数字必须为非负数", nameof(number)); + } + + if (number == 0) + { + return "0"; + } + + var result = new StringBuilder(); + while (number > 0) + { + result.Insert(0, chars[(int)(number % 36)]); + number /= 36; + } + return result.ToString(); + } + + private static string ToBase62(long number) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (number < 0) + { + throw new ArgumentException("数字必须为非负数", nameof(number)); + } + + if (number == 0) + { + return "0"; + } + + var result = new StringBuilder(); + while (number > 0) + { + result.Insert(0, chars[(int)(number % 62)]); + number /= 62; + } + return result.ToString(); + } + + private static long FromBase62(string str) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + long result = 0; + + foreach (var c in str) + { + result = result * 62 + chars.IndexOf(c); + } + + return result; + } + + private static char ComputeChecksum(string id) + { + var chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; + int sum = 0; + foreach (var c in id) + { + sum += chars.IndexOf(char.ToUpper(c)); + } + return chars[sum % chars.Length]; + } + + #endregion + } + + /// + /// 短 ID 生成选项 + /// + public enum ShortIdOptions + { + /// + /// 默认(去除易混淆字符) + /// + Default, + + /// + /// 小写字母和数字 + /// + Lowercase, + + /// + /// 大写字母和数字 + /// + Uppercase, + + /// + /// 纯数字 + /// + Numeric, + + /// + /// 完整字母数字(包含易混淆字符) + /// + Alphanumeric + } + + /// + /// 短 ID 生成器配置 + /// + public class ShortIdGenerator + { + private readonly int _length; + private readonly ShortIdOptions _options; + private readonly string? _prefix; + private readonly string? _suffix; + + /// + /// 创建短 ID 生成器 + /// + /// 长度 + /// 选项 + /// 前缀 + /// 后缀 + public ShortIdGenerator(int length = 8, ShortIdOptions options = ShortIdOptions.Default, string? prefix = null, string? suffix = null) + { + _length = length; + _options = options; + _prefix = prefix; + _suffix = suffix; + } + + /// + /// 生成短 ID + /// + /// 短 ID + public string Generate() + { + var id = ShortIdUtil.Generate(_length, _options); + return $"{_prefix}{id}{_suffix}"; + } + + /// + /// 批量生成短 ID + /// + /// 数量 + /// 短 ID 列表 + public string[] GenerateMany(int count) + { + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 创建默认生成器 + /// + public static ShortIdGenerator Default => new(); + + /// + /// 创建 URL 友好生成器 + /// + public static ShortIdGenerator UrlFriendly => new(6, ShortIdOptions.Lowercase); + + /// + /// 创建邀请码生成器 + /// + public static ShortIdGenerator InviteCode => new(12, ShortIdOptions.Uppercase); + + /// + /// 创建订单号生成器 + /// + public static ShortIdGenerator OrderNumber => new(10, ShortIdOptions.Numeric, "ORD"); + } +} diff --git a/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs b/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs new file mode 100644 index 0000000..e1018c7 --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs @@ -0,0 +1,237 @@ +using System; + +namespace EasyTool.IdentifierCategory +{ + /// + /// 雪花算法ID生成器 + /// + public class SnowflakeIdGenerator + { + private long _workerId; + private long _datacenterId; + private readonly object _lock = new(); + private long _sequence; + private long _lastTimestamp; + + /// + /// 机器ID位数 + /// + public const int WorkerIdBits = 5; + + /// + /// 数据中心ID位数 + /// + public const int DatacenterIdBits = 5; + + /// + /// 序列号位数 + /// + public const int SequenceBits = 12; + + /// + /// 时间戳位数 + /// + public const int TimestampBits = 41; + + /// + /// 机器ID最大值 + /// + public const long MaxWorkerId = (1L << WorkerIdBits) - 1; + + /// + /// 数据中心ID最大值 + /// + public const long MaxDatacenterId = (1L << DatacenterIdBits) - 1; + + /// + /// 序列号最大值 + /// + public const long MaxSequence = (1L << SequenceBits) - 1; + + /// + /// 时间戳最大值 + /// + public const long MaxTimestamp = (1L << TimestampBits) - 1; + + /// + /// 起始时间戳(2020-01-01 00:00:00) + /// + public const long Twepoch = 1577808000000L; + + /// + /// 时间戳左移位数 + /// + public const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits; + + /// + /// 数据中心ID左移位数 + /// + public const int DatacenterIdShift = SequenceBits + WorkerIdBits; + + /// + /// 工作机器ID左移位数 + /// + public const int WorkerIdShift = SequenceBits; + + /// + /// 自定义时间戳生成函数 + /// + public Func? CustomTimestampFunc { get; set; } + + /// + /// 获取或设置工作机器ID + /// + public long WorkerId + { + get => _workerId; + set + { + if (value < 0 || value > MaxWorkerId) + throw new ArgumentException($"工作机器ID必须在 0 到 {MaxWorkerId} 之间"); + + _workerId = value; + } + } + + /// + /// 获取或设置数据中心ID + /// + public long DatacenterId + { + get => _datacenterId; + set + { + if (value < 0 || value > MaxDatacenterId) + throw new ArgumentException($"数据中心ID必须在 0 到 {MaxDatacenterId} 之间"); + + _datacenterId = value; + } + } + + /// + /// 创建雪花算法ID生成器 + /// + /// 工作机器ID(0-31) + /// 数据中心ID(0-31) + /// 初始序列号 + public SnowflakeIdGenerator(long workerId, long datacenterId, long sequence = 0L) + { + if (workerId > MaxWorkerId || workerId < 0) + throw new ArgumentException($"工作机器ID必须在 0 到 {MaxWorkerId} 之间"); + + if (datacenterId > MaxDatacenterId || datacenterId < 0) + throw new ArgumentException($"数据中心ID必须在 0 到 {MaxDatacenterId} 之间"); + + _workerId = workerId; + _datacenterId = datacenterId; + _sequence = sequence; + _lastTimestamp = -1L; + } + + /// + /// 创建雪花算法ID生成器(使用默认配置) + /// + public SnowflakeIdGenerator() : this(1, 1, 0) { } + + /// + /// 生成下一个唯一ID + /// + /// 唯一ID + public virtual long NextId() + { + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + // 时钟回拨检测 + if (_lastTimestamp == timestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + // 序列号溢出,等待下一毫秒 + timestamp = GetCurrentTimestamp(); + _sequence = 0; + } + } + else + { + _sequence = 0; + } + + _lastTimestamp = timestamp; + + return ((timestamp - Twepoch) << TimestampLeftShift) + | (_datacenterId << DatacenterIdShift) + | (_workerId << WorkerIdShift) + | _sequence; + } + } + + /// + /// 获取当前时间戳(毫秒) + /// + private long GetCurrentTimestamp() + { + if (CustomTimestampFunc != null) + { + return CustomTimestampFunc(); + } + return (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond); + } + + /// + /// 解析ID + /// + /// 雪花ID + /// 解析结果 + public static SnowflakeIdInfo Parse(long id) + { + var timestamp = (id >> TimestampLeftShift) + Twepoch; + var datacenterId = (id >> DatacenterIdShift) & MaxDatacenterId; + var workerId = (id >> WorkerIdShift) & MaxWorkerId; + var sequence = id & MaxSequence; + + return new SnowflakeIdInfo + { + Timestamp = timestamp, + DataCenterId = datacenterId, + WorkerId = workerId, + Sequence = sequence + }; + } + } + + /// + /// 雪花ID信息 + /// + public class SnowflakeIdInfo + { + /// + /// 时间戳 + /// + public long Timestamp { get; set; } + + /// + /// 数据中心ID + /// + public long DataCenterId { get; set; } + + /// + /// 工作机器ID + /// + public long WorkerId { get; set; } + + /// + /// 序列号 + /// + public long Sequence { get; set; } + + /// + /// 创建时间 + /// + public DateTime DateTime => DateTime.FromBinary(Timestamp); + + } + +} diff --git a/EasyTool.Core/IdentifierCategory/TSIDUtil.cs b/EasyTool.Core/IdentifierCategory/TSIDUtil.cs new file mode 100644 index 0000000..6ebc5ff --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/TSIDUtil.cs @@ -0,0 +1,503 @@ +using System; +using System.Security.Cryptography; +using System.Threading; + +namespace EasyTool.IdentifierCategory +{ + /// + /// TSID(Time-Sorted Identifier)生成器 + /// 生成可按时间排序的唯一标识符,支持分布式环境 + /// + public static class TsidUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] EncodingChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); + private static readonly byte[] DecodingMap = BuildDecodingMap(); + + private const int TimestampBits = 42; + private const int NodeIdBits = 8; + private const int SequenceBits = 14; + + private const long MaxTimestamp = (1L << TimestampBits) - 1; + private const int MaxNodeId = (1 << NodeIdBits) - 1; + private const int MaxSequence = (1 << SequenceBits) - 1; + + private static readonly long CustomEpoch = new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds(); + private static int _nodeId; + private static int _sequence; + private static long _lastTimestamp = -1; + private static readonly object _lock = new(); + + static TsidUtil() + { + _nodeId = GenerateNodeId(); + _sequence = GetRandomSequence(); + } + + /// + /// 生成新的 TSID + /// + /// TSID 长整型 + public static long Generate() + { + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + // 序列号溢出,等待下一毫秒 + timestamp = WaitNextMillis(timestamp); + } + } + else + { + _sequence = GetRandomSequence(); + } + + _lastTimestamp = timestamp; + + return ((timestamp & MaxTimestamp) << (NodeIdBits + SequenceBits)) + | ((long)_nodeId << SequenceBits) + | _sequence; + } + } + + /// + /// 生成新的 TSID 字符串 + /// + /// TSID 字符串(13字符) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 使用指定节点 ID 生成 TSID + /// + /// 节点 ID(0-255) + /// TSID 长整型 + public static long Generate(int nodeId) + { + if (nodeId < 0 || nodeId > MaxNodeId) + { + throw new ArgumentOutOfRangeException(nameof(nodeId), $"节点 ID 必须在 0 到 {MaxNodeId} 之间"); + } + + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + timestamp = WaitNextMillis(timestamp); + } + } + else + { + _sequence = GetRandomSequence(); + } + + _lastTimestamp = timestamp; + + return ((timestamp & MaxTimestamp) << (NodeIdBits + SequenceBits)) + | ((long)nodeId << SequenceBits) + | _sequence; + } + } + + /// + /// 使用指定节点 ID 生成 TSID 字符串 + /// + /// 节点 ID + /// TSID 字符串 + public static string GenerateString(int nodeId) + { + return Encode(Generate(nodeId)); + } + + /// + /// 将 TSID 长整型编码为字符串 + /// + /// TSID 值 + /// TSID 字符串 + public static string Encode(long tsid) + { + var chars = new char[13]; + + for (int i = 12; i >= 0; i--) + { + chars[i] = EncodingChars[(int)(tsid & 0x1F)]; + tsid >>= 5; + } + + return new string(chars); + } + + /// + /// 将 TSID 字符串解码为长整型 + /// + /// TSID 字符串 + /// TSID 长整型 + public static long Decode(string tsid) + { + if (string.IsNullOrEmpty(tsid) || tsid.Length != 13) + { + throw new ArgumentException("TSID 字符串长度必须为 13", nameof(tsid)); + } + + long result = 0; + + foreach (var c in tsid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + throw new ArgumentException($"无效的 TSID 字符: {c}", nameof(tsid)); + } + + result = (result << 5) | DecodingMap[c]; + } + + return result; + } + + /// + /// 从 TSID 提取时间戳 + /// + /// TSID 值 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(long tsid) + { + var timestamp = tsid >> (NodeIdBits + SequenceBits); + var milliseconds = timestamp + CustomEpoch; + return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); + } + + /// + /// 从 TSID 字符串提取时间戳 + /// + /// TSID 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string tsid) + { + return ExtractTimestamp(Decode(tsid)); + } + + /// + /// 从 TSID 提取节点 ID + /// + /// TSID 值 + /// 节点 ID + public static int ExtractNodeId(long tsid) + { + return (int)((tsid >> SequenceBits) & MaxNodeId); + } + + /// + /// 从 TSID 字符串提取节点 ID + /// + /// TSID 字符串 + /// 节点 ID + public static int ExtractNodeId(string tsid) + { + return ExtractNodeId(Decode(tsid)); + } + + /// + /// 从 TSID 提取序列号 + /// + /// TSID 值 + /// 序列号 + public static int ExtractSequence(long tsid) + { + return (int)(tsid & MaxSequence); + } + + /// + /// 从 TSID 字符串提取序列号 + /// + /// TSID 字符串 + /// 序列号 + public static int ExtractSequence(string tsid) + { + return ExtractSequence(Decode(tsid)); + } + + /// + /// 验证 TSID 字符串是否有效 + /// + /// TSID 字符串 + /// 是否有效 + public static bool IsValid(string tsid) + { + if (string.IsNullOrEmpty(tsid) || tsid.Length != 13) + { + return false; + } + + foreach (var c in tsid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + return false; + } + } + + return true; + } + + /// + /// 设置节点 ID + /// + /// 节点 ID(0-255) + public static void SetNodeId(int nodeId) + { + if (nodeId < 0 || nodeId > MaxNodeId) + { + throw new ArgumentOutOfRangeException(nameof(nodeId), $"节点 ID 必须在 0 到 {MaxNodeId} 之间"); + } + _nodeId = nodeId; + } + + /// + /// 获取当前节点 ID + /// + /// 节点 ID + public static int GetNodeId() + { + return _nodeId; + } + + /// + /// 比较两个 TSID 的大小 + /// + /// 第一个 TSID + /// 第二个 TSID + /// 比较结果 + public static int Compare(long a, long b) + { + return a.CompareTo(b); + } + + /// + /// 比较两个 TSID 字符串的大小 + /// + /// 第一个 TSID 字符串 + /// 第二个 TSID 字符串 + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + #region 私有方法 + + private static long GetCurrentTimestamp() + { + var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var timestamp = currentTime - CustomEpoch; + + if (timestamp < 0) + { + throw new InvalidOperationException("当前时间早于自定义纪元时间"); + } + + if (timestamp > MaxTimestamp) + { + throw new OverflowException("时间戳溢出"); + } + + return timestamp; + } + + private static long WaitNextMillis(long currentTimestamp) + { + var timestamp = GetCurrentTimestamp(); + while (timestamp <= currentTimestamp) + { + Thread.SpinWait(10); + timestamp = GetCurrentTimestamp(); + } + return timestamp; + } + + private static int GenerateNodeId() + { + var bytes = new byte[1]; + _rng.GetBytes(bytes); + return bytes[0]; + } + + private static int GetRandomSequence() + { + var bytes = new byte[2]; + _rng.GetBytes(bytes); + return ((bytes[0] << 6) | (bytes[1] >> 2)) & MaxSequence; + } + + private static byte[] BuildDecodingMap() + { + var map = new byte[256]; + Array.Fill(map, (byte)0xFF); + + for (int i = 0; i < EncodingChars.Length; i++) + { + map[EncodingChars[i]] = (byte)i; + } + + // 支持小写字母 + map['a'] = map['A']; + map['b'] = map['B']; + map['c'] = map['C']; + map['d'] = map['D']; + map['e'] = map['E']; + map['f'] = map['F']; + map['g'] = map['G']; + map['h'] = map['H']; + map['i'] = map['I']; + map['j'] = map['J']; + map['k'] = map['K']; + map['l'] = map['L']; + map['m'] = map['M']; + map['n'] = map['N']; + map['o'] = map['O']; + map['p'] = map['P']; + map['q'] = map['Q']; + map['r'] = map['R']; + map['s'] = map['S']; + map['t'] = map['T']; + map['u'] = map['U']; + map['v'] = map['V']; + map['w'] = map['W']; + map['x'] = map['X']; + map['y'] = map['Y']; + map['z'] = map['Z']; + + return map; + } + + #endregion + } + + /// + /// TSID 结构体 + /// + public readonly struct Tsid : IComparable, IEquatable + { + private readonly long _value; + + /// + /// 创建 TSID + /// + /// TSID 值 + public Tsid(long value) + { + _value = value; + } + + /// + /// TSID 值 + /// + public long Value => _value; + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => TsidUtil.ExtractTimestamp(_value); + + /// + /// 节点 ID + /// + public int NodeId => TsidUtil.ExtractNodeId(_value); + + /// + /// 序列号 + /// + public int Sequence => TsidUtil.ExtractSequence(_value); + + /// + /// 转换为字符串 + /// + public override string ToString() => TsidUtil.Encode(_value); + + /// + /// 比较大小 + /// + public int CompareTo(Tsid other) => _value.CompareTo(other._value); + + /// + /// 判断相等 + /// + public bool Equals(Tsid other) => _value == other._value; + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) => obj is Tsid other && Equals(other); + + /// + /// 获取哈希码 + /// + public override int GetHashCode() => _value.GetHashCode(); + + /// + /// 等于运算符 + /// + public static bool operator ==(Tsid left, Tsid right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(Tsid left, Tsid right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(Tsid left, Tsid right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(Tsid left, Tsid right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(Tsid left, Tsid right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(Tsid left, Tsid right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 TSID + /// + public static Tsid NewTsid() => new Tsid(TsidUtil.Generate()); + + /// + /// 解析字符串 + /// + public static Tsid Parse(string tsid) => new Tsid(TsidUtil.Decode(tsid)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string tsid, out Tsid result) + { + if (TsidUtil.IsValid(tsid)) + { + result = Parse(tsid); + return true; + } + result = default; + return false; + } + } +} diff --git a/EasyTool.Core/IdentifierCategory/ULIDUtil.cs b/EasyTool.Core/IdentifierCategory/ULIDUtil.cs new file mode 100644 index 0000000..b431bdc --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ULIDUtil.cs @@ -0,0 +1,460 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// ULID(Universally Unique Lexicographically Sortable Identifier)生成器 + /// ULID 是一种可排序的唯一标识符,由 48 位时间戳和 80 位随机数组成 + /// + public static class UlidUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] EncodingChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".ToCharArray(); + private static readonly byte[] DecodingMap = BuildDecodingMap(); + + private const int TimestampLength = 6; + private const int RandomnessLength = 10; + private const int UlidLength = 16; + private const int StringLength = 26; + + /// + /// 生成新的 ULID + /// + /// ULID 字节数组 + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 ULID + /// + /// 时间戳 + /// ULID 字节数组 + public static byte[] Generate(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + // 写入时间戳(6字节,大端序) + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + + // 写入随机数(10字节) + _rng.GetBytes(ulid, TimestampLength, RandomnessLength); + + return ulid; + } + + /// + /// 生成新的 ULID 字符串 + /// + /// ULID 字符串(26字符) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 生成指定时间的 ULID 字符串 + /// + /// 时间戳 + /// ULID 字符串(26字符) + public static string GenerateString(DateTimeOffset timestamp) + { + return Encode(Generate(timestamp)); + } + + /// + /// 生成 ULID 结构体 + /// + /// ULID 结构体 + public static Ulid GenerateUlid() + { + return new Ulid(Generate()); + } + + /// + /// 生成指定时间的 ULID 结构体 + /// + /// 时间戳 + /// ULID 结构体 + public static Ulid GenerateUlid(DateTimeOffset timestamp) + { + return new Ulid(Generate(timestamp)); + } + + /// + /// 将 ULID 字节数组编码为字符串 + /// + /// ULID 字节数组 + /// ULID 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != UlidLength) + { + throw new ArgumentException($"ULID 字节数组长度必须为 {UlidLength}", nameof(bytes)); + } + + var result = new char[StringLength]; + var buffer = 0; + var bufferBits = 0; + var index = StringLength - 1; + + for (int i = UlidLength - 1; i >= 0; i--) + { + buffer = (buffer << 8) | bytes[i]; + bufferBits += 8; + + while (bufferBits >= 5) + { + result[index--] = EncodingChars[(buffer >> (bufferBits - 5)) & 0x1F]; + bufferBits -= 5; + } + } + + if (bufferBits > 0) + { + result[index] = EncodingChars[buffer & 0x1F]; + } + + return new string(result); + } + + /// + /// 将 ULID 字符串解码为字节数组 + /// + /// ULID 字符串 + /// ULID 字节数组 + public static byte[] Decode(string ulid) + { + if (string.IsNullOrEmpty(ulid) || ulid.Length != StringLength) + { + throw new ArgumentException($"ULID 字符串长度必须为 {StringLength}", nameof(ulid)); + } + + var result = new byte[UlidLength]; + var buffer = 0; + var bufferBits = 0; + var index = UlidLength - 1; + + for (int i = StringLength - 1; i >= 0; i--) + { + var c = ulid[i]; + var value = DecodingMap[c]; + + if (value == 0xFF) + { + throw new ArgumentException($"无效的 ULID 字符: {c}", nameof(ulid)); + } + + buffer = (buffer << 5) | value; + bufferBits += 5; + + while (bufferBits >= 8) + { + result[index--] = (byte)((buffer >> (bufferBits - 8)) & 0xFF); + bufferBits -= 8; + } + } + + return result; + } + + /// + /// 从 ULID 提取时间戳 + /// + /// ULID 字节数组 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(byte[] ulid) + { + if (ulid == null || ulid.Length != UlidLength) + { + throw new ArgumentException($"ULID 字节数组长度必须为 {UlidLength}", nameof(ulid)); + } + + var timestampMs = ((long)ulid[0] << 40) | + ((long)ulid[1] << 32) | + ((long)ulid[2] << 24) | + ((long)ulid[3] << 16) | + ((long)ulid[4] << 8) | + ulid[5]; + + return DateTimeOffset.FromUnixTimeMilliseconds(timestampMs); + } + + /// + /// 从 ULID 字符串提取时间戳 + /// + /// ULID 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string ulid) + { + return ExtractTimestamp(Decode(ulid)); + } + + /// + /// 验证 ULID 字符串是否有效 + /// + /// ULID 字符串 + /// 是否有效 + public static bool IsValid(string ulid) + { + if (string.IsNullOrEmpty(ulid) || ulid.Length != StringLength) + { + return false; + } + + foreach (var c in ulid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + return false; + } + } + + return true; + } + + /// + /// 比较两个 ULID 的大小 + /// + /// 第一个 ULID + /// 第二个 ULID + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + /// + /// 获取最小 ULID(指定时间) + /// + /// 时间戳 + /// 最小 ULID 字符串 + public static string Min(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + // 随机部分全部为 0 + + return Encode(ulid); + } + + /// + /// 获取最大 ULID(指定时间) + /// + /// 时间戳 + /// 最大 ULID 字符串 + public static string Max(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + // 随机部分全部为 0xFF + for (int i = TimestampLength; i < UlidLength; i++) + { + ulid[i] = 0xFF; + } + + return Encode(ulid); + } + + private static byte[] BuildDecodingMap() + { + var map = new byte[256]; + Array.Fill(map, (byte)0xFF); + + for (int i = 0; i < EncodingChars.Length; i++) + { + map[EncodingChars[i]] = (byte)i; + } + + // 支持小写字母 + map['a'] = map['A']; + map['b'] = map['B']; + map['c'] = map['C']; + map['d'] = map['D']; + map['e'] = map['E']; + map['f'] = map['F']; + map['g'] = map['G']; + map['h'] = map['H']; + map['j'] = map['J']; + map['k'] = map['K']; + map['m'] = map['M']; + map['n'] = map['N']; + map['p'] = map['P']; + map['q'] = map['Q']; + map['r'] = map['R']; + map['s'] = map['S']; + map['t'] = map['T']; + map['v'] = map['V']; + map['w'] = map['W']; + map['x'] = map['X']; + map['y'] = map['Y']; + map['z'] = map['Z']; + + return map; + } + } + + /// + /// ULID 结构体 + /// + public readonly struct Ulid : IComparable, IEquatable + { + private readonly byte[] _bytes; + + /// + /// 创建 ULID + /// + /// 字节数组 + public Ulid(byte[] bytes) + { + if (bytes == null || bytes.Length != 16) + { + throw new ArgumentException("ULID 字节数组长度必须为 16", nameof(bytes)); + } + _bytes = bytes; + } + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => UlidUtil.ExtractTimestamp(_bytes); + + /// + /// 字节数组 + /// + public byte[] ToByteArray() => (byte[])_bytes.Clone(); + + /// + /// 转换为字符串 + /// + public override string ToString() => UlidUtil.Encode(_bytes); + + /// + /// 比较大小 + /// + public int CompareTo(Ulid other) + { + for (int i = 0; i < 16; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return _bytes[i].CompareTo(other._bytes[i]); + } + } + return 0; + } + + /// + /// 判断相等 + /// + public bool Equals(Ulid other) + { + for (int i = 0; i < 16; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return false; + } + } + return true; + } + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) + { + return obj is Ulid other && Equals(other); + } + + /// + /// 获取哈希码 + /// + public override int GetHashCode() + { + var hash = 0; + for (int i = 0; i < 16; i++) + { + hash = (hash << 2) ^ _bytes[i]; + } + return hash; + } + + /// + /// 等于运算符 + /// + public static bool operator ==(Ulid left, Ulid right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(Ulid left, Ulid right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(Ulid left, Ulid right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(Ulid left, Ulid right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(Ulid left, Ulid right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(Ulid left, Ulid right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 ULID + /// + public static Ulid NewUlid() => UlidUtil.GenerateUlid(); + + /// + /// 解析字符串 + /// + public static Ulid Parse(string ulid) => new Ulid(UlidUtil.Decode(ulid)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string ulid, out Ulid result) + { + if (UlidUtil.IsValid(ulid)) + { + result = Parse(ulid); + return true; + } + result = default; + return false; + } + } +} diff --git a/EasyTool.Core/MathCategory/AngleUtil.cs b/EasyTool.Core/MathCategory/AngleUtil.cs new file mode 100644 index 0000000..3a1893d --- /dev/null +++ b/EasyTool.Core/MathCategory/AngleUtil.cs @@ -0,0 +1,312 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 角度运算工具类 + /// + public static class AngleUtil + { + /// + /// 弧度转角度 + /// + public static double RadiansToDegrees(double radians) + { + return radians * (180.0 / Math.PI); + } + + /// + /// 角度转弧度 + /// + public static double DegreesToRadians(double degrees) + { + return degrees * (Math.PI / 180.0); + } + + /// + /// 角度转弧度 + /// + public static Angle Radians(double radians) + { + return Angle.FromRadians(radians); + } + + /// + /// 角度转弧度 + /// + public static Angle Degrees(double degrees) + { + return Angle.FromDegrees(degrees); + } + + /// + /// 规范化角度到 [0, 360) 范围 + /// + public static double NormalizeDegrees(double degrees) + { + degrees %= 360; + return degrees < 0 ? degrees + 360 : degrees; + } + + /// + /// 规范化弧度到 [0, 2π) 范围 + /// + public static double NormalizeRadians(double radians) + { + radians %= (2 * Math.PI); + return radians < 0 ? radians + (2 * Math.PI) : radians; + } + + /// + /// 角度加法 + /// + public static double AddDegrees(double a, double b) + { + return NormalizeDegrees(a + b); + } + + /// + /// 角度减法 + /// + public static double SubtractDegrees(double a, double b) + { + return NormalizeDegrees(a - b); + } + + /// + /// 计算两个角度的最小差值 + /// + public static double MinimumAngleDifference(double a, double b) + { + var diff = NormalizeDegrees(a - b); + return diff > 180 ? 360 - diff : diff; + } + + /// + /// 角度线性插值 + /// + public static double LerpDegrees(double from, double to, double t) + { + var diff = to - from; + if (diff > 180) diff -= 360; + else if (diff < -180) diff += 360; + return NormalizeDegrees(from + diff * t); + } + + /// + /// 度分秒转十进制度 + /// + public static double DmsToDecimal(int degrees, int minutes, double seconds) + { + var sign = degrees < 0 ? -1 : 1; + return sign * (Math.Abs(degrees) + minutes / 60.0 + seconds / 3600.0); + } + + /// + /// 十进制度转度分秒 + /// + public static (int Degrees, int Minutes, double Seconds) DecimalToDms(double decimalDegrees) + { + var sign = decimalDegrees < 0 ? -1 : 1; + decimalDegrees = Math.Abs(decimalDegrees); + + var degrees = (int)decimalDegrees; + var minutes = (int)((decimalDegrees - degrees) * 60); + var seconds = ((decimalDegrees - degrees) * 60 - minutes) * 60; + + return (sign * degrees, minutes, seconds); + } + + /// + /// 格式化度分秒 + /// + public static string FormatDms(double decimalDegrees) + { + var (degrees, minutes, seconds) = DecimalToDms(decimalDegrees); + return $"{degrees}°{minutes}'{seconds:F2}″"; + } + + /// + /// 解析度分秒字符串 + /// + public static double ParseDms(string dms) + { + var parts = dms.Split(new[] { '°', '\'', '″', '"' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 0) + throw new ArgumentException("无效的度分秒格式"); + + var degrees = double.Parse(parts[0].Trim()); + var minutes = parts.Length > 1 ? double.Parse(parts[1].Trim()) : 0; + var seconds = parts.Length > 2 ? double.Parse(parts[2].Trim()) : 0; + + return DmsToDecimal((int)degrees, (int)minutes, seconds); + } + + #region 三角函数(角度版本) + + /// + /// 正弦(角度) + /// + public static double Sin(double degrees) + { + return Math.Sin(DegreesToRadians(degrees)); + } + + /// + /// 余弦(角度) + /// + public static double Cos(double degrees) + { + return Math.Cos(DegreesToRadians(degrees)); + } + + /// + /// 正切(角度) + /// + public static double Tan(double degrees) + { + return Math.Tan(DegreesToRadians(degrees)); + } + + /// + /// 反正弦(返回角度) + /// + public static double Asin(double value) + { + return RadiansToDegrees(Math.Asin(value)); + } + + /// + /// 反余弦(返回角度) + /// + public static double Acos(double value) + { + return RadiansToDegrees(Math.Acos(value)); + } + + /// + /// 反正切(返回角度) + /// + public static double Atan(double value) + { + return RadiansToDegrees(Math.Atan(value)); + } + + /// + /// 反正切2(返回角度) + /// + public static double Atan2(double y, double x) + { + return RadiansToDegrees(Math.Atan2(y, x)); + } + + #endregion + } + + /// + /// 角度结构 + /// + public readonly struct Angle : IEquatable, IComparable + { + private readonly double _degrees; + + private Angle(double degrees) + { + _degrees = AngleUtil.NormalizeDegrees(degrees); + } + + /// + /// 角度值 + /// + public double Degrees => _degrees; + + /// + /// 弧度值 + /// + public double Radians => AngleUtil.DegreesToRadians(_degrees); + + /// + /// 从度创建角度 + /// + public static Angle FromDegrees(double degrees) => new Angle(degrees); + + /// + /// 从弧度创建角度 + /// + public static Angle FromRadians(double radians) => new Angle(AngleUtil.RadiansToDegrees(radians)); + + /// + /// 从度分秒创建角度 + /// + public static Angle FromDms(int degrees, int minutes, double seconds) + => new Angle(AngleUtil.DmsToDecimal(degrees, minutes, seconds)); + + /// + /// 零度 + /// + public static Angle Zero => new Angle(0); + + /// + /// 直角 (90°) + /// + public static Angle Right => new Angle(90); + + /// + /// 平角 (180°) + /// + public static Angle Straight => new Angle(180); + + /// + /// 周角 (360°) + /// + public static Angle Full => new Angle(360); + + #region 运算符 + + public static Angle operator +(Angle a, Angle b) => new Angle(a._degrees + b._degrees); + public static Angle operator -(Angle a, Angle b) => new Angle(a._degrees - b._degrees); + public static Angle operator *(Angle a, double scalar) => new Angle(a._degrees * scalar); + public static Angle operator *(double scalar, Angle a) => new Angle(a._degrees * scalar); + public static Angle operator /(Angle a, double scalar) => new Angle(a._degrees / scalar); + public static Angle operator -(Angle a) => new Angle(-a._degrees); + public static bool operator ==(Angle a, Angle b) => a.Equals(b); + public static bool operator !=(Angle a, Angle b) => !a.Equals(b); + public static bool operator <(Angle a, Angle b) => a._degrees < b._degrees; + public static bool operator >(Angle a, Angle b) => a._degrees > b._degrees; + public static bool operator <=(Angle a, Angle b) => a._degrees <= b._degrees; + public static bool operator >=(Angle a, Angle b) => a._degrees >= b._degrees; + + public static implicit operator double(Angle angle) => angle._degrees; + + #endregion + + #region 三角函数 + + public double Sin() => AngleUtil.Sin(_degrees); + public double Cos() => AngleUtil.Cos(_degrees); + public double Tan() => AngleUtil.Tan(_degrees); + + #endregion + + #region 接口实现 + + public bool Equals(Angle other) => Math.Abs(_degrees - other._degrees) < double.Epsilon; + public override bool Equals(object? obj) => obj is Angle other && Equals(other); + public override int GetHashCode() => _degrees.GetHashCode(); + public int CompareTo(Angle other) => _degrees.CompareTo(other._degrees); + + public override string ToString() => $"{_degrees:F2}°"; + + public string ToString(string format) + { + if (format == "DMS") + { + var (degrees, minutes, seconds) = AngleUtil.DecimalToDms(_degrees); + return $"{degrees}°{minutes}'{seconds:F2}″"; + } + return $"{_degrees.ToString(format)}°"; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/CombinatoricsUtil.cs b/EasyTool.Core/MathCategory/CombinatoricsUtil.cs new file mode 100644 index 0000000..de61a79 --- /dev/null +++ b/EasyTool.Core/MathCategory/CombinatoricsUtil.cs @@ -0,0 +1,576 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 组合数学工具类 + /// 提供排列、组合等计算功能 + /// + public static class CombinatoricsUtil + { + #region 阶乘 + + /// + /// 计算阶乘 + /// + /// 非负整数 + /// n! + public static long Factorial(int n) + { + if (n < 0) + throw new ArgumentException("阶乘只能计算非负整数"); + + if (n <= 1) + return 1; + + long result = 1; + + for (int i = 2; i <= n; i++) + { + result *= i; + } + + return result; + } + + /// + /// 计算大数阶乘 + /// + /// 非负整数 + /// n! 的字符串表示 + public static string FactorialBig(int n) + { + if (n < 0) + throw new ArgumentException("阶乘只能计算非负整数"); + + if (n <= 1) + return "1"; + + var result = new List { 1 }; + + for (int i = 2; i <= n; i++) + { + int carry = 0; + + for (int j = 0; j < result.Count; j++) + { + int product = result[j] * i + carry; + result[j] = product % 10; + carry = product / 10; + } + + while (carry > 0) + { + result.Add(carry % 10); + carry /= 10; + } + } + + result.Reverse(); + return string.Join("", result); + } + + #endregion + + #region 排列组合 + + /// + /// 计算排列数 P(n, r) = n! / (n-r)! + /// + /// 总数 + /// 选取数 + /// 排列数 + public static long Permutation(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0) + return 1; + + long result = 1; + + for (int i = n; i > n - r; i--) + { + result *= i; + } + + return result; + } + + /// + /// 计算组合数 C(n, r) = n! / (r! * (n-r)!) + /// + /// 总数 + /// 选取数 + /// 组合数 + public static long Combination(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0 || r == n) + return 1; + + // 使用较小的 r 计算 + r = Math.Min(r, n - r); + + long result = 1; + + for (int i = 0; i < r; i++) + { + result = result * (n - i) / (i + 1); + } + + return result; + } + + /// + /// 计算组合数(大数) + /// + /// 总数 + /// 选取数 + /// 组合数的字符串表示 + public static string CombinationBig(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0 || r == n) + return "1"; + + r = Math.Min(r, n - r); + + var numerator = new List(); + var denominator = new List(); + + for (int i = 0; i < r; i++) + { + numerator.Add(n - i); + denominator.Add(i + 1); + } + + // 约分 + for (int i = 0; i < denominator.Count; i++) + { + for (int j = 0; j < numerator.Count; j++) + { + var gcd = PrimeUtil.Gcd(numerator[j], denominator[i]); + + if (gcd > 1) + { + numerator[j] /= (int)gcd; + denominator[i] /= (int)gcd; + + if (denominator[i] == 1) + break; + } + } + } + + // 计算乘积 + var result = new List { 1 }; + + foreach (var num in numerator) + { + int carry = 0; + + for (int j = 0; j < result.Count; j++) + { + int product = result[j] * num + carry; + result[j] = product % 10; + carry = product / 10; + } + + while (carry > 0) + { + result.Add(carry % 10); + carry /= 10; + } + } + + result.Reverse(); + return string.Join("", result); + } + + #endregion + + #region 排列生成 + + /// + /// 生成所有排列 + /// + /// 元素类型 + /// 元素集合 + /// 所有排列 + public static List> GetAllPermutations(IEnumerable elements) + { + var list = elements.ToList(); + var result = new List>(); + + Permute(list, 0, result); + + return result; + } + + /// + /// 生成指定长度的排列 + /// + /// 元素类型 + /// 元素集合 + /// 排列长度 + /// 所有排列 + public static List> GetPermutations(IEnumerable elements, int length) + { + var list = elements.ToList(); + var result = new List>(); + + if (length > list.Count) + throw new ArgumentException("排列长度不能超过元素数量"); + + GeneratePermutations(list, length, new List(), new bool[list.Count], result); + + return result; + } + + /// + /// 生成下一个排列(字典序) + /// + /// 元素类型 + /// 当前排列(会被修改) + /// 是否存在下一个排列 + public static bool NextPermutation(List elements) where T : IComparable + { + int i = elements.Count - 2; + + while (i >= 0 && elements[i].CompareTo(elements[i + 1]) >= 0) + { + i--; + } + + if (i < 0) + return false; + + int j = elements.Count - 1; + + while (elements[j].CompareTo(elements[i]) <= 0) + { + j--; + } + + // 交换 + var temp = elements[i]; + elements[i] = elements[j]; + elements[j] = temp; + + // 反转 + Reverse(elements, i + 1, elements.Count - 1); + + return true; + } + + #endregion + + #region 组合生成 + + /// + /// 生成所有组合 + /// + /// 元素类型 + /// 元素集合 + /// 所有组合(包括空集) + public static List> GetAllCombinations(IEnumerable elements) + { + var list = elements.ToList(); + var result = new List>(); + + for (int i = 0; i <= list.Count; i++) + { + result.AddRange(GetCombinations(list, i)); + } + + return result; + } + + /// + /// 生成指定长度的组合 + /// + /// 元素类型 + /// 元素集合 + /// 组合长度 + /// 所有组合 + public static List> GetCombinations(IEnumerable elements, int length) + { + var list = elements.ToList(); + var result = new List>(); + + if (length > list.Count) + return result; + + GenerateCombinations(list, length, 0, new List(), result); + + return result; + } + + #endregion + + #region 其他组合数学 + + /// + /// 计算卡特兰数 C_n = C(2n, n) / (n+1) + /// + /// 索引 + /// 第 n 个卡特兰数 + public static long Catalan(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + return Combination(2 * n, n) / (n + 1); + } + + /// + /// 计算第 n 行的杨辉三角 + /// + /// 行号(从0开始) + /// 杨辉三角第 n 行 + public static List PascalRow(int n) + { + var row = new List { 1 }; + + for (int i = 1; i <= n; i++) + { + row.Add(row[i - 1] * (n - i + 1) / i); + } + + return row; + } + + /// + /// 生成杨辉三角 + /// + /// 行数 + /// 杨辉三角 + public static List> PascalTriangle(int rows) + { + var triangle = new List>(); + + for (int i = 0; i < rows; i++) + { + triangle.Add(PascalRow(i)); + } + + return triangle; + } + + /// + /// 计算贝尔数(集合划分数) + /// + /// 索引 + /// 第 n 个贝尔数 + public static long Bell(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + var bell = new long[n + 1, n + 1]; + bell[0, 0] = 1; + + for (int i = 1; i <= n; i++) + { + bell[i, 0] = bell[i - 1, i - 1]; + + for (int j = 1; j <= i; j++) + { + bell[i, j] = bell[i - 1, j - 1] + bell[i, j - 1]; + } + } + + return bell[n, 0]; + } + + /// + /// 计算斯特林数(第二类) + /// + /// 元素数 + /// 集合数 + /// 斯特林数 + public static long StirlingSecond(int n, int k) + { + if (n < 0 || k < 0 || k > n) + throw new ArgumentException("参数无效"); + + if (k == 0) + return n == 0 ? 1 : 0; + + if (k == 1) + return 1; + + if (k == n) + return 1; + + var stirling = new long[n + 1, k + 1]; + stirling[0, 0] = 1; + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= Math.Min(i, k); j++) + { + stirling[i, j] = j * stirling[i - 1, j] + stirling[i - 1, j - 1]; + } + } + + return stirling[n, k]; + } + + /// + /// 计算错排数 D_n + /// + /// 元素数 + /// 错排数 + public static long Derangement(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + if (n == 0) + return 1; + + if (n == 1) + return 0; + + long prev2 = 1, prev1 = 0; + + for (int i = 2; i <= n; i++) + { + long current = (i - 1) * (prev1 + prev2); + prev2 = prev1; + prev1 = current; + } + + return prev1; + } + + /// + /// 计算斐波那契数 + /// + /// 索引 + /// 第 n 个斐波那契数 + public static long Fibonacci(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + if (n <= 1) + return n; + + long prev2 = 0, prev1 = 1; + + for (int i = 2; i <= n; i++) + { + long current = prev1 + prev2; + prev2 = prev1; + prev1 = current; + } + + return prev1; + } + + /// + /// 生成斐波那契数列 + /// + /// 数量 + /// 斐波那契数列 + public static List FibonacciSequence(int count) + { + var sequence = new List(); + + for (int i = 0; i < count; i++) + { + sequence.Add(Fibonacci(i)); + } + + return sequence; + } + + #endregion + + #region 私有方法 + + private static void Permute(List list, int start, List> result) + { + if (start == list.Count - 1) + { + result.Add(new List(list)); + return; + } + + for (int i = start; i < list.Count; i++) + { + Swap(list, start, i); + Permute(list, start + 1, result); + Swap(list, start, i); + } + } + + private static void GeneratePermutations(List list, int length, List current, bool[] used, List> result) + { + if (current.Count == length) + { + result.Add(new List(current)); + return; + } + + for (int i = 0; i < list.Count; i++) + { + if (used[i]) + continue; + + used[i] = true; + current.Add(list[i]); + + GeneratePermutations(list, length, current, used, result); + + current.RemoveAt(current.Count - 1); + used[i] = false; + } + } + + private static void GenerateCombinations(List list, int length, int start, List current, List> result) + { + if (current.Count == length) + { + result.Add(new List(current)); + return; + } + + for (int i = start; i < list.Count; i++) + { + current.Add(list[i]); + GenerateCombinations(list, length, i + 1, current, result); + current.RemoveAt(current.Count - 1); + } + } + + private static void Swap(List list, int i, int j) + { + var temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + + private static void Reverse(List list, int start, int end) + { + while (start < end) + { + Swap(list, start, end); + start++; + end--; + } + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/ComplexUtil.cs b/EasyTool.Core/MathCategory/ComplexUtil.cs new file mode 100644 index 0000000..cfe08b4 --- /dev/null +++ b/EasyTool.Core/MathCategory/ComplexUtil.cs @@ -0,0 +1,330 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 复数结构 + /// + public struct ComplexNumber : IEquatable, IFormattable + { + /// + /// 实部 + /// + public double Real { get; } + + /// + /// 虚部 + /// + public double Imaginary { get; } + + /// + /// 模(绝对值) + /// + public double Magnitude => Math.Sqrt(Real * Real + Imaginary * Imaginary); + + /// + /// 相位角(弧度) + /// + public double Phase => Math.Atan2(Imaginary, Real); + + /// + /// 共轭复数 + /// + public ComplexNumber Conjugate => new ComplexNumber(Real, -Imaginary); + + /// + /// 创建复数 + /// + public ComplexNumber(double real, double imaginary) + { + Real = real; + Imaginary = imaginary; + } + + #region 静态属性 + + /// + /// 零 + /// + public static ComplexNumber Zero => new ComplexNumber(0, 0); + + /// + /// 一 + /// + public static ComplexNumber One => new ComplexNumber(1, 0); + + /// + /// 虚数单位 i + /// + public static ComplexNumber ImaginaryOne => new ComplexNumber(0, 1); + + #endregion + + #region 静态方法 + + /// + /// 从极坐标创建复数 + /// + public static ComplexNumber FromPolarCoordinates(double magnitude, double phase) + { + return new ComplexNumber(magnitude * Math.Cos(phase), magnitude * Math.Sin(phase)); + } + + /// + /// 解析字符串为复数(支持格式: "a+bi", "a-bi", "a", "bi") + /// + public static ComplexNumber Parse(string s) + { + if (string.IsNullOrWhiteSpace(s)) + throw new ArgumentException("字符串不能为空"); + + s = s.Trim().Replace(" ", ""); + + // 尝试解析纯实数 + if (double.TryParse(s, out var real)) + return new ComplexNumber(real, 0); + + // 解析复数 + int iIndex = s.LastIndexOf('i'); + if (iIndex < 0) + throw new FormatException("无效的复数格式"); + + int signIndex = s.LastIndexOfAny(new[] { '+', '-' }, iIndex - 1, iIndex); + + if (signIndex < 0) + { + // 只有虚部 + var imaginaryStr = s.Substring(0, iIndex); + if (string.IsNullOrEmpty(imaginaryStr) || imaginaryStr == "+") + return new ComplexNumber(0, 1); + if (imaginaryStr == "-") + return new ComplexNumber(0, -1); + return new ComplexNumber(0, double.Parse(imaginaryStr)); + } + + var realStr = s.Substring(0, signIndex); + var imagPartStr = s.Substring(signIndex, iIndex - signIndex); + + var realPart = string.IsNullOrEmpty(realStr) ? 0 : double.Parse(realStr); + var imagPart = imagPartStr == "+" || imagPartStr == "" ? 1 : (imagPartStr == "-" ? -1 : double.Parse(imagPartStr)); + + return new ComplexNumber(realPart, imagPart); + } + + /// + /// 尝试解析字符串为复数 + /// + public static bool TryParse(string s, out ComplexNumber result) + { + try + { + result = Parse(s); + return true; + } + catch + { + result = Zero; + return false; + } + } + + #endregion + + #region 运算符重载 + + public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real + b.Real, a.Imaginary + b.Imaginary); + + public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real - b.Real, a.Imaginary - b.Imaginary); + + public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real * b.Real - a.Imaginary * b.Imaginary, a.Real * b.Imaginary + a.Imaginary * b.Real); + + public static ComplexNumber operator /(ComplexNumber a, ComplexNumber b) + { + var denom = b.Real * b.Real + b.Imaginary * b.Imaginary; + if (denom == 0) throw new DivideByZeroException(); + return new ComplexNumber( + (a.Real * b.Real + a.Imaginary * b.Imaginary) / denom, + (a.Imaginary * b.Real - a.Real * b.Imaginary) / denom); + } + + public static ComplexNumber operator -(ComplexNumber a) + => new ComplexNumber(-a.Real, -a.Imaginary); + + public static bool operator ==(ComplexNumber a, ComplexNumber b) + => a.Equals(b); + + public static bool operator !=(ComplexNumber a, ComplexNumber b) + => !a.Equals(b); + + public static implicit operator ComplexNumber(double value) + => new ComplexNumber(value, 0); + + #endregion + + #region 数学运算 + + /// + /// 平方根 + /// + public ComplexNumber Sqrt() + { + var m = Magnitude; + var r = Math.Sqrt((m + Real) / 2); + var i = Math.Sign(Imaginary) * Math.Sqrt((m - Real) / 2); + return new ComplexNumber(r, i); + } + + /// + /// 幂运算 + /// + public ComplexNumber Pow(double exponent) + { + var m = Math.Pow(Magnitude, exponent); + var p = Phase * exponent; + return FromPolarCoordinates(m, p); + } + + /// + /// 幂运算 + /// + public ComplexNumber Pow(ComplexNumber exponent) + { + return (exponent * Log()).Exp(); + } + + /// + /// 自然对数 + /// + public ComplexNumber Log() + { + return new ComplexNumber(Math.Log(Magnitude), Phase); + } + + /// + /// 指数函数 + /// + public ComplexNumber Exp() + { + return FromPolarCoordinates(Math.Exp(Real), Imaginary); + } + + /// + /// 正弦 + /// + public ComplexNumber Sin() + { + return new ComplexNumber( + Math.Sin(Real) * Math.Cosh(Imaginary), + Math.Cos(Real) * Math.Sinh(Imaginary)); + } + + /// + /// 余弦 + /// + public ComplexNumber Cos() + { + return new ComplexNumber( + Math.Cos(Real) * Math.Cosh(Imaginary), + -Math.Sin(Real) * Math.Sinh(Imaginary)); + } + + /// + /// 正切 + /// + public ComplexNumber Tan() + { + return Sin() / Cos(); + } + + #endregion + + #region 接口实现 + + public bool Equals(ComplexNumber other) + => Real.Equals(other.Real) && Imaginary.Equals(other.Imaginary); + + public override bool Equals(object? obj) + => obj is ComplexNumber other && Equals(other); + + public override int GetHashCode() + => HashCode.Combine(Real, Imaginary); + + public string ToString(string? format, IFormatProvider? formatProvider) + => $"({Real.ToString(format, formatProvider)}, {Imaginary.ToString(format, formatProvider)}i)"; + + public override string ToString() + => Imaginary >= 0 ? $"{Real}+{Imaginary}i" : $"{Real}{Imaginary}i"; + + #endregion + } + + /// + /// 复数运算工具类 + /// + public static class ComplexUtil + { + /// + /// 创建复数 + /// + public static ComplexNumber Create(double real, double imaginary) + => new ComplexNumber(real, imaginary); + + /// + /// 从极坐标创建复数 + /// + public static ComplexNumber FromPolar(double magnitude, double phase) + => ComplexNumber.FromPolarCoordinates(magnitude, phase); + + /// + /// 求和 + /// + public static ComplexNumber Sum(params ComplexNumber[] numbers) + { + var sum = ComplexNumber.Zero; + foreach (var n in numbers) + sum += n; + return sum; + } + + /// + /// 求积 + /// + public static ComplexNumber Product(params ComplexNumber[] numbers) + { + var product = ComplexNumber.One; + foreach (var n in numbers) + product *= n; + return product; + } + + /// + /// 平均值 + /// + public static ComplexNumber Average(params ComplexNumber[] numbers) + { + if (numbers.Length == 0) return ComplexNumber.Zero; + return Sum(numbers) / numbers.Length; + } + + /// + /// 欧拉公式 e^(ix) = cos(x) + i*sin(x) + /// + public static ComplexNumber Euler(double x) + => ComplexNumber.FromPolarCoordinates(1, x); + + /// + /// 解析字符串 + /// + public static ComplexNumber Parse(string s) + => ComplexNumber.Parse(s); + + /// + /// 尝试解析 + /// + public static bool TryParse(string s, out ComplexNumber result) + => ComplexNumber.TryParse(s, out result); + } +} diff --git a/EasyTool.Core/MathCategory/GeoUtil.cs b/EasyTool.Core/MathCategory/GeoUtil.cs new file mode 100644 index 0000000..7b49fa9 --- /dev/null +++ b/EasyTool.Core/MathCategory/GeoUtil.cs @@ -0,0 +1,283 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 地理坐标工具类 + /// 提供距离计算、坐标转换等功能 + /// + public static class GeoUtil + { + /// + /// 地球半径(米) + /// + public const double EarthRadius = 6371000; + + /// + /// 计算两点之间的距离(Haversine公式) + /// + /// 纬度1 + /// 经度1 + /// 纬度2 + /// 经度2 + /// 距离(米) + public static double Distance(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadius * c; + } + + /// + /// 计算两点之间的方位角(从北向顺时针) + /// + public static double Bearing(double lat1, double lon1, double lat2, double lon2) + { + var dLon = ToRadians(lon2 - lon1); + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + + var y = Math.Sin(dLon) * Math.Cos(lat2Rad); + var x = Math.Cos(lat1Rad) * Math.Sin(lat2Rad) - + Math.Sin(lat1Rad) * Math.Cos(lat2Rad) * Math.Cos(dLon); + + var bearing = Math.Atan2(y, x); + return (ToDegrees(bearing) + 360) % 360; + } + + /// + /// 根据起点、方位角和距离计算终点 + /// + public static (double Latitude, double Longitude) Destination( + double startLat, double startLon, double bearing, double distance) + { + var bearingRad = ToRadians(bearing); + var lat1 = ToRadians(startLat); + var lon1 = ToRadians(startLon); + + var angularDistance = distance / EarthRadius; + + var lat2 = Math.Asin( + Math.Sin(lat1) * Math.Cos(angularDistance) + + Math.Cos(lat1) * Math.Sin(angularDistance) * Math.Cos(bearingRad)); + + var lon2 = lon1 + Math.Atan2( + Math.Sin(bearingRad) * Math.Sin(angularDistance) * Math.Cos(lat1), + Math.Cos(angularDistance) - Math.Sin(lat1) * Math.Sin(lat2)); + + return (ToDegrees(lat2), ToDegrees(lon2)); + } + + /// + /// 计算矩形边界(用于数据库范围查询) + /// + public static (double MinLat, double MinLon, double MaxLat, double MaxLon) GetBoundingBox( + double centerLat, double centerLon, double radiusInMeters) + { + var latChange = radiusInMeters / EarthRadius * (180 / Math.PI); + var lonChange = radiusInMeters / (EarthRadius * Math.Cos(ToRadians(centerLat))) * (180 / Math.PI); + + return ( + centerLat - latChange, + centerLon - lonChange, + centerLat + latChange, + centerLon + lonChange + ); + } + + /// + /// 判断点是否在矩形范围内 + /// + public static bool IsInBoundingBox( + double lat, double lon, + double minLat, double minLon, double maxLat, double maxLon) + { + return lat >= minLat && lat <= maxLat && lon >= minLon && lon <= maxLon; + } + + /// + /// 判断点是否在圆形范围内 + /// + public static bool IsInCircle( + double lat, double lon, + double centerLat, double centerLon, double radiusInMeters) + { + return Distance(lat, lon, centerLat, centerLon) <= radiusInMeters; + } + + /// + /// 判断点是否在多边形内 + /// + public static bool IsInPolygon(double lat, double lon, params (double Lat, double Lon)[] polygon) + { + if (polygon == null || polygon.Length < 3) + return false; + + var inside = false; + var j = polygon.Length - 1; + + for (int i = 0; i < polygon.Length; j = i++) + { + var xi = polygon[i].Lon; + var yi = polygon[i].Lat; + var xj = polygon[j].Lon; + var yj = polygon[j].Lat; + + var intersect = ((yi > lat) != (yj > lat)) && + (lon < (xj - xi) * (lat - yi) / (yj - yi) + xi); + + if (intersect) + inside = !inside; + } + + return inside; + } + + /// + /// 计算多边形面积(平方米) + /// + public static double PolygonArea(params (double Lat, double Lon)[] polygon) + { + if (polygon == null || polygon.Length < 3) + return 0; + + var area = 0.0; + var j = polygon.Length - 1; + + for (int i = 0; i < polygon.Length; j = i++) + { + var xi = ToRadians(polygon[i].Lon); + var yi = ToRadians(polygon[i].Lat); + var xj = ToRadians(polygon[j].Lon); + var yj = ToRadians(polygon[j].Lat); + + area += (xj - xi) * (2 + Math.Sin(yi) + Math.Sin(yj)); + } + + return Math.Abs(area * EarthRadius * EarthRadius / 2); + } + + #region 坐标转换 + + /// + /// WGS84转GCJ02(火星坐标) + /// + public static (double Lat, double Lon) Wgs84ToGcj02(double wgsLat, double wgsLon) + { + var dLat = TransformLat(wgsLon - 105.0, wgsLat - 35.0); + var dLon = TransformLon(wgsLon - 105.0, wgsLat - 35.0); + + var radLat = wgsLat / 180.0 * Math.PI; + var magic = Math.Sin(radLat); + magic = 1 - 0.00669342162296594323 * magic * magic; + var sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((EarthRadius / 1000) * (1 - 0.00669342162296594323) * sqrtMagic * Math.PI); + dLon = (dLon * 180.0) / ((EarthRadius / 1000) * sqrtMagic * Math.Cos(radLat) * Math.PI); + + return (wgsLat + dLat, wgsLon + dLon); + } + + /// + /// GCJ02转WGS84 + /// + public static (double Lat, double Lon) Gcj02ToWgs84(double gcjLat, double gcjLon) + { + var dLat = TransformLat(gcjLon - 105.0, gcjLat - 35.0); + var dLon = TransformLon(gcjLon - 105.0, gcjLat - 35.0); + + var radLat = gcjLat / 180.0 * Math.PI; + var magic = Math.Sin(radLat); + magic = 1 - 0.00669342162296594323 * magic * magic; + var sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((EarthRadius / 1000) * (1 - 0.00669342162296594323) * sqrtMagic * Math.PI); + dLon = (dLon * 180.0) / ((EarthRadius / 1000) * sqrtMagic * Math.Cos(radLat) * Math.PI); + + return (gcjLat - dLat, gcjLon - dLon); + } + + /// + /// BD09转GCJ02 + /// + public static (double Lat, double Lon) Bd09ToGcj02(double bdLat, double bdLon) + { + var x = bdLon - 0.0065; + var y = bdLat - 0.006; + var z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Math.PI * 3000.0 / 180.0); + var theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Math.PI * 3000.0 / 180.0); + + return (z * Math.Sin(theta), z * Math.Cos(theta)); + } + + /// + /// GCJ02转BD09 + /// + public static (double Lat, double Lon) Gcj02ToBd09(double gcjLat, double gcjLon) + { + var z = Math.Sqrt(gcjLon * gcjLon + gcjLat * gcjLat) + 0.00002 * Math.Sin(gcjLat * Math.PI * 3000.0 / 180.0); + var theta = Math.Atan2(gcjLat, gcjLon) + 0.000003 * Math.Cos(gcjLon * Math.PI * 3000.0 / 180.0); + + return (z * Math.Sin(theta) + 0.006, z * Math.Cos(theta) + 0.0065); + } + + /// + /// BD09转WGS84 + /// + public static (double Lat, double Lon) Bd09ToWgs84(double bdLat, double bdLon) + { + var gcj = Bd09ToGcj02(bdLat, bdLon); + return Gcj02ToWgs84(gcj.Lat, gcj.Lon); + } + + /// + /// WGS84转BD09 + /// + public static (double Lat, double Lon) Wgs84ToBd09(double wgsLat, double wgsLon) + { + var gcj = Wgs84ToGcj02(wgsLat, wgsLon); + return Gcj02ToBd09(gcj.Lat, gcj.Lon); + } + + private static double TransformLat(double x, double y) + { + var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(y * Math.PI) + 40.0 * Math.Sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; + ret += (160.0 * Math.Sin(y / 12.0 * Math.PI) + 320 * Math.Sin(y * Math.PI / 30.0)) * 2.0 / 3.0; + return ret; + } + + private static double TransformLon(double x, double y) + { + var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(x * Math.PI) + 40.0 * Math.Sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; + ret += (150.0 * Math.Sin(x / 12.0 * Math.PI) + 300.0 * Math.Sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; + return ret; + } + + #endregion + + #region 辅助方法 + + private static double ToRadians(double degrees) + { + return degrees * Math.PI / 180.0; + } + + private static double ToDegrees(double radians) + { + return radians * 180.0 / Math.PI; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/MathUtil.cs b/EasyTool.Core/MathCategory/MathUtil.cs index 6887204..af442c1 100644 --- a/EasyTool.Core/MathCategory/MathUtil.cs +++ b/EasyTool.Core/MathCategory/MathUtil.cs @@ -1,416 +1,331 @@ using System; -using System.Text; +using System.Collections.Generic; +using System.Linq; namespace EasyTool.MathCategory { /// - /// 数学工具类,提供数字计算和数学运算方法 + /// 数学计算工具类 /// public static class MathUtil { - #region 质数与阶乘 - /// - /// 判断一个整数是否为质数 + /// 计算平均值 /// - /// 要判断的整数 - /// 如果是质数,则返回 true;否则返回 false - public static bool IsPrime(int n) + public static double Average(IEnumerable values) { - if (n <= 1) - { - return false; - } + var list = values.ToList(); + return list.Count == 0 ? 0 : list.Sum() / list.Count; + } - for (int i = 2; i <= Math.Sqrt(n); i++) - { - if (n % i == 0) - { - return false; - } - } + /// + /// 计算标准差 + /// + public static double StandardDeviation(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; - return true; + var avg = Average(list); + var sumOfSquares = list.Sum(v => Math.Pow(v - avg, 2)); + return Math.Sqrt(sumOfSquares / list.Count); } /// - /// 求一个整数的阶乘 + /// 计算方差 /// - /// 要求阶乘的整数 - /// 阶乘结果 - public static int Factorial(int n) + public static double Variance(IEnumerable values) { - if (n < 0) - { - throw new ArgumentException("阶乘只能求非负整数"); - } + var list = values.ToList(); + if (list.Count == 0) return 0; - int result = 1; - for (int i = 1; i <= n; i++) - { - result *= i; - } - - return result; + var avg = Average(list); + return list.Sum(v => Math.Pow(v - avg, 2)) / list.Count; } - #endregion + /// + /// 计算中位数 + /// + public static double Median(IEnumerable values) + { + var sorted = values.OrderBy(v => v).ToList(); + if (sorted.Count == 0) return 0; - #region 最大公约数与最小公倍数 + var mid = sorted.Count / 2; + return sorted.Count % 2 == 0 + ? (sorted[mid - 1] + sorted[mid]) / 2 + : sorted[mid]; + } /// - /// 计算两个整数的最大公约数 + /// 计算众数 /// - /// 第一个整数 - /// 第二个整数 - /// 最大公约数 - public static int Gcd(int a, int b) + public static List Mode(IEnumerable values) { - if (b == 0) - { - return a; - } - else - { - return Gcd(b, a % b); - } + var groups = values.GroupBy(v => v) + .OrderByDescending(g => g.Count()) + .ToList(); + + if (groups.Count == 0) return new List(); + + var maxCount = groups[0].Count(); + return groups.Where(g => g.Count() == maxCount) + .Select(g => g.Key) + .ToList(); } /// - /// 计算两个整数的最小公倍数 + /// 计算百分位数 /// - /// 第一个整数 - /// 第二个整数 - /// 最小公倍数 - public static int Lcm(int a, int b) + public static double Percentile(IEnumerable values, double percentile) { - return a * b / Gcd(a, b); - } + var sorted = values.OrderBy(v => v).ToList(); + if (sorted.Count == 0) return 0; + + var index = (percentile / 100) * (sorted.Count - 1); + var lower = (int)Math.Floor(index); + var upper = (int)Math.Ceiling(index); - #endregion + if (lower == upper) return sorted[lower]; - #region 数值计算 + return sorted[lower] + (index - lower) * (sorted[upper] - sorted[lower]); + } /// - /// 计算两个浮点数的差的绝对值是否小于指定的精度 + /// 限制值在指定范围内 /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 指定的精度 - /// 如果两个浮点数的差的绝对值小于指定的精度,则返回 true;否则返回 false - public static bool ApproxEqual(double a, double b, double eps) + public static double Clamp(double value, double min, double max) { - return Math.Abs(a - b) < eps; + return Math.Max(min, Math.Min(max, value)); } /// - /// 求两个浮点数的中位数 + /// 线性插值 /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 两个浮点数的中位数 - public static double Median(double a, double b) + public static double Lerp(double a, double b, double t) { - return (a + b) / 2; + return a + (b - a) * Clamp(t, 0, 1); } /// - /// 计算 n 的 k 次方 + /// 反向线性插值 /// - /// 底数 - /// 指数 - /// n 的 k 次方 - public static int Pow(int n, int k) + public static double InverseLerp(double a, double b, double value) { - if (k == 0) - { - return 1; - } - else if (k % 2 == 0) - { - int half = Pow(n, k / 2); - return half * half; - } - else - { - int half = Pow(n, k / 2); - return half * half * n; - } + if (a == b) return 0; + return Clamp((value - a) / (b - a), 0, 1); } /// - /// 求一个整数的绝对值 + /// 映射值从一个范围到另一个范围 /// - /// 待求绝对值的数字 - /// 该数字的绝对值 - public static int Abs(int number) + public static double Remap(double value, double fromMin, double fromMax, double toMin, double toMax) { - return number < 0 ? -number : number; + var t = InverseLerp(fromMin, fromMax, value); + return Lerp(toMin, toMax, t); } /// - /// 求一个整数的平方 + /// 计算最大公约数 /// - /// 待求平方的数字 - /// 该数字的平方 - public static int Square(int number) + public static long GCD(long a, long b) { - return number * number; + a = Math.Abs(a); + b = Math.Abs(b); + + while (b != 0) + { + var temp = b; + b = a % b; + a = temp; + } + + return a; } /// - /// 求一个整数的立方 + /// 计算最大公约数(别名) + /// + public static long Gcd(long a, long b) => GCD(a, b); + + /// + /// 计算最小公倍数 /// - /// 待求立方的数字 - /// 该数字的立方 - public static int Cube(int number) + public static long LCM(long a, long b) { - return number * number * number; + if (a == 0 || b == 0) return 0; + return Math.Abs(a * b) / GCD(a, b); } /// - /// 计算两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue + /// 计算最小公倍数(别名) /// - /// 第一个整数 - /// 第二个整数 - /// 两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue - public static int SafeAdd(int a, int b) + public static long Lcm(long a, long b) => LCM(a, b); + + /// + /// 判断是否为素数 + /// + public static bool IsPrime(long n) { - int sum = a + b; + if (n < 2) return false; + if (n == 2) return true; + if (n % 2 == 0) return false; - if (a > 0 && b > 0 && sum < 0) - { - return int.MaxValue; - } - else if (a < 0 && b < 0 && sum > 0) + var sqrt = (long)Math.Sqrt(n); + for (long i = 3; i <= sqrt; i += 2) { - return int.MinValue; + if (n % i == 0) return false; } - else - { - return sum; - } - } - - #endregion - #region 进制转换 + return true; + } /// - /// 把一个数字转换为二进制字符串 + /// 获取所有素数因子 /// - /// 待转换的数字 - /// 该数字的二进制字符串 - public static string ToBinaryString(int number) + public static List GetPrimeFactors(long n) { - if (number == 0) + var factors = new List(); + n = Math.Abs(n); + + while (n % 2 == 0) { - return "0"; + factors.Add(2); + n /= 2; } - string result = string.Empty; - while (number > 0) + for (long i = 3; i * i <= n; i += 2) { - result = (number % 2).ToString() + result; - number /= 2; + while (n % i == 0) + { + factors.Add(i); + n /= i; + } } - return result; + if (n > 2) factors.Add(n); + + return factors; } /// - /// 把一个数字转换为八进制字符串 + /// 计算阶乘 /// - /// 待转换的数字 - /// 该数字的八进制字符串 - public static string ToOctalString(int number) + public static long Factorial(int n) { - if (number == 0) - { - return "0"; - } + if (n < 0) throw new ArgumentException("阶乘不支持负数"); + if (n <= 1) return 1; - string result = string.Empty; - while (number > 0) + long result = 1; + for (int i = 2; i <= n; i++) { - result = (number % 8).ToString() + result; - number /= 8; + result *= i; } return result; } /// - /// 把一个数字转换为十六进制字符串 + /// 计算排列数 A(n, m) /// - /// 待转换的数字 - /// 该数字的十六进制字符串 - public static string ToHexString(int number) + public static long Permutation(int n, int m) { - if (number == 0) - { - return "0"; - } + if (m > n) return 0; + if (m == 0) return 1; - string result = string.Empty; - while (number > 0) + long result = 1; + for (int i = 0; i < m; i++) { - int remainder = number % 16; - if (remainder < 10) - { - result = remainder.ToString() + result; - } - else - { - result = (char)('A' + remainder - 10) + result; - } - number /= 16; + result *= (n - i); } return result; } - #endregion - - #region 高级数学函数 - /// - /// 求一个整数的斐波那契数列的值 + /// 计算组合数 C(n, m) /// - /// 要求斐波那契数列的整数 - /// 斐波那契数列的值 - public static int Fibonacci(int n) + public static long Combination(int n, int m) { - if (n <= 1) - { - return n; - } - else + if (m > n) return 0; + if (m == 0 || m == n) return 1; + + m = Math.Min(m, n - m); + + long result = 1; + for (int i = 0; i < m; i++) { - return Fibonacci(n - 1) + Fibonacci(n - 2); + result = result * (n - i) / (i + 1); } + + return result; } /// - /// 求一个整数的二进制表示中 1 的个数 + /// 计算斐波那契数 /// - /// 要求二进制表示中 1 的个数的整数 - /// 二进制表示中 1 的个数 - public static int CountBits(int n) + public static long Fibonacci(int n) { - int count = 0; + if (n < 0) throw new ArgumentException("斐波那契数不支持负数"); + if (n <= 1) return n; - while (n != 0) + long a = 0, b = 1; + for (int i = 2; i <= n; i++) { - count++; - n &= n - 1; + var temp = a + b; + a = b; + b = temp; } - return count; + return b; } /// - /// 判断一个整数是否为完全平方数 + /// 判断是否在范围内 /// - /// 要判断的整数 - /// 如果是完全平方数,则返回 true;否则返回 false - public static bool IsPerfectSquare(int n) + public static bool InRange(double value, double min, double max) { - int sqrt = (int)Math.Sqrt(n); - return sqrt * sqrt == n; + return value >= min && value <= max; } /// - /// 计算一个整数的各个数位上数字的平方和,如果结果为 1,则返回 true;否则进行下一次计算,直到结果为 1 或者进入死循环为止 + /// 判断两个浮点数是否近似相等 /// - /// 要计算的整数 - /// 如果结果为 1,则返回 true;否则返回 false - public static bool IsHappyNumber(int n) + public static bool Approximately(double a, double b, double epsilon = 1e-10) { - int sum = n; - - while (true) - { - int digitsSum = 0; - while (sum > 0) - { - int digit = sum % 10; - digitsSum += digit * digit; - sum /= 10; - } - - if (digitsSum == 1) - { - return true; - } - else if (digitsSum == 4) - { - return false; - } - - sum = digitsSum; - } + return Math.Abs(a - b) < epsilon; } /// - /// 计算两个整数的二进制表示中有多少位不同 + /// 计算两点之间的距离 /// - /// 第一个整数 - /// 第二个整数 - /// 两个整数的二进制表示中有多少位不同 - public static int HammingDistance(int a, int b) + public static double Distance(double x1, double y1, double x2, double y2) { - int count = 0; - int xor = a ^ b; - - while (xor != 0) - { - count++; - xor &= xor - 1; - } - - return count; + return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); } /// - /// 求一个整数的所有因子 + /// 计算两点之间的角度(弧度) /// - /// 要求因子的整数 - /// 所有因子 - public static int[] GetAllFactors(int n) + public static double Angle(double x1, double y1, double x2, double y2) { - int count = 0; - - for (int i = 1; i <= Math.Sqrt(n); i++) - { - if (n % i == 0) - { - count++; - if (i != n / i) - { - count++; - } - } - } - - int[] factors = new int[count]; - int index = 0; - - for (int i = 1; i <= Math.Sqrt(n); i++) - { - if (n % i == 0) - { - factors[index++] = i; - if (i != n / i) - { - factors[index++] = n / i; - } - } - } + return Math.Atan2(y2 - y1, x2 - x1); + } - return factors; + /// + /// 弧度转角度 + /// + public static double ToDegrees(double radians) + { + return radians * 180 / Math.PI; } - #endregion + /// + /// 角度转弧度 + /// + public static double ToRadians(double degrees) + { + return degrees * Math.PI / 180; + } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/MathCategory/MatrixUtil.cs b/EasyTool.Core/MathCategory/MatrixUtil.cs new file mode 100644 index 0000000..7967045 --- /dev/null +++ b/EasyTool.Core/MathCategory/MatrixUtil.cs @@ -0,0 +1,560 @@ +using System; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 矩阵工具类 + /// 提供矩阵的基本运算 + /// + public static class MatrixUtil + { + #region 创建 + + /// + /// 创建矩阵 + /// + /// 行数 + /// 列数 + /// 初始值 + /// 矩阵 + public static Matrix Create(int rows, int cols, double value = 0) + { + return new Matrix(rows, cols, value); + } + + /// + /// 从二维数组创建矩阵 + /// + /// 二维数组 + /// 矩阵 + public static Matrix FromArray(double[,] array) + { + var rows = array.GetLength(0); + var cols = array.GetLength(1); + var matrix = new Matrix(rows, cols); + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix[i, j] = array[i, j]; + } + } + + return matrix; + } + + /// + /// 创建单位矩阵 + /// + /// 大小 + /// 单位矩阵 + public static Matrix Identity(int size) + { + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + { + matrix[i, i] = 1; + } + return matrix; + } + + /// + /// 创建零矩阵 + /// + /// 行数 + /// 列数 + /// 零矩阵 + public static Matrix Zeros(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 创建全1矩阵 + /// + /// 行数 + /// 列数 + /// 全1矩阵 + public static Matrix Ones(int rows, int cols) + { + return new Matrix(rows, cols, 1); + } + + /// + /// 创建对角矩阵 + /// + /// 对角元素 + /// 对角矩阵 + public static Matrix Diagonal(params double[] diagonal) + { + var size = diagonal.Length; + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + { + matrix[i, i] = diagonal[i]; + } + return matrix; + } + + /// + /// 创建随机矩阵 + /// + /// 行数 + /// 列数 + /// 最小值 + /// 最大值 + /// 随机矩阵 + public static Matrix Random(int rows, int cols, double min = 0, double max = 1) + { + var random = new Random(); + var matrix = new Matrix(rows, cols); + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix[i, j] = random.NextDouble() * (max - min) + min; + } + } + + return matrix; + } + + #endregion + + #region 运算 + + /// + /// 矩阵加法 + /// + public static Matrix Add(Matrix a, Matrix b) + { + if (a.Rows != b.Rows || a.Cols != b.Cols) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j] + b[i, j]; + } + } + return result; + } + + /// + /// 矩阵减法 + /// + public static Matrix Subtract(Matrix a, Matrix b) + { + if (a.Rows != b.Rows || a.Cols != b.Cols) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j] - b[i, j]; + } + } + return result; + } + + /// + /// 矩阵乘法 + /// + public static Matrix Multiply(Matrix a, Matrix b) + { + if (a.Cols != b.Rows) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, b.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < b.Cols; j++) + { + double sum = 0; + for (int k = 0; k < a.Cols; k++) + { + sum += a[i, k] * b[k, j]; + } + result[i, j] = sum; + } + } + return result; + } + + /// + /// 标量乘法 + /// + public static Matrix Scale(Matrix matrix, double scalar) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[i, j] * scalar; + } + } + return result; + } + + /// + /// 矩阵转置 + /// + public static Matrix Transpose(Matrix matrix) + { + var result = new Matrix(matrix.Cols, matrix.Rows); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[j, i] = matrix[i, j]; + } + } + return result; + } + + /// + /// 行列式 + /// + public static double Determinant(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + return DeterminantInternal(matrix); + } + + private static double DeterminantInternal(Matrix matrix) + { + int n = matrix.Rows; + + if (n == 1) + return matrix[0, 0]; + + if (n == 2) + return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]; + + double det = 0; + for (int j = 0; j < n; j++) + { + det += matrix[0, j] * Cofactor(matrix, 0, j); + } + return det; + } + + private static double Cofactor(Matrix matrix, int row, int col) + { + var minor = GetMinor(matrix, row, col); + return Math.Pow(-1, row + col) * DeterminantInternal(minor); + } + + private static Matrix GetMinor(Matrix matrix, int excludeRow, int excludeCol) + { + var minor = new Matrix(matrix.Rows - 1, matrix.Cols - 1); + int mi = 0, mj = 0; + + for (int i = 0; i < matrix.Rows; i++) + { + if (i == excludeRow) continue; + + mj = 0; + for (int j = 0; j < matrix.Cols; j++) + { + if (j == excludeCol) continue; + minor[mi, mj] = matrix[i, j]; + mj++; + } + mi++; + } + + return minor; + } + + /// + /// 逆矩阵 + /// + public static Matrix? Inverse(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + var det = Determinant(matrix); + if (Math.Abs(det) < double.Epsilon) + return null; + + int n = matrix.Rows; + var result = new Matrix(n, n); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result[i, j] = Cofactor(matrix, i, j) / det; + } + } + + // 转置得到逆矩阵 + return Transpose(result); + } + + /// + /// 迹(对角元素之和) + /// + public static double Trace(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + double trace = 0; + for (int i = 0; i < matrix.Rows; i++) + { + trace += matrix[i, i]; + } + return trace; + } + + /// + /// Frobenius 范数 + /// + public static double FrobeniusNorm(Matrix matrix) + { + double sum = 0; + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + sum += matrix[i, j] * matrix[i, j]; + } + } + return Math.Sqrt(sum); + } + + #endregion + + #region 变换 + + /// + /// 水平翻转 + /// + public static Matrix FlipHorizontal(Matrix matrix) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[i, matrix.Cols - 1 - j]; + } + } + return result; + } + + /// + /// 垂直翻转 + /// + public static Matrix FlipVertical(Matrix matrix) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[matrix.Rows - 1 - i, j]; + } + } + return result; + } + + /// + /// 顺时针旋转 90 度 + /// + public static Matrix Rotate90(Matrix matrix) + { + var result = new Matrix(matrix.Cols, matrix.Rows); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[j, matrix.Rows - 1 - i] = matrix[i, j]; + } + } + return result; + } + + /// + /// 水平拼接 + /// + public static Matrix HorizontalConcat(Matrix a, Matrix b) + { + if (a.Rows != b.Rows) + throw new ArgumentException("矩阵行数不匹配"); + + var result = new Matrix(a.Rows, a.Cols + b.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j]; + } + for (int j = 0; j < b.Cols; j++) + { + result[i, a.Cols + j] = b[i, j]; + } + } + return result; + } + + /// + /// 垂直拼接 + /// + public static Matrix VerticalConcat(Matrix a, Matrix b) + { + if (a.Cols != b.Cols) + throw new ArgumentException("矩阵列数不匹配"); + + var result = new Matrix(a.Rows + b.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j]; + } + } + for (int i = 0; i < b.Rows; i++) + { + for (int j = 0; j < b.Cols; j++) + { + result[a.Rows + i, j] = b[i, j]; + } + } + return result; + } + + #endregion + } + + /// + /// 矩阵类 + /// + public class Matrix + { + private readonly double[,] _data; + + /// + /// 行数 + /// + public int Rows { get; } + + /// + /// 列数 + /// + public int Cols { get; } + + /// + /// 是否为方阵 + /// + public bool IsSquare => Rows == Cols; + + /// + /// 访问元素 + /// + public double this[int row, int col] + { + get => _data[row, col]; + set => _data[row, col] = value; + } + + /// + /// 创建矩阵 + /// + public Matrix(int rows, int cols, double value = 0) + { + Rows = rows; + Cols = cols; + _data = new double[rows, cols]; + + if (value != 0) + { + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + _data[i, j] = value; + } + } + } + } + + /// + /// 获取行 + /// + public double[] GetRow(int row) + { + var result = new double[Cols]; + for (int j = 0; j < Cols; j++) + { + result[j] = _data[row, j]; + } + return result; + } + + /// + /// 获取列 + /// + public double[] GetColumn(int col) + { + var result = new double[Rows]; + for (int i = 0; i < Rows; i++) + { + result[i] = _data[i, col]; + } + return result; + } + + /// + /// 转换为二维数组 + /// + public double[,] ToArray() + { + var result = new double[Rows, Cols]; + Array.Copy(_data, result, _data.Length); + return result; + } + + /// + /// 转换为字符串 + /// + public override string ToString() + { + var sb = new System.Text.StringBuilder(); + for (int i = 0; i < Rows; i++) + { + sb.Append("["); + for (int j = 0; j < Cols; j++) + { + sb.Append(_data[i, j].ToString("F4").PadLeft(10)); + if (j < Cols - 1) sb.Append(", "); + } + sb.AppendLine("]"); + } + return sb.ToString(); + } + + #region 运算符重载 + + public static Matrix operator +(Matrix a, Matrix b) => MatrixUtil.Add(a, b); + public static Matrix operator -(Matrix a, Matrix b) => MatrixUtil.Subtract(a, b); + public static Matrix operator *(Matrix a, Matrix b) => MatrixUtil.Multiply(a, b); + public static Matrix operator *(Matrix a, double scalar) => MatrixUtil.Scale(a, scalar); + public static Matrix operator *(double scalar, Matrix a) => MatrixUtil.Scale(a, scalar); + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/NumberFormatUtil.cs b/EasyTool.Core/MathCategory/NumberFormatUtil.cs new file mode 100644 index 0000000..2bee23c --- /dev/null +++ b/EasyTool.Core/MathCategory/NumberFormatUtil.cs @@ -0,0 +1,505 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace EasyTool.MathCategory +{ + /// + /// 数字格式化工具类 + /// 提供数字转换为大写金额、中文数字等功能 + /// + public static class NumberFormatUtil + { + #region 中文大写金额 + + private static readonly string[] ChineseDigits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + private static readonly string[] ChineseUnits = { "", "拾", "佰", "仟" }; + private static readonly string[] ChineseBigUnits = { "", "万", "亿", "兆" }; + + /// + /// 数字转中文大写金额 + /// + /// 金额 + /// 中文大写金额 + public static string ToChineseAmount(decimal amount) + { + if (amount == 0) + return "零元整"; + + var result = new StringBuilder(); + var isNegative = amount < 0; + + if (isNegative) + { + result.Append("负"); + amount = -amount; + } + + // 四舍五入到分 + amount = Math.Round(amount, 2); + + var intPart = (long)amount; + var decPart = (int)((amount - intPart) * 100); + + // 处理整数部分 + if (intPart > 0) + { + result.Append(ConvertToChineseAmount(intPart)); + result.Append("元"); + } + + // 处理小数部分 + if (decPart > 0) + { + var jiao = decPart / 10; + var fen = decPart % 10; + + if (jiao > 0) + { + result.Append(ChineseDigits[jiao]); + result.Append("角"); + } + + if (fen > 0) + { + result.Append(ChineseDigits[fen]); + result.Append("分"); + } + } + else + { + result.Append("整"); + } + + return result.ToString(); + } + + private static string ConvertToChineseAmount(long number) + { + var result = new StringBuilder(); + var parts = new List(); + int unitIndex = 0; + + while (number > 0) + { + var part = (int)(number % 10000); + var partStr = ConvertPartToChinese(part); + + if (!string.IsNullOrEmpty(partStr)) + { + if (unitIndex > 0) + partStr += ChineseBigUnits[unitIndex]; + parts.Insert(0, partStr); + } + else if (parts.Count > 0) + { + parts.Insert(0, "零"); + } + + number /= 10000; + unitIndex++; + } + + result.Append(string.Join("", parts)); + + // 处理连续的零 + var final = result.ToString(); + while (final.Contains("零零")) + final = final.Replace("零零", "零"); + + // 去掉末尾的零 + final = final.TrimEnd('零'); + + return final; + } + + private static string ConvertPartToChinese(int number) + { + if (number == 0) + return ""; + + var result = new StringBuilder(); + var needZero = false; + + for (int i = 3; i >= 0; i--) + { + var digit = (int)(number / Math.Pow(10, i)) % 10; + + if (digit == 0) + { + needZero = true; + } + else + { + if (needZero) + { + result.Append("零"); + needZero = false; + } + result.Append(ChineseDigits[digit]); + if (i > 0) + result.Append(ChineseUnits[i]); + } + } + + return result.ToString(); + } + + #endregion + + #region 中文数字 + + private static readonly string[] SimpleChineseDigits = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; + + /// + /// 数字转中文数字 + /// + public static string ToChineseNumber(long number) + { + if (number == 0) + return "零"; + + var result = new StringBuilder(); + var isNegative = number < 0; + + if (isNegative) + { + result.Append("负"); + number = -number; + } + + var parts = new List(); + int unitIndex = 0; + + while (number > 0) + { + var part = (int)(number % 10000); + var partStr = ConvertPartToSimpleChinese(part); + + if (!string.IsNullOrEmpty(partStr)) + { + if (unitIndex > 0) + partStr += ChineseBigUnits[unitIndex]; + parts.Insert(0, partStr); + } + else if (parts.Count > 0) + { + parts.Insert(0, "零"); + } + + number /= 10000; + unitIndex++; + } + + result.Append(string.Join("", parts)); + + var final = result.ToString(); + while (final.Contains("零零")) + final = final.Replace("零零", "零"); + + final = final.TrimEnd('零'); + + // 处理"一十"开头的特殊情况 + if (final.StartsWith("一十")) + final = final.Substring(1); + + return final; + } + + private static string ConvertPartToSimpleChinese(int number) + { + if (number == 0) + return ""; + + var result = new StringBuilder(); + var needZero = false; + + for (int i = 3; i >= 0; i--) + { + var digit = (int)(number / Math.Pow(10, i)) % 10; + + if (digit == 0) + { + needZero = true; + } + else + { + if (needZero) + { + result.Append("零"); + needZero = false; + } + result.Append(SimpleChineseDigits[digit]); + if (i > 0) + result.Append(ChineseUnits[i]); + } + } + + return result.ToString(); + } + + #endregion + + #region 英文数字 + + private static readonly string[] Ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; + private static readonly string[] Tens = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; + private static readonly string[] Thousands = { "", "thousand", "million", "billion", "trillion" }; + + /// + /// 数字转英文单词 + /// + public static string ToEnglishWords(long number) + { + if (number == 0) + return "zero"; + + var result = new StringBuilder(); + var isNegative = number < 0; + + if (isNegative) + { + result.Append("negative "); + number = -number; + } + + var groups = new List(); + while (number > 0) + { + groups.Add((int)(number % 1000)); + number /= 1000; + } + + for (int i = groups.Count - 1; i >= 0; i--) + { + if (groups[i] > 0) + { + result.Append(ConvertGroupToEnglish(groups[i])); + if (i > 0) + result.Append(" " + Thousands[i] + " "); + } + } + + return result.ToString().Trim(); + } + + private static string ConvertGroupToEnglish(int number) + { + var result = new StringBuilder(); + + if (number >= 100) + { + result.Append(Ones[number / 100] + " hundred"); + number %= 100; + if (number > 0) + result.Append(" "); + } + + if (number >= 20) + { + result.Append(Tens[number / 10]); + number %= 10; + if (number > 0) + result.Append("-" + Ones[number]); + } + else if (number > 0) + { + result.Append(Ones[number]); + } + + return result.ToString(); + } + + /// + /// 数字转英文金额 + /// + public static string ToEnglishAmount(decimal amount) + { + if (amount == 0) + return "zero dollars"; + + var result = new StringBuilder(); + var isNegative = amount < 0; + + if (isNegative) + { + result.Append("negative "); + amount = -amount; + } + + amount = Math.Round(amount, 2); + var intPart = (long)amount; + var decPart = (int)((amount - intPart) * 100); + + if (intPart > 0) + { + result.Append(ToEnglishWords(intPart)); + result.Append(intPart == 1 ? " dollar" : " dollars"); + } + + if (decPart > 0) + { + if (intPart > 0) + result.Append(" and "); + result.Append(ToEnglishWords(decPart)); + result.Append(decPart == 1 ? " cent" : " cents"); + } + + return result.ToString(); + } + + #endregion + + #region 罗马数字 + + private static readonly (int Value, string Symbol)[] RomanSymbols = + { + (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), + (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), + (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I") + }; + + /// + /// 数字转罗马数字 + /// + public static string ToRoman(int number) + { + if (number < 1 || number > 3999) + throw new ArgumentOutOfRangeException(nameof(number), "罗马数字范围是1-3999"); + + var result = new StringBuilder(); + + foreach (var (value, symbol) in RomanSymbols) + { + while (number >= value) + { + result.Append(symbol); + number -= value; + } + } + + return result.ToString(); + } + + /// + /// 罗马数字转数字 + /// + public static int FromRoman(string roman) + { + if (string.IsNullOrWhiteSpace(roman)) + throw new ArgumentException("罗马数字不能为空"); + + var values = new Dictionary + { + {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, + {'C', 100}, {'D', 500}, {'M', 1000} + }; + + roman = roman.ToUpper(); + int result = 0; + int prevValue = 0; + + for (int i = roman.Length - 1; i >= 0; i--) + { + if (!values.TryGetValue(roman[i], out var value)) + throw new ArgumentException($"无效的罗马数字字符: {roman[i]}"); + + if (value < prevValue) + result -= value; + else + result += value; + + prevValue = value; + } + + return result; + } + + #endregion + + #region 格式化 + + /// + /// 格式化为百分比 + /// + public static string ToPercent(double value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 格式化为货币 + /// + public static string ToCurrency(decimal amount, string currencySymbol = "¥") + { + return currencySymbol + amount.ToString("N2"); + } + + /// + /// 格式化为科学计数法 + /// + public static string ToScientific(double value, int decimals = 2) + { + return value.ToString($"E{decimals}"); + } + + /// + /// 格式化为千分位 + /// + public static string ToThousands(long number, string separator = ",") + { + return number.ToString("N0").Replace(",", separator); + } + + /// + /// 格式化文件大小 + /// + public static string ToFileSize(long bytes, int decimals = 2) + { + string[] units = { "B", "KB", "MB", "GB", "TB", "PB" }; + double size = bytes; + int unitIndex = 0; + + while (size >= 1024 && unitIndex < units.Length - 1) + { + size /= 1024; + unitIndex++; + } + + return $"{Math.Round(size, decimals)} {units[unitIndex]}"; + } + + /// + /// 格式化序数词(1st, 2nd, 3rd, 4th...) + /// + public static string ToOrdinal(int number) + { + if (number <= 0) + return number.ToString(); + + string suffix; + int mod100 = number % 100; + + if (mod100 == 11 || mod100 == 12 || mod100 == 13) + { + suffix = "th"; + } + else + { + int mod10 = number % 10; + suffix = mod10 switch + { + 1 => "st", + 2 => "nd", + 3 => "rd", + _ => "th" + }; + } + + return number + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/PrimeUtil.cs b/EasyTool.Core/MathCategory/PrimeUtil.cs new file mode 100644 index 0000000..8091bcf --- /dev/null +++ b/EasyTool.Core/MathCategory/PrimeUtil.cs @@ -0,0 +1,483 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 质数工具类 + /// 提供质数相关的计算功能 + /// + public static class PrimeUtil + { + /// + /// 检查是否为质数 + /// + /// 数字 + /// 是否为质数 + public static bool IsPrime(long n) + { + if (n < 2) + return false; + + if (n == 2) + return true; + + if (n % 2 == 0) + return false; + + var sqrt = (long)Math.Sqrt(n); + + for (long i = 3; i <= sqrt; i += 2) + { + if (n % i == 0) + return false; + } + + return true; + } + + /// + /// 使用 Miller-Rabin 算法检查大数是否为质数 + /// + /// 数字 + /// 迭代次数(精度) + /// 是否为质数 + public static bool IsPrimeMillerRabin(long n, int iterations = 5) + { + if (n < 2) + return false; + + if (n == 2 || n == 3) + return true; + + if (n % 2 == 0) + return false; + + // 将 n-1 分解为 2^r * d + long d = n - 1; + int r = 0; + + while (d % 2 == 0) + { + d /= 2; + r++; + } + + var random = new Random(); + + for (int i = 0; i < iterations; i++) + { + long a = 2 + (long)(random.NextDouble() * (n - 4)); + long x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + + for (int j = 0; j < r - 1; j++) + { + x = ModPow(x, 2, n); + + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + + /// + /// 获取下一个质数 + /// + /// 起始数字 + /// 下一个质数 + public static long NextPrime(long n) + { + if (n < 2) + return 2; + + long candidate = n + 1; + + if (candidate % 2 == 0) + candidate++; + + while (!IsPrime(candidate)) + { + candidate += 2; + } + + return candidate; + } + + /// + /// 获取前一个质数 + /// + /// 起始数字 + /// 前一个质数,如果不存在返回 -1 + public static long PreviousPrime(long n) + { + if (n <= 2) + return -1; + + if (n == 3) + return 2; + + long candidate = n - 1; + + if (candidate % 2 == 0) + candidate--; + + while (candidate >= 2 && !IsPrime(candidate)) + { + candidate -= 2; + } + + return candidate >= 2 ? candidate : -1; + } + + /// + /// 获取范围内的所有质数 + /// + /// 起始数字 + /// 结束数字 + /// 质数列表 + public static List GetPrimesInRange(long start, long end) + { + var primes = new List(); + + for (long i = start; i <= end; i++) + { + if (IsPrime(i)) + primes.Add(i); + } + + return primes; + } + + /// + /// 使用埃拉托斯特尼筛法获取指定范围内的所有质数 + /// + /// 上限 + /// 质数列表 + public static List SieveOfEratosthenes(long limit) + { + if (limit < 2) + return new List(); + + var isPrime = new bool[limit + 1]; + Array.Fill(isPrime, true); + + isPrime[0] = false; + isPrime[1] = false; + + var sqrt = (long)Math.Sqrt(limit); + + for (long i = 2; i <= sqrt; i++) + { + if (isPrime[i]) + { + for (long j = i * i; j <= limit; j += i) + { + isPrime[j] = false; + } + } + } + + var primes = new List(); + + for (long i = 2; i <= limit; i++) + { + if (isPrime[i]) + primes.Add(i); + } + + return primes; + } + + /// + /// 获取质因数分解 + /// + /// 数字 + /// 质因数及其幂次的字典 + public static Dictionary PrimeFactorization(long n) + { + var factors = new Dictionary(); + + if (n < 2) + return factors; + + // 处理因子 2 + while (n % 2 == 0) + { + if (factors.ContainsKey(2)) + factors[2]++; + else + factors[2] = 1; + + n /= 2; + } + + // 处理奇数因子 + for (long i = 3; i * i <= n; i += 2) + { + while (n % i == 0) + { + if (factors.ContainsKey(i)) + factors[i]++; + else + factors[i] = 1; + + n /= i; + } + } + + // 如果剩下的 n 大于 1,则它本身是质数 + if (n > 1) + { + factors[n] = 1; + } + + return factors; + } + + /// + /// 获取所有因数 + /// + /// 数字 + /// 因数列表 + public static List GetDivisors(long n) + { + var divisors = new List(); + + if (n < 1) + return divisors; + + var sqrt = (long)Math.Sqrt(n); + + for (long i = 1; i <= sqrt; i++) + { + if (n % i == 0) + { + divisors.Add(i); + + if (i != n / i) + { + divisors.Add(n / i); + } + } + } + + divisors.Sort(); + return divisors; + } + + /// + /// 计算因数个数 + /// + /// 数字 + /// 因数个数 + public static long CountDivisors(long n) + { + if (n < 1) + return 0; + + var factors = PrimeFactorization(n); + long count = 1; + + foreach (var power in factors.Values) + { + count *= (power + 1); + } + + return count; + } + + /// + /// 计算最大公约数 + /// + /// 数字1 + /// 数字2 + /// 最大公约数 + public static long Gcd(long a, long b) + { + a = Math.Abs(a); + b = Math.Abs(b); + + while (b != 0) + { + var temp = b; + b = a % b; + a = temp; + } + + return a; + } + + /// + /// 计算最小公倍数 + /// + /// 数字1 + /// 数字2 + /// 最小公倍数 + public static long Lcm(long a, long b) + { + if (a == 0 || b == 0) + return 0; + + return Math.Abs(a * b) / Gcd(a, b); + } + + /// + /// 计算多个数的最大公约数 + /// + /// 数字数组 + /// 最大公约数 + public static long Gcd(params long[] numbers) + { + if (numbers == null || numbers.Length == 0) + return 0; + + long result = numbers[0]; + + for (int i = 1; i < numbers.Length; i++) + { + result = Gcd(result, numbers[i]); + } + + return result; + } + + /// + /// 计算多个数的最小公倍数 + /// + /// 数字数组 + /// 最小公倍数 + public static long Lcm(params long[] numbers) + { + if (numbers == null || numbers.Length == 0) + return 0; + + long result = numbers[0]; + + for (int i = 1; i < numbers.Length; i++) + { + result = Lcm(result, numbers[i]); + } + + return result; + } + + /// + /// 计算欧拉函数 φ(n) + /// + /// 数字 + /// 欧拉函数值 + public static long EulerTotient(long n) + { + if (n < 1) + return 0; + + var factors = PrimeFactorization(n); + long result = n; + + foreach (var p in factors.Keys) + { + result = result / p * (p - 1); + } + + return result; + } + + /// + /// 判断是否为互质数 + /// + /// 数字1 + /// 数字2 + /// 是否互质 + public static bool AreCoprime(long a, long b) + { + return Gcd(a, b) == 1; + } + + /// + /// 获取第 n 个质数(从1开始) + /// + /// 序号 + /// 第 n 个质数 + public static long GetNthPrime(int n) + { + if (n < 1) + throw new ArgumentException("n must be positive"); + + if (n == 1) + return 2; + + int count = 1; + long candidate = 1; + + while (count < n) + { + candidate += 2; + + if (IsPrime(candidate)) + count++; + } + + return candidate; + } + + /// + /// 判断是否为梅森数 + /// + /// 数字 + /// 是否为梅森数 + public static bool IsMersennePrime(long n) + { + // 梅森数形式为 2^p - 1,其中 p 是质数 + n = n + 1; + + if (n <= 2 || (n & (n - 1)) != 0) + return false; + + int p = 0; + while (n > 1) + { + n >>= 1; + p++; + } + + return IsPrime(p); + } + + #region 私有方法 + + private static long ModPow(long baseVal, long exponent, long modulus) + { + long result = 1; + baseVal %= modulus; + + while (exponent > 0) + { + if (exponent % 2 == 1) + { + result = (result * baseVal) % modulus; + } + + exponent >>= 1; + baseVal = (baseVal * baseVal) % modulus; + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/RandomUtil.cs b/EasyTool.Core/MathCategory/RandomUtil.cs index 3e858c5..7ce180e 100644 --- a/EasyTool.Core/MathCategory/RandomUtil.cs +++ b/EasyTool.Core/MathCategory/RandomUtil.cs @@ -1,262 +1,446 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; using System.Text; -using System.Threading; namespace EasyTool.MathCategory { + /// + /// 随机数工具类 + /// 提供各种随机数生成功能,包括安全随机数 + /// public static class RandomUtil { -#if NET6_0_OR_GREATER - // .NET 6+ 使用 Random.Shared,线程安全且高性能 - private static Random SharedRandom => Random.Shared; -#else - // .NET Standard 2.1 使用 ThreadLocal 确保线程安全 - private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); - private static Random SharedRandom => ThreadLocalRandom.Value!; -#endif + private static readonly Random _random = new(); + private static readonly object _lock = new(); + private static readonly char[] _alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); + private static readonly char[] _numericChars = "0123456789".ToCharArray(); + private static readonly char[] _alphanumericChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray(); + private static readonly char[] _hexChars = "0123456789ABCDEF".ToCharArray(); /// /// 生成指定范围内的随机整数 - /// 注意:返回值为 [min, max) 区间,即包含 min 但不包含 max /// - /// 随机整数的最小值(包含) - /// 随机整数的最大值(不包含) - /// 生成的随机整数 - public static int RandomInt(int min, int max) + /// 最小值(包含) + /// 最大值(不包含) + /// 随机整数 + public static int Next(int min, int max) { - return SharedRandom.Next(min, max); + lock (_lock) + { + return _random.Next(min, max); + } } /// - /// 生成指定位数的随机数字字符串 - /// 仅包含数字 0-9 + /// 生成非负随机整数 /// - /// 生成的随机数字字符串的长度 - /// 生成的随机数字字符串 - public static string RandomDigitString(int length) + /// 随机整数 + public static int Next() { - var sb = new StringBuilder(length); - for (int i = 0; i < length; i++) + lock (_lock) { - sb.Append(SharedRandom.Next(10)); + return _random.Next(); } - return sb.ToString(); } /// - /// 生成指定位数的随机字母数字字符串 - /// 包含大小写字母 A-Z, a-z 和数字 0-9 + /// 生成指定范围内的随机浮点数 /// - /// 生成的随机字母数字字符串的长度 - /// 生成的随机字母数字字符串 - public static string RandomAlphanumericString(int length) + /// 最小值 + /// 最大值 + /// 随机浮点数 + public static double NextDouble(double min, double max) { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - var sb = new StringBuilder(length); - for (int i = 0; i < length; i++) + lock (_lock) { - sb.Append(chars[SharedRandom.Next(chars.Length)]); + return _random.NextDouble() * (max - min) + min; } - return sb.ToString(); } /// - /// 生成指定长度的随机字母字符串 + /// 生成随机布尔值 /// - /// 生成的随机字母字符串的长度 - /// 生成的随机字母字符串 - public static string RandomLetterString(int length) + /// 随机布尔值 + public static bool NextBool() { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var sb = new StringBuilder(length); - for (int i = 0; i < length; i++) + lock (_lock) + { + return _random.Next(2) == 1; + } + } + + /// + /// 生成随机字节数组 + /// + /// 长度 + /// 字节数组 + public static byte[] NextBytes(int length) + { + var bytes = new byte[length]; + lock (_lock) { - sb.Append(chars[SharedRandom.Next(chars.Length)]); + _random.NextBytes(bytes); } - return sb.ToString(); + return bytes; } /// - /// 生成随机的布尔值 + /// 生成随机字母字符串 /// - /// 生成的随机布尔值 - public static bool RandomBool() + /// 长度 + /// 随机字符串 + public static string NextAlphaString(int length) { - return SharedRandom.Next(2) == 0; + return NextString(length, _alphaChars); } /// - /// 生成指定长度的随机数组 + /// 生成随机数字字符串 /// - /// 生成的随机数组的长度 - /// 生成的随机数组 - public static int[] RandomIntArray(int length) + /// 长度 + /// 随机字符串 + public static string NextNumericString(int length) { - int[] result = new int[length]; - for (int i = 0; i < length; i++) + return NextString(length, _numericChars); + } + + /// + /// 生成随机字母数字字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextAlphanumericString(int length) + { + return NextString(length, _alphanumericChars); + } + + /// + /// 生成随机十六进制字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextHexString(int length) + { + return NextString(length, _hexChars); + } + + /// + /// 使用指定字符生成随机字符串 + /// + /// 长度 + /// 字符集 + /// 随机字符串 + public static string NextString(int length, char[] chars) + { + var result = new StringBuilder(length); + lock (_lock) { - result[i] = SharedRandom.Next(); + for (int i = 0; i < length; i++) + { + result.Append(chars[_random.Next(chars.Length)]); + } } - return result; + return result.ToString(); } /// - /// 生成指定长度的随机双精度浮点数数组 + /// 使用指定字符生成随机字符串 /// - /// 生成的随机数组的长度 - /// 生成的随机双精度浮点数数组 - public static double[] RandomDoubleArray(int length) + /// 长度 + /// 字符集 + /// 随机字符串 + public static string NextString(int length, string chars) { - double[] result = new double[length]; - for (int i = 0; i < length; i++) + return NextString(length, chars.ToCharArray()); + } + + /// + /// 从数组中随机选择一个元素 + /// + /// 元素类型 + /// 数组 + /// 随机元素 + public static T? NextItem(T[] array) + { + if (array == null || array.Length == 0) + return default; + + lock (_lock) { - result[i] = SharedRandom.NextDouble(); + return array[_random.Next(array.Length)]; } - return result; } /// - /// 生成指定长度的随机字符串数组 + /// 随机打乱数组 /// - /// 生成的随机数组的长度 - /// 每个随机字符串的长度 - /// 生成的随机字符串数组 - public static string[] RandomStringArray(int length, int strLength) + /// 元素类型 + /// 数组 + /// 打乱后的数组 + public static T[] Shuffle(T[] array) { - string[] result = new string[length]; - for (int i = 0; i < length; i++) + if (array == null || array.Length <= 1) + return array; + + var result = (T[])array.Clone(); + lock (_lock) { - result[i] = RandomAlphanumericString(strLength); + for (int i = result.Length - 1; i > 0; i--) + { + int j = _random.Next(i + 1); + (result[i], result[j]) = (result[j], result[i]); + } } return result; } /// - /// 生成随机日期 + /// 生成安全随机整数 /// - /// 随机日期的最早时间 - /// 随机日期的最晚时间 - /// 生成的随机日期 - public static DateTime RandomDate(DateTime startDate, DateTime endDate) + /// 最小值 + /// 最大值(不包含) + /// 安全随机整数 + public static int NextSecure(int min, int max) { - TimeSpan timeSpan = endDate - startDate; - TimeSpan newSpan = new TimeSpan(0, 0, SharedRandom.Next(0, (int)timeSpan.TotalSeconds)); - return startDate + newSpan; + if (min >= max) + throw new ArgumentException("max must be greater than min"); + + var range = (long)max - min; + var bytes = new byte[4]; + + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + var randomValue = BitConverter.ToUInt32(bytes, 0); + + return (int)(min + (randomValue % range)); } /// - /// 生成随机枚举值 + /// 生成安全随机字节数组 /// - /// 枚举类型 - /// 生成的随机枚举值 - public static T RandomEnumValue() + /// 长度 + /// 安全随机字节数组 + public static byte[] NextSecureBytes(int length) { - Array values = Enum.GetValues(typeof(T)); - return (T)values.GetValue(SharedRandom.Next(values.Length)); + var bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return bytes; } /// - /// 获取一个指定范围内的随机整数 - /// 注意:返回值为 [minValue, maxValue] 闭区间,即同时包含最小值和最大值 - /// 与 RandomInt 方法的区别:RandomInt 使用左闭右开区间 [min, max),本方法使用闭区间 + /// 生成安全随机字符串 /// - /// 最小值(包含) - /// 最大值(包含) - /// 随机整数 - public static int GetRandomInt(int minValue, int maxValue) + /// 长度 + /// 字符集 + /// 安全随机字符串 + public static string NextSecureString(int length, string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") { - return SharedRandom.Next(minValue, maxValue + 1); + var result = new StringBuilder(length); + var charArray = chars.ToCharArray(); + + using var rng = RandomNumberGenerator.Create(); + var bytes = new byte[4]; + + for (int i = 0; i < length; i++) + { + rng.GetBytes(bytes); + var randomIndex = BitConverter.ToUInt32(bytes, 0) % (uint)charArray.Length; + result.Append(charArray[randomIndex]); + } + + return result.ToString(); } /// - /// 获取一个指定范围内的随机双精度浮点数 + /// 生成随机 GUID(不带连字符) /// - /// 最小值 - /// 最大值 - /// 随机双精度浮点数 - public static double GetRandomDouble(double minValue, double maxValue) + /// 随机 GUID 字符串 + public static string NextGuid() { - return minValue + (maxValue - minValue) * SharedRandom.NextDouble(); + return Guid.NewGuid().ToString("N"); } /// - /// 获取一个指定范围内的随机日期时间 + /// 生成随机 UUID /// - /// 最小值 - /// 最大值 - /// 随机日期时间 - public static DateTime GetRandomDateTime(DateTime minValue, DateTime maxValue) + /// UUID 字符串 + public static string NextUuid() { - TimeSpan timeSpan = maxValue - minValue; - double totalSeconds = timeSpan.TotalSeconds; - int randomSeconds = GetRandomInt(0, (int)totalSeconds); - return minValue.AddSeconds(randomSeconds); + return Guid.NewGuid().ToString(); } /// - /// 从给定的集合中随机选取一个元素 + /// 随机选择多个不重复的元素 /// /// 元素类型 - /// 集合 - /// 随机选取的元素 - public static T GetRandomElement(IEnumerable source) + /// 数组 + /// 选择数量 + /// 随机选择的元素数组 + public static T[] NextItems(T[] array, int count) { - if (source == null) + if (array == null || array.Length == 0) + return Array.Empty(); + + if (count >= array.Length) + return Shuffle(array); + + var shuffled = Shuffle(array); + var result = new T[count]; + Array.Copy(shuffled, result, count); + return result; + } + + /// + /// 根据权重随机选择 + /// + /// 元素类型 + /// 元素数组 + /// 权重数组 + /// 随机选择的元素 + public static T? NextWeighted(T[] items, int[] weights) + { + if (items == null || items.Length == 0) + return default; + + if (weights == null || weights.Length != items.Length) + throw new ArgumentException("Weights array must have the same length as items array"); + + var totalWeight = 0; + foreach (var w in weights) + { + if (w < 0) + throw new ArgumentException("Weights must be non-negative"); + totalWeight += w; + } + + if (totalWeight == 0) + return default; + + int randomValue; + lock (_lock) { - throw new ArgumentNullException(nameof(source)); + randomValue = _random.Next(totalWeight); } - int count = source.Count(); - if (count == 0) + var currentSum = 0; + for (int i = 0; i < items.Length; i++) { - throw new ArgumentException("集合中必须至少有一个元素", nameof(source)); + currentSum += weights[i]; + if (randomValue < currentSum) + return items[i]; } - int index = GetRandomInt(0, count - 1); - return source.ElementAt(index); + return items[^1]; } + #region 向后兼容方法别名 + /// - /// 生成指定长度的随机数字字符串 + /// 生成随机整数(Next 的别名) /// - /// 字符串长度 - /// 随机数字字符串 - [Obsolete("请使用 RandomDigitString 替代,两者功能相同")] - public static string RandomNumberString(int length) + public static int RandomInt(int min, int max) => Next(min, max); + + /// + /// 生成随机整数(Next 的别名) + /// + public static int RandomInt() => Next(); + + /// + /// 从数组中随机选择一个元素(NextItem 的别名) + /// + public static T? GetRandomElement(T[] array) => NextItem(array); + + /// + /// 从列表中随机选择一个元素 + /// + public static T? GetRandomElement(IList list) { - var sb = new StringBuilder(length); - for (int i = 0; i < length; i++) + if (list == null || list.Count == 0) + return default; + + lock (_lock) { - sb.Append(SharedRandom.Next(10)); + return list[_random.Next(list.Count)]; } - return sb.ToString(); } /// - /// 生成指定长度的随机字母数字字符串 + /// 从集合中随机选择一个元素 /// - /// 字符串长度 - /// 随机字母数字字符串 - [Obsolete("请使用 RandomAlphanumericString 替代,该方法实现较复杂且性能较差")] - public static string RandomString(int length) + public static T? GetRandomElement(IEnumerable collection) { - var sb = new StringBuilder(length); - for (int i = 0; i < length; i++) + if (collection == null) + return default; + + var list = collection.ToList(); + if (list.Count == 0) + return default; + + return GetRandomElement(list); + } + + /// + /// 生成随机数字字符串(NextNumericString 的别名) + /// + public static string RandomDigitString(int length) => NextNumericString(length); + + /// + /// 生成随机字符串(NextAlphanumericString 的别名) + /// + public static string RandomString(int length) => NextAlphanumericString(length); + + /// + /// 生成随机字母字符串(NextAlphaString 的别名) + /// + public static string RandomAlphaString(int length) => NextAlphaString(length); + + /// + /// 生成随机布尔值(NextBool 的别名) + /// + public static bool RandomBool() => NextBool(); + + /// + /// 生成随机日期时间 + /// + /// 最小日期 + /// 最大日期 + /// 随机日期时间 + public static DateTime GetRandomDateTime(DateTime minDate, DateTime maxDate) + { + if (minDate >= maxDate) + throw new ArgumentException("minDate must be less than maxDate"); + + var range = (maxDate - minDate).Ticks; + lock (_lock) { - int code = SharedRandom.Next(36) + 48; - if (code >= 58 && code <= 64) - { - code += 7; - } - if (code >= 91 && code <= 96) - { - code += 6; - } - sb.Append(Convert.ToChar(code)); + var ticks = (long)(_random.NextDouble() * range); + return minDate.AddTicks(ticks); } - return sb.ToString(); } + + /// + /// 生成随机日期时间(默认1970年至今) + /// + /// 随机日期时间 + public static DateTime GetRandomDateTime() + { + return GetRandomDateTime(new DateTime(1970, 1, 1), DateTime.Now); + } + + /// + /// 生成随机日期(不含时间) + /// + /// 最小日期 + /// 最大日期 + /// 随机日期 + public static DateTime GetRandomDate(DateTime minDate, DateTime maxDate) + { + return GetRandomDateTime(minDate, maxDate).Date; + } + + #endregion } -} \ No newline at end of file +} diff --git a/EasyTool.Core/MathCategory/StatisticsUtil.cs b/EasyTool.Core/MathCategory/StatisticsUtil.cs new file mode 100644 index 0000000..ab721fe --- /dev/null +++ b/EasyTool.Core/MathCategory/StatisticsUtil.cs @@ -0,0 +1,633 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 统计分析工具类 + /// 提供常用的统计分析功能 + /// + public static class StatisticsUtil + { + #region 基础统计 + + /// + /// 计算总和 + /// + public static double Sum(IEnumerable values) + { + return values.Sum(); + } + + /// + /// 计算平均值 + /// + public static double Mean(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + return list.Sum() / list.Count; + } + + /// + /// 计算中位数 + /// + public static double Median(IEnumerable values) + { + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + if (count % 2 == 0) + { + return (sorted[count / 2 - 1] + sorted[count / 2]) / 2.0; + } + + return sorted[count / 2]; + } + + /// + /// 计算众数(出现频率最高的值) + /// + public static List Mode(IEnumerable values) + { + var groups = values.GroupBy(v => v) + .OrderByDescending(g => g.Count()) + .ToList(); + + if (groups.Count == 0) return new List(); + + var maxCount = groups[0].Count(); + return groups.Where(g => g.Count() == maxCount) + .Select(g => g.Key) + .ToList(); + } + + /// + /// 计算极差(最大值-最小值) + /// + public static double Range(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + return list.Max() - list.Min(); + } + + /// + /// 计算最小值 + /// + public static double Min(IEnumerable values) + { + return values.Min(); + } + + /// + /// 计算最大值 + /// + public static double Max(IEnumerable values) + { + return values.Max(); + } + + /// + /// 计算计数 + /// + public static int Count(IEnumerable values) + { + return values.Count(); + } + + #endregion + + #region 离散程度 + + /// + /// 计算方差(总体方差) + /// + public static double Variance(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + + var mean = Mean(list); + var sumSquaredDiff = list.Sum(v => Math.Pow(v - mean, 2)); + return sumSquaredDiff / list.Count; + } + + /// + /// 计算样本方差 + /// + public static double SampleVariance(IEnumerable values) + { + var list = values.ToList(); + if (list.Count <= 1) return 0; + + var mean = Mean(list); + var sumSquaredDiff = list.Sum(v => Math.Pow(v - mean, 2)); + return sumSquaredDiff / (list.Count - 1); + } + + /// + /// 计算标准差(总体标准差) + /// + public static double StandardDeviation(IEnumerable values) + { + return Math.Sqrt(Variance(values)); + } + + /// + /// 计算样本标准差 + /// + public static double SampleStandardDeviation(IEnumerable values) + { + return Math.Sqrt(SampleVariance(values)); + } + + /// + /// 计算变异系数(标准差/平均值) + /// + public static double CoefficientOfVariation(IEnumerable values) + { + var list = values.ToList(); + var mean = Mean(list); + if (mean == 0) return 0; + return StandardDeviation(list) / mean; + } + + /// + /// 计算平均绝对偏差 + /// + public static double MeanAbsoluteDeviation(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + + var mean = Mean(list); + return list.Average(v => Math.Abs(v - mean)); + } + + /// + /// 计算四分位数 + /// + /// 数据集 + /// 四分位数类型(1=Q1, 2=Q2/中位数, 3=Q3) + /// 四分位数值 + public static double Quartile(IEnumerable values, int q) + { + if (q < 1 || q > 3) + throw new ArgumentException("四分位数参数q必须为1、2或3", nameof(q)); + + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + if (q == 2) return Median(sorted); + + double position; + if (q == 1) + position = (count + 1) / 4.0; + else + position = 3 * (count + 1) / 4.0; + + var lowerIndex = (int)Math.Floor(position) - 1; + var upperIndex = (int)Math.Ceiling(position) - 1; + var fraction = position - Math.Floor(position); + + if (lowerIndex == upperIndex || fraction == 0) + return sorted[Math.Max(0, Math.Min(count - 1, lowerIndex))]; + + lowerIndex = Math.Max(0, Math.Min(count - 1, lowerIndex)); + upperIndex = Math.Max(0, Math.Min(count - 1, upperIndex)); + + return sorted[lowerIndex] * (1 - fraction) + sorted[upperIndex] * fraction; + } + + /// + /// 计算四分位距(IQR = Q3 - Q1) + /// + public static double InterquartileRange(IEnumerable values) + { + return Quartile(values, 3) - Quartile(values, 1); + } + + #endregion + + #region 百分位数 + + /// + /// 计算百分位数 + /// + /// 数据集 + /// 百分位(0-100) + /// 百分位数值 + public static double Percentile(IEnumerable values, double percentile) + { + if (percentile < 0 || percentile > 100) + throw new ArgumentException("百分位必须在0-100之间", nameof(percentile)); + + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + var position = (percentile / 100.0) * (count - 1); + var lowerIndex = (int)Math.Floor(position); + var upperIndex = (int)Math.Ceiling(position); + var fraction = position - lowerIndex; + + if (lowerIndex == upperIndex) + return sorted[lowerIndex]; + + return sorted[lowerIndex] * (1 - fraction) + sorted[upperIndex] * fraction; + } + + /// + /// 计算百分等级(某个值在数据集中的百分位) + /// + public static double PercentileRank(IEnumerable values, double value) + { + var list = values.ToList(); + var lessCount = list.Count(v => v < value); + var equalCount = list.Count(v => v == value); + var totalCount = list.Count; + + if (totalCount == 0) return 0; + + // 使用线性插值法 + return (lessCount + 0.5 * equalCount) / totalCount * 100; + } + + #endregion + + #region 分布形状 + + /// + /// 计算偏度(Skewness) + /// 正偏度表示右偏,负偏度表示左偏 + /// + public static double Skewness(IEnumerable values) + { + var list = values.ToList(); + if (list.Count < 3) return 0; + + var mean = Mean(list); + var stdDev = StandardDeviation(list); + if (stdDev == 0) return 0; + + var n = list.Count; + var sumCubedDiff = list.Sum(v => Math.Pow((v - mean) / stdDev, 3)); + + return (n / ((n - 1) * (n - 2))) * sumCubedDiff; + } + + /// + /// 计算峰度(Kurtosis) + /// 正态分布峰度为0,大于0表示尖峰,小于0表示平峰 + /// + public static double Kurtosis(IEnumerable values) + { + var list = values.ToList(); + if (list.Count < 4) return 0; + + var mean = Mean(list); + var stdDev = StandardDeviation(list); + if (stdDev == 0) return 0; + + var n = list.Count; + var sumFourthPower = list.Sum(v => Math.Pow((v - mean) / stdDev, 4)); + + return (n * (n + 1) / ((n - 1) * (n - 2) * (n - 3))) * sumFourthPower + - (3 * Math.Pow(n - 1, 2)) / ((n - 2) * (n - 3)); + } + + #endregion + + #region 协方差和相关系数 + + /// + /// 计算协方差 + /// + public static double Covariance(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + if (xList.Count == 0) return 0; + + var meanX = Mean(xList); + var meanY = Mean(yList); + var n = xList.Count; + + return xList.Zip(yList, (xi, yi) => (xi - meanX) * (yi - meanY)).Sum() / n; + } + + /// + /// 计算皮尔逊相关系数 + /// + public static double PearsonCorrelation(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + if (xList.Count == 0) return 0; + + var stdDevX = StandardDeviation(xList); + var stdDevY = StandardDeviation(yList); + + if (stdDevX == 0 || stdDevY == 0) return 0; + + return Covariance(xList, yList) / (stdDevX * stdDevY); + } + + /// + /// 计算斯皮尔曼等级相关系数 + /// + public static double SpearmanCorrelation(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + // 转换为秩 + var xRanks = GetRanks(xList); + var yRanks = GetRanks(yList); + + return PearsonCorrelation(xRanks, yRanks); + } + + private static List GetRanks(List values) + { + var sorted = values.Select((v, i) => new { Value = v, Index = i }) + .OrderBy(x => x.Value) + .ToList(); + + var ranks = new double[values.Count]; + for (int i = 0; i < sorted.Count; i++) + { + // 处理相同值的平均秩 + var sameValues = sorted.Where(s => s.Value == sorted[i].Value).ToList(); + var avgRank = sameValues.Select(s => s.Index).Average(); + ranks[sorted[i].Index] = avgRank + 1; + } + + return ranks.ToList(); + } + + #endregion + + #region 回归分析 + + /// + /// 简单线性回归 + /// + /// 斜率和截距 + public static (double Slope, double Intercept) LinearRegression(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + var n = xList.Count; + if (n == 0) return (0, 0); + + var meanX = Mean(xList); + var meanY = Mean(yList); + var stdDevX = StandardDeviation(xList); + var stdDevY = StandardDeviation(yList); + + if (stdDevX == 0) return (0, meanY); + + var correlation = PearsonCorrelation(xList, yList); + var slope = correlation * stdDevY / stdDevX; + var intercept = meanY - slope * meanX; + + return (slope, intercept); + } + + /// + /// 使用回归模型预测 + /// + public static double Predict(double x, double slope, double intercept) + { + return slope * x + intercept; + } + + /// + /// 计算R平方(决定系数) + /// + public static double RSquared(IEnumerable actual, IEnumerable predicted) + { + var actualList = actual.ToList(); + var predictedList = predicted.ToList(); + + if (actualList.Count != predictedList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + var mean = Mean(actualList); + var ssTotal = actualList.Sum(a => Math.Pow(a - mean, 2)); + var ssResidual = actualList.Zip(predictedList, (a, p) => Math.Pow(a - p, 2)).Sum(); + + if (ssTotal == 0) return 1; + + return 1 - (ssResidual / ssTotal); + } + + #endregion + + #region 描述性统计 + + /// + /// 获取完整统计摘要 + /// + public static StatisticsSummary GetSummary(IEnumerable values) + { + var list = values.ToList(); + + return new StatisticsSummary + { + Count = list.Count, + Sum = Sum(list), + Mean = Mean(list), + Median = Median(list), + Mode = Mode(list), + Min = Min(list), + Max = Max(list), + Range = Range(list), + Variance = Variance(list), + StandardDeviation = StandardDeviation(list), + SampleVariance = SampleVariance(list), + SampleStandardDeviation = SampleStandardDeviation(list), + Q1 = Quartile(list, 1), + Q3 = Quartile(list, 3), + IQR = InterquartileRange(list), + Skewness = Skewness(list), + Kurtosis = Kurtosis(list), + CoefficientOfVariation = CoefficientOfVariation(list) + }; + } + + #endregion + + #region 异常值检测 + + /// + /// 使用IQR方法检测异常值 + /// + public static List DetectOutliersIQR(IEnumerable values, double multiplier = 1.5) + { + var list = values.ToList(); + var q1 = Quartile(list, 1); + var q3 = Quartile(list, 3); + var iqr = q3 - q1; + + var lowerBound = q1 - multiplier * iqr; + var upperBound = q3 + multiplier * iqr; + + return list.Where(v => v < lowerBound || v > upperBound).ToList(); + } + + /// + /// 使用Z-Score方法检测异常值 + /// + public static List DetectOutliersZScore(IEnumerable values, double threshold = 3.0) + { + var list = values.ToList(); + var mean = Mean(list); + var stdDev = StandardDeviation(list); + + if (stdDev == 0) return new List(); + + return list.Where(v => Math.Abs((v - mean) / stdDev) > threshold).ToList(); + } + + /// + /// 计算Z-Score + /// + public static List ZScore(IEnumerable values) + { + var list = values.ToList(); + var mean = Mean(list); + var stdDev = StandardDeviation(list); + + if (stdDev == 0) return list.Select(_ => 0.0).ToList(); + + return list.Select(v => (v - mean) / stdDev).ToList(); + } + + #endregion + } + + /// + /// 统计摘要 + /// + public class StatisticsSummary + { + /// + /// 计数 + /// + public int Count { get; set; } + + /// + /// 总和 + /// + public double Sum { get; set; } + + /// + /// 平均值 + /// + public double Mean { get; set; } + + /// + /// 中位数 + /// + public double Median { get; set; } + + /// + /// 众数 + /// + public List Mode { get; set; } = new(); + + /// + /// 最小值 + /// + public double Min { get; set; } + + /// + /// 最大值 + /// + public double Max { get; set; } + + /// + /// 极差 + /// + public double Range { get; set; } + + /// + /// 总体方差 + /// + public double Variance { get; set; } + + /// + /// 总体标准差 + /// + public double StandardDeviation { get; set; } + + /// + /// 样本方差 + /// + public double SampleVariance { get; set; } + + /// + /// 样本标准差 + /// + public double SampleStandardDeviation { get; set; } + + /// + /// 第一四分位数 + /// + public double Q1 { get; set; } + + /// + /// 第三四分位数 + /// + public double Q3 { get; set; } + + /// + /// 四分位距 + /// + public double IQR { get; set; } + + /// + /// 偏度 + /// + public double Skewness { get; set; } + + /// + /// 峰度 + /// + public double Kurtosis { get; set; } + + /// + /// 变异系数 + /// + public double CoefficientOfVariation { get; set; } + + public override string ToString() + { + return $"统计摘要: N={Count}, 均值={Mean:F4}, 标准差={StandardDeviation:F4}, 中位数={Median:F4}, 范围=[{Min:F4}, {Max:F4}]"; + } + } +} diff --git a/EasyTool.Core/MediaCategory/AudioUtil.cs b/EasyTool.Core/MediaCategory/AudioUtil.cs new file mode 100644 index 0000000..e23a486 --- /dev/null +++ b/EasyTool.Core/MediaCategory/AudioUtil.cs @@ -0,0 +1,229 @@ +using System; +using System.IO; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace EasyTool.MediaCategory +{ + /// + /// 音频工具类 + /// 提供音频转换、提取、处理等功能 + /// 需要安装 FFmpeg + /// + public static class AudioUtil + { + /// + /// FFmpeg 可执行文件路径 + /// + public static string? FFmpegPath { get; set; } + + /// + /// 转换音频格式 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 输出格式(mp3, wav, aac, flac 等) + /// 比特率(如 "128k", "256k") + /// 采样率(如 44100, 48000) + /// 是否成功 + public static bool Convert(string inputPath, string outputPath, string format, string? bitrate = null, int? sampleRate = null) + { + var args = $"-i \"{inputPath}\""; + + if (!string.IsNullOrEmpty(bitrate)) + args += $" -b:a {bitrate}"; + + if (sampleRate.HasValue) + args += $" -ar {sampleRate.Value}"; + + args += $" -f {format} \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 异步转换音频格式 + /// + public static async Task ConvertAsync(string inputPath, string outputPath, string format, string? bitrate = null, int? sampleRate = null) + { + return await Task.Run(() => Convert(inputPath, outputPath, format, bitrate, sampleRate)); + } + + /// + /// 从视频中提取音频 + /// + /// 视频文件路径 + /// 输出音频路径 + /// 输出格式 + /// 比特率 + /// 是否成功 + public static bool ExtractFromVideo(string videoPath, string outputPath, string format = "mp3", string? bitrate = "192k") + { + var args = $"-i \"{videoPath}\" -vn"; + + if (!string.IsNullOrEmpty(bitrate)) + args += $" -b:a {bitrate}"; + + args += $" -f {format} \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 裁剪音频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 开始时间 + /// 持续时间 + /// 是否成功 + public static bool Trim(string inputPath, string outputPath, TimeSpan startTime, TimeSpan duration) + { + var args = $"-i \"{inputPath}\" -ss {startTime:hh\\:mm\\:ss\\.fff} -t {duration:hh\\:mm\\:ss\\.fff} -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 合并音频文件 + /// + /// 输入文件路径列表 + /// 输出文件路径 + /// 是否成功 + public static bool Merge(string[] inputPaths, string outputPath) + { + // 创建临时文件列表 + var tempListPath = Path.Combine(Path.GetTempPath(), $"ffmpeg_list_{Guid.NewGuid():N}.txt"); + using (var writer = new StreamWriter(tempListPath)) + { + foreach (var path in inputPaths) + { + writer.WriteLine($"file '{path}'"); + } + } + + try + { + var args = $"-f concat -safe 0 -i \"{tempListPath}\" -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + finally + { + File.Delete(tempListPath); + } + } + + /// + /// 调整音量 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 音量因子(1.0 = 原音量,2.0 = 两倍,0.5 = 一半) + /// 是否成功 + public static bool AdjustVolume(string inputPath, string outputPath, double volumeFactor) + { + var args = $"-i \"{inputPath}\" -af \"volume={volumeFactor}\" \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 获取音频信息 + /// + /// 音频文件路径 + /// 音频信息 + public static AudioInfo? GetInfo(string filePath) + { + var args = $"-i \"{filePath}\" -hide_banner -show_format -show_streams -of json"; + var result = ExecuteFFmpegProbe(args); + + if (string.IsNullOrEmpty(result)) + return null; + + try + { + var json = System.Text.Json.JsonDocument.Parse(result); + var format = json.RootElement.GetProperty("format"); + + return new AudioInfo + { + Duration = TimeSpan.FromSeconds(double.Parse(format.GetProperty("duration").GetString() ?? "0")), + BitRate = long.Parse(format.GetProperty("bit_rate").GetString() ?? "0"), + Format = format.GetProperty("format_name").GetString() ?? "", + Size = long.Parse(format.GetProperty("size").GetString() ?? "0") + }; + } + catch + { + return null; + } + } + + private static bool ExecuteFFmpeg(string arguments) + { + var ffmpeg = FFmpegPath ?? "ffmpeg"; + var psi = new ProcessStartInfo + { + FileName = ffmpeg, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return false; + + process.WaitForExit(); + return process.ExitCode == 0; + } + + private static string? ExecuteFFmpegProbe(string arguments) + { + var ffprobe = FFmpegPath ?? "ffprobe"; + var probePath = ffprobe.Replace("ffmpeg", "ffprobe"); + + var psi = new ProcessStartInfo + { + FileName = probePath, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return null; + + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return output; + } + } + + /// + /// 音频信息 + /// + public class AudioInfo + { + /// + /// 时长 + /// + public TimeSpan Duration { get; set; } + + /// + /// 比特率 + /// + public long BitRate { get; set; } + + /// + /// 格式 + /// + public string Format { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long Size { get; set; } + } +} diff --git a/EasyTool.Core/MediaCategory/VideoUtil.cs b/EasyTool.Core/MediaCategory/VideoUtil.cs new file mode 100644 index 0000000..04729f1 --- /dev/null +++ b/EasyTool.Core/MediaCategory/VideoUtil.cs @@ -0,0 +1,363 @@ +using System; +using System.IO; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace EasyTool.MediaCategory +{ + /// + /// 视频工具类 + /// 提供视频转换、剪辑、处理等功能 + /// 需要安装 FFmpeg + /// + public static class VideoUtil + { + /// + /// FFmpeg 可执行文件路径 + /// + public static string? FFmpegPath { get; set; } + + /// + /// 转换视频格式 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 视频编码器(libx264, libx265, vp9 等) + /// 音频编码器(aac, mp3, opus 等) + /// 视频质量(0-51,越小质量越高,默认23) + /// 是否成功 + public static bool Convert(string inputPath, string outputPath, string? videoCodec = null, string? audioCodec = null, int? crf = null) + { + var args = $"-i \"{inputPath}\""; + + if (!string.IsNullOrEmpty(videoCodec)) + args += $" -c:v {videoCodec}"; + else + args += " -c:v libx264"; + + if (!string.IsNullOrEmpty(audioCodec)) + args += $" -c:a {audioCodec}"; + else + args += " -c:a aac"; + + if (crf.HasValue) + args += $" -crf {crf.Value}"; + else + args += " -crf 23"; + + args += $" \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 异步转换视频格式 + /// + public static async Task ConvertAsync(string inputPath, string outputPath, string? videoCodec = null, string? audioCodec = null, int? crf = null) + { + return await Task.Run(() => Convert(inputPath, outputPath, videoCodec, audioCodec, crf)); + } + + /// + /// 压缩视频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 质量(1-100,越小压缩率越高) + /// 是否成功 + public static bool Compress(string inputPath, string outputPath, int quality = 50) + { + var crf = 51 - (quality * 51 / 100); + var args = $"-i \"{inputPath}\" -c:v libx264 -crf {crf} -c:a aac -b:a 128k \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 裁剪视频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 开始时间 + /// 持续时间 + /// 是否成功 + public static bool Trim(string inputPath, string outputPath, TimeSpan startTime, TimeSpan duration) + { + var args = $"-i \"{inputPath}\" -ss {startTime:hh\\:mm\\:ss\\.fff} -t {duration:hh\\:mm\\:ss\\.fff} -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 合并视频文件 + /// + /// 输入文件路径列表 + /// 输出文件路径 + /// 是否成功 + public static bool Merge(string[] inputPaths, string outputPath) + { + var tempListPath = Path.Combine(Path.GetTempPath(), $"ffmpeg_list_{Guid.NewGuid():N}.txt"); + using (var writer = new StreamWriter(tempListPath)) + { + foreach (var path in inputPaths) + { + writer.WriteLine($"file '{path}'"); + } + } + + try + { + var args = $"-f concat -safe 0 -i \"{tempListPath}\" -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + finally + { + File.Delete(tempListPath); + } + } + + /// + /// 提取视频帧为图片 + /// + /// 视频文件路径 + /// 输出目录 + /// 每秒帧数(默认1,即每秒1帧) + /// 图片格式(jpg, png) + /// 是否成功 + public static bool ExtractFrames(string videoPath, string outputDirectory, int fps = 1, string imageFormat = "jpg") + { + Directory.CreateDirectory(outputDirectory); + var args = $"-i \"{videoPath}\" -vf fps={fps} \"{outputDirectory}/frame_%04d.{imageFormat}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 从图片创建视频 + /// + /// 图片目录 + /// 输出视频路径 + /// 帧率 + /// 图片文件模式(如 "frame_%04d.jpg") + /// 是否成功 + public static bool CreateFromImages(string imageDirectory, string outputPath, int fps = 30, string imagePattern = "frame_%04d.jpg") + { + var args = $"-framerate {fps} -i \"{imageDirectory}/{imagePattern}\" -c:v libx264 -pix_fmt yuv420p \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 添加水印 + /// + /// 视频文件路径 + /// 水印图片路径 + /// 输出文件路径 + /// 水印位置 + /// 透明度(0-1) + /// 是否成功 + public static bool AddWatermark(string videoPath, string watermarkPath, string outputPath, WatermarkPosition position = WatermarkPosition.BottomRight, double opacity = 1.0) + { + var overlay = position switch + { + WatermarkPosition.TopLeft => "0:0", + WatermarkPosition.TopRight => "main_w-overlay_w-10:10", + WatermarkPosition.BottomLeft => "10:main_h-overlay_h-10", + WatermarkPosition.BottomRight => "main_w-overlay_w-10:main_h-overlay_h-10", + WatermarkPosition.Center => "(main_w-overlay_w)/2:(main_h-overlay_h)/2", + _ => "main_w-overlay_w-10:main_h-overlay_h-10" + }; + + var args = $"-i \"{videoPath}\" -i \"{watermarkPath}\" -filter_complex \"[1:v]format=rgba,colorchannelmixer=aa={opacity}[logo];[0:v][logo]overlay={overlay}\" -c:a copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 调整视频分辨率 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 目标宽度 + /// 目标高度 + /// 是否成功 + public static bool Resize(string inputPath, string outputPath, int width, int height) + { + var args = $"-i \"{inputPath}\" -vf scale={width}:{height} -c:a copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 获取视频信息 + /// + /// 视频文件路径 + /// 视频信息 + public static VideoInfo? GetInfo(string filePath) + { + var args = $"-i \"{filePath}\" -hide_banner -show_format -show_streams -of json"; + var result = ExecuteFFmpegProbe(args); + + if (string.IsNullOrEmpty(result)) + return null; + + try + { + var json = System.Text.Json.JsonDocument.Parse(result); + var format = json.RootElement.GetProperty("format"); + + var info = new VideoInfo + { + Duration = TimeSpan.FromSeconds(double.Parse(format.GetProperty("duration").GetString() ?? "0")), + BitRate = long.Parse(format.GetProperty("bit_rate").GetString() ?? "0"), + Format = format.GetProperty("format_name").GetString() ?? "", + Size = long.Parse(format.GetProperty("size").GetString() ?? "0") + }; + + // 获取视频流信息 + var streams = json.RootElement.GetProperty("streams"); + foreach (var stream in streams.EnumerateArray()) + { + if (stream.GetProperty("codec_type").GetString() == "video") + { + info.Width = stream.GetProperty("width").GetInt32(); + info.Height = stream.GetProperty("height").GetInt32(); + info.VideoCodec = stream.GetProperty("codec_name").GetString() ?? ""; + if (stream.TryGetProperty("r_frame_rate", out var frameRate)) + { + var fpsStr = frameRate.GetString() ?? "0/1"; + var parts = fpsStr.Split('/'); + if (parts.Length == 2 && int.TryParse(parts[1], out var denom) && denom > 0) + { + info.FrameRate = double.Parse(parts[0]) / denom; + } + } + break; + } + } + + return info; + } + catch + { + return null; + } + } + + /// + /// 生成 GIF + /// + /// 视频文件路径 + /// 输出 GIF 路径 + /// 开始时间 + /// 持续时间 + /// 宽度(默认320) + /// 帧率(默认10) + /// 是否成功 + public static bool CreateGif(string videoPath, string outputPath, TimeSpan startTime, TimeSpan duration, int width = 320, int fps = 10) + { + var args = $"-i \"{videoPath}\" -ss {startTime:hh\\:mm\\:ss} -t {duration:hh\\:mm\\:ss} -vf \"fps={fps},scale={width}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\" \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + private static bool ExecuteFFmpeg(string arguments) + { + var ffmpeg = FFmpegPath ?? "ffmpeg"; + var psi = new ProcessStartInfo + { + FileName = ffmpeg, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return false; + + process.WaitForExit(); + return process.ExitCode == 0; + } + + private static string? ExecuteFFmpegProbe(string arguments) + { + var ffprobe = FFmpegPath ?? "ffprobe"; + var probePath = ffprobe.Replace("ffmpeg", "ffprobe"); + + var psi = new ProcessStartInfo + { + FileName = probePath, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return null; + + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return output; + } + } + + /// + /// 视频信息 + /// + public class VideoInfo + { + /// + /// 时长 + /// + public TimeSpan Duration { get; set; } + + /// + /// 比特率 + /// + public long BitRate { get; set; } + + /// + /// 格式 + /// + public string Format { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long Size { get; set; } + + /// + /// 视频宽度 + /// + public int Width { get; set; } + + /// + /// 视频高度 + /// + public int Height { get; set; } + + /// + /// 视频编码 + /// + public string VideoCodec { get; set; } = string.Empty; + + /// + /// 帧率 + /// + public double FrameRate { get; set; } + + /// + /// 分辨率字符串 + /// + public string Resolution => $"{Width}x{Height}"; + } + + /// + /// 水印位置 + /// + public enum WatermarkPosition + { + TopLeft, + TopRight, + BottomLeft, + BottomRight, + Center + } +} diff --git a/EasyTool.Core/NetCategory/DnsServerUtil.cs b/EasyTool.Core/NetCategory/DnsServerUtil.cs new file mode 100644 index 0000000..5b1706e --- /dev/null +++ b/EasyTool.Core/NetCategory/DnsServerUtil.cs @@ -0,0 +1,511 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// DNS 记录类型 + /// + public enum DnsRecordType + { + A = 1, + NS = 2, + CNAME = 5, + SOA = 6, + PTR = 12, + MX = 15, + TXT = 16, + AAAA = 28 + } + + /// + /// DNS 记录 + /// + public class DnsRecord + { + /// + /// 记录名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 记录类型 + /// + public DnsRecordType Type { get; set; } + + /// + /// TTL(秒) + /// + public int Ttl { get; set; } + + /// + /// 记录值 + /// + public string Value { get; set; } = string.Empty; + + /// + /// MX 优先级(仅 MX 记录) + /// + public int? Priority { get; set; } + + public override string ToString() + { + var priority = Priority.HasValue ? $" {Priority}" : ""; + return $"{Name} {Ttl} IN {Type} {priority}{Value}"; + } + } + + /// + /// DNS 查询选项 + /// + public class DnsQueryOptions + { + /// + /// DNS 服务器地址 + /// + public IPAddress DnsServer { get; set; } = IPAddress.Parse("8.8.8.8"); + + /// + /// DNS 服务器端口 + /// + public int Port { get; set; } = 53; + + /// + /// 查询超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 是否使用 TCP + /// + public bool UseTcp { get; set; } + + /// + /// 是否递归查询 + /// + public bool RecursionDesired { get; set; } = true; + } + + /// + /// DNS 工具类 + /// 提供 DNS 查询和解析功能 + /// + public static class DnsServerUtil + { + private static readonly Random _random = new(); + + /// + /// 查询 A 记录 + /// + /// 域名 + /// 查询选项 + /// IP 地址列表 + public static async Task> QueryAAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.A, options); + + return records + .Select(r => IPAddress.TryParse(r.Value, out var ip) ? ip : null) + .Where(ip => ip != null) + .Cast() + .ToList(); + } + + /// + /// 查询 AAAA 记录 + /// + /// 域名 + /// 查询选项 + /// IPv6 地址列表 + public static async Task> QueryAaaaAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.AAAA, options); + + return records + .Select(r => IPAddress.TryParse(r.Value, out var ip) ? ip : null) + .Where(ip => ip != null) + .Cast() + .ToList(); + } + + /// + /// 查询 MX 记录 + /// + /// 域名 + /// 查询选项 + /// MX 记录列表 + public static async Task> QueryMxAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.MX, options); + + return records + .Where(r => r.Priority.HasValue) + .Select(r => (Priority: r.Priority!.Value, MailServer: r.Value)) + .OrderBy(r => r.Priority) + .ToList(); + } + + /// + /// 查询 TXT 记录 + /// + /// 域名 + /// 查询选项 + /// TXT 记录列表 + public static async Task> QueryTxtAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.TXT, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 查询 CNAME 记录 + /// + /// 域名 + /// 查询选项 + /// CNAME 目标 + public static async Task QueryCnameAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.CNAME, options); + return records.FirstOrDefault()?.Value; + } + + /// + /// 查询 NS 记录 + /// + /// 域名 + /// 查询选项 + /// NS 服务器列表 + public static async Task> QueryNsAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.NS, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 反向查询(IP 到域名) + /// + /// IP 地址 + /// 查询选项 + /// 域名列表 + public static async Task> ReverseQueryAsync(IPAddress ipAddress, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + + // 构建反向查询域名 + var bytes = ipAddress.GetAddressBytes(); + Array.Reverse(bytes); + var ptrDomain = $"{string.Join(".", bytes)}.in-addr.arpa"; + + var records = await QueryAsync(ptrDomain, DnsRecordType.PTR, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 通用 DNS 查询 + /// + /// 域名 + /// 记录类型 + /// 查询选项 + /// DNS 记录列表 + public static async Task> QueryAsync(string domain, DnsRecordType recordType, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + + // 构建查询包 + var queryPacket = BuildQueryPacket(domain, recordType, options.RecursionDesired); + + // 发送查询 + byte[] responseBytes; + + if (options.UseTcp) + { + responseBytes = await QueryOverTcpAsync(queryPacket, options); + } + else + { + responseBytes = await QueryOverUdpAsync(queryPacket, options); + } + + // 解析响应 + return ParseResponse(responseBytes); + } + + /// + /// 批量查询 + /// + /// 域名列表 + /// 记录类型 + /// 查询选项 + /// 域名到记录列表的映射 + public static async Task>> QueryManyAsync( + IEnumerable domains, + DnsRecordType recordType, + DnsQueryOptions? options = null) + { + var result = new Dictionary>(); + + foreach (var domain in domains) + { + result[domain] = await QueryAsync(domain, recordType, options); + } + + return result; + } + + /// + /// 获取本机 DNS 服务器 + /// + /// DNS 服务器列表 + public static List GetLocalDnsServers() + { + var servers = new List(); + + try + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var iface in interfaces) + { + if (iface.OperationalStatus != OperationalStatus.Up) + continue; + + var ipProps = iface.GetIPProperties(); + var dnsAddresses = ipProps.DnsAddresses; + foreach (var dns in dnsAddresses) + { + if (!servers.Contains(dns)) + { + servers.Add(dns); + } + } + } + } + catch + { + // 返回默认 DNS 服务器 + servers.Add(IPAddress.Parse("8.8.8.8")); + servers.Add(IPAddress.Parse("8.8.4.4")); + } + + return servers; + } + + #region 私有方法 + + private static byte[] BuildQueryPacket(string domain, DnsRecordType recordType, bool recursionDesired) + { + using var stream = new System.IO.MemoryStream(); + using var writer = new BinaryWriter(stream); + + // Transaction ID + writer.Write((ushort)_random.Next(0, 65536)); + + // Flags + var flags = (ushort)0x0100; // Standard query + if (recursionDesired) + flags |= 0x0100; + writer.Write(flags); + + // Questions count + writer.Write((ushort)1); + + // Answer, Authority, Additional counts + writer.Write((ushort)0); + writer.Write((ushort)0); + writer.Write((ushort)0); + + // Question section + WriteDomainName(writer, domain); + writer.Write((ushort)recordType); + writer.Write((ushort)1); // Class IN + + return stream.ToArray(); + } + + private static void WriteDomainName(BinaryWriter writer, string domain) + { + var parts = domain.Split('.'); + foreach (var part in parts) + { + var bytes = Encoding.ASCII.GetBytes(part); + writer.Write((byte)bytes.Length); + writer.Write(bytes); + } + writer.Write((byte)0); + } + + private static async Task QueryOverUdpAsync(byte[] query, DnsQueryOptions options) + { + using var client = new UdpClient(); + client.Client.ReceiveTimeout = (int)options.Timeout.TotalMilliseconds; + + await client.SendAsync(query, query.Length, options.DnsServer.ToString(), options.Port); + + var result = await client.ReceiveAsync(); + return result.Buffer; + } + + private static async Task QueryOverTcpAsync(byte[] query, DnsQueryOptions options) + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(options.DnsServer, options.Port); + + if (await Task.WhenAny(connectTask, Task.Delay(options.Timeout)) != connectTask) + { + throw new TimeoutException("DNS 查询超时"); + } + + await connectTask; + + var stream = client.GetStream(); + stream.ReadTimeout = (int)options.Timeout.TotalMilliseconds; + stream.WriteTimeout = (int)options.Timeout.TotalMilliseconds; + + // TCP DNS 需要在前面加 2 字节长度 + var lengthBytes = BitConverter.GetBytes((ushort)query.Length); + Array.Reverse(lengthBytes); // Big-endian + + await stream.WriteAsync(lengthBytes, 0, 2); + await stream.WriteAsync(query, 0, query.Length); + + // 读取响应 + var responseLengthBytes = new byte[2]; + await stream.ReadAsync(responseLengthBytes, 0, 2); + Array.Reverse(responseLengthBytes); + var responseLength = BitConverter.ToUInt16(responseLengthBytes, 0); + + var response = new byte[responseLength]; + await stream.ReadAsync(response, 0, responseLength); + + return response; + } + + private static List ParseResponse(byte[] response) + { + var records = new List(); + + using var stream = new System.IO.MemoryStream(response); + using var reader = new BinaryReader(stream); + + // 跳过 Header (12 bytes) + reader.ReadBytes(12); + + // Question count + var questionCount = reader.ReadUInt16(); + for (int i = 0; i < questionCount; i++) + { + ReadDomainName(reader); + reader.ReadUInt16(); // Type + reader.ReadUInt16(); // Class + } + + // Answer count + var answerCount = reader.ReadUInt16(); + for (int i = 0; i < answerCount; i++) + { + var name = ReadDomainName(reader); + var type = (DnsRecordType)reader.ReadUInt16(); + reader.ReadUInt16(); // Class + var ttl = (int)reader.ReadUInt32(); + var dataLength = reader.ReadUInt16(); + var dataPosition = stream.Position; + + var record = new DnsRecord + { + Name = name, + Type = type, + Ttl = ttl + }; + + switch (type) + { + case DnsRecordType.A: + var aBytes = reader.ReadBytes(4); + record.Value = new IPAddress(aBytes).ToString(); + break; + + case DnsRecordType.AAAA: + var aaaaBytes = reader.ReadBytes(16); + record.Value = new IPAddress(aaaaBytes).ToString(); + break; + + case DnsRecordType.CNAME: + case DnsRecordType.NS: + case DnsRecordType.PTR: + record.Value = ReadDomainName(reader); + break; + + case DnsRecordType.MX: + record.Priority = reader.ReadUInt16(); + record.Value = ReadDomainName(reader); + break; + + case DnsRecordType.TXT: + var txtLength = reader.ReadByte(); + record.Value = Encoding.ASCII.GetString(reader.ReadBytes(txtLength)); + break; + + default: + stream.Position = dataPosition + dataLength; + break; + } + + records.Add(record); + } + + return records; + } + + private static string ReadDomainName(BinaryReader reader) + { + var labels = new List(); + var visited = new HashSet(); + + while (true) + { + var length = reader.ReadByte(); + + if (length == 0) + break; + + // 指针压缩 + if ((length & 0xC0) == 0xC0) + { + var offset = ((length & 0x3F) << 8) | reader.ReadByte(); + if (visited.Contains(offset)) + break; + + visited.Add(offset); + var currentPos = reader.BaseStream.Position; + reader.BaseStream.Position = offset; + + var pointerLabel = ReadDomainName(reader); + labels.Add(pointerLabel); + + reader.BaseStream.Position = currentPos; + break; + } + + labels.Add(Encoding.ASCII.GetString(reader.ReadBytes(length))); + } + + return string.Join(".", labels); + } + + #endregion + } +} diff --git a/EasyTool.Core/NetCategory/GrpcUtil.cs b/EasyTool.Core/NetCategory/GrpcUtil.cs new file mode 100644 index 0000000..15b5ca0 --- /dev/null +++ b/EasyTool.Core/NetCategory/GrpcUtil.cs @@ -0,0 +1,424 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// gRPC 配置选项 + /// + public class GrpcOptions + { + /// + /// 服务地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } = true; + + /// + /// 是否忽略 SSL 证书错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 最大接收消息大小(字节) + /// + public int? MaxReceiveMessageSize { get; set; } + + /// + /// 最大发送消息大小(字节) + /// + public int? MaxSendMessageSize { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromSeconds(1); + } + + /// + /// gRPC 工具类 + /// 注意:此类提供 gRPC 调用的抽象接口,实际使用需要引入 Grpc.Net.Client 包 + /// + public static class GrpcUtil + { + /// + /// 创建 gRPC 通道配置 + /// + /// gRPC 配置 + /// 配置对象 + public static GrpcChannelConfiguration CreateChannelConfiguration(GrpcOptions options) + { + return new GrpcChannelConfiguration + { + Address = options.Address, + UseSsl = options.UseSsl, + IgnoreSslErrors = options.IgnoreSslErrors, + MaxReceiveMessageSize = options.MaxReceiveMessageSize, + MaxSendMessageSize = options.MaxSendMessageSize, + Timeout = options.Timeout, + Headers = options.Headers, + EnableCompression = options.EnableCompression + }; + } + + /// + /// 构建 gRPC 服务 URL + /// + /// 主机地址 + /// 端口 + /// 是否使用 SSL + /// 服务 URL + public static string BuildServiceUrl(string host, int port, bool useSsl = true) + { + var scheme = useSsl ? "https" : "http"; + return $"{scheme}://{host}:{port}"; + } + + /// + /// 创建 gRPC 元数据 + /// + /// 请求头 + /// 元数据 + public static GrpcMetadata CreateMetadata(Dictionary headers) + { + return new GrpcMetadata + { + Headers = headers + }; + } + + /// + /// 创建带认证的元数据 + /// + /// Bearer Token + /// 额外请求头 + /// 元数据 + public static GrpcMetadata CreateAuthenticatedMetadata(string token, Dictionary? additionalHeaders = null) + { + var headers = additionalHeaders ?? new Dictionary(); + headers["Authorization"] = $"Bearer {token}"; + return new GrpcMetadata { Headers = headers }; + } + + /// + /// 创建 API Key 认证元数据 + /// + /// API Key + /// 请求头名称 + /// 元数据 + public static GrpcMetadata CreateApiKeyMetadata(string apiKey, string headerName = "x-api-key") + { + return new GrpcMetadata + { + Headers = new Dictionary + { + [headerName] = apiKey + } + }; + } + + /// + /// 执行带重试的 gRPC 调用 + /// + /// 返回类型 + /// gRPC 调用 + /// 重试次数 + /// 重试延迟 + /// 取消令牌 + /// 调用结果 + public static async Task ExecuteWithRetryAsync( + Func> call, + int retryCount = 3, + TimeSpan? retryDelay = null, + CancellationToken cancellationToken = default) + { + var delay = retryDelay ?? TimeSpan.FromSeconds(1); + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await call(); + } + catch (Exception ex) when (IsRetryableError(ex)) + { + lastException = ex; + + if (i < retryCount) + { + await Task.Delay(delay, cancellationToken); + } + } + } + + throw lastException ?? new Exception("gRPC 调用失败"); + } + + /// + /// 执行带超时的 gRPC 调用 + /// + /// 返回类型 + /// gRPC 调用 + /// 超时时间 + /// 取消令牌 + /// 调用结果 + public static async Task ExecuteWithTimeoutAsync( + Func> call, + TimeSpan timeout, + CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + return await call(cts.Token); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + throw new TimeoutException($"gRPC 调用超时: {timeout}"); + } + } + + private static bool IsRetryableError(Exception ex) + { + // 判断是否为可重试的错误 + var message = ex.Message.ToLowerInvariant(); + return message.Contains("unavailable") || + message.Contains("deadline exceeded") || + message.Contains("resource exhausted") || + message.Contains("internal") || + message.Contains("unknown"); + } + } + + /// + /// gRPC 通道配置 + /// + public class GrpcChannelConfiguration + { + /// + /// 服务地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } = true; + + /// + /// 是否忽略 SSL 证书错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 最大接收消息大小 + /// + public int? MaxReceiveMessageSize { get; set; } + + /// + /// 最大发送消息大小 + /// + public int? MaxSendMessageSize { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + } + + /// + /// gRPC 元数据 + /// + public class GrpcMetadata + { + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 添加请求头 + /// + /// 键 + /// 值 + public void Add(string key, string value) + { + Headers[key] = value; + } + + /// + /// 获取请求头 + /// + /// 键 + /// + public string? Get(string key) + { + return Headers.TryGetValue(key, out var value) ? value : null; + } + } + + /// + /// gRPC 响应状态 + /// + public class GrpcResponseStatus + { + /// + /// 状态码 + /// + public GrpcStatusCode StatusCode { get; set; } + + /// + /// 错误详情 + /// + public string? Detail { get; set; } + + /// + /// 是否成功 + /// + public bool IsSuccess => StatusCode == GrpcStatusCode.OK; + + /// + /// 创建成功状态 + /// + public static GrpcResponseStatus Success => new() { StatusCode = GrpcStatusCode.OK }; + + /// + /// 创建错误状态 + /// + public static GrpcResponseStatus Error(GrpcStatusCode code, string detail) => new() + { + StatusCode = code, + Detail = detail + }; + } + + /// + /// gRPC 状态码 + /// + public enum GrpcStatusCode + { + /// + /// 成功 + /// + OK = 0, + + /// + /// 取消 + /// + Cancelled = 1, + + /// + /// 未知错误 + /// + Unknown = 2, + + /// + /// 参数无效 + /// + InvalidArgument = 3, + + /// + /// 超时 + /// + DeadlineExceeded = 4, + + /// + /// 未找到 + /// + NotFound = 5, + + /// + /// 已存在 + /// + AlreadyExists = 6, + + /// + /// 权限不足 + /// + PermissionDenied = 7, + + /// + /// 资源耗尽 + /// + ResourceExhausted = 8, + + /// + /// 前置条件失败 + /// + FailedPrecondition = 9, + + /// + /// 请求中止 + /// + Aborted = 10, + + /// + /// 超出范围 + /// + OutOfRange = 11, + + /// + /// 未实现 + /// + Unimplemented = 12, + + /// + /// 内部错误 + /// + Internal = 13, + + /// + /// 不可用 + /// + Unavailable = 14, + + /// + /// 数据丢失 + /// + DataLoss = 15, + + /// + /// 未认证 + /// + Unauthenticated = 16 + } +} diff --git a/EasyTool.Core/NetCategory/HttpClientBuilder.cs b/EasyTool.Core/NetCategory/HttpClientBuilder.cs new file mode 100644 index 0000000..ee1a6ef --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpClientBuilder.cs @@ -0,0 +1,681 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HttpClient 构建器 + /// 提供流畅的 HttpClient 配置接口 + /// + public class HttpClientBuilder + { + private readonly HttpClientHandler _handler; + private readonly List _handlers; + private TimeSpan _timeout = TimeSpan.FromSeconds(100); + private long _maxResponseContentBufferSize = int.MaxValue; + private Dictionary _defaultHeaders = new(); + private Dictionary _defaultRequestHeaders = new(); + private AuthenticationHeaderValue? _authorizationHeader; + private string? _baseAddress; + private TimeSpan? _pipeliningPolicy; + private bool _allowAutoRedirect = true; + private int _maxAutomaticRedirections = 50; + private DecompressionMethods _automaticDecompression = DecompressionMethods.None; + private ICredentials? _credentials; + private IWebProxy? _proxy; + private bool _useDefaultCredentials; + private TimeSpan? _connectionTimeout; + private int _maxConnectionsPerServer = int.MaxValue; + private int _maxResponseHeadersLength = 64; + + /// + /// 创建 HttpClient 构建器 + /// + public HttpClientBuilder() + { + _handler = new HttpClientHandler(); + _handlers = new List(); + } + + #region 基础配置 + + /// + /// 设置基础地址 + /// + /// 基础 URL + /// HttpClientBuilder + public HttpClientBuilder WithBaseAddress(string baseAddress) + { + _baseAddress = baseAddress; + return this; + } + + /// + /// 设置超时时间 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder WithTimeout(TimeSpan timeout) + { + _timeout = timeout; + return this; + } + + /// + /// 设置最大响应内容缓冲区大小 + /// + /// 大小(字节) + /// HttpClientBuilder + public HttpClientBuilder WithMaxResponseContentBufferSize(long size) + { + _maxResponseContentBufferSize = size; + return this; + } + + #endregion + + #region 请求头 + + /// + /// 添加默认请求头 + /// + /// 头名称 + /// 头值 + /// HttpClientBuilder + public HttpClientBuilder WithDefaultHeader(string name, string value) + { + _defaultHeaders[name] = value; + return this; + } + + /// + /// 批量添加默认请求头 + /// + /// 请求头字典 + /// HttpClientBuilder + public HttpClientBuilder WithDefaultHeaders(Dictionary headers) + { + foreach (var header in headers) + { + _defaultHeaders[header.Key] = header.Value; + } + return this; + } + + /// + /// 设置 Accept 头 + /// + /// 媒体类型 + /// HttpClientBuilder + public HttpClientBuilder WithAccept(string mediaType) + { + _defaultRequestHeaders["Accept"] = mediaType; + return this; + } + + /// + /// 设置 Content-Type 头 + /// + /// 媒体类型 + /// HttpClientBuilder + public HttpClientBuilder WithContentType(string mediaType) + { + _defaultRequestHeaders["Content-Type"] = mediaType; + return this; + } + + /// + /// 设置 User-Agent 头 + /// + /// User-Agent 字符串 + /// HttpClientBuilder + public HttpClientBuilder WithUserAgent(string userAgent) + { + _defaultRequestHeaders["User-Agent"] = userAgent; + return this; + } + + /// + /// 设置 Bearer Token 认证 + /// + /// Token + /// HttpClientBuilder + public HttpClientBuilder WithBearerToken(string token) + { + _authorizationHeader = new AuthenticationHeaderValue("Bearer", token); + return this; + } + + /// + /// 设置 Basic 认证 + /// + /// 用户名 + /// 密码 + /// HttpClientBuilder + public HttpClientBuilder WithBasicAuth(string username, string password) + { + var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")); + _authorizationHeader = new AuthenticationHeaderValue("Basic", credentials); + return this; + } + + /// + /// 设置自定义认证头 + /// + /// 认证方案 + /// 参数 + /// HttpClientBuilder + public HttpClientBuilder WithAuthorization(string scheme, string parameter) + { + _authorizationHeader = new AuthenticationHeaderValue(scheme, parameter); + return this; + } + + #endregion + + #region 代理和安全 + + /// + /// 设置代理 + /// + /// 代理 URL + /// HttpClientBuilder + public HttpClientBuilder WithProxy(string proxyUrl) + { + _proxy = new WebProxy(proxyUrl); + _handler.Proxy = _proxy; + _handler.UseProxy = true; + return this; + } + + /// + /// 设置代理 + /// + /// 代理对象 + /// HttpClientBuilder + public HttpClientBuilder WithProxy(IWebProxy proxy) + { + _proxy = proxy; + _handler.Proxy = proxy; + _handler.UseProxy = true; + return this; + } + + /// + /// 设置代理凭据 + /// + /// 用户名 + /// 密码 + /// HttpClientBuilder + public HttpClientBuilder WithProxyCredentials(string username, string password) + { + if (_proxy != null) + { + _proxy.Credentials = new NetworkCredential(username, password); + } + return this; + } + + /// + /// 忽略 SSL 证书错误 + /// + /// HttpClientBuilder + public HttpClientBuilder IgnoreSslErrors() + { + _handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; + return this; + } + + /// + /// 设置客户端证书 + /// + /// 证书集合 + /// HttpClientBuilder + public HttpClientBuilder WithClientCertificates(System.Security.Cryptography.X509Certificates.X509CertificateCollection certificates) + { + _handler.ClientCertificates.AddRange(certificates); + return this; + } + + #endregion + + #region 重定向和压缩 + + /// + /// 设置是否允许自动重定向 + /// + /// 是否允许 + /// HttpClientBuilder + public HttpClientBuilder WithAutoRedirect(bool allow) + { + _allowAutoRedirect = allow; + _handler.AllowAutoRedirect = allow; + return this; + } + + /// + /// 设置最大自动重定向次数 + /// + /// 次数 + /// HttpClientBuilder + public HttpClientBuilder WithMaxAutomaticRedirections(int count) + { + _maxAutomaticRedirections = count; + _handler.MaxAutomaticRedirections = count; + return this; + } + + /// + /// 启用自动解压缩 + /// + /// 解压缩方法 + /// HttpClientBuilder + public HttpClientBuilder WithAutomaticDecompression(DecompressionMethods methods) + { + _automaticDecompression = methods; + _handler.AutomaticDecompression = methods; + return this; + } + + /// + /// 启用 Gzip 解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithGzipDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.GZip); + } + + /// + /// 启用 Deflate 解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithDeflateDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.Deflate); + } + + /// + /// 启用所有解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithAllDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.GZip | DecompressionMethods.Deflate); + } + + #endregion + + #region 连接配置 + + /// + /// 设置连接超时 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder WithConnectionTimeout(TimeSpan timeout) + { + _connectionTimeout = timeout; + return this; + } + + /// + /// 设置每服务器最大连接数 + /// + /// 连接数 + /// HttpClientBuilder + public HttpClientBuilder WithMaxConnectionsPerServer(int count) + { + _maxConnectionsPerServer = count; + _handler.MaxConnectionsPerServer = count; + return this; + } + + /// + /// 设置最大响应头长度 + /// + /// 长度(KB) + /// HttpClientBuilder + public HttpClientBuilder WithMaxResponseHeadersLength(int length) + { + _maxResponseHeadersLength = length; + _handler.MaxResponseHeadersLength = length; + return this; + } + + /// + /// 使用默认凭据 + /// + /// HttpClientBuilder + public HttpClientBuilder WithDefaultCredentials() + { + _useDefaultCredentials = true; + _handler.UseDefaultCredentials = true; + return this; + } + + /// + /// 设置凭据 + /// + /// 凭据 + /// HttpClientBuilder + public HttpClientBuilder WithCredentials(ICredentials credentials) + { + _credentials = credentials; + _handler.Credentials = credentials; + return this; + } + + #endregion + + #region 中间件 + + /// + /// 添加委托处理器 + /// + /// 处理器 + /// HttpClientBuilder + public HttpClientBuilder AddHandler(DelegatingHandler handler) + { + _handlers.Add(handler); + return this; + } + + /// + /// 添加重试中间件 + /// + /// 重试次数 + /// 重试延迟 + /// HttpClientBuilder + public HttpClientBuilder AddRetry(int retryCount, TimeSpan? retryDelay = null) + { + _handlers.Add(new RetryHandler(retryCount, retryDelay ?? TimeSpan.FromSeconds(1))); + return this; + } + + /// + /// 添加超时中间件 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder AddTimeout(TimeSpan timeout) + { + _handlers.Add(new TimeoutHandler(timeout)); + return this; + } + + /// + /// 添加日志中间件 + /// + /// 日志记录器 + /// HttpClientBuilder + public HttpClientBuilder AddLogging(Action logger) + { + _handlers.Add(new LoggingHandler(logger)); + return this; + } + + #endregion + + #region 构建 + + /// + /// 构建 HttpClient + /// + /// HttpClient 实例 + public HttpClient Build() + { + HttpMessageHandler handler = _handler; + + // 反向添加处理器以形成正确的链 + for (int i = _handlers.Count - 1; i >= 0; i--) + { + _handlers[i].InnerHandler = handler; + handler = _handlers[i]; + } + + var client = new HttpClient(handler); + + // 应用配置 + if (!string.IsNullOrEmpty(_baseAddress)) + { + client.BaseAddress = new Uri(_baseAddress); + } + + client.Timeout = _timeout; + client.MaxResponseContentBufferSize = _maxResponseContentBufferSize; + + // 添加默认头 + foreach (var header in _defaultHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + + foreach (var header in _defaultRequestHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + + // 设置认证头 + if (_authorizationHeader != null) + { + client.DefaultRequestHeaders.Authorization = _authorizationHeader; + } + + return client; + } + + /// + /// 构建并返回一次性使用的 HttpClient(自动释放 Handler) + /// + /// HttpClient 实例 + public HttpClient BuildDisposable() + { + return Build(); + } + + #endregion + } + + #region 中间件处理器 + + /// + /// 重试处理器 + /// + internal class RetryHandler : DelegatingHandler + { + private readonly int _retryCount; + private readonly TimeSpan _retryDelay; + + public RetryHandler(int retryCount, TimeSpan retryDelay) + { + _retryCount = retryCount; + _retryDelay = retryDelay; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + HttpResponseMessage? response = null; + Exception? lastException = null; + + for (int i = 0; i <= _retryCount; i++) + { + try + { + response = await base.SendAsync(request, cancellationToken); + + if (response.IsSuccessStatusCode) + { + return response; + } + + // 服务器错误时重试 + if ((int)response.StatusCode >= 500) + { + lastException = new HttpRequestException($"服务器返回错误: {response.StatusCode}"); + response.Dispose(); + } + else + { + return response; + } + } + catch (Exception ex) + { + lastException = ex; + } + + if (i < _retryCount) + { + await Task.Delay(_retryDelay, cancellationToken); + } + } + + throw lastException ?? new HttpRequestException("重试次数已用尽"); + } + } + + /// + /// 超时处理器 + /// + internal class TimeoutHandler : DelegatingHandler + { + private readonly TimeSpan _timeout; + + public TimeoutHandler(TimeSpan timeout) + { + _timeout = timeout; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(_timeout); + + try + { + return await base.SendAsync(request, cts.Token); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + throw new TimeoutException($"请求超时: {_timeout}"); + } + } + } + + /// + /// 日志处理器 + /// + internal class LoggingHandler : DelegatingHandler + { + private readonly Action _logger; + + public LoggingHandler(Action logger) + { + _logger = logger; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri}"); + + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + + try + { + var response = await base.SendAsync(request, cancellationToken); + stopwatch.Stop(); + + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri} -> {(int)response.StatusCode} ({stopwatch.ElapsedMilliseconds}ms)"); + + return response; + } + catch (Exception ex) + { + stopwatch.Stop(); + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri} -> ERROR: {ex.Message} ({stopwatch.ElapsedMilliseconds}ms)"); + throw; + } + } + } + + #endregion + + /// + /// HttpClient 构建工具类 + /// + public static class HttpClientBuilderUtil + { + /// + /// 创建 HttpClient 构建器 + /// + /// HttpClientBuilder + public static HttpClientBuilder Create() + { + return new HttpClientBuilder(); + } + + /// + /// 创建默认 HttpClient + /// + /// HttpClient + public static HttpClient CreateDefault() + { + return new HttpClientBuilder() + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .Build(); + } + + /// + /// 创建 JSON API HttpClient + /// + /// 基础地址 + /// HttpClient + public static HttpClient CreateForJsonApi(string baseAddress) + { + return new HttpClientBuilder() + .WithBaseAddress(baseAddress) + .WithAccept("application/json") + .WithContentType("application/json") + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .Build(); + } + + /// + /// 创建带重试的 HttpClient + /// + /// 重试次数 + /// HttpClient + public static HttpClient CreateWithRetry(int retryCount = 3) + { + return new HttpClientBuilder() + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .AddRetry(retryCount) + .Build(); + } + + /// + /// 创建忽略 SSL 的 HttpClient + /// + /// HttpClient + public static HttpClient CreateIgnoringSsl() + { + return new HttpClientBuilder() + .IgnoreSslErrors() + .WithAllDecompression() + .Build(); + } + } +} diff --git a/EasyTool.Core/NetCategory/HttpClientPool.cs b/EasyTool.Core/NetCategory/HttpClientPool.cs new file mode 100644 index 0000000..b9fa77b --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpClientPool.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HttpClient 连接池管理器 + /// 正确管理 HttpClient 的生命周期,避免 socket 耗尽问题 + /// + public sealed class HttpClientPool : IDisposable + { + private static readonly Lazy _default = new(() => new HttpClientPool()); + private readonly ConcurrentDictionary _clients = new(); + private readonly ConcurrentDictionary _handlers = new(); + private readonly object _lock = new(); + private bool _disposed; + + /// + /// 默认 HttpClient 池实例 + /// + public static HttpClientPool Default => _default.Value; + + /// + /// 获取或创建 HttpClient + /// + /// 客户端名称 + /// 配置操作 + /// HttpClient 实例 + public HttpClient GetClient(string name = "default", Action? configure = null) + { + ThrowIfDisposed(); + + return _clients.GetOrAdd(name, key => + { + var options = new HttpClientOptions(); + configure?.Invoke(options); + + var handler = CreateHandler(options); + _handlers[key] = handler; + + var client = new HttpClient(handler, disposeHandler: false); + ConfigureClient(client, options); + + return client; + }); + } + + /// + /// 获取或创建 HttpClient(异步) + /// + /// 客户端名称 + /// 配置操作 + /// HttpClient 实例 + public Task GetClientAsync(string name = "default", Action? configure = null) + { + return Task.FromResult(GetClient(name, configure)); + } + + /// + /// 移除并释放指定的 HttpClient + /// + /// 客户端名称 + public void RemoveClient(string name) + { + ThrowIfDisposed(); + + if (_clients.TryRemove(name, out var client)) + { + client.CancelPendingRequests(); + client.Dispose(); + } + + if (_handlers.TryRemove(name, out var handler)) + { + handler.Dispose(); + } + } + + /// + /// 获取所有客户端名称 + /// + /// 客户端名称集合 + public string[] GetClientNames() + { + return _clients.Keys.ToArray(); + } + + /// + /// 获取客户端数量 + /// + public int ClientCount => _clients.Count; + + /// + /// 设置默认请求头 + /// + /// 客户端名称 + /// 请求头 + public void SetDefaultHeaders(string name, params (string name, string value)[] headers) + { + var client = GetClient(name); + foreach (var (headerName, headerValue) in headers) + { + client.DefaultRequestHeaders.Remove(headerName); + client.DefaultRequestHeaders.TryAddWithoutValidation(headerName, headerValue); + } + } + + /// + /// 清除所有客户端 + /// + public void Clear() + { + ThrowIfDisposed(); + + foreach (var client in _clients.Values) + { + client.CancelPendingRequests(); + client.Dispose(); + } + _clients.Clear(); + + foreach (var handler in _handlers.Values) + { + handler.Dispose(); + } + _handlers.Clear(); + } + + /// + /// 为所有客户端设置代理 + /// + /// 代理地址 + public void SetProxyForAll(string proxyAddress) + { + ThrowIfDisposed(); + + foreach (var kvp in _handlers) + { +#if NET5_0_OR_GREATER + if (kvp.Value is SocketsHttpHandler socketsHandler) + { + if (!string.IsNullOrEmpty(proxyAddress)) + { + socketsHandler.Proxy = new WebProxy(proxyAddress); + socketsHandler.UseProxy = true; + } + else + { + socketsHandler.UseProxy = false; + } + } +#else + if (kvp.Value is HttpClientHandler httpHandler) + { + if (!string.IsNullOrEmpty(proxyAddress)) + { + httpHandler.Proxy = new WebProxy(proxyAddress); + httpHandler.UseProxy = true; + } + else + { + httpHandler.UseProxy = false; + } + } +#endif + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (_disposed) + return; + + lock (_lock) + { + if (_disposed) + return; + + Clear(); + _disposed = true; + } + } + + private HttpMessageHandler CreateHandler(HttpClientOptions options) + { +#if NET5_0_OR_GREATER + // 在 .NET 5+ 中使用 SocketsHttpHandler 以支持连接池设置 + var handler = new SocketsHttpHandler + { + AllowAutoRedirect = options.AllowAutoRedirect, + MaxAutomaticRedirections = options.MaxAutomaticRedirections, + AutomaticDecompression = options.AutomaticDecompression, + UseCookies = options.UseCookies, + UseProxy = options.UseProxy, + MaxConnectionsPerServer = options.MaxConnectionsPerServer, + PooledConnectionLifetime = options.PooledConnectionLifetime, + PooledConnectionIdleTimeout = options.PooledConnectionIdleTimeout + }; + + if (options.Proxy != null) + { + handler.Proxy = options.Proxy; + } + + // SocketsHttpHandler 使用不同的证书验证方式 + if (options.ServerCertificateCustomValidationCallback != null) + { +#if NET10_0_OR_GREATER + // .NET 10 中 RemoteCertificateValidationCallback 需要不同的委托签名 + var callback = options.ServerCertificateCustomValidationCallback; + handler.SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => + { + return callback(null, certificate as X509Certificate2, chain, sslPolicyErrors); + } + }; +#else + handler.SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = options.ServerCertificateCustomValidationCallback + }; +#endif + } + + // SocketsHttpHandler 不直接支持 ClientCertificates,需要通过 SslOptions 配置 +#else + // 在 netstandard2.1 中使用 HttpClientHandler(不支持连接池设置) + var handler = new HttpClientHandler + { + AllowAutoRedirect = options.AllowAutoRedirect, + MaxAutomaticRedirections = options.MaxAutomaticRedirections, + AutomaticDecompression = options.AutomaticDecompression, + UseCookies = options.UseCookies, + UseProxy = options.UseProxy, + MaxConnectionsPerServer = options.MaxConnectionsPerServer + }; + + if (options.Proxy != null) + { + handler.Proxy = options.Proxy; + } + + if (options.ServerCertificateCustomValidationCallback != null) + { + handler.ServerCertificateCustomValidationCallback = options.ServerCertificateCustomValidationCallback; + } + + if (options.ClientCertificates?.Count > 0) + { + handler.ClientCertificates.AddRange(options.ClientCertificates); + } +#endif + + return handler; + } + + private void ConfigureClient(HttpClient client, HttpClientOptions options) + { + client.Timeout = options.Timeout; + client.BaseAddress = options.BaseAddress; + + if (options.DefaultRequestHeaders != null) + { + foreach (var header in options.DefaultRequestHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + } + + if (!string.IsNullOrEmpty(options.UserAgent)) + { + client.DefaultRequestHeaders.UserAgent.TryParseAdd(options.UserAgent); + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(HttpClientPool)); + } + } + } + + /// + /// HttpClient 配置选项 + /// + public class HttpClientOptions + { + /// + /// 基础地址 + /// + public Uri? BaseAddress { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(100); + + /// + /// 是否允许自动重定向 + /// + public bool AllowAutoRedirect { get; set; } = true; + + /// + /// 最大自动重定向次数 + /// + public int MaxAutomaticRedirections { get; set; } = 50; + + /// + /// 自动解压缩方式 + /// + public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip | DecompressionMethods.Deflate; + + /// + /// 是否使用 Cookie + /// + public bool UseCookies { get; set; } = false; + + /// + /// 是否使用代理 + /// + public bool UseProxy { get; set; } = false; + + /// + /// 代理设置 + /// + public IWebProxy? Proxy { get; set; } + + /// + /// 每个服务器最大连接数 + /// + public int MaxConnectionsPerServer { get; set; } = int.MaxValue; + + /// + /// 连接池连接生存期 + /// + public TimeSpan PooledConnectionLifetime { get; set; } = TimeSpan.FromMinutes(15); + + /// + /// 连接池空闲超时 + /// + public TimeSpan PooledConnectionIdleTimeout { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 默认请求头 + /// + public Dictionary? DefaultRequestHeaders { get; set; } + + /// + /// User-Agent + /// + public string? UserAgent { get; set; } + + /// + /// 服务器证书验证回调 + /// + public Func? + ServerCertificateCustomValidationCallback { get; set; } + + /// + /// 客户端证书集合 + /// + public X509CertificateCollection? ClientCertificates { get; set; } + } + + /// + /// HttpClient 扩展方法 + /// + public static class HttpClientPoolExtensions + { + /// + /// 创建配置好的 HttpClient + /// + public static HttpClient CreateClient(Action? configure = null) + { + return HttpClientPool.Default.GetClient(Guid.NewGuid().ToString(), configure); + } + + /// + /// 创建用于 JSON API 的 HttpClient + /// + public static HttpClient CreateJsonClient(string? baseUrl = null) + { + return HttpClientPool.Default.GetClient("json_" + (baseUrl ?? "default"), options => + { + if (!string.IsNullOrEmpty(baseUrl)) + { + options.BaseAddress = new Uri(baseUrl); + } + options.DefaultRequestHeaders = new Dictionary + { + ["Accept"] = "application/json", + ["Content-Type"] = "application/json" + }; + }); + } + + /// + /// 创建用于下载大文件的 HttpClient + /// + public static HttpClient CreateDownloadClient() + { + return HttpClientPool.Default.GetClient("download", options => + { + options.Timeout = TimeSpan.FromMinutes(30); + options.AutomaticDecompression = DecompressionMethods.None; + }); + } + + /// + /// 创建允许无效证书的 HttpClient(仅用于开发环境) + /// + public static HttpClient CreateInsecureClient() + { + return HttpClientPool.Default.GetClient("insecure_" + Guid.NewGuid(), options => + { + options.ServerCertificateCustomValidationCallback = (_, _, _, _) => true; + }); + } + + /// + /// 创建带代理的 HttpClient + /// + public static HttpClient CreateProxyClient(string proxyAddress) + { + return HttpClientPool.Default.GetClient("proxy_" + proxyAddress.GetHashCode(), options => + { + options.UseProxy = true; + options.Proxy = new WebProxy(proxyAddress); + }); + } + } +} diff --git a/EasyTool.Core/NetCategory/HttpUtil.cs b/EasyTool.Core/NetCategory/HttpUtil.cs index 498c258..641edbb 100644 --- a/EasyTool.Core/NetCategory/HttpUtil.cs +++ b/EasyTool.Core/NetCategory/HttpUtil.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Threading; @@ -13,664 +10,372 @@ namespace EasyTool.NetCategory { /// - /// HTTP 请求配置 - /// - public class HttpRequestConfig - { - /// - /// 请求超时时间(毫秒) - /// - public int Timeout { get; set; } = 30000; - - /// - /// 请求头 - /// - public Dictionary Headers { get; set; } = new(); - - /// - /// URL 参数 - /// - public Dictionary QueryParams { get; set; } = new(); - - /// - /// 内容类型 - /// - public string ContentType { get; set; } = "application/json"; - - /// - /// 字符编码 - /// - public Encoding Encoding { get; set; } = Encoding.UTF8; - - /// - /// 是否跟随重定向 - /// - public bool AllowRedirect { get; set; } = true; - - /// - /// 重试次数 - /// - public int RetryCount { get; set; } = 0; - - /// - /// 重试间隔(毫秒) - /// - public int RetryInterval { get; set; } = 1000; - - /// - /// Basic 认证 - /// - public (string Username, string Password)? BasicAuth { get; set; } - - /// - /// Bearer Token - /// - public string? BearerToken { get; set; } - } - - /// - /// HTTP 响应结果 - /// - public class HttpResponse - { - /// - /// 是否成功 - /// - public bool IsSuccess { get; set; } - - /// - /// HTTP 状态码 - /// - public HttpStatusCode StatusCode { get; set; } - - /// - /// 响应内容 - /// - public string Content { get; set; } = string.Empty; - - /// - /// 响应头 - /// - public Dictionary Headers { get; set; } = new(); - - /// - /// 错误信息 - /// - public string? ErrorMessage { get; set; } - - /// - /// 异常 - /// - public Exception? Exception { get; set; } - } - - /// - /// HTTP 响应结果(泛型) - /// - public class HttpResponse : HttpResponse - { - /// - /// 反序列化后的数据 - /// - public T? Data { get; set; } - } - - /// - /// HTTP 工具类 - /// 提供便捷的 HTTP 请求方法 + /// HTTP工具类 + /// 提供HTTP请求的便捷操作 /// public static class HttpUtil { - private static readonly Lazy _sharedClient = new(() => CreateDefaultClient()); + private static readonly HttpClient _sharedClient = new(); private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping + PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; /// - /// 获取共享的 HttpClient 实例 + /// 获取共享HttpClient /// - public static HttpClient SharedClient => _sharedClient.Value; + public static HttpClient SharedClient => _sharedClient; - private static HttpClient CreateDefaultClient() + /// + /// 创建HttpClient + /// + /// 基础地址 + /// 超时时间 + /// HttpClient实例 + public static HttpClient CreateClient(string? baseAddress = null, TimeSpan? timeout = null) { - var handler = new HttpClientHandler + var client = new HttpClient(); + + if (!string.IsNullOrEmpty(baseAddress)) { - AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, - AllowAutoRedirect = true, - MaxAutomaticRedirections = 10 - }; + client.BaseAddress = new Uri(baseAddress); + } - var client = new HttpClient(handler) + if (timeout.HasValue) { - Timeout = TimeSpan.FromSeconds(30) - }; + client.Timeout = timeout.Value; + } - client.DefaultRequestHeaders.UserAgent.ParseAdd("EasyTool/1.0"); return client; } - #region GET 请求 + #region GET请求 /// - /// 发送 GET 请求 + /// GET请求 /// - /// 请求地址 - /// 请求配置(可选) - /// 取消令牌 - /// 响应结果 - public static async Task GetAsync( - string url, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task GetStringAsync(string url, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Get, null, config, cancellationToken); +#if NET5_0_OR_GREATER + return await _sharedClient.GetStringAsync(url, cancellationToken); +#else + return await _sharedClient.GetStringAsync(url); +#endif } /// - /// 发送 GET 请求并反序列化响应 + /// GET请求 /// - /// 响应类型 - /// 请求地址 - /// 请求配置(可选) - /// 取消令牌 - /// 响应结果 - public static async Task> GetAsync( - string url, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task GetStringAsync(string url, Dictionary? headers, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Get, null, config, cancellationToken); - } + using var request = new HttpRequestMessage(HttpMethod.Get, url); - /// - /// 发送 GET 请求(同步) - /// - public static HttpResponse Get(string url, HttpRequestConfig? config = null) - { - return GetAsync(url, config).GetAwaiter().GetResult(); - } - - #endregion - - #region POST 请求 + if (headers != null) + { + foreach (var header in headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + } - /// - /// 发送 POST 请求 - /// - /// 请求地址 - /// 请求体 - /// 请求配置(可选) - /// 取消令牌 - /// 响应结果 - public static async Task PostAsync( - string url, - object? body = null, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) - { - return await SendAsync(url, HttpMethod.Post, body, config, cancellationToken); + using var response = await _sharedClient.SendAsync(request, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } /// - /// 发送 POST 请求并反序列化响应 + /// GET请求(返回字节数组) /// - public static async Task> PostAsync( - string url, - object? body = null, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task GetBytesAsync(string url, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Post, body, config, cancellationToken); +#if NET5_0_OR_GREATER + return await _sharedClient.GetByteArrayAsync(url, cancellationToken); +#else + return await _sharedClient.GetByteArrayAsync(url); +#endif } /// - /// 发送 POST 请求(同步) + /// GET请求(返回流) /// - public static HttpResponse Post(string url, object? body = null, HttpRequestConfig? config = null) + public static async Task GetStreamAsync(string url, CancellationToken cancellationToken = default) { - return PostAsync(url, body, config).GetAwaiter().GetResult(); +#if NET5_0_OR_GREATER + return await _sharedClient.GetStreamAsync(url, cancellationToken); +#else + return await _sharedClient.GetStreamAsync(url); +#endif } /// - /// 发送 JSON POST 请求 + /// GET请求(反序列化为对象) /// - public static async Task PostJsonAsync( - string url, - T data, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task GetJsonAsync(string url, CancellationToken cancellationToken = default) { - config ??= new HttpRequestConfig(); - config.ContentType = "application/json"; - return await PostAsync(url, data, config, cancellationToken); + var json = await GetStringAsync(url, cancellationToken); + return JsonSerializer.Deserialize(json, _jsonOptions); } + #endregion + + #region POST请求 + /// - /// 发送表单 POST 请求 + /// POST请求(字符串内容) /// - public static async Task PostFormAsync( - string url, - Dictionary formData, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task PostStringAsync(string url, string content, string? contentType = null, CancellationToken cancellationToken = default) { - config ??= new HttpRequestConfig(); - config.ContentType = "application/x-www-form-urlencoded"; - return await PostAsync(url, formData, config, cancellationToken); + using var httpContent = new StringContent(content, Encoding.UTF8, contentType ?? "text/plain"); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } - #endregion - - #region PUT 请求 - /// - /// 发送 PUT 请求 + /// POST请求(JSON内容) /// - public static async Task PutAsync( - string url, - object? body = null, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task PostJsonAsync(string url, T data, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Put, body, config, cancellationToken); + var json = JsonSerializer.Serialize(data, _jsonOptions); + using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } /// - /// 发送 PUT 请求并反序列化响应 + /// POST请求(JSON内容,返回反序列化对象) /// - public static async Task> PutAsync( - string url, - object? body = null, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task PostJsonAsync(string url, TRequest data, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Put, body, config, cancellationToken); + var json = await PostJsonAsync(url, data, cancellationToken); + return JsonSerializer.Deserialize(json, _jsonOptions); } /// - /// 发送 PUT 请求(同步) + /// POST请求(表单数据) /// - public static HttpResponse Put(string url, object? body = null, HttpRequestConfig? config = null) + public static async Task PostFormAsync(string url, Dictionary formData, CancellationToken cancellationToken = default) { - return PutAsync(url, body, config).GetAwaiter().GetResult(); + using var httpContent = new FormUrlEncodedContent(formData); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } #endregion - #region DELETE 请求 + #region PUT请求 /// - /// 发送 DELETE 请求 + /// PUT请求 /// - public static async Task DeleteAsync( - string url, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task PutStringAsync(string url, string content, string? contentType = null, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Delete, null, config, cancellationToken); + using var httpContent = new StringContent(content, Encoding.UTF8, contentType ?? "text/plain"); + using var response = await _sharedClient.PutAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } /// - /// 发送 DELETE 请求并反序列化响应 + /// PUT请求(JSON内容) /// - public static async Task> DeleteAsync( - string url, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task PutJsonAsync(string url, T data, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Delete, null, config, cancellationToken); + var json = JsonSerializer.Serialize(data, _jsonOptions); + using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); + using var response = await _sharedClient.PutAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } + #endregion + + #region DELETE请求 + /// - /// 发送 DELETE 请求(同步) + /// DELETE请求 /// - public static HttpResponse Delete(string url, HttpRequestConfig? config = null) + public static async Task DeleteAsync(string url, CancellationToken cancellationToken = default) { - return DeleteAsync(url, config).GetAwaiter().GetResult(); + using var response = await _sharedClient.DeleteAsync(url, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } #endregion - #region PATCH 请求 + #region 通用请求 /// - /// 发送 PATCH 请求 + /// 发送请求 /// - public static async Task PatchAsync( - string url, - object? body = null, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) { - return await SendAsync(url, HttpMethod.Patch, body, config, cancellationToken); + return await _sharedClient.SendAsync(request, cancellationToken); } /// - /// 发送 PATCH 请求(同步) + /// 发送请求并返回字符串 /// - public static HttpResponse Patch(string url, object? body = null, HttpRequestConfig? config = null) + public static async Task SendAsStringAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) { - return PatchAsync(url, body, config).GetAwaiter().GetResult(); + using var response = await _sharedClient.SendAsync(request, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } - #endregion - - #region 文件操作 - /// /// 下载文件 /// - /// 文件地址 - /// 保存路径 - /// 请求配置(可选) - /// 取消令牌 - /// 是否成功 - public static async Task DownloadFileAsync( - string url, - string savePath, - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task DownloadFileAsync(string url, string filePath, CancellationToken cancellationToken = default) { - try - { - var directory = Path.GetDirectoryName(savePath); - if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) - { - Directory.CreateDirectory(directory); - } + using var response = await _sharedClient.GetAsync(url, cancellationToken); + response.EnsureSuccessStatusCode(); - using var client = CreateClient(config); - using var response = await client.GetAsync(BuildUrl(url, config), cancellationToken); - response.EnsureSuccessStatusCode(); - - using var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None); - await response.Content.CopyToAsync(fs); - return true; - } - catch + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { - return false; + Directory.CreateDirectory(directory); } + + using var fileStream = File.Create(filePath); +#if NET5_0_OR_GREATER + await response.Content.CopyToAsync(fileStream, cancellationToken); +#else + await response.Content.CopyToAsync(fileStream); +#endif } /// /// 上传文件 /// - /// 上传地址 - /// 文件路径 - /// 表单字段名 - /// 请求配置(可选) - /// 取消令牌 - /// 响应结果 - public static async Task UploadFileAsync( - string url, - string filePath, - string fieldName = "file", - HttpRequestConfig? config = null, - CancellationToken cancellationToken = default) + public static async Task UploadFileAsync(string url, string filePath, string? formFieldName = null, CancellationToken cancellationToken = default) { - try - { - if (!File.Exists(filePath)) - { - return new HttpResponse - { - IsSuccess = false, - ErrorMessage = $"文件不存在: {filePath}" - }; - } - - using var client = CreateClient(config); - using var content = new MultipartFormDataContent(); - - var fileBytes = await File.ReadAllBytesAsync(filePath, cancellationToken); - var fileContent = new ByteArrayContent(fileBytes); - fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(GetMimeType(filePath)); - - content.Add(fileContent, fieldName, Path.GetFileName(filePath)); - - // 添加其他表单字段 - if (config?.QueryParams != null) - { - foreach (var param in config.QueryParams) - { - content.Add(new StringContent(param.Value), param.Key); - } - } - - using var response = await client.PostAsync(BuildUrl(url, config), content, cancellationToken); - var responseContent = await response.Content.ReadAsStringAsync(); - - return new HttpResponse - { - IsSuccess = response.IsSuccessStatusCode, - StatusCode = response.StatusCode, - Content = responseContent - }; - } - catch (Exception ex) - { - return new HttpResponse - { - IsSuccess = false, - ErrorMessage = ex.Message, - Exception = ex - }; - } + using var fileStream = File.OpenRead(filePath); + using var streamContent = new StreamContent(fileStream); + using var formData = new MultipartFormDataContent(); + + var fieldName = formFieldName ?? "file"; + var fileName = Path.GetFileName(filePath); + + formData.Add(streamContent, fieldName, fileName); + + using var response = await _sharedClient.PostAsync(url, formData, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif } #endregion - #region 核心方法 + #region 辅助方法 /// - /// 发送 HTTP 请求 + /// 构建查询字符串 /// - private static async Task SendAsync( - string url, - HttpMethod method, - object? body, - HttpRequestConfig? config, - CancellationToken cancellationToken) + public static string BuildQueryString(Dictionary parameters) { - var result = new HttpResponse(); - int retryCount = config?.RetryCount ?? 0; - int retryInterval = config?.RetryInterval ?? 1000; + if (parameters == null || parameters.Count == 0) + return string.Empty; - for (int attempt = 0; attempt <= retryCount; attempt++) + var sb = new StringBuilder(); + foreach (var kvp in parameters) { - try - { - using var client = CreateClient(config); - using var request = CreateRequest(url, method, body, config); - - using var response = await client.SendAsync(request, cancellationToken); - var content = await response.Content.ReadAsStringAsync(); - - result.IsSuccess = response.IsSuccessStatusCode; - result.StatusCode = response.StatusCode; - result.Content = content; - - foreach (var header in response.Headers) - { - result.Headers[header.Key] = string.Join(",", header.Value); - } - - if (result.IsSuccess || attempt == retryCount) - { - return result; - } - } - catch (Exception ex) + if (kvp.Value != null) { - result.Exception = ex; - result.ErrorMessage = ex.Message; - - if (attempt == retryCount) - { - return result; - } - } - - if (attempt < retryCount) - { - await Task.Delay(retryInterval, cancellationToken); + if (sb.Length > 0) + sb.Append('&'); + sb.Append(Uri.EscapeDataString(kvp.Key)); + sb.Append('='); + sb.Append(Uri.EscapeDataString(kvp.Value)); } } - return result; + return sb.ToString(); } /// - /// 发送 HTTP 请求并反序列化响应 + /// 解析查询字符串 /// - private static async Task> SendAsync( - string url, - HttpMethod method, - object? body, - HttpRequestConfig? config, - CancellationToken cancellationToken) + public static Dictionary ParseQueryString(string query) { - var response = await SendAsync(url, method, body, config, cancellationToken); - var result = new HttpResponse - { - IsSuccess = response.IsSuccess, - StatusCode = response.StatusCode, - Content = response.Content, - Headers = response.Headers, - ErrorMessage = response.ErrorMessage, - Exception = response.Exception - }; - - if (response.IsSuccess && !string.IsNullOrEmpty(response.Content)) - { - try - { - result.Data = JsonSerializer.Deserialize(response.Content, _jsonOptions); - } - catch (Exception ex) - { - result.ErrorMessage = $"反序列化失败: {ex.Message}"; - } - } - - return result; - } - - private static HttpClient CreateClient(HttpRequestConfig? config) - { - if (config == null) - { - return _sharedClient.Value; - } + var result = new Dictionary(); - var handler = new HttpClientHandler - { - AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, - AllowAutoRedirect = config.AllowRedirect - }; - - var client = new HttpClient(handler) - { - Timeout = TimeSpan.FromMilliseconds(config.Timeout) - }; + if (string.IsNullOrEmpty(query)) + return result; - // 添加请求头 - foreach (var header in config.Headers) - { - client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); - } + if (query.StartsWith("?")) + query = query.Substring(1); - // Basic 认证 - if (config.BasicAuth.HasValue) + foreach (var pair in query.Split('&')) { - var authValue = Convert.ToBase64String( - config.Encoding.GetBytes($"{config.BasicAuth.Value.Username}:{config.BasicAuth.Value.Password}")); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authValue); - } - - // Bearer Token - if (!string.IsNullOrEmpty(config.BearerToken)) - { - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", config.BearerToken); - } - - return client; - } - - private static HttpRequestMessage CreateRequest( - string url, - HttpMethod method, - object? body, - HttpRequestConfig? config) - { - var request = new HttpRequestMessage(method, BuildUrl(url, config)); - - if (body != null) - { - string content; - string contentType = config?.ContentType ?? "application/json"; - - if (body is string str) + var index = pair.IndexOf('='); + if (index > 0) { - content = str; + var key = Uri.UnescapeDataString(pair.Substring(0, index)); + var value = Uri.UnescapeDataString(pair.Substring(index + 1)); + result[key] = value; } - else if (body is Dictionary formData && - contentType.Contains("x-www-form-urlencoded")) + else if (pair.Length > 0) { - content = string.Join("&", formData.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}")); + result[Uri.UnescapeDataString(pair)] = string.Empty; } - else - { - content = JsonSerializer.Serialize(body, _jsonOptions); - } - - request.Content = new StringContent(content, config?.Encoding ?? Encoding.UTF8, contentType); } - return request; + return result; } - private static string BuildUrl(string url, HttpRequestConfig? config) + /// + /// 组合URL和查询参数 + /// + public static string CombineUrl(string baseUrl, Dictionary parameters) { - if (config?.QueryParams == null || config.QueryParams.Count == 0) - { - return url; - } - - var queryParts = new List(); - foreach (var param in config.QueryParams) - { - queryParts.Add($"{Uri.EscapeDataString(param.Key)}={Uri.EscapeDataString(param.Value)}"); - } - - var queryString = string.Join("&", queryParts); - return url.Contains('?') ? $"{url}&{queryString}" : $"{url}?{queryString}"; - } + var queryString = BuildQueryString(parameters); + if (string.IsNullOrEmpty(queryString)) + return baseUrl; - private static string GetMimeType(string filePath) - { - var ext = Path.GetExtension(filePath).ToLowerInvariant(); - return ext switch - { - ".jpg" or ".jpeg" => "image/jpeg", - ".png" => "image/png", - ".gif" => "image/gif", - ".bmp" => "image/bmp", - ".pdf" => "application/pdf", - ".doc" => "application/msword", - ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - ".xls" => "application/vnd.ms-excel", - ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - ".txt" => "text/plain", - ".zip" => "application/zip", - _ => "application/octet-stream" - }; + var separator = baseUrl.Contains('?') ? "&" : "?"; + return baseUrl + separator + queryString; } #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs b/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs new file mode 100644 index 0000000..5090574 --- /dev/null +++ b/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; + +namespace EasyTool.NetCategory +{ + /// + /// 网络接口工具类 + /// + public static class NetworkInterfaceUtil + { + /// + /// 获取所有网络接口 + /// + public static List GetAllInterfaces() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + var info = new NetworkInterfaceInfo + { + Id = ni.Id, + Name = ni.Name, + Description = ni.Description, + InterfaceType = ni.NetworkInterfaceType.ToString(), + OperationalStatus = ni.OperationalStatus.ToString(), + Speed = ni.Speed, + IsReceiveOnly = ni.IsReceiveOnly, + SupportsMulticast = ni.SupportsMulticast + }; + + // 获取MAC地址 + var mac = ni.GetPhysicalAddress(); + info.MacAddress = mac.ToString(); + + // 获取IP地址 + var ipProps = ni.GetIPProperties(); + foreach (var addr in ipProps.UnicastAddresses) + { + if (addr.Address.AddressFamily == AddressFamily.InterNetwork) + { + info.IPv4Addresses.Add(new IPAddressInfo + { + Address = addr.Address.ToString(), + SubnetMask = addr.IPv4Mask?.ToString() ?? "" + }); + } + else if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6) + { + info.IPv6Addresses.Add(addr.Address.ToString()); + } + } + + // 获取网关 + foreach (var gateway in ipProps.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + info.Gateway = gateway.Address.ToString(); + break; + } + } + + // 获取DNS服务器 + foreach (var dns in ipProps.DnsAddresses) + { + info.DnsServers.Add(dns.ToString()); + } + + // 获取DHCP服务器 + foreach (var dhcp in ipProps.DhcpServerAddresses) + { + info.DhcpServers.Add(dhcp.ToString()); + } + + // 获取统计信息 + try + { + var stats = ni.GetIPv4Statistics(); + info.BytesReceived = stats.BytesReceived; + info.BytesSent = stats.BytesSent; + info.UnicastPacketsReceived = stats.UnicastPacketsReceived; + info.UnicastPacketsSent = stats.UnicastPacketsSent; + info.NonUnicastPacketsReceived = stats.NonUnicastPacketsReceived; + info.NonUnicastPacketsSent = stats.NonUnicastPacketsSent; + } + catch + { + } + + result.Add(info); + } + + return result; + } + + /// + /// 获取活动网络接口 + /// + public static List GetActiveInterfaces() + { + return GetAllInterfaces() + .Where(i => i.OperationalStatus == "Up") + .ToList(); + } + + /// + /// 获取本机IP地址 + /// + public static string GetLocalIPAddress() + { + using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0); + socket.Connect("8.8.8.8", 65530); + var endPoint = socket.LocalEndPoint as IPEndPoint; + return endPoint?.Address.ToString() ?? ""; + } + + /// + /// 获取本机所有IPv4地址 + /// + public static List GetAllLocalIPv4Addresses() + { + var result = new List(); + var host = Dns.GetHostEntry(Dns.GetHostName()); + + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork) + { + result.Add(ip.ToString()); + } + } + + return result; + } + + /// + /// 获取本机MAC地址 + /// + public static string GetMacAddress() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up) + .OrderByDescending(ni => ni.Speed); + + foreach (var ni in interfaces) + { + var mac = ni.GetPhysicalAddress(); + if (!string.IsNullOrEmpty(mac.ToString())) + { + return mac.ToString(); + } + } + + return string.Empty; + } + + /// + /// 获取默认网关 + /// + public static string GetDefaultGateway() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + var ipProps = ni.GetIPProperties(); + foreach (var gateway in ipProps.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + return gateway.Address.ToString(); + } + } + } + + return string.Empty; + } + + /// + /// 获取DNS服务器 + /// + public static List GetDnsServers() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + var ipProps = ni.GetIPProperties(); + foreach (var dns in ipProps.DnsAddresses) + { + if (!result.Contains(dns.ToString())) + { + result.Add(dns.ToString()); + } + } + } + + return result; + } + + /// + /// 检查是否联网 + /// + public static bool IsNetworkAvailable() + { + return NetworkInterface.GetIsNetworkAvailable(); + } + + /// + /// 获取网络流量统计 + /// + public static NetworkTrafficStats GetNetworkTrafficStats() + { + var stats = new NetworkTrafficStats(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + try + { + var ipv4Stats = ni.GetIPv4Statistics(); + stats.TotalBytesReceived += ipv4Stats.BytesReceived; + stats.TotalBytesSent += ipv4Stats.BytesSent; + stats.TotalPacketsReceived += ipv4Stats.UnicastPacketsReceived + ipv4Stats.NonUnicastPacketsReceived; + stats.TotalPacketsSent += ipv4Stats.UnicastPacketsSent + ipv4Stats.NonUnicastPacketsSent; + } + catch + { + } + } + + return stats; + } + + /// + /// 刷新DNS缓存 + /// + public static bool FlushDnsCache() + { + try + { + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "ipconfig", + Arguments = "/flushdns", + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch + { + return false; + } + } + + /// + /// 释放并续订DHCP + /// + public static bool RenewDhcp() + { + try + { + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "ipconfig", + Arguments = "/renew", + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch + { + return false; + } + } + + /// + /// 获取主机名 + /// + public static string GetHostName() + { + return Dns.GetHostName(); + } + + /// + /// 根据主机名获取IP地址 + /// + public static string[] GetHostAddresses(string hostName) + { + try + { + var addresses = Dns.GetHostAddresses(hostName); + return addresses.Select(a => a.ToString()).ToArray(); + } + catch + { + return Array.Empty(); + } + } + } + + /// + /// 网络接口信息 + /// + public class NetworkInterfaceInfo + { + public string Id { get; set; } = ""; + public string Name { get; set; } = ""; + public string Description { get; set; } = ""; + public string InterfaceType { get; set; } = ""; + public string OperationalStatus { get; set; } = ""; + public string MacAddress { get; set; } = ""; + public long Speed { get; set; } + public bool IsReceiveOnly { get; set; } + public bool SupportsMulticast { get; set; } + public List IPv4Addresses { get; set; } = new(); + public List IPv6Addresses { get; set; } = new(); + public string Gateway { get; set; } = ""; + public List DnsServers { get; set; } = new(); + public List DhcpServers { get; set; } = new(); + public long BytesReceived { get; set; } + public long BytesSent { get; set; } + public long UnicastPacketsReceived { get; set; } + public long UnicastPacketsSent { get; set; } + public long NonUnicastPacketsReceived { get; set; } + public long NonUnicastPacketsSent { get; set; } + + public double SpeedMbps => Speed / 1_000_000.0; + public double SpeedGbps => Speed / 1_000_000_000.0; + } + + /// + /// IP地址信息 + /// + public class IPAddressInfo + { + public string Address { get; set; } = ""; + public string SubnetMask { get; set; } = ""; + } + + /// + /// 网络流量统计 + /// + public class NetworkTrafficStats + { + public long TotalBytesReceived { get; set; } + public long TotalBytesSent { get; set; } + public long TotalPacketsReceived { get; set; } + public long TotalPacketsSent { get; set; } + + public double TotalGBReceived => TotalBytesReceived / (1024.0 * 1024 * 1024); + public double TotalGBSent => TotalBytesSent / (1024.0 * 1024 * 1024); + } +} diff --git a/EasyTool.Core/NetCategory/PingUtil.cs b/EasyTool.Core/NetCategory/PingUtil.cs new file mode 100644 index 0000000..e31406f --- /dev/null +++ b/EasyTool.Core/NetCategory/PingUtil.cs @@ -0,0 +1,523 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Ping结果 + /// + public class PingResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 目标地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 解析后的IP地址 + /// + public IPAddress? IpAddress { get; set; } + + /// + /// 响应时间(毫秒) + /// + public long RoundtripTime { get; set; } + + /// + /// TTL(生存时间) + /// + public int Ttl { get; set; } + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } + + /// + /// IP状态 + /// + public IPStatus Status { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 时间戳 + /// + public DateTime Timestamp { get; set; } = DateTime.UtcNow; + + public override string ToString() + { + return Success + ? $"回复来自 {IpAddress}: 字节={BufferSize} 时间={RoundtripTime}ms TTL={Ttl}" + : $"请求超时: {Status}"; + } + } + + /// + /// Ping统计信息 + /// + public class PingStatistics + { + /// + /// 目标地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 总发包数 + /// + public int PacketsSent { get; set; } + + /// + /// 收包数 + /// + public int PacketsReceived { get; set; } + + /// + /// 丢包数 + /// + public int PacketsLost => PacketsSent - PacketsReceived; + + /// + /// 丢包率 + /// + public double LossRate => PacketsSent > 0 ? (double)PacketsLost / PacketsSent : 0; + + /// + /// 最小延迟(毫秒) + /// + public long MinRoundtripTime { get; set; } + + /// + /// 最大延迟(毫秒) + /// + public long MaxRoundtripTime { get; set; } + + /// + /// 平均延迟(毫秒) + /// + public double AverageRoundtripTime { get; set; } + + /// + /// 结果列表 + /// + public List Results { get; set; } = new(); + + public override string ToString() + { + return $"Ping {Address}: 已发送={PacketsSent}, 已接收={PacketsReceived}, 丢失={PacketsLost}({LossRate:P0}丢失), " + + $"延迟: 最小={MinRoundtripTime}ms, 最大={MaxRoundtripTime}ms, 平均={AverageRoundtripTime:F2}ms"; + } + } + + /// + /// Ping配置 + /// + public class PingOptions + { + /// + /// 超时时间(毫秒) + /// + public int Timeout { get; set; } = 5000; + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } = 32; + + /// + /// TTL + /// + public int Ttl { get; set; } = 128; + + /// + /// 是否允许分片 + /// + public bool DontFragment { get; set; } = true; + + /// + /// 发送次数 + /// + public int Count { get; set; } = 4; + + /// + /// 发送间隔(毫秒) + /// + public int Interval { get; set; } = 1000; + } + + /// + /// Ping工具类 + /// + public static class PingUtil + { + /// + /// Ping指定主机 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// Ping结果 + public static PingResult Ping(string hostNameOrAddress, int timeout = 5000) + { + return PingAsync(hostNameOrAddress, timeout).GetAwaiter().GetResult(); + } + + /// + /// 异步Ping指定主机 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 取消令牌 + /// Ping结果 + public static async Task PingAsync(string hostNameOrAddress, int timeout = 5000, CancellationToken cancellationToken = default) + { + var result = new PingResult + { + Address = hostNameOrAddress, + Timestamp = DateTime.UtcNow + }; + + try + { + // 解析IP地址 + IPAddress ipAddress; + if (IPAddress.TryParse(hostNameOrAddress, out var parsedIp)) + { + ipAddress = parsedIp; + } + else + { +#if NET5_0_OR_GREATER + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); +#else + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress); +#endif + if (addresses.Length == 0) + { + result.Status = IPStatus.Unknown; + result.ErrorMessage = "无法解析主机名"; + return result; + } + ipAddress = addresses[0]; + } + + result.IpAddress = ipAddress; + + using var ping = new System.Net.NetworkInformation.Ping(); + var buffer = new byte[32]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)('a' + (i % 26)); + } + + var options = new System.Net.NetworkInformation.PingOptions(128, true); + var reply = await ping.SendPingAsync(ipAddress, timeout, buffer, options); + + result.Status = reply.Status; + result.Success = reply.Status == IPStatus.Success; + + if (reply.Status == IPStatus.Success) + { + result.RoundtripTime = reply.RoundtripTime; + result.Ttl = reply.Options?.Ttl ?? 0; + result.BufferSize = reply.Buffer.Length; + } + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + result.Status = IPStatus.Unknown; + } + + return result; + } + + /// + /// Ping指定主机(带完整配置) + /// + /// 主机名或IP地址 + /// 配置 + /// 取消令牌 + /// Ping结果 + public static async Task PingAsync(string hostNameOrAddress, PingOptions options, CancellationToken cancellationToken = default) + { + var result = new PingResult + { + Address = hostNameOrAddress, + Timestamp = DateTime.UtcNow + }; + + try + { + IPAddress ipAddress; + if (IPAddress.TryParse(hostNameOrAddress, out var parsedIp)) + { + ipAddress = parsedIp; + } + else + { +#if NET5_0_OR_GREATER + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); +#else + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress); +#endif + if (addresses.Length == 0) + { + result.Status = IPStatus.Unknown; + result.ErrorMessage = "无法解析主机名"; + return result; + } + ipAddress = addresses[0]; + } + + result.IpAddress = ipAddress; + + using var ping = new System.Net.NetworkInformation.Ping(); + var buffer = new byte[options.BufferSize]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)('a' + (i % 26)); + } + + var pingOptions = new System.Net.NetworkInformation.PingOptions(options.Ttl, options.DontFragment); + var reply = await ping.SendPingAsync(ipAddress, options.Timeout, buffer, pingOptions); + + result.Status = reply.Status; + result.Success = reply.Status == IPStatus.Success; + + if (reply.Status == IPStatus.Success) + { + result.RoundtripTime = reply.RoundtripTime; + result.Ttl = reply.Options?.Ttl ?? 0; + result.BufferSize = reply.Buffer.Length; + } + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + result.Status = IPStatus.Unknown; + } + + return result; + } + + /// + /// 持续Ping指定主机 + /// + /// 主机名或IP地址 + /// 配置 + /// 取消令牌 + /// Ping统计信息 + public static async Task PingContinuousAsync(string hostNameOrAddress, PingOptions? options = null, CancellationToken cancellationToken = default) + { + options ??= new PingOptions(); + var stats = new PingStatistics { Address = hostNameOrAddress }; + + long totalTime = 0; + long minTime = long.MaxValue; + long maxTime = long.MinValue; + + for (int i = 0; i < options.Count; i++) + { + if (cancellationToken.IsCancellationRequested) + break; + + var result = await PingAsync(hostNameOrAddress, options, cancellationToken); + stats.Results.Add(result); + stats.PacketsSent++; + + if (result.Success) + { + stats.PacketsReceived++; + totalTime += result.RoundtripTime; + + if (result.RoundtripTime < minTime) + minTime = result.RoundtripTime; + + if (result.RoundtripTime > maxTime) + maxTime = result.RoundtripTime; + } + + if (i < options.Count - 1) + { + await Task.Delay(options.Interval, cancellationToken); + } + } + + if (stats.PacketsReceived > 0) + { + stats.MinRoundtripTime = minTime; + stats.MaxRoundtripTime = maxTime; + stats.AverageRoundtripTime = (double)totalTime / stats.PacketsReceived; + } + + return stats; + } + + /// + /// 批量Ping多个主机 + /// + /// 主机列表 + /// 超时时间(毫秒) + /// 主机与结果的字典 + public static async Task> PingMultipleAsync(IEnumerable hosts, int timeout = 5000) + { + var tasks = hosts.Select(h => PingAsync(h, timeout)); + var results = await Task.WhenAll(tasks); + + return hosts.Zip(results, (h, r) => new { Host = h, Result = r }) + .ToDictionary(x => x.Host, x => x.Result); + } + + /// + /// 检测主机是否可达 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 是否可达 + public static async Task IsReachableAsync(string hostNameOrAddress, int timeout = 5000) + { + var result = await PingAsync(hostNameOrAddress, timeout); + return result.Success; + } + + /// + /// 检测主机是否可达 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 是否可达 + public static bool IsReachable(string hostNameOrAddress, int timeout = 5000) + { + return IsReachableAsync(hostNameOrAddress, timeout).GetAwaiter().GetResult(); + } + + /// + /// 检测TCP端口是否开放 + /// + /// 主机名或IP地址 + /// 端口号 + /// 超时时间(毫秒) + /// 端口是否开放 + public static async Task IsPortOpenAsync(string hostNameOrAddress, int port, int timeout = 5000) + { + try + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(hostNameOrAddress, port); + var timeoutTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(connectTask, timeoutTask); + + if (completedTask == connectTask && client.Connected) + { + return true; + } + + return false; + } + catch + { + return false; + } + } + + /// + /// 检测TCP端口是否开放 + /// + /// 主机名或IP地址 + /// 端口号 + /// 超时时间(毫秒) + /// 端口是否开放 + public static bool IsPortOpen(string hostNameOrAddress, int port, int timeout = 5000) + { + return IsPortOpenAsync(hostNameOrAddress, port, timeout).GetAwaiter().GetResult(); + } + + /// + /// 测试网络连接速度 + /// + /// 主机名或IP地址 + /// 测试次数 + /// 平均延迟(毫秒) + public static async Task TestLatencyAsync(string hostNameOrAddress, int count = 5) + { + var options = new PingOptions { Count = count }; + var stats = await PingContinuousAsync(hostNameOrAddress, options); + return stats.AverageRoundtripTime; + } + + /// + /// 获取本机IP地址 + /// + /// IP地址列表 + public static List GetLocalIPAddresses() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + if (ni.OperationalStatus != OperationalStatus.Up) + continue; + + if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) + continue; + + var ipProperties = ni.GetIPProperties(); + foreach (var ip in ipProperties.UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork || + ip.Address.AddressFamily == AddressFamily.InterNetworkV6) + { + result.Add(ip.Address); + } + } + } + + return result; + } + + /// + /// 获取默认网关 + /// + /// 默认网关地址 + public static IPAddress? GetDefaultGateway() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + if (ni.OperationalStatus != OperationalStatus.Up) + continue; + + var ipProperties = ni.GetIPProperties(); + foreach (var gateway in ipProperties.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + return gateway.Address; + } + } + } + + return null; + } + } +} diff --git a/EasyTool.Core/NetCategory/PortScannerUtil.cs b/EasyTool.Core/NetCategory/PortScannerUtil.cs new file mode 100644 index 0000000..c696d88 --- /dev/null +++ b/EasyTool.Core/NetCategory/PortScannerUtil.cs @@ -0,0 +1,256 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 端口扫描工具类 + /// + public static class PortScannerUtil + { + /// + /// 检查端口是否开放 + /// + public static async Task IsPortOpenAsync(string host, int port, int timeoutMs = 1000) + { + try + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(host, port); + var timeoutTask = Task.Delay(timeoutMs); + + var completedTask = await Task.WhenAny(connectTask, timeoutTask); + return completedTask == connectTask && client.Connected; + } + catch + { + return false; + } + } + + /// + /// 检查端口是否开放 + /// + public static bool IsPortOpen(string host, int port, int timeoutMs = 1000) + { + return IsPortOpenAsync(host, port, timeoutMs).GetAwaiter().GetResult(); + } + + /// + /// 扫描单个端口 + /// + public static PortScanResult ScanPort(string host, int port, int timeoutMs = 1000) + { + var startTime = DateTime.Now; + var isOpen = IsPortOpen(host, port, timeoutMs); + var duration = DateTime.Now - startTime; + + return new PortScanResult + { + Host = host, + Port = port, + IsOpen = isOpen, + ResponseTime = duration, + ServiceName = GetServiceName(port) + }; + } + + /// + /// 扫描多个端口 + /// + public static List ScanPorts(string host, IEnumerable ports, int timeoutMs = 1000) + { + var results = new List(); + foreach (var port in ports) + { + results.Add(ScanPort(host, port, timeoutMs)); + } + return results; + } + + /// + /// 异步扫描多个端口 + /// + public static async Task> ScanPortsAsync(string host, IEnumerable ports, int timeoutMs = 1000, int maxConcurrent = 100) + { + var results = new List(); + var semaphore = new SemaphoreSlim(maxConcurrent); + var tasks = new List>(); + + foreach (var port in ports) + { + tasks.Add(ScanPortAsync(host, port, timeoutMs, semaphore)); + } + + var completedResults = await Task.WhenAll(tasks); + results.AddRange(completedResults); + return results; + } + + private static async Task ScanPortAsync(string host, int port, int timeoutMs, SemaphoreSlim semaphore) + { + await semaphore.WaitAsync(); + try + { + var startTime = DateTime.Now; + var isOpen = await IsPortOpenAsync(host, port, timeoutMs); + var duration = DateTime.Now - startTime; + + return new PortScanResult + { + Host = host, + Port = port, + IsOpen = isOpen, + ResponseTime = duration, + ServiceName = GetServiceName(port) + }; + } + finally + { + semaphore.Release(); + } + } + + /// + /// 扫描端口范围 + /// + public static List ScanPortRange(string host, int startPort, int endPort, int timeoutMs = 1000) + { + var ports = new List(); + for (int i = startPort; i <= endPort; i++) + { + ports.Add(i); + } + return ScanPorts(host, ports, timeoutMs); + } + + /// + /// 异步扫描端口范围 + /// + public static Task> ScanPortRangeAsync(string host, int startPort, int endPort, int timeoutMs = 1000, int maxConcurrent = 100) + { + var ports = new List(); + for (int i = startPort; i <= endPort; i++) + { + ports.Add(i); + } + return ScanPortsAsync(host, ports, timeoutMs, maxConcurrent); + } + + /// + /// 扫描常用端口 + /// + public static List ScanCommonPorts(string host, int timeoutMs = 1000) + { + return ScanPorts(host, CommonPorts.Keys, timeoutMs); + } + + /// + /// 异步扫描常用端口 + /// + public static Task> ScanCommonPortsAsync(string host, int timeoutMs = 1000, int maxConcurrent = 100) + { + return ScanPortsAsync(host, CommonPorts.Keys, timeoutMs, maxConcurrent); + } + + /// + /// 获取服务名称 + /// + public static string GetServiceName(int port) + { + return CommonPorts.TryGetValue(port, out var name) ? name : "unknown"; + } + + /// + /// 常用端口映射 + /// + public static readonly Dictionary CommonPorts = new() + { + { 20, "FTP Data" }, + { 21, "FTP" }, + { 22, "SSH" }, + { 23, "Telnet" }, + { 25, "SMTP" }, + { 53, "DNS" }, + { 80, "HTTP" }, + { 110, "POP3" }, + { 119, "NNTP" }, + { 123, "NTP" }, + { 135, "RPC" }, + { 137, "NetBIOS Name" }, + { 138, "NetBIOS Datagram" }, + { 139, "NetBIOS Session" }, + { 143, "IMAP" }, + { 161, "SNMP" }, + { 162, "SNMP Trap" }, + { 389, "LDAP" }, + { 443, "HTTPS" }, + { 445, "SMB" }, + { 465, "SMTPS" }, + { 514, "Syslog" }, + { 587, "SMTP(TLS)" }, + { 636, "LDAPS" }, + { 993, "IMAPS" }, + { 995, "POP3S" }, + { 1080, "SOCKS" }, + { 1433, "MSSQL" }, + { 1434, "MSSQL Monitor" }, + { 1521, "Oracle" }, + { 1723, "PPTP" }, + { 2049, "NFS" }, + { 3306, "MySQL" }, + { 3389, "RDP" }, + { 5432, "PostgreSQL" }, + { 5900, "VNC" }, + { 5901, "VNC-1" }, + { 5902, "VNC-2" }, + { 6379, "Redis" }, + { 8080, "HTTP-Alt" }, + { 8443, "HTTPS-Alt" }, + { 9000, "PHP-FPM" }, + { 9200, "Elasticsearch" }, + { 9300, "Elasticsearch Transport" }, + { 11211, "Memcached" }, + { 27017, "MongoDB" } + }; + } + + /// + /// 端口扫描结果 + /// + public class PortScanResult + { + /// + /// 主机 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 端口 + /// + public int Port { get; set; } + + /// + /// 是否开放 + /// + public bool IsOpen { get; set; } + + /// + /// 响应时间 + /// + public TimeSpan ResponseTime { get; set; } + + /// + /// 服务名称 + /// + public string ServiceName { get; set; } = string.Empty; + + public override string ToString() + { + return $"{Host}:{Port} - {(IsOpen ? "Open" : "Closed")} ({ServiceName})"; + } + } +} diff --git a/EasyTool.Core/NetCategory/ProxyUtil.cs b/EasyTool.Core/NetCategory/ProxyUtil.cs new file mode 100644 index 0000000..795aaa4 --- /dev/null +++ b/EasyTool.Core/NetCategory/ProxyUtil.cs @@ -0,0 +1,619 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 代理类型 + /// + public enum ProxyType + { + /// + /// HTTP代理 + /// + Http, + + /// + /// HTTPS代理 + /// + Https, + + /// + /// SOCKS4代理 + /// + Socks4, + + /// + /// SOCKS4a代理 + /// + Socks4a, + + /// + /// SOCKS5代理 + /// + Socks5 + } + + /// + /// 代理信息 + /// + public class ProxyInfo + { + /// + /// 代理地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 代理端口 + /// + public int Port { get; set; } + + /// + /// 用户名 + /// + public string? Username { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 代理类型 + /// + public ProxyType Type { get; set; } = ProxyType.Http; + + /// + /// 是否需要认证 + /// + public bool RequiresAuthentication => !string.IsNullOrEmpty(Username); + + /// + /// 代理地址(格式:host:port) + /// + public string Address => $"{Host}:{Port}"; + + /// + /// 代理URL + /// + public string ProxyUrl + { + get + { + var scheme = Type switch + { + ProxyType.Http => "http", + ProxyType.Https => "https", + ProxyType.Socks4 => "socks4", + ProxyType.Socks4a => "socks4a", + ProxyType.Socks5 => "socks5", + _ => "http" + }; + + if (RequiresAuthentication) + { + return $"{scheme}://{Username}:{Password}@{Host}:{Port}"; + } + return $"{scheme}://{Host}:{Port}"; + } + } + + public override string ToString() + { + return $"{Type}://{Address}"; + } + } + + /// + /// 代理工具类 + /// + public static class ProxyUtil + { + /// + /// 解析代理字符串 + /// 支持格式: + /// - host:port + /// - http://host:port + /// - http://user:pass@host:port + /// - socks5://host:port + /// + /// 代理字符串 + /// 代理信息 + public static ProxyInfo Parse(string proxyString) + { + if (string.IsNullOrWhiteSpace(proxyString)) + throw new ArgumentException("代理字符串不能为空", nameof(proxyString)); + + var info = new ProxyInfo(); + + // 解析协议 + if (proxyString.Contains("://")) + { + var parts = proxyString.Split(new[] { "://" }, 2, StringSplitOptions.None); + var scheme = parts[0].ToLower(); + info.Type = scheme switch + { + "http" => ProxyType.Http, + "https" => ProxyType.Https, + "socks4" => ProxyType.Socks4, + "socks4a" => ProxyType.Socks4a, + "socks5" => ProxyType.Socks5, + _ => ProxyType.Http + }; + proxyString = parts[1]; + } + + // 解析认证信息 + if (proxyString.Contains("@")) + { + var authParts = proxyString.Split('@'); + var credentials = authParts[0].Split(':'); + if (credentials.Length == 2) + { + info.Username = credentials[0]; + info.Password = credentials[1]; + } + proxyString = authParts[1]; + } + + // 解析主机和端口 + var hostPort = proxyString.Split(':'); + if (hostPort.Length >= 2) + { + info.Host = hostPort[0]; + if (int.TryParse(hostPort[1], out var port)) + { + info.Port = port; + } + } + else + { + info.Host = proxyString; + } + + return info; + } + + /// + /// 创建WebProxy + /// + /// 代理信息 + /// WebProxy + public static WebProxy CreateWebProxy(ProxyInfo proxyInfo) + { + var proxy = new WebProxy(proxyInfo.Host, proxyInfo.Port); + + if (proxyInfo.RequiresAuthentication) + { + proxy.Credentials = new NetworkCredential(proxyInfo.Username, proxyInfo.Password); + } + + return proxy; + } + + /// + /// 创建WebProxy + /// + /// 主机 + /// 端口 + /// 用户名 + /// 密码 + /// WebProxy + public static WebProxy CreateWebProxy(string host, int port, string? username = null, string? password = null) + { + return CreateWebProxy(new ProxyInfo + { + Host = host, + Port = port, + Username = username, + Password = password + }); + } + + /// + /// 创建HttpClientHandler(带代理) + /// + /// 代理信息 + /// HttpClientHandler + public static HttpClientHandler CreateHttpClientHandler(ProxyInfo proxyInfo) + { + var handler = new HttpClientHandler + { + Proxy = CreateWebProxy(proxyInfo), + UseProxy = true + }; + + if (proxyInfo.RequiresAuthentication) + { + handler.PreAuthenticate = true; + } + + return handler; + } + + /// + /// 创建HttpClient(带代理) + /// + /// 代理信息 + /// HttpClient + public static HttpClient CreateHttpClient(ProxyInfo proxyInfo) + { + var handler = CreateHttpClientHandler(proxyInfo); + return new HttpClient(handler); + } + + /// + /// 创建HttpClient(带代理) + /// + /// 代理字符串 + /// HttpClient + public static HttpClient CreateHttpClient(string proxyString) + { + var proxyInfo = Parse(proxyString); + return CreateHttpClient(proxyInfo); + } + + /// + /// 测试代理连接 + /// + /// 代理信息 + /// 测试URL + /// 超时时间 + /// 是否可用 + public static async Task TestProxyAsync(ProxyInfo proxyInfo, string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + try + { + using var client = CreateHttpClient(proxyInfo); + client.Timeout = timeout ?? TimeSpan.FromSeconds(30); + + var response = await client.GetAsync(testUrl); + return response.IsSuccessStatusCode; + } + catch + { + return false; + } + } + + /// + /// 测试代理连接 + /// + /// 代理字符串 + /// 测试URL + /// 超时时间 + /// 是否可用 + public static async Task TestProxyAsync(string proxyString, string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + var proxyInfo = Parse(proxyString); + return await TestProxyAsync(proxyInfo, testUrl, timeout); + } + + /// + /// 获取代理响应时间 + /// + /// 代理信息 + /// 测试URL + /// 响应时间(毫秒),失败返回-1 + public static async Task GetResponseTimeAsync(ProxyInfo proxyInfo, string testUrl = "http://www.google.com") + { + try + { + using var client = CreateHttpClient(proxyInfo); + client.Timeout = TimeSpan.FromSeconds(30); + + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + await client.GetAsync(testUrl); + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds; + } + catch + { + return -1; + } + } + + /// + /// 获取系统代理设置 + /// + /// 代理信息(无代理返回null) + public static ProxyInfo? GetSystemProxy() + { + var proxy = WebRequest.GetSystemWebProxy(); + + if (proxy == null) + return null; + + var defaultProxy = proxy.GetProxy(new Uri("http://example.com")); + if (defaultProxy == null || defaultProxy.Host == "example.com") + return null; + + return new ProxyInfo + { + Host = defaultProxy.Host, + Port = defaultProxy.Port, + Type = ProxyType.Http + }; + } + + /// + /// 是否使用系统代理 + /// + /// 是否使用系统代理 + public static bool IsSystemProxyEnabled() + { + return GetSystemProxy() != null; + } + } + + /// + /// 代理池 + /// + public class ProxyPool + { + private readonly List _proxies = new(); + private readonly Dictionary _stats = new(); + private int _currentIndex = 0; + private readonly object _lock = new(); + + /// + /// 代理数量 + /// + public int Count => _proxies.Count; + + /// + /// 添加代理 + /// + /// 代理信息 + public void Add(ProxyInfo proxyInfo) + { + lock (_lock) + { + _proxies.Add(proxyInfo); + _stats[proxyInfo.Address] = new ProxyStats { ProxyAddress = proxyInfo.Address }; + } + } + + /// + /// 添加代理 + /// + /// 代理字符串 + public void Add(string proxyString) + { + Add(ProxyUtil.Parse(proxyString)); + } + + /// + /// 批量添加代理 + /// + /// 代理字符串列表 + public void AddRange(IEnumerable proxyStrings) + { + foreach (var proxyString in proxyStrings) + { + Add(proxyString); + } + } + + /// + /// 移除代理 + /// + /// 代理信息 + public void Remove(ProxyInfo proxyInfo) + { + lock (_lock) + { + _proxies.RemoveAll(p => p.Address == proxyInfo.Address); + _stats.Remove(proxyInfo.Address); + } + } + + /// + /// 清空代理池 + /// + public void Clear() + { + lock (_lock) + { + _proxies.Clear(); + _stats.Clear(); + _currentIndex = 0; + } + } + + /// + /// 获取下一个代理(轮询) + /// + /// 代理信息 + public ProxyInfo? GetNext() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + var proxy = _proxies[_currentIndex]; + _currentIndex = (_currentIndex + 1) % _proxies.Count; + return proxy; + } + } + + /// + /// 获取随机代理 + /// + /// 代理信息 + public ProxyInfo? GetRandom() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + var random = new Random(); + return _proxies[random.Next(_proxies.Count)]; + } + } + + /// + /// 获取最快的代理 + /// + /// 代理信息 + public ProxyInfo? GetFastest() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + return _proxies + .Where(p => _stats.ContainsKey(p.Address)) + .OrderBy(p => _stats[p.Address].AverageResponseTime) + .FirstOrDefault() ?? GetRandom(); + } + } + + /// + /// 报告代理使用结果 + /// + /// 代理地址 + /// 是否成功 + /// 响应时间 + public void ReportResult(string proxyAddress, bool success, long responseTime = 0) + { + lock (_lock) + { + if (_stats.TryGetValue(proxyAddress, out var stats)) + { + stats.TotalRequests++; + if (success) + { + stats.SuccessCount++; + if (responseTime > 0) + { + stats.TotalResponseTime += responseTime; + stats.AverageResponseTime = stats.TotalResponseTime / stats.SuccessCount; + } + } + else + { + stats.FailureCount++; + } + } + } + } + + /// + /// 获取代理统计信息 + /// + /// 代理地址 + /// 统计信息 + public ProxyStats? GetStats(string proxyAddress) + { + lock (_lock) + { + return _stats.TryGetValue(proxyAddress, out var stats) ? stats : null; + } + } + + /// + /// 移除失败率高的代理 + /// + /// 最大失败率(0-1) + /// 移除的代理数量 + public int RemoveHighFailureProxies(double maxFailureRate = 0.5) + { + lock (_lock) + { + var toRemove = _stats + .Where(s => s.Value.TotalRequests >= 5 && s.Value.FailureRate > maxFailureRate) + .Select(s => s.Key) + .ToList(); + + foreach (var address in toRemove) + { + _proxies.RemoveAll(p => p.Address == address); + _stats.Remove(address); + } + + return toRemove.Count; + } + } + + /// + /// 测试所有代理 + /// + /// 测试URL + /// 超时时间 + /// 可用代理数量 + public async Task TestAllAsync(string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + var tasks = _proxies.Select(async proxy => + { + var responseTime = await ProxyUtil.GetResponseTimeAsync(proxy, testUrl); + var success = responseTime >= 0; + ReportResult(proxy.Address, success, responseTime); + return success; + }); + + var results = await Task.WhenAll(tasks); + return results.Count(r => r); + } + } + + /// + /// 代理统计信息 + /// + public class ProxyStats + { + /// + /// 代理地址 + /// + public string ProxyAddress { get; set; } = string.Empty; + + /// + /// 总请求数 + /// + public int TotalRequests { get; set; } + + /// + /// 成功次数 + /// + public int SuccessCount { get; set; } + + /// + /// 失败次数 + /// + public int FailureCount { get; set; } + + /// + /// 总响应时间 + /// + public long TotalResponseTime { get; set; } + + /// + /// 平均响应时间 + /// + public long AverageResponseTime { get; set; } + + /// + /// 成功率 + /// + public double SuccessRate => TotalRequests > 0 ? (double)SuccessCount / TotalRequests : 0; + + /// + /// 失败率 + /// + public double FailureRate => TotalRequests > 0 ? (double)FailureCount / TotalRequests : 0; + + public override string ToString() + { + return $"代理: {ProxyAddress}, 总请求: {TotalRequests}, 成功率: {SuccessRate:P2}, 平均响应: {AverageResponseTime}ms"; + } + } +} diff --git a/EasyTool.Core/NetCategory/SmtpUtil.cs b/EasyTool.Core/NetCategory/SmtpUtil.cs new file mode 100644 index 0000000..9adea89 --- /dev/null +++ b/EasyTool.Core/NetCategory/SmtpUtil.cs @@ -0,0 +1,389 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// SMTP 邮件发送工具类 + /// + public static class SmtpUtil + { + /// + /// 发送邮件 + /// + /// SMTP 配置 + /// 邮件消息 + public static void Send(SmtpOptions options, EmailMessage message) + { + using var smtpClient = CreateSmtpClient(options); + using var mailMessage = CreateMailMessage(message); + smtpClient.Send(mailMessage); + } + + /// + /// 异步发送邮件 + /// + public static async Task SendAsync(SmtpOptions options, EmailMessage message) + { + using var smtpClient = CreateSmtpClient(options); + using var mailMessage = CreateMailMessage(message); + await smtpClient.SendMailAsync(mailMessage); + } + + /// + /// 批量发送邮件 + /// + public static void SendBatch(SmtpOptions options, IEnumerable messages) + { + using var smtpClient = CreateSmtpClient(options); + + foreach (var message in messages) + { + using var mailMessage = CreateMailMessage(message); + smtpClient.Send(mailMessage); + } + } + + /// + /// 发送简单邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + /// 是否为 HTML + public static void SendSimple(SmtpOptions options, string to, string subject, string body, bool isHtml = false) + { + var message = new EmailMessage + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml + }; + + Send(options, message); + } + + /// + /// 发送带附件的邮件 + /// + public static void SendWithAttachment(SmtpOptions options, string to, string subject, string body, string attachmentPath, bool isHtml = false) + { + var message = new EmailMessage + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml, + Attachments = new List + { + new EmailAttachment { FilePath = attachmentPath } + } + }; + + Send(options, message); + } + + /// + /// 发送模板邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板内容 + /// 模板参数 + /// 是否为 HTML + public static void SendTemplate(SmtpOptions options, string to, string subject, string template, Dictionary parameters, bool isHtml = false) + { + var body = template; + foreach (var kvp in parameters) + { + body = body.Replace($"{{{kvp.Key}}}", kvp.Value?.ToString() ?? string.Empty); + } + + SendSimple(options, to, subject, body, isHtml); + } + + /// + /// 测试 SMTP 连接 + /// + public static bool TestConnection(SmtpOptions options) + { + try + { + using var smtpClient = CreateSmtpClient(options); + smtpClient.Send(new MailMessage(options.Username, options.Username, "Test", "Test connection")); + return true; + } + catch + { + return false; + } + } + + private static SmtpClient CreateSmtpClient(SmtpOptions options) + { + var client = new SmtpClient(options.Host, options.Port) + { + EnableSsl = options.EnableSsl, + Timeout = options.Timeout * 1000 + }; + + if (!string.IsNullOrEmpty(options.Username)) + { + client.Credentials = new NetworkCredential(options.Username, options.Password); + } + + return client; + } + + private static MailMessage CreateMailMessage(EmailMessage message) + { + var mailMessage = new MailMessage + { + Subject = message.Subject, + Body = message.Body, + IsBodyHtml = message.IsBodyHtml, + SubjectEncoding = Encoding.UTF8, + BodyEncoding = Encoding.UTF8 + }; + + // 发件人 + if (!string.IsNullOrEmpty(message.From)) + { + mailMessage.From = new MailAddress(message.From, message.FromName, Encoding.UTF8); + } + + // 收件人 + foreach (var to in message.To ?? Enumerable.Empty()) + { + mailMessage.To.Add(new MailAddress(to)); + } + + // 抄送 + foreach (var cc in message.Cc ?? Enumerable.Empty()) + { + mailMessage.CC.Add(new MailAddress(cc)); + } + + // 密送 + foreach (var bcc in message.Bcc ?? Enumerable.Empty()) + { + mailMessage.Bcc.Add(new MailAddress(bcc)); + } + + // 回复地址 + if (!string.IsNullOrEmpty(message.ReplyTo)) + { + mailMessage.ReplyToList.Add(new MailAddress(message.ReplyTo)); + } + + // 附件 + foreach (var attachment in message.Attachments ?? Enumerable.Empty()) + { + Attachment mailAttachment; + + if (!string.IsNullOrEmpty(attachment.FilePath)) + { + mailAttachment = new Attachment(attachment.FilePath, GetMimeType(attachment.FilePath)); + } + else if (attachment.Content != null && !string.IsNullOrEmpty(attachment.FileName)) + { + var stream = new MemoryStream(attachment.Content); + mailAttachment = new Attachment(stream, attachment.FileName, attachment.ContentType ?? GetMimeType(attachment.FileName)); + } + else + { + continue; + } + + mailAttachment.ContentDisposition!.DispositionType = DispositionTypeNames.Attachment; + if (!string.IsNullOrEmpty(attachment.ContentId)) + { + mailAttachment.ContentId = attachment.ContentId; + } + + mailMessage.Attachments.Add(mailAttachment); + } + + // 优先级 + mailMessage.Priority = message.Priority switch + { + EmailPriority.High => MailPriority.High, + EmailPriority.Low => MailPriority.Low, + _ => MailPriority.Normal + }; + + return mailMessage; + } + + private static string GetMimeType(string fileName) + { + var extension = Path.GetExtension(fileName).ToLowerInvariant(); + return extension switch + { + ".txt" => "text/plain", + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".ppt" => "application/vnd.ms-powerpoint", + ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".jpg" or ".jpeg" => "image/jpeg", + ".png" => "image/png", + ".gif" => "image/gif", + ".zip" => "application/zip", + ".rar" => "application/x-rar-compressed", + ".7z" => "application/x-7z-compressed", + _ => "application/octet-stream" + }; + } + } + + /// + /// SMTP 配置选项 + /// + public class SmtpOptions + { + /// + /// SMTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 端口号 + /// + public int Port { get; set; } = 25; + + /// + /// 是否启用 SSL + /// + public bool EnableSsl { get; set; } = true; + + /// + /// 用户名 + /// + public string? Username { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 超时时间(秒) + /// + public int Timeout { get; set; } = 30; + } + + /// + /// 邮件消息 + /// + public class EmailMessage + { + /// + /// 发件人地址 + /// + public string? From { get; set; } + + /// + /// 发件人名称 + /// + public string? FromName { get; set; } + + /// + /// 收件人列表 + /// + public List? To { get; set; } + + /// + /// 抄送列表 + /// + public List? Cc { get; set; } + + /// + /// 密送列表 + /// + public List? Bcc { get; set; } + + /// + /// 回复地址 + /// + public string? ReplyTo { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 正文 + /// + public string Body { get; set; } = string.Empty; + + /// + /// 是否为 HTML 格式 + /// + public bool IsBodyHtml { get; set; } + + /// + /// 附件列表 + /// + public List? Attachments { get; set; } + + /// + /// 优先级 + /// + public EmailPriority Priority { get; set; } = EmailPriority.Normal; + } + + /// + /// 邮件附件 + /// + public class EmailAttachment + { + /// + /// 文件路径 + /// + public string? FilePath { get; set; } + + /// + /// 文件名 + /// + public string? FileName { get; set; } + + /// + /// 文件内容(字节) + /// + public byte[]? Content { get; set; } + + /// + /// 内容类型(MIME 类型) + /// + public string? ContentType { get; set; } + + /// + /// 内容 ID(用于嵌入图片) + /// + public string? ContentId { get; set; } + } + + /// + /// 邮件优先级 + /// + public enum EmailPriority + { + Low, + Normal, + High + } +} diff --git a/EasyTool.Core/NetCategory/SseUtil.cs b/EasyTool.Core/NetCategory/SseUtil.cs new file mode 100644 index 0000000..253397e --- /dev/null +++ b/EasyTool.Core/NetCategory/SseUtil.cs @@ -0,0 +1,521 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Server-Sent Events (SSE) 客户端 + /// 用于接收服务器推送的事件流 + /// + public class SseClient : IDisposable + { + private readonly HttpClient _httpClient; + private readonly Uri _endpoint; + private readonly Dictionary _headers; + private CancellationTokenSource? _cts; + private bool _disposed; + private bool _isConnected; + + /// + /// 接收到事件时触发 + /// + public event EventHandler? EventReceived; + + /// + /// 连接打开时触发 + /// + public event EventHandler? Connected; + + /// + /// 连接关闭时触发 + /// + public event EventHandler? Disconnected; + + /// + /// 发生错误时触发 + /// + public event EventHandler? Error; + + /// + /// 是否已连接 + /// + public bool IsConnected => _isConnected; + + /// + /// 最后接收的事件 ID + /// + public string? LastEventId { get; private set; } + + /// + /// 重连等待时间(毫秒) + /// + public int ReconnectDelay { get; set; } = 3000; + + /// + /// 最大重连次数 + /// + public int MaxReconnectAttempts { get; set; } = 5; + + /// + /// 是否自动重连 + /// + public bool AutoReconnect { get; set; } = true; + + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + public SseClient(string endpoint, Dictionary? headers = null) + : this(new Uri(endpoint), headers) + { + } + + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + public SseClient(Uri endpoint, Dictionary? headers = null) + : this(new HttpClient(), endpoint, headers) + { + } + + /// + /// 创建 SSE 客户端 + /// + /// HttpClient 实例 + /// SSE 端点 URL + /// 请求头 + public SseClient(HttpClient httpClient, Uri endpoint, Dictionary? headers = null) + { + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); + _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); + _headers = headers ?? new Dictionary(); + } + + /// + /// 连接并开始接收事件 + /// + /// 取消令牌 + public async Task ConnectAsync(CancellationToken cancellationToken = default) + { + ThrowIfDisposed(); + + _cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + + try + { + await ConnectInternalAsync(_cts.Token); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + // 正常取消,不触发错误 + } + catch (Exception ex) + { + OnError(ex); + } + } + + /// + /// 断开连接 + /// + public async Task DisconnectAsync() + { + if (_cts != null && !_cts.IsCancellationRequested) + { + _cts.Cancel(); + } + + _isConnected = false; + OnDisconnected(null); + await Task.CompletedTask; + } + + /// + /// 异步获取所有事件(直到连接关闭) + /// + /// 取消令牌 + /// 事件集合 + public async IAsyncEnumerable GetEventsAsync( + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var events = new System.Collections.Concurrent.BlockingCollection(); + var completed = new TaskCompletionSource(); + + EventReceived += (_, e) => events.Add(e); + Disconnected += (_, _) => completed.TrySetResult(true); + Error += (_, _) => completed.TrySetResult(true); + + _ = ConnectAsync(cancellationToken); + + while (!cancellationToken.IsCancellationRequested) + { + if (events.TryTake(out var sseEvent, 100, cancellationToken)) + { + yield return sseEvent; + } + + if (completed.Task.IsCompleted) + { + while (events.TryTake(out var remainingEvent)) + { + yield return remainingEvent; + } + break; + } + } + } + + private async Task ConnectInternalAsync(CancellationToken cancellationToken) + { + var reconnectAttempts = 0; + + while (!cancellationToken.IsCancellationRequested && (reconnectAttempts < MaxReconnectAttempts || !AutoReconnect)) + { + try + { + var request = new HttpRequestMessage(HttpMethod.Get, _endpoint); + request.Headers.Accept.ParseAdd("text/event-stream"); + request.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue { NoCache = true }; + + foreach (var header in _headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + // 添加 Last-Event-ID 头 + if (!string.IsNullOrEmpty(LastEventId)) + { + request.Headers.TryAddWithoutValidation("Last-Event-ID", LastEventId); + } + + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + response.EnsureSuccessStatusCode(); + + _isConnected = true; + reconnectAttempts = 0; + OnConnected(); + +#if NETSTANDARD2_1 + using var stream = await response.Content.ReadAsStreamAsync(); +#else + using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); +#endif + using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true); + + await ProcessEventStreamAsync(reader, cancellationToken); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + break; + } + catch (Exception ex) + { + OnError(ex); + } + + _isConnected = false; + + if (AutoReconnect && reconnectAttempts < MaxReconnectAttempts && !cancellationToken.IsCancellationRequested) + { + reconnectAttempts++; + OnDisconnected(reconnectAttempts); + + try + { + await Task.Delay(ReconnectDelay * reconnectAttempts, cancellationToken); + } + catch (OperationCanceledException) + { + break; + } + } + else + { + break; + } + } + } + + private async Task ProcessEventStreamAsync(StreamReader reader, CancellationToken cancellationToken) + { + var currentEvent = new SseEventBuilder(); + + while (!cancellationToken.IsCancellationRequested) + { + var line = await reader.ReadLineAsync(); + if (line == null) + { + // 流结束 + if (currentEvent.HasData) + { + OnEventReceived(currentEvent.Build()); + } + break; + } + + if (string.IsNullOrEmpty(line)) + { + // 空行表示事件结束 + if (currentEvent.HasData) + { + var sseEvent = currentEvent.Build(); + LastEventId = sseEvent.Id; + OnEventReceived(sseEvent); + currentEvent = new SseEventBuilder(); + } + continue; + } + + if (line.StartsWith(':')) + { + // 注释行,忽略 + continue; + } + + var colonIndex = line.IndexOf(':'); + string field, value; + + if (colonIndex < 0) + { + field = line; + value = string.Empty; + } + else + { + field = line[..colonIndex]; + value = line[(colonIndex + 1)..]; + if (value.StartsWith(' ')) + { + value = value[1..]; + } + } + + switch (field) + { + case "event": + currentEvent.EventType = value; + break; + case "data": + currentEvent.AppendData(value); + break; + case "id": + currentEvent.Id = value; + break; + case "retry": + if (int.TryParse(value, out var retryMs)) + { + ReconnectDelay = retryMs; + } + break; + } + } + } + + protected virtual void OnEventReceived(SseEvent sseEvent) + { + EventReceived?.Invoke(this, sseEvent); + } + + protected virtual void OnConnected() + { + Connected?.Invoke(this, EventArgs.Empty); + } + + protected virtual void OnDisconnected(int? reconnectAttempt) + { + Disconnected?.Invoke(this, new SseDisconnectEventArgs(reconnectAttempt)); + } + + protected virtual void OnError(Exception ex) + { + Error?.Invoke(this, ex); + } + + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(SseClient)); + } + } + + public void Dispose() + { + if (_disposed) + return; + + _cts?.Cancel(); + _cts?.Dispose(); + _disposed = true; + } + } + + /// + /// SSE 事件 + /// + public class SseEvent + { + /// + /// 事件 ID + /// + public string? Id { get; set; } + + /// + /// 事件类型 + /// + public string? EventType { get; set; } + + /// + /// 事件数据 + /// + public string Data { get; set; } = string.Empty; + + /// + /// 接收时间 + /// + public DateTimeOffset ReceivedAt { get; set; } = DateTimeOffset.UtcNow; + + /// + /// 尝试解析数据为 JSON + /// + public T? ParseJson() + { + try + { + return System.Text.Json.JsonSerializer.Deserialize(Data); + } + catch + { + return default; + } + } + + public override string ToString() + { + var sb = new StringBuilder(); + if (!string.IsNullOrEmpty(Id)) + sb.Append($"[{Id}] "); + if (!string.IsNullOrEmpty(EventType)) + sb.Append($"({EventType}) "); + sb.Append(Data); + return sb.ToString(); + } + } + + /// + /// SSE 断开连接事件参数 + /// + public class SseDisconnectEventArgs : EventArgs + { + /// + /// 重连尝试次数(如果正在重连) + /// + public int? ReconnectAttempt { get; } + + public SseDisconnectEventArgs(int? reconnectAttempt) + { + ReconnectAttempt = reconnectAttempt; + } + } + + /// + /// SSE 事件构建器 + /// + internal class SseEventBuilder + { + public string? Id { get; set; } + public string? EventType { get; set; } + private readonly StringBuilder _data = new(); + + public bool HasData => _data.Length > 0; + + public void AppendData(string data) + { + if (_data.Length > 0) + { + _data.AppendLine(); + } + _data.Append(data); + } + + public SseEvent Build() + { + return new SseEvent + { + Id = Id, + EventType = EventType, + Data = _data.ToString() + }; + } + } + + /// + /// SSE 客户端扩展方法 + /// + public static class SseClientExtensions + { + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + /// SSE 客户端实例 + public static SseClient CreateSseClient(string endpoint, Dictionary? headers = null) + { + return new SseClient(endpoint, headers); + } + + /// + /// 创建 SSE 客户端(带认证) + /// + /// SSE 端点 URL + /// Bearer Token + /// SSE 客户端实例 + public static SseClient CreateSseClientWithAuth(string endpoint, string bearerToken) + { + return new SseClient(endpoint, new Dictionary + { + ["Authorization"] = $"Bearer {bearerToken}" + }); + } + + /// + /// 异步获取单个 SSE 事件 + /// + /// SSE 端点 URL + /// 超时时间 + /// 取消令牌 + /// SSE 事件 + public static async Task GetSingleEventAsync(string endpoint, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + using var client = new SseClient(endpoint); + SseEvent? result = null; + var tcs = new TaskCompletionSource(); + + client.EventReceived += (_, e) => + { + result = e; + tcs.TrySetResult(e); + }; + + _ = client.ConnectAsync(cts.Token); + + var completedTask = await Task.WhenAny(tcs.Task, Task.Delay(timeout, cts.Token)); + + await client.DisconnectAsync(); + + return completedTask == tcs.Task ? result : null; + } + } +} diff --git a/EasyTool.Core/NetCategory/WebSocketUtil.cs b/EasyTool.Core/NetCategory/WebSocketUtil.cs new file mode 100644 index 0000000..d22cde3 --- /dev/null +++ b/EasyTool.Core/NetCategory/WebSocketUtil.cs @@ -0,0 +1,411 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// WebSocket客户端配置 + /// + public class WebSocketClientOptions + { + /// + /// 连接超时时间 + /// + public TimeSpan ConnectTimeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 接收缓冲区大小 + /// + public int ReceiveBufferSize { get; set; } = 8192; + + /// + /// 发送缓冲区大小 + /// + public int SendBufferSize { get; set; } = 8192; + + /// + /// 是否保持连接 + /// + public bool KeepAlive { get; set; } = true; + + /// + /// 保持连接间隔 + /// + public TimeSpan KeepAliveInterval { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 子协议 + /// + public List? SubProtocols { get; set; } + } + + /// + /// WebSocket消息 + /// + public class WebSocketMessage + { + /// + /// 消息类型 + /// + public WebSocketMessageType MessageType { get; set; } + + /// + /// 文本内容 + /// + public string? Text { get; set; } + + /// + /// 二进制内容 + /// + public byte[]? Binary { get; set; } + + /// + /// 是否为结束消息 + /// + public bool EndOfMessage { get; set; } = true; + } + + /// + /// WebSocket客户端 + /// + public class WebSocketClient : IDisposable + { + private readonly ClientWebSocket _webSocket; + private readonly WebSocketClientOptions _options; + private readonly CancellationTokenSource _cts; + private readonly ConcurrentQueue _sendQueue = new(); + private Task? _receiveTask; + private Task? _sendTask; + private bool _disposed; + + /// + /// 是否已连接 + /// + public bool IsConnected => _webSocket.State == WebSocketState.Open; + + /// + /// 当前状态 + /// + public WebSocketState State => _webSocket.State; + + /// + /// 接收到消息时触发 + /// + public event Action? OnMessage; + + /// + /// 连接关闭时触发 + /// + public event Action? OnClosed; + + /// + /// 发生错误时触发 + /// + public event Action? OnError; + + /// + /// 创建WebSocket客户端 + /// + /// 配置 + public WebSocketClient(WebSocketClientOptions? options = null) + { + _options = options ?? new WebSocketClientOptions(); + _webSocket = new ClientWebSocket(); + _cts = new CancellationTokenSource(); + + // 设置子协议 + if (_options.SubProtocols != null) + { + foreach (var protocol in _options.SubProtocols) + { + _webSocket.Options.AddSubProtocol(protocol); + } + } + + // 设置请求头 + foreach (var header in _options.Headers) + { + _webSocket.Options.SetRequestHeader(header.Key, header.Value); + } + + _webSocket.Options.KeepAliveInterval = _options.KeepAlive ? _options.KeepAliveInterval : TimeSpan.Zero; + } + + /// + /// 连接到服务器 + /// + /// 服务器地址 + /// 取消令牌 + public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cts.Token); + cts.CancelAfter(_options.ConnectTimeout); + + await _webSocket.ConnectAsync(uri, cts.Token); + + // 启动接收和发送任务 + _receiveTask = ReceiveLoopAsync(_cts.Token); + _sendTask = SendLoopAsync(_cts.Token); + } + + /// + /// 连接到服务器 + /// + /// 服务器地址 + /// 取消令牌 + public async Task ConnectAsync(string url, CancellationToken cancellationToken = default) + { + await ConnectAsync(new Uri(url), cancellationToken); + } + + /// + /// 发送文本消息 + /// + /// 消息内容 + public void Send(string message) + { + _sendQueue.Enqueue(new WebSocketMessage + { + MessageType = WebSocketMessageType.Text, + Text = message + }); + } + + /// + /// 发送二进制消息 + /// + /// 二进制数据 + public void Send(byte[] data) + { + _sendQueue.Enqueue(new WebSocketMessage + { + MessageType = WebSocketMessageType.Binary, + Binary = data + }); + } + + /// + /// 异步发送文本消息 + /// + /// 消息内容 + /// 取消令牌 + public async Task SendAsync(string message, CancellationToken cancellationToken = default) + { + var bytes = Encoding.UTF8.GetBytes(message); + await _webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, cancellationToken); + } + + /// + /// 异步发送二进制消息 + /// + /// 二进制数据 + /// 取消令牌 + public async Task SendAsync(byte[] data, CancellationToken cancellationToken = default) + { + await _webSocket.SendAsync(new ArraySegment(data), WebSocketMessageType.Binary, true, cancellationToken); + } + + /// + /// 关闭连接 + /// + /// 关闭状态 + /// 关闭原因 + /// 取消令牌 + public async Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.NormalClosure, string reason = "", CancellationToken cancellationToken = default) + { + if (_webSocket.State == WebSocketState.Open) + { + await _webSocket.CloseAsync(closeStatus, reason, cancellationToken); + } + _cts.Cancel(); + } + + private async Task ReceiveLoopAsync(CancellationToken cancellationToken) + { + var buffer = new byte[_options.ReceiveBufferSize]; + + try + { + while (!cancellationToken.IsCancellationRequested && _webSocket.State == WebSocketState.Open) + { + var result = await _webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken); + + if (result.MessageType == WebSocketMessageType.Close) + { + await CloseAsync(WebSocketCloseStatus.NormalClosure, "Server closed", cancellationToken); + OnClosed?.Invoke(result.CloseStatus, result.CloseStatusDescription); + break; + } + + var message = new WebSocketMessage + { + MessageType = result.MessageType, + EndOfMessage = result.EndOfMessage + }; + + if (result.MessageType == WebSocketMessageType.Text) + { + message.Text = Encoding.UTF8.GetString(buffer, 0, result.Count); + } + else + { + message.Binary = new byte[result.Count]; + Array.Copy(buffer, message.Binary, result.Count); + } + + OnMessage?.Invoke(message); + } + } + catch (OperationCanceledException) + { + // 正常取消 + } + catch (Exception ex) + { + OnError?.Invoke(ex); + } + } + + private async Task SendLoopAsync(CancellationToken cancellationToken) + { + try + { + while (!cancellationToken.IsCancellationRequested && _webSocket.State == WebSocketState.Open) + { + if (_sendQueue.TryDequeue(out var message)) + { + if (message.MessageType == WebSocketMessageType.Text && message.Text != null) + { + var bytes = Encoding.UTF8.GetBytes(message.Text); + await _webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, message.EndOfMessage, cancellationToken); + } + else if (message.MessageType == WebSocketMessageType.Binary && message.Binary != null) + { + await _webSocket.SendAsync(new ArraySegment(message.Binary), WebSocketMessageType.Binary, message.EndOfMessage, cancellationToken); + } + } + else + { + await Task.Delay(10, cancellationToken); + } + } + } + catch (OperationCanceledException) + { + // 正常取消 + } + catch (Exception ex) + { + OnError?.Invoke(ex); + } + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _cts.Cancel(); + _webSocket.Dispose(); + _cts.Dispose(); + } + } + } + + /// + /// WebSocket工具类 + /// + public static class WebSocketUtil + { + /// + /// 创建WebSocket客户端 + /// + /// 配置 + /// 客户端实例 + public static WebSocketClient CreateClient(WebSocketClientOptions? options = null) + { + return new WebSocketClient(options); + } + + /// + /// 连接并发送消息(一次性通信) + /// + /// 服务器地址 + /// 消息内容 + /// 超时时间 + /// 响应消息列表 + public static async Task> SendAndReceiveAsync(string url, string message, TimeSpan? timeout = null) + { + var responses = new List(); + var tcs = new TaskCompletionSource(); + + using var client = new WebSocketClient(); + client.OnMessage += msg => + { + responses.Add(msg); + if (msg.EndOfMessage) + { + tcs.TrySetResult(true); + } + }; + + await client.ConnectAsync(url); + + using var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(30)); + cts.Token.Register(() => tcs.TrySetCanceled()); + + client.Send(message); + + await Task.WhenAny(tcs.Task, Task.Delay(timeout ?? TimeSpan.FromSeconds(30))); + await client.CloseAsync(); + + return responses; + } + + /// + /// 检查WebSocket URL是否有效 + /// + /// URL字符串 + /// 是否有效 + public static bool IsValidWebSocketUrl(string url) + { + if (string.IsNullOrWhiteSpace(url)) + return false; + + if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) + return false; + + return uri.Scheme == "ws" || uri.Scheme == "wss"; + } + + /// + /// 获取WebSocket状态描述 + /// + /// 状态 + /// 状态描述 + public static string GetStateDescription(WebSocketState state) + { + return state switch + { + WebSocketState.None => "无连接", + WebSocketState.Connecting => "连接中", + WebSocketState.Open => "已连接", + WebSocketState.CloseSent => "已发送关闭请求", + WebSocketState.CloseReceived => "已接收关闭请求", + WebSocketState.Closed => "已关闭", + WebSocketState.Aborted => "已中止", + _ => "未知状态" + }; + } + } +} diff --git a/EasyTool.Core/NetCategory/WebhookUtil.cs b/EasyTool.Core/NetCategory/WebhookUtil.cs new file mode 100644 index 0000000..7ededcd --- /dev/null +++ b/EasyTool.Core/NetCategory/WebhookUtil.cs @@ -0,0 +1,511 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Webhook配置 + /// + public class WebhookOptions + { + /// + /// Webhook URL + /// + public string Url { get; set; } = string.Empty; + + /// + /// HTTP方法(默认POST) + /// + public HttpMethod Method { get; set; } = HttpMethod.Post; + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 重试次数 + /// + public int MaxRetries { get; set; } = 3; + + /// + /// 重试延迟(毫秒) + /// + public int RetryDelayMs { get; set; } = 1000; + + /// + /// 是否验证SSL + /// + public bool ValidateSsl { get; set; } = true; + + /// + /// 成功响应回调 + /// + public Action? OnSuccess { get; set; } + + /// + /// 失败响应回调 + /// + public Action? OnFailure { get; set; } + } + + /// + /// Webhook响应 + /// + public class WebhookResponse + { + /// + /// 是否成功 + /// + public bool IsSuccess { get; set; } + + /// + /// HTTP状态码 + /// + public int StatusCode { get; set; } + + /// + /// 响应内容 + /// + public string? Content { get; set; } + + /// + /// 响应头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 请求耗时 + /// + public TimeSpan Duration { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + } + + /// + /// Webhook发送结果 + /// + public class WebhookResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 响应 + /// + public WebhookResponse? Response { get; set; } + + /// + /// 异常 + /// + public Exception? Exception { get; set; } + } + + /// + /// Webhook工具类 + /// + public static class WebhookUtil + { + private static readonly HttpClient _httpClient; + + static WebhookUtil() + { + _httpClient = new HttpClient(); + } + + /// + /// 发送Webhook + /// + /// 配置 + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(WebhookOptions options, object payload, CancellationToken cancellationToken = default) + { + var json = JsonSerializer.Serialize(payload); + return await SendAsync(options, json, "application/json", cancellationToken); + } + + /// + /// 发送Webhook + /// + /// 配置 + /// 内容 + /// 内容类型 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(WebhookOptions options, string content, string contentType = "application/json", CancellationToken cancellationToken = default) + { + var result = new WebhookResult(); + Exception? lastException = null; + WebhookResponse? lastResponse = null; + + for (int retry = 0; retry <= options.MaxRetries; retry++) + { + try + { + var response = await SendRequestAsync(options, content, contentType, cancellationToken); + response.RetryCount = retry; + lastResponse = response; + + if (response.IsSuccess) + { + result.Success = true; + result.Response = response; + options.OnSuccess?.Invoke(response); + return result; + } + } + catch (Exception ex) + { + lastException = ex; + } + + // 延迟重试 + if (retry < options.MaxRetries) + { + await Task.Delay(options.RetryDelayMs * (retry + 1), cancellationToken); + } + } + + result.Success = false; + result.Response = lastResponse; + result.Exception = lastException; + + if (lastException != null) + { + options.OnFailure?.Invoke(lastResponse ?? new WebhookResponse(), lastException); + } + + return result; + } + + private static async Task SendRequestAsync(WebhookOptions options, string content, string contentType, CancellationToken cancellationToken) + { + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + var response = new WebhookResponse(); + + try + { + using var request = new HttpRequestMessage(options.Method, options.Url); + request.Content = new StringContent(content, Encoding.UTF8, contentType); + + // 添加自定义请求头 + foreach (var header in options.Headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + // 配置HttpClient + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(options.Timeout); + + var handler = new HttpClientHandler(); + if (!options.ValidateSsl) + { + handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; + } + + using var client = new HttpClient(handler); + client.Timeout = options.Timeout; + + var httpResponse = await client.SendAsync(request, cts.Token); + stopwatch.Stop(); + + response.StatusCode = (int)httpResponse.StatusCode; + response.IsSuccess = httpResponse.IsSuccessStatusCode; +#if NETSTANDARD2_1 + response.Content = await httpResponse.Content.ReadAsStringAsync(); +#else + response.Content = await httpResponse.Content.ReadAsStringAsync(cancellationToken); +#endif + response.Duration = stopwatch.Elapsed; + + foreach (var header in httpResponse.Headers) + { + response.Headers[header.Key] = string.Join(",", header.Value); + } + } + catch (Exception ex) + { + stopwatch.Stop(); + response.IsSuccess = false; + response.ErrorMessage = ex.Message; + response.Duration = stopwatch.Elapsed; + } + + return response; + } + + /// + /// 发送JSON Webhook + /// + /// URL + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendJsonAsync(string url, object payload, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + return await SendAsync(options, payload, cancellationToken); + } + + /// + /// 发送文本Webhook + /// + /// URL + /// 内容 + /// 取消令牌 + /// 发送结果 + public static async Task SendTextAsync(string url, string content, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + return await SendAsync(options, content, "text/plain", cancellationToken); + } + + /// + /// 发送表单Webhook + /// + /// URL + /// 表单数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendFormAsync(string url, Dictionary formData, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + var content = new FormUrlEncodedContent(formData); + var contentString = await content.ReadAsStringAsync(); + return await SendAsync(options, contentString, "application/x-www-form-urlencoded", cancellationToken); + } + } + + /// + /// Webhook客户端 + /// + public class WebhookClient + { + private readonly WebhookOptions _options; + + /// + /// 创建Webhook客户端 + /// + /// 配置 + public WebhookClient(WebhookOptions options) + { + _options = options; + } + + /// + /// 创建Webhook客户端 + /// + /// URL + public WebhookClient(string url) + { + _options = new WebhookOptions { Url = url }; + } + + /// + /// 发送Webhook + /// + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public async Task SendAsync(object payload, CancellationToken cancellationToken = default) + { + return await WebhookUtil.SendAsync(_options, payload, cancellationToken); + } + + /// + /// 发送Webhook + /// + /// 内容 + /// 内容类型 + /// 取消令牌 + /// 发送结果 + public async Task SendAsync(string content, string contentType = "application/json", CancellationToken cancellationToken = default) + { + return await WebhookUtil.SendAsync(_options, content, contentType, cancellationToken); + } + + /// + /// 添加请求头 + /// + /// 键 + /// 值 + /// this + public WebhookClient WithHeader(string key, string value) + { + _options.Headers[key] = value; + return this; + } + + /// + /// 设置超时时间 + /// + /// 超时时间 + /// this + public WebhookClient WithTimeout(TimeSpan timeout) + { + _options.Timeout = timeout; + return this; + } + + /// + /// 设置重试次数 + /// + /// 最大重试次数 + /// this + public WebhookClient WithRetry(int maxRetries) + { + _options.MaxRetries = maxRetries; + return this; + } + + /// + /// 设置成功回调 + /// + /// 回调 + /// this + public WebhookClient OnSuccess(Action onSuccess) + { + _options.OnSuccess = onSuccess; + return this; + } + + /// + /// 设置失败回调 + /// + /// 回调 + /// this + public WebhookClient OnFailure(Action onFailure) + { + _options.OnFailure = onFailure; + return this; + } + } + + /// + /// Webhook管理器 + /// + public static class WebhookManager + { + private static readonly Dictionary _webhooks = new(); + + /// + /// 注册Webhook + /// + /// 名称 + /// 配置 + public static void Register(string name, WebhookOptions options) + { + _webhooks[name] = options; + } + + /// + /// 注册Webhook + /// + /// 名称 + /// URL + public static void Register(string name, string url) + { + _webhooks[name] = new WebhookOptions { Url = url }; + } + + /// + /// 获取Webhook配置 + /// + /// 名称 + /// 配置 + public static WebhookOptions? Get(string name) + { + return _webhooks.TryGetValue(name, out var options) ? options : null; + } + + /// + /// 移除Webhook + /// + /// 名称 + /// 是否成功移除 + public static bool Remove(string name) + { + return _webhooks.Remove(name); + } + + /// + /// 发送Webhook + /// + /// 名称 + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(string name, object payload, CancellationToken cancellationToken = default) + { + var options = Get(name); + if (options == null) + { + return new WebhookResult + { + Success = false, + Exception = new Exception($"未找到名为 '{name}' 的Webhook配置") + }; + } + + return await WebhookUtil.SendAsync(options, payload, cancellationToken); + } + + /// + /// 发送到所有Webhook + /// + /// 负载数据 + /// 取消令牌 + /// 所有结果 + public static async Task> SendToAllAsync(object payload, CancellationToken cancellationToken = default) + { + var results = new Dictionary(); + + foreach (var kvp in _webhooks) + { + results[kvp.Key] = await WebhookUtil.SendAsync(kvp.Value, payload, cancellationToken); + } + + return results; + } + + /// + /// 获取所有已注册的Webhook名称 + /// + /// 名称列表 + public static IEnumerable GetNames() + { + return _webhooks.Keys; + } + + /// + /// 清空所有Webhook + /// + public static void Clear() + { + _webhooks.Clear(); + } + } +} diff --git a/EasyTool.Core/Options.cs b/EasyTool.Core/Options.cs new file mode 100644 index 0000000..f588a2c --- /dev/null +++ b/EasyTool.Core/Options.cs @@ -0,0 +1,276 @@ +using System; + +namespace EasyTool +{ + /// + /// 限流器配置选项 + /// + public class RateLimiterOptions + { + /// + /// 限流算法 + /// + public ToolCategory.RateLimitAlgorithm Algorithm { get; set; } = ToolCategory.RateLimitAlgorithm.TokenBucket; + + /// + /// 限制数量 + /// + public int Limit { get; set; } = 100; + + /// + /// 时间窗口 + /// + public TimeSpan Window { get; set; } = TimeSpan.FromSeconds(1); + + /// + /// 令牌桶容量(仅 TokenBucket 算法) + /// + public int? Capacity { get; set; } + + /// + /// 令牌补充速率(仅 TokenBucket 算法) + /// + public int? RefillRate { get; set; } + } + + /// + /// 熔断器配置选项 + /// + public class CircuitBreakerOptions + { + /// + /// 失败阈值次数 + /// + public int FailureThreshold { get; set; } = 5; + + /// + /// 成功阈值次数(半开状态) + /// + public int SuccessThreshold { get; set; } = 2; + + /// + /// 打开状态持续时间 + /// + public TimeSpan OpenDuration { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10); + } + + /// + /// 重试配置选项 + /// + public class RetryOptions + { + /// + /// 最大重试次数 + /// + public int MaxRetries { get; set; } = 3; + + /// + /// 重试延迟 + /// + public TimeSpan Delay { get; set; } = TimeSpan.FromSeconds(1); + + /// + /// 最大延迟(指数退避) + /// + public TimeSpan MaxDelay { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 是否使用指数退避 + /// + public bool UseExponentialBackoff { get; set; } = true; + + /// + /// 退避倍数 + /// + public double BackoffMultiplier { get; set; } = 2.0; + } + + /// + /// HTTP 客户端配置选项 + /// + public class HttpClientOptions + { + /// + /// 基础地址 + /// + public string? BaseAddress { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 最大响应内容缓冲区大小 + /// + public long MaxResponseContentBufferSize { get; set; } = int.MaxValue; + + /// + /// 是否自动解压缩 + /// + public bool EnableAutoDecompression { get; set; } = true; + + /// + /// 是否忽略 SSL 错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 默认请求头 + /// + public System.Collections.Generic.Dictionary DefaultHeaders { get; set; } = new(); + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } = 0; + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromSeconds(1); + } + + /// + /// 文件监控配置选项 + /// + public class FileWatcherOptions + { + /// + /// 监控路径 + /// + public string Path { get; set; } = string.Empty; + + /// + /// 文件过滤模式 + /// + public string Filter { get; set; } = "*.*"; + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories { get; set; } = true; + + /// + /// 是否监控文件名变更 + /// + public bool EnableRaisingEvents { get; set; } = true; + + /// + /// 内部缓冲区大小 + /// + public int InternalBufferSize { get; set; } = 8192; + + /// + /// 通知过滤器 + /// + public System.IO.NotifyFilters NotifyFilter { get; set; } = + System.IO.NotifyFilters.FileName | + System.IO.NotifyFilters.DirectoryName | + System.IO.NotifyFilters.LastWrite; + } + + /// + /// 日志配置选项 + /// + public class LogOptions + { + /// + /// 最小日志级别 + /// + public LogLevel MinimumLevel { get; set; } = LogLevel.Information; + + /// + /// 是否输出到控制台 + /// + public bool WriteToConsole { get; set; } = true; + + /// + /// 是否输出到文件 + /// + public bool WriteToFile { get; set; } + + /// + /// 日志文件路径 + /// + public string? LogFilePath { get; set; } + + /// + /// 日志文件滚动间隔 + /// + public RollingInterval RollingInterval { get; set; } = RollingInterval.Day; + + /// + /// 日志文件最大大小(字节) + /// + public long? MaxFileSize { get; set; } + + /// + /// 保留日志文件数量 + /// + public int? RetainedFileCount { get; set; } + + /// + /// 输出模板 + /// + public string OutputTemplate { get; set; } = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; + } + + /// + /// 日志级别 + /// + public enum LogLevel + { + Trace = 0, + Debug = 1, + Information = 2, + Warning = 3, + Error = 4, + Critical = 5, + None = 6 + } + + /// + /// 日志文件滚动间隔 + /// + public enum RollingInterval + { + Infinite = 0, + Year = 1, + Month = 2, + Day = 3, + Hour = 4, + Minute = 5 + } + + /// + /// 对象池配置选项 + /// + public class ObjectPoolOptions + { + /// + /// 最大容量 + /// + public int MaximumCapacity { get; set; } = 1024; + + /// + /// 初始容量 + /// + public int InitialCapacity { get; set; } = 10; + + /// + /// 对象最大闲置时间 + /// + public TimeSpan MaxIdleTime { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 清理间隔 + /// + public TimeSpan CleanupInterval { get; set; } = TimeSpan.FromMinutes(1); + } +} diff --git a/EasyTool.Core/QueueCategory/ChannelUtil.cs b/EasyTool.Core/QueueCategory/ChannelUtil.cs new file mode 100644 index 0000000..c76432c --- /dev/null +++ b/EasyTool.Core/QueueCategory/ChannelUtil.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Channels; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// Channel 工具类 + /// 提供线程安全的生产者-消费者队列实现 + /// + public static class ChannelUtil + { + /// + /// 创建无界 Channel + /// + /// 元素类型 + /// Channel 实例 + public static Channel CreateUnbounded() + { + return Channel.CreateUnbounded(); + } + + /// + /// 创建无界 Channel(带选项) + /// + /// 元素类型 + /// Channel 选项 + /// Channel 实例 + public static Channel CreateUnbounded(UnboundedChannelOptions options) + { + return Channel.CreateUnbounded(options); + } + + /// + /// 创建有界 Channel + /// + /// 元素类型 + /// 容量 + /// Channel 实例 + public static Channel CreateBounded(int capacity) + { + return Channel.CreateBounded(capacity); + } + + /// + /// 创建有界 Channel(带选项) + /// + /// 元素类型 + /// Channel 选项 + /// Channel 实例 + public static Channel CreateBounded(BoundedChannelOptions options) + { + return Channel.CreateBounded(options); + } + + /// + /// 批量写入数据 + /// + /// 元素类型 + /// Channel 实例 + /// 数据集合 + /// 取消令牌 + public static async Task WriteManyAsync(Channel channel, IEnumerable items, CancellationToken cancellationToken = default) + { + foreach (var item in items) + { + await channel.Writer.WriteAsync(item, cancellationToken); + } + } + + /// + /// 批量读取数据 + /// + /// 元素类型 + /// Channel 实例 + /// 读取数量 + /// 取消令牌 + /// 数据列表 + public static async Task> ReadManyAsync(Channel channel, int count, CancellationToken cancellationToken = default) + { + var result = new List(); + + for (int i = 0; i < count; i++) + { + if (await channel.Reader.WaitToReadAsync(cancellationToken)) + { + if (channel.Reader.TryRead(out var item)) + { + result.Add(item); + } + } + else + { + break; + } + } + + return result; + } + + /// + /// 读取所有数据直到完成 + /// + /// 元素类型 + /// Channel 实例 + /// 取消令牌 + /// 数据列表 + public static async Task> ReadAllAsync(Channel channel, CancellationToken cancellationToken = default) + { + var result = new List(); + + await foreach (var item in channel.Reader.ReadAllAsync(cancellationToken)) + { + result.Add(item); + } + + return result; + } + + /// + /// 创建异步生产者-消费者处理器 + /// + /// 元素类型 + /// 容量(null 表示无界) + /// 处理函数 + /// 消费者数量 + /// 取消令牌 + /// 生产者写入器和完成任务 + public static (ChannelWriter Writer, Task Completion) CreateProcessor( + int? capacity, + Func processAction, + int consumerCount = 1, + CancellationToken cancellationToken = default) + { + var channel = capacity.HasValue + ? Channel.CreateBounded(capacity.Value) + : Channel.CreateUnbounded(); + + var consumers = new Task[consumerCount]; + + for (int i = 0; i < consumerCount; i++) + { + consumers[i] = ConsumeAsync(channel.Reader, processAction, cancellationToken); + } + + var completion = Task.WhenAll(consumers); + + return (channel.Writer, completion); + } + + /// + /// 创建带批处理的消费者 + /// + /// 元素类型 + /// 容量 + /// 批处理大小 + /// 批处理超时 + /// 处理函数 + /// 取消令牌 + /// 生产者写入器和完成任务 + public static (ChannelWriter Writer, Task Completion) CreateBatchProcessor( + int capacity, + int batchSize, + TimeSpan batchTimeout, + Func, Task> processAction, + CancellationToken cancellationToken = default) + { + var channel = Channel.CreateBounded(new BoundedChannelOptions(capacity) + { + FullMode = BoundedChannelFullMode.Wait + }); + + var completion = ProcessBatchAsync(channel.Reader, batchSize, batchTimeout, processAction, cancellationToken); + + return (channel.Writer, completion); + } + + private static async Task ConsumeAsync(ChannelReader reader, Func processAction, CancellationToken cancellationToken) + { + await foreach (var item in reader.ReadAllAsync(cancellationToken)) + { + await processAction(item); + } + } + + private static async Task ProcessBatchAsync( + ChannelReader reader, + int batchSize, + TimeSpan batchTimeout, + Func, Task> processAction, + CancellationToken cancellationToken) + { + var batch = new List(batchSize); + + while (await reader.WaitToReadAsync(cancellationToken)) + { + batch.Clear(); + + // 尝试收集一批数据 + while (batch.Count < batchSize && reader.TryRead(out var item)) + { + batch.Add(item); + } + + if (batch.Count > 0) + { + await processAction(batch); + } + } + + // 处理剩余数据 + if (batch.Count > 0) + { + await processAction(batch); + } + } + } + + /// + /// 异步队列 + /// 提供简单的异步队列操作封装 + /// + /// 元素类型 + public class AsyncQueue : IDisposable + { + private readonly Channel _channel; + private bool _disposed; + + /// + /// 获取当前队列长度 + /// + public int Count => _channel.Reader.Count; + + /// + /// 是否完成写入 + /// + public bool IsCompleted => _channel.Reader.Completion.IsCompleted; + + /// + /// 创建异步队列(无界) + /// + public AsyncQueue() + { + _channel = Channel.CreateUnbounded(); + } + + /// + /// 创建异步队列(有界) + /// + /// 容量 + /// 满时策略 + public AsyncQueue(int capacity, BoundedChannelFullMode fullMode = BoundedChannelFullMode.Wait) + { + _channel = Channel.CreateBounded(new BoundedChannelOptions(capacity) + { + FullMode = fullMode + }); + } + + /// + /// 入队 + /// + /// 元素 + /// 取消令牌 + public async ValueTask EnqueueAsync(T item, CancellationToken cancellationToken = default) + { + await _channel.Writer.WriteAsync(item, cancellationToken); + } + + /// + /// 入队(同步) + /// + /// 元素 + /// 是否成功 + public bool Enqueue(T item) + { + return _channel.Writer.TryWrite(item); + } + + /// + /// 出队 + /// + /// 取消令牌 + /// 元素 + public async ValueTask DequeueAsync(CancellationToken cancellationToken = default) + { + return await _channel.Reader.ReadAsync(cancellationToken); + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) + { + return _channel.Reader.TryRead(out item); + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 是否成功 + public bool TryPeek(out T? item) + { + return _channel.Reader.TryPeek(out item); + } + + /// + /// 等待有数据可读 + /// + /// 取消令牌 + /// 是否有数据 + public ValueTask WaitToReadAsync(CancellationToken cancellationToken = default) + { + return _channel.Reader.WaitToReadAsync(cancellationToken); + } + + /// + /// 获取所有数据(异步迭代) + /// + /// 取消令牌 + /// 异步迭代器 + public IAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default) + { + return _channel.Reader.ReadAllAsync(cancellationToken); + } + + /// + /// 完成写入 + /// + public void Complete() + { + _channel.Writer.Complete(); + } + + /// + /// 等待完成 + /// + /// 完成任务 + public Task Completion => _channel.Reader.Completion; + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _channel.Writer.TryComplete(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/QueueCategory/DelayQueue.cs b/EasyTool.Core/QueueCategory/DelayQueue.cs new file mode 100644 index 0000000..3cd0d43 --- /dev/null +++ b/EasyTool.Core/QueueCategory/DelayQueue.cs @@ -0,0 +1,315 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 延迟队列项 + /// + /// 元素类型 + internal class DelayQueueItem + { + public T Value { get; set; } = default!; + public DateTime ExecuteTime { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); + } + + /// + /// 延迟队列 + /// 支持按时间延迟执行任务 + /// + /// 元素类型 + public class DelayQueue : IDisposable + { + private readonly ConcurrentDictionary> _items; + private readonly List> _sortedItems; + private readonly SemaphoreSlim _signal; + private readonly CancellationTokenSource _cts; + private readonly Task _processTask; + private readonly object _lock = new(); + private bool _disposed; + + /// + /// 获取队列长度 + /// + public int Count => _items.Count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.IsEmpty; + + /// + /// 创建延迟队列 + /// + public DelayQueue() + { + _items = new ConcurrentDictionary>(); + _sortedItems = new List>(); + _signal = new SemaphoreSlim(0); + _cts = new CancellationTokenSource(); + _processTask = ProcessAsync(_cts.Token); + } + + /// + /// 添加延迟元素 + /// + /// 元素值 + /// 延迟时间 + /// 元素ID,可用于取消 + public Guid Add(T value, TimeSpan delay) + { + return Add(value, DateTime.UtcNow.Add(delay)); + } + + /// + /// 添加延迟元素 + /// + /// 元素值 + /// 执行时间 + /// 元素ID,可用于取消 + public Guid Add(T value, DateTime executeTime) + { + var item = new DelayQueueItem + { + Value = value, + ExecuteTime = executeTime + }; + + lock (_lock) + { + _items[item.Id] = item; + InsertSorted(_sortedItems, item); + } + + _signal.Release(); + return item.Id; + } + + /// + /// 尝试取消元素 + /// + /// 元素ID + /// 是否取消成功 + public bool TryCancel(Guid id) + { + lock (_lock) + { + if (_items.TryRemove(id, out var item)) + { + _sortedItems.Remove(item); + return true; + } + } + + return false; + } + + /// + /// 异步等待并获取到期元素 + /// + /// 取消令牌 + /// 到期元素 + public async Task TakeAsync(CancellationToken cancellationToken = default) + { + while (!cancellationToken.IsCancellationRequested) + { + DelayQueueItem? item; + + lock (_lock) + { + if (_sortedItems.Count > 0) + { + var first = _sortedItems[0]; + var now = DateTime.UtcNow; + + if (now >= first.ExecuteTime) + { + _sortedItems.RemoveAt(0); + _items.TryRemove(first.Id, out _); + return first.Value; + } + } + } + + // 计算等待时间 + var waitTime = GetWaitTime(); + + if (waitTime > TimeSpan.Zero) + { + await Task.Delay(waitTime, cancellationToken); + } + } + + throw new OperationCanceledException(); + } + + /// + /// 尝试获取到期元素(非阻塞) + /// + /// 元素值 + /// 是否成功获取 + public bool TryTake(out T? value) + { + value = default; + + lock (_lock) + { + if (_sortedItems.Count > 0) + { + var first = _sortedItems[0]; + + if (DateTime.UtcNow >= first.ExecuteTime) + { + _sortedItems.RemoveAt(0); + _items.TryRemove(first.Id, out _); + value = first.Value; + return true; + } + } + } + + return false; + } + + /// + /// 尝试在指定时间内获取到期元素 + /// + /// 超时时间 + /// 元素值 + /// 是否成功获取 + public bool TryTake(TimeSpan timeout, out T? value) + { + value = default; + var endTime = DateTime.UtcNow.Add(timeout); + + while (DateTime.UtcNow < endTime) + { + if (TryTake(out value)) + { + return true; + } + + var remaining = endTime - DateTime.UtcNow; + if (remaining <= TimeSpan.Zero) + break; + + var waitTime = GetWaitTime(); + if (waitTime > TimeSpan.Zero) + { + Thread.Sleep((int)Math.Min(waitTime.TotalMilliseconds, remaining.TotalMilliseconds)); + } + } + + return false; + } + + /// + /// 获取所有元素(不等待到期) + /// + /// 元素列表 + public List<(T Value, DateTime ExecuteTime)> GetAll() + { + lock (_lock) + { + return _sortedItems + .Select(i => (i.Value, i.ExecuteTime)) + .ToList(); + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + _sortedItems.Clear(); + } + } + + /// + /// 创建处理器 + /// + /// 处理函数 + /// 取消令牌 + /// 处理任务 + public Task ProcessAsync(Func handler, CancellationToken cancellationToken = default) + { + return Task.Run(async () => + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var value = await TakeAsync(cancellationToken); + await handler(value); + } + catch (OperationCanceledException) + { + break; + } + } + }, cancellationToken); + } + + private TimeSpan GetWaitTime() + { + lock (_lock) + { + if (_sortedItems.Count == 0) + return TimeSpan.FromSeconds(1); + + var first = _sortedItems[0]; + var waitTime = first.ExecuteTime - DateTime.UtcNow; + return waitTime > TimeSpan.Zero ? waitTime : TimeSpan.Zero; + } + } + + private void InsertSorted(List> list, DelayQueueItem item) + { + var index = list.BinarySearch(item, Comparer>.Create((a, b) => + a.ExecuteTime.CompareTo(b.ExecuteTime))); + + if (index < 0) + index = ~index; + + list.Insert(index, item); + } + + private async Task ProcessAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await _signal.WaitAsync(cancellationToken); + } + catch (OperationCanceledException) + { + break; + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cts.Cancel(); + _signal.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/QueueCategory/MessageQueueUtil.cs b/EasyTool.Core/QueueCategory/MessageQueueUtil.cs new file mode 100644 index 0000000..61d5d7a --- /dev/null +++ b/EasyTool.Core/QueueCategory/MessageQueueUtil.cs @@ -0,0 +1,543 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 消息类型 + /// + public enum MessageType + { + /// + /// 普通消息 + /// + Normal, + + /// + /// 延迟消息 + /// + Delayed, + + /// + /// 优先级消息 + /// + Priority + } + + /// + /// 消息封装 + /// + /// 消息体类型 + public class Message + { + /// + /// 消息ID + /// + public string Id { get; set; } = Guid.NewGuid().ToString("N"); + + /// + /// 消息体 + /// + public T Body { get; set; } = default!; + + /// + /// 创建时间 + /// + public DateTime CreateTime { get; set; } = DateTime.UtcNow; + + /// + /// 过期时间 + /// + public DateTime? ExpireTime { get; set; } + + /// + /// 延迟执行时间 + /// + public DateTime? DelayTo { get; set; } + + /// + /// 优先级(越大越优先) + /// + public int Priority { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + + /// + /// 最大重试次数 + /// + public int MaxRetryCount { get; set; } = 3; + + /// + /// 消息头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否已过期 + /// + public bool IsExpired => ExpireTime.HasValue && DateTime.UtcNow >= ExpireTime.Value; + + /// + /// 是否可以处理(延迟消息检查) + /// + public bool CanProcess => DelayTo == null || DateTime.UtcNow >= DelayTo.Value; + } + + /// + /// 消息队列选项 + /// + public class MessageQueueOptions + { + /// + /// 队列名称 + /// + public string Name { get; set; } = "default"; + + /// + /// 最大容量 + /// + public int MaxCapacity { get; set; } = 10000; + + /// + /// 消费者数量 + /// + public int ConsumerCount { get; set; } = 1; + + /// + /// 默认消息过期时间 + /// + public TimeSpan? DefaultMessageTtl { get; set; } + + /// + /// 默认重试延迟 + /// + public TimeSpan DefaultRetryDelay { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 死信队列是否启用 + /// + public bool EnableDeadLetterQueue { get; set; } = true; + } + + /// + /// 内存消息队列 + /// + /// 消息体类型 + public class MessageQueue : IDisposable + { + private readonly MessageQueueOptions _options; + private readonly ConcurrentQueue> _normalQueue; + private readonly PriorityQueue, int> _priorityQueue; + private readonly ConcurrentQueue> _delayedQueue; + private readonly ConcurrentQueue> _deadLetterQueue; + private readonly SemaphoreSlim _signal; + private readonly CancellationTokenSource _cts; + private readonly List _consumerTasks; + private readonly Func, Task> _handler; + private bool _disposed; + private bool _started; + + /// + /// 队列名称 + /// + public string Name => _options.Name; + + /// + /// 普通队列长度 + /// + public int NormalQueueCount => _normalQueue.Count; + + /// + /// 优先级队列长度 + /// + public int PriorityQueueCount => _priorityQueue.Count; + + /// + /// 延迟队列长度 + /// + public int DelayedQueueCount => _delayedQueue.Count; + + /// + /// 死信队列长度 + /// + public int DeadLetterQueueCount => _deadLetterQueue.Count; + + /// + /// 创建消息队列 + /// + /// 消息处理器 + /// 队列选项 + public MessageQueue(Func, Task> handler, MessageQueueOptions? options = null) + { + _options = options ?? new MessageQueueOptions(); + _handler = handler; + _normalQueue = new ConcurrentQueue>(); + _priorityQueue = new PriorityQueue, int>(); + _delayedQueue = new ConcurrentQueue>(); + _deadLetterQueue = new ConcurrentQueue>(); + _signal = new SemaphoreSlim(0); + _cts = new CancellationTokenSource(); + _consumerTasks = new List(); + } + + /// + /// 启动队列消费者 + /// + public void Start() + { + if (_started) + return; + + _started = true; + + for (int i = 0; i < _options.ConsumerCount; i++) + { + _consumerTasks.Add(ConsumeAsync(_cts.Token)); + } + + // 启动延迟消息检查任务 + _consumerTasks.Add(ProcessDelayedMessagesAsync(_cts.Token)); + } + + /// + /// 停止队列消费者 + /// + /// 是否等待处理完成 + public async Task StopAsync(bool waitForCompletion = true) + { + _cts.Cancel(); + + if (waitForCompletion) + { + await Task.WhenAll(_consumerTasks); + } + } + + /// + /// 发布消息 + /// + /// 消息体 + /// 消息类型 + /// 优先级 + /// 延迟时间 + /// 消息ID + public string Publish(T body, MessageType type = MessageType.Normal, int priority = 0, TimeSpan? delay = null) + { + var message = new Message + { + Body = body, + Priority = priority, + ExpireTime = _options.DefaultMessageTtl.HasValue + ? DateTime.UtcNow.Add(_options.DefaultMessageTtl.Value) + : null + }; + + if (delay.HasValue) + { + message.DelayTo = DateTime.UtcNow.Add(delay.Value); + type = MessageType.Delayed; + } + + switch (type) + { + case MessageType.Priority: + _priorityQueue.Enqueue(message, -priority); // 负数让高优先级先出 + break; + case MessageType.Delayed: + _delayedQueue.Enqueue(message); + break; + default: + _normalQueue.Enqueue(message); + break; + } + + _signal.Release(); + return message.Id; + } + + /// + /// 批量发布消息 + /// + /// 消息体集合 + /// 消息ID列表 + public List PublishMany(IEnumerable bodies) + { + var ids = new List(); + + foreach (var body in bodies) + { + ids.Add(Publish(body)); + } + + return ids; + } + + /// + /// 获取死信队列消息 + /// + /// 消息列表 + public List> GetDeadLetterMessages() + { + var messages = new List>(); + + while (_deadLetterQueue.TryDequeue(out var message)) + { + messages.Add(message); + } + + return messages; + } + + /// + /// 重试死信消息 + /// + public void RetryDeadLetterMessages() + { + var messages = GetDeadLetterMessages(); + + foreach (var message in messages) + { + message.RetryCount = 0; + Publish(message.Body); + } + } + + private async Task ConsumeAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await _signal.WaitAsync(cancellationToken); + + Message? message = null; + + // 优先处理优先级队列 + if (_priorityQueue.TryDequeue(out var priorityMessage, out _)) + { + message = priorityMessage; + } + // 再处理普通队列 + else if (_normalQueue.TryDequeue(out var normalMessage)) + { + message = normalMessage; + } + + if (message == null) + continue; + + // 检查是否过期 + if (message.IsExpired) + continue; + + // 处理消息 + var result = await _handler(message); + + switch (result.Action) + { + case ProcessAction.Complete: + // 消息处理完成,无需操作 + break; + + case ProcessAction.Retry: + message.RetryCount++; + if (message.RetryCount < message.MaxRetryCount) + { + await Task.Delay(_options.DefaultRetryDelay, cancellationToken); + Publish(message.Body, MessageType.Normal); + } + else if (_options.EnableDeadLetterQueue) + { + _deadLetterQueue.Enqueue(message); + } + break; + + case ProcessAction.DeadLetter: + if (_options.EnableDeadLetterQueue) + { + _deadLetterQueue.Enqueue(message); + } + break; + } + } + catch (OperationCanceledException) + { + break; + } + catch (Exception) + { + // 记录错误,继续处理 + } + } + } + + private async Task ProcessDelayedMessagesAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); + + var now = DateTime.UtcNow; + var readyMessages = new List>(); + + // 检查延迟消息 + while (_delayedQueue.TryDequeue(out var message)) + { + if (message.CanProcess && !message.IsExpired) + { + readyMessages.Add(message); + } + else if (!message.IsExpired) + { + // 未到处理时间,重新入队 + _delayedQueue.Enqueue(message); + break; + } + } + + // 将就绪的消息发送到普通队列 + foreach (var message in readyMessages) + { + _normalQueue.Enqueue(message); + _signal.Release(); + } + } + catch (OperationCanceledException) + { + break; + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cts.Cancel(); + _signal.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } + } + + /// + /// 处理结果 + /// + public class ProcessResult + { + /// + /// 处理动作 + /// + public ProcessAction Action { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 创建完成结果 + /// + public static ProcessResult Complete => new() { Action = ProcessAction.Complete }; + + /// + /// 创建重试结果 + /// + public static ProcessResult Retry => new() { Action = ProcessAction.Retry }; + + /// + /// 创建死信结果 + /// + public static ProcessResult DeadLetter => new() { Action = ProcessAction.DeadLetter }; + + /// + /// 创建错误结果 + /// + public static ProcessResult Error(string message) => new() { Action = ProcessAction.Retry, ErrorMessage = message }; + } + + /// + /// 处理动作 + /// + public enum ProcessAction + { + /// + /// 完成 + /// + Complete, + + /// + /// 重试 + /// + Retry, + + /// + /// 死信 + /// + DeadLetter + } + + /// + /// 消息队列工具类 + /// + public static class MessageQueueUtil + { + private static readonly ConcurrentDictionary _queues = new(); + + /// + /// 创建或获取消息队列 + /// + /// 消息体类型 + /// 队列名称 + /// 消息处理器 + /// 队列选项 + /// 消息队列 + public static MessageQueue GetOrCreate( + string name, + Func, Task> handler, + MessageQueueOptions? options = null) + { + options ??= new MessageQueueOptions { Name = name }; + + return (MessageQueue)_queues.GetOrAdd(name, _ => new MessageQueue(handler, options)); + } + + /// + /// 移除消息队列 + /// + /// 队列名称 + public static void Remove(string name) + { + if (_queues.TryRemove(name, out var queue)) + { + (queue as IDisposable)?.Dispose(); + } + } + + /// + /// 清空所有队列 + /// + public static void ClearAll() + { + foreach (var queue in _queues.Values) + { + (queue as IDisposable)?.Dispose(); + } + + _queues.Clear(); + } + } +} diff --git a/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs b/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs new file mode 100644 index 0000000..ea6219d --- /dev/null +++ b/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs @@ -0,0 +1,211 @@ +#if NETSTANDARD2_1 +using System; +using System.Collections; +using System.Collections.Generic; + +namespace System.Collections.Generic +{ + /// + /// 优先队列 polyfill for netstandard2.1 + /// + /// 元素类型 + /// 优先级类型 + public class PriorityQueue + { + private readonly List<(TElement Element, TPriority Priority)> _items; + private readonly IComparer? _comparer; + + /// + /// 获取队列中的元素数量 + /// + public int Count => _items.Count; + + /// + /// 创建优先队列 + /// + public PriorityQueue() + { + _items = new List<(TElement, TPriority)>(); + _comparer = null; + } + + /// + /// 创建优先队列 + /// + /// 初始容量 + public PriorityQueue(int initialCapacity) + { + _items = new List<(TElement, TPriority)>(initialCapacity); + _comparer = null; + } + + /// + /// 创建优先队列 + /// + /// 优先级比较器 + public PriorityQueue(IComparer? comparer) + { + _items = new List<(TElement, TPriority)>(); + _comparer = comparer; + } + + /// + /// 创建优先队列 + /// + /// 初始容量 + /// 优先级比较器 + public PriorityQueue(int initialCapacity, IComparer? comparer) + { + _items = new List<(TElement, TPriority)>(initialCapacity); + _comparer = comparer; + } + + /// + /// 入队 + /// + /// 元素 + /// 优先级 + public void Enqueue(TElement element, TPriority priority) + { + _items.Add((element, priority)); + HeapifyUp(_items.Count - 1); + } + + /// + /// 出队 + /// + /// 元素 + public TElement Dequeue() + { + if (_items.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + var result = _items[0].Element; + var lastIndex = _items.Count - 1; + _items[0] = _items[lastIndex]; + _items.RemoveAt(lastIndex); + + if (_items.Count > 0) + HeapifyDown(0); + + return result; + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryDequeue(out TElement element, out TPriority priority) + { + if (_items.Count == 0) + { + element = default!; + priority = default!; + return false; + } + + var item = _items[0]; + element = item.Element; + priority = item.Priority; + + var lastIndex = _items.Count - 1; + _items[0] = _items[lastIndex]; + _items.RemoveAt(lastIndex); + + if (_items.Count > 0) + HeapifyDown(0); + + return true; + } + + /// + /// 查看队首元素 + /// + /// 元素 + public TElement Peek() + { + if (_items.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + return _items[0].Element; + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryPeek(out TElement element, out TPriority priority) + { + if (_items.Count == 0) + { + element = default!; + priority = default!; + return false; + } + + var item = _items[0]; + element = item.Element; + priority = item.Priority; + return true; + } + + /// + /// 清空队列 + /// + public void Clear() + { + _items.Clear(); + } + + private void HeapifyUp(int index) + { + var comparer = _comparer ?? Comparer.Default; + while (index > 0) + { + var parentIndex = (index - 1) / 2; + if (comparer.Compare(_items[index].Priority, _items[parentIndex].Priority) >= 0) + break; + + Swap(index, parentIndex); + index = parentIndex; + } + } + + private void HeapifyDown(int index) + { + var comparer = _comparer ?? Comparer.Default; + var count = _items.Count; + + while (true) + { + var leftChild = 2 * index + 1; + var rightChild = 2 * index + 2; + var smallest = index; + + if (leftChild < count && comparer.Compare(_items[leftChild].Priority, _items[smallest].Priority) < 0) + smallest = leftChild; + + if (rightChild < count && comparer.Compare(_items[rightChild].Priority, _items[smallest].Priority) < 0) + smallest = rightChild; + + if (smallest == index) + break; + + Swap(index, smallest); + index = smallest; + } + } + + private void Swap(int i, int j) + { + var temp = _items[i]; + _items[i] = _items[j]; + _items[j] = temp; + } + } +} +#endif diff --git a/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs b/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs new file mode 100644 index 0000000..809f276 --- /dev/null +++ b/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs @@ -0,0 +1,329 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 优先级队列工具类 + /// + public static class PriorityQueueUtil + { + /// + /// 创建最小堆优先队列 + /// + /// 元素类型 + /// 比较器 + /// 优先队列 + public static PriorityQueue CreateMin(IComparer? comparer = null) + { + return new PriorityQueue(comparer ?? Comparer.Default); + } + + /// + /// 创建最大堆优先队列 + /// + /// 元素类型 + /// 比较器 + /// 优先队列 + public static PriorityQueue CreateMax(IComparer? comparer = null) + { + var reverseComparer = Comparer.Create((a, b) => + (comparer ?? Comparer.Default).Compare(b, a)); + return new PriorityQueue(reverseComparer); + } + + /// + /// 从集合创建优先队列 + /// + /// 元素类型 + /// 优先级类型 + /// 元素集合 + /// 优先级选择器 + /// 优先队列 + public static PriorityQueue FromCollection( + IEnumerable items, + Func prioritySelector) + { + var queue = new PriorityQueue(); + foreach (var item in items) + { + queue.Enqueue(item, prioritySelector(item)); + } + return queue; + } + + /// + /// 批量入队 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素集合 + /// 优先级选择器 + public static void EnqueueRange( + this PriorityQueue queue, + IEnumerable items, + Func prioritySelector) + { + foreach (var item in items) + { + queue.Enqueue(item, prioritySelector(item)); + } + } + + /// + /// 批量出队 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 数量 + /// 元素列表 + public static List DequeueRange( + this PriorityQueue queue, + int count) + { + var result = new List(); + + for (int i = 0; i < count && queue.Count > 0; i++) + { + if (queue.TryDequeue(out var element, out _)) + { + result.Add(element); + } + } + + return result; + } + + /// + /// 查看队首元素但不移除 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素 + /// 优先级 + /// 是否成功 + public static bool TryPeek( + this PriorityQueue queue, + out TElement? element, + out TPriority? priority) + { + element = default; + priority = default; + + if (queue.Count == 0) + return false; + + // 通过出队再入队的方式实现 Peek + if (queue.TryDequeue(out element, out priority)) + { + queue.Enqueue(element!, priority!); + return true; + } + + return false; + } + + /// + /// 获取所有元素(按优先级排序,不移除) + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素列表 + public static List<(TElement Element, TPriority Priority)> ToSortedList( + this PriorityQueue queue) + { + var tempQueue = new PriorityQueue(); + var result = new List<(TElement, TPriority)>(); + + while (queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + tempQueue.Enqueue(element!, priority!); + } + + // 恢复队列 + foreach (var (element, priority) in result) + { + queue.Enqueue(element, priority); + } + + return result; + } + } + + /// + /// 线程安全的优先队列 + /// + /// 元素类型 + /// 优先级类型 + public class ConcurrentPriorityQueue where TPriority : IComparable + { + private readonly PriorityQueue _queue; + private readonly object _lock = new(); + + /// + /// 获取队列长度 + /// + public int Count + { + get + { + lock (_lock) + { + return _queue.Count; + } + } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 创建线程安全的优先队列 + /// + /// 优先级比较器 + public ConcurrentPriorityQueue(IComparer? comparer = null) + { + _queue = new PriorityQueue(comparer ?? Comparer.Default); + } + + /// + /// 入队 + /// + /// 元素 + /// 优先级 + public void Enqueue(TElement element, TPriority priority) + { + lock (_lock) + { + _queue.Enqueue(element, priority); + } + } + + /// + /// 批量入队 + /// + /// 元素集合 + public void EnqueueRange(IEnumerable<(TElement Element, TPriority Priority)> items) + { + lock (_lock) + { + foreach (var (element, priority) in items) + { + _queue.Enqueue(element, priority); + } + } + } + + /// + /// 出队 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryDequeue(out TElement? element, out TPriority? priority) + { + lock (_lock) + { + return _queue.TryDequeue(out element, out priority); + } + } + + /// + /// 批量出队 + /// + /// 数量 + /// 元素列表 + public List<(TElement Element, TPriority Priority)> DequeueRange(int count) + { + var result = new List<(TElement, TPriority)>(); + + lock (_lock) + { + for (int i = 0; i < count && _queue.Count > 0; i++) + { + if (_queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + } + } + } + + return result; + } + + /// + /// 查看队首元素 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryPeek(out TElement? element, out TPriority? priority) + { + lock (_lock) + { + if (_queue.Count == 0) + { + element = default; + priority = default; + return false; + } + + if (_queue.TryDequeue(out element, out priority)) + { + _queue.Enqueue(element!, priority!); + return true; + } + + return false; + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + while (_queue.TryDequeue(out _, out _)) { } + } + } + + /// + /// 转换为数组 + /// + /// 数组 + public (TElement Element, TPriority Priority)[] ToArray() + { + lock (_lock) + { + var tempQueue = new PriorityQueue(); + var result = new List<(TElement, TPriority)>(); + + while (_queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + tempQueue.Enqueue(element!, priority!); + } + + // 恢复队列 + foreach (var (element, priority) in result) + { + _queue.Enqueue(element, priority); + } + + return result.ToArray(); + } + } + } +} diff --git a/EasyTool.Core/ReflectCategory/EnumUtil.cs b/EasyTool.Core/ReflectCategory/EnumUtil.cs new file mode 100644 index 0000000..435421d --- /dev/null +++ b/EasyTool.Core/ReflectCategory/EnumUtil.cs @@ -0,0 +1,212 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ReflectCategory +{ + /// + /// 枚举工具类 + /// + public static class EnumUtil + { + /// + /// 获取枚举所有值 + /// + public static IEnumerable GetValues() where T : struct, Enum + { + return Enum.GetValues(typeof(T)).Cast(); + } + + /// + /// 获取枚举所有名称 + /// + public static IEnumerable GetNames() where T : struct, Enum + { + return Enum.GetNames(typeof(T)); + } + + /// + /// 解析枚举 + /// + public static T Parse(string value, bool ignoreCase = true) where T : struct, Enum + { + return (T)Enum.Parse(typeof(T), value, ignoreCase); + } + + /// + /// 尝试解析枚举 + /// + public static bool TryParse(string value, out T result, bool ignoreCase = true) where T : struct, Enum + { + return Enum.TryParse(value, ignoreCase, out result); + } + + /// + /// 检查值是否定义 + /// + public static bool IsDefined(T value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 检查整数值是否定义 + /// + public static bool IsDefined(int value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 转换为整数 + /// + public static int ToInt(T value) where T : struct, Enum + { + return Convert.ToInt32(value); + } + + /// + /// 从整数转换 + /// + public static T FromInt(int value) where T : struct, Enum + { + return (T)Enum.ToObject(typeof(T), value); + } + + /// + /// 获取枚举项信息 + /// + public static IEnumerable> GetItems() where T : struct, Enum + { + var type = typeof(T); + var names = Enum.GetNames(type); + var values = Enum.GetValues(type).Cast(); + + return names.Zip(values, (name, value) => new EnumItem + { + Name = name, + Value = value, + IntValue = Convert.ToInt32(value) + }); + } + + /// + /// 获取枚举项数量 + /// + public static int GetCount() where T : struct, Enum + { + return Enum.GetNames(typeof(T)).Length; + } + + /// + /// 获取随机枚举值 + /// + public static T GetRandomValue(Random? random = null) where T : struct, Enum + { + var values = GetValues().ToArray(); + var r = random ?? new Random(); + return values[r.Next(values.Length)]; + } + + /// + /// 检查是否包含标志 + /// + public static bool HasFlag(T value, T flag) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + return (intValue & intFlag) == intFlag; + } + + /// + /// 设置标志 + /// + public static T SetFlag(T value, T flag, bool set = true) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + + if (set) + intValue |= intFlag; + else + intValue &= ~intFlag; + + return (T)Enum.ToObject(typeof(T), intValue); + } + + /// + /// 清除标志 + /// + public static T ClearFlag(T value, T flag) where T : struct, Enum + { + return SetFlag(value, flag, false); + } + + /// + /// 切换标志 + /// + public static T ToggleFlag(T value, T flag) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + intValue ^= intFlag; + return (T)Enum.ToObject(typeof(T), intValue); + } + + /// + /// 获取所有标志 + /// + public static IEnumerable GetFlags(T value) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + + foreach (var flag in GetValues()) + { + var intFlag = Convert.ToInt64(flag); + if (intFlag != 0 && (intValue & intFlag) == intFlag) + { + yield return flag; + } + } + } + + /// + /// 组合标志 + /// + public static T CombineFlags(params T[] flags) where T : struct, Enum + { + long result = 0; + foreach (var flag in flags) + { + result |= Convert.ToInt64(flag); + } + return (T)Enum.ToObject(typeof(T), result); + } + } + + /// + /// 枚举项信息 + /// + public class EnumItem where T : struct, Enum + { + /// + /// 名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 枚举值 + /// + public T Value { get; set; } + + /// + /// 整数值 + /// + public int IntValue { get; set; } + + public override string ToString() + { + return $"{Name} ({IntValue})"; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ReflectCategory/ExpressionUtil.cs b/EasyTool.Core/ReflectCategory/ExpressionUtil.cs new file mode 100644 index 0000000..265db9b --- /dev/null +++ b/EasyTool.Core/ReflectCategory/ExpressionUtil.cs @@ -0,0 +1,324 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// 表达式工具类 + /// + public static class ExpressionUtil + { + #region 属性访问 + + /// + /// 获取属性名称 + /// + public static string GetPropertyName(Expression> propertyExpression) + { + if (propertyExpression.Body is MemberExpression memberExpression) + { + return memberExpression.Member.Name; + } + + if (propertyExpression.Body is UnaryExpression unaryExpression && + unaryExpression.Operand is MemberExpression operand) + { + return operand.Member.Name; + } + + throw new ArgumentException("表达式不是有效的属性访问表达式"); + } + + /// + /// 获取属性信息 + /// + public static PropertyInfo GetProperty(Expression> propertyExpression) + { + if (propertyExpression.Body is MemberExpression memberExpression && + memberExpression.Member is PropertyInfo propertyInfo) + { + return propertyInfo; + } + + if (propertyExpression.Body is UnaryExpression unaryExpression && + unaryExpression.Operand is MemberExpression operand && + operand.Member is PropertyInfo propInfo) + { + return propInfo; + } + + throw new ArgumentException("表达式不是有效的属性访问表达式"); + } + + /// + /// 创建属性获取器 + /// + public static Func CreateGetter(Expression> propertyExpression) + { + return propertyExpression.Compile(); + } + + /// + /// 创建属性设置器 + /// + public static Action CreateSetter(Expression> propertyExpression) + { + var parameter = Expression.Parameter(typeof(TProperty), "value"); + var property = GetProperty(propertyExpression); + + var setter = Expression.Lambda>( + Expression.Call(propertyExpression.Parameters[0], property.GetSetMethod()!, parameter), + propertyExpression.Parameters[0], parameter); + + return setter.Compile(); + } + + #endregion + + #region 条件表达式 + + /// + /// 组合多个条件表达式(AND) + /// + public static Expression> And(params Expression>[] expressions) + { + if (expressions == null || expressions.Length == 0) + return _ => true; + + if (expressions.Length == 1) + return expressions[0]; + + var parameter = expressions[0].Parameters[0]; + var body = expressions[0].Body; + + for (int i = 1; i < expressions.Length; i++) + { + var visitor = new ParameterReplacer(expressions[i].Parameters[0], parameter); + body = Expression.AndAlso(body, visitor.Visit(expressions[i].Body)); + } + + return Expression.Lambda>(body, parameter); + } + + /// + /// 组合多个条件表达式(OR) + /// + public static Expression> Or(params Expression>[] expressions) + { + if (expressions == null || expressions.Length == 0) + return _ => false; + + if (expressions.Length == 1) + return expressions[0]; + + var parameter = expressions[0].Parameters[0]; + var body = expressions[0].Body; + + for (int i = 1; i < expressions.Length; i++) + { + var visitor = new ParameterReplacer(expressions[i].Parameters[0], parameter); + body = Expression.OrElse(body, visitor.Visit(expressions[i].Body)); + } + + return Expression.Lambda>(body, parameter); + } + + /// + /// 取反条件表达式 + /// + public static Expression> Not(Expression> expression) + { + var body = Expression.Not(expression.Body); + return Expression.Lambda>(body, expression.Parameters[0]); + } + + #endregion + + #region 排序表达式 + + /// + /// 创建排序表达式 + /// + public static Expression> CreateOrderBy(Expression> keySelector) + { + return keySelector; + } + + /// + /// 应用排序 + /// + public static IOrderedQueryable ApplyOrder(IQueryable source, string propertyName, bool ascending = true) + { + var parameter = Expression.Parameter(typeof(T), "x"); + var property = Expression.Property(parameter, propertyName); + var lambda = Expression.Lambda(property, parameter); + + var methodName = ascending ? "OrderBy" : "OrderByDescending"; + var method = typeof(Queryable).GetMethods() + .Where(m => m.Name == methodName && m.GetParameters().Length == 2) + .Single(); + + var genericMethod = method.MakeGenericMethod(typeof(T), property.Type); + return (IOrderedQueryable)genericMethod.Invoke(null, new object[] { source, lambda })!; + } + + /// + /// 应用后续排序 + /// + public static IOrderedQueryable ApplyThenBy(IOrderedQueryable source, string propertyName, bool ascending = true) + { + var parameter = Expression.Parameter(typeof(T), "x"); + var property = Expression.Property(parameter, propertyName); + var lambda = Expression.Lambda(property, parameter); + + var methodName = ascending ? "ThenBy" : "ThenByDescending"; + var method = typeof(Queryable).GetMethods() + .Where(m => m.Name == methodName && m.GetParameters().Length == 2) + .Single(); + + var genericMethod = method.MakeGenericMethod(typeof(T), property.Type); + return (IOrderedQueryable)genericMethod.Invoke(null, new object[] { source, lambda })!; + } + + #endregion + + #region 构造表达式 + + /// + /// 创建等于条件 + /// + public static Expression> CreateEqual(Expression> propertyExpression, TValue value) + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.Equal(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建大于条件 + /// + public static Expression> CreateGreaterThan(Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.GreaterThan(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建小于条件 + /// + public static Expression> CreateLessThan(Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.LessThan(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建包含条件 + /// + public static Expression> CreateContains(Expression> propertyExpression, string value) + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value); + var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) })!; + var body = Expression.Call(property, containsMethod, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建范围条件 + /// + public static Expression> CreateInRange( + Expression> propertyExpression, + TValue min, TValue max) where TValue : IComparable + { + var greaterThanOrEqual = CreateGreaterThanOrEqual(propertyExpression, min); + var lessThanOrEqual = CreateLessThanOrEqual(propertyExpression, max); + return And(greaterThanOrEqual, lessThanOrEqual); + } + + /// + /// 创建大于等于条件 + /// + public static Expression> CreateGreaterThanOrEqual( + Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.GreaterThanOrEqual(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建小于等于条件 + /// + public static Expression> CreateLessThanOrEqual( + Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.LessThanOrEqual(property, constant); + return Expression.Lambda>(body, parameter); + } + + #endregion + + #region 编译执行 + + /// + /// 编译并执行表达式 + /// + public static TResult Execute(Expression> expression, T instance) + { + var func = expression.Compile(); + return func(instance); + } + + /// + /// 编译表达式 + /// + public static Func Compile(Expression> expression) + { + return expression.Compile(); + } + + #endregion + } + + /// + /// 参数替换器 + /// + internal class ParameterReplacer : ExpressionVisitor + { + private readonly ParameterExpression _oldParameter; + private readonly ParameterExpression _newParameter; + + public ParameterReplacer(ParameterExpression oldParameter, ParameterExpression newParameter) + { + _oldParameter = oldParameter; + _newParameter = newParameter; + } + + protected override Expression VisitParameter(ParameterExpression node) + { + return node == _oldParameter ? _newParameter : node; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ReflectCategory/TypeUtil.cs b/EasyTool.Core/ReflectCategory/TypeUtil.cs new file mode 100644 index 0000000..1cc3193 --- /dev/null +++ b/EasyTool.Core/ReflectCategory/TypeUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// 类型工具类 + /// + public static class TypeUtil + { + #region 类型判断 + + /// + /// 判断是否为简单类型 + /// + public static bool IsSimpleType(Type type) + { + if (type == null) return false; + + return type.IsPrimitive || + type.IsEnum || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Guid) || + type == typeof(byte[]) || + Nullable.GetUnderlyingType(type) != null && IsSimpleType(Nullable.GetUnderlyingType(type)!); + } + + /// + /// 判断是否为可空类型 + /// + public static bool IsNullableType(Type type) + { + return type != null && Nullable.GetUnderlyingType(type) != null; + } + + /// + /// 判断是否为集合类型 + /// + public static bool IsCollectionType(Type type) + { + if (type == null) return false; + return type != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(type); + } + + /// + /// 判断是否为字典类型 + /// + public static bool IsDictionaryType(Type type) + { + if (type == null) return false; + + return type.GetInterfaces() + .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)); + } + + /// + /// 判断是否为元组类型 + /// + public static bool IsTupleType(Type type) + { + if (type == null) return false; + + if (!type.IsGenericType) + return false; + + var definition = type.GetGenericTypeDefinition(); + return definition == typeof(Tuple<>) || + definition == typeof(Tuple<,>) || + definition == typeof(Tuple<,,>) || + definition == typeof(Tuple<,,,>) || + definition == typeof(Tuple<,,,,>) || + definition == typeof(Tuple<,,,,,>) || + definition == typeof(Tuple<,,,,,,>) || + definition == typeof(Tuple<,,,,,,,>) || + definition == typeof(ValueTuple<>) || + definition == typeof(ValueTuple<,>) || + definition == typeof(ValueTuple<,,>) || + definition == typeof(ValueTuple<,,,>) || + definition == typeof(ValueTuple<,,,,>) || + definition == typeof(ValueTuple<,,,,,>) || + definition == typeof(ValueTuple<,,,,,,>) || + definition == typeof(ValueTuple<,,,,,,,>); + } + + /// + /// 获取可空类型的基类型 + /// + public static Type? GetUnderlyingType(Type type) + { + return Nullable.GetUnderlyingType(type); + } + + /// + /// 获取集合元素类型 + /// + public static Type? GetElementType(Type type) + { + if (type == null) return null; + + if (type.IsArray) + return type.GetElementType(); + + if (type.IsGenericType) + { + var genericArgs = type.GetGenericArguments(); + if (genericArgs.Length > 0) + { + // 对于 IEnumerable、List 等 + if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type)) + { + return genericArgs[0]; + } + } + } + + return null; + } + + #endregion + + #region 类型创建 + + /// + /// 创建实例 + /// + public static object? CreateInstance(Type type, params object[] args) + { + if (type == null) return null; + + if (args == null || args.Length == 0) + { + return Activator.CreateInstance(type); + } + + return Activator.CreateInstance(type, args); + } + + /// + /// 创建泛型实例 + /// + public static object? CreateGenericInstance(Type genericType, Type[] typeArguments, params object[] args) + { + if (genericType == null || typeArguments == null) return null; + + if (!genericType.IsGenericTypeDefinition) + throw new ArgumentException("类型必须是泛型定义"); + + var constructedType = genericType.MakeGenericType(typeArguments); + return CreateInstance(constructedType, args); + } + + #endregion + + #region 属性/字段访问 + + /// + /// 获取所有属性 + /// + public static PropertyInfo[] GetProperties(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetProperties(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取属性 + /// + public static PropertyInfo? GetProperty(Type type, string propertyName, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetProperty(propertyName, bindingFlags); + } + + /// + /// 获取属性值 + /// + public static object? GetPropertyValue(object obj, string propertyName) + { + if (obj == null) return null; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + return property?.GetValue(obj); + } + + /// + /// 设置属性值 + /// + public static void SetPropertyValue(object obj, string propertyName, object? value) + { + if (obj == null) return; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + property?.SetValue(obj, value); + } + + /// + /// 获取所有字段 + /// + public static FieldInfo[] GetFields(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetFields(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取字段值 + /// + public static object? GetFieldValue(object obj, string fieldName) + { + if (obj == null) return null; + + var type = obj.GetType(); + var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + return field?.GetValue(obj); + } + + /// + /// 设置字段值 + /// + public static void SetFieldValue(object obj, string fieldName, object? value) + { + if (obj == null) return; + + var type = obj.GetType(); + var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + field?.SetValue(obj, value); + } + + #endregion + + #region 方法调用 + + /// + /// 获取所有方法 + /// + public static MethodInfo[] GetMethods(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetMethods(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取方法 + /// + public static MethodInfo? GetMethod(Type type, string methodName, Type[]? parameterTypes = null, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + if (type == null) return null; + + if (parameterTypes == null) + return type.GetMethod(methodName, bindingFlags); + + return type.GetMethod(methodName, bindingFlags, null, parameterTypes, null); + } + + /// + /// 调用方法 + /// + public static object? InvokeMethod(object obj, string methodName, params object[] args) + { + if (obj == null) return null; + + var type = obj.GetType(); + var argTypes = args?.Select(a => a?.GetType() ?? typeof(object)).ToArray(); + var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, argTypes, null); + + return method?.Invoke(obj, args); + } + + /// + /// 调用静态方法 + /// + public static object? InvokeStaticMethod(Type type, string methodName, params object[] args) + { + if (type == null) return null; + + var argTypes = args?.Select(a => a?.GetType() ?? typeof(object)).ToArray(); + var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static, null, argTypes, null); + + return method?.Invoke(null, args); + } + + #endregion + + #region 类型继承 + + /// + /// 判断类型是否继承自指定类型 + /// + public static bool IsAssignableTo(Type type, Type targetType) + { + return targetType?.IsAssignableFrom(type) ?? false; + } + + /// + /// 获取基类型 + /// + public static Type? GetBaseType(Type type) + { + return type?.BaseType; + } + + /// + /// 获取所有接口 + /// + public static Type[] GetInterfaces(Type type) + { + return type?.GetInterfaces() ?? Array.Empty(); + } + + /// + /// 获取继承层次 + /// + public static IEnumerable GetInheritanceHierarchy(Type type) + { + if (type == null) yield break; + + var current = type; + while (current != null && current != typeof(object)) + { + yield return current; + current = current.BaseType; + } + + if (type != typeof(object)) + yield return typeof(object); + } + + #endregion + + #region 特性 + + /// + /// 获取特性 + /// + public static T? GetAttribute(MemberInfo member) where T : Attribute + { + return member?.GetCustomAttribute(); + } + + /// + /// 获取所有特性 + /// + public static IEnumerable GetAttributes(MemberInfo member) where T : Attribute + { + return member?.GetCustomAttributes() ?? Enumerable.Empty(); + } + + /// + /// 检查是否有特性 + /// + public static bool HasAttribute(MemberInfo member) where T : Attribute + { + return member?.IsDefined(typeof(T), true) ?? false; + } + + #endregion + + #region 类型信息 + + /// + /// 获取类型友好名称 + /// + public static string GetFriendlyName(Type type) + { + if (type == null) return string.Empty; + + if (!type.IsGenericType) + return type.Name; + + var name = type.Name; + var backtickIndex = name.IndexOf('`'); + if (backtickIndex >= 0) + name = name.Substring(0, backtickIndex); + + var genericArgs = type.GetGenericArguments(); + var argNames = string.Join(", ", genericArgs.Select(GetFriendlyName)); + + return $"{name}<{argNames}>"; + } + + /// + /// 获取默认值 + /// + public static object? GetDefaultValue(Type type) + { + if (type == null) return null; + + if (type.IsValueType) + return Activator.CreateInstance(type); + + return null; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/SecurityCategory/CertificateUtil.cs b/EasyTool.Core/SecurityCategory/CertificateUtil.cs new file mode 100644 index 0000000..90ecc81 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/CertificateUtil.cs @@ -0,0 +1,535 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 证书工具类 + /// 提供证书生成、加载和验证功能 + /// + public static class CertificateUtil + { + /// + /// 生成自签名证书 + /// + /// 主题名称 + /// 有效期(年) + /// 密钥大小 + /// X509 证书 + public static X509Certificate2 GenerateSelfSignedCertificate( + string subjectName, + int validYears = 1, + int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + var request = new CertificateRequest( + $"CN={subjectName}", + rsa, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + + // 添加基本约束 + request.CertificateExtensions.Add( + new X509BasicConstraintsExtension(true, true, 0, false)); + + // 添加密钥用法 + request.CertificateExtensions.Add( + new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | + X509KeyUsageFlags.KeyEncipherment | + X509KeyUsageFlags.CrlSign | + X509KeyUsageFlags.KeyCertSign, + false)); + + // 添加增强密钥用法 + request.CertificateExtensions.Add( + new X509EnhancedKeyUsageExtension( + new OidCollection + { + new Oid("1.3.6.1.5.5.7.3.1"), // Server Authentication + new Oid("1.3.6.1.5.5.7.3.2") // Client Authentication + }, + false)); + + // 添加主题备用名称 + var sanBuilder = new SubjectAlternativeNameBuilder(); + sanBuilder.AddDnsName(subjectName); + sanBuilder.AddDnsName("localhost"); + request.CertificateExtensions.Add(sanBuilder.Build()); + + // 创建证书 + var notBefore = DateTimeOffset.UtcNow.AddDays(-1); + var notAfter = DateTimeOffset.UtcNow.AddYears(validYears); + + var certificate = request.CreateSelfSigned(notBefore, notAfter); + + return new X509Certificate2( + certificate.Export(X509ContentType.Pfx), + (string?)null, + X509KeyStorageFlags.MachineKeySet | + X509KeyStorageFlags.PersistKeySet | + X509KeyStorageFlags.Exportable); + } + + /// + /// 生成客户端证书 + /// + /// 主题名称 + /// 颁发者证书 + /// 有效期(天) + /// 客户端证书 + public static X509Certificate2 GenerateClientCertificate( + string subjectName, + X509Certificate2 issuerCertificate, + int validDays = 365) + { + using var rsa = RSA.Create(2048); + var request = new CertificateRequest( + $"CN={subjectName}", + rsa, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + + // 添加基本约束 + request.CertificateExtensions.Add( + new X509BasicConstraintsExtension(false, false, 0, false)); + + // 添加密钥用法 + request.CertificateExtensions.Add( + new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | + X509KeyUsageFlags.KeyEncipherment, + false)); + + // 添加增强密钥用法 + request.CertificateExtensions.Add( + new X509EnhancedKeyUsageExtension( + new OidCollection + { + new Oid("1.3.6.1.5.5.7.3.2") // Client Authentication + }, + false)); + + // 使用颁发者证书签名 + var notBefore = DateTimeOffset.UtcNow.AddDays(-1); + var notAfter = DateTimeOffset.UtcNow.AddDays(validDays); + + var serialNumber = new byte[16]; + RandomNumberGenerator.Fill(serialNumber); + + var certificate = request.Create( + issuerCertificate, + notBefore, + notAfter, + serialNumber); + + return certificate.CopyWithPrivateKey(rsa); + } + + /// + /// 从文件加载证书 + /// + /// 文件路径 + /// 密码 + /// 证书 + public static X509Certificate2 LoadFromFile(string filePath, string? password = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"证书文件不存在: {filePath}"); + + var bytes = File.ReadAllBytes(filePath); + return new X509Certificate2(bytes, password); + } + + /// + /// 从 PFX 文件加载证书 + /// + /// 文件路径 + /// 密码 + /// 证书 + public static X509Certificate2 LoadPfx(string filePath, string? password = null) + { + return LoadFromFile(filePath, password); + } + + /// + /// 从 PEM 文件加载证书 + /// + /// 证书文件路径 + /// 私钥文件路径(可选) + /// 证书 + public static X509Certificate2 LoadPem(string certPath, string? keyPath = null) + { + var certPem = File.ReadAllText(certPath); +#if NET5_0_OR_GREATER + var cert = X509Certificate2.CreateFromPem(certPem); + + if (!string.IsNullOrEmpty(keyPath) && File.Exists(keyPath)) + { + var keyPem = File.ReadAllText(keyPath); + using var rsa = RSA.Create(); + rsa.ImportFromPem(keyPem); + return cert.CopyWithPrivateKey(rsa); + } + + return cert; +#else + // netstandard2.1 不支持 PEM 格式,使用 Pfx 格式 + throw new PlatformNotSupportedException("PEM 格式证书需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 保存证书到文件 + /// + /// 证书 + /// 文件路径 + /// 密码 + /// 格式 + public static void SaveToFile( + X509Certificate2 certificate, + string filePath, + string? password = null, + CertificateFormat format = CertificateFormat.Pfx) + { + byte[] data; + + switch (format) + { + case CertificateFormat.Pfx: + data = certificate.Export(X509ContentType.Pfx, password); + break; + case CertificateFormat.Pem: +#if NET5_0_OR_GREATER + var pem = certificate.ExportCertificatePem(); + data = Encoding.UTF8.GetBytes(pem); +#else + // netstandard2.1 不支持 PEM 导出 + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + break; + case CertificateFormat.Cer: + data = certificate.Export(X509ContentType.Cert); + break; + default: + throw new ArgumentException($"不支持的证书格式: {format}"); + } + + File.WriteAllBytes(filePath, data); + } + + /// + /// 导出证书为 PEM 格式 + /// + /// 证书 + /// PEM 字符串 + public static string ExportToPem(X509Certificate2 certificate) + { +#if NET5_0_OR_GREATER + return certificate.ExportCertificatePem(); +#else + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 导出私钥为 PEM 格式 + /// + /// 证书 + /// PEM 字符串 + public static string ExportPrivateKeyToPem(X509Certificate2 certificate) + { + var rsa = certificate.GetRSAPrivateKey(); + if (rsa == null) + throw new InvalidOperationException("证书不包含私钥"); + +#if NET5_0_OR_GREATER + return rsa.ExportRSAPrivateKeyPem(); +#else + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 验证证书 + /// + /// 证书 + /// 证书链(可选) + /// 验证结果 + public static CertificateValidationResult Validate( + X509Certificate2 certificate, + X509Certificate2Collection? chain = null) + { + var result = new CertificateValidationResult + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint + }; + + // 检查有效期 + var now = DateTime.UtcNow; + + if (now < certificate.NotBefore) + { + result.IsValid = false; + result.Errors.Add($"证书尚未生效(生效时间: {certificate.NotBefore})"); + } + + if (now > certificate.NotAfter) + { + result.IsValid = false; + result.Errors.Add($"证书已过期(过期时间: {certificate.NotAfter})"); + } + + // 验证证书链 + using var chainBuilder = new X509Chain(); + chainBuilder.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chainBuilder.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; + + if (chain != null) + { + foreach (var cert in chain) + { + chainBuilder.ChainPolicy.ExtraStore.Add(cert); + } + } + + var chainValid = chainBuilder.Build(certificate); + + if (!chainValid) + { + foreach (var status in chainBuilder.ChainStatus) + { + result.Warnings.Add($"证书链状态: {status.StatusInformation}"); + } + } + + result.HasPrivateKey = certificate.HasPrivateKey; + result.IsValid ??= true; + + return result; + } + + /// + /// 验证证书链 + /// + /// 证书 + /// 颁发者证书 + /// 是否有效 + public static bool ValidateChain(X509Certificate2 certificate, X509Certificate2 issuerCertificate) + { + using var chain = new X509Chain(); + chain.ChainPolicy.ExtraStore.Add(issuerCertificate); + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; + + return chain.Build(certificate); + } + + /// + /// 从证书存储区获取证书 + /// + /// 存储区名称 + /// 存储区位置 + /// 证书指纹 + /// 证书 + public static X509Certificate2? GetFromStore( + StoreName storeName, + StoreLocation storeLocation, + string thumbprint) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadOnly); + + var certificates = store.Certificates.Find( + X509FindType.FindByThumbprint, + thumbprint, + false); + + return certificates.Count > 0 ? certificates[0] : null; + } + + /// + /// 将证书添加到存储区 + /// + /// 证书 + /// 存储区名称 + /// 存储区位置 + public static void AddToStore( + X509Certificate2 certificate, + StoreName storeName, + StoreLocation storeLocation) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadWrite); + store.Add(certificate); + } + + /// + /// 从存储区移除证书 + /// + /// 证书 + /// 存储区名称 + /// 存储区位置 + public static void RemoveFromStore( + X509Certificate2 certificate, + StoreName storeName, + StoreLocation storeLocation) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadWrite); + store.Remove(certificate); + } + + /// + /// 获取证书信息 + /// + /// 证书 + /// 证书信息 + public static CertificateInfo GetCertificateInfo(X509Certificate2 certificate) + { + return new CertificateInfo + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint, + SerialNumber = certificate.SerialNumber, + HasPrivateKey = certificate.HasPrivateKey, + KeySize = certificate.GetRSAPublicKey()?.KeySize ?? 0, + SignatureAlgorithm = certificate.SignatureAlgorithm.FriendlyName ?? "Unknown" + }; + } + } + + /// + /// 证书格式 + /// + public enum CertificateFormat + { + /// + /// PFX/P12 格式 + /// + Pfx, + + /// + /// PEM 格式 + /// + Pem, + + /// + /// CER/DER 格式 + /// + Cer + } + + /// + /// 证书验证结果 + /// + public class CertificateValidationResult + { + /// + /// 是否有效 + /// + public bool? IsValid { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 颁发者 + /// + public string Issuer { get; set; } = string.Empty; + + /// + /// 生效时间 + /// + public DateTime NotBefore { get; set; } + + /// + /// 过期时间 + /// + public DateTime NotAfter { get; set; } + + /// + /// 指纹 + /// + public string Thumbprint { get; set; } = string.Empty; + + /// + /// 是否有私钥 + /// + public bool HasPrivateKey { get; set; } + + /// + /// 错误信息 + /// + public List Errors { get; set; } = new(); + + /// + /// 警告信息 + /// + public List Warnings { get; set; } = new(); + } + + /// + /// 证书信息 + /// + public class CertificateInfo + { + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 颁发者 + /// + public string Issuer { get; set; } = string.Empty; + + /// + /// 生效时间 + /// + public DateTime NotBefore { get; set; } + + /// + /// 过期时间 + /// + public DateTime NotAfter { get; set; } + + /// + /// 指纹 + /// + public string Thumbprint { get; set; } = string.Empty; + + /// + /// 序列号 + /// + public string SerialNumber { get; set; } = string.Empty; + + /// + /// 是否有私钥 + /// + public bool HasPrivateKey { get; set; } + + /// + /// 密钥大小 + /// + public int KeySize { get; set; } + + /// + /// 签名算法 + /// + public string SignatureAlgorithm { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/SecurityCategory/CsrfUtil.cs b/EasyTool.Core/SecurityCategory/CsrfUtil.cs new file mode 100644 index 0000000..d0e0d75 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/CsrfUtil.cs @@ -0,0 +1,342 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// CSRF(跨站请求伪造)防护工具类 + /// + public static class CsrfUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly int _defaultTokenLength = 32; + + /// + /// 生成 CSRF Token + /// + /// Token 长度(字节数) + /// Base64 编码的 Token + public static string GenerateToken(int length = 0) + { + var tokenLength = length > 0 ? length : _defaultTokenLength; + var bytes = new byte[tokenLength]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 URL 安全的 CSRF Token + /// + /// Token 长度(字节数) + /// URL 安全的 Base64 编码 Token + public static string GenerateUrlSafeToken(int length = 0) + { + var token = GenerateToken(length); + return token.Replace("+", "-").Replace("/", "_").TrimEnd('='); + } + + /// + /// 生成带签名的 CSRF Token + /// + /// 签名密钥 + /// 要签名的数据(如用户ID、会话ID等) + /// 过期时间(分钟) + /// 签名的 Token + public static string GenerateSignedToken(string secret, string data, int expirationMinutes = 0) + { + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var expiration = expirationMinutes > 0 ? timestamp + (expirationMinutes * 60) : 0; + + var payload = expiration > 0 + ? $"{data}|{timestamp}|{expiration}" + : $"{data}|{timestamp}"; + + var signature = ComputeHmacSha256(secret, payload); + var token = $"{payload}|{signature}"; + + return Convert.ToBase64String(Encoding.UTF8.GetBytes(token)); + } + + /// + /// 验证签名的 CSRF Token + /// + /// 签名密钥 + /// 要验证的 Token + /// 期望的数据 + /// 验证结果 + public static CsrfValidationResult ValidateSignedToken(string secret, string token, string data) + { + try + { + var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(token)); + var parts = decoded.Split('|'); + + if (parts.Length != 4) + { + return CsrfValidationResult.Fail("Token 格式无效"); + } + + var tokenData = parts[0]; + var timestampStr = parts[1]; + var expirationStr = parts[2]; + var signature = parts[3]; + + // 验证数据 + if (tokenData != data) + { + return CsrfValidationResult.Fail("Token 数据不匹配"); + } + + // 验证签名 + var payload = $"{tokenData}|{timestampStr}|{expirationStr}"; + var expectedSignature = ComputeHmacSha256(secret, payload); + + if (!ConstantTimeEquals(signature, expectedSignature)) + { + return CsrfValidationResult.Fail("Token 签名无效"); + } + + // 验证过期时间 + if (long.TryParse(expirationStr, out var expiration) && expiration > 0) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (now > expiration) + { + return CsrfValidationResult.Fail("Token 已过期"); + } + } + + // 解析创建时间 + var createdAt = long.TryParse(timestampStr, out var ts) + ? DateTimeOffset.FromUnixTimeSeconds(ts) + : (DateTimeOffset?)null; + + return CsrfValidationResult.Success(createdAt); + } + catch (Exception ex) + { + return CsrfValidationResult.Fail($"Token 验证失败: {ex.Message}"); + } + } + + /// + /// 生成双提交 Cookie 模式的 Token + /// + /// Cookie 中的 Token + /// 请求中应携带的 Token + public static string GenerateDoubleSubmitToken(string cookieToken) + { + var randomPart = GenerateToken(16); + return $"{cookieToken}:{randomPart}"; + } + + /// + /// 验证双提交 Cookie 模式 + /// + /// Cookie 中的 Token + /// 请求中携带的 Token + /// 验证结果 + public static bool ValidateDoubleSubmitToken(string cookieToken, string requestToken) + { + if (string.IsNullOrEmpty(cookieToken) || string.IsNullOrEmpty(requestToken)) + { + return false; + } + + var parts = requestToken.Split(':'); + if (parts.Length != 2) + { + return false; + } + + return ConstantTimeEquals(cookieToken, parts[0]); + } + + /// + /// 生成同步器 Token 模式的 Token + /// + /// 会话 Token + /// 表单 ID + /// 同步器 Token + public static string GenerateSynchronizerToken(string sessionToken, string formId) + { + var combined = $"{sessionToken}|{formId}"; + using var sha256 = SHA256.Create(); + var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(combined)); + return Convert.ToBase64String(hash); + } + + /// + /// 验证同步器 Token + /// + /// 会话 Token + /// 表单 ID + /// 要验证的 Token + /// 验证结果 + public static bool ValidateSynchronizerToken(string sessionToken, string formId, string token) + { + if (string.IsNullOrEmpty(sessionToken) || string.IsNullOrEmpty(token)) + { + return false; + } + + var expected = GenerateSynchronizerToken(sessionToken, formId); + return ConstantTimeEquals(expected, token); + } + + /// + /// 生成基于会话的 CSRF Token + /// + /// 会话 ID + /// 应用密钥 + /// 额外数据 + /// CSRF Token + public static string GenerateSessionToken(string sessionId, string secret, params string[] additionalData) + { + var data = additionalData.Length > 0 + ? $"{sessionId}|{string.Join("|", additionalData)}" + : sessionId; + + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var payload = $"{data}|{timestamp}"; + var signature = ComputeHmacSha256(secret, payload); + + return $"{payload}|{signature}"; + } + + /// + /// 验证基于会话的 CSRF Token + /// + /// 会话 ID + /// 应用密钥 + /// 要验证的 Token + /// 最大有效期(秒) + /// 验证结果 + public static CsrfValidationResult ValidateSessionToken(string sessionId, string secret, string token, long maxAgeSeconds = 0) + { + try + { + var parts = token.Split('|'); + if (parts.Length < 3) + { + return CsrfValidationResult.Fail("Token 格式无效"); + } + + // 提取签名和数据 + var signature = parts[^1]; + var payloadParts = new ArraySegment(parts, 0, parts.Length - 1).ToArray(); + var payload = string.Join("|", payloadParts); + + // 验证签名 + var expectedSignature = ComputeHmacSha256(secret, payload); + if (!ConstantTimeEquals(signature, expectedSignature)) + { + return CsrfValidationResult.Fail("Token 签名无效"); + } + + // 提取时间戳(最后一个非签名部分) + var timestampStr = payloadParts[^1]; + if (!long.TryParse(timestampStr, out var timestamp)) + { + return CsrfValidationResult.Fail("Token 时间戳无效"); + } + + // 检查过期时间 + if (maxAgeSeconds > 0) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (now - timestamp > maxAgeSeconds) + { + return CsrfValidationResult.Fail("Token 已过期"); + } + } + + // 提取会话 ID(第一个部分) + var tokenSessionId = payloadParts[0]; + if (tokenSessionId != sessionId) + { + return CsrfValidationResult.Fail("Token 会话不匹配"); + } + + var createdAt = DateTimeOffset.FromUnixTimeSeconds(timestamp); + return CsrfValidationResult.Success(createdAt); + } + catch (Exception ex) + { + return CsrfValidationResult.Fail($"Token 验证失败: {ex.Message}"); + } + } + + private static string ComputeHmacSha256(string key, string data) + { + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); + var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(data)); + return Convert.ToBase64String(hash); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a == null || b == null) + { + return a == b; + } + + if (a.Length != b.Length) + { + return false; + } + + var result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + } + + /// + /// CSRF 验证结果 + /// + public class CsrfValidationResult + { + /// + /// 是否验证通过 + /// + public bool IsValid { get; } + + /// + /// 错误消息 + /// + public string? ErrorMessage { get; } + + /// + /// Token 创建时间 + /// + public DateTimeOffset? CreatedAt { get; } + + private CsrfValidationResult(bool isValid, string? errorMessage, DateTimeOffset? createdAt) + { + IsValid = isValid; + ErrorMessage = errorMessage; + CreatedAt = createdAt; + } + + /// + /// 验证成功 + /// + public static CsrfValidationResult Success(DateTimeOffset? createdAt = null) + { + return new CsrfValidationResult(true, null, createdAt); + } + + /// + /// 验证失败 + /// + public static CsrfValidationResult Fail(string errorMessage) + { + return new CsrfValidationResult(false, errorMessage, null); + } + } +} diff --git a/EasyTool.Core/SecurityCategory/InputSanitizer.cs b/EasyTool.Core/SecurityCategory/InputSanitizer.cs new file mode 100644 index 0000000..da37068 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/InputSanitizer.cs @@ -0,0 +1,403 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// 输入净化器 + /// + public static class InputSanitizer + { + /// + /// 净化选项 + /// + public class SanitizeOptions + { + /// + /// 是否移除HTML标签 + /// + public bool RemoveHtmlTags { get; set; } = true; + + /// + /// 是否移除脚本 + /// + public bool RemoveScripts { get; set; } = true; + + /// + /// 是否转义HTML特殊字符 + /// + public bool EscapeHtml { get; set; } = true; + + /// + /// 是否移除SQL注入 + /// + public bool RemoveSqlInjection { get; set; } = true; + + /// + /// 是否移除路径遍历 + /// + public bool RemovePathTraversal { get; set; } = true; + + /// + /// 是否移除控制字符 + /// + public bool RemoveControlChars { get; set; } = true; + + /// + /// 是否标准化空白 + /// + public bool NormalizeWhitespace { get; set; } = false; + + /// + /// 最大长度(0表示不限制) + /// + public int MaxLength { get; set; } = 0; + + /// + /// 允许的字符正则(为空表示不限制) + /// + public string? AllowedCharsPattern { get; set; } + + /// + /// 获取默认选项 + /// + public static SanitizeOptions Default => new(); + + /// + /// 获取严格选项 + /// + public static SanitizeOptions Strict => new() + { + RemoveHtmlTags = true, + RemoveScripts = true, + EscapeHtml = true, + RemoveSqlInjection = true, + RemovePathTraversal = true, + RemoveControlChars = true, + NormalizeWhitespace = true + }; + + /// + /// 获取宽松选项(仅移除脚本) + /// + public static SanitizeOptions Lenient => new() + { + RemoveHtmlTags = false, + RemoveScripts = true, + EscapeHtml = false, + RemoveSqlInjection = false, + RemovePathTraversal = true, + RemoveControlChars = true + }; + } + + private static readonly Regex HtmlTagPattern = new(@"<[^>]*>", RegexOptions.Compiled); + private static readonly Regex ScriptPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); + private static readonly Regex ControlCharPattern = new(@"[\x00-\x08\x0B\x0C\x0E-\x1F]", RegexOptions.Compiled); + private static readonly Regex PathTraversalPattern = new(@"(\.\.[\\/])|([\\/]\.\.)", RegexOptions.Compiled); + private static readonly Regex MultiWhitespacePattern = new(@"\s+", RegexOptions.Compiled); + + /// + /// 净化输入字符串 + /// + public static string Sanitize(string? input, SanitizeOptions? options = null) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + options ??= SanitizeOptions.Default; + var result = input; + + // 移除控制字符 + if (options.RemoveControlChars) + { + result = ControlCharPattern.Replace(result, ""); + } + + // 移除脚本 + if (options.RemoveScripts) + { + result = ScriptPattern.Replace(result, ""); + } + + // 移除HTML标签 + if (options.RemoveHtmlTags) + { + result = HtmlTagPattern.Replace(result, ""); + } + + // 转义HTML + if (options.EscapeHtml) + { + result = EscapeHtml(result); + } + + // 移除SQL注入 + if (options.RemoveSqlInjection) + { + result = SqlInjectionUtil.Sanitize(result); + } + + // 移除路径遍历 + if (options.RemovePathTraversal) + { + result = PathTraversalPattern.Replace(result, ""); + } + + // 标准化空白 + if (options.NormalizeWhitespace) + { + result = MultiWhitespacePattern.Replace(result, " ").Trim(); + } + + // 过滤允许的字符 + if (!string.IsNullOrEmpty(options.AllowedCharsPattern)) + { + result = Regex.Replace(result, options.AllowedCharsPattern, ""); + } + + // 限制长度 + if (options.MaxLength > 0 && result.Length > options.MaxLength) + { + result = result.Substring(0, options.MaxLength); + } + + return result; + } + + /// + /// 净化文件名 + /// + public static string SanitizeFileName(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + return string.Empty; + + var invalidChars = new string(System.IO.Path.GetInvalidFileNameChars()); + var pattern = $"[{Regex.Escape(invalidChars)}]"; + var result = Regex.Replace(fileName, pattern, "_"); + + // 移除路径遍历 + result = PathTraversalPattern.Replace(result, ""); + + // 移除前导/尾随空格和点 + result = result.Trim().Trim('.'); + + return result; + } + + /// + /// 净化路径 + /// + public static string SanitizePath(string path) + { + if (string.IsNullOrEmpty(path)) + return string.Empty; + + var invalidChars = new string(System.IO.Path.GetInvalidPathChars()); + var pattern = $"[{Regex.Escape(invalidChars)}]"; + var result = Regex.Replace(path, pattern, "_"); + + // 移除路径遍历 + result = PathTraversalPattern.Replace(result, ""); + + return result; + } + + /// + /// 净化URL + /// + public static string SanitizeUrl(string url) + { + if (string.IsNullOrEmpty(url)) + return string.Empty; + + // 只允许http/https协议 + var lower = url.ToLower().Trim(); + if (!lower.StartsWith("http://") && !lower.StartsWith("https://")) + { + return string.Empty; + } + + // 移除危险字符 + var result = url.Replace("<", "").Replace(">", "").Replace("\"", ""); + result = PathTraversalPattern.Replace(result, ""); + + return result; + } + + /// + /// 净化邮箱 + /// + public static string SanitizeEmail(string email) + { + if (string.IsNullOrEmpty(email)) + return string.Empty; + + var result = email.ToLowerInvariant().Trim(); + + // 移除控制字符 + result = ControlCharPattern.Replace(result, ""); + + // 基本验证 + if (!Regex.IsMatch(result, @"^[^@\s]+@[^@\s]+\.[^@\s]+$")) + return string.Empty; + + return result; + } + + /// + /// 净化电话号码 + /// + public static string SanitizePhone(string phone) + { + if (string.IsNullOrEmpty(phone)) + return string.Empty; + + // 只保留数字和+ + var result = Regex.Replace(phone, @"[^\d+]", ""); + + // 验证格式 + if (!Regex.IsMatch(result, @"^\+?\d{6,15}$")) + return string.Empty; + + return result; + } + + /// + /// 净化数字字符串 + /// + public static string SanitizeNumber(string input, bool allowDecimal = false, bool allowNegative = false) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + var pattern = allowDecimal + ? (allowNegative ? @"[^0-9.\-]" : @"[^0-9.]") + : (allowNegative ? @"[^0-9\-]" : @"[^0-9]"); + + var result = Regex.Replace(input, pattern, ""); + + // 验证格式 + if (allowDecimal) + { + if (!decimal.TryParse(result, out _)) + return string.Empty; + } + else + { + if (!long.TryParse(result, out _)) + return string.Empty; + } + + return result; + } + + /// + /// 净化JSON字符串 + /// + public static string SanitizeJson(string json) + { + if (string.IsNullOrEmpty(json)) + return string.Empty; + + var result = new StringBuilder(json.Length); + foreach (var c in json) + { + switch (c) + { + case '"': + result.Append("\\\""); + break; + case '\\': + result.Append("\\\\"); + break; + case '\b': + result.Append("\\b"); + break; + case '\f': + result.Append("\\f"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\t': + result.Append("\\t"); + break; + default: + if (c < 32) + { + result.Append($"\\u{(int)c:X4}"); + } + else + { + result.Append(c); + } + break; + } + } + return result.ToString(); + } + + /// + /// 净化XML字符串 + /// + public static string SanitizeXml(string xml) + { + if (string.IsNullOrEmpty(xml)) + return string.Empty; + + var result = new StringBuilder(xml.Length); + foreach (var c in xml) + { + result.Append(c switch + { + '<' => "<", + '>' => ">", + '&' => "&", + '"' => """, + '\'' => "'", + _ => c + }); + } + return result.ToString(); + } + + private static string EscapeHtml(string input) + { + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + result.Append(c switch + { + '<' => "<", + '>' => ">", + '&' => "&", + '"' => """, + '\'' => "'", + '/' => "/", + _ => c + }); + } + return result.ToString(); + } + + /// + /// 批量净化 + /// + public static Dictionary SanitizeMultiple(IDictionary inputs, SanitizeOptions? options = null) + { + var results = new Dictionary(); + foreach (var kvp in inputs) + { + results[kvp.Key] = Sanitize(kvp.Value, options); + } + return results; + } + } +} diff --git a/EasyTool.Core/SecurityCategory/JwtBuilder.cs b/EasyTool.Core/SecurityCategory/JwtBuilder.cs new file mode 100644 index 0000000..385f2e8 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/JwtBuilder.cs @@ -0,0 +1,616 @@ +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; +using Microsoft.IdentityModel.Tokens; + +namespace EasyTool.SecurityCategory +{ + /// + /// JWT 构建器 + /// 提供流畅的 JWT 生成接口 + /// + public class JwtBuilder + { + private readonly List _claims; + private string? _issuer; + private string? _audience; + private DateTime? _notBefore; + private DateTime? _expires; + private DateTime? _issuedAt; + private string? _subject; + private string? _id; + private SecurityKey? _signingKey; + private SecurityKey? _encryptingKey; + private string _algorithm = SecurityAlgorithms.HmacSha256; + private SigningCredentials? _signingCredentials; + private EncryptingCredentials? _encryptingCredentials; + + /// + /// 创建 JWT 构建器 + /// + public JwtBuilder() + { + _claims = new List(); + } + + /// + /// 设置颁发者 + /// + /// 颁发者 + /// JwtBuilder + public JwtBuilder WithIssuer(string issuer) + { + _issuer = issuer; + return this; + } + + /// + /// 设置受众 + /// + /// 受众 + /// JwtBuilder + public JwtBuilder WithAudience(string audience) + { + _audience = audience; + return this; + } + + /// + /// 设置主题 + /// + /// 主题 + /// JwtBuilder + public JwtBuilder WithSubject(string subject) + { + _subject = subject; + return this; + } + + /// + /// 设置 JWT ID + /// + /// ID + /// JwtBuilder + public JwtBuilder WithId(string id) + { + _id = id; + return this; + } + + /// + /// 设置生效时间 + /// + /// 生效时间 + /// JwtBuilder + public JwtBuilder WithNotBefore(DateTime notBefore) + { + _notBefore = notBefore; + return this; + } + + /// + /// 设置过期时间 + /// + /// 过期时间 + /// JwtBuilder + public JwtBuilder WithExpires(DateTime expires) + { + _expires = expires; + return this; + } + + /// + /// 设置过期时间(相对) + /// + /// 有效时长 + /// JwtBuilder + public JwtBuilder WithExpiresIn(TimeSpan duration) + { + _expires = DateTime.UtcNow.Add(duration); + return this; + } + + /// + /// 设置签发时间 + /// + /// 签发时间 + /// JwtBuilder + public JwtBuilder WithIssuedAt(DateTime issuedAt) + { + _issuedAt = issuedAt; + return this; + } + + /// + /// 添加声明 + /// + /// 类型 + /// 值 + /// JwtBuilder + public JwtBuilder WithClaim(string type, string value) + { + _claims.Add(new Claim(type, value)); + return this; + } + + /// + /// 添加声明 + /// + /// 声明 + /// JwtBuilder + public JwtBuilder WithClaim(Claim claim) + { + _claims.Add(claim); + return this; + } + + /// + /// 批量添加声明 + /// + /// 声明集合 + /// JwtBuilder + public JwtBuilder WithClaims(IEnumerable claims) + { + _claims.AddRange(claims); + return this; + } + + /// + /// 设置用户ID声明 + /// + /// 用户ID + /// JwtBuilder + public JwtBuilder WithUserId(string userId) + { + return WithClaim(JwtRegisteredClaimNames.Sub, userId); + } + + /// + /// 设置用户名声明 + /// + /// 用户名 + /// JwtBuilder + public JwtBuilder WithUsername(string username) + { + return WithClaim(ClaimTypes.Name, username); + } + + /// + /// 设置角色声明 + /// + /// 角色列表 + /// JwtBuilder + public JwtBuilder WithRoles(params string[] roles) + { + foreach (var role in roles) + { + _claims.Add(new Claim(ClaimTypes.Role, role)); + } + return this; + } + + /// + /// 设置邮箱声明 + /// + /// 邮箱 + /// JwtBuilder + public JwtBuilder WithEmail(string email) + { + return WithClaim(ClaimTypes.Email, email); + } + + /// + /// 设置签名密钥(字符串) + /// + /// 密钥字符串 + /// JwtBuilder + public JwtBuilder WithSecretKey(string secretKey) + { + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + return WithSigningKey(key); + } + + /// + /// 设置签名密钥 + /// + /// 密钥 + /// JwtBuilder + public JwtBuilder WithSigningKey(SecurityKey key) + { + _signingKey = key; + _signingCredentials = new SigningCredentials(key, _algorithm); + return this; + } + + /// + /// 设置签名密钥(RSA) + /// + /// RSA 密钥 + /// 算法 + /// JwtBuilder + public JwtBuilder WithRsaKey(RSA rsa, string algorithm = SecurityAlgorithms.RsaSha256) + { + var key = new RsaSecurityKey(rsa); + _algorithm = algorithm; + _signingCredentials = new SigningCredentials(key, algorithm); + return this; + } + + /// + /// 设置签名凭据 + /// + /// 签名凭据 + /// JwtBuilder + public JwtBuilder WithSigningCredentials(SigningCredentials credentials) + { + _signingCredentials = credentials; + return this; + } + + /// + /// 设置加密密钥 + /// + /// 加密密钥 + /// 加密算法 + /// JwtBuilder + public JwtBuilder WithEncryptingKey(SecurityKey key, string algorithm = SecurityAlgorithms.Aes256CbcHmacSha512) + { + _encryptingKey = key; + if (key is SymmetricSecurityKey symmetricKey) + { + _encryptingCredentials = new EncryptingCredentials(symmetricKey, algorithm); + } + else + { + throw new ArgumentException("加密密钥必须是 SymmetricSecurityKey 类型", nameof(key)); + } + return this; + } + + /// + /// 设置签名算法 + /// + /// 算法 + /// JwtBuilder + public JwtBuilder WithAlgorithm(string algorithm) + { + _algorithm = algorithm; + if (_signingKey != null) + { + _signingCredentials = new SigningCredentials(_signingKey, algorithm); + } + return this; + } + + /// + /// 构建 JWT Token + /// + /// JWT 字符串 + public string Build() + { + if (_signingCredentials == null) + throw new InvalidOperationException("必须设置签名密钥"); + + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(_claims), + Issuer = _issuer, + Audience = _audience, + NotBefore = _notBefore, + Expires = _expires, + IssuedAt = _issuedAt, + SigningCredentials = _signingCredentials, + EncryptingCredentials = _encryptingCredentials + }; + + if (!string.IsNullOrEmpty(_subject)) + { + tokenDescriptor.Subject.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, _subject)); + } + + if (!string.IsNullOrEmpty(_id)) + { + tokenDescriptor.Subject.AddClaim(new Claim(JwtRegisteredClaimNames.Jti, _id)); + } + + var tokenHandler = new JwtSecurityTokenHandler(); + var token = tokenHandler.CreateToken(tokenDescriptor); + + return tokenHandler.WriteToken(token); + } + + /// + /// 构建并返回 Token 及其信息 + /// + /// Token 信息 + public JwtTokenInfo BuildWithInfo() + { + var token = Build(); + var handler = new JwtSecurityTokenHandler(); + var jwtToken = handler.ReadJwtToken(token); + + return new JwtTokenInfo + { + Token = token, + Header = jwtToken.Header, + Payload = jwtToken.Payload, + Claims = jwtToken.Claims, + ValidFrom = jwtToken.ValidFrom, + ValidTo = jwtToken.ValidTo, + Issuer = jwtToken.Issuer, + Audiences = jwtToken.Audiences + }; + } + } + + /// + /// JWT Token 信息 + /// + public class JwtTokenInfo + { + /// + /// Token 字符串 + /// + public string Token { get; set; } = string.Empty; + + /// + /// 头部 + /// + public JwtHeader Header { get; set; } = null!; + + /// + /// 载荷 + /// + public JwtPayload Payload { get; set; } = null!; + + /// + /// 声明集合 + /// + public IEnumerable Claims { get; set; } = Array.Empty(); + + /// + /// 生效时间 + /// + public DateTime ValidFrom { get; set; } + + /// + /// 过期时间 + /// + public DateTime ValidTo { get; set; } + + /// + /// 颁发者 + /// + public string? Issuer { get; set; } + + /// + /// 受众 + /// + public IEnumerable Audiences { get; set; } = Array.Empty(); + } + + /// + /// JWT 验证结果 + /// + public class JwtValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 声明主体 + /// + public ClaimsPrincipal? Principal { get; set; } + + /// + /// Token 信息 + /// + public JwtSecurityToken? Token { get; set; } + } + + /// + /// JWT 工具类 + /// + public static class JwtUtil + { + /// + /// 创建 JWT 构建器 + /// + /// JwtBuilder + public static JwtBuilder Create() + { + return new JwtBuilder(); + } + + /// + /// 快速生成 JWT Token + /// + /// 密钥 + /// 声明 + /// 有效时长 + /// 颁发者 + /// 受众 + /// JWT Token + public static string GenerateToken( + string secretKey, + Dictionary? claims = null, + TimeSpan? expiresIn = null, + string? issuer = null, + string? audience = null) + { + var builder = new JwtBuilder() + .WithSecretKey(secretKey); + + if (claims != null) + { + foreach (var claim in claims) + { + builder.WithClaim(claim.Key, claim.Value); + } + } + + if (expiresIn.HasValue) + { + builder.WithExpiresIn(expiresIn.Value); + } + + if (!string.IsNullOrEmpty(issuer)) + { + builder.WithIssuer(issuer); + } + + if (!string.IsNullOrEmpty(audience)) + { + builder.WithAudience(audience); + } + + return builder.Build(); + } + + /// + /// 验证 JWT Token + /// + /// Token + /// 密钥 + /// 颁发者 + /// 受众 + /// 验证结果 + public static JwtValidationResult Validate( + string token, + string secretKey, + string? issuer = null, + string? audience = null) + { + try + { + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + + var validationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = key, + ValidateIssuer = !string.IsNullOrEmpty(issuer), + ValidIssuer = issuer, + ValidateAudience = !string.IsNullOrEmpty(audience), + ValidAudience = audience, + ValidateLifetime = true, + ClockSkew = TimeSpan.Zero + }; + + var handler = new JwtSecurityTokenHandler(); + var principal = handler.ValidateToken(token, validationParameters, out var validatedToken); + + return new JwtValidationResult + { + IsValid = true, + Principal = principal, + Token = validatedToken as JwtSecurityToken + }; + } + catch (Exception ex) + { + return new JwtValidationResult + { + IsValid = false, + ErrorMessage = ex.Message + }; + } + } + + /// + /// 解析 JWT Token(不验证) + /// + /// Token + /// Token 信息 + public static JwtSecurityToken Parse(string token) + { + var handler = new JwtSecurityTokenHandler(); + return handler.ReadJwtToken(token); + } + + /// + /// 获取 Token 中的声明 + /// + /// Token + /// 声明集合 + public static IEnumerable GetClaims(string token) + { + var jwtToken = Parse(token); + return jwtToken.Claims; + } + + /// + /// 获取指定声明 + /// + /// Token + /// 声明类型 + /// 声明值 + public static string? GetClaim(string token, string claimType) + { + var claims = GetClaims(token); + return claims.FirstOrDefault(c => c.Type == claimType)?.Value; + } + + /// + /// 检查 Token 是否即将过期 + /// + /// Token + /// 阈值 + /// 是否即将过期 + public static bool IsExpiringSoon(string token, TimeSpan threshold) + { + var jwtToken = Parse(token); + return jwtToken.ValidTo - DateTime.UtcNow < threshold; + } + + /// + /// 刷新 Token + /// + /// 旧 Token + /// 密钥 + /// 新的有效时长 + /// 颁发者 + /// 受众 + /// 新 Token + public static string RefreshToken( + string oldToken, + string secretKey, + TimeSpan? expiresIn = null, + string? issuer = null, + string? audience = null) + { + var claims = GetClaims(oldToken); + var builder = new JwtBuilder() + .WithSecretKey(secretKey) + .WithClaims(claims); + + if (expiresIn.HasValue) + { + builder.WithExpiresIn(expiresIn.Value); + } + + if (!string.IsNullOrEmpty(issuer)) + { + builder.WithIssuer(issuer); + } + + if (!string.IsNullOrEmpty(audience)) + { + builder.WithAudience(audience); + } + + return builder.Build(); + } + } +} diff --git a/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs b/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs new file mode 100644 index 0000000..3e634ed --- /dev/null +++ b/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs @@ -0,0 +1,419 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 密钥和Token生成工具类 + /// + public static class KeyGeneratorUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + + #region API Key 生成 + + /// + /// 生成 API Key + /// + /// 密钥长度(字节数) + /// 前缀 + /// API Key + public static string GenerateApiKey(int length = 32, string? prefix = null) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + var key = Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + + return string.IsNullOrEmpty(prefix) ? key : $"{prefix}_{key}"; + } + + /// + /// 生成标准格式的 API Key(如 sk_xxx) + /// + /// 前缀(默认 sk) + /// API Key + public static string GenerateStandardApiKey(string prefix = "sk") + { + return GenerateApiKey(32, prefix); + } + + /// + /// 生成带有校验位的 API Key + /// + /// 前缀 + /// 签名密钥 + /// 带校验位的 API Key + public static string GenerateApiKeyWithChecksum(string? prefix = null, string? secret = null) + { + var bytes = new byte[24]; + _rng.GetBytes(bytes); + var keyPart = Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + + // 计算校验位 + var checksum = ComputeChecksum(keyPart, secret); + var fullKey = $"{keyPart}_{checksum}"; + + return string.IsNullOrEmpty(prefix) ? fullKey : $"{prefix}_{fullKey}"; + } + + /// + /// 验证带校验位的 API Key + /// + /// API Key + /// 签名密钥 + /// 是否有效 + public static bool ValidateApiKeyChecksum(string apiKey, string? secret = null) + { + if (string.IsNullOrEmpty(apiKey)) + { + return false; + } + + var parts = apiKey.Split('_'); + if (parts.Length < 2) + { + return false; + } + + var checksum = parts[^1]; + var keyPart = string.Join("_", parts[..^1]); + + var expectedChecksum = ComputeChecksum(keyPart, secret); + return ConstantTimeEquals(checksum, expectedChecksum); + } + + #endregion + + #region Token 生成 + + /// + /// 生成访问令牌 + /// + /// Token 长度(字节数) + /// 访问令牌 + public static string GenerateAccessToken(int length = 32) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成刷新令牌 + /// + /// 刷新令牌 + public static string GenerateRefreshToken() + { + var bytes = new byte[64]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成一次性令牌(OTP) + /// + /// OTP 长度 + /// 一次性令牌 + public static string GenerateOneTimePassword(int length = 6) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + + var result = new StringBuilder(length); + for (int i = 0; i < length; i++) + { + result.Append(bytes[i] % 10); + } + + return result.ToString(); + } + + /// + /// 生成一次性令牌(字母数字) + /// + /// Token 长度 + /// 一次性令牌 + public static string GenerateOneTimeToken(int length = 16) + { + const string chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 排除易混淆字符 + var bytes = new byte[length]; + _rng.GetBytes(bytes); + + var result = new char[length]; + for (int i = 0; i < length; i++) + { + result[i] = chars[bytes[i] % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成验证码 + /// + /// 验证码长度 + /// 验证码 + public static string GenerateVerificationCode(int length = 6) + { + return GenerateOneTimePassword(length); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成对称加密密钥 + /// + /// 密钥大小(位) + /// Base64 编码的密钥 + public static string GenerateSymmetricKey(int keySize = 256) + { + using var aes = Aes.Create(); + aes.KeySize = keySize; + aes.GenerateKey(); + return Convert.ToBase64String(aes.Key); + } + + /// + /// 生成对称加密密钥(字节数组) + /// + /// 密钥大小(位) + /// 密钥字节数组 + public static byte[] GenerateSymmetricKeyBytes(int keySize = 256) + { + using var aes = Aes.Create(); + aes.KeySize = keySize; + aes.GenerateKey(); + return aes.Key; + } + + /// + /// 生成 RSA 密钥对 + /// + /// 密钥大小(位) + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateRsaKeyPair(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); +#if NET5_0_OR_GREATER + var publicKey = rsa.ExportRSAPublicKeyPem(); + var privateKey = rsa.ExportRSAPrivateKeyPem(); +#else + // netstandard2.1 使用 ToXmlString 或手动转换为 PEM + var publicKey = Convert.ToBase64String(rsa.ExportRSAPublicKey()); + var privateKey = Convert.ToBase64String(rsa.ExportRSAPrivateKey()); +#endif + return (publicKey, privateKey); + } + + /// + /// 生成 RSA 密钥对(XML 格式) + /// + /// 密钥大小(位) + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateRsaKeyPairXml(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + var publicKey = rsa.ToXmlString(false); + var privateKey = rsa.ToXmlString(true); + return (publicKey, privateKey); + } + + /// + /// 生成 ECDSA 密钥对 + /// + /// 椭圆曲线 + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateEcdsaKeyPair(ECCurve? curve = null) + { + using var ecdsa = ECDsa.Create(curve ?? ECCurve.NamedCurves.nistP256); +#if NET5_0_OR_GREATER + var publicKey = ecdsa.ExportSubjectPublicKeyInfoPem(); + var privateKey = ecdsa.ExportECPrivateKeyPem(); +#else + // netstandard2.1 使用 Base64 格式 + var publicKey = Convert.ToBase64String(ecdsa.ExportSubjectPublicKeyInfo()); + var privateKey = Convert.ToBase64String(ecdsa.ExportECPrivateKey()); +#endif + return (publicKey, privateKey); + } + + /// + /// 生成 HMAC 密钥 + /// + /// 密钥长度(字节数) + /// Base64 编码的密钥 + public static string GenerateHmacKey(int length = 64) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 IV(初始化向量) + /// + /// IV 长度(字节数) + /// Base64 编码的 IV + public static string GenerateIV(int length = 16) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 Salt(盐值) + /// + /// Salt 长度(字节数) + /// Base64 编码的 Salt + public static string GenerateSalt(int length = 16) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + #endregion + + #region 密钥派生 + + /// + /// 从密码派生密钥 + /// + /// 密码 + /// 盐值 + /// 密钥长度(字节数) + /// 迭代次数 + /// 派生的密钥 + public static byte[] DeriveKeyFromPassword(string password, byte[] salt, int keyLength = 32, int iterations = 100000) + { + using var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256); + return pbkdf2.GetBytes(keyLength); + } + + /// + /// 从密码派生密钥(Base64 输出) + /// + /// 密码 + /// 盐值(Base64) + /// 密钥长度(字节数) + /// 迭代次数 + /// 派生的密钥(Base64) + public static string DeriveKeyFromPasswordBase64(string password, string salt, int keyLength = 32, int iterations = 100000) + { + var saltBytes = Convert.FromBase64String(salt); + var key = DeriveKeyFromPassword(password, saltBytes, keyLength, iterations); + return Convert.ToBase64String(key); + } + + /// + /// 使用 HKDF 派生密钥 + /// + /// 输入密钥材料 + /// 盐值 + /// 上下文信息 + /// 输出长度 + /// 派生的密钥 + public static byte[] DeriveKeyHKDF(byte[] inputKeyMaterial, byte[] salt, byte[]? info = null, int outputLength = 32) + { + using var hkdf = new HKDFSHA256(); + return hkdf.DeriveKey(inputKeyMaterial, salt, info ?? Array.Empty(), outputLength); + } + + #endregion + + #region 辅助方法 + + private static string ComputeChecksum(string data, string? secret) + { + var bytes = Encoding.UTF8.GetBytes(data); + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret ?? "default_checksum_key")); + var hash = hmac.ComputeHash(bytes); + return Convert.ToBase64String(hash)[..8]; + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + { + return false; + } + + var result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + + #endregion + } + + /// + /// HKDF (HMAC-based Key Derivation Function) 实现 + /// + internal class HKDFSHA256 : IDisposable + { + private const int HashLength = 32; + + public byte[] DeriveKey(byte[] inputKeyMaterial, byte[] salt, byte[] info, int outputLength) + { + if (outputLength > 255 * HashLength) + { + throw new ArgumentOutOfRangeException(nameof(outputLength), $"输出长度不能超过 {255 * HashLength} 字节"); + } + + // Extract + var prk = Extract(inputKeyMaterial, salt); + + // Expand + return Expand(prk, info, outputLength); + } + + private byte[] Extract(byte[] inputKeyMaterial, byte[] salt) + { + using var hmac = new HMACSHA256(salt.Length == 0 ? new byte[HashLength] : salt); + return hmac.ComputeHash(inputKeyMaterial); + } + + private byte[] Expand(byte[] prk, byte[] info, int outputLength) + { + var result = new byte[outputLength]; + var blockCount = (int)Math.Ceiling((double)outputLength / HashLength); + + var previousBlock = Array.Empty(); + + using var hmac = new HMACSHA256(prk); + + for (int i = 1; i <= blockCount; i++) + { + var input = new byte[previousBlock.Length + info.Length + 1]; + Buffer.BlockCopy(previousBlock, 0, input, 0, previousBlock.Length); + Buffer.BlockCopy(info, 0, input, previousBlock.Length, info.Length); + input[^1] = (byte)i; + + previousBlock = hmac.ComputeHash(input); + + var bytesToCopy = Math.Min(HashLength, outputLength - (i - 1) * HashLength); + Buffer.BlockCopy(previousBlock, 0, result, (i - 1) * HashLength, bytesToCopy); + } + + return result; + } + + public void Dispose() + { + // HMACSHA256 会在 using 块中自动释放 + } + } +} diff --git a/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs b/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs new file mode 100644 index 0000000..581511e --- /dev/null +++ b/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs @@ -0,0 +1,343 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// 密码强度工具类 + /// + public static class PasswordStrengthUtil + { + // 为 netstandard2.1 提供 Math.Log2 的兼容实现 +#if NETSTANDARD2_1 + private static double Log2(double x) => Math.Log(x, 2); +#else + private static double Log2(double x) => Math.Log2(x); +#endif + + /// + /// 检测密码强度 + /// + public static PasswordStrengthResult CheckStrength(string password) + { + var result = new PasswordStrengthResult + { + Password = password + }; + + if (string.IsNullOrEmpty(password)) + { + result.Score = 0; + result.Level = PasswordStrengthLevel.VeryWeak; + result.AddIssue("密码不能为空"); + return result; + } + + var score = 0; + var length = password.Length; + + // 长度评分 + if (length >= 8) score += 1; + if (length >= 12) score += 1; + if (length >= 16) score += 1; + if (length < 6) result.AddIssue("密码长度不足6位"); + + // 包含小写字母 + if (Regex.IsMatch(password, @"[a-z]")) + { + score += 1; + result.HasLowerCase = true; + } + else + { + result.AddIssue("缺少小写字母"); + } + + // 包含大写字母 + if (Regex.IsMatch(password, @"[A-Z]")) + { + score += 1; + result.HasUpperCase = true; + } + else + { + result.AddSuggestion("建议添加大写字母"); + } + + // 包含数字 + if (Regex.IsMatch(password, @"\d")) + { + score += 1; + result.HasDigit = true; + } + else + { + result.AddIssue("缺少数字"); + } + + // 包含特殊字符 + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?~` ")) + { + score += 2; + result.HasSpecialChar = true; + } + else + { + result.AddSuggestion("建议添加特殊字符"); + } + + // 检查连续字符 + if (HasConsecutiveChars(password, 3)) + { + score -= 1; + result.AddIssue("存在连续字符"); + } + + // 检查重复字符 + if (HasRepeatingChars(password, 3)) + { + score -= 1; + result.AddIssue("存在重复字符"); + } + + // 检查常见弱密码 + if (IsCommonPassword(password)) + { + score = Math.Max(0, score - 3); + result.AddIssue("这是一个常见弱密码"); + } + + // 计算熵 + result.Entropy = CalculateEntropy(password); + + // 最终评分 + result.Score = Math.Max(0, Math.Min(10, score)); + + // 确定等级 + result.Level = result.Score switch + { + >= 8 => PasswordStrengthLevel.VeryStrong, + >= 6 => PasswordStrengthLevel.Strong, + >= 4 => PasswordStrengthLevel.Medium, + >= 2 => PasswordStrengthLevel.Weak, + _ => PasswordStrengthLevel.VeryWeak + }; + + return result; + } + + /// + /// 生成强密码 + /// + public static string GenerateStrongPassword(int length = 16, PasswordOptions? options = null) + { + options ??= new PasswordOptions(); + var random = new Random(); + var password = new List(); + + const string lowerChars = "abcdefghijklmnopqrstuvwxyz"; + const string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digitChars = "0123456789"; + const string specialChars = "!@#$%^&*()_+-=[]{}|;:,.<>?"; + + // 确保每种要求的字符至少有一个 + if (options.RequireLowerCase) + password.Add(lowerChars[random.Next(lowerChars.Length)]); + if (options.RequireUpperCase) + password.Add(upperChars[random.Next(upperChars.Length)]); + if (options.RequireDigit) + password.Add(digitChars[random.Next(digitChars.Length)]); + if (options.RequireSpecialChar) + password.Add(specialChars[random.Next(specialChars.Length)]); + + // 构建可用字符集 + var allChars = ""; + if (options.AllowLowerCase) allChars += lowerChars; + if (options.AllowUpperCase) allChars += upperChars; + if (options.AllowDigit) allChars += digitChars; + if (options.AllowSpecialChar) allChars += specialChars; + + if (string.IsNullOrEmpty(allChars)) + allChars = lowerChars + digitChars; + + // 排除相似字符 + if (options.ExcludeSimilarChars) + allChars = Regex.Replace(allChars, @"[il1Lo0O]", ""); + + // 排除歧义字符 + if (options.ExcludeAmbiguousChars) + allChars = Regex.Replace(allChars, @"[{}[\]()""'`~,;:.<>\\/|]", ""); + + // 填充剩余长度 + while (password.Count < length) + { + password.Add(allChars[random.Next(allChars.Length)]); + } + + // 打乱顺序 + for (int i = password.Count - 1; i > 0; i--) + { + var j = random.Next(i + 1); + (password[i], password[j]) = (password[j], password[i]); + } + + return new string(password.ToArray()); + } + + /// + /// 检查是否为常见弱密码 + /// + public static bool IsCommonPassword(string password) + { + var commonPasswords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "password", "123456", "12345678", "qwerty", "abc123", + "monkey", "master", "dragon", "111111", "baseball", + "iloveyou", "trustno1", "sunshine", "princess", "welcome", + "shadow", "superman", "michael", "football", "letmein", + "password1", "password123", "admin", "root", "test" + }; + + return commonPasswords.Contains(password); + } + + /// + /// 计算密码熵 + /// + public static double CalculateEntropy(string password) + { + if (string.IsNullOrEmpty(password)) + return 0; + + var charPool = 0; + if (Regex.IsMatch(password, @"[a-z]")) charPool += 26; + if (Regex.IsMatch(password, @"[A-Z]")) charPool += 26; + if (Regex.IsMatch(password, @"\d")) charPool += 10; + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?~`]")) charPool += 32; + + if (charPool == 0) return 0; + + return password.Length * Log2(charPool); + } + + /// + /// 检查密码是否过期 + /// + public static bool IsPasswordExpired(DateTime lastChangeDate, int maxAgeDays = 90) + { + return (DateTime.Now - lastChangeDate).TotalDays > maxAgeDays; + } + + /// + /// 估算密码破解时间 + /// + public static TimeSpan EstimateCrackTime(string password, int guessesPerSecond = 1_000_000_000) + { + var entropy = CalculateEntropy(password); + var combinations = Math.Pow(2, entropy); + var seconds = combinations / guessesPerSecond / 2; // 平均尝试次数 + + if (seconds < 1) return TimeSpan.FromMilliseconds(seconds * 1000); + if (seconds < 60) return TimeSpan.FromSeconds(seconds); + if (seconds < 3600) return TimeSpan.FromMinutes(seconds / 60); + if (seconds < 86400) return TimeSpan.FromHours(seconds / 3600); + if (seconds < 2592000) return TimeSpan.FromDays(seconds / 86400); + if (seconds < 31536000) return TimeSpan.FromDays(seconds / 86400); + + return TimeSpan.FromDays(seconds / 86400); + } + + private static bool HasConsecutiveChars(string password, int count) + { + for (int i = 0; i <= password.Length - count; i++) + { + var consecutive = true; + for (int j = 1; j < count && consecutive; j++) + { + if (password[i + j] != password[i] + j && password[i + j] != password[i] - j) + { + consecutive = false; + } + } + if (consecutive) return true; + } + return false; + } + + private static bool HasRepeatingChars(string password, int count) + { + for (int i = 0; i <= password.Length - count; i++) + { + var repeating = true; + for (int j = 1; j < count && repeating; j++) + { + if (password[i + j] != password[i]) + { + repeating = false; + } + } + if (repeating) return true; + } + return false; + } + } + + /// + /// 密码强度结果 + /// + public class PasswordStrengthResult + { + public string Password { get; set; } = ""; + public int Score { get; set; } + public PasswordStrengthLevel Level { get; set; } + public double Entropy { get; set; } + public bool HasLowerCase { get; set; } + public bool HasUpperCase { get; set; } + public bool HasDigit { get; set; } + public bool HasSpecialChar { get; set; } + public List Issues { get; } = new(); + public List Suggestions { get; } = new(); + + internal void AddIssue(string issue) => Issues.Add(issue); + internal void AddSuggestion(string suggestion) => Suggestions.Add(suggestion); + + public string LevelDescription => Level switch + { + PasswordStrengthLevel.VeryStrong => "非常强", + PasswordStrengthLevel.Strong => "强", + PasswordStrengthLevel.Medium => "中等", + PasswordStrengthLevel.Weak => "弱", + _ => "非常弱" + }; + } + + /// + /// 密码强度等级 + /// + public enum PasswordStrengthLevel + { + VeryWeak = 0, + Weak = 1, + Medium = 2, + Strong = 3, + VeryStrong = 4 + } + + /// + /// 密码生成选项 + /// + public class PasswordOptions + { + public bool AllowLowerCase { get; set; } = true; + public bool AllowUpperCase { get; set; } = true; + public bool AllowDigit { get; set; } = true; + public bool AllowSpecialChar { get; set; } = true; + public bool RequireLowerCase { get; set; } = true; + public bool RequireUpperCase { get; set; } = true; + public bool RequireDigit { get; set; } = true; + public bool RequireSpecialChar { get; set; } = false; + public bool ExcludeSimilarChars { get; set; } = true; + public bool ExcludeAmbiguousChars { get; set; } = false; + } +} diff --git a/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs b/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs new file mode 100644 index 0000000..c62307a --- /dev/null +++ b/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs @@ -0,0 +1,533 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 安全随机数生成器,使用加密安全的随机数生成器 + /// + public static class SecureRandomUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] _alphanumericChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); + private static readonly char[] _lowercaseChars = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); + private static readonly char[] _uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); + private static readonly char[] _digitChars = "0123456789".ToCharArray(); + private static readonly char[] _specialChars = "!@#$%^&*()_+-=[]{}|;:,.<>?".ToCharArray(); + private static readonly char[] _hexChars = "0123456789abcdef".ToCharArray(); + + #region 基本随机数生成 + + /// + /// 生成指定长度的随机字节数组 + /// + /// 字节长度 + /// 随机字节数组 + public static byte[] GetBytes(int length) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于0"); + } + + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return bytes; + } + + /// + /// 生成随机整数 + /// + /// 非负随机整数 + public static int GetInt() + { + return GetInt(0, int.MaxValue); + } + + /// + /// 生成指定范围内的随机整数 + /// + /// 最小值(包含) + /// 最大值(不包含) + /// 随机整数 + public static int GetInt(int min, int max) + { + if (min >= max) + { + throw new ArgumentOutOfRangeException(nameof(max), "最大值必须大于最小值"); + } + + var range = (long)max - min; + var bytes = new byte[4]; + + // 使用拒绝采样避免模偏差 + while (true) + { + _rng.GetBytes(bytes); + var value = BitConverter.ToUInt32(bytes, 0); + var remainder = range * (value / range); + + if (remainder <= uint.MaxValue - range) + { + return (int)(min + (value - remainder)); + } + } + } + + /// + /// 生成随机长整数 + /// + /// 最小值(包含) + /// 最大值(不包含) + /// 随机长整数 + public static long GetLong(long min, long max) + { + if (min >= max) + { + throw new ArgumentOutOfRangeException(nameof(max), "最大值必须大于最小值"); + } + + var range = (decimal)max - min; + var bytes = new byte[8]; + + while (true) + { + _rng.GetBytes(bytes); + var value = (decimal)BitConverter.ToUInt64(bytes, 0); + var remainder = range * (value / range); + + if (remainder <= ulong.MaxValue - range) + { + return (long)(min + (value - remainder)); + } + } + } + + /// + /// 生成随机双精度浮点数(0.0 到 1.0) + /// + /// 随机双精度浮点数 + public static double GetDouble() + { + var bytes = new byte[8]; + _rng.GetBytes(bytes); + var value = BitConverter.ToUInt64(bytes, 0); + return value / (double)ulong.MaxValue; + } + + /// + /// 生成随机布尔值 + /// + /// 随机布尔值 + public static bool GetBool() + { + var bytes = new byte[1]; + _rng.GetBytes(bytes); + return (bytes[0] & 1) == 1; + } + + #endregion + + #region 字符串生成 + + /// + /// 生成随机字符串(字母数字) + /// + /// 字符串长度 + /// 随机字符串 + public static string GetString(int length) + { + return GetString(length, _alphanumericChars); + } + + /// + /// 使用指定字符集生成随机字符串 + /// + /// 字符串长度 + /// 字符集 + /// 随机字符串 + public static string GetString(int length, char[] chars) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于0"); + } + + var result = new char[length]; + var bytes = new byte[length * 2]; + + _rng.GetBytes(bytes); + + for (int i = 0; i < length; i++) + { + var value = BitConverter.ToUInt16(bytes, i * 2); + result[i] = chars[value % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成随机小写字符串 + /// + /// 字符串长度 + /// 随机小写字符串 + public static string GetLowercaseString(int length) + { + return GetString(length, _lowercaseChars); + } + + /// + /// 生成随机大写字符串 + /// + /// 字符串长度 + /// 随机大写字符串 + public static string GetUppercaseString(int length) + { + return GetString(length, _uppercaseChars); + } + + /// + /// 生成随机数字字符串 + /// + /// 字符串长度 + /// 随机数字字符串 + public static string GetNumericString(int length) + { + return GetString(length, _digitChars); + } + + /// + /// 生成随机十六进制字符串 + /// + /// 字符串长度 + /// 是否使用大写字母 + /// 随机十六进制字符串 + public static string GetHexString(int length, bool uppercase = false) + { + var result = GetString(length, _hexChars); + return uppercase ? result.ToUpperInvariant() : result; + } + + /// + /// 生成随机 Base64 字符串 + /// + /// 原始字节长度 + /// Base64 编码的随机字符串 + public static string GetBase64String(int byteLength) + { + var bytes = GetBytes(byteLength); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 URL 安全的随机字符串 + /// + /// 原始字节长度 + /// URL 安全的随机字符串 + public static string GetUrlSafeString(int byteLength) + { + var bytes = GetBytes(byteLength); + return Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + } + + #endregion + + #region 密码生成 + + /// + /// 生成随机密码 + /// + /// 密码长度 + /// 包含小写字母 + /// 包含大写字母 + /// 包含数字 + /// 包含特殊字符 + /// 随机密码 + public static string GeneratePassword(int length = 16, + bool includeLowercase = true, + bool includeUppercase = true, + bool includeDigits = true, + bool includeSpecial = true) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "密码长度必须大于0"); + } + + var charSets = new List(); + var allChars = new List(); + + if (includeLowercase) + { + charSets.Add(_lowercaseChars); + allChars.AddRange(_lowercaseChars); + } + + if (includeUppercase) + { + charSets.Add(_uppercaseChars); + allChars.AddRange(_uppercaseChars); + } + + if (includeDigits) + { + charSets.Add(_digitChars); + allChars.AddRange(_digitChars); + } + + if (includeSpecial) + { + charSets.Add(_specialChars); + allChars.AddRange(_specialChars); + } + + if (charSets.Count == 0) + { + throw new ArgumentException("至少需要选择一种字符类型"); + } + + var result = new char[length]; + var allCharsArray = allChars.ToArray(); + + // 确保每种选中的字符类型至少有一个字符 + var usedCharsets = Math.Min(charSets.Count, length); + for (int i = 0; i < usedCharsets; i++) + { + result[i] = GetString(1, charSets[i])[0]; + } + + // 填充剩余字符 + for (int i = usedCharsets; i < length; i++) + { + result[i] = GetString(1, allCharsArray)[0]; + } + + // 打乱顺序 + Shuffle(result); + + return new string(result); + } + + /// + /// 生成强密码(至少包含一个大写、小写、数字和特殊字符) + /// + /// 密码长度(至少4) + /// 强密码 + public static string GenerateStrongPassword(int length = 16) + { + if (length < 4) + { + throw new ArgumentOutOfRangeException(nameof(length), "强密码长度至少为4"); + } + + return GeneratePassword(length, true, true, true, true); + } + + /// + /// 生成 PIN 码(纯数字) + /// + /// PIN 码长度 + /// PIN 码 + public static string GeneratePin(int length = 6) + { + return GetNumericString(length); + } + + #endregion + + #region 集合操作 + + /// + /// 从数组中随机选择一个元素 + /// + /// 元素类型 + /// 源数组 + /// 随机选择的元素 + public static T Choice(T[] array) + { + if (array == null || array.Length == 0) + { + throw new ArgumentException("数组不能为空", nameof(array)); + } + + var index = GetInt(0, array.Length); + return array[index]; + } + + /// + /// 从列表中随机选择一个元素 + /// + /// 元素类型 + /// 源列表 + /// 随机选择的元素 + public static T Choice(IList list) + { + if (list == null || list.Count == 0) + { + throw new ArgumentException("列表不能为空", nameof(list)); + } + + var index = GetInt(0, list.Count); + return list[index]; + } + + /// + /// 从数组中随机选择多个元素(不重复) + /// + /// 元素类型 + /// 源数组 + /// 选择数量 + /// 随机选择的元素数组 + public static T[] Sample(T[] array, int count) + { + if (array == null || array.Length == 0) + { + throw new ArgumentException("数组不能为空", nameof(array)); + } + + if (count <= 0 || count > array.Length) + { + throw new ArgumentOutOfRangeException(nameof(count), "选择数量必须在1到数组长度之间"); + } + + var indices = new int[array.Length]; + for (int i = 0; i < indices.Length; i++) + { + indices[i] = i; + } + + Shuffle(indices); + + var result = new T[count]; + for (int i = 0; i < count; i++) + { + result[i] = array[indices[i]]; + } + + return result; + } + + /// + /// 原地打乱数组顺序(Fisher-Yates 洗牌算法) + /// + /// 元素类型 + /// 要打乱的数组 + public static void Shuffle(T[] array) + { + if (array == null || array.Length <= 1) + { + return; + } + + for (int i = array.Length - 1; i > 0; i--) + { + var j = GetInt(0, i + 1); + (array[i], array[j]) = (array[j], array[i]); + } + } + + /// + /// 原地打乱列表顺序 + /// + /// 元素类型 + /// 要打乱的列表 + public static void Shuffle(IList list) + { + if (list == null || list.Count <= 1) + { + return; + } + + for (int i = list.Count - 1; i > 0; i--) + { + var j = GetInt(0, i + 1); + (list[i], list[j]) = (list[j], list[i]); + } + } + + #endregion + + #region GUID 和 UUID + + /// + /// 生成随机 GUID + /// + /// 随机 GUID + public static Guid GetGuid() + { + var bytes = GetBytes(16); + return new Guid(bytes); + } + + /// + /// 生成 UUID v4(随机 UUID) + /// + /// UUID v4 字符串 + public static string GetUuidV4() + { + var bytes = GetBytes(16); + + // 设置版本位(版本4) + bytes[6] = (byte)((bytes[6] & 0x0F) | 0x40); + + // 设置变体位 + bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80); + + return new Guid(bytes).ToString(); + } + + #endregion + + #region 填充 + + /// + /// 用随机字节填充数组 + /// + /// 目标数组 + public static void Fill(byte[] buffer) + { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + _rng.GetBytes(buffer); + } + + /// + /// 用随机字节填充数组的一部分 + /// + /// 目标数组 + /// 起始位置 + /// 填充数量 + public static void Fill(byte[] buffer, int offset, int count) + { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + if (offset < 0 || offset >= buffer.Length) + { + throw new ArgumentOutOfRangeException(nameof(offset)); + } + + if (count <= 0 || offset + count > buffer.Length) + { + throw new ArgumentOutOfRangeException(nameof(count)); + } + + var segment = new ArraySegment(buffer, offset, count).ToArray(); + _rng.GetBytes(segment); + Array.Copy(segment, 0, buffer, offset, count); + } + + #endregion + } +} diff --git a/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs b/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs new file mode 100644 index 0000000..e3ec37e --- /dev/null +++ b/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs @@ -0,0 +1,312 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// SQL注入防护工具类 + /// + public static class SqlInjectionUtil + { + private static readonly Regex SqlKeywordsPattern = new( + @"\b(select|insert|update|delete|drop|create|alter|truncate|exec|execute|xp_|sp_|declare|cast|convert|union|join|where|from|into|values|set|order|group|having|limit|offset)\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex SqlCommentPattern = new( + @"(--)|(/\*)|(\*/)", + RegexOptions.Compiled); + + private static readonly Regex SqlQuotePattern = new( + @"('|""|`)", + RegexOptions.Compiled); + + private static readonly Regex SqlSemicolonPattern = new( + @";", + RegexOptions.Compiled); + + private static readonly Regex SqlDangerousPattern = new( + @"(\b(OR|AND)\s*['""]?\d+['""]?\s*=\s*['""]?\d+)|" + // OR 1=1 + @"(\b(OR|AND)\s*['""][^'""]*['""]\s*=\s*['""][^'""]*['""])|" + // OR 'a'='a' + @"(UNION\s+(ALL\s+)?SELECT)|" + // UNION SELECT + @"(EXEC\s+)|" + // EXEC + @"(Xp_\w+)|" + // xp_cmdshell等 + @"(WAITFOR\s+DELAY)|" + // WAITFOR DELAY + @"(BENCHMARK\s*\()|" + // MySQL BENCHMARK + @"(SLEEP\s*\()", // MySQL SLEEP + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly HashSet DangerousKeywords = new(StringComparer.OrdinalIgnoreCase) + { + "xp_cmdshell", "xp_regread", "xp_regwrite", "xp_regdelete", + "sp_executesql", "sp_oacreate", "sp_oamethod", + "information_schema", "sysobjects", "syscolumns", + "pg_catalog", "pg_class", "pg_attribute" + }; + + /// + /// 检测是否存在SQL注入风险 + /// + public static bool HasSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + // 检测危险模式 + if (SqlDangerousPattern.IsMatch(input)) + return true; + + // 检测注释 + if (SqlCommentPattern.IsMatch(input)) + return true; + + // 检测危险关键字组合 + var upperInput = input.ToUpperInvariant(); + foreach (var keyword in DangerousKeywords) + { + if (upperInput.Contains(keyword.ToUpperInvariant())) + return true; + } + + // 检测单引号后面跟SQL关键字 + if (Regex.IsMatch(input, @"'\s*(OR|AND|UNION|SELECT|INSERT|UPDATE|DELETE)", RegexOptions.IgnoreCase)) + return true; + + return false; + } + + /// + /// 转义SQL字符串参数 + /// + public static string EscapeString(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + switch (c) + { + case '\'': + result.Append("''"); + break; + case '\\': + result.Append("\\\\"); + break; + case '\0': + result.Append("\\0"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\x1a': + result.Append("\\Z"); + break; + default: + result.Append(c); + break; + } + } + return result.ToString(); + } + + /// + /// 移除潜在的SQL注入字符 + /// + public static string Sanitize(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + + // 移除注释 + result = SqlCommentPattern.Replace(result, ""); + + // 移除分号(防止多语句执行) + result = SqlSemicolonPattern.Replace(result, ""); + + // 转义引号 + result = SqlQuotePattern.Replace(result, m => m.Value == "'" ? "''" : "\\" + m.Value); + + return result; + } + + /// + /// 过滤SQL关键字 + /// + public static string FilterKeywords(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return SqlKeywordsPattern.Replace(input, ""); + } + + /// + /// 验证标识符(表名、列名等) + /// + public static bool IsValidIdentifier(string identifier) + { + if (string.IsNullOrEmpty(identifier)) + return false; + + // 只允许字母、数字、下划线 + if (!Regex.IsMatch(identifier, @"^[a-zA-Z_][a-zA-Z0-9_]*$")) + return false; + + // 不能是SQL关键字 + if (SqlKeywordsPattern.IsMatch(identifier)) + return false; + + return true; + } + + /// + /// 安全的标识符包装 + /// + public static string QuoteIdentifier(string identifier, string quoteChar = "`") + { + if (string.IsNullOrEmpty(identifier)) + return identifier; + + // 转义内部的引号 + identifier = identifier.Replace(quoteChar, quoteChar + quoteChar); + return $"{quoteChar}{identifier}{quoteChar}"; + } + + /// + /// 构建安全的IN子句参数 + /// + public static string BuildInClause(IEnumerable values, bool numeric = false) + { + var items = new List(); + foreach (var value in values) + { + if (numeric && int.TryParse(value, out _)) + { + items.Add(value); + } + else + { + items.Add($"'{EscapeString(value)}'"); + } + } + return string.Join(", ", items); + } + + /// + /// 构建安全的LIKE子句 + /// + public static string EscapeLikePattern(string pattern) + { + if (string.IsNullOrEmpty(pattern)) + return pattern; + + var result = new StringBuilder(); + foreach (var c in pattern) + { + switch (c) + { + case '%': + case '_': + case '[': + case ']': + result.Append('\\'); + break; + } + result.Append(c); + } + return result.ToString(); + } + + /// + /// 检测批量SQL注入 + /// + public static Dictionary CheckMultiple(IEnumerable> inputs) + { + var results = new Dictionary(); + foreach (var kvp in inputs) + { + results[kvp.Key] = HasSqlInjection(kvp.Value); + } + return results; + } + + /// + /// 获取SQL注入风险详情 + /// + public static SqlInjectionAnalysis Analyze(string input) + { + var analysis = new SqlInjectionAnalysis + { + Input = input, + HasRisk = false + }; + + if (string.IsNullOrEmpty(input)) + return analysis; + + // 检测各种风险 + if (SqlKeywordsPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含SQL关键字"); + } + + if (SqlCommentPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含SQL注释"); + } + + if (SqlDangerousPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含危险SQL模式"); + } + + foreach (var keyword in DangerousKeywords) + { + if (input.Contains(keyword, StringComparison.OrdinalIgnoreCase)) + { + analysis.HasRisk = true; + analysis.DetectedKeywords.Add(keyword); + } + } + + return analysis; + } + } + + /// + /// SQL注入分析结果 + /// + public class SqlInjectionAnalysis + { + /// + /// 输入字符串 + /// + public string Input { get; set; } = ""; + + /// + /// 是否有风险 + /// + public bool HasRisk { get; set; } + + /// + /// 检测到的风险列表 + /// + public List Risks { get; } = new(); + + /// + /// 检测到的危险关键字 + /// + public List DetectedKeywords { get; } = new(); + } +} diff --git a/EasyTool.Core/SecurityCategory/TlsUtil.cs b/EasyTool.Core/SecurityCategory/TlsUtil.cs new file mode 100644 index 0000000..e33465a --- /dev/null +++ b/EasyTool.Core/SecurityCategory/TlsUtil.cs @@ -0,0 +1,423 @@ +using System; +using System.Collections.Generic; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; + +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// TLS/SSL 配置和验证工具类 + /// + public static class TlsUtil + { + #region SSL/TLS 协议配置 + + /// + /// 获取支持的 SSL/TLS 协议 + /// + /// 支持的协议列表 + public static SslProtocols GetSupportedProtocols() + { +#if NETSTANDARD2_1 + // netstandard2.1 不支持 TLS 1.3 + return SslProtocols.Tls12; +#else + return SslProtocols.Tls12 | SslProtocols.Tls13; +#endif + } + + /// + /// 获取安全的 SSL/TLS 协议(排除不安全版本) + /// + /// 安全的协议 + public static SslProtocols GetSecureProtocols() + { +#if NETSTANDARD2_1 + return SslProtocols.Tls12; +#else + return SslProtocols.Tls12 | SslProtocols.Tls13; +#endif + } + + /// + /// 检查协议是否安全 + /// + /// 要检查的协议 + /// 是否安全 + public static bool IsSecureProtocol(SslProtocols protocol) + { + // SSL 2.0, SSL 3.0, TLS 1.0, TLS 1.1 被认为不安全 + var insecureProtocols = SslProtocols.Ssl2 | SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11; + + return (protocol & insecureProtocols) == 0 && + (protocol & SslProtocols.Tls12) != 0; + } + + #endregion + + #region 证书验证 + + /// + /// 创建证书验证回调(验证服务器证书) + /// + /// 是否允许无效证书 + /// 是否验证证书链 + /// 远程证书验证回调 + public static RemoteCertificateValidationCallback CreateCertificateValidationCallback( + bool allowInvalidCerts = false, + bool validateChain = true) + { + return (sender, certificate, chain, sslPolicyErrors) => + { + // 如果允许无效证书,直接返回 true + if (allowInvalidCerts) + { + return true; + } + + // 如果没有错误,返回 true + if (sslPolicyErrors == SslPolicyErrors.None) + { + return true; + } + + // 如果不验证证书链,只检查是否有证书 + if (!validateChain) + { + return certificate != null; + } + + // 默认:严格验证 + return false; + }; + } + + /// + /// 创建验证特定域名的证书验证回调 + /// + /// 允许的域名列表 + /// 远程证书验证回调 + public static RemoteCertificateValidationCallback CreateDomainValidationCallback(params string[] allowedDomains) + { + return (sender, certificate, chain, sslPolicyErrors) => + { + if (sslPolicyErrors == SslPolicyErrors.None) + { + return true; + } + + // 检查域名不匹配的情况 + if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) + { + if (certificate is X509Certificate2 cert && allowedDomains.Length > 0) + { + var certDomain = cert.GetNameInfo(X509NameType.DnsName, false); + foreach (var domain in allowedDomains) + { + if (MatchesDomain(certDomain, domain)) + { + return true; + } + } + } + } + + return false; + }; + } + + /// + /// 验证证书有效性 + /// + /// 证书 + /// 是否检查吊销状态 + /// 验证结果 + public static CertificateValidationResult ValidateCertificate(X509Certificate2 certificate, bool checkRevocation = false) + { + var result = new CertificateValidationResult { IsValid = true }; + + if (certificate == null) + { + return new CertificateValidationResult { IsValid = false, Errors = new List { "证书为空" } }; + } + + // 检查证书是否过期 + var now = DateTime.UtcNow; + if (certificate.NotBefore > now) + { + return new CertificateValidationResult + { + IsValid = false, + Errors = new List { $"证书尚未生效,生效时间: {certificate.NotBefore}" } + }; + } + + if (certificate.NotAfter < now) + { + return new CertificateValidationResult + { + IsValid = false, + Errors = new System.Collections.Generic.List { $"证书已过期,过期时间: {certificate.NotAfter}" } + }; + } + + // 检查证书链 + using var chain = new X509Chain(); + chain.ChainPolicy.RevocationMode = checkRevocation + ? X509RevocationMode.Online + : X509RevocationMode.NoCheck; + + if (!chain.Build(certificate)) + { + var errors = new System.Collections.Generic.List(); + foreach (var status in chain.ChainStatus) + { + errors.Add(status.StatusInformation); + } + + return new CertificateValidationResult + { + IsValid = false, + Errors = new System.Collections.Generic.List { $"证书链验证失败: {string.Join(", ", errors)}" } + }; + } + + result.Subject = certificate.Subject; + result.Issuer = certificate.Issuer; + result.NotBefore = certificate.NotBefore; + result.NotAfter = certificate.NotAfter; + result.Thumbprint = certificate.Thumbprint; + + return result; + } + + #endregion + + #region 证书加载 + + /// + /// 从文件加载证书 + /// + /// 证书文件路径 + /// 密码(可选) + /// X509 证书 + public static X509Certificate2 LoadCertificate(string filePath, string? password = null) + { + if (string.IsNullOrEmpty(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + + if (!System.IO.File.Exists(filePath)) + { + throw new System.IO.FileNotFoundException("证书文件不存在", filePath); + } + + return string.IsNullOrEmpty(password) + ? new X509Certificate2(filePath) + : new X509Certificate2(filePath, password); + } + + /// + /// 从 PFX 文件加载证书 + /// + /// PFX 文件路径 + /// 密码 + /// X509 证书 + public static X509Certificate2 LoadPfxCertificate(string filePath, string password) + { + return LoadCertificate(filePath, password); + } + + /// + /// 从 PEM 文件加载证书 + /// + /// 证书文件路径 + /// 私钥文件路径(可选) + /// X509 证书 + public static X509Certificate2 LoadPemCertificate(string certPath, string? keyPath = null) + { + if (string.IsNullOrEmpty(certPath)) + { + throw new ArgumentNullException(nameof(certPath)); + } + +#if NETSTANDARD2_1 + // netstandard2.1 不支持 CreateFromPem,使用替代方案 + var certBytes = System.IO.File.ReadAllBytes(certPath); + return new X509Certificate2(certBytes); +#else + var certPem = System.IO.File.ReadAllText(certPath); + + if (string.IsNullOrEmpty(keyPath)) + { + return X509Certificate2.CreateFromPem(certPem); + } + + var keyPem = System.IO.File.ReadAllText(keyPath); + return X509Certificate2.CreateFromPem(certPem, keyPem); +#endif + } + + /// + /// 从证书存储区加载证书 + /// + /// 存储区名称 + /// 存储区位置 + /// 证书指纹 + /// X509 证书 + public static X509Certificate2? LoadCertificateFromStore(StoreName storeName, StoreLocation storeLocation, string thumbprint) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadOnly); + + var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false); + + return certificates.Count > 0 ? certificates[0] : null; + } + + #endregion + + #region 证书信息 + + /// + /// 获取证书信息 + /// + /// 证书 + /// 证书信息 + public static CertificateInfo GetCertificateInfo(X509Certificate2 certificate) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + return new CertificateInfo + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint, + SerialNumber = certificate.SerialNumber, + HasPrivateKey = certificate.HasPrivateKey, + KeySize = certificate.GetRSAPublicKey()?.KeySize ?? 0, + SignatureAlgorithm = certificate.SignatureAlgorithm.FriendlyName ?? "Unknown" + }; + } + + /// + /// 检查证书是否即将过期 + /// + /// 证书 + /// 提前天数阈值 + /// 是否即将过期 + public static bool IsCertificateExpiringSoon(X509Certificate2 certificate, int daysThreshold = 30) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + var timeRemaining = certificate.NotAfter - DateTime.UtcNow; + return timeRemaining.TotalDays <= daysThreshold; + } + + /// + /// 获取证书剩余有效天数 + /// + /// 证书 + /// 剩余有效天数 + public static int GetDaysUntilExpiration(X509Certificate2 certificate) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + var timeRemaining = certificate.NotAfter - DateTime.UtcNow; + return Math.Max(0, (int)timeRemaining.TotalDays); + } + + #endregion + + #region SSL 选项 + + /// + /// 创建服务器 SSL 选项 + /// + /// 目标主机 + /// 服务器证书 + /// SSL 选项 + public static SslServerAuthenticationOptions CreateServerSslOptions(string targetHost, X509Certificate2? serverCertificate = null) + { + var options = new SslServerAuthenticationOptions + { + EnabledSslProtocols = GetSecureProtocols(), + ClientCertificateRequired = false + }; + + if (serverCertificate != null) + { + options.ServerCertificate = serverCertificate; + } + + return options; + } + + /// + /// 创建客户端 SSL 选项 + /// + /// 客户端证书 + /// 是否允许无效的服务器证书 + /// SSL 选项 + public static SslClientAuthenticationOptions CreateClientSslOptions( + X509Certificate2? clientCertificate = null, + bool allowInvalidServerCert = false) + { + var options = new SslClientAuthenticationOptions + { + EnabledSslProtocols = GetSecureProtocols(), + RemoteCertificateValidationCallback = CreateCertificateValidationCallback(allowInvalidServerCert) + }; + + if (clientCertificate != null) + { + options.ClientCertificates = new X509Certificate2Collection { clientCertificate }; + } + + return options; + } + + #endregion + + #region 辅助方法 + + private static bool MatchesDomain(string? certDomain, string allowedDomain) + { + if (string.IsNullOrEmpty(certDomain)) + { + return false; + } + + // 支持通配符域名 + if (certDomain.StartsWith("*.")) + { + var certBaseDomain = certDomain[2..]; + return allowedDomain.EndsWith(certBaseDomain, StringComparison.OrdinalIgnoreCase) || + allowedDomain.Equals(certBaseDomain, StringComparison.OrdinalIgnoreCase); + } + + return certDomain.Equals(allowedDomain, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + } + + } + + \ No newline at end of file diff --git a/EasyTool.Core/SecurityCategory/XssUtil.cs b/EasyTool.Core/SecurityCategory/XssUtil.cs new file mode 100644 index 0000000..c0126fa --- /dev/null +++ b/EasyTool.Core/SecurityCategory/XssUtil.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// XSS防护工具类 + /// + public static class XssUtil + { + private static readonly Dictionary HtmlEntityEncodeMap = new() + { + { "<", "<" }, + { ">", ">" }, + { "&", "&" }, + { "\"", """ }, + { "'", "'" }, + { "/", "/" }, + { "`", "`" }, + { "=", "=" } + }; + + private static readonly HashSet AllowedTags = new(StringComparer.OrdinalIgnoreCase) + { + "b", "i", "u", "strong", "em", "p", "br", "span", "div", "a", "img", + "ul", "ol", "li", "h1", "h2", "h3", "h4", "h5", "h6", + "table", "thead", "tbody", "tr", "td", "th", "blockquote", "pre", "code" + }; + + private static readonly HashSet AllowedAttributes = new(StringComparer.OrdinalIgnoreCase) + { + "href", "src", "alt", "title", "class", "id", "style" + }; + + private static readonly Regex ScriptPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex EventPattern = new(@"\s*on\w+\s*=", RegexOptions.IgnoreCase); + private static readonly Regex JavaScriptPattern = new(@"javascript\s*:", RegexOptions.IgnoreCase); + private static readonly Regex VbscriptPattern = new(@"vbscript\s*:", RegexOptions.IgnoreCase); + private static readonly Regex DataUrlPattern = new(@"data\s*:", RegexOptions.IgnoreCase); + private static readonly Regex HtmlCommentPattern = new(@"", RegexOptions.Singleline); + private static readonly Regex SvgPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex IframePattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex ObjectPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex EmbedPattern = new(@"]*>", RegexOptions.IgnoreCase); + private static readonly Regex ExpressionPattern = new(@"expression\s*\(", RegexOptions.IgnoreCase); + + /// + /// HTML实体编码 + /// + public static string HtmlEncode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + var str = c.ToString(); + result.Append(HtmlEntityEncodeMap.TryGetValue(str, out var encoded) ? encoded : str); + } + return result.ToString(); + } + + /// + /// HTML实体解码 + /// + public static string HtmlDecode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + foreach (var kvp in HtmlEntityEncodeMap) + { + result = result.Replace(kvp.Value, kvp.Key); + } + + // 解码数字实体 + result = Regex.Replace(result, @"&#(\d+);", m => + { + var code = int.Parse(m.Groups[1].Value); + return ((char)code).ToString(); + }); + + // 解码十六进制实体 + result = Regex.Replace(result, @"&#x([0-9a-fA-F]+);", m => + { + var code = Convert.ToInt32(m.Groups[1].Value, 16); + return ((char)code).ToString(); + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 过滤XSS攻击代码 + /// + public static string Sanitize(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + + // 移除脚本标签 + result = ScriptPattern.Replace(result, ""); + result = SvgPattern.Replace(result, ""); + result = IframePattern.Replace(result, ""); + result = ObjectPattern.Replace(result, ""); + result = EmbedPattern.Replace(result, ""); + + // 移除事件处理器 + result = EventPattern.Replace(result, ""); + + // 移除危险协议 + result = JavaScriptPattern.Replace(result, ""); + result = VbscriptPattern.Replace(result, ""); + result = DataUrlPattern.Replace(result, ""); + + // 移除CSS表达式 + result = ExpressionPattern.Replace(result, ""); + + // 移除HTML注释 + result = HtmlCommentPattern.Replace(result, ""); + + return result; + } + + /// + /// 清理HTML标签(只保留允许的标签和属性) + /// + public static string CleanHtml(string input, IEnumerable? allowedTags = null, IEnumerable? allowedAttributes = null) + { + if (string.IsNullOrEmpty(input)) + return input; + + var tags = allowedTags != null ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) : AllowedTags; + var attrs = allowedAttributes != null ? new HashSet(allowedAttributes, StringComparer.OrdinalIgnoreCase) : AllowedAttributes; + + // 先进行基本清理 + var result = Sanitize(input); + + // 移除不允许的标签 + result = Regex.Replace(result, @"]*>", m => + { + var tagName = m.Groups[1].Value; + if (!tags.Contains(tagName)) + { + return ""; + } + + // 保留标签但移除不允许的属性 + var tagContent = m.Value; + tagContent = Regex.Replace(tagContent, @"(\w+)\s*=\s*[""'][^""']*[""']", attrMatch => + { + var attrName = Regex.Match(attrMatch.Value, @"\w+").Value; + return attrs.Contains(attrName) ? attrMatch.Value : ""; + }); + + return tagContent; + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 移除所有HTML标签 + /// + public static string StripHtml(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return Regex.Replace(input, @"<[^>]*>", ""); + } + + /// + /// 验证是否包含XSS攻击代码 + /// + public static bool ContainsXss(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + return ScriptPattern.IsMatch(input) || + EventPattern.IsMatch(input) || + JavaScriptPattern.IsMatch(input) || + VbscriptPattern.IsMatch(input) || + DataUrlPattern.IsMatch(input) || + ExpressionPattern.IsMatch(input) || + SvgPattern.IsMatch(input) || + IframePattern.IsMatch(input) || + ObjectPattern.IsMatch(input) || + EmbedPattern.IsMatch(input); + } + + /// + /// 安全的URL编码 + /// + public static string SafeUrlEncode(string url) + { + if (string.IsNullOrEmpty(url)) + return url; + + // 检查危险的协议 + var lowerUrl = url.ToLower(); + if (lowerUrl.StartsWith("javascript:") || lowerUrl.StartsWith("vbscript:") || lowerUrl.StartsWith("data:")) + { + return ""; + } + + return Uri.EscapeUriString(url); + } + + /// + /// 验证URL是否安全 + /// + public static bool IsUrlSafe(string url) + { + if (string.IsNullOrEmpty(url)) + return false; + + var lowerUrl = url.ToLower(); + if (lowerUrl.StartsWith("javascript:") || lowerUrl.StartsWith("vbscript:") || lowerUrl.StartsWith("data:")) + { + return false; + } + + return Uri.TryCreate(url, UriKind.Absolute, out _); + } + + /// + /// 清理CSS样式(移除表达式和URL) + /// + public static string CleanCss(string css) + { + if (string.IsNullOrEmpty(css)) + return css; + + var result = css; + + // 移除expression + result = ExpressionPattern.Replace(result, ""); + + // 移除url() + result = Regex.Replace(result, @"url\s*\([^)]*\)", "", RegexOptions.IgnoreCase); + + // 移除behavior + result = Regex.Replace(result, @"behavior\s*:", "", RegexOptions.IgnoreCase); + + // 移除-moz-binding + result = Regex.Replace(result, @"-moz-binding\s*:", "", RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 安全的JSON字符串 + /// + public static string SafeJsonString(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(); + foreach (var c in input) + { + switch (c) + { + case '"': + result.Append("\\\""); + break; + case '\\': + result.Append("\\\\"); + break; + case '\b': + result.Append("\\b"); + break; + case '\f': + result.Append("\\f"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\t': + result.Append("\\t"); + break; + default: + if (c < 32) + { + result.Append($"\\u{(int)c:X4}"); + } + else + { + result.Append(c); + } + break; + } + } + return result.ToString(); + } + + /// + /// 属性值转义 + /// + public static string EscapeAttribute(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(); + foreach (var c in input) + { + switch (c) + { + case '<': + result.Append("<"); + break; + case '>': + result.Append(">"); + break; + case '"': + result.Append("""); + break; + case '\'': + result.Append("'"); + break; + case '&': + result.Append("&"); + break; + default: + result.Append(c); + break; + } + } + return result.ToString(); + } + } +} diff --git a/EasyTool.Core/ServiceCollectionExtensions.cs b/EasyTool.Core/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..3d31db9 --- /dev/null +++ b/EasyTool.Core/ServiceCollectionExtensions.cs @@ -0,0 +1,184 @@ +using System; +using System.Net.Http; +using EasyTool.CacheCategory; +using EasyTool.DatabaseCategory; +using EasyTool.QueueCategory; +using Microsoft.Extensions.DependencyInjection; + +namespace EasyTool +{ + /// + /// IServiceCollection 扩展方法 + /// 提供依赖注入注册 + /// + public static class ServiceCollectionExtensions + { + /// + /// 添加 EasyTool 核心服务 + /// + /// 服务集合 + /// 服务集合 + public static IServiceCollection AddEasyTool(this IServiceCollection services) + { + // 注册缓存服务 + services.AddSingleton(); + + // 注册 HttpClient 工厂 + services.AddHttpClient(); + + return services; + } + + /// + /// 添加 EasyTool 缓存服务 + /// + /// 服务集合 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolCache( + this IServiceCollection services, + Action? configure = null) + { + var options = new CacheOptions(); + configure?.Invoke(options); + + services.AddSingleton(sp => + { + return new MemoryCacheProvider( + options.CleanupInterval, + options.SizeLimit); + }); + + return services; + } + + /// + /// 添加 EasyTool Redis 缓存服务 + /// + /// 服务集合 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolRedisCache( + this IServiceCollection services, + Action configure) + { + var options = new RedisCacheOptions(); + configure(options); + + services.AddSingleton(sp => + { + return new RedisCacheProvider(options); + }); + + return services; + } + + /// + /// 添加 EasyTool 数据库连接池服务 + /// + /// 服务集合 + /// 连接字符串 + /// 数据库提供者工厂 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolConnectionPool( + this IServiceCollection services, + string connectionString, + System.Data.Common.DbProviderFactory providerFactory, + Action? configure = null) + { + var options = new ConnectionPoolOptions(); + configure?.Invoke(options); + + services.AddSingleton(sp => + { + return new ConnectionPool(connectionString, providerFactory, options); + }); + + return services; + } + + /// + /// 添加 EasyTool 消息队列服务 + /// + /// 消息类型 + /// 服务集合 + /// 消息处理器 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolMessageQueue( + this IServiceCollection services, + Func, System.Threading.Tasks.Task> handler, + Action? configure = null) + { + var options = new QueueCategory.MessageQueueOptions(); + configure?.Invoke(options); + + services.AddSingleton>(sp => + { + return new QueueCategory.MessageQueue(handler, options); + }); + + return services; + } + + /// + /// 添加 EasyTool HttpClient 服务 + /// + /// 服务集合 + /// 客户端名称 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolHttpClient( + this IServiceCollection services, + string name, + Action? configure = null) + { + services.AddHttpClient(name) + .ConfigurePrimaryHttpMessageHandler(() => + { + var builder = new NetCategory.HttpClientBuilder(); + configure?.Invoke(builder); + return new HttpClientHandler(); + }); + + return services; + } + + /// + /// 添加 EasyTool 多级缓存服务 + /// + /// 服务集合 + /// 分布式缓存提供者 + /// 本地缓存过期时间 + /// 服务集合 + public static IServiceCollection AddEasyToolMultiLevelCache( + this IServiceCollection services, + ICacheProvider? distributedCacheProvider = null, + TimeSpan? localCacheExpiration = null) + { + services.AddSingleton(sp => + { + return new MultiLevelCache(distributedCacheProvider, localCacheExpiration); + }); + + return services; + } + } + + /// + /// 缓存配置选项 + /// + public class CacheOptions + { + /// + /// 清理间隔 + /// + public TimeSpan? CleanupInterval { get; set; } + + /// + /// 大小限制 + /// + public long? SizeLimit { get; set; } + } +} diff --git a/EasyTool.Core/SystemCategory/AudioUtil.cs b/EasyTool.Core/SystemCategory/AudioUtil.cs new file mode 100644 index 0000000..d0d3813 --- /dev/null +++ b/EasyTool.Core/SystemCategory/AudioUtil.cs @@ -0,0 +1,244 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace EasyTool.SystemCategory +{ + /// + /// 音频设备控制工具类 + /// + public static class AudioUtil + { + #region 音量控制 + + /// + /// 获取主音量(0-100) + /// + public static int GetVolume() + { + try + { + var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator(); + var device = enumerator.GetDefaultAudioEndpoint(0, 0); // ERender = 0, eConsole = 0 + var iid = typeof(IAudioEndpointVolume).GUID; + device.Activate(ref iid, 0, IntPtr.Zero, out var interfacePtr); + var audioEndpoint = (IAudioEndpointVolume)Marshal.GetObjectForIUnknown(interfacePtr); + var volume = audioEndpoint.GetMasterVolumeLevelScalar(); + Marshal.Release(interfacePtr); + return (int)(volume * 100); + } + catch + { + return -1; + } + } + + /// + /// 设置主音量(0-100) + /// + public static bool SetVolume(int volume) + { + try + { + if (volume < 0 || volume > 100) + return false; + + var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator(); + var device = enumerator.GetDefaultAudioEndpoint(0, 0); + var iid = typeof(IAudioEndpointVolume).GUID; + device.Activate(ref iid, 0, IntPtr.Zero, out var interfacePtr); + var audioEndpoint = (IAudioEndpointVolume)Marshal.GetObjectForIUnknown(interfacePtr); + audioEndpoint.SetMasterVolumeLevelScalar(volume / 100f, Guid.Empty); + Marshal.Release(interfacePtr); + return true; + } + catch + { + return false; + } + } + + /// + /// 是否静音 + /// + public static bool IsMuted() + { + try + { + var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator(); + var device = enumerator.GetDefaultAudioEndpoint(0, 0); + var iid = typeof(IAudioEndpointVolume).GUID; + device.Activate(ref iid, 0, IntPtr.Zero, out var interfacePtr); + var audioEndpoint = (IAudioEndpointVolume)Marshal.GetObjectForIUnknown(interfacePtr); + var result = audioEndpoint.GetMute() != 0; + Marshal.Release(interfacePtr); + return result; + } + catch + { + return false; + } + } + + /// + /// 设置静音 + /// + public static bool SetMute(bool mute) + { + try + { + var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator(); + var device = enumerator.GetDefaultAudioEndpoint(0, 0); + var iid = typeof(IAudioEndpointVolume).GUID; + device.Activate(ref iid, 0, IntPtr.Zero, out var interfacePtr); + var audioEndpoint = (IAudioEndpointVolume)Marshal.GetObjectForIUnknown(interfacePtr); + audioEndpoint.SetMute(mute ? 1 : 0, Guid.Empty); + Marshal.Release(interfacePtr); + return true; + } + catch + { + return false; + } + } + + /// + /// 切换静音状态 + /// + public static bool ToggleMute() + { + return SetMute(!IsMuted()); + } + + /// + /// 音量增加 + /// + public static bool VolumeUp(int amount = 5) + { + var current = GetVolume(); + if (current < 0) return false; + return SetVolume(Math.Min(100, current + amount)); + } + + /// + /// 音量减少 + /// + public static bool VolumeDown(int amount = 5) + { + var current = GetVolume(); + if (current < 0) return false; + return SetVolume(Math.Max(0, current - amount)); + } + + #endregion + + #region COM接口 + + [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] + private class MMDeviceEnumerator { } + + [ComImport, Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IMMDeviceEnumerator + { + int EnumAudioEndpoints(int dataFlow, int stateMask, out IntPtr devices); + IMMDevice GetDefaultAudioEndpoint(int dataFlow, int role); + int GetDevice(string id, out IntPtr device); + int RegisterEndpointNotificationCallback(IntPtr client); + int UnregisterEndpointNotificationCallback(IntPtr client); + } + + [ComImport, Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IMMDevice + { + int Activate(ref Guid iid, int dwClsCtx, IntPtr activationParams, out IntPtr interfacePtr); + int OpenPropertyStore(int stgmAccess, out IntPtr properties); + int GetId(out string id); + int GetState(out int state); + } + + [ComImport, Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IAudioEndpointVolume + { + int RegisterControlChangeNotify(IntPtr notify); + int UnregisterControlChangeNotify(IntPtr notify); + int GetChannelCount(out int channelCount); + int SetMasterVolumeLevel(float level, Guid eventContext); + int SetMasterVolumeLevelScalar(float level, Guid eventContext); + float GetMasterVolumeLevel(); + float GetMasterVolumeLevelScalar(); + int SetChannelVolumeLevel(int channel, float level, Guid eventContext); + int SetChannelVolumeLevelScalar(int channel, float level, Guid eventContext); + float GetChannelVolumeLevel(int channel); + float GetChannelVolumeLevelScalar(int channel); + int SetMute(int mute, Guid eventContext); + int GetMute(); + } + + #endregion + } + + /// + /// 系统提示音 + /// + public static class SystemSoundUtil + { + /// + /// 播放系统提示音 + /// + public static void Beep() + { + Console.Beep(); + } + + /// + /// 播放指定频率和时长的提示音 + /// + public static void Beep(int frequency, int duration) + { + Console.Beep(frequency, duration); + } + + /// + /// 播放系统默认声音 + /// + public static void PlayDefault() + { + MessageBeep(0xFFFFFFFF); + } + + /// + /// 播放系统错误声音 + /// + public static void PlayError() + { + MessageBeep(0x10); + } + + /// + /// 播放系统问号声音 + /// + public static void PlayQuestion() + { + MessageBeep(0x20); + } + + /// + /// 播放系统警告声音 + /// + public static void PlayWarning() + { + MessageBeep(0x30); + } + + /// + /// 播放系统信息声音 + /// + public static void PlayInformation() + { + MessageBeep(0x40); + } + + [DllImport("user32.dll")] + private static extern bool MessageBeep(uint type); + } +} diff --git a/EasyTool.Core/SystemCategory/BatteryUtil.cs b/EasyTool.Core/SystemCategory/BatteryUtil.cs new file mode 100644 index 0000000..f6f5588 --- /dev/null +++ b/EasyTool.Core/SystemCategory/BatteryUtil.cs @@ -0,0 +1,359 @@ +using System; +using System.Runtime.InteropServices; + +namespace EasyTool.SystemCategory +{ + /// + /// 电池工具类 + /// 提供电池状态信息查询 + /// + public static class BatteryUtil + { + /// + /// 获取电池状态 + /// + /// 电池状态信息 + public static BatteryInfo GetStatus() + { + var status = new BatteryInfo(); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + GetWindowsBatteryStatus(status); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + GetLinuxBatteryStatus(status); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + GetMacOsBatteryStatus(status); + } + + return status; + } + + /// + /// 是否使用电池供电 + /// + /// 是否使用电池 + public static bool IsOnBattery() + { + var status = GetStatus(); + return status.PowerSource == PowerSource.Battery; + } + + /// + /// 是否正在充电 + /// + /// 是否正在充电 + public static bool IsCharging() + { + var status = GetStatus(); + return status.ChargeState == BatteryChargeState.Charging; + } + + /// + /// 电池电量是否低 + /// + /// 阈值(默认 20%) + /// 是否电量低 + public static bool IsLowBattery(double threshold = 20) + { + var status = GetStatus(); + return status.PercentRemaining <= threshold; + } + + /// + /// 获取剩余电量百分比 + /// + /// 剩余电量(0-100) + public static double GetBatteryPercent() + { + return GetStatus().PercentRemaining; + } + + /// + /// 获取剩余时间 + /// + /// 剩余时间 + public static TimeSpan GetRemainingTime() + { + return GetStatus().RemainingTime; + } + + #region Windows 实现 + + [StructLayout(LayoutKind.Sequential)] + private struct SYSTEM_POWER_STATUS + { + public byte ACLineStatus; + public byte BatteryFlag; + public byte BatteryLifePercent; + public byte SystemStatusFlag; + public int BatteryLifeTime; + public int BatteryFullLifeTime; + } + + [DllImport("kernel32.dll")] + private static extern bool GetSystemPowerStatus(ref SYSTEM_POWER_STATUS lpSystemPowerStatus); + + private static void GetWindowsBatteryStatus(BatteryInfo status) + { + var sps = new SYSTEM_POWER_STATUS(); + if (GetSystemPowerStatus(ref sps)) + { + status.PercentRemaining = sps.BatteryLifePercent == 255 ? 100 : sps.BatteryLifePercent; + status.PowerSource = sps.ACLineStatus switch + { + 0 => PowerSource.Battery, + 1 => PowerSource.AC, + _ => PowerSource.Unknown + }; + + status.ChargeState = sps.BatteryFlag switch + { + 1 => BatteryChargeState.Discharging, + 2 => BatteryChargeState.Charging, + 8 => BatteryChargeState.Charging, + 9 => BatteryChargeState.Full, + _ => BatteryChargeState.Unknown + }; + + if (sps.BatteryLifeTime > 0) + { + status.RemainingTime = TimeSpan.FromSeconds(sps.BatteryLifeTime); + } + + status.IsBatteryPresent = sps.BatteryFlag != 128; + } + } + + #endregion + + #region Linux 实现 + + private static void GetLinuxBatteryStatus(BatteryInfo status) + { + try + { + var batteryDir = "/sys/class/power_supply/BAT0"; + if (!System.IO.Directory.Exists(batteryDir)) + { + batteryDir = "/sys/class/power_supply/BAT1"; + } + + if (System.IO.Directory.Exists(batteryDir)) + { + // 读取电量百分比 + var capacityFile = System.IO.Path.Combine(batteryDir, "capacity"); + if (System.IO.File.Exists(capacityFile)) + { + var capacity = System.IO.File.ReadAllText(capacityFile).Trim(); + if (int.TryParse(capacity, out var percent)) + { + status.PercentRemaining = percent; + } + } + + // 读取状态 + var statusFile = System.IO.Path.Combine(batteryDir, "status"); + if (System.IO.File.Exists(statusFile)) + { + var batteryStatus = System.IO.File.ReadAllText(statusFile).Trim(); + status.ChargeState = batteryStatus switch + { + "Charging" => BatteryChargeState.Charging, + "Discharging" => BatteryChargeState.Discharging, + "Full" => BatteryChargeState.Full, + _ => BatteryChargeState.Unknown + }; + + status.PowerSource = batteryStatus == "Discharging" + ? PowerSource.Battery + : PowerSource.AC; + } + + status.IsBatteryPresent = true; + } + } + catch + { + // 忽略异常 + } + } + + #endregion + + #region macOS 实现 + + private static void GetMacOsBatteryStatus(BatteryInfo status) + { + try + { + var info = RunCommand("pmset", "-g batt"); + if (!string.IsNullOrEmpty(info)) + { + // 解析 pmset 输出 + // 示例: -InternalBattery-0 (id=...); 100%; charging; 0:00 remaining + if (info.Contains("charging")) + { + status.ChargeState = BatteryChargeState.Charging; + status.PowerSource = PowerSource.AC; + } + else if (info.Contains("discharging")) + { + status.ChargeState = BatteryChargeState.Discharging; + status.PowerSource = PowerSource.Battery; + } + else if (info.Contains("charged")) + { + status.ChargeState = BatteryChargeState.Full; + status.PowerSource = PowerSource.AC; + } + + // 提取百分比 + var percentIndex = info.IndexOf('%'); + if (percentIndex > 0) + { + var start = percentIndex - 1; + while (start > 0 && char.IsDigit(info[start - 1])) + start--; + + if (int.TryParse(info.Substring(start, percentIndex - start), out var percent)) + { + status.PercentRemaining = percent; + } + } + + status.IsBatteryPresent = info.Contains("Battery"); + } + } + catch + { + // 忽略异常 + } + } + + private static string RunCommand(string command, string args) + { + try + { + var psi = new System.Diagnostics.ProcessStartInfo + { + FileName = command, + Arguments = args, + RedirectStandardOutput = true, + UseShellExecute = false + }; + + using var process = System.Diagnostics.Process.Start(psi); + if (process != null) + { + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return output; + } + } + catch { } + + return string.Empty; + } + + #endregion + } + + /// + /// 电池状态信息 + /// + public class BatteryInfo + { + /// + /// 剩余电量百分比 + /// + public double PercentRemaining { get; set; } + + /// + /// 电源来源 + /// + public PowerSource PowerSource { get; set; } + + /// + /// 充电状态 + /// + public BatteryChargeState ChargeState { get; set; } + + /// + /// 剩余时间 + /// + public TimeSpan RemainingTime { get; set; } + + /// + /// 是否有电池 + /// + public bool IsBatteryPresent { get; set; } + + /// + /// 是否电量低(低于 20%) + /// + public bool IsLow => PercentRemaining <= 20; + + /// + /// 是否充满 + /// + public bool IsFull => PercentRemaining >= 95; + + public override string ToString() + { + return $"电量: {PercentRemaining:F1}%, 状态: {ChargeState}, 电源: {PowerSource}" + + (RemainingTime.TotalSeconds > 0 ? $", 剩余时间: {RemainingTime:hh\\:mm}" : ""); + } + } + + /// + /// 电源来源类型 + /// + public enum PowerSource + { + /// + /// 未知 + /// + Unknown, + + /// + /// 电池供电 + /// + Battery, + + /// + /// 交流电供电 + /// + AC + } + + /// + /// 电池充电状态 + /// + public enum BatteryChargeState + { + /// + /// 未知 + /// + Unknown, + + /// + /// 正在充电 + /// + Charging, + + /// + /// 正在放电 + /// + Discharging, + + /// + /// 已充满 + /// + Full + } +} \ No newline at end of file diff --git a/EasyTool.Core/SystemCategory/ClipboardUtil.cs b/EasyTool.Core/SystemCategory/ClipboardUtil.cs new file mode 100644 index 0000000..689e14d --- /dev/null +++ b/EasyTool.Core/SystemCategory/ClipboardUtil.cs @@ -0,0 +1,587 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.SystemCategory +{ + /// + /// 剪贴板工具类 + /// 提供剪贴板的读写操作功能 + /// + public static class ClipboardUtil + { + #region 文本操作 + + /// + /// 设置剪贴板文本 + /// + /// 文本内容 + public static void SetText(string text) + { + if (string.IsNullOrEmpty(text)) + { + Clear(); + return; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + WindowsClipboard.SetText(text); + } + else + { + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + } + + /// + /// 获取剪贴板文本 + /// + /// 文本内容 + public static string? GetText() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.GetText(); + } + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + + /// + /// 检查剪贴板是否包含文本 + /// + /// 是否包含文本 + public static bool ContainsText() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.ContainsText(); + } + return false; + } + + /// + /// 异步设置剪贴板文本 + /// + /// 文本内容 + public static async Task SetTextAsync(string text) + { + await Task.Run(() => SetText(text)); + } + + /// + /// 异步获取剪贴板文本 + /// + /// 文本内容 + public static async Task GetTextAsync() + { + return await Task.Run(() => GetText()); + } + + #endregion + + #region 图像操作 + + /// + /// 设置剪贴板图像数据 + /// + /// 图像数据(如 PNG、BMP 格式) + public static void SetImageData(byte[] imageData) + { + if (imageData == null || imageData.Length == 0) + throw new ArgumentNullException(nameof(imageData)); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + WindowsClipboard.SetImageData(imageData); + } + else + { + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + } + + /// + /// 设置剪贴板图像(从文件) + /// + /// 图像文件路径 + public static void SetImageFromFile(string imagePath) + { + if (!File.Exists(imagePath)) + throw new FileNotFoundException("图像文件不存在", imagePath); + + var imageData = File.ReadAllBytes(imagePath); + SetImageData(imageData); + } + + /// + /// 获取剪贴板图像数据 + /// + /// 图像数据 + public static byte[]? GetImageData() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.GetImageData(); + } + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + + /// + /// 检查剪贴板是否包含图像 + /// + /// 是否包含图像 + public static bool ContainsImage() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.ContainsImage(); + } + return false; + } + + /// + /// 保存剪贴板图像到文件 + /// + /// 文件路径 + /// 是否保存成功 + public static bool SaveImageToFile(string filePath) + { + var imageData = GetImageData(); + if (imageData == null || imageData.Length == 0) + return false; + + try + { + File.WriteAllBytes(filePath, imageData); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 文件操作 + + /// + /// 设置剪贴板文件列表 + /// + /// 文件路径列表 + public static void SetFiles(params string[] filePaths) + { + if (filePaths == null || filePaths.Length == 0) + { + Clear(); + return; + } + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + WindowsClipboard.SetFiles(filePaths); + } + else + { + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + } + + /// + /// 获取剪贴板文件列表 + /// + /// 文件路径列表 + public static string[]? GetFiles() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.GetFiles(); + } + throw new PlatformNotSupportedException("当前平台不支持剪贴板操作"); + } + + /// + /// 检查剪贴板是否包含文件 + /// + /// 是否包含文件 + public static bool ContainsFiles() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.ContainsFiles(); + } + return false; + } + + #endregion + + #region 通用操作 + + /// + /// 清空剪贴板 + /// + public static void Clear() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + WindowsClipboard.Clear(); + } + } + + /// + /// 检查剪贴板是否为空 + /// + /// 是否为空 + public static bool IsEmpty() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return WindowsClipboard.IsEmpty(); + } + return true; + } + + #endregion + } + + /// + /// Windows 平台剪贴板实现 + /// + internal static class WindowsClipboard + { + [DllImport("user32.dll", SetLastError = true)] + private static extern bool OpenClipboard(IntPtr hWndNewOwner); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool CloseClipboard(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool EmptyClipboard(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); + + [DllImport("user32.dll", SetLastError = true)] + private static extern IntPtr GetClipboardData(uint uFormat); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool IsClipboardFormatAvailable(uint uFormat); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr GlobalLock(IntPtr hMem); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool GlobalUnlock(IntPtr hMem); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern UIntPtr GlobalSize(IntPtr hMem); + + [DllImport("msvcrt.dll", SetLastError = true)] + private static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count); + + private const uint CF_TEXT = 1; + private const uint CF_UNICODETEXT = 13; + private const uint CF_BITMAP = 2; + private const uint CF_DIB = 8; + private const uint CF_HDROP = 15; + private const uint GMEM_MOVEABLE = 0x0002; + private const uint GMEM_ZEROINIT = 0x0040; + private const uint GHND = GMEM_MOVEABLE | GMEM_ZEROINIT; + + public static void SetText(string text) + { + if (!OpenClipboard(IntPtr.Zero)) + throw new InvalidOperationException("无法打开剪贴板"); + + try + { + EmptyClipboard(); + + var bytes = Encoding.Unicode.GetBytes(text + "\0"); + var hMem = GlobalAlloc(GHND, (UIntPtr)bytes.Length); + + if (hMem == IntPtr.Zero) + throw new InvalidOperationException("内存分配失败"); + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + throw new InvalidOperationException("内存锁定失败"); + + try + { + Marshal.Copy(bytes, 0, ptr, bytes.Length); + } + finally + { + GlobalUnlock(hMem); + } + + if (SetClipboardData(CF_UNICODETEXT, hMem) == IntPtr.Zero) + throw new InvalidOperationException("设置剪贴板数据失败"); + } + finally + { + CloseClipboard(); + } + } + + public static string? GetText() + { + if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) + return null; + + if (!OpenClipboard(IntPtr.Zero)) + return null; + + try + { + var hMem = GetClipboardData(CF_UNICODETEXT); + if (hMem == IntPtr.Zero) + return null; + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + return null; + + try + { + var size = GlobalSize(hMem); + if (size == UIntPtr.Zero) + return null; + + var bytes = new byte[(int)size]; + Marshal.Copy(ptr, bytes, 0, bytes.Length); + + var text = Encoding.Unicode.GetString(bytes); + var nullIndex = text.IndexOf('\0'); + return nullIndex >= 0 ? text.Substring(0, nullIndex) : text; + } + finally + { + GlobalUnlock(hMem); + } + } + finally + { + CloseClipboard(); + } + } + + public static bool ContainsText() + { + return IsClipboardFormatAvailable(CF_UNICODETEXT) || IsClipboardFormatAvailable(CF_TEXT); + } + + public static void SetImageData(byte[] imageData) + { + if (!OpenClipboard(IntPtr.Zero)) + throw new InvalidOperationException("无法打开剪贴板"); + + try + { + EmptyClipboard(); + + // 将图像数据放入剪贴板(使用 DIB 格式) + var hMem = GlobalAlloc(GHND, (UIntPtr)imageData.Length); + if (hMem == IntPtr.Zero) + throw new InvalidOperationException("内存分配失败"); + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + throw new InvalidOperationException("内存锁定失败"); + + try + { + Marshal.Copy(imageData, 0, ptr, imageData.Length); + } + finally + { + GlobalUnlock(hMem); + } + + if (SetClipboardData(CF_DIB, hMem) == IntPtr.Zero) + throw new InvalidOperationException("设置剪贴板图像失败"); + } + finally + { + CloseClipboard(); + } + } + + public static byte[]? GetImageData() + { + if (!IsClipboardFormatAvailable(CF_DIB) && !IsClipboardFormatAvailable(CF_BITMAP)) + return null; + + if (!OpenClipboard(IntPtr.Zero)) + return null; + + try + { + var hMem = GetClipboardData(CF_DIB); + if (hMem == IntPtr.Zero) + return null; + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + return null; + + try + { + var size = GlobalSize(hMem); + var data = new byte[(int)size]; + Marshal.Copy(ptr, data, 0, (int)size); + return data; + } + finally + { + GlobalUnlock(hMem); + } + } + finally + { + CloseClipboard(); + } + } + + public static bool ContainsImage() + { + return IsClipboardFormatAvailable(CF_BITMAP) || IsClipboardFormatAvailable(CF_DIB); + } + + public static void SetFiles(string[] filePaths) + { + if (!OpenClipboard(IntPtr.Zero)) + throw new InvalidOperationException("无法打开剪贴板"); + + try + { + EmptyClipboard(); + + // 构建 DROP 结构 + var dropList = new DROPFILES(); + var filePathsStr = string.Join("\0", filePaths) + "\0\0"; + var bytes = Encoding.Unicode.GetBytes(filePathsStr); + + dropList.pFiles = Marshal.SizeOf(typeof(DROPFILES)); + dropList.fWide = true; + + var totalSize = Marshal.SizeOf(typeof(DROPFILES)) + bytes.Length; + var hMem = GlobalAlloc(GHND, (UIntPtr)totalSize); + + if (hMem == IntPtr.Zero) + throw new InvalidOperationException("内存分配失败"); + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + throw new InvalidOperationException("内存锁定失败"); + + try + { + // 写入 DROPFILES 结构 + Marshal.StructureToPtr(dropList, ptr, false); + // 写入文件路径 + Marshal.Copy(bytes, 0, ptr + Marshal.SizeOf(typeof(DROPFILES)), bytes.Length); + } + finally + { + GlobalUnlock(hMem); + } + + if (SetClipboardData(CF_HDROP, hMem) == IntPtr.Zero) + throw new InvalidOperationException("设置剪贴板文件列表失败"); + } + finally + { + CloseClipboard(); + } + } + + public static string[]? GetFiles() + { + if (!IsClipboardFormatAvailable(CF_HDROP)) + return null; + + if (!OpenClipboard(IntPtr.Zero)) + return null; + + try + { + var hMem = GetClipboardData(CF_HDROP); + if (hMem == IntPtr.Zero) + return null; + + var ptr = GlobalLock(hMem); + if (ptr == IntPtr.Zero) + return null; + + try + { + var dropFiles = Marshal.PtrToStructure(ptr); + var filesPtr = ptr + dropFiles.pFiles; + var size = GlobalSize(hMem); + var filesSize = (int)size - dropFiles.pFiles; + + if (filesSize <= 0) + return Array.Empty(); + + var bytes = new byte[filesSize]; + Marshal.Copy(filesPtr, bytes, 0, filesSize); + + var filesStr = Encoding.Unicode.GetString(bytes); + var files = filesStr.Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); + return files; + } + finally + { + GlobalUnlock(hMem); + } + } + finally + { + CloseClipboard(); + } + } + + public static bool ContainsFiles() + { + return IsClipboardFormatAvailable(CF_HDROP); + } + + public static void Clear() + { + if (OpenClipboard(IntPtr.Zero)) + { + EmptyClipboard(); + CloseClipboard(); + } + } + + public static bool IsEmpty() + { + return !ContainsText() && !ContainsImage() && !ContainsFiles(); + } + + [StructLayout(LayoutKind.Sequential)] + private struct DROPFILES + { + public int pFiles; + public POINT pt; + public bool fNC; + public bool fWide; + } + + [StructLayout(LayoutKind.Sequential)] + private struct POINT + { + public int X; + public int Y; + } + } +} diff --git a/EasyTool.Core/SystemCategory/HardwareInfoUtil.cs b/EasyTool.Core/SystemCategory/HardwareInfoUtil.cs new file mode 100644 index 0000000..117f91e --- /dev/null +++ b/EasyTool.Core/SystemCategory/HardwareInfoUtil.cs @@ -0,0 +1,450 @@ +using System; +using System.Collections.Generic; +using System.Management; +using System.Runtime.InteropServices; + +namespace EasyTool.SystemCategory +{ + /// + /// 硬件信息工具类 + /// + public static class HardwareInfoUtil + { + /// + /// 获取CPU信息 + /// + public static CpuInfo GetCpuInfo() + { + var info = new CpuInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); + foreach (ManagementObject obj in searcher.Get()) + { + info.Name = obj["Name"]?.ToString()?.Trim() ?? ""; + info.Manufacturer = obj["Manufacturer"]?.ToString() ?? ""; + info.MaxClockSpeed = Convert.ToInt32(obj["MaxClockSpeed"]); + info.NumberOfCores = Convert.ToInt32(obj["NumberOfCores"]); + info.NumberOfLogicalProcessors = Convert.ToInt32(obj["NumberOfLogicalProcessors"]); + info.L2CacheSize = Convert.ToInt32(obj["L2CacheSize"]); + info.L3CacheSize = Convert.ToInt32(obj["L3CacheSize"]); + info.Architecture = obj["Architecture"]?.ToString() ?? ""; + info.ProcessorId = obj["ProcessorId"]?.ToString() ?? ""; + break; + } + } + catch + { + // 在某些环境可能无法访问WMI + } + + return info; + } + + /// + /// 获取内存信息 + /// + public static MemoryInfo GetMemoryInfo() + { + var info = new MemoryInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory"); + long totalCapacity = 0; + var modules = new List(); + + foreach (ManagementObject obj in searcher.Get()) + { + var capacity = Convert.ToInt64(obj["Capacity"]); + totalCapacity += capacity; + + modules.Add(new MemoryModule + { + Capacity = capacity, + Speed = Convert.ToInt32(obj["Speed"]), + Manufacturer = obj["Manufacturer"]?.ToString() ?? "", + PartNumber = obj["PartNumber"]?.ToString()?.Trim() ?? "", + MemoryType = obj["MemoryType"]?.ToString() ?? "" + }); + } + + info.TotalCapacity = totalCapacity; + info.Modules = modules; + } + catch + { + } + + // 使用GC获取可用内存 + try + { +#if NET5_0_OR_GREATER + var gcMemoryInfo = GC.GetGCMemoryInfo(); +#if NET10_0_OR_GREATER + // .NET 10+ 使用 TotalAvailableMemoryBytes 属性 + info.AvailableMemory = gcMemoryInfo.TotalAvailableMemoryBytes; +#else + info.AvailableMemory = gcMemoryInfo.TotalAvailableMemoryPages * Environment.SystemPageSize; +#endif +#else + // 对于 netstandard2.1,使用另一种方式获取可用内存 + var memCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); + info.AvailableMemory = (long)memCounter.NextValue(); +#endif + } + catch + { + // 如果无法获取,使用0作为默认值 + info.AvailableMemory = 0; + } + + return info; + } + + /// + /// 获取磁盘信息 + /// + public static List GetDiskInfo() + { + var disks = new List(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk"); + foreach (ManagementObject obj in searcher.Get()) + { + disks.Add(new DiskInfo + { + DeviceId = obj["DeviceID"]?.ToString() ?? "", + VolumeName = obj["VolumeName"]?.ToString() ?? "", + FileSystem = obj["FileSystem"]?.ToString() ?? "", + Size = Convert.ToInt64(obj["Size"]), + FreeSpace = Convert.ToInt64(obj["FreeSpace"]), + DriveType = Convert.ToInt32(obj["DriveType"]) + }); + } + } + catch + { + } + + return disks; + } + + /// + /// 获取显卡信息 + /// + public static List GetGpuInfo() + { + var gpus = new List(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController"); + foreach (ManagementObject obj in searcher.Get()) + { + gpus.Add(new GpuInfo + { + Name = obj["Name"]?.ToString() ?? "", + DriverVersion = obj["DriverVersion"]?.ToString() ?? "", + DriverDate = obj["DriverDate"]?.ToString() ?? "", + VideoProcessor = obj["VideoProcessor"]?.ToString() ?? "", + AdapterRAM = Convert.ToInt64(obj["AdapterRAM"]), + CurrentHorizontalResolution = Convert.ToInt32(obj["CurrentHorizontalResolution"]), + CurrentVerticalResolution = Convert.ToInt32(obj["CurrentVerticalResolution"]), + CurrentRefreshRate = Convert.ToInt32(obj["CurrentRefreshRate"]) + }); + } + } + catch + { + } + + return gpus; + } + + /// + /// 获取主板信息 + /// + public static MotherboardInfo GetMotherboardInfo() + { + var info = new MotherboardInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard"); + foreach (ManagementObject obj in searcher.Get()) + { + info.Manufacturer = obj["Manufacturer"]?.ToString() ?? ""; + info.Product = obj["Product"]?.ToString() ?? ""; + info.SerialNumber = obj["SerialNumber"]?.ToString() ?? ""; + info.Version = obj["Version"]?.ToString() ?? ""; + break; + } + } + catch + { + } + + return info; + } + + /// + /// 获取BIOS信息 + /// + public static BiosInfo GetBiosInfo() + { + var info = new BiosInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS"); + foreach (ManagementObject obj in searcher.Get()) + { + info.Manufacturer = obj["Manufacturer"]?.ToString() ?? ""; + info.Version = obj["Version"]?.ToString() ?? ""; + info.ReleaseDate = obj["ReleaseDate"]?.ToString() ?? ""; + info.SerialNumber = obj["SerialNumber"]?.ToString() ?? ""; + info.SMBIOSBIOSVersion = obj["SMBIOSBIOSVersion"]?.ToString() ?? ""; + break; + } + } + catch + { + } + + return info; + } + + /// + /// 获取操作系统信息 + /// + public static OsInfo GetOsInfo() + { + var info = new OsInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); + foreach (ManagementObject obj in searcher.Get()) + { + info.Caption = obj["Caption"]?.ToString() ?? ""; + info.Version = obj["Version"]?.ToString() ?? ""; + info.BuildNumber = obj["BuildNumber"]?.ToString() ?? ""; + info.OSArchitecture = obj["OSArchitecture"]?.ToString() ?? ""; + info.SerialNumber = obj["SerialNumber"]?.ToString() ?? ""; + info.InstallDate = obj["InstallDate"]?.ToString() ?? ""; + info.LastBootUpTime = obj["LastBootUpTime"]?.ToString() ?? ""; + info.TotalVisibleMemorySize = Convert.ToInt64(obj["TotalVisibleMemorySize"]) * 1024; + info.FreePhysicalMemory = Convert.ToInt64(obj["FreePhysicalMemory"]) * 1024; + break; + } + } + catch + { + } + + return info; + } + + /// + /// 获取网络适配器信息 + /// + public static List GetNetworkAdapters() + { + var adapters = new List(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE NetEnabled = true"); + foreach (ManagementObject obj in searcher.Get()) + { + adapters.Add(new NetworkAdapterInfo + { + Name = obj["Name"]?.ToString() ?? "", + Description = obj["Description"]?.ToString() ?? "", + MACAddress = obj["MACAddress"]?.ToString() ?? "", + Speed = Convert.ToInt64(obj["Speed"]), + NetConnectionStatus = obj["NetConnectionStatus"]?.ToString() ?? "", + AdapterType = obj["AdapterType"]?.ToString() ?? "" + }); + } + } + catch + { + } + + return adapters; + } + + /// + /// 获取计算机系统信息 + /// + public static ComputerSystemInfo GetComputerSystemInfo() + { + var info = new ComputerSystemInfo(); + + try + { + using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"); + foreach (ManagementObject obj in searcher.Get()) + { + info.Manufacturer = obj["Manufacturer"]?.ToString() ?? ""; + info.Model = obj["Model"]?.ToString() ?? ""; + info.TotalPhysicalMemory = Convert.ToInt64(obj["TotalPhysicalMemory"]); + info.NumberOfProcessors = Convert.ToInt32(obj["NumberOfProcessors"]); + info.NumberOfLogicalProcessors = Convert.ToInt32(obj["NumberOfLogicalProcessors"]); + info.SystemType = obj["SystemType"]?.ToString() ?? ""; + info.PCSystemType = obj["PCSystemType"]?.ToString() ?? ""; + break; + } + } + catch + { + } + + return info; + } + } + + #region 信息类 + + public class CpuInfo + { + public string Name { get; set; } = ""; + public string Manufacturer { get; set; } = ""; + public int MaxClockSpeed { get; set; } + public int NumberOfCores { get; set; } + public int NumberOfLogicalProcessors { get; set; } + public int L2CacheSize { get; set; } + public int L3CacheSize { get; set; } + public string Architecture { get; set; } = ""; + public string ProcessorId { get; set; } = ""; + + public double MaxClockSpeedGHz => MaxClockSpeed / 1000.0; + } + + public class MemoryInfo + { + public long TotalCapacity { get; set; } + public long AvailableMemory { get; set; } + public List Modules { get; set; } = new(); + + public double TotalCapacityGB => TotalCapacity / (1024.0 * 1024 * 1024); + public double UsedMemory => TotalCapacity - AvailableMemory; + public double UsedMemoryGB => UsedMemory / (1024.0 * 1024 * 1024); + public double UsagePercent => TotalCapacity > 0 ? (double)UsedMemory / TotalCapacity * 100 : 0; + } + + public class MemoryModule + { + public long Capacity { get; set; } + public int Speed { get; set; } + public string Manufacturer { get; set; } = ""; + public string PartNumber { get; set; } = ""; + public string MemoryType { get; set; } = ""; + + public double CapacityGB => Capacity / (1024.0 * 1024 * 1024); + } + + public class DiskInfo + { + public string DeviceId { get; set; } = ""; + public string VolumeName { get; set; } = ""; + public string FileSystem { get; set; } = ""; + public long Size { get; set; } + public long FreeSpace { get; set; } + public int DriveType { get; set; } + + public double SizeGB => Size / (1024.0 * 1024 * 1024); + public double FreeSpaceGB => FreeSpace / (1024.0 * 1024 * 1024); + public double UsedSpace => Size - FreeSpace; + public double UsedSpaceGB => UsedSpace / (1024.0 * 1024 * 1024); + public double UsagePercent => Size > 0 ? (double)UsedSpace / Size * 100 : 0; + public string DriveTypeName => DriveType switch + { + 1 => "可移动磁盘", + 2 => "本地磁盘", + 3 => "网络驱动器", + 4 => "光盘驱动器", + 5 => "RAM磁盘", + _ => "未知" + }; + } + + public class GpuInfo + { + public string Name { get; set; } = ""; + public string DriverVersion { get; set; } = ""; + public string DriverDate { get; set; } = ""; + public string VideoProcessor { get; set; } = ""; + public long AdapterRAM { get; set; } + public int CurrentHorizontalResolution { get; set; } + public int CurrentVerticalResolution { get; set; } + public int CurrentRefreshRate { get; set; } + + public double AdapterRAMGB => AdapterRAM / (1024.0 * 1024 * 1024); + public string Resolution => $"{CurrentHorizontalResolution} x {CurrentVerticalResolution}"; + } + + public class MotherboardInfo + { + public string Manufacturer { get; set; } = ""; + public string Product { get; set; } = ""; + public string SerialNumber { get; set; } = ""; + public string Version { get; set; } = ""; + } + + public class BiosInfo + { + public string Manufacturer { get; set; } = ""; + public string Version { get; set; } = ""; + public string ReleaseDate { get; set; } = ""; + public string SerialNumber { get; set; } = ""; + public string SMBIOSBIOSVersion { get; set; } = ""; + } + + public class OsInfo + { + public string Caption { get; set; } = ""; + public string Version { get; set; } = ""; + public string BuildNumber { get; set; } = ""; + public string OSArchitecture { get; set; } = ""; + public string SerialNumber { get; set; } = ""; + public string InstallDate { get; set; } = ""; + public string LastBootUpTime { get; set; } = ""; + public long TotalVisibleMemorySize { get; set; } + public long FreePhysicalMemory { get; set; } + + public string DisplayName => $"{Caption} {OSArchitecture}"; + } + + public class NetworkAdapterInfo + { + public string Name { get; set; } = ""; + public string Description { get; set; } = ""; + public string MACAddress { get; set; } = ""; + public long Speed { get; set; } + public string NetConnectionStatus { get; set; } = ""; + public string AdapterType { get; set; } = ""; + + public double SpeedMbps => Speed / 1_000_000.0; + } + + public class ComputerSystemInfo + { + public string Manufacturer { get; set; } = ""; + public string Model { get; set; } = ""; + public long TotalPhysicalMemory { get; set; } + public int NumberOfProcessors { get; set; } + public int NumberOfLogicalProcessors { get; set; } + public string SystemType { get; set; } = ""; + public string PCSystemType { get; set; } = ""; + + public double TotalPhysicalMemoryGB => TotalPhysicalMemory / (1024.0 * 1024 * 1024); + } + + #endregion +} diff --git a/EasyTool.Core/SystemCategory/HotKeyUtil.cs b/EasyTool.Core/SystemCategory/HotKeyUtil.cs new file mode 100644 index 0000000..c02c8d5 --- /dev/null +++ b/EasyTool.Core/SystemCategory/HotKeyUtil.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading; + +namespace EasyTool.SystemCategory +{ + /// + /// 全局热键工具类 + /// + public class HotKeyUtil : IDisposable + { + private readonly Dictionary _registrations = new(); + private int _nextId = 1; + private bool _disposed; + private readonly object _lock = new(); + + /// + /// 热键按下事件 + /// + public event EventHandler? HotKeyPressed; + + /// + /// 注册全局热键 + /// + /// 修饰键 + /// 按键 + /// 触发动作(可选) + /// 热键ID + public int Register(HotKeyModifiers modifiers, VirtualKeyCode key, Action? action = null) + { + lock (_lock) + { + var id = _nextId++; + + // 获取活动窗口句柄 + var hWnd = GetActiveWindow(); + if (hWnd == IntPtr.Zero) + { + hWnd = GetConsoleWindow(); + } + + if (!RegisterHotKey(hWnd, id, (int)modifiers, (int)key)) + { + var error = Marshal.GetLastWin32Error(); + throw new InvalidOperationException($"注册热键失败,错误码: {error}"); + } + + _registrations[id] = new HotKeyRegistration + { + Id = id, + Modifiers = modifiers, + Key = key, + Action = action, + WindowHandle = hWnd + }; + + return id; + } + } + + /// + /// 注销热键 + /// + public bool Unregister(int id) + { + lock (_lock) + { + if (!_registrations.TryGetValue(id, out var registration)) + return false; + + var result = UnregisterHotKey(registration.WindowHandle, id); + _registrations.Remove(id); + return result; + } + } + + /// + /// 注销所有热键 + /// + public void UnregisterAll() + { + lock (_lock) + { + foreach (var registration in _registrations.Values) + { + UnregisterHotKey(registration.WindowHandle, registration.Id); + } + _registrations.Clear(); + } + } + + /// + /// 处理Windows消息(在消息循环中调用) + /// + public bool ProcessMessage(IntPtr wParam, IntPtr lParam) + { + var id = wParam.ToInt32(); + + if (_registrations.TryGetValue(id, out var registration)) + { + var args = new HotKeyEventArgs(registration.Id, registration.Modifiers, registration.Key); + HotKeyPressed?.Invoke(this, args); + registration.Action?.Invoke(); + return true; + } + + return false; + } + + /// + /// 开始消息循环(阻塞) + /// + public void StartMessageLoop() + { + while (!_disposed) + { + if (GetMessage(out var msg, IntPtr.Zero, 0, 0)) + { + if (msg.message == WM_HOTKEY) + { + ProcessMessage(msg.wParam, msg.lParam); + } + else + { + TranslateMessage(ref msg); + DispatchMessage(ref msg); + } + } + } + } + + /// + /// 获取已注册的热键列表 + /// + public IReadOnlyList GetRegisteredHotKeys() + { + lock (_lock) + { + return _registrations.Values + .Select(r => new HotKeyInfo(r.Id, r.Modifiers, r.Key)) + .ToList() + .AsReadOnly(); + } + } + + public void Dispose() + { + if (_disposed) + return; + + UnregisterAll(); + _disposed = true; + } + + #region P/Invoke + + private const int WM_HOTKEY = 0x0312; + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + [DllImport("user32.dll")] + private static extern IntPtr GetActiveWindow(); + + [DllImport("kernel32.dll")] + private static extern IntPtr GetConsoleWindow(); + + [DllImport("user32.dll")] + private static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); + + [DllImport("user32.dll")] + private static extern bool TranslateMessage(ref MSG lpMsg); + + [DllImport("user32.dll")] + private static extern IntPtr DispatchMessage(ref MSG lpMsg); + + [StructLayout(LayoutKind.Sequential)] + private struct MSG + { + public IntPtr hWnd; + public int message; + public IntPtr wParam; + public IntPtr lParam; + public uint time; + public POINT pt; + } + + [StructLayout(LayoutKind.Sequential)] + private struct POINT + { + public int X; + public int Y; + } + + #endregion + } + + /// + /// 热键修饰键 + /// + [Flags] + public enum HotKeyModifiers + { + None = 0, + Alt = 1, + Control = 2, + Shift = 4, + Windows = 8 + } + + /// + /// 热键注册信息 + /// + internal class HotKeyRegistration + { + public int Id { get; set; } + public HotKeyModifiers Modifiers { get; set; } + public VirtualKeyCode Key { get; set; } + public Action? Action { get; set; } + public IntPtr WindowHandle { get; set; } + } + + /// + /// 热键信息 + /// + public class HotKeyInfo + { + /// + /// 热键ID + /// + public int Id { get; } + + /// + /// 修饰键 + /// + public HotKeyModifiers Modifiers { get; } + + /// + /// 按键 + /// + public VirtualKeyCode Key { get; } + + public HotKeyInfo(int id, HotKeyModifiers modifiers, VirtualKeyCode key) + { + Id = id; + Modifiers = modifiers; + Key = key; + } + + public override string ToString() + { + var parts = new List(); + if (Modifiers.HasFlag(HotKeyModifiers.Control)) parts.Add("Ctrl"); + if (Modifiers.HasFlag(HotKeyModifiers.Alt)) parts.Add("Alt"); + if (Modifiers.HasFlag(HotKeyModifiers.Shift)) parts.Add("Shift"); + if (Modifiers.HasFlag(HotKeyModifiers.Windows)) parts.Add("Win"); + parts.Add(Key.ToString()); + return string.Join(" + ", parts); + } + } + + /// + /// 热键事件参数 + /// + public class HotKeyEventArgs : EventArgs + { + public int Id { get; } + public HotKeyModifiers Modifiers { get; } + public VirtualKeyCode Key { get; } + + public HotKeyEventArgs(int id, HotKeyModifiers modifiers, VirtualKeyCode key) + { + Id = id; + Modifiers = modifiers; + Key = key; + } + } +} diff --git a/EasyTool.Core/SystemCategory/KeyboardUtil.cs b/EasyTool.Core/SystemCategory/KeyboardUtil.cs new file mode 100644 index 0000000..f7d23d1 --- /dev/null +++ b/EasyTool.Core/SystemCategory/KeyboardUtil.cs @@ -0,0 +1,325 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading; + +namespace EasyTool.SystemCategory +{ + /// + /// 键盘工具类 + /// + public static class KeyboardUtil + { + #region 键盘状态检测 + + /// + /// 检测按键是否按下 + /// + public static bool IsKeyDown(VirtualKeyCode keyCode) + { + return (GetKeyState((int)keyCode) & 0x8000) != 0; + } + + /// + /// 检测按键是否切换(如CapsLock、NumLock) + /// + public static bool IsKeyToggled(VirtualKeyCode keyCode) + { + return (GetKeyState((int)keyCode) & 0x0001) != 0; + } + + /// + /// 检测CapsLock是否开启 + /// + public static bool IsCapsLockOn() + { + return IsKeyToggled(VirtualKeyCode.CapsLock); + } + + /// + /// 检测NumLock是否开启 + /// + public static bool IsNumLockOn() + { + return IsKeyToggled(VirtualKeyCode.NumLock); + } + + /// + /// 检测ScrollLock是否开启 + /// + public static bool IsScrollLockOn() + { + return IsKeyToggled(VirtualKeyCode.ScrollLock); + } + + /// + /// 检测Shift是否按下 + /// + public static bool IsShiftDown() + { + return IsKeyDown(VirtualKeyCode.Shift) || IsKeyDown(VirtualKeyCode.LeftShift) || IsKeyDown(VirtualKeyCode.RightShift); + } + + /// + /// 检测Ctrl是否按下 + /// + public static bool IsCtrlDown() + { + return IsKeyDown(VirtualKeyCode.Control) || IsKeyDown(VirtualKeyCode.LeftControl) || IsKeyDown(VirtualKeyCode.RightControl); + } + + /// + /// 检测Alt是否按下 + /// + public static bool IsAltDown() + { + return IsKeyDown(VirtualKeyCode.Alt) || IsKeyDown(VirtualKeyCode.LeftMenu) || IsKeyDown(VirtualKeyCode.RightMenu); + } + + /// + /// 检测Windows键是否按下 + /// + public static bool IsWindowsKeyDown() + { + return IsKeyDown(VirtualKeyCode.LeftWindows) || IsKeyDown(VirtualKeyCode.RightWindows); + } + + #endregion + + #region 模拟按键 + + /// + /// 模拟按键按下 + /// + public static void KeyDown(VirtualKeyCode keyCode) + { + keybd_event((byte)keyCode, 0, KEYEVENTF_KEYDOWN, 0); + } + + /// + /// 模拟按键释放 + /// + public static void KeyUp(VirtualKeyCode keyCode) + { + keybd_event((byte)keyCode, 0, KEYEVENTF_KEYUP, 0); + } + + /// + /// 模拟按键(按下并释放) + /// + public static void KeyPress(VirtualKeyCode keyCode) + { + KeyDown(keyCode); + Thread.Sleep(50); + KeyUp(keyCode); + } + + /// + /// 模拟快捷键 + /// + public static void SendHotKey(params VirtualKeyCode[] keys) + { + if (keys == null || keys.Length == 0) + return; + + // 按下所有键 + foreach (var key in keys) + { + KeyDown(key); + Thread.Sleep(50); + } + + // 释放所有键(逆序) + for (int i = keys.Length - 1; i >= 0; i--) + { + KeyUp(keys[i]); + Thread.Sleep(50); + } + } + + /// + /// 模拟文本输入 + /// + public static void SendText(string text) + { + foreach (var c in text) + { + SendChar(c); + Thread.Sleep(50); + } + } + + private static void SendChar(char c) + { + var inputs = new INPUT[2]; + + inputs[0].type = INPUT_KEYBOARD; + inputs[0].u.ki.wVk = 0; + inputs[0].u.ki.wScan = c; + inputs[0].u.ki.dwFlags = KEYEVENTF_UNICODE; + + inputs[1].type = INPUT_KEYBOARD; + inputs[1].u.ki.wVk = 0; + inputs[1].u.ki.wScan = c; + inputs[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP; + + SendInput(2, inputs, INPUT.Size); + } + + #endregion + + #region P/Invoke + + private const int KEYEVENTF_KEYDOWN = 0x0000; + private const int KEYEVENTF_KEYUP = 0x0002; + private const int KEYEVENTF_UNICODE = 0x0004; + private const int INPUT_KEYBOARD = 1; + + [DllImport("user32.dll")] + private static extern short GetKeyState(int nVirtKey); + + [DllImport("user32.dll")] + private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); + + [DllImport("user32.dll", SetLastError = true)] + private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); + + [StructLayout(LayoutKind.Sequential)] + private struct INPUT + { + public int type; + public InputUnion u; + + public static int Size => Marshal.SizeOf(typeof(INPUT)); + } + + [StructLayout(LayoutKind.Explicit)] + private struct InputUnion + { + [FieldOffset(0)] + public KEYBDINPUT ki; + } + + [StructLayout(LayoutKind.Sequential)] + private struct KEYBDINPUT + { + public ushort wVk; + public ushort wScan; + public uint dwFlags; + public uint time; + public IntPtr dwExtraInfo; + } + + #endregion + } + + /// + /// 虚拟键码 + /// + public enum VirtualKeyCode : short + { + LeftButton = 0x01, + RightButton = 0x02, + Cancel = 0x03, + MiddleButton = 0x04, + Back = 0x08, + Tab = 0x09, + Clear = 0x0C, + Return = 0x0D, + Shift = 0x10, + Control = 0x11, + Alt = 0x12, + Pause = 0x13, + CapsLock = 0x14, + Escape = 0x1B, + Space = 0x20, + PageUp = 0x21, + PageDown = 0x22, + End = 0x23, + Home = 0x24, + Left = 0x25, + Up = 0x26, + Right = 0x27, + Down = 0x28, + PrintScreen = 0x2A, + Insert = 0x2D, + Delete = 0x2E, + D0 = 0x30, + D1 = 0x31, + D2 = 0x32, + D3 = 0x33, + D4 = 0x34, + D5 = 0x35, + D6 = 0x36, + D7 = 0x37, + D8 = 0x38, + D9 = 0x39, + A = 0x41, + B = 0x42, + C = 0x43, + D = 0x44, + E = 0x45, + F = 0x46, + G = 0x47, + H = 0x48, + I = 0x49, + J = 0x4A, + K = 0x4B, + L = 0x4C, + M = 0x4D, + N = 0x4E, + O = 0x4F, + P = 0x50, + Q = 0x51, + R = 0x52, + S = 0x53, + T = 0x54, + U = 0x55, + V = 0x56, + W = 0x57, + X = 0x58, + Y = 0x59, + Z = 0x5A, + LeftWindows = 0x5B, + RightWindows = 0x5C, + Apps = 0x5D, + NumLock = 0x90, + ScrollLock = 0x91, + F1 = 0x70, + F2 = 0x71, + F3 = 0x72, + F4 = 0x73, + F5 = 0x74, + F6 = 0x75, + F7 = 0x76, + F8 = 0x77, + F9 = 0x78, + F10 = 0x79, + F11 = 0x7A, + F12 = 0x7B, + NumPad0 = 0x60, + NumPad1 = 0x61, + NumPad2 = 0x62, + NumPad3 = 0x63, + NumPad4 = 0x64, + NumPad5 = 0x65, + NumPad6 = 0x66, + NumPad7 = 0x67, + NumPad8 = 0x68, + NumPad9 = 0x69, + Multiply = 0x6A, + Add = 0x6B, + Separator = 0x6C, + Subtract = 0x6D, + Decimal = 0x6E, + Divide = 0x6F, + LeftShift = 0xA0, + RightShift = 0xA1, + LeftControl = 0xA2, + RightControl = 0xA3, + LeftMenu = 0xA4, + RightMenu = 0xA5, + VolumeMute = 0xAD, + VolumeDown = 0xAE, + VolumeUp = 0xAF + } +} diff --git a/EasyTool.Core/SystemCategory/MouseUtil.cs b/EasyTool.Core/SystemCategory/MouseUtil.cs new file mode 100644 index 0000000..5edc6b4 --- /dev/null +++ b/EasyTool.Core/SystemCategory/MouseUtil.cs @@ -0,0 +1,314 @@ +using System; +using System.Runtime.InteropServices; + +namespace EasyTool.SystemCategory +{ + /// + /// 鼠标工具类 + /// + public static class MouseUtil + { + #region 鼠标位置 + + /// + /// 获取鼠标位置 + /// + public static (int X, int Y) GetPosition() + { + GetCursorPos(out var point); + return (point.X, point.Y); + } + + /// + /// 设置鼠标位置 + /// + public static void SetPosition(int x, int y) + { + SetCursorPos(x, y); + } + + /// + /// 移动鼠标到指定位置(平滑移动) + /// + public static void MoveTo(int x, int y, int steps = 10, int delayMs = 10) + { + var (currentX, currentY) = GetPosition(); + var stepX = (x - currentX) / (double)steps; + var stepY = (y - currentY) / (double)steps; + + for (int i = 1; i <= steps; i++) + { + SetPosition((int)(currentX + stepX * i), (int)(currentY + stepY * i)); + System.Threading.Thread.Sleep(delayMs); + } + } + + #endregion + + #region 鼠标点击 + + /// + /// 左键单击 + /// + public static void LeftClick() + { + mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); + System.Threading.Thread.Sleep(50); + mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); + } + + /// + /// 左键双击 + /// + public static void LeftDoubleClick() + { + LeftClick(); + System.Threading.Thread.Sleep(100); + LeftClick(); + } + + /// + /// 右键单击 + /// + public static void RightClick() + { + mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); + System.Threading.Thread.Sleep(50); + mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); + } + + /// + /// 中键单击 + /// + public static void MiddleClick() + { + mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); + System.Threading.Thread.Sleep(50); + mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); + } + + /// + /// 在指定位置点击 + /// + public static void ClickAt(int x, int y, MouseButton button = MouseButton.Left) + { + SetPosition(x, y); + System.Threading.Thread.Sleep(50); + + switch (button) + { + case MouseButton.Left: + LeftClick(); + break; + case MouseButton.Right: + RightClick(); + break; + case MouseButton.Middle: + MiddleClick(); + break; + } + } + + #endregion + + #region 鼠标按下/释放 + + /// + /// 按下左键 + /// + public static void LeftDown() + { + mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); + } + + /// + /// 释放左键 + /// + public static void LeftUp() + { + mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); + } + + /// + /// 按下右键 + /// + public static void RightDown() + { + mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); + } + + /// + /// 释放右键 + /// + public static void RightUp() + { + mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); + } + + /// + /// 按下中键 + /// + public static void MiddleDown() + { + mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); + } + + /// + /// 释放中键 + /// + public static void MiddleUp() + { + mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); + } + + #endregion + + #region 鼠标拖拽 + + /// + /// 鼠标拖拽(从起点拖到终点) + /// + public static void Drag(int fromX, int fromY, int toX, int toY, int steps = 20) + { + SetPosition(fromX, fromY); + System.Threading.Thread.Sleep(50); + LeftDown(); + System.Threading.Thread.Sleep(50); + MoveTo(toX, toY, steps); + System.Threading.Thread.Sleep(50); + LeftUp(); + } + + #endregion + + #region 鼠标滚轮 + + /// + /// 滚动鼠标滚轮 + /// + /// 滚动量,正数向上,负数向下 + public static void Scroll(int delta) + { + mouse_event(MOUSEEVENTF_WHEEL, 0, 0, delta * 120, 0); + } + + /// + /// 向上滚动 + /// + public static void ScrollUp(int amount = 1) + { + Scroll(amount); + } + + /// + /// 向下滚动 + /// + public static void ScrollDown(int amount = 1) + { + Scroll(-amount); + } + + /// + /// 水平滚动 + /// + public static void HorizontalScroll(int delta) + { + mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, delta * 120, 0); + } + + #endregion + + #region 鼠标状态 + + /// + /// 检测鼠标左键是否按下 + /// + public static bool IsLeftButtonDown() + { + return (GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0; + } + + /// + /// 检测鼠标右键是否按下 + /// + public static bool IsRightButtonDown() + { + return (GetAsyncKeyState(VK_RBUTTON) & 0x8000) != 0; + } + + /// + /// 检测鼠标中键是否按下 + /// + public static bool IsMiddleButtonDown() + { + return (GetAsyncKeyState(VK_MBUTTON) & 0x8000) != 0; + } + + /// + /// 显示鼠标光标 + /// + public static void ShowCursor() + { + ShowCursor(true); + } + + /// + /// 隐藏鼠标光标 + /// + public static void HideCursor() + { + ShowCursor(false); + } + + #endregion + + #region P/Invoke + + private const int MOUSEEVENTF_LEFTDOWN = 0x0002; + private const int MOUSEEVENTF_LEFTUP = 0x0004; + private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; + private const int MOUSEEVENTF_RIGHTUP = 0x0010; + private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; + private const int MOUSEEVENTF_MIDDLEUP = 0x0040; + private const int MOUSEEVENTF_WHEEL = 0x0800; + private const int MOUSEEVENTF_HWHEEL = 0x01000; + + private const int VK_LBUTTON = 0x01; + private const int VK_RBUTTON = 0x02; + private const int VK_MBUTTON = 0x04; + + [DllImport("user32.dll")] + private static extern bool GetCursorPos(out POINT lpPoint); + + [DllImport("user32.dll")] + private static extern bool SetCursorPos(int x, int y); + + [DllImport("user32.dll")] + private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); + + [DllImport("user32.dll")] + private static extern short GetAsyncKeyState(int vKey); + + [DllImport("user32.dll")] + private static extern int ShowCursor(bool bShow); + + [StructLayout(LayoutKind.Sequential)] + private struct POINT + { + public int X; + public int Y; + } + + #endregion + } + + /// + /// 鼠标按钮 + /// + public enum MouseButton + { + Left, + Right, + Middle + } +} diff --git a/EasyTool.Core/SystemCategory/PerformanceUtil.cs b/EasyTool.Core/SystemCategory/PerformanceUtil.cs new file mode 100644 index 0000000..1acd392 --- /dev/null +++ b/EasyTool.Core/SystemCategory/PerformanceUtil.cs @@ -0,0 +1,339 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Threading; + +namespace EasyTool.SystemCategory +{ + /// + /// 性能监控工具类 + /// + public static class PerformanceUtil + { + private static readonly PerformanceCounter? CpuCounter; + private static readonly PerformanceCounter? MemoryCounter; + private static readonly PerformanceCounter? DiskReadCounter; + private static readonly PerformanceCounter? DiskWriteCounter; + private static readonly PerformanceCounter? NetworkSentCounter; + private static readonly PerformanceCounter? NetworkReceivedCounter; + + static PerformanceUtil() + { + try + { + CpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + MemoryCounter = new PerformanceCounter("Memory", "Available MBytes"); + + // 获取第一个物理磁盘 + var diskInstance = GetFirstDiskInstance(); + if (diskInstance != null) + { + DiskReadCounter = new PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", diskInstance); + DiskWriteCounter = new PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", diskInstance); + } + + // 获取第一个网络接口 + var networkInstance = GetFirstNetworkInstance(); + if (networkInstance != null) + { + NetworkSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkInstance); + NetworkReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkInstance); + } + } + catch + { + // 性能计数器可能在某些环境不可用 + } + } + + private static string? GetFirstDiskInstance() + { + try + { + var category = new PerformanceCounterCategory("PhysicalDisk"); + var instances = category.GetInstanceNames(); + return instances.Length > 0 ? instances[0] : null; + } + catch + { + return null; + } + } + + private static string? GetFirstNetworkInstance() + { + try + { + var category = new PerformanceCounterCategory("Network Interface"); + var instances = category.GetInstanceNames(); + return instances.Length > 0 ? instances[0] : null; + } + catch + { + return null; + } + } + + /// + /// 获取CPU使用率 + /// + public static float GetCpuUsage() + { + try + { + CpuCounter?.NextValue(); // 第一次调用返回0 + Thread.Sleep(100); + return CpuCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取可用内存(MB) + /// + public static float GetAvailableMemoryMB() + { + try + { + return MemoryCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取总物理内存(字节) + /// + public static long GetTotalPhysicalMemory() + { + var memStatus = new MEMORYSTATUSEX(); + if (GlobalMemoryStatusEx(memStatus)) + { + return (long)memStatus.ullTotalPhys; + } + return 0; + } + + /// + /// 获取已用内存百分比 + /// + public static float GetMemoryUsagePercent() + { + var total = GetTotalPhysicalMemory(); + var available = GetAvailableMemoryMB() * 1024 * 1024; + if (total == 0) return 0; + return (float)((total - available) / (double)total * 100); + } + + /// + /// 获取磁盘读取速度(字节/秒) + /// + public static float GetDiskReadSpeed() + { + try + { + DiskReadCounter?.NextValue(); + Thread.Sleep(100); + return DiskReadCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取磁盘写入速度(字节/秒) + /// + public static float GetDiskWriteSpeed() + { + try + { + DiskWriteCounter?.NextValue(); + Thread.Sleep(100); + return DiskWriteCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取网络发送速度(字节/秒) + /// + public static float GetNetworkSentSpeed() + { + try + { + NetworkSentCounter?.NextValue(); + Thread.Sleep(100); + return NetworkSentCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取网络接收速度(字节/秒) + /// + public static float GetNetworkReceivedSpeed() + { + try + { + NetworkReceivedCounter?.NextValue(); + Thread.Sleep(100); + return NetworkReceivedCounter?.NextValue() ?? 0; + } + catch + { + return 0; + } + } + + /// + /// 获取进程数量 + /// + public static int GetProcessCount() + { + return Process.GetProcesses().Length; + } + + /// + /// 获取系统启动时间 + /// + public static DateTime GetSystemUptime() + { +#if NET5_0_OR_GREATER + return DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount64); +#else + return DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount); +#endif + } + + /// + /// 获取系统运行时长 + /// + public static TimeSpan GetSystemUptimeDuration() + { +#if NET5_0_OR_GREATER + return TimeSpan.FromMilliseconds(Environment.TickCount64); +#else + return TimeSpan.FromMilliseconds(Environment.TickCount); +#endif + } + + /// + /// 获取完整的性能数据 + /// + public static PerformanceData GetPerformanceData() + { + return new PerformanceData + { + CpuUsage = GetCpuUsage(), + MemoryUsagePercent = GetMemoryUsagePercent(), + TotalPhysicalMemory = GetTotalPhysicalMemory(), + AvailableMemoryMB = GetAvailableMemoryMB(), + DiskReadSpeed = GetDiskReadSpeed(), + DiskWriteSpeed = GetDiskWriteSpeed(), + NetworkSentSpeed = GetNetworkSentSpeed(), + NetworkReceivedSpeed = GetNetworkReceivedSpeed(), + ProcessCount = GetProcessCount(), + SystemUptime = GetSystemUptimeDuration() + }; + } + + /// + /// 监控进程CPU使用率 + /// + public static float GetProcessCpuUsage(int processId) + { + try + { + using var process = Process.GetProcessById(processId); + var cpuCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName); + cpuCounter.NextValue(); + Thread.Sleep(100); + return cpuCounter.NextValue() / Environment.ProcessorCount; + } + catch + { + return 0; + } + } + + /// + /// 监控进程内存使用 + /// + public static long GetProcessMemoryUsage(int processId) + { + try + { + using var process = Process.GetProcessById(processId); + return process.WorkingSet64; + } + catch + { + return 0; + } + } + + #region P/Invoke + + [StructLayout(LayoutKind.Sequential)] + private class MEMORYSTATUSEX + { + public uint dwLength; + public uint dwMemoryLoad; + public ulong ullTotalPhys; + public ulong ullAvailPhys; + public ulong ullTotalPageFile; + public ulong ullAvailPageFile; + public ulong ullTotalVirtual; + public ulong ullAvailVirtual; + public ulong ullAvailExtendedVirtual; + + public MEMORYSTATUSEX() + { + dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); + } + } + + [DllImport("kernel32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer); + + #endregion + } + + /// + /// 性能数据 + /// + public class PerformanceData + { + public float CpuUsage { get; set; } + public float MemoryUsagePercent { get; set; } + public long TotalPhysicalMemory { get; set; } + public float AvailableMemoryMB { get; set; } + public float DiskReadSpeed { get; set; } + public float DiskWriteSpeed { get; set; } + public float NetworkSentSpeed { get; set; } + public float NetworkReceivedSpeed { get; set; } + public int ProcessCount { get; set; } + public TimeSpan SystemUptime { get; set; } + + public double TotalPhysicalMemoryGB => TotalPhysicalMemory / (1024.0 * 1024 * 1024); + public double DiskReadSpeedMB => DiskReadSpeed / (1024.0 * 1024); + public double DiskWriteSpeedMB => DiskWriteSpeed / (1024.0 * 1024); + public double NetworkSentSpeedMB => NetworkSentSpeed / (1024.0 * 1024); + public double NetworkReceivedSpeedMB => NetworkReceivedSpeed / (1024.0 * 1024); + } +} diff --git a/EasyTool.Core/SystemCategory/PowerUtil.cs b/EasyTool.Core/SystemCategory/PowerUtil.cs new file mode 100644 index 0000000..23e68d9 --- /dev/null +++ b/EasyTool.Core/SystemCategory/PowerUtil.cs @@ -0,0 +1,364 @@ +using System; +using System.Runtime.InteropServices; + +namespace EasyTool.SystemCategory +{ + /// + /// 电源状态 + /// + public class PowerStatus + { + /// + /// 是否在使用交流电源 + /// + public bool IsAcConnected { get; set; } + + /// + /// 电池充电状态 + /// + public BatteryChargeStatus BatteryChargeStatus { get; set; } + + /// + /// 电池剩余电量百分比(0-100) + /// + public int BatteryLifePercent { get; set; } + + /// + /// 电池剩余时间(秒) + /// + public int BatteryLifeRemaining { get; set; } + + /// + /// 电池充满时间(秒) + /// + public int BatteryFullLifeTime { get; set; } + + /// + /// 电源线状态 + /// + public PowerLineStatus PowerLineStatus { get; set; } + + public override string ToString() + { + return $"电源状态: {(IsAcConnected ? "交流电源" : "电池")}, 电量: {BatteryLifePercent}%, 剩余时间: {BatteryLifeRemaining}s"; + } + } + + /// + /// 电池充电状态 + /// + [Flags] + public enum BatteryChargeStatus + { + /// + /// 充电状态未知 + /// + Unknown = 0, + + /// + /// 正在充电 + /// + Charging = 1, + + /// + /// 未充电 + /// + NoCharging = 2, + + /// + /// 电量低 + /// + Low = 4, + + /// + /// 电量严重不足 + /// + Critical = 8, + + /// + /// 无电池 + /// + NoBattery = 128, + + /// + /// 电池已充满 + /// + Full = 255 + } + + /// + /// 电源线状态 + /// + public enum PowerLineStatus + { + /// + /// 离线(电池供电) + /// + Offline = 0, + + /// + /// 在线(交流电源) + /// + Online = 1, + + /// + /// 未知 + /// + Unknown = 255 + } + + /// + /// 电源管理工具类 + /// + public static class PowerUtil + { + #region Windows API + + [StructLayout(LayoutKind.Sequential)] + private struct SYSTEM_POWER_STATUS + { + public byte ACLineStatus; + public byte BatteryFlag; + public byte BatteryLifePercent; + public byte SystemStatusFlag; + public uint BatteryLifeTime; + public uint BatteryFullLifeTime; + } + + [DllImport("kernel32.dll")] + private static extern bool GetSystemPowerStatus(ref SYSTEM_POWER_STATUS lpSystemPowerStatus); + + [DllImport("kernel32.dll")] + private static extern bool SetSystemPowerState(bool hibernate, bool force); + + [DllImport("kernel32.dll")] + private static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent); + + [DllImport("powrprof.dll")] + private static extern bool SetSuspendState2(bool hibernate, bool force, bool disableWakeEvent); + + #endregion + + /// + /// 获取电源状态 + /// + /// 电源状态信息 + public static PowerStatus GetPowerStatus() + { + var status = new SYSTEM_POWER_STATUS(); + GetSystemPowerStatus(ref status); + + return new PowerStatus + { + IsAcConnected = status.ACLineStatus == 1, + BatteryChargeStatus = (BatteryChargeStatus)status.BatteryFlag, + BatteryLifePercent = status.BatteryLifePercent > 100 ? 100 : status.BatteryLifePercent, + BatteryLifeRemaining = (int)status.BatteryLifeTime, + BatteryFullLifeTime = (int)status.BatteryFullLifeTime, + PowerLineStatus = (PowerLineStatus)status.ACLineStatus + }; + } + + /// + /// 是否使用交流电源 + /// + /// true表示使用交流电源 + public static bool IsAcConnected() + { + var status = GetPowerStatus(); + return status.IsAcConnected; + } + + /// + /// 是否使用电池 + /// + /// true表示使用电池 + public static bool IsOnBattery() + { + return !IsAcConnected(); + } + + /// + /// 获取电池电量百分比 + /// + /// 电量百分比(0-100),无电池返回-1 + public static int GetBatteryPercent() + { + var status = GetPowerStatus(); + return status.BatteryLifePercent; + } + + /// + /// 获取电池剩余时间 + /// + /// 剩余时间,未知返回TimeSpan.Zero + public static TimeSpan GetBatteryLifeRemaining() + { + var status = GetPowerStatus(); + return status.BatteryLifeRemaining > 0 + ? TimeSpan.FromSeconds(status.BatteryLifeRemaining) + : TimeSpan.Zero; + } + + /// + /// 是否电量低 + /// + /// 阈值百分比(默认20%) + /// true表示电量低 + public static bool IsLowBattery(int threshold = 20) + { + var percent = GetBatteryPercent(); + return percent > 0 && percent <= threshold && IsOnBattery(); + } + + /// + /// 是否电量严重不足 + /// + /// 阈值百分比(默认10%) + /// true表示电量严重不足 + public static bool IsCriticalBattery(int threshold = 10) + { + var percent = GetBatteryPercent(); + return percent > 0 && percent <= threshold && IsOnBattery(); + } + + /// + /// 是否正在充电 + /// + /// true表示正在充电 + public static bool IsCharging() + { + var status = GetPowerStatus(); + return status.BatteryChargeStatus.HasFlag(BatteryChargeStatus.Charging); + } + + /// + /// 是否电池已充满 + /// + /// true表示电池已充满 + public static bool IsBatteryFull() + { + var status = GetPowerStatus(); + return status.BatteryLifePercent >= 100; + } + + /// + /// 是否有电池 + /// + /// true表示有电池 + public static bool HasBattery() + { + var status = GetPowerStatus(); + return !status.BatteryChargeStatus.HasFlag(BatteryChargeStatus.NoBattery); + } + + /// + /// 使系统进入睡眠状态 + /// + /// 是否强制进入 + /// 是否成功 + public static bool Sleep(bool force = false) + { + try + { + return SetSuspendState(false, force, false); + } + catch + { + return false; + } + } + + /// + /// 使系统进入休眠状态 + /// + /// 是否强制进入 + /// 是否成功 + public static bool Hibernate(bool force = false) + { + try + { + return SetSuspendState(true, force, false); + } + catch + { + return false; + } + } + + /// + /// 获取电源状态描述 + /// + /// 电源状态描述字符串 + public static string GetPowerStatusDescription() + { + var status = GetPowerStatus(); + var sb = new System.Text.StringBuilder(); + + sb.AppendLine($"电源线状态: {status.PowerLineStatus}"); + sb.AppendLine($"是否使用交流电源: {(status.IsAcConnected ? "是" : "否")}"); + + if (HasBattery()) + { + sb.AppendLine($"电池电量: {status.BatteryLifePercent}%"); + sb.AppendLine($"充电状态: {status.BatteryChargeStatus}"); + + if (status.BatteryLifeRemaining > 0) + { + var time = TimeSpan.FromSeconds(status.BatteryLifeRemaining); + sb.AppendLine($"剩余时间: {time.Hours}小时{time.Minutes}分钟"); + } + } + else + { + sb.AppendLine("无电池"); + } + + return sb.ToString(); + } + + /// + /// 监听电源状态变化 + /// + public static event Action? PowerStatusChanged; + + private static System.Threading.Timer? _monitorTimer; + private static PowerStatus? _lastStatus; + + /// + /// 开始监控电源状态 + /// + /// 检查间隔(毫秒) + public static void StartMonitoring(int interval = 5000) + { + _lastStatus = GetPowerStatus(); + _monitorTimer = new System.Threading.Timer(_ => + { + var currentStatus = GetPowerStatus(); + if (HasPowerStatusChanged(_lastStatus, currentStatus)) + { + PowerStatusChanged?.Invoke(currentStatus); + _lastStatus = currentStatus; + } + }, null, interval, interval); + } + + /// + /// 停止监控电源状态 + /// + public static void StopMonitoring() + { + _monitorTimer?.Dispose(); + _monitorTimer = null; + } + + private static bool HasPowerStatusChanged(PowerStatus? old, PowerStatus current) + { + if (old == null) return true; + + return old.IsAcConnected != current.IsAcConnected || + old.BatteryLifePercent != current.BatteryLifePercent || + old.BatteryChargeStatus != current.BatteryChargeStatus; + } + } +} diff --git a/EasyTool.Core/SystemCategory/RegistryUtil.cs b/EasyTool.Core/SystemCategory/RegistryUtil.cs new file mode 100644 index 0000000..813b6c2 --- /dev/null +++ b/EasyTool.Core/SystemCategory/RegistryUtil.cs @@ -0,0 +1,265 @@ +using System; +using System.Diagnostics; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 注册表工具类 + /// + public static class RegistryUtil + { + /// + /// 读取注册表值 + /// + public static string? GetValue(string path, string name) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + return key?.GetValue(name)?.ToString(); + } + + /// + /// 读取注册表值(指定根键) + /// + public static string? GetValue(Microsoft.Win32.RegistryHive hive, string path, string name) + { + using var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(hive, Microsoft.Win32.RegistryView.Default); + using var key = baseKey.OpenSubKey(path); + return key?.GetValue(name)?.ToString(); + } + + /// + /// 设置注册表值 + /// + public static void SetValue(string path, string name, object value, Microsoft.Win32.RegistryValueKind valueKind = Microsoft.Win32.RegistryValueKind.String) + { + using var key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(path); + key?.SetValue(name, value, valueKind); + } + + /// + /// 设置注册表值(指定根键) + /// + public static void SetValue(Microsoft.Win32.RegistryHive hive, string path, string name, object value, Microsoft.Win32.RegistryValueKind valueKind = Microsoft.Win32.RegistryValueKind.String) + { + using var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(hive, Microsoft.Win32.RegistryView.Default); + using var key = baseKey.CreateSubKey(path); + key?.SetValue(name, value, valueKind); + } + + /// + /// 删除注册表值 + /// + public static void DeleteValue(string path, string name) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, true); + key?.DeleteValue(name, false); + } + + /// + /// 删除注册表键 + /// + public static void DeleteSubKey(string path) + { + var parentPath = System.IO.Path.GetDirectoryName(path)?.Replace('\\', '/'); + var keyName = System.IO.Path.GetFileName(path); + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(parentPath, true); + key?.DeleteSubKey(keyName, false); + } + + /// + /// 检查键是否存在 + /// + public static bool KeyExists(string path) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + return key != null; + } + + /// + /// 检查值是否存在 + /// + public static bool ValueExists(string path, string name) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + return key?.GetValue(name) != null; + } + + /// + /// 获取所有子键名称 + /// + public static string[] GetSubKeyNames(string path) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + return key?.GetSubKeyNames() ?? Array.Empty(); + } + + /// + /// 获取所有值名称 + /// + public static string[] GetValueNames(string path) + { + using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + return key?.GetValueNames() ?? Array.Empty(); + } + + /// + /// 获取开机启动项列表 + /// + public static System.Collections.Generic.Dictionary GetStartupPrograms() + { + var programs = new System.Collections.Generic.Dictionary(); + + var paths = new[] + { + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", + @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run" + }; + + foreach (var path in paths) + { + var names = GetValueNames(path); + foreach (var name in names) + { + var value = GetValue(path, name); + if (value != null && !programs.ContainsKey(name)) + { + programs[name] = value; + } + } + } + + return programs; + } + + /// + /// 添加开机启动项 + /// + public static void AddStartupProgram(string name, string command) + { + SetValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", name, command); + } + + /// + /// 删除开机启动项 + /// + public static void RemoveStartupProgram(string name) + { + DeleteValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", name); + } + + /// + /// 获取已安装软件列表 + /// + public static System.Collections.Generic.List GetInstalledPrograms() + { + var programs = new System.Collections.Generic.List(); + + var paths = new[] + { + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", + @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + }; + + foreach (var path in paths) + { + var subKeys = GetSubKeyNames(path); + foreach (var subKey in subKeys) + { + var programPath = $@"{path}\{subKey}"; + var name = GetValue(programPath, "DisplayName"); + if (name != null) + { + programs.Add(new InstalledProgram + { + Name = name, + Version = GetValue(programPath, "DisplayVersion"), + Publisher = GetValue(programPath, "Publisher"), + InstallDate = GetValue(programPath, "InstallDate"), + UninstallString = GetValue(programPath, "UninstallString"), + InstallLocation = GetValue(programPath, "InstallLocation") + }); + } + } + } + + return programs; + } + + /// + /// 设置文件关联 + /// + public static void SetFileAssociation(string extension, string programPath, string description) + { + var progId = extension.TrimStart('.'); + SetValue($@"SOFTWARE\Classes\{extension}", "", progId); + SetValue($@"SOFTWARE\Classes\{progId}", "", description); + SetValue($@"SOFTWARE\Classes\{progId}\shell\open\command", "", $"\"{programPath}\" \"%1\""); + } + + /// + /// 添加右键菜单项 + /// + public static void AddContextMenu(string name, string command, string? iconPath = null) + { + var path = $@"SOFTWARE\Classes\*\shell\{name}"; + SetValue(path, "", name); + SetValue($@"{path}\command", "", command); + if (iconPath != null) + { + SetValue(path, "Icon", iconPath); + } + } + + /// + /// 删除右键菜单项 + /// + public static void RemoveContextMenu(string name) + { + DeleteSubKey($@"SOFTWARE\Classes\*\shell\{name}\command"); + DeleteSubKey($@"SOFTWARE\Classes\*\shell\{name}"); + } + } + + /// + /// 已安装程序信息 + /// + public class InstalledProgram + { + /// + /// 名称 + /// + public string? Name { get; set; } + + /// + /// 版本 + /// + public string? Version { get; set; } + + /// + /// 发布者 + /// + public string? Publisher { get; set; } + + /// + /// 安装日期 + /// + public string? InstallDate { get; set; } + + /// + /// 卸载命令 + /// + public string? UninstallString { get; set; } + + /// + /// 安装位置 + /// + public string? InstallLocation { get; set; } + + public override string ToString() + { + return $"{Name} {Version}"; + } + } +} diff --git a/EasyTool.Core/SystemCategory/ScreenUtil.cs b/EasyTool.Core/SystemCategory/ScreenUtil.cs new file mode 100644 index 0000000..fe39b96 --- /dev/null +++ b/EasyTool.Core/SystemCategory/ScreenUtil.cs @@ -0,0 +1,243 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Runtime.InteropServices; + +namespace EasyTool.SystemCategory +{ + /// + /// 屏幕工具类 + /// + public static class ScreenUtil + { + /// + /// 获取主屏幕 + /// + public static ScreenInfo GetPrimaryScreen() + { + var bounds = GetPrimaryScreenBounds(); + return new ScreenInfo + { + DeviceName = "Primary", + Width = bounds.Width, + Height = bounds.Height, + X = bounds.X, + Y = bounds.Y, + BitsPerPixel = 32, + IsPrimary = true + }; + } + + /// + /// 获取所有屏幕 + /// + public static List GetAllScreens() + { + var result = new List(); + var monitors = new List(); + + EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, + (hMonitor, hdcMonitor, lprcMonitor, dwData) => + { + var info = new MonitorInfoEx(); + info.Size = Marshal.SizeOf(info); + if (GetMonitorInfo(hMonitor, ref info)) + { + monitors.Add(new MonitorInfo + { + DeviceName = info.DeviceName, + Bounds = info.Monitor, + WorkArea = info.WorkArea, + IsPrimary = (info.Flags & 1) != 0 + }); + } + return true; + }, IntPtr.Zero); + + foreach (var monitor in monitors) + { + result.Add(new ScreenInfo + { + DeviceName = monitor.DeviceName, + Width = monitor.Bounds.Right - monitor.Bounds.Left, + Height = monitor.Bounds.Bottom - monitor.Bounds.Top, + X = monitor.Bounds.Left, + Y = monitor.Bounds.Top, + BitsPerPixel = 32, + IsPrimary = monitor.IsPrimary + }); + } + + return result; + } + + /// + /// 获取虚拟屏幕尺寸(所有屏幕合并) + /// + public static (int Width, int Height) GetVirtualScreenSize() + { + return (GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN)); + } + + /// + /// 截取屏幕截图 + /// + public static Bitmap? CaptureScreen() + { + try + { + var bounds = GetPrimaryScreenBounds(); + return CaptureRegion(bounds.X, bounds.Y, bounds.Width, bounds.Height); + } + catch + { + return null; + } + } + + /// + /// 截取指定区域 + /// + public static Bitmap? CaptureRegion(int x, int y, int width, int height) + { + try + { + var bitmap = new Bitmap(width, height); + using (var g = Graphics.FromImage(bitmap)) + { + g.CopyFromScreen(x, y, 0, 0, new Size(width, height)); + } + return bitmap; + } + catch + { + return null; + } + } + + /// + /// 截取活动窗口 + /// + public static Bitmap? CaptureActiveWindow() + { + try + { + var handle = GetForegroundWindow(); + GetWindowRect(handle, out var rect); + return CaptureRegion(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); + } + catch + { + return null; + } + } + + /// + /// 获取鼠标位置 + /// + public static (int X, int Y) GetMousePosition() + { + GetCursorPos(out var point); + return (point.X, point.Y); + } + + /// + /// 设置鼠标位置 + /// + public static void SetMousePosition(int x, int y) + { + SetCursorPos(x, y); + } + + private static Rectangle GetPrimaryScreenBounds() + { + var width = GetSystemMetrics(SM_CXSCREEN); + var height = GetSystemMetrics(SM_CYSCREEN); + return new Rectangle(0, 0, width, height); + } + + #region P/Invoke + + private const int SM_CXSCREEN = 0; + private const int SM_CYSCREEN = 1; + private const int SM_CXVIRTUALSCREEN = 78; + private const int SM_CYVIRTUALSCREEN = 79; + + [DllImport("user32.dll")] + private static extern int GetSystemMetrics(int nIndex); + + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); + + [DllImport("user32.dll")] + private static extern bool GetCursorPos(out POINT lpPoint); + + [DllImport("user32.dll")] + private static extern bool SetCursorPos(int X, int Y); + + [DllImport("user32.dll")] + private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, + EnumMonitorsDelegate lpfnEnum, IntPtr dwData); + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi); + + private delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, RECT lprcMonitor, IntPtr dwData); + + [StructLayout(LayoutKind.Sequential)] + private struct RECT + { + public int Left; + public int Top; + public int Right; + public int Bottom; + } + + [StructLayout(LayoutKind.Sequential)] + private struct POINT + { + public int X; + public int Y; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + private struct MonitorInfoEx + { + public int Size; + public RECT Monitor; + public RECT WorkArea; + public int Flags; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string DeviceName; + } + + private class MonitorInfo + { + public string DeviceName { get; set; } = ""; + public RECT Bounds { get; set; } + public RECT WorkArea { get; set; } + public bool IsPrimary { get; set; } + } + + #endregion + } + + /// + /// 屏幕信息 + /// + public class ScreenInfo + { + public string DeviceName { get; set; } = ""; + public int Width { get; set; } + public int Height { get; set; } + public int X { get; set; } + public int Y { get; set; } + public int BitsPerPixel { get; set; } + public bool IsPrimary { get; set; } + + public string Resolution => $"{Width} x {Height}"; + } +} \ No newline at end of file diff --git a/EasyTool.Core/SystemCategory/ServiceUtil.cs b/EasyTool.Core/SystemCategory/ServiceUtil.cs new file mode 100644 index 0000000..cfc5082 --- /dev/null +++ b/EasyTool.Core/SystemCategory/ServiceUtil.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.ServiceProcess; +using System.Threading.Tasks; + +namespace EasyTool.SystemCategory +{ + /// + /// Windows服务工具类 + /// + public static class ServiceUtil + { + /// + /// 获取所有服务 + /// + /// 服务列表 + public static List GetAllServices() + { + return new List(ServiceController.GetServices()); + } + + /// + /// 获取指定服务 + /// + /// 服务名称 + /// 服务控制器 + public static ServiceController? GetService(string serviceName) + { + try + { + return new ServiceController(serviceName); + } + catch + { + return null; + } + } + + /// + /// 检查服务是否存在 + /// + /// 服务名称 + /// 是否存在 + public static bool ServiceExists(string serviceName) + { + try + { + var services = ServiceController.GetServices(); + foreach (var service in services) + { + if (service.ServiceName.Equals(serviceName, StringComparison.OrdinalIgnoreCase)) + { + service.Dispose(); + return true; + } + service.Dispose(); + } + return false; + } + catch + { + return false; + } + } + + /// + /// 启动服务 + /// + /// 服务名称 + /// 超时时间 + /// 是否成功 + public static bool StartService(string serviceName, TimeSpan? timeout = null) + { + using var service = GetService(serviceName); + if (service == null) return false; + + try + { + if (service.Status == ServiceControllerStatus.Running) + return true; + + service.Start(); + service.WaitForStatus(ServiceControllerStatus.Running, timeout ?? TimeSpan.FromMinutes(1)); + return service.Status == ServiceControllerStatus.Running; + } + catch + { + return false; + } + } + + /// + /// 停止服务 + /// + /// 服务名称 + /// 超时时间 + /// 是否成功 + public static bool StopService(string serviceName, TimeSpan? timeout = null) + { + using var service = GetService(serviceName); + if (service == null) return false; + + try + { + if (service.Status == ServiceControllerStatus.Stopped) + return true; + + if (!service.CanStop) + return false; + + service.Stop(); + service.WaitForStatus(ServiceControllerStatus.Stopped, timeout ?? TimeSpan.FromMinutes(1)); + return service.Status == ServiceControllerStatus.Stopped; + } + catch + { + return false; + } + } + + /// + /// 重启服务 + /// + /// 服务名称 + /// 超时时间 + /// 是否成功 + public static bool RestartService(string serviceName, TimeSpan? timeout = null) + { + if (!StopService(serviceName, timeout)) + return false; + + System.Threading.Thread.Sleep(1000); + return StartService(serviceName, timeout); + } + + /// + /// 暂停服务 + /// + /// 服务名称 + /// 超时时间 + /// 是否成功 + public static bool PauseService(string serviceName, TimeSpan? timeout = null) + { + using var service = GetService(serviceName); + if (service == null) return false; + + try + { + if (!service.CanPauseAndContinue) + return false; + + service.Pause(); + service.WaitForStatus(ServiceControllerStatus.Paused, timeout ?? TimeSpan.FromMinutes(1)); + return service.Status == ServiceControllerStatus.Paused; + } + catch + { + return false; + } + } + + /// + /// 继续服务 + /// + /// 服务名称 + /// 超时时间 + /// 是否成功 + public static bool ContinueService(string serviceName, TimeSpan? timeout = null) + { + using var service = GetService(serviceName); + if (service == null) return false; + + try + { + if (!service.CanPauseAndContinue) + return false; + + service.Continue(); + service.WaitForStatus(ServiceControllerStatus.Running, timeout ?? TimeSpan.FromMinutes(1)); + return service.Status == ServiceControllerStatus.Running; + } + catch + { + return false; + } + } + + /// + /// 获取服务状态 + /// + /// 服务名称 + /// 服务状态 + public static ServiceControllerStatus? GetServiceStatus(string serviceName) + { + using var service = GetService(serviceName); + return service?.Status; + } + + /// + /// 检查服务是否正在运行 + /// + /// 服务名称 + /// 是否正在运行 + public static bool IsServiceRunning(string serviceName) + { + return GetServiceStatus(serviceName) == ServiceControllerStatus.Running; + } + + /// + /// 检查服务是否已停止 + /// + /// 服务名称 + /// 是否已停止 + public static bool IsServiceStopped(string serviceName) + { + return GetServiceStatus(serviceName) == ServiceControllerStatus.Stopped; + } + + /// + /// 获取服务信息 + /// + /// 服务名称 + /// 服务信息 + public static ServiceInfo? GetServiceInfo(string serviceName) + { + using var service = GetService(serviceName); + if (service == null) return null; + + return new ServiceInfo + { + ServiceName = service.ServiceName, + DisplayName = service.DisplayName, + Status = service.Status, + CanStop = service.CanStop, + CanPauseAndContinue = service.CanPauseAndContinue, + ServiceType = service.ServiceType, + MachineName = service.MachineName + }; + } + + /// + /// 按状态获取服务列表 + /// + /// 服务状态 + /// 服务列表 + public static List GetServicesByStatus(ServiceControllerStatus status) + { + var result = new List(); + var services = ServiceController.GetServices(); + + foreach (var service in services) + { + if (service.Status == status) + { + result.Add(new ServiceInfo + { + ServiceName = service.ServiceName, + DisplayName = service.DisplayName, + Status = service.Status, + CanStop = service.CanStop, + CanPauseAndContinue = service.CanPauseAndContinue, + ServiceType = service.ServiceType + }); + } + service.Dispose(); + } + + return result; + } + + /// + /// 获取正在运行的服务 + /// + /// 服务列表 + public static List GetRunningServices() + { + return GetServicesByStatus(ServiceControllerStatus.Running); + } + + /// + /// 获取已停止的服务 + /// + /// 服务列表 + public static List GetStoppedServices() + { + return GetServicesByStatus(ServiceControllerStatus.Stopped); + } + + /// + /// 等待服务达到指定状态 + /// + /// 服务名称 + /// 目标状态 + /// 超时时间 + /// 是否成功 + public static bool WaitForStatus(string serviceName, ServiceControllerStatus targetStatus, TimeSpan? timeout = null) + { + using var service = GetService(serviceName); + if (service == null) return false; + + try + { + service.WaitForStatus(targetStatus, timeout ?? TimeSpan.FromMinutes(1)); + return service.Status == targetStatus; + } + catch + { + return false; + } + } + + /// + /// 异步启动服务 + /// + public static Task StartServiceAsync(string serviceName, TimeSpan? timeout = null) + { + return Task.Run(() => StartService(serviceName, timeout)); + } + + /// + /// 异步停止服务 + /// + public static Task StopServiceAsync(string serviceName, TimeSpan? timeout = null) + { + return Task.Run(() => StopService(serviceName, timeout)); + } + + /// + /// 异步重启服务 + /// + public static Task RestartServiceAsync(string serviceName, TimeSpan? timeout = null) + { + return Task.Run(() => RestartService(serviceName, timeout)); + } + } + + /// + /// 服务信息 + /// + public class ServiceInfo + { + /// + /// 服务名称 + /// + public string ServiceName { get; set; } = string.Empty; + + /// + /// 显示名称 + /// + public string DisplayName { get; set; } = string.Empty; + + /// + /// 服务状态 + /// + public ServiceControllerStatus Status { get; set; } + + /// + /// 是否可以停止 + /// + public bool CanStop { get; set; } + + /// + /// 是否可以暂停和继续 + /// + public bool CanPauseAndContinue { get; set; } + + /// + /// 服务类型 + /// + public ServiceType ServiceType { get; set; } + + /// + /// 机器名 + /// + public string MachineName { get; set; } = "."; + + /// + /// 是否正在运行 + /// + public bool IsRunning => Status == ServiceControllerStatus.Running; + + /// + /// 是否已停止 + /// + public bool IsStopped => Status == ServiceControllerStatus.Stopped; + + public override string ToString() + { + return $"{ServiceName} ({DisplayName}) - {Status}"; + } + } +} diff --git a/EasyTool.Core/SystemCategory/SystemMonitorUtil.cs b/EasyTool.Core/SystemCategory/SystemMonitorUtil.cs new file mode 100644 index 0000000..9a57776 --- /dev/null +++ b/EasyTool.Core/SystemCategory/SystemMonitorUtil.cs @@ -0,0 +1,991 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.SystemCategory +{ + /// + /// 系统监控工具类 + /// 提供 CPU、内存、磁盘等系统资源的监控功能 + /// + public static class SystemMonitorUtil + { + #region CPU 监控 + + /// + /// 获取 CPU 使用率 + /// + /// CPU 使用率(0-100) + public static float GetCpuUsage() + { + using var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + cpuCounter.NextValue(); // 第一次调用返回 0 + Thread.Sleep(1000); + return cpuCounter.NextValue(); + } + + /// + /// 异步获取 CPU 使用率 + /// + /// CPU 使用率 + public static async Task GetCpuUsageAsync() + { + return await Task.Run(() => GetCpuUsage()); + } + + /// + /// 获取各核心 CPU 使用率 + /// + /// 各核心使用率数组 + public static float[] GetCpuCoreUsage() + { + var coreCount = Environment.ProcessorCount; + var counters = new PerformanceCounter[coreCount]; + var result = new float[coreCount]; + + for (int i = 0; i < coreCount; i++) + { + counters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString()); + counters[i].NextValue(); + } + + Thread.Sleep(1000); + + for (int i = 0; i < coreCount; i++) + { + result[i] = counters[i].NextValue(); + counters[i].Dispose(); + } + + return result; + } + + /// + /// 获取 CPU 信息 + /// + /// CPU 信息 + public static CpuMetrics GetCpuMetrics() + { + return new CpuMetrics + { + ProcessorCount = Environment.ProcessorCount, + CurrentUsage = GetCpuUsage() + }; + } + + #endregion + + #region 内存监控 + + /// + /// 获取可用内存大小 + /// + /// 可用内存(MB) + public static long GetAvailableMemory() + { + using var memCounter = new PerformanceCounter("Memory", "Available MBytes"); + return (long)memCounter.NextValue(); + } + + /// + /// 获取总物理内存大小 + /// + /// 总物理内存(字节) + public static long GetTotalPhysicalMemory() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return GetTotalPhysicalMemoryWindows(); + } + return 0; + } + + [DllImport("kernel32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool GetPhysicallyInstalledSystemMemory(out ulong TotalMemoryInKilobytes); + + private static long GetTotalPhysicalMemoryWindows() + { + try + { + if (GetPhysicallyInstalledSystemMemory(out var totalMemoryKB)) + { + return (long)(totalMemoryKB * 1024); // 转换为字节 + } + } + catch { } + + // 备用方法 + using var memCounter = new PerformanceCounter("Memory", "Available MBytes"); + var available = memCounter.NextValue(); + // 估算(不准确) + return (long)(available * 1024 * 1024 * 2); // 假设使用了一半 + } + + /// + /// 获取内存使用率 + /// + /// 内存使用率(0-100) + public static float GetMemoryUsage() + { + var totalMemory = GetTotalPhysicalMemory(); + if (totalMemory == 0) + return 0; + + var availableMemory = GetAvailableMemory() * 1024 * 1024; // MB 转 Bytes + var usedMemory = totalMemory - availableMemory; + return (float)usedMemory / totalMemory * 100; + } + + /// + /// 获取当前进程内存使用 + /// + /// 内存使用(字节) + public static long GetCurrentProcessMemory() + { + using var process = Process.GetCurrentProcess(); + process.Refresh(); + return process.WorkingSet64; + } + + /// + /// 获取内存信息 + /// + /// 内存信息 + public static MemoryMetrics GetMemoryMetrics() + { + var totalPhysical = GetTotalPhysicalMemory(); + var availableMB = GetAvailableMemory(); + var availableBytes = availableMB * 1024 * 1024; + + return new MemoryMetrics + { + TotalPhysicalMemory = totalPhysical, + AvailablePhysicalMemory = availableBytes, + UsedPhysicalMemory = totalPhysical - availableBytes, + MemoryUsagePercent = totalPhysical > 0 ? (float)(totalPhysical - availableBytes) / totalPhysical * 100 : 0, + CurrentProcessMemory = GetCurrentProcessMemory() + }; + } + + #endregion + + #region 磁盘监控 + + /// + /// 获取所有驱动器信息 + /// + /// 驱动器信息列表 + public static List GetDiskMetrics() + { + var drives = DriveInfo.GetDrives(); + var result = new List(); + + foreach (var drive in drives) + { + try + { + var info = new DiskMetrics + { + Name = drive.Name, + DriveType = drive.DriveType.ToString(), + VolumeLabel = drive.VolumeLabel, + FileSystem = drive.DriveFormat, + TotalSize = drive.TotalSize, + TotalFreeSpace = drive.TotalFreeSpace, + AvailableFreeSpace = drive.AvailableFreeSpace + }; + info.UsedSpace = info.TotalSize - info.TotalFreeSpace; + info.UsagePercent = info.TotalSize > 0 ? (float)info.UsedSpace / info.TotalSize * 100 : 0; + result.Add(info); + } + catch + { + // 跳过无法访问的驱动器 + } + } + + return result; + } + + /// + /// 获取指定驱动器信息 + /// + /// 驱动器名称(如 "C:") + /// 驱动器信息 + public static DiskMetrics? GetDiskMetrics(string driveName) + { + try + { + var drive = new DriveInfo(driveName); + var info = new DiskMetrics + { + Name = drive.Name, + DriveType = drive.DriveType.ToString(), + VolumeLabel = drive.VolumeLabel, + FileSystem = drive.DriveFormat, + TotalSize = drive.TotalSize, + TotalFreeSpace = drive.TotalFreeSpace, + AvailableFreeSpace = drive.AvailableFreeSpace + }; + info.UsedSpace = info.TotalSize - info.TotalFreeSpace; + info.UsagePercent = info.TotalSize > 0 ? (float)info.UsedSpace / info.TotalSize * 100 : 0; + return info; + } + catch + { + return null; + } + } + + /// + /// 获取磁盘读取速度 + /// + /// 驱动器名称 + /// 读取速度(字节/秒) + public static long GetDiskReadSpeed(string driveName = null) + { + try + { + var instance = driveName != null && driveName.Length >= 2 + ? driveName.Substring(0, 2) + ":" + : "_Total"; + + using var counter = new PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", instance); + counter.NextValue(); + Thread.Sleep(1000); + return (long)counter.NextValue(); + } + catch + { + return 0; + } + } + + /// + /// 获取磁盘写入速度 + /// + /// 驱动器名称 + /// 写入速度(字节/秒) + public static long GetDiskWriteSpeed(string driveName = null) + { + try + { + var instance = driveName != null && driveName.Length >= 2 + ? driveName.Substring(0, 2) + ":" + : "_Total"; + + using var counter = new PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", instance); + counter.NextValue(); + Thread.Sleep(1000); + return (long)counter.NextValue(); + } + catch + { + return 0; + } + } + + #endregion + + #region 网络监控 + + /// + /// 获取网络接口信息 + /// + /// 网络接口列表 + public static List GetNetworkInterfaces() + { + var result = new List(); + + try + { + var interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + var info = new NetworkInterfaceInfo + { + Name = ni.Name, + Description = ni.Description, + Id = ni.Id, + Type = ni.NetworkInterfaceType.ToString(), + Status = ni.OperationalStatus.ToString(), + Speed = ni.Speed, + IsUp = ni.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up + }; + + // 获取 IP 地址 + var ipProps = ni.GetIPProperties(); + info.IpAddresses = ipProps.UnicastAddresses + .Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + .Select(a => a.Address.ToString()) + .ToList(); + + result.Add(info); + } + } + catch + { + // 忽略异常 + } + + return result; + } + + /// + /// 获取网络下载速度 + /// + /// 网络接口名称 + /// 下载速度(字节/秒) + public static long GetNetworkDownloadSpeed(string interfaceName = null) + { + try + { + var instance = interfaceName ?? GetFirstNetworkInterfaceName(); + if (instance == null) + return 0; + + using var counter = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance); + counter.NextValue(); + Thread.Sleep(1000); + return (long)counter.NextValue(); + } + catch + { + return 0; + } + } + + /// + /// 获取网络上传速度 + /// + /// 网络接口名称 + /// 上传速度(字节/秒) + public static long GetNetworkUploadSpeed(string interfaceName = null) + { + try + { + var instance = interfaceName ?? GetFirstNetworkInterfaceName(); + if (instance == null) + return 0; + + using var counter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance); + counter.NextValue(); + Thread.Sleep(1000); + return (long)counter.NextValue(); + } + catch + { + return 0; + } + } + + private static string? GetFirstNetworkInterfaceName() + { + try + { + var interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); + var first = interfaces.FirstOrDefault(ni => + ni.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up && + ni.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback); + + return first?.Description; + } + catch + { + return null; + } + } + + #endregion + + #region 进程监控 + + /// + /// 获取占用 CPU 最高的进程 + /// + /// 返回数量 + /// 进程列表 + public static List GetTopCpuProcesses(int topN = 10) + { + var processes = Process.GetProcesses(); + var result = new List(); + + // 获取第一次采样 + var cpuCounters = new Dictionary(); + foreach (var p in processes) + { + try + { + var counter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName); + counter.NextValue(); + cpuCounters[p.Id] = counter; + } + catch + { + p.Dispose(); + } + } + + Thread.Sleep(1000); + + // 获取第二次采样并计算 + foreach (var p in processes) + { + try + { + if (cpuCounters.TryGetValue(p.Id, out var counter)) + { + result.Add(new ProcessUsageInfo + { + Id = p.Id, + Name = p.ProcessName, + CpuUsage = counter.NextValue() / Environment.ProcessorCount, + MemoryUsage = p.WorkingSet64 + }); + counter.Dispose(); + } + } + catch { } + finally + { + p.Dispose(); + } + } + + return result.OrderByDescending(p => p.CpuUsage).Take(topN).ToList(); + } + + /// + /// 获取占用内存最高的进程 + /// + /// 返回数量 + /// 进程列表 + public static List GetTopMemoryProcesses(int topN = 10) + { + var processes = Process.GetProcesses(); + var result = new List(); + + foreach (var p in processes) + { + try + { + result.Add(new ProcessUsageInfo + { + Id = p.Id, + Name = p.ProcessName, + MemoryUsage = p.WorkingSet64 + }); + } + catch { } + finally + { + p.Dispose(); + } + } + + return result.OrderByDescending(p => p.MemoryUsage).Take(topN).ToList(); + } + + /// + /// 获取运行中的进程数量 + /// + /// 进程数量 + public static int GetRunningProcessCount() + { + return Process.GetProcesses().Length; + } + + #endregion + + #region 系统信息 + + /// + /// 获取系统综合信息 + /// + /// 系统信息 + public static SystemInfo GetSystemInfo() + { + return new SystemInfo + { + MachineName = Environment.MachineName, + UserName = Environment.UserName, + OsVersion = RuntimeInformation.OSDescription, + RuntimeVersion = RuntimeInformation.FrameworkDescription, + ProcessorCount = Environment.ProcessorCount, + SystemDirectory = Environment.SystemDirectory, + CurrentDirectory = Environment.CurrentDirectory, + SystemUptime = GetSystemUptime(), + CpuMetrics = GetCpuMetrics(), + MemoryMetrics = GetMemoryMetrics(), + DiskMetrics = GetDiskMetrics() + }; + } + + /// + /// 获取系统运行时间 + /// + /// 运行时间 + public static TimeSpan GetSystemUptime() + { +#if NET5_0_OR_GREATER + return TimeSpan.FromMilliseconds(Environment.TickCount64); +#else + // 使用 Environment.TickCount 作为备选(会有溢出问题,但兼容性更好) + return TimeSpan.FromMilliseconds(Environment.TickCount); +#endif + } + + #endregion + + #region 实时监控 + + /// + /// 创建系统监控器 + /// + /// 监控间隔 + /// 系统监控器实例 + public static SystemMonitor CreateMonitor(TimeSpan? interval = null) + { + return new SystemMonitor(interval ?? TimeSpan.FromSeconds(1)); + } + + #endregion + } + + #region 数据类 + + /// + /// CPU 监控指标 + /// + public class CpuMetrics + { + /// + /// 处理器核心数 + /// + public int ProcessorCount { get; set; } + + /// + /// 当前使用率(%) + /// + public float CurrentUsage { get; set; } + + public override string ToString() + { + return $"核心数: {ProcessorCount}, 使用率: {CurrentUsage:F1}%"; + } + } + + /// + /// 内存监控指标 + /// + public class MemoryMetrics + { + /// + /// 总物理内存(字节) + /// + public long TotalPhysicalMemory { get; set; } + + /// + /// 可用物理内存(字节) + /// + public long AvailablePhysicalMemory { get; set; } + + /// + /// 已用物理内存(字节) + /// + public long UsedPhysicalMemory { get; set; } + + /// + /// 内存使用率(%) + /// + public float MemoryUsagePercent { get; set; } + + /// + /// 当前进程内存(字节) + /// + public long CurrentProcessMemory { get; set; } + + /// + /// 总物理内存(GB) + /// + public double TotalPhysicalMemoryGB => TotalPhysicalMemory / 1024.0 / 1024 / 1024; + + /// + /// 可用物理内存(GB) + /// + public double AvailablePhysicalMemoryGB => AvailablePhysicalMemory / 1024.0 / 1024 / 1024; + + /// + /// 已用物理内存(GB) + /// + public double UsedPhysicalMemoryGB => UsedPhysicalMemory / 1024.0 / 1024 / 1024; + + /// + /// 当前进程内存(MB) + /// + public double CurrentProcessMemoryMB => CurrentProcessMemory / 1024.0 / 1024; + + public override string ToString() + { + return $"总内存: {TotalPhysicalMemoryGB:F2}GB, 可用: {AvailablePhysicalMemoryGB:F2}GB, 使用率: {MemoryUsagePercent:F1}%"; + } + } + + /// + /// 磁盘监控指标 + /// + public class DiskMetrics + { + /// + /// 驱动器名称 + /// + public string? Name { get; set; } + + /// + /// 驱动器类型 + /// + public string? DriveType { get; set; } + + /// + /// 卷标 + /// + public string? VolumeLabel { get; set; } + + /// + /// 文件系统 + /// + public string? FileSystem { get; set; } + + /// + /// 总大小(字节) + /// + public long TotalSize { get; set; } + + /// + /// 总可用空间(字节) + /// + public long TotalFreeSpace { get; set; } + + /// + /// 可用空间(字节) + /// + public long AvailableFreeSpace { get; set; } + + /// + /// 已用空间(字节) + /// + public long UsedSpace { get; set; } + + /// + /// 使用率(%) + /// + public float UsagePercent { get; set; } + + /// + /// 总大小(GB) + /// + public double TotalSizeGB => TotalSize / 1024.0 / 1024 / 1024; + + /// + /// 可用空间(GB) + /// + public double AvailableFreeSpaceGB => AvailableFreeSpace / 1024.0 / 1024 / 1024; + + public override string ToString() + { + return $"{Name} [{VolumeLabel}] - 总: {TotalSizeGB:F2}GB, 可用: {AvailableFreeSpaceGB:F2}GB, 使用率: {UsagePercent:F1}%"; + } + } + + /// + /// 网络接口信息 + /// + public class NetworkInterfaceInfo + { + /// + /// 名称 + /// + public string? Name { get; set; } + + /// + /// 描述 + /// + public string? Description { get; set; } + + /// + /// ID + /// + public string? Id { get; set; } + + /// + /// 类型 + /// + public string? Type { get; set; } + + /// + /// 状态 + /// + public string? Status { get; set; } + + /// + /// 速度(bps) + /// + public long Speed { get; set; } + + /// + /// 是否在线 + /// + public bool IsUp { get; set; } + + /// + /// IP 地址列表 + /// + public List? IpAddresses { get; set; } + + /// + /// 速度(Mbps) + /// + public double SpeedMbps => Speed / 1000000.0; + + public override string ToString() + { + return $"{Name} ({Type}) - {Status}, 速度: {SpeedMbps:F0}Mbps"; + } + } + + /// + /// 进程使用信息 + /// + public class ProcessUsageInfo + { + /// + /// 进程 ID + /// + public int Id { get; set; } + + /// + /// 进程名称 + /// + public string? Name { get; set; } + + /// + /// CPU 使用率(%) + /// + public float CpuUsage { get; set; } + + /// + /// 内存使用(字节) + /// + public long MemoryUsage { get; set; } + + /// + /// 内存使用(MB) + /// + public double MemoryUsageMB => MemoryUsage / 1024.0 / 1024; + + public override string ToString() + { + return $"[{Id}] {Name} - CPU: {CpuUsage:F1}%, 内存: {MemoryUsageMB:F1}MB"; + } + } + + /// + /// 系统综合信息 + /// + public class SystemInfo + { + /// + /// 机器名 + /// + public string? MachineName { get; set; } + + /// + /// 用户名 + /// + public string? UserName { get; set; } + + /// + /// 操作系统版本 + /// + public string? OsVersion { get; set; } + + /// + /// 运行时版本 + /// + public string? RuntimeVersion { get; set; } + + /// + /// 处理器核心数 + /// + public int ProcessorCount { get; set; } + + /// + /// 系统目录 + /// + public string? SystemDirectory { get; set; } + + /// + /// 当前目录 + /// + public string? CurrentDirectory { get; set; } + + /// + /// 系统运行时间 + /// + public TimeSpan SystemUptime { get; set; } + + /// + /// CPU 监控指标 + /// + public CpuMetrics? CpuMetrics { get; set; } + + /// + /// 内存监控指标 + /// + public MemoryMetrics? MemoryMetrics { get; set; } + + /// + /// 磁盘监控指标 + /// + public List? DiskMetrics { get; set; } + } + + /// + /// 系统监控器 + /// + public class SystemMonitor : IDisposable + { + private readonly TimeSpan _interval; + private Timer? _timer; + private bool _disposed; + + /// + /// 监控数据更新事件 + /// + public event EventHandler? DataUpdated; + + /// + /// 监控间隔 + /// + public TimeSpan Interval => _interval; + + /// + /// 是否正在监控 + /// + public bool IsMonitoring { get; private set; } + + internal SystemMonitor(TimeSpan interval) + { + _interval = interval; + } + + /// + /// 开始监控 + /// + public void Start() + { + if (_disposed) + throw new ObjectDisposedException(nameof(SystemMonitor)); + + if (IsMonitoring) + return; + + IsMonitoring = true; + _timer = new Timer(OnTimerCallback, null, _interval, _interval); + } + + /// + /// 停止监控 + /// + public void Stop() + { + if (!IsMonitoring) + return; + + IsMonitoring = false; + _timer?.Dispose(); + _timer = null; + } + + private void OnTimerCallback(object? state) + { + try + { + var data = new MonitorData + { + Timestamp = DateTime.Now, + CpuUsage = SystemMonitorUtil.GetCpuUsage(), + MemoryUsage = SystemMonitorUtil.GetMemoryUsage(), + CurrentProcessMemory = SystemMonitorUtil.GetCurrentProcessMemory(), + ProcessCount = SystemMonitorUtil.GetRunningProcessCount() + }; + + DataUpdated?.Invoke(this, new MonitorDataEventArgs { Data = data }); + } + catch + { + // 忽略监控异常 + } + } + + public void Dispose() + { + if (_disposed) + return; + + Stop(); + _disposed = true; + } + } + + /// + /// 监控数据 + /// + public class MonitorData + { + /// + /// 时间戳 + /// + public DateTime Timestamp { get; set; } + + /// + /// CPU 使用率(%) + /// + public float CpuUsage { get; set; } + + /// + /// 内存使用率(%) + /// + public float MemoryUsage { get; set; } + + /// + /// 当前进程内存(字节) + /// + public long CurrentProcessMemory { get; set; } + + /// + /// 进程数量 + /// + public int ProcessCount { get; set; } + } + + /// + /// 监控数据事件参数 + /// + public class MonitorDataEventArgs : EventArgs + { + /// + /// 监控数据 + /// + public MonitorData? Data { get; set; } + } + + #endregion +} diff --git a/EasyTool.Core/TextCategory/ChineseUtil.cs b/EasyTool.Core/TextCategory/ChineseUtil.cs new file mode 100644 index 0000000..9300407 --- /dev/null +++ b/EasyTool.Core/TextCategory/ChineseUtil.cs @@ -0,0 +1,336 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 中文处理工具类 + /// 提供简繁转换、中文数字转换、中文字符判断等功能 + /// + public static class ChineseUtil + { + #region 简繁转换 + + private static readonly Dictionary SimplifiedToTraditionalMap = new(); + private static readonly Dictionary TraditionalToSimplifiedMap = new(); + + static ChineseUtil() + { + InitSimplifiedTraditionalMap(); + } + + /// + /// 简体转繁体 + /// + public static string ToTraditional(string simplified) + { + if (string.IsNullOrEmpty(simplified)) + return simplified; + + var sb = new StringBuilder(simplified.Length); + foreach (var c in simplified) + { + sb.Append(SimplifiedToTraditionalMap.TryGetValue(c, out var traditional) ? traditional : c); + } + return sb.ToString(); + } + + /// + /// 繁体转简体 + /// + public static string ToSimplified(string traditional) + { + if (string.IsNullOrEmpty(traditional)) + return traditional; + + var sb = new StringBuilder(traditional.Length); + foreach (var c in traditional) + { + sb.Append(TraditionalToSimplifiedMap.TryGetValue(c, out var simplified) ? simplified : c); + } + return sb.ToString(); + } + + private static void InitSimplifiedTraditionalMap() + { + var pairs = "几幾,发發,历歷,后後,里裡,面麵,松鬆,干乾,干幹,于於,才纔,台臺,云雲,术術,板闆,表錶,别彆,卜蔔,布佈,冲衝,虫蟲,丑醜,党黨,斗鬥,范範,谷穀,胡鬍,回迴,汇匯,伙夥,饥饑,家傢,价價,姜薑,借藉,局侷,据據,克剋,夸誇,困睏,腊臘,蜡蠟,累纍,漓灕,厘釐,帘簾,蒙矇,弥彌,蔑衊,千韆,签籤,秋鞦,曲麯,舍捨,沈瀋,胜勝,松鬆,体體,涂塗,万萬,系係,纤纖,咸鹹,向嚮,吁籲,叶葉,佣傭,余餘,御禦,郁鬱,愿願,岳嶽,扎紮,制製,致緻,钟鐘,种種,周週,注註"; + + foreach (var pair in pairs.Split(',')) + { + var chars = pair.ToCharArray(); + if (chars.Length >= 2) + { + SimplifiedToTraditionalMap[chars[0]] = chars[1]; + TraditionalToSimplifiedMap[chars[1]] = chars[0]; + } + } + } + + #endregion + + #region 中文数字 + + private static readonly string[] ChineseDigits = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; + private static readonly string[] ChineseUnits = { "", "十", "百", "千" }; + private static readonly string[] ChineseBigUnits = { "", "万", "亿", "兆" }; + + /// + /// 数字转中文数字 + /// + public static string ToChineseNumber(long number) + { + if (number == 0) + return "零"; + + var result = new StringBuilder(); + var isNegative = number < 0; + if (isNegative) number = -number; + + var parts = new List(); + int unitIndex = 0; + + while (number > 0) + { + var part = number % 10000; + var partStr = ConvertPart((int)part); + if (!string.IsNullOrEmpty(partStr)) + { + if (unitIndex > 0) partStr += ChineseBigUnits[unitIndex]; + parts.Insert(0, partStr); + } + number /= 10000; + unitIndex++; + } + + result.Append(string.Join("", parts)); + var final = result.ToString(); + while (final.Contains("零零")) final = final.Replace("零零", "零"); + final = final.TrimEnd('零'); + if (final.StartsWith("一十")) final = final.Substring(1); + + return (isNegative ? "负" : "") + final; + } + + private static string ConvertPart(int number) + { + if (number == 0) return ""; + var result = new StringBuilder(); + var needZero = false; + + for (int i = 3; i >= 0; i--) + { + var digit = (number / (int)Math.Pow(10, i)) % 10; + if (digit == 0) needZero = true; + else + { + if (needZero) { result.Append("零"); needZero = false; } + result.Append(ChineseDigits[digit]); + if (i > 0) result.Append(ChineseUnits[i]); + } + } + return result.ToString(); + } + + /// + /// 数字转中文金额(大写) + /// + public static string ToChineseMoney(decimal amount) + { + var moneyDigits = new[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + + if (amount == 0) return "零元整"; + + var result = new StringBuilder(); + var isNegative = amount < 0; + if (isNegative) amount = -amount; + + var intPart = (long)amount; + if (intPart > 0) + { + result.Append(ConvertMoneyPart(intPart, moneyDigits)); + result.Append("元"); + } + + var decPart = (int)((amount - intPart) * 100); + if (decPart > 0) + { + var jiao = decPart / 10; + var fen = decPart % 10; + if (jiao > 0) { result.Append(moneyDigits[jiao]); result.Append("角"); } + if (fen > 0) { result.Append(moneyDigits[fen]); result.Append("分"); } + } + else + { + result.Append("整"); + } + + return (isNegative ? "负" : "") + result.ToString(); + } + + private static string ConvertMoneyPart(long number, string[] digits) + { + var result = new StringBuilder(); + var parts = new List(); + int unitIndex = 0; + var units = new[] { "", "拾", "佰", "仟" }; + var bigUnits = new[] { "", "万", "亿" }; + + while (number > 0) + { + var part = number % 10000; + var partStr = new StringBuilder(); + + for (int i = 3; i >= 0; i--) + { + var digit = (int)((part / Math.Pow(10, i)) % 10); + if (digit > 0) + { + partStr.Append(digits[digit]); + if (i > 0) partStr.Append(units[i]); + } + } + + if (partStr.Length > 0) + { + if (unitIndex > 0) partStr.Append(bigUnits[unitIndex]); + parts.Insert(0, partStr.ToString()); + } + number /= 10000; + unitIndex++; + } + + return string.Join("", parts); + } + + #endregion + + #region 中文字符判断 + + /// + /// 判断是否为中文字符 + /// + public static bool IsChinese(char c) + { + return c >= 0x4E00 && c <= 0x9FA5; + } + + /// + /// 判断字符串是否全部为中文 + /// + public static bool IsAllChinese(string text) + { + if (string.IsNullOrEmpty(text)) return false; + foreach (var c in text) + if (!IsChinese(c)) return false; + return true; + } + + /// + /// 判断字符串是否包含中文 + /// + public static bool ContainsChinese(string text) + { + if (string.IsNullOrEmpty(text)) return false; + foreach (var c in text) + if (IsChinese(c)) return true; + return false; + } + + /// + /// 获取中文字符数量 + /// + public static int CountChinese(string text) + { + if (string.IsNullOrEmpty(text)) return 0; + int count = 0; + foreach (var c in text) + if (IsChinese(c)) count++; + return count; + } + + #endregion + + #region 中文标点转换 + + /// + /// 中文标点转英文标点 + /// + public static string ToEnglishPunctuation(string text) + { + if (string.IsNullOrEmpty(text)) return text; + var map = new Dictionary + { + {',', ','}, {'。', '.'}, {'!', '!'}, {'?', '?'}, + {';', ';'}, {':', ':'}, {'(', '('}, {')', ')'}, + {'【', '['}, {'】', ']'}, {'《', '<'}, {'》', '>'} + }; + var sb = new StringBuilder(text.Length); + foreach (var c in text) + sb.Append(map.TryGetValue(c, out var en) ? en : c); + return sb.ToString(); + } + + /// + /// 英文标点转中文标点 + /// + public static string ToChinesePunctuation(string text) + { + if (string.IsNullOrEmpty(text)) return text; + var map = new Dictionary + { + {',', ','}, {'.', '。'}, {'!', '!'}, {'?', '?'}, + {';', ';'}, {':', ':'}, {'(', '('}, {')', ')'}, + {'[', '【'}, {']', '】'} + }; + var sb = new StringBuilder(text.Length); + foreach (var c in text) + sb.Append(map.TryGetValue(c, out var cn) ? cn : c); + return sb.ToString(); + } + + #endregion + + #region 获取中文长度 + + /// + /// 获取字符串显示宽度(中文为2,英文为1) + /// + public static int GetDisplayWidth(string text) + { + if (string.IsNullOrEmpty(text)) return 0; + int width = 0; + foreach (var c in text) + { + if (IsChinese(c) || c > 0xFF) + width += 2; + else + width += 1; + } + return width; + } + + /// + /// 按显示宽度截取字符串 + /// + public static string SubstringByWidth(string text, int width) + { + if (string.IsNullOrEmpty(text) || width <= 0) return ""; + int currentWidth = 0; + var result = new StringBuilder(); + + foreach (var c in text) + { + var charWidth = IsChinese(c) || c > 0xFF ? 2 : 1; + if (currentWidth + charWidth > width) + break; + result.Append(c); + currentWidth += charWidth; + } + return result.ToString(); + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/TextCategory/EncodingDetector.cs b/EasyTool.Core/TextCategory/EncodingDetector.cs new file mode 100644 index 0000000..6789357 --- /dev/null +++ b/EasyTool.Core/TextCategory/EncodingDetector.cs @@ -0,0 +1,268 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 文本编码检测工具类 + /// + public static class EncodingDetector + { + /// + /// 检测字节数组的编码 + /// + public static Encoding Detect(byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return Encoding.Default; + + // 检查BOM标记 + var bomEncoding = DetectByBom(bytes); + if (bomEncoding != null) + return bomEncoding; + + // 检查UTF-8 + if (IsValidUtf8(bytes)) + return Encoding.UTF8; + + // 检查是否为纯ASCII + if (IsAscii(bytes)) + return Encoding.ASCII; + + // 尝试检测中文编码 + var chineseEncoding = DetectChineseEncoding(bytes); + if (chineseEncoding != null) + return chineseEncoding; + + // 默认返回系统默认编码 + return Encoding.Default; + } + + /// + /// 通过BOM标记检测编码 + /// + public static Encoding? DetectByBom(byte[] bytes) + { + if (bytes.Length >= 3) + { + // UTF-8 BOM: EF BB BF + if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) + return Encoding.UTF8; + + // UTF-32 BE BOM: 00 00 FE FF + if (bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) + return Encoding.GetEncoding("UTF-32BE"); + + // UTF-32 LE BOM: FF FE 00 00 + if (bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0x00 && bytes[3] == 0x00) + return Encoding.GetEncoding("UTF-32LE"); + } + + if (bytes.Length >= 2) + { + // UTF-16 BE BOM: FE FF + if (bytes[0] == 0xFE && bytes[1] == 0xFF) + return Encoding.BigEndianUnicode; + + // UTF-16 LE BOM: FF FE + if (bytes[0] == 0xFF && bytes[1] == 0xFE) + return Encoding.Unicode; + } + + return null; + } + + /// + /// 验证是否为有效的UTF-8 + /// + public static bool IsValidUtf8(byte[] bytes) + { + int i = 0; + while (i < bytes.Length) + { + byte b = bytes[i]; + + if (b <= 0x7F) + { + // ASCII字符 + i++; + } + else if ((b & 0xE0) == 0xC0) + { + // 2字节序列 + if (i + 1 >= bytes.Length) return false; + if ((bytes[i + 1] & 0xC0) != 0x80) return false; + i += 2; + } + else if ((b & 0xF0) == 0xE0) + { + // 3字节序列 + if (i + 2 >= bytes.Length) return false; + if ((bytes[i + 1] & 0xC0) != 0x80) return false; + if ((bytes[i + 2] & 0xC0) != 0x80) return false; + i += 3; + } + else if ((b & 0xF8) == 0xF0) + { + // 4字节序列 + if (i + 3 >= bytes.Length) return false; + if ((bytes[i + 1] & 0xC0) != 0x80) return false; + if ((bytes[i + 2] & 0xC0) != 0x80) return false; + if ((bytes[i + 3] & 0xC0) != 0x80) return false; + i += 4; + } + else + { + return false; + } + } + return true; + } + + /// + /// 检测是否为纯ASCII + /// + public static bool IsAscii(byte[] bytes) + { + foreach (var b in bytes) + { + if (b > 0x7F) + return false; + } + return true; + } + + /// + /// 检测中文编码(GB2312、GBK、Big5) + /// + public static Encoding? DetectChineseEncoding(byte[] bytes) + { + // 尝试GB2312 + if (TryDecode(bytes, Encoding.GetEncoding("GB2312"), out var gb2312Text)) + { + if (ContainsValidChinese(gb2312Text)) + { + // 进一步检查是否更可能是GBK + if (IsLikelyGbk(bytes)) + return Encoding.GetEncoding("GBK"); + return Encoding.GetEncoding("GB2312"); + } + } + + // 尝试Big5(繁体中文) + if (TryDecode(bytes, Encoding.GetEncoding("Big5"), out var big5Text)) + { + if (ContainsValidChinese(big5Text)) + return Encoding.GetEncoding("Big5"); + } + + return null; + } + + private static bool TryDecode(byte[] bytes, Encoding encoding, out string text) + { + try + { + text = encoding.GetString(bytes); + return true; + } + catch + { + text = ""; + return false; + } + } + + private static bool ContainsValidChinese(string text) + { + int chineseCount = 0; + int otherCount = 0; + + foreach (var c in text) + { + if (IsChineseCharacter(c)) + chineseCount++; + else if (c > 127) + otherCount++; + } + + // 如果中文字符占比高,认为是有效的中文编码 + return chineseCount > 0 && (chineseCount > otherCount || otherCount == 0); + } + + private static bool IsChineseCharacter(char c) + { + // CJK统一汉字范围(BMP内的字符) + return c >= '\u4E00' && c <= '\u9FFF' || + c >= '\u3400' && c <= '\u4DBF' || + c >= '\uF900' && c <= '\uFAFF'; + } + + private static bool IsLikelyGbk(byte[] bytes) + { + int gbkSpecific = 0; + + for (int i = 0; i < bytes.Length - 1; i++) + { + byte b1 = bytes[i]; + byte b2 = bytes[i + 1]; + + // GBK扩展字符范围 + if (b1 >= 0x81 && b1 <= 0xFE) + { + // GB2312范围 + if (b2 >= 0x40 && b2 <= 0xFE && b2 != 0x7F) + { + // GBK特有字符(GB2312之外的) + if (b2 >= 0x40 && b2 <= 0x7E) + { + gbkSpecific++; + } + } + } + } + + return gbkSpecific > bytes.Length / 50; // 约2%的GBK特有字符 + } + + /// + /// 尝试将字节数组转换为字符串 + /// + public static string GetString(byte[] bytes, Encoding? preferredEncoding = null) + { + var encoding = preferredEncoding ?? Detect(bytes); + return encoding.GetString(bytes); + } + + /// + /// 从文件读取文本(自动检测编码) + /// + public static string ReadFileText(string filePath, Encoding? preferredEncoding = null) + { + var bytes = System.IO.File.ReadAllBytes(filePath); + return GetString(bytes, preferredEncoding); + } + + /// + /// 获取编码名称 + /// + public static string GetEncodingName(Encoding encoding) + { + return encoding.WebName.ToUpperInvariant() switch + { + "UTF-8" => "UTF-8", + "UTF-16" => "UTF-16 LE", + "UTF-16BE" => "UTF-16 BE", + "UTF-32" => "UTF-32 LE", + "UTF-32BE" => "UTF-32 BE", + "GB2312" => "GB2312", + "GBK" => "GBK", + "BIG5" => "Big5", + "US-ASCII" => "ASCII", + "ISO-8859-1" => "ISO-8859-1", + _ => encoding.WebName.ToUpperInvariant() + }; + } + } +} diff --git a/EasyTool.Core/TextCategory/HtmlSanitizerUtil.cs b/EasyTool.Core/TextCategory/HtmlSanitizerUtil.cs new file mode 100644 index 0000000..12d0578 --- /dev/null +++ b/EasyTool.Core/TextCategory/HtmlSanitizerUtil.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// HTML 清理工具类 + /// 提供安全的 HTML 过滤和清理功能 + /// + public static class HtmlSanitizerUtil + { + // 允许的安全标签 + private static readonly HashSet SafeTags = new(StringComparer.OrdinalIgnoreCase) + { + "a", "abbr", "acronym", "address", "area", "article", "aside", "b", "bdi", "bdo", + "blockquote", "br", "caption", "center", "cite", "code", "col", "colgroup", "dd", + "details", "div", "dl", "dt", "em", "figcaption", "figure", "font", "footer", "h1", + "h2", "h3", "h4", "h5", "h6", "header", "hr", "i", "img", "li", "main", "mark", + "nav", "ol", "p", "pre", "q", "s", "section", "small", "span", "strike", "strong", + "sub", "summary", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", + "tt", "u", "ul", "var", "wbr" + }; + + // 允许的安全属性 + private static readonly Dictionary> SafeAttributes = new(StringComparer.OrdinalIgnoreCase) + { + ["*"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "class", "id", "title", "lang", "dir" }, + ["a"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "href", "target", "rel", "name" }, + ["img"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "src", "alt", "width", "height", "loading" }, + ["font"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "color", "size", "face" }, + ["table"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "border", "cellpadding", "cellspacing", "width" }, + ["td"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "colspan", "rowspan", "width", "height", "align", "valign" }, + ["th"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "colspan", "rowspan", "width", "height", "align", "valign" }, + ["col"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "span", "width" }, + ["colgroup"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "span" }, + ["ol"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "start", "type" }, + ["ul"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "type" }, + ["li"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "value" }, + ["blockquote"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "cite" }, + ["q"] = new HashSet(StringComparer.OrdinalIgnoreCase) { "cite" } + }; + + // 危险属性模式 + private static readonly Regex DangerousAttributePattern = new Regex( + @"^\s*(on\w+|data-[^d]|formaction|xlink:href)\s*$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + // 危险 URL 协议 + private static readonly Regex DangerousUrlPattern = new Regex( + @"^\s*(javascript|vbscript|data(?!:image/))\s*:", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 清理 HTML,移除危险标签和属性 + /// + /// 原始 HTML + /// 允许的标签(可选,默认使用安全列表) + /// 允许的属性(可选) + /// 清理后的安全 HTML + public static string Sanitize(string html, IEnumerable? allowTags = null, IDictionary>? allowAttributes = null) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + var safeTags = allowTags != null + ? new HashSet(allowTags, StringComparer.OrdinalIgnoreCase) + : SafeTags; + + var safeAttributes = allowAttributes?.ToDictionary( + k => k.Key, + v => new HashSet(v.Value, StringComparer.OrdinalIgnoreCase), + StringComparer.OrdinalIgnoreCase) ?? SafeAttributes; + + return ProcessHtml(html, safeTags, safeAttributes); + } + + /// + /// 移除所有 HTML 标签,只保留文本 + /// + /// 原始 HTML + /// 纯文本 + public static string StripTags(string html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + // 移除脚本和样式 + html = Regex.Replace(html, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + html = Regex.Replace(html, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + + // 移除所有标签 + html = Regex.Replace(html, @"<[^>]+>", ""); + + // 解码 HTML 实体 + html = System.Net.WebUtility.HtmlDecode(html); + + // 清理多余空白 + html = Regex.Replace(html, @"\s+", " "); + + return html.Trim(); + } + + /// + /// 转义 HTML 特殊字符 + /// + /// 原始文本 + /// 转义后的 HTML + public static string Escape(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + return System.Net.WebUtility.HtmlEncode(text); + } + + /// + /// 反转义 HTML 实体 + /// + /// HTML 文本 + /// 解码后的文本 + public static string Unescape(string html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + return System.Net.WebUtility.HtmlDecode(html); + } + + /// + /// 检查 HTML 是否包含潜在危险内容 + /// + /// HTML 内容 + /// 是否包含危险内容 + public static bool ContainsDangerousContent(string html) + { + if (string.IsNullOrEmpty(html)) + return false; + + // 检查脚本标签 + if (Regex.IsMatch(html, @" + /// 提取所有链接 + /// + /// HTML 内容 + /// 链接列表 + public static List ExtractLinks(string html) + { + var links = new List(); + + if (string.IsNullOrEmpty(html)) + return links; + + var pattern = @"]*href\s*=\s*[""']([^""']+)[""'][^>]*>(.*?)"; + var matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); + + foreach (Match match in matches) + { + links.Add(new HtmlLink + { + Url = match.Groups[1].Value, + Text = StripTags(match.Groups[2].Value) + }); + } + + return links; + } + + /// + /// 提取所有图片 + /// + /// HTML 内容 + /// 图片列表 + public static List ExtractImages(string html) + { + var images = new List(); + + if (string.IsNullOrEmpty(html)) + return images; + + var pattern = @"]*src\s*=\s*[""']([^""']+)[""'][^>]*>"; + var matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase); + + foreach (Match match in matches) + { + var img = new HtmlImage { Src = match.Groups[1].Value }; + + // 提取 alt + var altMatch = Regex.Match(match.Value, @"alt\s*=\s*[""']([^""']*)[""']", RegexOptions.IgnoreCase); + if (altMatch.Success) + img.Alt = altMatch.Groups[1].Value; + + images.Add(img); + } + + return images; + } + + private static string ProcessHtml(string html, HashSet safeTags, Dictionary> safeAttributes) + { + var result = new StringBuilder(); + var pos = 0; + + while (pos < html.Length) + { + var tagStart = html.IndexOf('<', pos); + + if (tagStart < 0) + { + result.Append(html.Substring(pos)); + break; + } + + result.Append(html.Substring(pos, tagStart - pos)); + + var tagEnd = html.IndexOf('>', tagStart); + if (tagEnd < 0) + { + result.Append(html.Substring(tagStart)); + break; + } + + var tagContent = html.Substring(tagStart + 1, tagEnd - tagStart - 1); + + // 处理注释 + if (tagContent.StartsWith("!--")) + { + var commentEnd = html.IndexOf("-->", tagStart); + if (commentEnd > 0) + { + pos = commentEnd + 3; + continue; + } + } + + // 处理标签 + if (tagContent.StartsWith("/")) + { + // 结束标签 + var tagName = GetTagName(tagContent.Substring(1)); + if (safeTags.Contains(tagName)) + { + result.Append($""); + } + } + else + { + // 开始标签 + var tagName = GetTagName(tagContent); + if (safeTags.Contains(tagName)) + { + var cleanedAttributes = CleanAttributes(tagName, tagContent, safeAttributes); + var selfClosing = tagContent.TrimEnd().EndsWith("/"); + result.Append(selfClosing + ? $"<{tagName}{cleanedAttributes}/>" + : $"<{tagName}{cleanedAttributes}>"); + } + } + + pos = tagEnd + 1; + } + + return result.ToString(); + } + + private static string GetTagName(string tagContent) + { + var match = Regex.Match(tagContent, @"^(\w+)"); + return match.Success ? match.Groups[1].Value.ToLowerInvariant() : string.Empty; + } + + private static string CleanAttributes(string tagName, string tagContent, Dictionary> safeAttributes) + { + var safeAttrs = new HashSet(StringComparer.OrdinalIgnoreCase); + if (safeAttributes.TryGetValue("*", out var globalAttrs)) + safeAttrs.UnionWith(globalAttrs); + if (safeAttributes.TryGetValue(tagName, out var tagAttrs)) + safeAttrs.UnionWith(tagAttrs); + + var result = new StringBuilder(); + var matches = Regex.Matches(tagContent, @"(\w+)\s*=\s*[""']([^""']*)[""']"); + + foreach (Match match in matches) + { + var attrName = match.Groups[1].Value.ToLowerInvariant(); + var attrValue = match.Groups[2].Value; + + if (!safeAttrs.Contains(attrName)) + continue; + + if (DangerousAttributePattern.IsMatch(attrName)) + continue; + + // 检查 URL 属性 + if (attrName == "href" || attrName == "src") + { + if (DangerousUrlPattern.IsMatch(attrValue)) + continue; + } + + result.Append($" {attrName}=\"{Escape(attrValue)}\""); + } + + return result.ToString(); + } + } + + /// + /// HTML 链接 + /// + public class HtmlLink + { + /// + /// URL + /// + public string Url { get; set; } = string.Empty; + + /// + /// 链接文本 + /// + public string Text { get; set; } = string.Empty; + } + + /// + /// HTML 图片 + /// + public class HtmlImage + { + /// + /// 图片 URL + /// + public string Src { get; set; } = string.Empty; + + /// + /// 替代文本 + /// + public string Alt { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/TextCategory/KeywordExtractor.cs b/EasyTool.Core/TextCategory/KeywordExtractor.cs new file mode 100644 index 0000000..6272c20 --- /dev/null +++ b/EasyTool.Core/TextCategory/KeywordExtractor.cs @@ -0,0 +1,300 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 关键词提取工具类 + /// + public static class KeywordExtractor + { + /// + /// 中文停用词 + /// + private static readonly HashSet ChineseStopWords = new() + { + "的", "了", "在", "是", "我", "有", "和", "就", "不", "人", "都", "一", "一个", + "上", "也", "很", "到", "说", "要", "去", "你", "会", "着", "没有", "看", "好", + "自己", "这", "那", "什么", "他", "她", "它", "们", "这个", "那个", "哪个", + "怎么", "为什么", "因为", "所以", "但是", "然后", "如果", "可以", "可能", + "已经", "还是", "只是", "就是", "这样", "那样", "怎样", "这么", "那么", + "更", "最", "比", "而", "且", "或", "与", "及", "等", "等等", "之", "于", + "以", "为", "让", "把", "被", "从", "向", "对", "给", "跟", "像", "关于", + "通过", "按照", "根据", "由于", "为了", "既然", "无论", "不管", "即使", + "虽然", "即使", "哪怕", "只要", "除非", "假如", "倘若", "若是", "要是" + }; + + /// + /// 英文停用词 + /// + private static readonly HashSet EnglishStopWords = new(StringComparer.OrdinalIgnoreCase) + { + "a", "an", "the", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", + "by", "from", "as", "is", "was", "are", "were", "been", "be", "have", "has", "had", + "do", "does", "did", "will", "would", "could", "should", "may", "might", "must", + "shall", "can", "need", "dare", "ought", "used", "it", "its", "this", "that", + "these", "those", "i", "you", "he", "she", "we", "they", "what", "which", "who", + "whom", "whose", "where", "when", "why", "how", "all", "each", "every", "both", + "few", "more", "most", "other", "some", "such", "no", "not", "only", "same", "so", + "than", "too", "very", "just", "also", "now", "here", "there", "then", "once" + }; + + /// + /// 使用TF-IDF算法提取关键词 + /// + public static List ExtractByTfIdf(string text, int topN = 10, int minWordLength = 2) + { + // 分词(简单实现:按空格和标点分割) + var words = Tokenize(text, minWordLength); + + // 计算词频 + var wordFreq = new Dictionary(); + foreach (var word in words) + { + if (IsStopWord(word)) continue; + if (!wordFreq.ContainsKey(word)) + wordFreq[word] = 0; + wordFreq[word]++; + } + + // 计算TF-IDF(简化版,使用词频和词长作为权重) + var results = new List(); + var totalWords = words.Count; + + foreach (var kvp in wordFreq) + { + var tf = (double)kvp.Value / totalWords; + var wordLength = kvp.Key.Length; + + // 词长权重:较长的词可能更重要 + var lengthWeight = Math.Min(wordLength / 4.0, 1.0); + + // 简化的IDF:使用词的稀有度 + var idf = Math.Log((double)totalWords / kvp.Value + 1); + + var score = tf * idf * (1 + lengthWeight); + + results.Add(new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = score + }); + } + + return results.OrderByDescending(r => r.Score).Take(topN).ToList(); + } + + /// + /// 提取高频词 + /// + public static List ExtractTopWords(string text, int topN = 10, int minWordLength = 2) + { + var words = Tokenize(text, minWordLength); + + var wordFreq = new Dictionary(); + foreach (var word in words) + { + if (IsStopWord(word)) continue; + if (!wordFreq.ContainsKey(word)) + wordFreq[word] = 0; + wordFreq[word]++; + } + + return wordFreq + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 提取n-gram + /// + public static List ExtractNgrams(string text, int n = 2, int topN = 10) + { + var ngrams = new Dictionary(); + var cleanText = Regex.Replace(text, @"[\s\p{P}]+", " ").Trim(); + + for (int i = 0; i <= cleanText.Length - n; i++) + { + var ngram = cleanText.Substring(i, n); + if (!ngrams.ContainsKey(ngram)) + ngrams[ngram] = 0; + ngrams[ngram]++; + } + + return ngrams + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 提取中文短语(双字词组) + /// + public static List ExtractChinesePhrases(string text, int topN = 10) + { + var phrases = new Dictionary(); + var chinesePattern = new Regex(@"[\u4e00-\u9fa5]{2,}"); + + foreach (Match match in chinesePattern.Matches(text)) + { + var phrase = match.Value; + if (!phrases.ContainsKey(phrase)) + phrases[phrase] = 0; + phrases[phrase]++; + } + + // 过滤停用词 + var filtered = phrases + .Where(kvp => !IsStopWord(kvp.Key)) + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value * kvp.Key.Length // 长词权重更高 + }); + + return filtered.ToList(); + } + + /// + /// 提取英文短语 + /// + public static List ExtractEnglishPhrases(string text, int topN = 10) + { + var phrases = new Dictionary(); + var wordPattern = new Regex(@"\b[a-zA-Z]{2,}\b"); + + foreach (Match match in wordPattern.Matches(text)) + { + var word = match.Value.ToLower(); + if (!IsStopWord(word)) + { + if (!phrases.ContainsKey(word)) + phrases[word] = 0; + phrases[word]++; + } + } + + return phrases + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 分词 + /// + private static List Tokenize(string text, int minWordLength = 2) + { + var words = new List(); + + // 提取中文词 + var chinesePattern = new Regex(@"[\u4e00-\u9fa5]+"); + foreach (Match match in chinesePattern.Matches(text)) + { + var word = match.Value; + // 中文简单分词:提取双字词 + for (int i = 0; i < word.Length - 1; i++) + { + words.Add(word.Substring(i, 2)); + } + if (word.Length >= minWordLength) + { + words.Add(word); + } + } + + // 提取英文词 + var englishPattern = new Regex(@"\b[a-zA-Z]{2,}\b"); + foreach (Match match in englishPattern.Matches(text)) + { + words.Add(match.Value.ToLower()); + } + + // 提取数字 + var numberPattern = new Regex(@"\b\d+(\.\d+)?\b"); + foreach (Match match in numberPattern.Matches(text)) + { + words.Add(match.Value); + } + + return words; + } + + /// + /// 判断是否为停用词 + /// + private static bool IsStopWord(string word) + { + return ChineseStopWords.Contains(word) || EnglishStopWords.Contains(word); + } + + /// + /// 添加自定义停用词 + /// + public static void AddStopWords(IEnumerable words) + { + foreach (var word in words) + { + if (Regex.IsMatch(word, @"[\u4e00-\u9fa5]")) + { + ChineseStopWords.Add(word); + } + else + { + EnglishStopWords.Add(word.ToLower()); + } + } + } + } + + /// + /// 关键词结果 + /// + public class KeywordResult + { + /// + /// 关键词 + /// + public string Word { get; set; } = ""; + + /// + /// 出现频率 + /// + public int Frequency { get; set; } + + /// + /// 权重分数 + /// + public double Score { get; set; } + + public override string ToString() + { + return $"{Word} (频率:{Frequency}, 分数:{Score:F4})"; + } + } +} diff --git a/EasyTool.Core/TextCategory/MarkdownUtil.cs b/EasyTool.Core/TextCategory/MarkdownUtil.cs new file mode 100644 index 0000000..a11925e --- /dev/null +++ b/EasyTool.Core/TextCategory/MarkdownUtil.cs @@ -0,0 +1,485 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// Markdown工具类 + /// 提供Markdown解析和转换功能 + /// + public static class MarkdownUtil + { + /// + /// Markdown转HTML + /// + public static string ToHtml(string markdown) + { + if (string.IsNullOrEmpty(markdown)) + return string.Empty; + + var html = markdown; + + // 转义HTML特殊字符 + html = EscapeHtml(html); + + // 标题 + html = Regex.Replace(html, @"^###### (.+)$", "
$1
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^##### (.+)$", "
$1
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^#### (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^### (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^## (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^# (.+)$", "

$1

", RegexOptions.Multiline); + + // 代码块 + html = Regex.Replace(html, @"```(\w*)\n([\s\S]*?)```", "
$2
"); + html = Regex.Replace(html, @"`([^`]+)`", "$1"); + + // 粗体和斜体 + html = Regex.Replace(html, @"\*\*\*(.+?)\*\*\*", "$1"); + html = Regex.Replace(html, @"\*\*(.+?)\*\*", "$1"); + html = Regex.Replace(html, @"\*(.+?)\*", "$1"); + html = Regex.Replace(html, @"___(.+?)___", "$1"); + html = Regex.Replace(html, @"__(.+?)__", "$1"); + html = Regex.Replace(html, @"_(.+?)_", "$1"); + html = Regex.Replace(html, @"~~(.+?)~~", "$1"); + + // 链接和图片 + html = Regex.Replace(html, @"!\[([^\]]*)\]\(([^)]+)\)", "\"$1\""); + html = Regex.Replace(html, @"\[([^\]]+)\]\(([^)]+)\)", "$1"); + + // 引用 + html = Regex.Replace(html, @"^> (.+)$", "
$1
", RegexOptions.Multiline); + + // 水平线 + html = Regex.Replace(html, @"^---$", "
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^\*\*\*$", "
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^___$", "
", RegexOptions.Multiline); + + // 无序列表 + html = ProcessUnorderedList(html); + + // 有序列表 + html = ProcessOrderedList(html); + + // 表格 + html = ProcessTable(html); + + // 段落 + html = ProcessParagraphs(html); + + return html; + } + + /// + /// HTML转Markdown(基础实现) + /// + public static string FromHtml(string html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + var markdown = html; + + // 标题 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "# $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "## $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "#### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "##### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "###### $1", RegexOptions.IgnoreCase); + + // 链接 + markdown = Regex.Replace(markdown, @"]*href=""([^""]+)""[^>]*>(.*?)", "[$2]($1)", RegexOptions.IgnoreCase); + + // 图片 + markdown = Regex.Replace(markdown, @"]*src=""([^""]+)""[^>]*alt=""([^""]*)""[^>]*>", "![$2]($1)", RegexOptions.IgnoreCase); + + // 粗体和斜体 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "**$1**", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "**$1**", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "*$1*", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "*$1*", RegexOptions.IgnoreCase); + + // 代码 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "`$1`", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>]*>([\s\S]*?)", "```\n$1\n```", RegexOptions.IgnoreCase); + + // 引用 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "> $1", RegexOptions.IgnoreCase); + + // 列表 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "- $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>([\s\S]*?)", "$1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>([\s\S]*?)", "$1", RegexOptions.IgnoreCase); + + // 段落和换行 + markdown = Regex.Replace(markdown, @"", "\n", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)

", "$1\n\n", RegexOptions.IgnoreCase); + + // 清理其他标签 + markdown = Regex.Replace(markdown, @"<[^>]+>", ""); + + // 解码HTML实体 + markdown = UnescapeHtml(markdown); + + return markdown.Trim(); + } + + /// + /// 提取Markdown标题 + /// + public static List ExtractHeadings(string markdown) + { + var headings = new List(); + + if (string.IsNullOrEmpty(markdown)) + return headings; + + var lines = markdown.Split('\n'); + for (int i = 0; i < lines.Length; i++) + { + var line = lines[i]; + var match = Regex.Match(line, @"^(#{1,6})\s+(.+)$"); + if (match.Success) + { + headings.Add(new MarkdownHeading + { + Level = match.Groups[1].Value.Length, + Text = match.Groups[2].Value.Trim(), + LineNumber = i + 1 + }); + } + } + + return headings; + } + + /// + /// 提取Markdown中的所有链接 + /// + public static List ExtractLinks(string markdown) + { + var links = new List(); + + if (string.IsNullOrEmpty(markdown)) + return links; + + var regex = new Regex(@"\[([^\]]+)\]\(([^)]+)\)"); + var matches = regex.Matches(markdown); + + foreach (Match match in matches) + { + links.Add(new MarkdownLink + { + Text = match.Groups[1].Value, + Url = match.Groups[2].Value + }); + } + + return links; + } + + /// + /// 提取Markdown中的所有图片 + /// + public static List ExtractImages(string markdown) + { + var images = new List(); + + if (string.IsNullOrEmpty(markdown)) + return images; + + var regex = new Regex(@"!\[([^\]]*)\]\(([^)]+)\)"); + var matches = regex.Matches(markdown); + + foreach (Match match in matches) + { + images.Add(new MarkdownImage + { + Alt = match.Groups[1].Value, + Url = match.Groups[2].Value + }); + } + + return images; + } + + /// + /// 生成目录(TOC) + /// + public static string GenerateToc(string markdown) + { + var headings = ExtractHeadings(markdown); + var toc = new StringBuilder(); + + foreach (var heading in headings) + { + var indent = new string(' ', (heading.Level - 1) * 2); + var anchor = GenerateAnchor(heading.Text); + toc.AppendLine($"{indent}- [{heading.Text}](#{anchor})"); + } + + return toc.ToString(); + } + + /// + /// 简化Markdown(移除格式) + /// + public static string StripFormatting(string markdown) + { + if (string.IsNullOrEmpty(markdown)) + return string.Empty; + + var text = markdown; + + // 移除代码块 + text = Regex.Replace(text, @"```\w*\n[\s\S]*?```", ""); + text = Regex.Replace(text, @"`([^`]+)`", "$1"); + + // 移除标题标记 + text = Regex.Replace(text, @"^#{1,6}\s+", "", RegexOptions.Multiline); + + // 移除粗体、斜体、删除线 + text = Regex.Replace(text, @"\*\*\*(.+?)\*\*\*", "$1"); + text = Regex.Replace(text, @"\*\*(.+?)\*\*", "$1"); + text = Regex.Replace(text, @"\*(.+?)\*", "$1"); + text = Regex.Replace(text, @"~~(.+?)~~", "$1"); + + // 移除链接,保留文本 + text = Regex.Replace(text, @"!\[([^\]]*)\]\([^)]+\)", "$1"); + text = Regex.Replace(text, @"\[([^\]]+)\]\([^)]+\)", "$1"); + + // 移除引用标记 + text = Regex.Replace(text, @"^>\s+", "", RegexOptions.Multiline); + + // 移除列表标记 + text = Regex.Replace(text, @"^[\*\-\+]\s+", "", RegexOptions.Multiline); + text = Regex.Replace(text, @"^\d+\.\s+", "", RegexOptions.Multiline); + + return text.Trim(); + } + + private static string EscapeHtml(string text) + { + return text + .Replace("&", "&") + .Replace("<", "<") + .Replace(">", ">"); + } + + private static string UnescapeHtml(string text) + { + return text + .Replace("&", "&") + .Replace("<", "<") + .Replace(">", ">") + .Replace(""", "\"") + .Replace("'", "'"); + } + + private static string ProcessUnorderedList(string html) + { + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inList = false; + + foreach (var line in lines) + { + var match = Regex.Match(line, @"^[\*\-\+]\s+(.+)$"); + if (match.Success) + { + if (!inList) + { + result.AppendLine("
    "); + inList = true; + } + result.AppendLine($"
  • {match.Groups[1].Value}
  • "); + } + else + { + if (inList) + { + result.AppendLine("
"); + inList = false; + } + result.AppendLine(line); + } + } + + if (inList) + result.AppendLine(""); + + return result.ToString(); + } + + private static string ProcessOrderedList(string html) + { + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inList = false; + + foreach (var line in lines) + { + var match = Regex.Match(line, @"^\d+\.\s+(.+)$"); + if (match.Success) + { + if (!inList) + { + result.AppendLine("
    "); + inList = true; + } + result.AppendLine($"
  1. {match.Groups[1].Value}
  2. "); + } + else + { + if (inList) + { + result.AppendLine("
"); + inList = false; + } + result.AppendLine(line); + } + } + + if (inList) + result.AppendLine(""); + + return result.ToString(); + } + + private static string ProcessTable(string html) + { + // 简单的表格处理 + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inTable = false; + + foreach (var line in lines) + { + if (line.Trim().StartsWith("|") && line.Trim().EndsWith("|")) + { + if (!inTable) + { + result.AppendLine(""); + inTable = true; + } + + var cells = line.Trim('|').Split('|'); + var isHeader = line.Contains("---"); + + if (!isHeader) + { + result.AppendLine(""); + foreach (var cell in cells) + { + result.AppendLine($""); + } + result.AppendLine(""); + } + } + else + { + if (inTable) + { + result.AppendLine("
{cell.Trim()}
"); + inTable = false; + } + result.AppendLine(line); + } + } + + return result.ToString(); + } + + private static string ProcessParagraphs(string html) + { + var lines = html.Split(new[] { "\n\n" }, StringSplitOptions.None); + var result = new StringBuilder(); + + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (!string.IsNullOrEmpty(trimmed) && + !trimmed.StartsWith("{trimmed}

"); + } + else + { + result.AppendLine(trimmed); + } + } + + return result.ToString(); + } + + private static string GenerateAnchor(string text) + { + var anchor = text.ToLower(); + anchor = Regex.Replace(anchor, @"[^\w\s-]", ""); + anchor = Regex.Replace(anchor, @"\s+", "-"); + return anchor; + } + } + + /// + /// Markdown标题 + /// + public class MarkdownHeading + { + /// + /// 标题级别(1-6) + /// + public int Level { get; set; } + + /// + /// 标题文本 + /// + public string Text { get; set; } = string.Empty; + + /// + /// 行号 + /// + public int LineNumber { get; set; } + } + + /// + /// Markdown链接 + /// + public class MarkdownLink + { + /// + /// 链接文本 + /// + public string Text { get; set; } = string.Empty; + + /// + /// 链接URL + /// + public string Url { get; set; } = string.Empty; + } + + /// + /// Markdown图片 + /// + public class MarkdownImage + { + /// + /// 替代文本 + /// + public string Alt { get; set; } = string.Empty; + + /// + /// 图片URL + /// + public string Url { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/TextCategory/SegmenterUtil.cs b/EasyTool.Core/TextCategory/SegmenterUtil.cs new file mode 100644 index 0000000..3350eac --- /dev/null +++ b/EasyTool.Core/TextCategory/SegmenterUtil.cs @@ -0,0 +1,433 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 分词模式 + /// + public enum SegmentMode + { + /// + /// 精确模式 + /// + Exact, + + /// + /// 全模式 + /// + Full, + + /// + /// 搜索引擎模式 + /// + Search + } + + /// + /// 中文分词工具类 + /// 提供基础的中文分词功能(基于词典) + /// + public static class SegmenterUtil + { + private static readonly HashSet _defaultDictionary = new HashSet(StringComparer.OrdinalIgnoreCase); + private static readonly HashSet _punctuation = new HashSet + { + '\uFF0C', '\u3002', '\uFF01', '\uFF1F', '\uFF1B', '\uFF1A', // 中文标点:,。!?;: + '\u201C', '\u201D', '\u2018', '\u2019', // 中文引号:""'' + '\uFF08', '\uFF09', '\u3010', '\u3011', '\u300A', '\u300B', // 中文括号:()【】《》 + '\u3001', '\u2026', // 中文其他:、… + ',', '.', '!', '?', ';', ':', '"', '\'', // 英文标点 + '(', ')', '[', ']', '<', '>', '/', '\\', '@', '#', '$', '%', '^', '&', '*' // 英文符号 + }; + + private static readonly HashSet _stopWords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "的", "了", "在", "是", "我", "有", "和", "就", "不", "人", "都", "一", "一个", + "上", "也", "很", "到", "说", "要", "去", "你", "会", "着", "没有", "看", "好", + "自己", "这", "那", "但", "而", "与", "或", "因为", "所以", "如果", "虽然", + "可以", "什么", "怎么", "如何", "为什么", "哪", "哪里", "哪个", "谁", "多少" + }; + + static SegmenterUtil() + { + // 初始化默认词典 + InitializeDefaultDictionary(); + } + + /// + /// 分词 + /// + /// 文本 + /// 分词模式 + /// 词语列表 + public static List Segment(string text, SegmentMode mode = SegmentMode.Exact) + { + if (string.IsNullOrWhiteSpace(text)) + return new List(); + + var result = new List(); + var words = new List(); + var buffer = new StringBuilder(); + + int i = 0; + while (i < text.Length) + { + // 跳过空白字符 + if (char.IsWhiteSpace(text[i])) + { + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + i++; + continue; + } + + // 处理标点符号 + if (_punctuation.Contains(text[i])) + { + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + i++; + continue; + } + + // 处理英文和数字 + if (IsEnglishOrDigit(text[i])) + { + if (buffer.Length > 0 && !IsEnglishOrDigit(buffer[buffer.Length - 1])) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + buffer.Append(text[i]); + i++; + continue; + } + + // 处理中文 + buffer.Append(text[i]); + i++; + + // 尝试匹配词典中的词 + if (buffer.Length > 0 && !IsEnglishOrDigit(buffer[0])) + { + var matched = TryMatchWord(buffer.ToString(), out var matchedWord); + if (matched) + { + // 检查是否可以匹配更长的词 + if (i < text.Length && !IsEnglishOrDigit(text[i])) + { + var extended = buffer.ToString() + text[i]; + if (_defaultDictionary.Contains(extended)) + { + continue; + } + } + + words.Add(matchedWord); + buffer.Clear(); + } + } + } + + // 处理剩余的 buffer + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + } + + return mode switch + { + SegmentMode.Full => GetAllPossibleWords(text), + SegmentMode.Search => GetSearchModeWords(words), + _ => words + }; + } + + /// + /// 分词并过滤停用词 + /// + /// 文本 + /// 分词模式 + /// 词语列表(不含停用词) + public static List SegmentWithoutStopWords(string text, SegmentMode mode = SegmentMode.Exact) + { + return Segment(text, mode) + .Where(w => !_stopWords.Contains(w)) + .ToList(); + } + + /// + /// 提取关键词 + /// + /// 文本 + /// 返回前N个关键词 + /// 关键词列表 + public static List ExtractKeywords(string text, int topN = 10) + { + var words = SegmentWithoutStopWords(text); + var frequency = new Dictionary(); + + foreach (var word in words) + { + if (word.Length < 2) + continue; + + if (frequency.ContainsKey(word)) + frequency[word]++; + else + frequency[word] = 1; + } + + return frequency + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => kvp.Key) + .ToList(); + } + + /// + /// 添加自定义词典 + /// + /// 词语列表 + public static void AddToDictionary(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _defaultDictionary.Add(word.Trim()); + } + } + } + + /// + /// 添加停用词 + /// + /// 停用词列表 + public static void AddStopWords(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _stopWords.Add(word.Trim()); + } + } + } + + /// + /// 检查是否为中文 + /// + /// 字符 + /// 是否为中文 + public static bool IsChinese(char c) + { + return c >= 0x4E00 && c <= 0x9FA5; + } + + /// + /// 检查是否为英文或数字 + /// + /// 字符 + /// 是否为英文或数字 + public static bool IsEnglishOrDigit(char c) + { + return (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + char.IsDigit(c); + } + + /// + /// 统计词频 + /// + /// 文本 + /// 词频字典 + public static Dictionary GetWordFrequency(string text) + { + var words = SegmentWithoutStopWords(text); + var frequency = new Dictionary(); + + foreach (var word in words) + { + if (frequency.ContainsKey(word)) + frequency[word]++; + else + frequency[word] = 1; + } + + return frequency; + } + + #region 私有方法 + + private static void InitializeDefaultDictionary() + { + // 常用词汇 + var commonWords = new[] + { + "中国", "北京", "上海", "广州", "深圳", "杭州", "南京", "武汉", "成都", "西安", + "计算机", "互联网", "软件", "硬件", "程序", "开发", "设计", "测试", "运维", "管理", + "公司", "企业", "集团", "有限", "责任", "股份", "有限", "科技", "技术", "信息", + "手机", "电脑", "笔记本", "平板", "显示器", "键盘", "鼠标", "耳机", "音箱", + "汽车", "火车", "飞机", "地铁", "公交", "出租车", "自行车", "电动车", + "今天", "明天", "昨天", "上午", "下午", "晚上", "中午", "早上", "傍晚", + "时间", "地点", "人物", "事件", "原因", "结果", "过程", "方法", "步骤", + "学习", "工作", "生活", "娱乐", "运动", "休息", "旅游", "购物", "吃饭", + "银行", "医院", "学校", "超市", "商场", "餐厅", "酒店", "公园", "图书馆", + "苹果", "香蕉", "橙子", "西瓜", "葡萄", "草莓", "芒果", "桃子", "梨子", + "开始", "结束", "继续", "暂停", "停止", "运行", "执行", "完成", "失败", "成功", + "问题", "答案", "解决", "方案", "建议", "意见", "观点", "看法", "想法", "思路", + "重要", "紧急", "必要", "可能", "必须", "应该", "需要", "想要", "希望", "期待", + "人工智能", "机器学习", "深度学习", "自然语言", "计算机视觉", "数据分析", + "云计算", "大数据", "区块链", "物联网", "虚拟现实", "增强现实", + "程序员", "工程师", "设计师", "产品经理", "项目经理", "架构师", "测试工程师" + }; + + foreach (var word in commonWords) + { + _defaultDictionary.Add(word); + } + } + + private static void ProcessBuffer(StringBuilder buffer, List words) + { + var text = buffer.ToString(); + + if (string.IsNullOrWhiteSpace(text)) + return; + + // 如果是英文或数字,直接添加 + if (text.All(c => IsEnglishOrDigit(c) || char.IsWhiteSpace(c))) + { + words.Add(text.Trim()); + return; + } + + // 对于中文,进行最大匹配分词 + var segments = MaxMatchSegment(text); + words.AddRange(segments); + } + + private static List MaxMatchSegment(string text) + { + var result = new List(); + var maxLength = 5; // 最大词长 + var i = 0; + + while (i < text.Length) + { + var matched = false; + + // 从最大长度开始匹配 + for (var len = Math.Min(maxLength, text.Length - i); len >= 1; len--) + { + var word = text.Substring(i, len); + + if (_defaultDictionary.Contains(word)) + { + result.Add(word); + i += len; + matched = true; + break; + } + } + + if (!matched) + { + // 单字切分 + result.Add(text[i].ToString()); + i++; + } + } + + return result; + } + + private static bool TryMatchWord(string text, out string matchedWord) + { + matchedWord = string.Empty; + + if (string.IsNullOrEmpty(text)) + return false; + + // 精确匹配 + if (_defaultDictionary.Contains(text)) + { + matchedWord = text; + return true; + } + + // 尝试匹配最长前缀词 + for (int len = text.Length; len >= 1; len--) + { + var prefix = text.Substring(0, len); + if (_defaultDictionary.Contains(prefix)) + { + matchedWord = prefix; + return true; + } + } + + return false; + } + + private static List GetAllPossibleWords(string text) + { + var result = new List(); + + for (int i = 0; i < text.Length; i++) + { + for (int len = 1; len <= text.Length - i && len <= 5; len++) + { + var word = text.Substring(i, len); + if (_defaultDictionary.Contains(word)) + { + result.Add(word); + } + } + } + + return result; + } + + private static List GetSearchModeWords(List words) + { + var result = new List(); + + foreach (var word in words) + { + result.Add(word); + + // 对长词进行二次切分 + if (word.Length > 2) + { + for (int len = 2; len < word.Length; len++) + { + for (int i = 0; i <= word.Length - len; i++) + { + var subWord = word.Substring(i, len); + if (_defaultDictionary.Contains(subWord)) + { + result.Add(subWord); + } + } + } + } + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/SlugUtil.cs b/EasyTool.Core/TextCategory/SlugUtil.cs index 6238b4b..23bedbe 100644 --- a/EasyTool.Core/TextCategory/SlugUtil.cs +++ b/EasyTool.Core/TextCategory/SlugUtil.cs @@ -1,127 +1,120 @@ using System; +using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace EasyTool.TextCategory { /// - /// URL Slug 工具类 - /// 用于生成 URL 友好的字符串标识符 + /// URL Slug 生成工具类 + /// 用于生成友好的 URL 路径 /// public static class SlugUtil { /// - /// 默认最大长度 - /// - private const int DefaultMaxLength = 100; - - /// - /// 生成 URL 友好的 Slug + /// 生成 URL Slug /// /// 原始文本 - /// 最大长度 + /// 生成选项 /// Slug 字符串 - public static string Generate(string? text, int maxLength = DefaultMaxLength) + public static string Generate(string text, SlugOptions? options = null) { - if (string.IsNullOrWhiteSpace(text)) + if (string.IsNullOrEmpty(text)) return string.Empty; - // 转小写 - var result = text.ToLowerInvariant(); + options ??= new SlugOptions(); + + var result = text; + + // 转换为小写 + if (options.Lowercase) + { + result = result.ToLowerInvariant(); + } + + // 音译非拉丁字符 + result = Transliterate(result); - // 中文转拼音首字母(简化处理) - result = ConvertChineseToPinyin(result); + // 移除 HTML 标签 + if (options.StripHtml) + { + result = Regex.Replace(result, @"<[^>]+>", ""); + } - // 移除特殊字符,保留字母、数字、中文 - result = Regex.Replace(result, @"[^a-z0-9\u4e00-\u9fa5\s-]", ""); + // 移除特殊字符 + result = Regex.Replace(result, @"[^\w\s\-]", ""); - // 将空格和多个连续空格替换为单个连字符 - result = Regex.Replace(result, @"\s+", "-"); + // 替换空格 + result = Regex.Replace(result, @"\s+", options.Delimiter.ToString()); - // 将多个连字符合并为一个 - result = Regex.Replace(result, @"-+", "-"); + // 替换多个分隔符 + var delimiterPattern = Regex.Escape(options.Delimiter.ToString()); + result = Regex.Replace(result, $@"{delimiterPattern}+", options.Delimiter.ToString()); - // 移除首尾的连字符 - result = result.Trim('-'); + // 裁剪首尾分隔符 + result = result.Trim(options.Delimiter); - // 截断到指定长度 - if (result.Length > maxLength) + // 限制长度 + if (options.MaxLength > 0 && result.Length > options.MaxLength) { - result = result.Substring(0, maxLength).TrimEnd('-'); + result = result.Substring(0, options.MaxLength); + + // 确保不在单词中间截断 + var lastDelimiter = result.LastIndexOf(options.Delimiter); + if (lastDelimiter > options.MaxLength * 0.5) + { + result = result.Substring(0, lastDelimiter); + } } return result; } /// - /// 生成带时间戳的 Slug - /// - /// 原始文本 - /// 最大长度 - /// 带时间戳的 Slug - public static string GenerateWithTimestamp(string? text, int maxLength = DefaultMaxLength) - { - var slug = Generate(text, maxLength - 15); - var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); - - return string.IsNullOrEmpty(slug) ? timestamp : $"{slug}-{timestamp}"; - } - - /// - /// 生成带随机后缀的 Slug + /// 从标题生成 Slug /// - /// 原始文本 - /// 后缀长度 - /// 最大长度 - /// 带随机后缀的 Slug - public static string GenerateWithRandomSuffix(string? text, int suffixLength = 6, int maxLength = DefaultMaxLength) + /// 标题 + /// Slug 字符串 + public static string FromTitle(string title) { - var slug = Generate(text, maxLength - suffixLength - 1); - var suffix = GenerateRandomString(suffixLength); - - return string.IsNullOrEmpty(slug) ? suffix : $"{slug}-{suffix}"; + return Generate(title, new SlugOptions { MaxLength = 100 }); } /// - /// 生成唯一 Slug(检查重复时使用) + /// 生成唯一 Slug(添加数字后缀) /// /// 原始文本 - /// 检查是否存在的函数 - /// 最大长度 + /// 已存在的 Slug 集合 + /// 生成选项 /// 唯一的 Slug - public static string GenerateUnique(string? text, Func exists, int maxLength = DefaultMaxLength) + public static string GenerateUnique(string text, System.Collections.Generic.ISet existingSlugs, SlugOptions? options = null) { - var baseSlug = Generate(text, maxLength); - - if (string.IsNullOrEmpty(baseSlug)) - baseSlug = GenerateRandomString(8); + var baseSlug = Generate(text, options); - if (!exists(baseSlug)) + if (!existingSlugs.Contains(baseSlug)) return baseSlug; - for (int i = 1; i <= 100; i++) - { - var suffix = i == 1 ? "" : $"-{i}"; - var newSlug = baseSlug.Length + suffix.Length > maxLength - ? baseSlug.Substring(0, maxLength - suffix.Length) + suffix - : baseSlug + suffix; + var counter = 1; + string uniqueSlug; - if (!exists(newSlug)) - return newSlug; + do + { + uniqueSlug = $"{baseSlug}-{counter}"; + counter++; } + while (existingSlugs.Contains(uniqueSlug)); - // 如果还是冲突,添加随机后缀 - return GenerateWithRandomSuffix(baseSlug, 8, maxLength); + return uniqueSlug; } /// - /// 验证 Slug 是否有效 + /// 验证 Slug 格式 /// /// Slug 字符串 /// 是否有效 - public static bool IsValid(string? slug) + public static bool IsValid(string slug) { - if (string.IsNullOrWhiteSpace(slug)) + if (string.IsNullOrEmpty(slug)) return false; // 只允许小写字母、数字、连字符 @@ -129,123 +122,144 @@ public static bool IsValid(string? slug) } /// - /// 规范化 Slug - /// - /// 原始 Slug - /// 规范化后的 Slug - public static string Normalize(string? slug) - { - if (string.IsNullOrWhiteSpace(slug)) - return string.Empty; - - // 转小写 - var result = slug.ToLowerInvariant(); - - // 移除非法字符 - result = Regex.Replace(result, @"[^a-z0-9\s-]", ""); - - // 空格转连字符 - result = Regex.Replace(result, @"\s+", "-"); - - // 合并多个连字符 - result = Regex.Replace(result, @"-+", "-"); - - // 移除首尾连字符 - result = result.Trim('-'); - - return result; - } - - /// - /// 将 Slug 转换为标题格式 + /// 从 Slug 还原可读文本 /// /// Slug 字符串 - /// 标题格式字符串 - public static string ToTitle(string? slug) + /// 可读文本 + public static string ToReadable(string slug) { - if (string.IsNullOrWhiteSpace(slug)) + if (string.IsNullOrEmpty(slug)) return string.Empty; - var words = slug.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries); - var result = new StringBuilder(); + var result = slug.Replace('-', ' '); - foreach (var word in words) + // 首字母大写 + var words = result.Split(' '); + for (int i = 0; i < words.Length; i++) { - if (result.Length > 0) - result.Append(' '); - - result.Append(char.ToUpperInvariant(word[0]) + word.Substring(1)); + if (words[i].Length > 0) + { + words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1); + } } - return result.ToString(); + return string.Join(" ", words); } /// - /// 从标题生成 Slug(保留更多语义) + /// 音译非拉丁字符 /// - /// 标题 - /// 最大长度 - /// Slug - public static string FromTitle(string? title, int maxLength = DefaultMaxLength) + private static string Transliterate(string text) { - if (string.IsNullOrWhiteSpace(title)) - return string.Empty; - - // 移除常见停用词(可选) - var stopWords = new[] { "a", "an", "the", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", "by" }; - - var words = title.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); - var filteredWords = new System.Collections.Generic.List(); + var result = new StringBuilder(); - foreach (var word in words) + foreach (var c in text.Normalize(NormalizationForm.FormD)) { - var lower = word.ToLowerInvariant(); - if (Array.IndexOf(stopWords, lower) == -1) + // 移除变音符号 + var category = char.GetUnicodeCategory(c); + if (category != System.Globalization.UnicodeCategory.NonSpacingMark) { - filteredWords.Add(lower); + result.Append(c); } } - var result = string.Join("-", filteredWords); - return Generate(result, maxLength); - } + text = result.ToString().Normalize(NormalizationForm.FormC); + + // 中文拼音映射(常用字) + text = TransliterateChinese(text); - #region 私有方法 + // 其他常见音译 + text = text + .Replace("ß", "ss") + .Replace("æ", "ae") + .Replace("ø", "o") + .Replace("å", "a") + .Replace("ł", "l") + .Replace("ń", "n") + .Replace("ś", "s") + .Replace("ż", "z") + .Replace("ź", "z"); - private static string ConvertChineseToPinyin(string text) + return text; + } + + /// + /// 中文转拼音(简化版) + /// + private static string TransliterateChinese(string text) { - // 简化处理:移除中文字符(实际项目中可引入拼音库) - // 这里只做基础处理,实际应用建议使用 PinyinUtil var result = new StringBuilder(); foreach (var c in text) { - if (c >= 0x4E00 && c <= 0x9FA5) + var pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin)) + { + result.Append(pinyin); + result.Append('-'); + } + else { - // 中文字符,可以调用 PinyinUtil 获取拼音 - // 这里简化处理,移除中文 - continue; + result.Append(c); } - result.Append(c); } - return result.ToString(); + return result.ToString().TrimEnd('-'); } - private static string GenerateRandomString(int length) + /// + /// 获取汉字拼音(简化映射) + /// + private static string GetPinyin(char c) { - const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; - var random = new Random(); - var result = new char[length]; - - for (int i = 0; i < length; i++) + // 这里只提供一些常用字的映射,实际应用可以使用完整的拼音库 + var pinyinMap = new System.Collections.Generic.Dictionary { - result[i] = chars[random.Next(chars.Length)]; - } - - return new string(result); + {'你', "ni"}, {'好', "hao"}, {'是', "shi"}, {'我', "wo"}, {'他', "ta"}, + {'她', "ta"}, {'们', "men"}, {'的', "de"}, {'了', "le"}, {'在', "zai"}, + {'有', "you"}, {'和', "he"}, {'人', "ren"}, {'这', "zhe"}, {'中', "zhong"}, + {'大', "da"}, {'为', "wei"}, {'上', "shang"}, {'个', "ge"}, {'国', "guo"}, + {'到', "dao"}, {'说', "shuo"}, {'要', "yao"}, {'也', "ye"}, {'出', "chu"}, + {'会', "hui"}, {'可', "ke"}, {'能', "neng"}, {'对', "dui"}, {'生', "sheng"}, + {'而', "er"}, {'子', "zi"}, {'那', "na"}, {'得', "de"}, {'于', "yu"}, + {'着', "zhe"}, {'下', "xia"}, {'自', "zi"}, {'之', "zhi"}, {'年', "nian"}, + {'过', "guo"}, {'发', "fa"}, {'后', "hou"}, {'作', "zuo"}, {'里', "li"}, + {'用', "yong"}, {'道', "dao"}, {'行', "xing"}, {'所', "suo"}, {'然', "ran"}, + {'家', "jia"}, {'种', "zhong"}, {'事', "shi"}, {'成', "cheng"}, {'方', "fang"}, + {'多', "duo"}, {'经', "jing"}, {'么', "me"}, {'去', "qu"}, {'法', "fa"}, + {'学', "xue"}, {'如', "ru"}, {'都', "dou"}, {'同', "tong"}, {'现', "xian"}, + {'当', "dang"}, {'没', "mei"}, {'动', "dong"}, {'面', "mian"}, {'起', "qi"}, + {'看', "kan"}, {'定', "ding"}, {'天', "tian"}, {'分', "fen"}, {'还', "hai"}, + {'进', "jin"}, {'小', "xiao"}, {'其', "qi"} + }; + + return pinyinMap.TryGetValue(c, out var pinyin) ? pinyin : string.Empty; } + } - #endregion + /// + /// Slug 生成选项 + /// + public class SlugOptions + { + /// + /// 是否转换为小写 + /// + public bool Lowercase { get; set; } = true; + + /// + /// 分隔符 + /// + public char Delimiter { get; set; } = '-'; + + /// + /// 最大长度 + /// + public int MaxLength { get; set; } = 0; + + /// + /// 是否移除 HTML 标签 + /// + public bool StripHtml { get; set; } = true; } } diff --git a/EasyTool.Core/TextCategory/SpellCheckerUtil.cs b/EasyTool.Core/TextCategory/SpellCheckerUtil.cs new file mode 100644 index 0000000..1bb880c --- /dev/null +++ b/EasyTool.Core/TextCategory/SpellCheckerUtil.cs @@ -0,0 +1,340 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 拼写检查器 + /// 提供英文拼写检查和纠错功能 + /// + public static class SpellCheckerUtil + { + private static readonly HashSet _dictionary = new HashSet(StringComparer.OrdinalIgnoreCase); + private static readonly char[] _alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); + + static SpellCheckerUtil() + { + InitializeDictionary(); + } + + /// + /// 检查单词拼写是否正确 + /// + /// 单词 + /// 是否正确 + public static bool IsCorrect(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return true; + + return _dictionary.Contains(word.Trim().ToLowerInvariant()); + } + + /// + /// 获取拼写建议 + /// + /// 单词 + /// 最大建议数量 + /// 建议列表 + public static List GetSuggestions(string word, int maxSuggestions = 5) + { + if (string.IsNullOrWhiteSpace(word)) + return new List(); + + word = word.Trim().ToLowerInvariant(); + + // 如果拼写正确,返回空列表 + if (_dictionary.Contains(word)) + return new List(); + + var candidates = new Dictionary(); + + // 编辑距离为1的候选词 + var edits1 = GetEdits1(word); + foreach (var edit in edits1) + { + if (_dictionary.Contains(edit)) + { + candidates[edit] = 1; + } + } + + // 编辑距离为2的候选词(如果没有找到距离1的) + if (candidates.Count == 0) + { + foreach (var edit1 in edits1) + { + var edits2 = GetEdits1(edit1); + foreach (var edit2 in edits2) + { + if (_dictionary.Contains(edit2) && !candidates.ContainsKey(edit2)) + { + candidates[edit2] = 2; + } + } + } + } + + return candidates + .OrderBy(kvp => kvp.Value) + .ThenBy(kvp => LevenshteinDistance(word, kvp.Key)) + .Take(maxSuggestions) + .Select(kvp => kvp.Key) + .ToList(); + } + + /// + /// 检查文本中的拼写错误 + /// + /// 文本 + /// 错误单词及其建议 + public static Dictionary> CheckText(string text) + { + var result = new Dictionary>(); + + if (string.IsNullOrWhiteSpace(text)) + return result; + + var words = ExtractWords(text); + + foreach (var word in words) + { + if (!IsCorrect(word) && !result.ContainsKey(word)) + { + result[word] = GetSuggestions(word); + } + } + + return result; + } + + /// + /// 自动纠正拼写错误 + /// + /// 文本 + /// 纠正后的文本 + public static string AutoCorrect(string text) + { + if (string.IsNullOrWhiteSpace(text)) + return text; + + var words = ExtractWords(text); + var result = text; + + foreach (var word in words) + { + if (!IsCorrect(word)) + { + var suggestions = GetSuggestions(word, 1); + if (suggestions.Count > 0) + { + result = ReplaceWord(result, word, suggestions[0]); + } + } + } + + return result; + } + + /// + /// 添加单词到词典 + /// + /// 单词列表 + public static void AddToDictionary(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _dictionary.Add(word.Trim().ToLowerInvariant()); + } + } + } + + /// + /// 从文件加载词典 + /// + /// 文件路径 + public static void LoadDictionary(string filePath) + { + try + { + var lines = System.IO.File.ReadAllLines(filePath); + AddToDictionary(lines); + } + catch (Exception) + { + // 忽略错误 + } + } + + /// + /// 获取词典大小 + /// + /// 词典单词数量 + public static int GetDictionarySize() + { + return _dictionary.Count; + } + + #region 私有方法 + + private static void InitializeDictionary() + { + // 常用英语单词 + var commonWords = new[] + { + "the", "be", "to", "of", "and", "a", "in", "that", "have", "i", + "it", "for", "not", "on", "with", "he", "as", "you", "do", "at", + "this", "but", "his", "by", "from", "they", "we", "say", "her", "she", + "or", "an", "will", "my", "one", "all", "would", "there", "their", "what", + "so", "up", "out", "if", "about", "who", "get", "which", "go", "me", + "when", "make", "can", "like", "time", "no", "just", "him", "know", "take", + "people", "into", "year", "your", "good", "some", "could", "them", "see", "other", + "than", "then", "now", "look", "only", "come", "its", "over", "think", "also", + "back", "after", "use", "two", "how", "our", "work", "first", "well", "way", + "even", "new", "want", "because", "any", "these", "give", "day", "most", "us", + "hello", "world", "computer", "program", "software", "hardware", "system", "network", + "internet", "website", "application", "development", "design", "testing", "code", + "data", "database", "server", "client", "user", "password", "email", "message", + "file", "folder", "directory", "document", "image", "video", "audio", "music", + "game", "play", "player", "team", "sport", "football", "basketball", "tennis", + "school", "student", "teacher", "class", "lesson", "book", "read", "write", + "learn", "study", "exam", "test", "question", "answer", "problem", "solution", + "work", "job", "office", "company", "business", "money", "price", "cost", + "buy", "sell", "shop", "store", "market", "product", "service", "customer", + "food", "drink", "water", "coffee", "tea", "breakfast", "lunch", "dinner", + "house", "home", "room", "door", "window", "bed", "table", "chair", "kitchen", + "car", "bus", "train", "plane", "airport", "station", "road", "street", "city", + "country", "world", "earth", "sun", "moon", "star", "sky", "weather", "rain", + "love", "hate", "happy", "sad", "angry", "tired", "hungry", "thirsty", "sleep", + "family", "mother", "father", "brother", "sister", "child", "baby", "friend", + "health", "doctor", "hospital", "medicine", "sick", "healthy", "exercise", + "phone", "call", "number", "address", "name", "age", "birthday", "date", + "time", "hour", "minute", "second", "week", "month", "year", "today", + "tomorrow", "yesterday", "morning", "afternoon", "evening", "night", + "spring", "summer", "autumn", "winter", "hot", "cold", "warm", "cool", + "big", "small", "large", "little", "long", "short", "high", "low", + "fast", "slow", "quick", "easy", "hard", "simple", "complex", "different" + }; + + foreach (var word in commonWords) + { + _dictionary.Add(word.ToLowerInvariant()); + } + } + + private static HashSet GetEdits1(string word) + { + var edits = new HashSet(); + + // 删除 + for (int i = 0; i < word.Length; i++) + { + edits.Add(word.Substring(0, i) + word.Substring(i + 1)); + } + + // 交换 + for (int i = 0; i < word.Length - 1; i++) + { + edits.Add(word.Substring(0, i) + word[i + 1] + word[i] + word.Substring(i + 2)); + } + + // 替换 + for (int i = 0; i < word.Length; i++) + { + foreach (var c in _alphabet) + { + edits.Add(word.Substring(0, i) + c + word.Substring(i + 1)); + } + } + + // 插入 + for (int i = 0; i <= word.Length; i++) + { + foreach (var c in _alphabet) + { + edits.Add(word.Substring(0, i) + c + word.Substring(i)); + } + } + + return edits; + } + + private static int LevenshteinDistance(string s1, string s2) + { + var matrix = new int[s1.Length + 1, s2.Length + 1]; + + for (int i = 0; i <= s1.Length; i++) + matrix[i, 0] = i; + + for (int j = 0; j <= s2.Length; j++) + matrix[0, j] = j; + + for (int i = 1; i <= s1.Length; i++) + { + for (int j = 1; j <= s2.Length; j++) + { + var cost = s1[i - 1] == s2[j - 1] ? 0 : 1; + matrix[i, j] = Math.Min( + Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1), + matrix[i - 1, j - 1] + cost); + } + } + + return matrix[s1.Length, s2.Length]; + } + + private static List ExtractWords(string text) + { + var words = new List(); + var currentWord = new System.Text.StringBuilder(); + + foreach (var c in text) + { + if (char.IsLetter(c)) + { + currentWord.Append(c); + } + else if (currentWord.Length > 0) + { + words.Add(currentWord.ToString()); + currentWord.Clear(); + } + } + + if (currentWord.Length > 0) + { + words.Add(currentWord.ToString()); + } + + return words; + } + + private static string ReplaceWord(string text, string oldWord, string newWord) + { + // 保持原始大小写 + var index = text.IndexOf(oldWord, StringComparison.OrdinalIgnoreCase); + if (index < 0) + return text; + + var originalWord = text.Substring(index, oldWord.Length); + + // 调整新词的大小写 + string replacement; + if (char.IsUpper(originalWord[0])) + { + replacement = char.ToUpper(newWord[0]) + newWord.Substring(1); + } + else + { + replacement = newWord; + } + + return text.Substring(0, index) + replacement + text.Substring(index + oldWord.Length); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/StringBuilderPool.cs b/EasyTool.Core/TextCategory/StringBuilderPool.cs new file mode 100644 index 0000000..c9dfebf --- /dev/null +++ b/EasyTool.Core/TextCategory/StringBuilderPool.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 字符串构建器池 + /// + public class StringBuilderPool + { + private readonly Stack _pool; + private readonly int _maxCapacity; + private readonly int _defaultCapacity; + private readonly object _lock = new(); + + /// + /// 默认实例 + /// + public static StringBuilderPool Default { get; } = new(); + + /// + /// 池中可用数量 + /// + public int AvailableCount + { + get + { + lock (_lock) + { + return _pool.Count; + } + } + } + + /// + /// 创建字符串构建器池 + /// + /// 最大池容量 + /// 默认StringBuilder容量 + /// 预分配数量 + public StringBuilderPool(int maxCapacity = 50, int defaultCapacity = 256, int preallocate = 5) + { + _maxCapacity = maxCapacity; + _defaultCapacity = defaultCapacity; + _pool = new Stack(maxCapacity); + + for (int i = 0; i < preallocate && i < maxCapacity; i++) + { + _pool.Push(new StringBuilder(defaultCapacity)); + } + } + + /// + /// 获取StringBuilder + /// + public StringBuilder Get() + { + lock (_lock) + { + if (_pool.Count > 0) + { + return _pool.Pop(); + } + } + return new StringBuilder(_defaultCapacity); + } + + /// + /// 归还StringBuilder + /// + public void Return(StringBuilder sb) + { + if (sb == null) return; + + // 清空内容 + sb.Clear(); + + // 如果容量过大,不归还 + if (sb.Capacity > _defaultCapacity * 4) + return; + + lock (_lock) + { + if (_pool.Count < _maxCapacity) + { + _pool.Push(sb); + } + } + } + + /// + /// 使用StringBuilder执行操作 + /// + public string Execute(Action action) + { + var sb = Get(); + try + { + action(sb); + return sb.ToString(); + } + finally + { + Return(sb); + } + } + + /// + /// 使用StringBuilder执行操作并返回结果 + /// + public TResult Execute(Func func) + { + var sb = Get(); + try + { + return func(sb); + } + finally + { + Return(sb); + } + } + + /// + /// 连接字符串 + /// + public static string Concat(IEnumerable values, string separator = "") + { + return Default.Execute(sb => + { + var first = true; + foreach (var value in values) + { + if (!first && !string.IsNullOrEmpty(separator)) + sb.Append(separator); + sb.Append(value); + first = false; + } + }); + } + + /// + /// 连接字符串 + /// + public static string Concat(IEnumerable values, string separator = "", Func? selector = null) + { + return Default.Execute(sb => + { + var first = true; + foreach (var value in values) + { + if (!first && !string.IsNullOrEmpty(separator)) + sb.Append(separator); + sb.Append(selector != null ? selector(value) : value?.ToString()); + first = false; + } + }); + } + + /// + /// 清空池 + /// + public void Clear() + { + lock (_lock) + { + _pool.Clear(); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/TextCategory/TemplateUtil.cs b/EasyTool.Core/TextCategory/TemplateUtil.cs index fe965bb..83f3d3b 100644 --- a/EasyTool.Core/TextCategory/TemplateUtil.cs +++ b/EasyTool.Core/TextCategory/TemplateUtil.cs @@ -1,365 +1,282 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; -using System.Text.RegularExpressions; namespace EasyTool.TextCategory { /// - /// 模板渲染工具类 - /// 支持变量替换、条件渲染、循环等 + /// 模板工具类 /// public static class TemplateUtil { - #region 简单变量替换 - /// - /// 渲染模板(使用 ${variable} 或 {{variable}} 语法) + /// 渲染模板(使用 ${var} 语法) /// - /// 模板字符串 - /// 变量字典 - /// 渲染后的字符串 - public static string Render(string template, Dictionary variables) + public static string Render(string template, IDictionary variables) { if (string.IsNullOrEmpty(template)) return template; - if (variables == null || variables.Count == 0) - return template; - - var result = template; + var result = new StringBuilder(template); + var start = 0; - // 支持 ${variable} 格式 - result = Regex.Replace(result, @"\$\{(\w+)\}", match => + while ((start = result.ToString().IndexOf("${", start)) >= 0) { - var key = match.Groups[1].Value; - return variables.TryGetValue(key, out var value) ? value?.ToString() ?? "" : ""; - }); + var end = result.ToString().IndexOf("}", start); + if (end < 0) break; - // 支持 {{variable}} 格式 - result = Regex.Replace(result, @"\{\{(\w+)\}\}", match => - { - var key = match.Groups[1].Value; - return variables.TryGetValue(key, out var value) ? value?.ToString() ?? "" : ""; - }); + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + if (variables.TryGetValue(varName, out var value)) + { + result.Remove(start, end - start + 1); + result.Insert(start, value?.ToString() ?? ""); + } + else + { + start = end + 1; + } + } - return result; + return result.ToString(); } /// /// 渲染模板(使用匿名对象) /// - /// 模板字符串 - /// 数据模型 - /// 渲染后的字符串 public static string Render(string template, object model) { - if (model == null) + if (string.IsNullOrEmpty(template)) return template; - var variables = new Dictionary(); - var properties = model.GetType().GetProperties(); - - foreach (var prop in properties) + var dict = new Dictionary(); + foreach (var prop in model.GetType().GetProperties()) { - variables[prop.Name] = prop.GetValue(model) ?? ""; + dict[prop.Name] = prop.GetValue(model); } - return Render(template, variables); + return Render(template, dict); } - #endregion - - #region 带默认值的渲染 - /// - /// 渲染模板(支持默认值) - /// 语法:${variable:default} 或 {{variable|default}} + /// 渲染模板(使用 {{var}} 语法) /// - /// 模板字符串 - /// 变量字典 - /// 渲染后的字符串 - public static string RenderWithDefault(string template, Dictionary variables) + public static string RenderMustache(string template, IDictionary variables) { if (string.IsNullOrEmpty(template)) return template; - var result = template; + var result = new StringBuilder(template); + var start = 0; - // ${variable:default} 格式 - result = Regex.Replace(result, @"\$\{(\w+):([^}]*)\}", match => + while ((start = result.ToString().IndexOf("{{", start)) >= 0) { - var key = match.Groups[1].Value; - var defaultValue = match.Groups[2].Value; + var end = result.ToString().IndexOf("}}", start); + if (end < 0) break; - if (variables != null && variables.TryGetValue(key, out var value) && value != null) + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + if (variables.TryGetValue(varName, out var value)) { - return value.ToString(); + result.Remove(start, end - start + 2); + result.Insert(start, value?.ToString() ?? ""); } - - return defaultValue; - }); - - // {{variable|default}} 格式 - result = Regex.Replace(result, @"\{\{(\w+)\|([^}]*)\}\}", match => - { - var key = match.Groups[1].Value; - var defaultValue = match.Groups[2].Value; - - if (variables != null && variables.TryGetValue(key, out var value) && value != null) + else { - return value.ToString(); + start = end + 2; } + } - return defaultValue; - }); - - return result; + return result.ToString(); } - #endregion - - #region 条件渲染 - /// - /// 条件渲染 - /// 语法:{{#if condition}}...{{/if}} - /// {{#if condition}}...{{else}}...{{/if}} + /// 渲染模板(带默认值) /// - public static string RenderConditional(string template, Dictionary variables) + public static string Render(string template, IDictionary variables, string defaultValue = "") { if (string.IsNullOrEmpty(template)) return template; - var result = template; + var result = new StringBuilder(template); + var start = 0; - // 处理 if-else 结构 - var ifElsePattern = @"\{\{#if\s+(\w+)\}\}(.*?)\{\{else\}\}(.*?)\{\{/if\}\}"; - result = Regex.Replace(result, ifElsePattern, match => + while ((start = result.ToString().IndexOf("${", start)) >= 0) { - var condition = match.Groups[1].Value; - var trueContent = match.Groups[2].Value; - var falseContent = match.Groups[3].Value; + var end = result.ToString().IndexOf("}", start); + if (end < 0) break; - if (variables != null && variables.TryGetValue(condition, out var value)) + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + + // 检查是否有默认值 (var:default) + string? defaultValueLocal = null; + var colonIndex = varName.IndexOf(':'); + if (colonIndex > 0) { - var isTrue = value switch - { - bool b => b, - string s => !string.IsNullOrEmpty(s), - int i => i != 0, - null => false, - _ => true - }; - - return isTrue ? trueContent : falseContent; + defaultValueLocal = varName.Substring(colonIndex + 1); + varName = varName.Substring(0, colonIndex); } - return falseContent; - }, RegexOptions.Singleline); - - // 处理简单 if 结构 - var ifPattern = @"\{\{#if\s+(\w+)\}\}(.*?)\{\{/if\}\}"; - result = Regex.Replace(result, ifPattern, match => - { - var condition = match.Groups[1].Value; - var content = match.Groups[2].Value; - - if (variables != null && variables.TryGetValue(condition, out var value)) + object? value; + if (variables.TryGetValue(varName, out value)) { - var isTrue = value switch - { - bool b => b, - string s => !string.IsNullOrEmpty(s), - int i => i != 0, - null => false, - _ => true - }; - - return isTrue ? content : ""; + result.Remove(start, end - start + 1); + result.Insert(start, value?.ToString() ?? defaultValueLocal ?? defaultValue); } + else + { + result.Remove(start, end - start + 1); + result.Insert(start, defaultValueLocal ?? defaultValue); + } + } - return ""; - }, RegexOptions.Singleline); - - return result; + return result.ToString(); } - #endregion - - #region 循环渲染 - /// - /// 循环渲染 - /// 语法:{{#each items}}...{{this}}...{{/each}} + /// 提取模板中的变量名 /// - public static string RenderLoop(string template, Dictionary variables) + public static List ExtractVariables(string template, string startTag = "${", string endTag = "}") { + var result = new List(); if (string.IsNullOrEmpty(template)) - return template; - - var result = template; - var eachPattern = @"\{\{#each\s+(\w+)\}\}(.*?)\{\{/each\}\}"; + return result; - result = Regex.Replace(result, eachPattern, match => + var start = 0; + while ((start = template.IndexOf(startTag, start)) >= 0) { - var listName = match.Groups[1].Value; - var itemTemplate = match.Groups[2].Value; + var end = template.IndexOf(endTag, start + startTag.Length); + if (end < 0) break; - if (variables == null || !variables.TryGetValue(listName, out var listValue)) - return ""; - - var items = listValue as IEnumerable; - if (items == null) - return ""; - - var sb = new StringBuilder(); - var index = 0; - - foreach (var item in items) + var varName = template.Substring(start + startTag.Length, end - start - startTag.Length).Trim(); + if (!string.IsNullOrEmpty(varName)) { - var itemResult = itemTemplate; - - // 替换 {{this}} - itemResult = itemResult.Replace("{{this}}", item?.ToString() ?? ""); - - // 替换 {{@index}} - itemResult = itemResult.Replace("{{@index}}", index.ToString()); - - // 替换 {{@first}} - itemResult = itemResult.Replace("{{@first}}", (index == 0).ToString().ToLower()); - - // 替换 {{@last}} - var isLast = index == (items as ICollection)?.Count - 1; - itemResult = itemResult.Replace("{{@last}}", isLast.ToString().ToLower()); + // 移除默认值部分 + var colonIndex = varName.IndexOf(':'); + if (colonIndex > 0) + varName = varName.Substring(0, colonIndex); - // 如果是对象,替换其属性 - if (item != null && !(item is string)) - { - var props = item.GetType().GetProperties(); - foreach (var prop in props) - { - itemResult = itemResult.Replace($"{{{{{prop.Name}}}}}", prop.GetValue(item)?.ToString() ?? ""); - } - } - - sb.Append(itemResult); - index++; + if (!result.Contains(varName)) + result.Add(varName); } - return sb.ToString(); - }, RegexOptions.Singleline); + start = end + endTag.Length; + } return result; } - #endregion - - #region 完整渲染 - /// - /// 完整渲染(包含变量、条件、循环) + /// 验证模板是否有未替换的变量 /// - /// 模板字符串 - /// 变量字典 - /// 渲染后的字符串 - public static string RenderFull(string template, Dictionary variables) + public static bool HasUnresolvedVariables(string template, string startTag = "${", string endTag = "}") { - if (string.IsNullOrEmpty(template)) - return template; - - var result = template; - - // 先处理循环 - result = RenderLoop(result, variables); - - // 再处理条件 - result = RenderConditional(result, variables); - - // 最后处理变量 - result = RenderWithDefault(result, variables); - - return result; + return template.Contains(startTag) && template.Contains(endTag); } - #endregion - - #region 模板缓存 - - private static readonly Dictionary _templateCache = new(); - private static readonly object _cacheLock = new(); - /// - /// 缓存模板 + /// 格式化字符串(类似Python的f-string) /// - /// 模板名称 - /// 模板内容 - public static void CacheTemplate(string name, string template) + public static string Format(string template, params object[] args) { - lock (_cacheLock) + if (string.IsNullOrEmpty(template) || args == null || args.Length == 0) + return template; + + var result = new StringBuilder(template); + for (int i = 0; i < args.Length; i++) { - _templateCache[name] = template; + result.Replace($"{{{i}}}", args[i]?.ToString() ?? ""); } + + return result.ToString(); } /// - /// 从缓存加载模板 + /// 条件渲染 /// - /// 模板名称 - /// 模板内容 - public static string? GetCachedTemplate(string name) + public static string RenderConditional(string template, IDictionary variables) { - lock (_cacheLock) + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(template); + + // 处理 {?condition}...{?} 条件块 + var start = 0; + while ((start = result.ToString().IndexOf("{?", start)) >= 0) { - return _templateCache.TryGetValue(name, out var template) ? template : null; + var endCondition = result.ToString().IndexOf("}", start); + if (endCondition < 0) break; + + var condition = result.ToString().Substring(start + 2, endCondition - start - 2).Trim(); + var endBlock = result.ToString().IndexOf("{?}", endCondition); + if (endBlock < 0) break; + + var content = result.ToString().Substring(endCondition + 1, endBlock - endCondition - 1); + + bool shouldInclude = false; + if (variables.TryGetValue(condition, out var value)) + { + shouldInclude = value is bool b ? b : value != null; + } + + result.Remove(start, endBlock - start + 3); + if (shouldInclude) + { + result.Insert(start, content); + } } + + // 渲染变量 + return Render(result.ToString(), variables); } /// - /// 渲染缓存的模板 + /// 循环渲染 /// - /// 模板名称 - /// 变量字典 - /// 渲染后的字符串 - public static string RenderCached(string name, Dictionary variables) + public static string RenderLoop(string template, string varName, IEnumerable items) { - var template = GetCachedTemplate(name); - if (template == null) - throw new KeyNotFoundException($"模板 '{name}' 不存在"); + if (string.IsNullOrEmpty(template)) + return template; - return RenderFull(template, variables); - } + var result = new StringBuilder(); - /// - /// 清除模板缓存 - /// - public static void ClearCache() - { - lock (_cacheLock) - { - _templateCache.Clear(); - } - } + // 找到循环块 {#var}...{/var} + var startTag = $"{{#{varName}}}"; + var endTag = $"{{/{varName}}}"; - #endregion + var start = template.IndexOf(startTag); + if (start < 0) return template; - #region 文件模板 + var end = template.IndexOf(endTag, start); + if (end < 0) return template; - /// - /// 从文件渲染模板 - /// - /// 模板文件路径 - /// 变量字典 - /// 渲染后的字符串 - public static string RenderFromFile(string filePath, Dictionary variables) - { - if (!System.IO.File.Exists(filePath)) - throw new System.IO.FileNotFoundException($"模板文件不存在: {filePath}"); + var prefix = template.Substring(0, start); + var loopTemplate = template.Substring(start + startTag.Length, end - start - startTag.Length); + var suffix = template.Substring(end + endTag.Length); - var template = System.IO.File.ReadAllText(filePath); - return RenderFull(template, variables); - } + result.Append(prefix); - #endregion + foreach (var item in items) + { + var dict = new Dictionary + { + [varName] = item + }; + + // 如果item是匿名对象,展开其属性 + if (item != null) + { + foreach (var prop in item.GetType().GetProperties()) + { + dict[$"{varName}.{prop.Name}"] = prop.GetValue(item); + } + } + + result.Append(Render(loopTemplate, dict)); + } + + result.Append(suffix); + return result.ToString(); + } } } diff --git a/EasyTool.Core/TextCategory/TextSimilarityUtil.cs b/EasyTool.Core/TextCategory/TextSimilarityUtil.cs new file mode 100644 index 0000000..0ab2992 --- /dev/null +++ b/EasyTool.Core/TextCategory/TextSimilarityUtil.cs @@ -0,0 +1,413 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 文本相似度算法 + /// + public enum SimilarityAlgorithm + { + /// + /// Levenshtein 编辑距离 + /// + Levenshtein, + + /// + /// Jaccard 相似度 + /// + Jaccard, + + /// + /// Cosine 余弦相似度 + /// + Cosine, + + /// + /// Dice 系数 + /// + Dice, + + /// + /// Jaro-Winkler 相似度 + /// + JaroWinkler, + + /// + /// Hamming 距离 + /// + Hamming + } + + /// + /// 文本相似度工具类 + /// 提供多种文本相似度计算算法 + /// + public static class TextSimilarityUtil + { + /// + /// 计算文本相似度 + /// + /// 文本1 + /// 文本2 + /// 算法 + /// 相似度(0-1) + public static double Calculate(string text1, string text2, SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return algorithm switch + { + SimilarityAlgorithm.Levenshtein => LevenshteinSimilarity(text1, text2), + SimilarityAlgorithm.Jaccard => JaccardSimilarity(text1, text2), + SimilarityAlgorithm.Cosine => CosineSimilarity(text1, text2), + SimilarityAlgorithm.Dice => DiceSimilarity(text1, text2), + SimilarityAlgorithm.JaroWinkler => JaroWinklerSimilarity(text1, text2), + SimilarityAlgorithm.Hamming => HammingSimilarity(text1, text2), + _ => throw new ArgumentException($"不支持的算法: {algorithm}") + }; + } + + /// + /// 计算编辑距离 + /// + /// 文本1 + /// 文本2 + /// 编辑距离 + public static int LevenshteinDistance(string text1, string text2) + { + if (string.IsNullOrEmpty(text1)) + return text2?.Length ?? 0; + + if (string.IsNullOrEmpty(text2)) + return text1.Length; + + var matrix = new int[text1.Length + 1, text2.Length + 1]; + + for (int i = 0; i <= text1.Length; i++) + matrix[i, 0] = i; + + for (int j = 0; j <= text2.Length; j++) + matrix[0, j] = j; + + for (int i = 1; i <= text1.Length; i++) + { + for (int j = 1; j <= text2.Length; j++) + { + var cost = text1[i - 1] == text2[j - 1] ? 0 : 1; + + matrix[i, j] = Math.Min( + Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1), + matrix[i - 1, j - 1] + cost); + } + } + + return matrix[text1.Length, text2.Length]; + } + + /// + /// Levenshtein 相似度 + /// + public static double LevenshteinSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var distance = LevenshteinDistance(text1, text2); + var maxLength = Math.Max(text1.Length, text2.Length); + + return 1.0 - (double)distance / maxLength; + } + + /// + /// Jaccard 相似度 + /// + public static double JaccardSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var set1 = GetNgrams(text1, 2); + var set2 = GetNgrams(text2, 2); + + var intersection = set1.Intersect(set2).Count(); + var union = set1.Union(set2).Count(); + + return union == 0 ? 0.0 : (double)intersection / union; + } + + /// + /// Cosine 余弦相似度 + /// + public static double CosineSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var vector1 = GetTermFrequency(text1); + var vector2 = GetTermFrequency(text2); + + var allTerms = vector1.Keys.Union(vector2.Keys).ToList(); + + double dotProduct = 0; + double magnitude1 = 0; + double magnitude2 = 0; + + foreach (var term in allTerms) + { + var v1 = vector1.TryGetValue(term, out var val1) ? val1 : 0; + var v2 = vector2.TryGetValue(term, out var val2) ? val2 : 0; + + dotProduct += v1 * v2; + magnitude1 += v1 * v1; + magnitude2 += v2 * v2; + } + + magnitude1 = Math.Sqrt(magnitude1); + magnitude2 = Math.Sqrt(magnitude2); + + if (magnitude1 == 0 || magnitude2 == 0) + return 0.0; + + return dotProduct / (magnitude1 * magnitude2); + } + + /// + /// Dice 系数 + /// + public static double DiceSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var set1 = GetNgrams(text1, 2); + var set2 = GetNgrams(text2, 2); + + var intersection = set1.Intersect(set2).Count(); + + return (2.0 * intersection) / (set1.Count + set2.Count); + } + + /// + /// Jaro-Winkler 相似度 + /// + public static double JaroWinklerSimilarity(string text1, string text2) + { + var jaroSimilarity = JaroSimilarity(text1, text2); + + // 计算 common prefix 长度(最多4个字符) + var prefixLength = 0; + var minLength = Math.Min(Math.Min(text1.Length, text2.Length), 4); + + for (int i = 0; i < minLength; i++) + { + if (text1[i] == text2[i]) + prefixLength++; + else + break; + } + + // Winkler 修正 + return jaroSimilarity + (prefixLength * 0.1 * (1 - jaroSimilarity)); + } + + /// + /// Jaro 相似度 + /// + public static double JaroSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + if (text1 == text2) + return 1.0; + + var matchDistance = Math.Max(text1.Length, text2.Length) / 2 - 1; + var matches1 = new bool[text1.Length]; + var matches2 = new bool[text2.Length]; + + var matches = 0; + var transpositions = 0; + + // 查找匹配字符 + for (int i = 0; i < text1.Length; i++) + { + var start = Math.Max(0, i - matchDistance); + var end = Math.Min(i + matchDistance + 1, text2.Length); + + for (int j = start; j < end; j++) + { + if (matches2[j] || text1[i] != text2[j]) + continue; + + matches1[i] = true; + matches2[j] = true; + matches++; + break; + } + } + + if (matches == 0) + return 0.0; + + // 计算转置次数 + var k = 0; + for (int i = 0; i < text1.Length; i++) + { + if (!matches1[i]) + continue; + + while (!matches2[k]) + k++; + + if (text1[i] != text2[k]) + transpositions++; + + k++; + } + + return ((double)matches / text1.Length + + (double)matches / text2.Length + + (matches - transpositions / 2.0) / matches) / 3.0; + } + + /// + /// Hamming 距离(仅适用于等长字符串) + /// + public static int HammingDistance(string text1, string text2) + { + if (text1.Length != text2.Length) + throw new ArgumentException("Hamming 距离要求两个字符串长度相等"); + + return text1.Zip(text2, (c1, c2) => c1 != c2 ? 1 : 0).Sum(); + } + + /// + /// Hamming 相似度 + /// + public static double HammingSimilarity(string text1, string text2) + { + if (text1.Length != text2.Length) + return 0.0; + + if (text1.Length == 0) + return 1.0; + + var distance = HammingDistance(text1, text2); + return 1.0 - (double)distance / text1.Length; + } + + /// + /// 查找最相似的文本 + /// + /// 查询文本 + /// 候选文本列表 + /// 算法 + /// 返回前N个 + /// 相似度排序结果 + public static List<(string Text, double Similarity)> FindMostSimilar( + string query, + IEnumerable candidates, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein, + int topN = 5) + { + return candidates + .Select(c => (Text: c, Similarity: Calculate(query, c, algorithm))) + .OrderByDescending(r => r.Similarity) + .Take(topN) + .ToList(); + } + + /// + /// 检查是否相似(超过阈值) + /// + /// 文本1 + /// 文本2 + /// 阈值(0-1) + /// 算法 + /// 是否相似 + public static bool IsSimilar( + string text1, + string text2, + double threshold = 0.8, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return Calculate(text1, text2, algorithm) >= threshold; + } + + /// + /// 模糊搜索 + /// + /// 查询文本 + /// 候选文本列表 + /// 阈值 + /// 算法 + /// 匹配结果 + public static List FuzzySearch( + string query, + IEnumerable candidates, + double threshold = 0.6, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return candidates + .Where(c => Calculate(query, c, algorithm) >= threshold) + .ToList(); + } + + #region 私有方法 + + private static HashSet GetNgrams(string text, int n) + { + var ngrams = new HashSet(); + + if (string.IsNullOrEmpty(text) || text.Length < n) + { + ngrams.Add(text ?? ""); + return ngrams; + } + + for (int i = 0; i <= text.Length - n; i++) + { + ngrams.Add(text.Substring(i, n)); + } + + return ngrams; + } + + private static Dictionary GetTermFrequency(string text) + { + var frequency = new Dictionary(); + + if (string.IsNullOrEmpty(text)) + return frequency; + + // 按字符分词 + foreach (var c in text) + { + var term = c.ToString(); + if (frequency.ContainsKey(term)) + frequency[term]++; + else + frequency[term] = 1; + } + + return frequency; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/AsyncLockUtil.cs b/EasyTool.Core/ToolCategory/AsyncLockUtil.cs new file mode 100644 index 0000000..73614f2 --- /dev/null +++ b/EasyTool.Core/ToolCategory/AsyncLockUtil.cs @@ -0,0 +1,626 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 异步锁 + /// 支持异步等待的互斥锁 + /// + public class AsyncLock + { + private readonly SemaphoreSlim _semaphore = new(1, 1); + private readonly Task _releaser; + + /// + /// 创建异步锁 + /// + public AsyncLock() + { + _releaser = Task.FromResult(new Releaser(this)); + } + + /// + /// 获取锁 + /// + /// 释放器 + public Task LockAsync() + { + var wait = _semaphore.WaitAsync(); + return wait.IsCompleted + ? _releaser + : wait.ContinueWith((_, state) => new Releaser((AsyncLock)state!), + this, CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); + } + + /// + /// 尝试获取锁 + /// + /// 释放器 + /// 是否成功获取 + public bool TryLock(out Releaser releaser) + { + if (_semaphore.Wait(0)) + { + releaser = new Releaser(this); + return true; + } + releaser = default; + return false; + } + + /// + /// 尝试获取锁(带超时) + /// + /// 超时时间 + /// 释放器 + /// 是否成功获取 + public bool TryLock(TimeSpan timeout, out Releaser releaser) + { + if (_semaphore.Wait(timeout)) + { + releaser = new Releaser(this); + return true; + } + releaser = default; + return false; + } + + /// + /// 尝试获取锁(带超时,异步) + /// + /// 超时时间 + /// 是否成功获取和释放器 + public async Task<(bool acquired, Releaser releaser)> TryLockAsync(TimeSpan timeout) + { + if (await _semaphore.WaitAsync(timeout)) + { + return (true, new Releaser(this)); + } + return (false, default); + } + + /// + /// 锁释放器 + /// + public struct Releaser : IDisposable + { + private readonly AsyncLock? _lock; + + internal Releaser(AsyncLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + _lock?._semaphore.Release(); + } + } + } + + /// + /// 异步读写锁 + /// 支持读写分离的异步锁 + /// + public class AsyncReaderWriterLock + { + private readonly SemaphoreSlim _readLock = new(1, 1); + private readonly SemaphoreSlim _writeLock = new(1, 1); + private int _readersCount; + + /// + /// 获取读锁 + /// + /// 释放器 + public async Task ReaderLockAsync() + { + await _readLock.WaitAsync(); + if (Interlocked.Increment(ref _readersCount) == 1) + { + await _writeLock.WaitAsync(); + } + _readLock.Release(); + + return new ReaderReleaser(this); + } + + /// + /// 获取写锁 + /// + /// 释放器 + public async Task WriterLockAsync() + { + await _writeLock.WaitAsync(); + return new WriterReleaser(this); + } + + /// + /// 尝试获取读锁(带超时) + /// + public async Task<(bool acquired, ReaderReleaser releaser)> TryReaderLockAsync(TimeSpan timeout) + { + if (!await _readLock.WaitAsync(timeout)) + return (false, default); + + try + { + if (Interlocked.Increment(ref _readersCount) == 1) + { + if (!await _writeLock.WaitAsync(timeout)) + { + Interlocked.Decrement(ref _readersCount); + return (false, default); + } + } + _readLock.Release(); + return (true, new ReaderReleaser(this)); + } + catch + { + _readLock.Release(); + return (false, default); + } + } + + /// + /// 尝试获取写锁(带超时) + /// + public async Task<(bool acquired, WriterReleaser releaser)> TryWriterLockAsync(TimeSpan timeout) + { + if (await _writeLock.WaitAsync(timeout)) + { + return (true, new WriterReleaser(this)); + } + return (false, default); + } + + /// + /// 读锁释放器 + /// + public struct ReaderReleaser : IDisposable + { + private readonly AsyncReaderWriterLock? _lock; + + internal ReaderReleaser(AsyncReaderWriterLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + if (_lock == null) return; + + _lock._readLock.Wait(); + if (Interlocked.Decrement(ref _lock._readersCount) == 0) + { + _lock._writeLock.Release(); + } + _lock._readLock.Release(); + } + } + + /// + /// 写锁释放器 + /// + public struct WriterReleaser : IDisposable + { + private readonly AsyncReaderWriterLock? _lock; + + internal WriterReleaser(AsyncReaderWriterLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + _lock?._writeLock.Release(); + } + } + } + + /// + /// 异步信号量 + /// + public class AsyncSemaphore + { + private readonly SemaphoreSlim _semaphore; + + /// + /// 当前计数 + /// + public int CurrentCount => _semaphore.CurrentCount; + + /// + /// 创建异步信号量 + /// + /// 初始计数 + /// 最大计数 + public AsyncSemaphore(int initialCount, int maxCount = int.MaxValue) + { + _semaphore = new SemaphoreSlim(initialCount, maxCount); + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + return _semaphore.WaitAsync(); + } + + /// + /// 等待信号(带超时) + /// + public Task WaitAsync(TimeSpan timeout) + { + return _semaphore.WaitAsync(timeout); + } + + /// + /// 等待信号(带取消令牌) + /// + public Task WaitAsync(CancellationToken cancellationToken) + { + return _semaphore.WaitAsync(cancellationToken); + } + + /// + /// 释放信号 + /// + public void Release() + { + _semaphore.Release(); + } + + /// + /// 释放指定数量的信号 + /// + public void Release(int releaseCount) + { + _semaphore.Release(releaseCount); + } + } + + /// + /// 异步自动重置事件 + /// + public class AsyncAutoResetEvent + { + private readonly Queue> _waits = new(); + private bool _signaled; + + /// + /// 创建异步自动重置事件 + /// + /// 初始状态 + public AsyncAutoResetEvent(bool initialState = false) + { + _signaled = initialState; + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + lock (_waits) + { + if (_signaled) + { + _signaled = false; + return Task.CompletedTask; + } + + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _waits.Enqueue(tcs); + return tcs.Task; + } + } + + /// + /// 发送信号 + /// + public void Set() + { + lock (_waits) + { + if (_waits.Count > 0) + { + var tcs = _waits.Dequeue(); + tcs.TrySetResult(true); + } + else if (!_signaled) + { + _signaled = true; + } + } + } + } + + /// + /// 异步手动重置事件 + /// + public class AsyncManualResetEvent + { + private TaskCompletionSource _tcs; + + /// + /// 创建异步手动重置事件 + /// + /// 初始状态 + public AsyncManualResetEvent(bool initialState = false) + { + _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + if (initialState) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + return _tcs.Task; + } + + /// + /// 发送信号(设置) + /// + public void Set() + { + _tcs.TrySetResult(true); + } + + /// + /// 重置 + /// + public void Reset() + { + var newTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + while (true) + { + var oldTcs = _tcs; + if (!oldTcs.Task.IsCompleted) + return; + + if (Interlocked.CompareExchange(ref _tcs, newTcs, oldTcs) == oldTcs) + return; + } + } + + /// + /// 是否已设置 + /// + public bool IsSet => _tcs.Task.IsCompleted; + } + + /// + /// 异步倒计时事件 + /// + public class AsyncCountdownEvent + { + private int _count; + private readonly TaskCompletionSource _tcs; + + /// + /// 当前计数 + /// + public int CurrentCount => _count; + + /// + /// 是否已完成 + /// + public bool IsSet => _count == 0; + + /// + /// 创建异步倒计时事件 + /// + /// 初始计数 + public AsyncCountdownEvent(int initialCount) + { + _count = initialCount; + _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + if (initialCount <= 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 等待完成 + /// + public Task WaitAsync() + { + return _tcs.Task; + } + + /// + /// 信号(计数减1) + /// + public void Signal() + { + if (Interlocked.Decrement(ref _count) == 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 批量信号 + /// + /// 信号数量 + public void Signal(int signalCount) + { + if (signalCount <= 0) + throw new ArgumentOutOfRangeException(nameof(signalCount)); + + if (Interlocked.Add(ref _count, -signalCount) == 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 增加计数 + /// + public void AddCount() + { + Interlocked.Increment(ref _count); + } + + /// + /// 重置 + /// + /// 新计数 + public void Reset(int count) + { + _count = count; + } + } + + /// + /// 异步锁工具类 + /// + public static class AsyncLockUtil + { + private static readonly System.Collections.Concurrent.ConcurrentDictionary _locks = new(); + private static readonly System.Collections.Concurrent.ConcurrentDictionary _rwLocks = new(); + private static readonly System.Collections.Concurrent.ConcurrentDictionary _semaphores = new(); + + /// + /// 获取或创建异步锁 + /// + /// 锁名称 + /// 异步锁 + public static AsyncLock GetOrCreateLock(string name) + { + return _locks.GetOrAdd(name, _ => new AsyncLock()); + } + + /// + /// 使用锁执行操作 + /// + /// 返回类型 + /// 锁名称 + /// 操作 + /// 操作结果 + public static async Task WithLockAsync(string name, Func> action) + { + var @lock = GetOrCreateLock(name); + using (await @lock.LockAsync()) + { + return await action(); + } + } + + /// + /// 使用锁执行操作(无返回值) + /// + /// 锁名称 + /// 操作 + public static async Task WithLockAsync(string name, Func action) + { + var @lock = GetOrCreateLock(name); + using (await @lock.LockAsync()) + { + await action(); + } + } + + /// + /// 获取或创建读写锁 + /// + /// 锁名称 + /// 读写锁 + public static AsyncReaderWriterLock GetOrCreateReaderWriterLock(string name) + { + return _rwLocks.GetOrAdd(name, _ => new AsyncReaderWriterLock()); + } + + /// + /// 使用读锁执行操作 + /// + public static async Task WithReaderLockAsync(string name, Func> action) + { + var @lock = GetOrCreateReaderWriterLock(name); + using (await @lock.ReaderLockAsync()) + { + return await action(); + } + } + + /// + /// 使用写锁执行操作 + /// + public static async Task WithWriterLockAsync(string name, Func> action) + { + var @lock = GetOrCreateReaderWriterLock(name); + using (await @lock.WriterLockAsync()) + { + return await action(); + } + } + + /// + /// 获取或创建信号量 + /// + /// 名称 + /// 初始计数 + /// 最大计数 + /// 信号量 + public static AsyncSemaphore GetOrCreateSemaphore(string name, int initialCount = 1, int maxCount = int.MaxValue) + { + return _semaphores.GetOrAdd(name, _ => new AsyncSemaphore(initialCount, maxCount)); + } + + /// + /// 创建异步自动重置事件 + /// + public static AsyncAutoResetEvent CreateAutoResetEvent(bool initialState = false) + { + return new AsyncAutoResetEvent(initialState); + } + + /// + /// 创建异步手动重置事件 + /// + public static AsyncManualResetEvent CreateManualResetEvent(bool initialState = false) + { + return new AsyncManualResetEvent(initialState); + } + + /// + /// 创建异步倒计时事件 + /// + public static AsyncCountdownEvent CreateCountdownEvent(int initialCount) + { + return new AsyncCountdownEvent(initialCount); + } + + /// + /// 移除锁 + /// + public static bool RemoveLock(string name) + { + return _locks.TryRemove(name, out _); + } + + /// + /// 清空所有锁 + /// + public static void Clear() + { + _locks.Clear(); + _rwLocks.Clear(); + _semaphores.Clear(); + } + } +} diff --git a/EasyTool.Core/ToolCategory/BackoffUtil.cs b/EasyTool.Core/ToolCategory/BackoffUtil.cs new file mode 100644 index 0000000..9aafe99 --- /dev/null +++ b/EasyTool.Core/ToolCategory/BackoffUtil.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 退避策略工具类 + /// 提供指数退避、线性退避等重试间隔计算 + /// + public static class BackoffUtil + { + /// + /// 指数退避 + /// + /// 尝试次数(从0开始) + /// 基础延迟 + /// 最大延迟 + /// 是否添加随机抖动 + public static TimeSpan Exponential(int attempt, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + var delay = TimeSpan.FromTicks(baseDelay.Ticks * (long)Math.Pow(2, attempt)); + + if (maxDelay.HasValue && delay > maxDelay.Value) + delay = maxDelay.Value; + + if (jitter) + { + var random = new Random(); + var jitterRange = delay.TotalMilliseconds * 0.1; + delay = TimeSpan.FromMilliseconds(delay.TotalMilliseconds + random.NextDouble() * jitterRange); + } + + return delay; + } + + /// + /// 线性退避 + /// + public static TimeSpan Linear(int attempt, TimeSpan baseDelay, TimeSpan? maxDelay = null) + { + var delay = TimeSpan.FromTicks(baseDelay.Ticks * (attempt + 1)); + + if (maxDelay.HasValue && delay > maxDelay.Value) + delay = maxDelay.Value; + + return delay; + } + + /// + /// 固定延迟 + /// + public static TimeSpan Fixed(TimeSpan delay) + { + return delay; + } + + /// + /// 装饰退避(Decorrelated Jitter) + /// + public static TimeSpan DecorrelatedJitter(int attempt, TimeSpan baseDelay, TimeSpan maxDelay, TimeSpan? previousDelay = null) + { + var random = new Random(); + var prev = previousDelay ?? baseDelay; + var delay = TimeSpan.FromTicks((long)(prev.TotalMilliseconds * random.NextDouble() * 3)); + + if (delay < baseDelay) + delay = baseDelay; + + if (delay > maxDelay) + delay = maxDelay; + + return delay; + } + + /// + /// 等距退避 + /// + public static TimeSpan EqualJitter(int attempt, TimeSpan baseDelay, TimeSpan maxDelay) + { + var random = new Random(); + var exponentialDelay = Exponential(attempt, baseDelay, maxDelay, false); + var half = exponentialDelay.TotalMilliseconds / 2; + var delay = TimeSpan.FromMilliseconds(half + random.NextDouble() * half); + return delay; + } + + /// + /// 创建退避策略生成器 + /// + public static BackoffGenerator CreateGenerator(BackoffStrategy strategy, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + return new BackoffGenerator(strategy, baseDelay, maxDelay, jitter); + } + + /// + /// 使用退避策略执行操作 + /// + public static async Task ExecuteWithBackoffAsync( + Func> action, + int maxRetries, + BackoffStrategy strategy = BackoffStrategy.Exponential, + TimeSpan? baseDelay = null, + TimeSpan? maxDelay = null, + Func? shouldRetry = null) + { + var delay = baseDelay ?? TimeSpan.FromSeconds(1); + var max = maxDelay ?? TimeSpan.FromMinutes(1); + Exception? lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return await action(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt == maxRetries || (shouldRetry != null && !shouldRetry(ex, attempt))) + break; + + var waitTime = strategy switch + { + BackoffStrategy.Exponential => Exponential(attempt, delay, max), + BackoffStrategy.Linear => Linear(attempt, delay, max), + BackoffStrategy.Fixed => delay, + _ => Exponential(attempt, delay, max) + }; + + await Task.Delay(waitTime); + } + } + + throw lastException ?? new Exception("操作失败"); + } + + /// + /// 使用退避策略执行操作 + /// + public static async Task ExecuteWithBackoffAsync( + Func action, + int maxRetries, + BackoffStrategy strategy = BackoffStrategy.Exponential, + TimeSpan? baseDelay = null, + TimeSpan? maxDelay = null, + Func? shouldRetry = null) + { + await ExecuteWithBackoffAsync(async () => + { + await action(); + return true; + }, maxRetries, strategy, baseDelay, maxDelay, shouldRetry); + } + } + + /// + /// 退避策略生成器 + /// + public class BackoffGenerator + { + private readonly BackoffStrategy _strategy; + private readonly TimeSpan _baseDelay; + private readonly TimeSpan? _maxDelay; + private readonly bool _jitter; + private int _attempt; + private TimeSpan? _previousDelay; + + public BackoffGenerator(BackoffStrategy strategy, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + _strategy = strategy; + _baseDelay = baseDelay; + _maxDelay = maxDelay; + _jitter = jitter; + _attempt = 0; + } + + /// + /// 获取下一个延迟时间 + /// + public TimeSpan Next() + { + var delay = _strategy switch + { + BackoffStrategy.Exponential => BackoffUtil.Exponential(_attempt, _baseDelay, _maxDelay, _jitter), + BackoffStrategy.Linear => BackoffUtil.Linear(_attempt, _baseDelay, _maxDelay), + BackoffStrategy.Fixed => _baseDelay, + _ => _baseDelay + }; + + _previousDelay = delay; + _attempt++; + return delay; + } + + /// + /// 重置生成器 + /// + public void Reset() + { + _attempt = 0; + _previousDelay = null; + } + + /// + /// 获取当前尝试次数 + /// + public int Attempt => _attempt; + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/BenchmarkUtil.cs b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs index 0da5c6f..26535da 100644 --- a/EasyTool.Core/ToolCategory/BenchmarkUtil.cs +++ b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs @@ -2,542 +2,316 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Threading; using System.Threading.Tasks; namespace EasyTool.ToolCategory { /// - /// 性能计时工具类 - /// 提供代码执行时间测量和性能分析功能 + /// 性能测试结果 + /// + public class BenchmarkResult + { + /// + /// 操作名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 执行次数 + /// + public int Iterations { get; set; } + + /// + /// 总耗时 + /// + public TimeSpan TotalTime { get; set; } + + /// + /// 平均耗时 + /// + public TimeSpan AverageTime => Iterations > 0 ? TimeSpan.FromTicks(TotalTime.Ticks / Iterations) : TimeSpan.Zero; + + /// + /// 最小耗时 + /// + public TimeSpan MinTime { get; set; } + + /// + /// 最大耗时 + /// + public TimeSpan MaxTime { get; set; } + + /// + /// 每秒操作数 + /// + public double OperationsPerSecond => TotalTime.TotalSeconds > 0 ? Iterations / TotalTime.TotalSeconds : 0; + + /// + /// 详细耗时记录 + /// + public List DetailedTimes { get; set; } = new(); + + public override string ToString() + { + return $"[{Name}] {Iterations} 次, 总计: {TotalTime.TotalMilliseconds:F2}ms, 平均: {AverageTime.TotalMilliseconds:F4}ms, " + + $"最小: {MinTime.TotalMilliseconds:F4}ms, 最大: {MaxTime.TotalMilliseconds:F4}ms, {OperationsPerSecond:F0} ops/s"; + } + } + + /// + /// 性能测试工具类 + /// 提供代码执行性能测量功能 /// public static class BenchmarkUtil { /// - /// 测量操作执行时间 + /// 测量单次执行时间 /// /// 要测量的操作 - /// 操作名称 - /// 测量结果 - public static BenchmarkResult Measure(Action action, string? name = null) + /// 执行时间 + public static TimeSpan Measure(Action action) { - if (action == null) - throw new ArgumentNullException(nameof(action)); - var stopwatch = Stopwatch.StartNew(); action(); stopwatch.Stop(); - - return new BenchmarkResult - { - Name = name ?? "Operation", - ElapsedTicks = stopwatch.ElapsedTicks, - ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, - ElapsedTime = stopwatch.Elapsed - }; + return stopwatch.Elapsed; } /// - /// 测量异步操作执行时间 + /// 测量单次执行时间(带返回值) /// - /// 要测量的异步操作 - /// 操作名称 - /// 测量结果 - public static async Task MeasureAsync(Func func, string? name = null) + /// 返回值类型 + /// 要测量的操作 + /// 执行结果 + /// 执行时间 + public static TimeSpan Measure(Func func, out T result) { - if (func == null) - throw new ArgumentNullException(nameof(func)); - var stopwatch = Stopwatch.StartNew(); - await func(); + result = func(); stopwatch.Stop(); - - return new BenchmarkResult - { - Name = name ?? "Operation", - ElapsedTicks = stopwatch.ElapsedTicks, - ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, - ElapsedTime = stopwatch.Elapsed - }; + return stopwatch.Elapsed; } /// - /// 测量带返回值的操作执行时间 + /// 异步测量单次执行时间 /// - /// 返回值类型 - /// 要测量的操作 - /// 操作名称 - /// 带返回值的测量结果 - public static BenchmarkResult Measure(Func func, string? name = null) + /// 要测量的操作 + /// 执行时间 + public static async Task MeasureAsync(Func action) { - if (func == null) - throw new ArgumentNullException(nameof(func)); - var stopwatch = Stopwatch.StartNew(); - var result = func(); + await action(); stopwatch.Stop(); - - return new BenchmarkResult - { - Name = name ?? "Operation", - ElapsedTicks = stopwatch.ElapsedTicks, - ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, - ElapsedTime = stopwatch.Elapsed, - Value = result - }; + return stopwatch.Elapsed; } /// - /// 测量带返回值的异步操作执行时间 + /// 异步测量单次执行时间(带返回值) /// /// 返回值类型 - /// 要测量的异步操作 - /// 操作名称 - /// 带返回值的测量结果 - public static async Task> MeasureAsync(Func> func, string? name = null) + /// 要测量的操作 + /// 执行时间和结果 + public static async Task<(TimeSpan Elapsed, T Result)> MeasureAsync(Func> func) { - if (func == null) - throw new ArgumentNullException(nameof(func)); - var stopwatch = Stopwatch.StartNew(); var result = await func(); stopwatch.Stop(); - - return new BenchmarkResult - { - Name = name ?? "Operation", - ElapsedTicks = stopwatch.ElapsedTicks, - ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, - ElapsedTime = stopwatch.Elapsed, - Value = result - }; + return (stopwatch.Elapsed, result); } /// - /// 多次执行并计算平均执行时间 + /// 基准测试 /// - /// 要测量的操作 + /// 测试名称 + /// 要测试的操作 /// 迭代次数 - /// 操作名称 - /// 统计结果 - public static BenchmarkStatistics Benchmark(Action action, int iterations = 100, string? name = null) + /// 预热次数 + /// 测试结果 + public static BenchmarkResult Run(string name, Action action, int iterations = 1000, int warmupIterations = 10) { - if (action == null) - throw new ArgumentNullException(nameof(action)); - if (iterations < 1) - throw new ArgumentOutOfRangeException(nameof(iterations)); - // 预热 - action(); - - var times = new List(iterations); - var stopwatch = new Stopwatch(); - - for (int i = 0; i < iterations; i++) + for (int i = 0; i < warmupIterations; i++) { - stopwatch.Restart(); action(); - stopwatch.Stop(); - times.Add(stopwatch.ElapsedTicks); } - return CalculateStatistics(name ?? "Operation", times, iterations); - } - - /// - /// 多次执行异步操作并计算平均执行时间 - /// - /// 要测量的异步操作 - /// 迭代次数 - /// 操作名称 - /// 统计结果 - public static async Task BenchmarkAsync(Func func, int iterations = 100, string? name = null) - { - if (func == null) - throw new ArgumentNullException(nameof(func)); - if (iterations < 1) - throw new ArgumentOutOfRangeException(nameof(iterations)); - - // 预热 - await func(); - - var times = new List(iterations); - var stopwatch = new Stopwatch(); + // 正式测试 + var times = new List(iterations); + var totalTime = TimeSpan.Zero; + var minTime = TimeSpan.MaxValue; + var maxTime = TimeSpan.Zero; for (int i = 0; i < iterations; i++) { - stopwatch.Restart(); - await func(); - stopwatch.Stop(); - times.Add(stopwatch.ElapsedTicks); - } + var time = Measure(action); + times.Add(time); + totalTime += time; - return CalculateStatistics(name ?? "Operation", times, iterations); - } - - /// - /// 比较多个操作的执行时间 - /// - /// 操作列表(名称和操作) - /// 每个操作的迭代次数 - /// 比较结果列表 - public static List Compare(IEnumerable<(string Name, Action Action)> operations, int iterations = 100) - { - if (operations == null) - throw new ArgumentNullException(nameof(operations)); - - var results = new List(); - foreach (var (name, action) in operations) - { - results.Add(Benchmark(action, iterations, name)); + if (time < minTime) minTime = time; + if (time > maxTime) maxTime = time; } - return results.OrderBy(r => r.AverageMilliseconds).ToList(); + return new BenchmarkResult + { + Name = name, + Iterations = iterations, + TotalTime = totalTime, + MinTime = minTime, + MaxTime = maxTime, + DetailedTimes = times + }; } /// - /// 比较多个异步操作的执行时间 + /// 异步基准测试 /// - /// 操作列表(名称和操作) - /// 每个操作的迭代次数 - /// 比较结果列表 - public static async Task> CompareAsync( - IEnumerable<(string Name, Func Func)> operations, - int iterations = 100) + /// 测试名称 + /// 要测试的操作 + /// 迭代次数 + /// 预热次数 + /// 测试结果 + public static async Task RunAsync(string name, Func action, int iterations = 1000, int warmupIterations = 10) { - if (operations == null) - throw new ArgumentNullException(nameof(operations)); - - var results = new List(); - foreach (var (name, func) in operations) + // 预热 + for (int i = 0; i < warmupIterations; i++) { - results.Add(await BenchmarkAsync(func, iterations, name)); + await action(); } - return results.OrderBy(r => r.AverageMilliseconds).ToList(); - } + // 正式测试 + var times = new List(iterations); + var totalTime = TimeSpan.Zero; + var minTime = TimeSpan.MaxValue; + var maxTime = TimeSpan.Zero; - /// - /// 创建一个可多次记录的计时器 - /// - /// 计时器名称 - /// 计时器实例 - public static BenchmarkTimer CreateTimer(string? name = null) - { - return new BenchmarkTimer(name); - } - - /// - /// 内存使用测量 - /// - /// 要测量的操作 - /// 操作名称 - /// 测量结果 - public static MemoryBenchmarkResult MeasureMemory(Action action, string? name = null) - { - if (action == null) - throw new ArgumentNullException(nameof(action)); - - // 强制GC以获得更准确的内存测量 - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - var beforeMemory = GC.GetTotalMemory(forceFullCollection: true); - - var stopwatch = Stopwatch.StartNew(); - action(); - stopwatch.Stop(); - - var afterMemory = GC.GetTotalMemory(forceFullCollection: false); - - return new MemoryBenchmarkResult + for (int i = 0; i < iterations; i++) { - Name = name ?? "Operation", - ElapsedMilliseconds = stopwatch.ElapsedMilliseconds, - MemoryBefore = beforeMemory, - MemoryAfter = afterMemory, - MemoryDelta = afterMemory - beforeMemory - }; - } - - private static BenchmarkStatistics CalculateStatistics(string name, List times, int iterations) - { - times.Sort(); - var frequency = Stopwatch.Frequency; - - var avgTicks = times.Average(); - var minTicks = times[0]; - var maxTicks = times[times.Count - 1]; - var medianTicks = times[times.Count / 2]; - var stdDev = Math.Sqrt(times.Average(t => Math.Pow(t - avgTicks, 2))); + var time = await MeasureAsync(action); + times.Add(time); + totalTime += time; - var p95Index = (int)(iterations * 0.95); - var p99Index = (int)(iterations * 0.99); + if (time < minTime) minTime = time; + if (time > maxTime) maxTime = time; + } - return new BenchmarkStatistics + return new BenchmarkResult { Name = name, Iterations = iterations, - TotalMilliseconds = (long)times.Sum(t => t * 1000.0 / frequency), - AverageMilliseconds = avgTicks * 1000.0 / frequency, - MinMilliseconds = minTicks * 1000.0 / frequency, - MaxMilliseconds = maxTicks * 1000.0 / frequency, - MedianMilliseconds = medianTicks * 1000.0 / frequency, - StdDevMilliseconds = stdDev * 1000.0 / frequency, - P95Milliseconds = times[Math.Min(p95Index, times.Count - 1)] * 1000.0 / frequency, - P99Milliseconds = times[Math.Min(p99Index, times.Count - 1)] * 1000.0 / frequency, - OperationsPerSecond = 1000.0 / (avgTicks * 1000.0 / frequency) + TotalTime = totalTime, + MinTime = minTime, + MaxTime = maxTime, + DetailedTimes = times }; } - } - - /// - /// 基准测试结果 - /// - public class BenchmarkResult - { - /// - /// 操作名称 - /// - public string Name { get; set; } = string.Empty; - - /// - /// 执行时间(Tick数) - /// - public long ElapsedTicks { get; set; } /// - /// 执行时间(毫秒) + /// 比较多个操作的性能 /// - public long ElapsedMilliseconds { get; set; } - - /// - /// 执行时间 - /// - public TimeSpan ElapsedTime { get; set; } - - /// - /// 执行时间(微秒) - /// - public double ElapsedMicroseconds => ElapsedTicks * 1_000_000.0 / Stopwatch.Frequency; - - /// - /// 执行时间(纳秒) - /// - public double ElapsedNanoseconds => ElapsedTicks * 1_000_000_000.0 / Stopwatch.Frequency; - - public override string ToString() + /// 迭代次数 + /// 操作列表 + /// 测试结果列表 + public static List Compare(int iterations, params (string Name, Action Action)[] actions) { - if (ElapsedMilliseconds > 0) - return $"{Name}: {ElapsedMilliseconds}ms"; - return $"{Name}: {ElapsedMicroseconds:F2}μs"; - } - } - - /// - /// 带返回值的基准测试结果 - /// - /// 返回值类型 - public class BenchmarkResult : BenchmarkResult - { - /// - /// 返回值 - /// - public T? Value { get; set; } - } - - /// - /// 基准测试统计信息 - /// - public class BenchmarkStatistics - { - /// - /// 操作名称 - /// - public string Name { get; set; } = string.Empty; - - /// - /// 迭代次数 - /// - public int Iterations { get; set; } - - /// - /// 总执行时间(毫秒) - /// - public long TotalMilliseconds { get; set; } - - /// - /// 平均执行时间(毫秒) - /// - public double AverageMilliseconds { get; set; } - - /// - /// 最小执行时间(毫秒) - /// - public double MinMilliseconds { get; set; } - - /// - /// 最大执行时间(毫秒) - /// - public double MaxMilliseconds { get; set; } - - /// - /// 中位数执行时间(毫秒) - /// - public double MedianMilliseconds { get; set; } - - /// - /// 标准差(毫秒) - /// - public double StdDevMilliseconds { get; set; } - - /// - /// 第95百分位执行时间(毫秒) - /// - public double P95Milliseconds { get; set; } + var results = new List(); - /// - /// 第99百分位执行时间(毫秒) - /// - public double P99Milliseconds { get; set; } - - /// - /// 每秒操作数 - /// - public double OperationsPerSecond { get; set; } + foreach (var (name, action) in actions) + { + results.Add(Run(name, action, iterations)); + } - public override string ToString() - { - return $"{Name}: Avg={AverageMilliseconds:F3}ms, Min={MinMilliseconds:F3}ms, Max={MaxMilliseconds:F3}ms, Ops/s={OperationsPerSecond:F0}"; + return results.OrderBy(r => r.AverageTime).ToList(); } - } - - /// - /// 内存基准测试结果 - /// - public class MemoryBenchmarkResult - { - /// - /// 操作名称 - /// - public string Name { get; set; } = string.Empty; - - /// - /// 执行时间(毫秒) - /// - public long ElapsedMilliseconds { get; set; } - - /// - /// 执行前内存(字节) - /// - public long MemoryBefore { get; set; } - - /// - /// 执行后内存(字节) - /// - public long MemoryAfter { get; set; } - - /// - /// 内存变化(字节) - /// - public long MemoryDelta { get; set; } /// - /// 内存变化(MB) + /// 异步比较多个操作的性能 /// - public double MemoryDeltaMB => MemoryDelta / (1024.0 * 1024.0); - - public override string ToString() + /// 迭代次数 + /// 操作列表 + /// 测试结果列表 + public static async Task> CompareAsync(int iterations, params (string Name, Func Action)[] actions) { - var sign = MemoryDelta >= 0 ? "+" : ""; - return $"{Name}: {ElapsedMilliseconds}ms, Memory: {sign}{MemoryDeltaMB:F2}MB"; - } - } + var results = new List(); - /// - /// 可记录多个时间点的计时器 - /// - public class BenchmarkTimer - { - private readonly Stopwatch _stopwatch; - private readonly List<(string Label, long Ticks)> _laps; - private readonly string? _name; - - public BenchmarkTimer(string? name = null) - { - _name = name; - _stopwatch = new Stopwatch(); - _laps = new List<(string, long)>(); - } + foreach (var (name, action) in actions) + { + results.Add(await RunAsync(name, action, iterations)); + } - /// - /// 开始计时 - /// - public BenchmarkTimer Start() - { - _stopwatch.Start(); - return this; + return results.OrderBy(r => r.AverageTime).ToList(); } /// - /// 记录一个时间点 + /// 使用计时器测量操作 /// - /// 标签 - public BenchmarkTimer Lap(string label) + /// 要测量的操作 + /// 耗时回调 + public static void WithTimer(Action action, Action elapsed) { - _laps.Add((label, _stopwatch.ElapsedTicks)); - return this; + var time = Measure(action); + elapsed(time); } /// - /// 停止计时 + /// 异步使用计时器测量操作 /// - public BenchmarkTimer Stop() + /// 要测量的操作 + /// 耗时回调 + public static async Task WithTimerAsync(Func action, Action elapsed) { - _stopwatch.Stop(); - return this; + var time = await MeasureAsync(action); + elapsed(time); } /// - /// 重置计时器 + /// 格式化时间输出 /// - public BenchmarkTimer Reset() + /// 时间 + /// 格式化字符串 + public static string FormatTime(TimeSpan time) { - _stopwatch.Reset(); - _laps.Clear(); - return this; + if (time.TotalSeconds >= 1) + return $"{time.TotalSeconds:F2}s"; + if (time.TotalMilliseconds >= 1) + return $"{time.TotalMilliseconds:F2}ms"; +#if NET7_0_OR_GREATER + if (time.TotalMicroseconds >= 1) + return $"{time.TotalMicroseconds:F2}μs"; + return $"{time.TotalNanoseconds:F2}ns"; +#else + // For older frameworks, use ticks for sub-millisecond precision + var ticks = time.Ticks; + if (ticks >= 10) // >= 1 microsecond (10 ticks = 1 μs) + return $"{ticks / 10.0:F2}μs"; + return $"{ticks * 100.0:F2}ns"; +#endif } /// - /// 获取所有记录点 + /// 打印比较结果 /// - /// 记录点列表 - public List<(string Label, double Milliseconds)> GetLaps() + /// 测试结果列表 + public static void PrintComparison(List results) { - return _laps.Select(l => (l.Label, l.Ticks * 1000.0 / Stopwatch.Frequency)).ToList(); - } + if (results.Count == 0) + return; - /// - /// 获取总执行时间(毫秒) - /// - public double TotalMilliseconds => _stopwatch.ElapsedTicks * 1000.0 / Stopwatch.Frequency; + Console.WriteLine("=== 性能比较结果 ==="); + Console.WriteLine(); - /// - /// 获取执行时间 - /// - public TimeSpan Elapsed => _stopwatch.Elapsed; + var baseline = results[0].AverageTime; - /// - /// 是否正在运行 - /// - public bool IsRunning => _stopwatch.IsRunning; - - public override string ToString() - { - return _name != null - ? $"{_name}: {TotalMilliseconds:F2}ms" - : $"{TotalMilliseconds:F2}ms"; + for (int i = 0; i < results.Count; i++) + { + var result = results[i]; + var ratio = i == 0 ? 1.0 : result.AverageTime.TotalMilliseconds / baseline.TotalMilliseconds; + + Console.WriteLine($"{i + 1}. {result.Name}"); + Console.WriteLine($" 平均: {FormatTime(result.AverageTime)}"); + Console.WriteLine($" 比率: {ratio:F2}x"); + Console.WriteLine($" 吞吐: {result.OperationsPerSecond:F0} ops/s"); + Console.WriteLine(); + } } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs b/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs new file mode 100644 index 0000000..33b4309 --- /dev/null +++ b/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs @@ -0,0 +1,301 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 熔断器状态 + /// + public enum CircuitState + { + /// + /// 关闭(正常) + /// + Closed, + + /// + /// 开启(熔断) + /// + Open, + + /// + /// 半开(尝试恢复) + /// + HalfOpen + } + + /// + /// 熔断器配置 + /// + public class CircuitBreakerOptions + { + /// + /// 失败阈值(触发熔断的最小失败次数) + /// + public int FailureThreshold { get; set; } = 5; + + /// + /// 成功阈值(半开状态下恢复的最小成功次数) + /// + public int SuccessThreshold { get; set; } = 2; + + /// + /// 熔断持续时间 + /// + public TimeSpan BreakDuration { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 熔断超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10); + + /// + /// 判断是否应该熔断的异常类型 + /// + public List ExceptionTypesToTrack { get; set; } = new() { typeof(Exception) }; + } + + /// + /// 熔断器 + /// + public class CircuitBreaker + { + private readonly CircuitBreakerOptions _options; + private readonly object _lock = new(); + private CircuitState _state = CircuitState.Closed; + private int _failureCount; + private int _successCount; + private DateTime _lastFailureTime; + + /// + /// 当前状态 + /// + public CircuitState State + { + get + { + lock (_lock) + { + if (_state == CircuitState.Open && ShouldAttemptReset()) + { + _state = CircuitState.HalfOpen; + _successCount = 0; + } + return _state; + } + } + } + + /// + /// 失败次数 + /// + public int FailureCount => _failureCount; + + /// + /// 成功次数 + /// + public int SuccessCount => _successCount; + + /// + /// 最后失败时间 + /// + public DateTime LastFailureTime => _lastFailureTime; + + /// + /// 状态变更事件 + /// + public event EventHandler? StateChanged; + + /// + /// 创建熔断器 + /// + public CircuitBreaker(CircuitBreakerOptions? options = null) + { + _options = options ?? new CircuitBreakerOptions(); + } + + /// + /// 执行操作 + /// + public async Task ExecuteAsync(Func> action) + { + var state = State; + + if (state == CircuitState.Open) + { + throw new CircuitBreakerOpenException("熔断器处于开启状态"); + } + + try + { + using var cts = new System.Threading.CancellationTokenSource(_options.Timeout); + var task = action(); + var completedTask = await Task.WhenAny(task, Task.Delay(_options.Timeout)); + + if (completedTask != task) + { + OnFailure(); + throw new TimeoutException("操作超时"); + } + + var result = await task; + OnSuccess(); + return result; + } + catch (Exception ex) when (ShouldTrackException(ex)) + { + OnFailure(); + throw; + } + } + + /// + /// 执行操作 + /// + public async Task ExecuteAsync(Func action) + { + await ExecuteAsync(async () => + { + await action(); + return true; + }); + } + + /// + /// 尝试执行操作 + /// + public async Task<(bool Success, T? Result, Exception? Error)> TryExecuteAsync(Func> action) + { + try + { + var result = await ExecuteAsync(action); + return (true, result, null); + } + catch (Exception ex) + { + return (false, default, ex); + } + } + + private bool ShouldAttemptReset() + { + return DateTime.UtcNow - _lastFailureTime >= _options.BreakDuration; + } + + private bool ShouldTrackException(Exception ex) + { + foreach (var type in _options.ExceptionTypesToTrack) + { + if (type.IsAssignableFrom(ex.GetType())) + return true; + } + return false; + } + + private void OnSuccess() + { + lock (_lock) + { + if (_state == CircuitState.HalfOpen) + { + _successCount++; + if (_successCount >= _options.SuccessThreshold) + { + TripTo(CircuitState.Closed); + _failureCount = 0; + _successCount = 0; + } + } + else if (_state == CircuitState.Closed) + { + _failureCount = 0; + } + } + } + + private void OnFailure() + { + lock (_lock) + { + _lastFailureTime = DateTime.UtcNow; + _failureCount++; + + if (_state == CircuitState.HalfOpen) + { + TripTo(CircuitState.Open); + } + else if (_state == CircuitState.Closed && _failureCount >= _options.FailureThreshold) + { + TripTo(CircuitState.Open); + } + } + } + + private void TripTo(CircuitState newState) + { + var oldState = _state; + _state = newState; + StateChanged?.Invoke(this, newState); + } + + /// + /// 重置熔断器 + /// + public void Reset() + { + lock (_lock) + { + _state = CircuitState.Closed; + _failureCount = 0; + _successCount = 0; + } + } + + /// + /// 强制打开熔断器 + /// + public void Open() + { + lock (_lock) + { + TripTo(CircuitState.Open); + _lastFailureTime = DateTime.UtcNow; + } + } + } + + /// + /// 熔断器开启异常 + /// + public class CircuitBreakerOpenException : Exception + { + public CircuitBreakerOpenException(string message) : base(message) { } + } + + /// + /// 熔断器工具类 + /// + public static class CircuitBreakerUtil + { + /// + /// 创建熔断器 + /// + public static CircuitBreaker Create(CircuitBreakerOptions? options = null) + { + return new CircuitBreaker(options); + } + + /// + /// 创建熔断器 + /// + public static CircuitBreaker Create(int failureThreshold, TimeSpan breakDuration) + { + return new CircuitBreaker(new CircuitBreakerOptions + { + FailureThreshold = failureThreshold, + BreakDuration = breakDuration + }); + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/CommandLineParser.cs b/EasyTool.Core/ToolCategory/CommandLineParser.cs new file mode 100644 index 0000000..253d057 --- /dev/null +++ b/EasyTool.Core/ToolCategory/CommandLineParser.cs @@ -0,0 +1,297 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 命令行参数解析器 + /// + public class CommandLineParser + { + private readonly Dictionary _options = new(StringComparer.OrdinalIgnoreCase); + private readonly List _arguments = new(); + private readonly HashSet _flags = new(StringComparer.OrdinalIgnoreCase); + + /// + /// 获取选项数量 + /// + public int OptionCount => _options.Count; + + /// + /// 获取参数数量 + /// + public int ArgumentCount => _arguments.Count; + + /// + /// 获取标志数量 + /// + public int FlagCount => _flags.Count; + + /// + /// 解析命令行参数 + /// + public static CommandLineParser Parse(string[] args, CommandLineOptions? options = null) + { + var parser = new CommandLineParser(); + options ??= new CommandLineOptions(); + + for (int i = 0; i < args.Length; i++) + { + var arg = args[i]; + + if (arg.StartsWith("--")) + { + // 长选项 --option 或 --option=value + var option = arg.Substring(2); + var eqIndex = option.IndexOf('='); + + if (eqIndex >= 0) + { + var name = option.Substring(0, eqIndex); + var value = option.Substring(eqIndex + 1); + parser._options[name] = value; + } + else if (i + 1 < args.Length && !args[i + 1].StartsWith("-")) + { + parser._options[option] = args[++i]; + } + else + { + parser._flags.Add(option); + } + } + else if (arg.StartsWith("-")) + { + // 短选项 -o 或 -o value + var option = arg.Substring(1); + + if (option.Length == 1) + { + if (i + 1 < args.Length && !args[i + 1].StartsWith("-")) + { + parser._options[option] = args[++i]; + } + else + { + parser._flags.Add(option); + } + } + else + { + // 组合短选项 -abc 相当于 -a -b -c + foreach (var c in option) + { + parser._flags.Add(c.ToString()); + } + } + } + else + { + parser._arguments.Add(arg); + } + } + + return parser; + } + + /// + /// 获取选项值 + /// + public string? GetOption(string name, string? defaultValue = null) + { + return _options.TryGetValue(name, out var value) ? value : defaultValue; + } + + /// + /// 获取选项值(转换为指定类型) + /// + public T? GetOption(string name, T? defaultValue = default) + { + var value = GetOption(name); + if (value == null) return defaultValue; + + try + { + return (T?)Convert.ChangeType(value, typeof(T)); + } + catch + { + return defaultValue; + } + } + + /// + /// 检查是否有选项 + /// + public bool HasOption(string name) + { + return _options.ContainsKey(name) || _flags.Contains(name); + } + + /// + /// 检查是否有标志 + /// + public bool HasFlag(string flag) + { + return _flags.Contains(flag); + } + + /// + /// 获取参数 + /// + public string? GetArgument(int index, string? defaultValue = null) + { + return index >= 0 && index < _arguments.Count ? _arguments[index] : defaultValue; + } + + /// + /// 获取参数(转换为指定类型) + /// + public T? GetArgument(int index, T? defaultValue = default) + { + var value = GetArgument(index); + if (value == null) return defaultValue; + + try + { + return (T?)Convert.ChangeType(value, typeof(T)); + } + catch + { + return defaultValue; + } + } + + /// + /// 获取所有参数 + /// + public IReadOnlyList GetArguments() + { + return _arguments.AsReadOnly(); + } + + /// + /// 获取所有选项 + /// + public IReadOnlyDictionary GetOptions() + { + return _options; + } + + /// + /// 获取所有标志 + /// + public IReadOnlyCollection GetFlags() + { + return _flags.ToList().AsReadOnly(); + } + } + + /// + /// 命令行解析选项 + /// + public class CommandLineOptions + { + /// + /// 是否允许组合短选项 + /// + public bool AllowCombinedShortOptions { get; set; } = true; + + /// + /// 是否忽略未知选项 + /// + public bool IgnoreUnknownOptions { get; set; } = true; + } + + /// + /// 参数构建器 + /// + public class ArgumentBuilder + { + private readonly List _args = new(); + + /// + /// 添加参数 + /// + public ArgumentBuilder Add(string value) + { + _args.Add(value); + return this; + } + + /// + /// 添加选项 + /// + public ArgumentBuilder AddOption(string name, string? value = null) + { + _args.Add($"--{name}"); + if (value != null) + { + _args.Add(value); + } + return this; + } + + /// + /// 添加短选项 + /// + public ArgumentBuilder AddShortOption(char name, string? value = null) + { + _args.Add($"-{name}"); + if (value != null) + { + _args.Add(value); + } + return this; + } + + /// + /// 添加标志 + /// + public ArgumentBuilder AddFlag(string name) + { + _args.Add($"--{name}"); + return this; + } + + /// + /// 添加多个参数 + /// + public ArgumentBuilder AddRange(IEnumerable values) + { + _args.AddRange(values); + return this; + } + + /// + /// 构建参数数组 + /// + public string[] Build() + { + return _args.ToArray(); + } + + /// + /// 构建命令行字符串 + /// + public string BuildCommandLine() + { + return string.Join(" ", _args.Select(QuoteIfNeeded)); + } + + private static string QuoteIfNeeded(string arg) + { + if (arg.Contains(' ') || arg.Contains('"')) + { + return $"\"{arg.Replace("\"", "\\\"")}\""; + } + return arg; + } + + public override string ToString() + { + return BuildCommandLine(); + } + } +} diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs deleted file mode 100644 index 564bc84..0000000 --- a/EasyTool.Core/ToolCategory/EnumUtil.cs +++ /dev/null @@ -1,364 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; - -namespace EasyTool.ToolCategory -{ - /// - /// 枚举增强工具类 - /// 提供枚举类型的扩展功能 - /// - public static class EnumUtil - { -#if NET5_0_OR_GREATER - private static System.Random GetSharedRandom() => System.Random.Shared; -#else - private static readonly ThreadLocal ThreadLocalRandom = new(() => new System.Random(Guid.NewGuid().GetHashCode())); - private static System.Random GetSharedRandom() => ThreadLocalRandom.Value!; -#endif - - /// - /// 获取枚举的所有值 - /// - /// 枚举类型 - /// 枚举值数组 - public static T[] GetValues() where T : struct, Enum - { -#if NET5_0_OR_GREATER - return Enum.GetValues(); -#else - return (T[])Enum.GetValues(typeof(T)); -#endif - } - - /// - /// 获取枚举的所有名称 - /// - /// 枚举类型 - /// 名称数组 - public static string[] GetNames() where T : struct, Enum - { -#if NET5_0_OR_GREATER - return Enum.GetNames(); -#else - return Enum.GetNames(typeof(T)); -#endif - } - - /// - /// 将名称转换为枚举值 - /// - /// 枚举类型 - /// 名称 - /// 是否忽略大小写 - /// 枚举值 - public static T Parse(string name, bool ignoreCase = false) where T : struct, Enum - { - return (T)Enum.Parse(typeof(T), name, ignoreCase); - } - - /// - /// 尝试将名称转换为枚举值 - /// - /// 枚举类型 - /// 名称 - /// 转换结果 - /// 是否忽略大小写 - /// 是否转换成功 - public static bool TryParse(string? name, out T result, bool ignoreCase = false) where T : struct, Enum - { - result = default; - if (name == null) return false; - - try - { - result = (T)Enum.Parse(typeof(T), name, ignoreCase); - return true; - } - catch - { - return false; - } - } - - /// - /// 将整数值转换为枚举 - /// - /// 枚举类型 - /// 整数值 - /// 枚举值 - public static T FromInt(int value) where T : struct, Enum - { - return (T)(object)value; - } - - /// - /// 尝试将整数值转换为枚举 - /// - /// 枚举类型 - /// 整数值 - /// 转换结果 - /// 是否转换成功 - public static bool TryFromInt(int value, out T result) where T : struct, Enum - { - result = default; - if (!Enum.IsDefined(typeof(T), value)) - return false; - - result = (T)(object)value; - return true; - } - - /// - /// 获取枚举值的名称 - /// - /// 枚举类型 - /// 枚举值 - /// 名称 - public static string GetName(T value) where T : struct, Enum - { - return Enum.GetName(typeof(T), value) ?? string.Empty; - } - - /// - /// 检查值是否为有效的枚举值 - /// - /// 枚举类型 - /// 要检查的值 - /// 是否有效 - public static bool IsDefined(T value) where T : struct, Enum - { - return Enum.IsDefined(typeof(T), value); - } - - /// - /// 检查整数值是否为有效的枚举值 - /// - /// 枚举类型 - /// 要检查的整数值 - /// 是否有效 - public static bool IsDefined(int value) where T : struct, Enum - { - return Enum.IsDefined(typeof(T), value); - } - - /// - /// 获取枚举的基础类型 - /// - /// 枚举类型 - /// 基础类型 - public static Type GetUnderlyingType() where T : struct, Enum - { - return Enum.GetUnderlyingType(typeof(T)); - } - - /// - /// 获取枚举值的整数形式 - /// - /// 枚举类型 - /// 枚举值 - /// 整数值 - public static int ToInt(T value) where T : struct, Enum - { - return Convert.ToInt32(value); - } - - /// - /// 获取枚举的描述信息列表 - /// - /// 枚举类型 - /// 描述信息列表 - public static List> GetInfoList() where T : struct, Enum - { - return GetValues() - .Select(v => new EnumInfo - { - Value = v, - Name = GetName(v), - IntValue = ToInt(v) - }) - .ToList(); - } - - /// - /// 获取下一个枚举值(循环) - /// - /// 枚举类型 - /// 当前值 - /// 下一个值 - public static T Next(T value) where T : struct, Enum - { - var values = GetValues(); - var index = Array.IndexOf(values, value); - return values[(index + 1) % values.Length]; - } - - /// - /// 获取上一个枚举值(循环) - /// - /// 枚举类型 - /// 当前值 - /// 上一个值 - public static T Previous(T value) where T : struct, Enum - { - var values = GetValues(); - var index = Array.IndexOf(values, value); - return values[(index - 1 + values.Length) % values.Length]; - } - - /// - /// 获取枚举值数量 - /// - /// 枚举类型 - /// 数量 - public static int Count() where T : struct, Enum - { - return Enum.GetNames(typeof(T)).Length; - } - - /// - /// 获取最小枚举值 - /// - /// 枚举类型 - /// 最小值 - public static T Min() where T : struct, Enum - { - return GetValues().Min(); - } - - /// - /// 获取最大枚举值 - /// - /// 枚举类型 - /// 最大值 - public static T Max() where T : struct, Enum - { - return GetValues().Max(); - } - - /// - /// 随机获取一个枚举值 - /// - /// 枚举类型 - /// 随机枚举值 - public static T Random() where T : struct, Enum - { - var values = GetValues(); - return values[GetSharedRandom().Next(values.Length)]; - } - - /// - /// 检查是否为标志枚举(Flags) - /// - /// 枚举类型 - /// 是否为标志枚举 - public static bool IsFlags() where T : struct, Enum - { - return typeof(T).IsDefined(typeof(FlagsAttribute), false); - } - - /// - /// 获取标志枚举中设置的所有标志 - /// - /// 枚举类型 - /// 标志值 - /// 设置的标志列表 - public static List GetFlags(T flags) where T : struct, Enum - { - var result = new List(); - foreach (var value in GetValues()) - { - if (flags.HasFlag(value) && ToInt(value) != 0) - { - result.Add(value); - } - } - return result; - } - - /// - /// 设置标志 - /// - /// 枚举类型 - /// 当前标志 - /// 要设置的标志 - /// 新的标志值 - public static T SetFlag(T flags, T flag) where T : struct, Enum - { - return (T)(object)(ToInt(flags) | ToInt(flag)); - } - - /// - /// 清除标志 - /// - /// 枚举类型 - /// 当前标志 - /// 要清除的标志 - /// 新的标志值 - public static T ClearFlag(T flags, T flag) where T : struct, Enum - { - return (T)(object)(ToInt(flags) & ~ToInt(flag)); - } - - /// - /// 切换标志 - /// - /// 枚举类型 - /// 当前标志 - /// 要切换的标志 - /// 新的标志值 - public static T ToggleFlag(T flags, T flag) where T : struct, Enum - { - return (T)(object)(ToInt(flags) ^ ToInt(flag)); - } - - /// - /// 创建枚举值的字典(名称 -> 值) - /// - /// 枚举类型 - /// 字典 - public static Dictionary ToDictionary() where T : struct, Enum - { - return GetNames().ToDictionary( - name => name, - name => Parse(name)); - } - - /// - /// 创建枚举值的字典(值 -> 名称) - /// - /// 枚举类型 - /// 字典 - public static Dictionary ToValueNameDictionary() where T : struct, Enum - { - return GetValues().ToDictionary( - value => ToInt(value), - value => GetName(value)); - } - } - - /// - /// 枚举信息 - /// - /// 枚举类型 - public class EnumInfo where T : struct, Enum - { - /// - /// 枚举值 - /// - public T Value { get; set; } - - /// - /// 名称 - /// - public string Name { get; set; } = string.Empty; - - /// - /// 整数值 - /// - public int IntValue { get; set; } - - public override string ToString() => $"{Name} = {IntValue}"; - } -} diff --git a/EasyTool.Core/ToolCategory/EventArgsUtil.cs b/EasyTool.Core/ToolCategory/EventArgsUtil.cs new file mode 100644 index 0000000..501257c --- /dev/null +++ b/EasyTool.Core/ToolCategory/EventArgsUtil.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 弱事件订阅器 + /// 避免内存泄漏 + /// + public class WeakEvent where TEventArgs : EventArgs + { + private readonly List _handlers = new(); + private readonly object _lock = new(); + + /// + /// 订阅 + /// + public void Subscribe(EventHandler handler) + { + lock (_lock) + { + _handlers.Add(new WeakReference(handler)); + } + } + + /// + /// 取消订阅 + /// + public void Unsubscribe(EventHandler handler) + { + lock (_lock) + { + for (int i = _handlers.Count - 1; i >= 0; i--) + { + if (_handlers[i].Target is EventHandler existing && + existing == handler) + { + _handlers.RemoveAt(i); + } + } + } + } + + /// + /// 触发事件 + /// + public void Raise(object sender, TEventArgs args) + { + List?> handlers; + + lock (_lock) + { + handlers = _handlers + .Where(w => w.IsAlive) + .Select(w => w.Target as EventHandler) + .ToList(); + } + + foreach (var handler in handlers) + { + handler?.Invoke(sender, args); + } + } + + /// + /// 清理无效引用 + /// + public void Cleanup() + { + lock (_lock) + { + for (int i = _handlers.Count - 1; i >= 0; i--) + { + if (!_handlers[i].IsAlive) + { + _handlers.RemoveAt(i); + } + } + } + } + } + + /// + /// 属性变更事件参数 + /// + public class PropertyChangedEventArgs : EventArgs + { + /// + /// 属性名 + /// + public string PropertyName { get; } + + /// + /// 旧值 + /// + public object? OldValue { get; } + + /// + /// 新值 + /// + public object? NewValue { get; } + + /// + /// 创建属性变更事件参数 + /// + public PropertyChangedEventArgs(string propertyName, object? oldValue, object? newValue) + { + PropertyName = propertyName; + OldValue = oldValue; + NewValue = newValue; + } + } + + /// + /// 可观察对象 + /// + public class ObservableObject + { + private readonly Dictionary _properties = new(); + + /// + /// 属性变更事件 + /// + public event EventHandler? PropertyChanged; + + /// + /// 获取属性值 + /// + protected T? GetProperty(string name, T? defaultValue = default) + { + return _properties.TryGetValue(name, out var value) ? (T?)value : defaultValue; + } + + /// + /// 设置属性值 + /// + protected bool SetProperty(string name, T? value) + { + var oldValue = GetProperty(name); + + if (EqualityComparer.Default.Equals(oldValue, value)) + return false; + + _properties[name] = value; + OnPropertyChanged(name, oldValue, value); + return true; + } + + /// + /// 触发属性变更 + /// + protected virtual void OnPropertyChanged(string propertyName, object? oldValue, object? newValue) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName, oldValue, newValue)); + } + } +} diff --git a/EasyTool.Core/ToolCategory/EventBus.cs b/EasyTool.Core/ToolCategory/EventBus.cs new file mode 100644 index 0000000..55f9b5a --- /dev/null +++ b/EasyTool.Core/ToolCategory/EventBus.cs @@ -0,0 +1,252 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 事件总线 + /// 提供发布/订阅模式的实现 + /// + public static class EventBus + { + private static readonly Dictionary> _handlers = new(); + private static readonly object _lock = new(); + + /// + /// 订阅事件 + /// + public static void Subscribe(Action handler) + { + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers = new List(); + _handlers[typeof(T)] = handlers; + } + handlers.Add(handler); + } + } + + /// + /// 取消订阅 + /// + public static void Unsubscribe(Action handler) + { + lock (_lock) + { + if (_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers.Remove(handler); + } + } + } + + /// + /// 发布事件 + /// + public static void Publish(T eventData) + { + List? handlersCopy; + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + return; + handlersCopy = new List(handlers); + } + + foreach (var handler in handlersCopy) + { + if (handler is Action typedHandler) + { + typedHandler(eventData); + } + } + } + + /// + /// 异步发布事件 + /// + public static async Task PublishAsync(T eventData) + { + List? handlersCopy; + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + return; + handlersCopy = new List(handlers); + } + + var tasks = new List(); + foreach (var handler in handlersCopy) + { + if (handler is Action typedHandler) + { + tasks.Add(Task.Run(() => typedHandler(eventData))); + } + else if (handler is Func asyncHandler) + { + tasks.Add(asyncHandler(eventData)); + } + } + + await Task.WhenAll(tasks); + } + + /// + /// 订阅异步事件 + /// + public static void SubscribeAsync(Func handler) + { + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers = new List(); + _handlers[typeof(T)] = handlers; + } + handlers.Add(handler); + } + } + + /// + /// 清除所有订阅 + /// + public static void Clear() + { + lock (_lock) + { + _handlers.Clear(); + } + } + + /// + /// 清除指定类型的订阅 + /// + public static void Clear() + { + lock (_lock) + { + _handlers.Remove(typeof(T)); + } + } + } + + /// + /// 泛型事件总线 + /// + public class EventBus where T : class + { + private static readonly EventBus _instance = new(); + private readonly List> _handlers = new(); + private readonly List> _asyncHandlers = new(); + private readonly object _lock = new(); + + /// + /// 获取单例实例 + /// + public static EventBus Instance => _instance; + + /// + /// 订阅 + /// + public void Subscribe(Action handler) + { + lock (_lock) + { + _handlers.Add(handler); + } + } + + /// + /// 取消订阅 + /// + public void Unsubscribe(Action handler) + { + lock (_lock) + { + _handlers.Remove(handler); + } + } + + /// + /// 异步订阅 + /// + public void SubscribeAsync(Func handler) + { + lock (_lock) + { + _asyncHandlers.Add(handler); + } + } + + /// + /// 取消异步订阅 + /// + public void UnsubscribeAsync(Func handler) + { + lock (_lock) + { + _asyncHandlers.Remove(handler); + } + } + + /// + /// 发布 + /// + public void Publish(T eventData) + { + List> handlersCopy; + lock (_lock) + { + handlersCopy = new List>(_handlers); + } + + foreach (var handler in handlersCopy) + { + handler(eventData); + } + } + + /// + /// 异步发布 + /// + public async Task PublishAsync(T eventData) + { + List> handlersCopy; + List> asyncHandlersCopy; + + lock (_lock) + { + handlersCopy = new List>(_handlers); + asyncHandlersCopy = new List>(_asyncHandlers); + } + + var tasks = new List(); + foreach (var handler in handlersCopy) + { + tasks.Add(Task.Run(() => handler(eventData))); + } + + foreach (var handler in asyncHandlersCopy) + { + tasks.Add(handler(eventData)); + } + + await Task.WhenAll(tasks); + } + + /// + /// 清除所有订阅 + /// + public void Clear() + { + lock (_lock) + { + _handlers.Clear(); + _asyncHandlers.Clear(); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/GuardUtil.cs b/EasyTool.Core/ToolCategory/GuardUtil.cs new file mode 100644 index 0000000..8230fb4 --- /dev/null +++ b/EasyTool.Core/ToolCategory/GuardUtil.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 防御性编程工具类 + /// 提供参数验证和断言功能 + /// + public static class GuardUtil + { + /// + /// 验证参数不为null + /// + public static T NotNull(T? value, string paramName) where T : class + { + if (value == null) + throw new ArgumentNullException(paramName); + return value; + } + + /// + /// 验证可空值类型不为null + /// + public static T NotNull(T? value, string paramName) where T : struct + { + if (value == null) + throw new ArgumentNullException(paramName); + return value.Value; + } + + /// + /// 验证字符串不为空或null + /// + public static string NotNullOrEmpty(string? value, string paramName) + { + if (string.IsNullOrEmpty(value)) + throw new ArgumentException("字符串不能为空或null", paramName); + return value; + } + + /// + /// 验证字符串不为空白 + /// + public static string NotNullOrWhiteSpace(string? value, string paramName) + { + if (string.IsNullOrWhiteSpace(value)) + throw new ArgumentException("字符串不能为空白", paramName); + return value; + } + + /// + /// 验证集合不为空 + /// + public static IEnumerable NotEmpty(IEnumerable? value, string paramName) + { + if (value == null) + throw new ArgumentNullException(paramName); + + var collection = value as ICollection ?? new List(value); + if (collection.Count == 0) + throw new ArgumentException("集合不能为空", paramName); + + return collection; + } + + /// + /// 验证范围 + /// + public static int InRange(int value, int min, int max, string paramName) + { + if (value < min || value > max) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须在 {min} 和 {max} 之间"); + return value; + } + + /// + /// 验证范围 + /// + public static double InRange(double value, double min, double max, string paramName) + { + if (value < min || value > max) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须在 {min} 和 {max} 之间"); + return value; + } + + /// + /// 验证大于指定值 + /// + public static int GreaterThan(int value, int threshold, string paramName) + { + if (value <= threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须大于 {threshold}"); + return value; + } + + /// + /// 验证大于等于指定值 + /// + public static int GreaterThanOrEqual(int value, int threshold, string paramName) + { + if (value < threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须大于或等于 {threshold}"); + return value; + } + + /// + /// 验证小于指定值 + /// + public static int LessThan(int value, int threshold, string paramName) + { + if (value >= threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须小于 {threshold}"); + return value; + } + + /// + /// 验证小于等于指定值 + /// + public static int LessThanOrEqual(int value, int threshold, string paramName) + { + if (value > threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须小于或等于 {threshold}"); + return value; + } + + /// + /// 验证条件为真 + /// + public static void IsTrue(bool condition, string message, string? paramName = null) + { + if (!condition) + throw new ArgumentException(message, paramName); + } + + /// + /// 验证条件为假 + /// + public static void IsFalse(bool condition, string message, string? paramName = null) + { + if (condition) + throw new ArgumentException(message, paramName); + } + + /// + /// 验证类型 + /// + public static T IsType(object value, string paramName) + { + if (value is not T typed) + throw new ArgumentException($"值必须是 {typeof(T).Name} 类型", paramName); + return typed; + } + + /// + /// 验证枚举值有效 + /// + public static T EnumDefined(T value, string paramName) where T : struct, Enum + { + if (!Enum.IsDefined(typeof(T), value)) + throw new ArgumentException($"无效的枚举值: {value}", paramName); + return value; + } + + /// + /// 验证邮箱格式 + /// + public static string Email(string? value, string paramName) + { + NotNullOrEmpty(value, paramName); + if (!System.Text.RegularExpressions.Regex.IsMatch(value!, @"^[^@\s]+@[^@\s]+\.[^@\s]+$")) + throw new ArgumentException("无效的邮箱格式", paramName); + return value!; + } + + /// + /// 验证文件存在 + /// + public static string FileExists(string? path, string paramName) + { + NotNullOrEmpty(path, paramName); + if (!System.IO.File.Exists(path)) + throw new System.IO.FileNotFoundException($"文件不存在: {path}", path); + return path!; + } + + /// + /// 验证目录存在 + /// + public static string DirectoryExists(string? path, string paramName) + { + NotNullOrEmpty(path, paramName); + if (!System.IO.Directory.Exists(path)) + throw new System.IO.DirectoryNotFoundException($"目录不存在: {path}"); + return path!; + } + + /// + /// 抛出异常 + /// + public static void Throw(string message) where TException : Exception, new() + { + var exception = (TException?)Activator.CreateInstance(typeof(TException), message) + ?? new TException(); + throw exception; + } + + /// + /// 如果条件为真,抛出异常 + /// + public static void ThrowIf(bool condition, string message) where TException : Exception, new() + { + if (condition) + Throw(message); + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/LogUtil.cs b/EasyTool.Core/ToolCategory/LogUtil.cs new file mode 100644 index 0000000..bff5211 --- /dev/null +++ b/EasyTool.Core/ToolCategory/LogUtil.cs @@ -0,0 +1,194 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.ToolCategory +{ + /// + /// 日志级别 + /// + public enum LogLevel + { + Trace, + Debug, + Information, + Warning, + Error, + Critical + } + + /// + /// 日志工具类 + /// + public static class LogUtil + { + private static LogLevel _minLevel = LogLevel.Information; + private static string _logDirectory = "logs"; + private static bool _consoleOutput = true; + private static bool _fileOutput = true; + private static readonly object _lock = new(); + + /// + /// 最小日志级别 + /// + public static LogLevel MinLevel + { + get => _minLevel; + set => _minLevel = value; + } + + /// + /// 日志目录 + /// + public static string LogDirectory + { + get => _logDirectory; + set + { + _logDirectory = value; + if (!Directory.Exists(value)) + Directory.CreateDirectory(value); + } + } + + /// + /// 是否输出到控制台 + /// + public static bool ConsoleOutput + { + get => _consoleOutput; + set => _consoleOutput = value; + } + + /// + /// 是否输出到文件 + /// + public static bool FileOutput + { + get => _fileOutput; + set => _fileOutput = value; + } + + /// + /// 配置日志 + /// + public static void Configure(LogLevel minLevel, string? logDirectory = null, bool consoleOutput = true, bool fileOutput = true) + { + _minLevel = minLevel; + _consoleOutput = consoleOutput; + _fileOutput = fileOutput; + if (logDirectory != null) + LogDirectory = logDirectory; + } + + /// + /// 记录日志 + /// + public static void Log(LogLevel level, string message, Exception? exception = null, string? category = null) + { + if (level < _minLevel) + return; + + var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); + var levelStr = level.ToString().ToUpper().PadLeft(5); + var categoryStr = category != null ? $"[{category}] " : ""; + var sb = new StringBuilder(); + sb.Append($"[{timestamp}] [{levelStr}] {categoryStr}{message}"); + + if (exception != null) + { + sb.AppendLine(); + sb.Append($"Exception: {exception.GetType().Name}: {exception.Message}"); + if (!string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine(); + sb.Append(exception.StackTrace); + } + } + + var logMessage = sb.ToString(); + + lock (_lock) + { + if (_consoleOutput) + { + var color = GetConsoleColor(level); + var originalColor = Console.ForegroundColor; + Console.ForegroundColor = color; + Console.WriteLine(logMessage); + Console.ForegroundColor = originalColor; + } + + if (_fileOutput) + { + WriteToFile(level, logMessage); + } + } + } + + private static ConsoleColor GetConsoleColor(LogLevel level) + { + return level switch + { + LogLevel.Trace => ConsoleColor.Gray, + LogLevel.Debug => ConsoleColor.Gray, + LogLevel.Information => ConsoleColor.White, + LogLevel.Warning => ConsoleColor.Yellow, + LogLevel.Error => ConsoleColor.Red, + LogLevel.Critical => ConsoleColor.DarkRed, + _ => ConsoleColor.White + }; + } + + private static void WriteToFile(LogLevel level, string message) + { + if (!Directory.Exists(_logDirectory)) + Directory.CreateDirectory(_logDirectory); + + var fileName = level switch + { + LogLevel.Error or LogLevel.Critical => $"error_{DateTime.Now:yyyyMMdd}.log", + _ => $"log_{DateTime.Now:yyyyMMdd}.log" + }; + + var filePath = Path.Combine(_logDirectory, fileName); + File.AppendAllText(filePath, message + Environment.NewLine); + } + + /// + /// 记录跟踪日志 + /// + public static void Trace(string message, string? category = null) + => Log(LogLevel.Trace, message, category: category); + + /// + /// 记录调试日志 + /// + public static void Debug(string message, string? category = null) + => Log(LogLevel.Debug, message, category: category); + + /// + /// 记录信息日志 + /// + public static void Info(string message, string? category = null) + => Log(LogLevel.Information, message, category: category); + + /// + /// 记录警告日志 + /// + public static void Warning(string message, string? category = null) + => Log(LogLevel.Warning, message, category: category); + + /// + /// 记录错误日志 + /// + public static void Error(string message, Exception? exception = null, string? category = null) + => Log(LogLevel.Error, message, exception, category); + + /// + /// 记录严重错误日志 + /// + public static void Critical(string message, Exception? exception = null, string? category = null) + => Log(LogLevel.Critical, message, exception, category); + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/ObjectPool.cs b/EasyTool.Core/ToolCategory/ObjectPool.cs new file mode 100644 index 0000000..d3e40d0 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ObjectPool.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 对象池 + /// 用于重用对象,减少GC压力 + /// + /// 对象类型 + public class ObjectPool where T : class + { + private readonly Stack _pool; + private readonly Func _factory; + private readonly Action? _reset; + private readonly int _maxSize; + private readonly object _lock = new(); + + /// + /// 创建对象池 + /// + /// 对象工厂 + /// 最大池大小 + /// 重置动作 + public ObjectPool(Func factory, int maxSize = 100, Action? reset = null) + { + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); + _maxSize = maxSize; + _reset = reset; + _pool = new Stack(); + } + + /// + /// 当前池中对象数量 + /// + public int Count + { + get { lock (_lock) { return _pool.Count; } } + } + + /// + /// 从池中获取对象 + /// + public T Get() + { + lock (_lock) + { + if (_pool.Count > 0) + return _pool.Pop(); + } + return _factory(); + } + + /// + /// 将对象归还到池中 + /// + public void Return(T item) + { + if (item == null) + return; + + _reset?.Invoke(item); + + lock (_lock) + { + if (_pool.Count < _maxSize) + _pool.Push(item); + } + } + + /// + /// 使用池中对象执行操作 + /// + public TResult Use(Func action) + { + var item = Get(); + try + { + return action(item); + } + finally + { + Return(item); + } + } + + /// + /// 使用池中对象执行操作 + /// + public void Use(Action action) + { + var item = Get(); + try + { + action(item); + } + finally + { + Return(item); + } + } + + /// + /// 清空池 + /// + public void Clear() + { + lock (_lock) + { + _pool.Clear(); + } + } + + /// + /// 预热池(创建指定数量的对象) + /// + public void WarmUp(int count) + { + for (int i = 0; i < count; i++) + { + var item = _factory(); + Return(item); + } + } + } + + /// + /// 对象池扩展 + /// + public static class ObjectPoolExtensions + { + /// + /// 创建对象池 + /// + public static ObjectPool CreatePool(this Func factory, int maxSize = 100, Action? reset = null) + where T : class + { + return new ObjectPool(factory, maxSize, reset); + } + } +} diff --git a/EasyTool.Core/ToolCategory/ObjectUtil.cs b/EasyTool.Core/ToolCategory/ObjectUtil.cs new file mode 100644 index 0000000..21da7a3 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ObjectUtil.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text.Json; + +namespace EasyTool.ToolCategory +{ + /// + /// 对象工具类 + /// 提供对象的常用操作功能 + /// + public static class ObjectUtil + { + /// + /// 深拷贝对象(使用 JSON 序列化) + /// + /// 对象类型 + /// 原对象 + /// 拷贝后的对象 + public static T? DeepClone(T obj) + { + if (obj == null) + return default; + + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize(json); + } + + /// + /// 浅拷贝对象 + /// + /// 对象类型 + /// 原对象 + /// 拷贝后的对象 + public static T? ShallowClone(T obj) where T : class + { + if (obj == null) + return null; + + var type = obj.GetType(); + var method = type.GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic); + + if (method != null) + { + return (T?)method.Invoke(obj, null); + } + + return null; + } + + /// + /// 比较两个对象是否相等(深度比较) + /// + /// 对象类型 + /// 对象1 + /// 对象2 + /// 是否相等 + public static bool DeepEquals(T? obj1, T? obj2) + { + if (ReferenceEquals(obj1, obj2)) + return true; + + if (obj1 == null || obj2 == null) + return false; + + var json1 = JsonSerializer.Serialize(obj1); + var json2 = JsonSerializer.Serialize(obj2); + + return json1 == json2; + } + + /// + /// 获取对象的哈希码(基于内容) + /// + /// 对象类型 + /// 对象 + /// 哈希码 + public static int GetDeepHashCode(T obj) + { + if (obj == null) + return 0; + + var json = JsonSerializer.Serialize(obj); + return json.GetHashCode(); + } + + /// + /// 检查对象是否为默认值 + /// + /// 对象类型 + /// 对象 + /// 是否为默认值 + public static bool IsDefault(T obj) + { + return EqualityComparer.Default.Equals(obj, default); + } + + /// + /// 获取对象的类型名称 + /// + /// 对象类型 + /// 对象 + /// 类型名称 + public static string GetTypeName(T obj) + { + if (obj == null) + return "null"; + + return obj.GetType().Name; + } + + /// + /// 获取对象的完整类型名称 + /// + /// 对象类型 + /// 对象 + /// 完整类型名称 + public static string GetTypeFullName(T obj) + { + if (obj == null) + return "null"; + + return obj.GetType().FullName ?? obj.GetType().Name; + } + + /// + /// 将对象转换为字典 + /// + /// 对象 + /// 属性字典 + public static Dictionary ToDictionary(object obj) + { + if (obj == null) + return new Dictionary(); + + if (obj is IDictionary dict) + { + var result = new Dictionary(); + foreach (DictionaryEntry entry in dict) + { + result[entry.Key?.ToString() ?? ""] = entry.Value; + } + return result; + } + + var type = obj.GetType(); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + var result2 = new Dictionary(); + + foreach (var property in properties) + { + if (property.CanRead) + { + result2[property.Name] = property.GetValue(obj); + } + } + + return result2; + } + + /// + /// 从字典创建对象 + /// + /// 对象类型 + /// 属性字典 + /// 对象实例 + public static T? FromDictionary(Dictionary dictionary) where T : class, new() + { + if (dictionary == null || dictionary.Count == 0) + return default; + + var type = typeof(T); + var obj = new T(); + + foreach (var kvp in dictionary) + { + var property = type.GetProperty(kvp.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + if (property != null && property.CanWrite) + { + var value = ConvertValue(kvp.Value, property.PropertyType); + property.SetValue(obj, value); + } + } + + return obj; + } + + private static object? ConvertValue(object? value, Type targetType) + { + if (value == null) + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + + if (targetType.IsAssignableFrom(value.GetType())) + return value; + + try + { + return Convert.ChangeType(value, targetType); + } + catch + { + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + } + } + + /// + /// 合并两个对象的属性 + /// + /// 对象类型 + /// 目标对象 + /// 源对象 + /// 是否覆盖已有值 + /// 合并后的对象 + public static T Merge(T target, T source, bool overwrite = true) where T : class + { + if (target == null) + return source ?? target; + + if (source == null) + return target; + + var type = typeof(T); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in properties) + { + if (!property.CanRead || !property.CanWrite) + continue; + + var sourceValue = property.GetValue(source); + var targetValue = property.GetValue(target); + + if (sourceValue != null && (overwrite || targetValue == null)) + { + property.SetValue(target, sourceValue); + } + } + + return target; + } + + /// + /// 检查对象是否有指定属性 + /// + /// 对象类型 + /// 对象 + /// 属性名 + /// 是否有属性 + public static bool HasProperty(T obj, string propertyName) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return false; + + var type = obj.GetType(); + return type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance) != null; + } + + /// + /// 获取对象的属性值 + /// + /// 对象类型 + /// 对象 + /// 属性名 + /// 属性值 + public static object? GetPropertyValue(T obj, string propertyName) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return null; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); + + return property?.CanRead == true ? property.GetValue(obj) : null; + } + + /// + /// 设置对象的属性值 + /// + /// 对象类型 + /// 对象 + /// 属性名 + /// 属性值 + /// 是否设置成功 + public static bool SetPropertyValue(T obj, string propertyName, object? value) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return false; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); + + if (property?.CanWrite != true) + return false; + + try + { + var convertedValue = ConvertValue(value, property.PropertyType); + property.SetValue(obj, convertedValue); + return true; + } + catch + { + return false; + } + } + + /// + /// 获取对象的所有属性名 + /// + /// 对象类型 + /// 对象 + /// 属性名列表 + public static string[] GetPropertyNames(T obj) + { + if (obj == null) + return Array.Empty(); + + var type = obj.GetType(); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + return properties.Select(p => p.Name).ToArray(); + } + + /// + /// 转换类型 + /// + /// 目标类型 + /// 值 + /// 转换后的值 + public static T? ConvertTo(object? value) + { + if (value == null) + return default; + + if (value is T t) + return t; + + try + { + var targetType = typeof(T); + + if (targetType == typeof(Guid) && value is string str) + { + return (T)(object)Guid.Parse(str); + } + + if (targetType == typeof(DateTime) && value is string dateStr) + { + return (T)(object)DateTime.Parse(dateStr); + } + + return (T)Convert.ChangeType(value, targetType); + } + catch + { + return default; + } + } + + /// + /// 安全转换为字符串 + /// + /// 值 + /// 字符串 + public static string SafeToString(object? value) + { + return value?.ToString() ?? string.Empty; + } + + /// + /// 检查对象是否为空或空集合 + /// + /// 值 + /// 是否为空 + public static bool IsNullOrEmpty(object? value) + { + if (value == null) + return true; + + if (value is string str) + return string.IsNullOrEmpty(str); + + if (value is IEnumerable enumerable) + { + var enumerator = enumerable.GetEnumerator(); + return !enumerator.MoveNext(); + } + + return false; + } + } +} diff --git a/EasyTool.Core/ToolCategory/PipelineUtil.cs b/EasyTool.Core/ToolCategory/PipelineUtil.cs new file mode 100644 index 0000000..dc060bc --- /dev/null +++ b/EasyTool.Core/ToolCategory/PipelineUtil.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 管道上下文 + /// + public class PipelineContext + { + private readonly Dictionary _items = new(); + + /// + /// 获取或设置项 + /// + public object? this[string key] + { + get => _items.TryGetValue(key, out var value) ? value : null; + set => _items[key] = value!; + } + + /// + /// 获取值 + /// + public T? Get(string key) + { + return _items.TryGetValue(key, out var value) ? (T?)value : default; + } + + /// + /// 设置值 + /// + public void Set(string key, T value) + { + _items[key] = value!; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(string key) => _items.ContainsKey(key); + + /// + /// 移除项 + /// + public bool Remove(string key) => _items.Remove(key); + + /// + /// 清空 + /// + public void Clear() => _items.Clear(); + } + + /// + /// 管道处理委托 + /// + public delegate Task PipelineDelegate(PipelineContext context); + + /// + /// 管道构建器 + /// + public class PipelineBuilder + { + private readonly List> _middlewares = new(); + + /// + /// 添加中间件 + /// + public PipelineBuilder Use(Func middleware) + { + _middlewares.Add(middleware); + return this; + } + + /// + /// 添加中间件 + /// + public PipelineBuilder Use(Func middleware) + { + return Use(next => context => middleware(context, next)); + } + + /// + /// 添加同步中间件 + /// + public PipelineBuilder Use(Action middleware) + { + return Use(next => context => + { + middleware(context, () => next(context).GetAwaiter().GetResult()); + return Task.CompletedTask; + }); + } + + /// + /// 条件分支 + /// + public PipelineBuilder UseWhen(Func predicate, Action configure) + { + var branchBuilder = new PipelineBuilder(); + configure(branchBuilder); + + return Use(next => + { + var branch = branchBuilder.Build(next); + return context => predicate(context) ? branch(context) : next(context); + }); + } + + /// + /// 映射分支 + /// + public PipelineBuilder Map(string path, Action configure) + { + return UseWhen(ctx => ctx.Get("Path")?.StartsWith(path) == true, configure); + } + + /// + /// 异常处理 + /// + public PipelineBuilder UseExceptionHandling(Func? handler = null) + { + return Use(next => async context => + { + try + { + await next(context); + } + catch (Exception ex) + { + if (handler != null) + await handler(context, ex); + else + context.Set("Exception", ex); + } + }); + } + + /// + /// 超时处理 + /// + public PipelineBuilder UseTimeout(TimeSpan timeout) + { + return Use(next => async context => + { + using var cts = new System.Threading.CancellationTokenSource(timeout); + var task = next(context); + var completed = await Task.WhenAny(task, Task.Delay(timeout)); + + if (completed != task) + { + context.Set("Timeout", true); + throw new TimeoutException($"管道执行超时: {timeout}"); + } + + await task; + }); + } + + /// + /// 日志记录 + /// + public PipelineBuilder UseLogging(Action? log = null) + { + return Use(async (context, next) => + { + log?.Invoke($"[{DateTime.Now:HH:mm:ss}] 开始执行管道"); + await next(context); + log?.Invoke($"[{DateTime.Now:HH:mm:ss}] 结束执行管道"); + }); + } + + /// + /// 计时 + /// + public PipelineBuilder UseTiming(Action? callback = null) + { + return Use(async (context, next) => + { + var sw = System.Diagnostics.Stopwatch.StartNew(); + await next(context); + sw.Stop(); + callback?.Invoke(sw.Elapsed); + context.Set("ElapsedTime", sw.Elapsed); + }); + } + + /// + /// 构建管道 + /// + public PipelineDelegate Build(PipelineDelegate? terminal = null) + { + terminal ??= _ => Task.CompletedTask; + + for (int i = _middlewares.Count - 1; i >= 0; i--) + { + terminal = _middlewares[i](terminal); + } + + return terminal; + } + } + + /// + /// 泛型管道(带结果类型) + /// + public class Pipeline + { + private readonly List>, Task>> _middlewares = new(); + + /// + /// 添加中间件 + /// + public Pipeline Use(Func>, Task> middleware) + { + _middlewares.Add(middleware); + return this; + } + + /// + /// 执行管道 + /// + public async Task ExecuteAsync(TInput input, Func> terminal) + { + Func> current = terminal; + + for (int i = _middlewares.Count - 1; i >= 0; i--) + { + var middleware = _middlewares[i]; + var next = current; + current = ctx => middleware(ctx, next); + } + + return await current(input); + } + } + + /// + /// 管道工具类 + /// + public static class PipelineUtil + { + /// + /// 创建管道构建器 + /// + public static PipelineBuilder CreateBuilder() + { + return new PipelineBuilder(); + } + + /// + /// 创建泛型管道 + /// + public static Pipeline Create() + { + return new Pipeline(); + } + + /// + /// 快速执行管道 + /// + public static async Task ExecuteAsync(Action configure, PipelineContext? context = null) + { + var builder = new PipelineBuilder(); + configure(builder); + var pipeline = builder.Build(); + await pipeline(context ?? new PipelineContext()); + } + } +} diff --git a/EasyTool.Core/ToolCategory/ProducerConsumer.cs b/EasyTool.Core/ToolCategory/ProducerConsumer.cs new file mode 100644 index 0000000..89a4087 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ProducerConsumer.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 生产者消费者模式工具类 + /// + /// 数据类型 + public class ProducerConsumer + { + private readonly System.Collections.Concurrent.BlockingCollection _collection; + private readonly Action _consumer; + private readonly int _maxConsumers; + private readonly List _consumerTasks; + private bool _isRunning; + private readonly object _lock = new(); + + /// + /// 队列中元素数量 + /// + public int Count => _collection.Count; + + /// + /// 是否正在运行 + /// + public bool IsRunning => _isRunning; + + /// + /// 是否已完成添加 + /// + public bool IsAddingCompleted => _collection.IsAddingCompleted; + + /// + /// 创建生产者消费者 + /// + /// 消费者处理函数 + /// 队列容量(0表示无限制) + /// 最大消费者数量 + public ProducerConsumer(Action consumer, int boundedCapacity = 0, int maxConsumers = 1) + { + _consumer = consumer ?? throw new ArgumentNullException(nameof(consumer)); + _maxConsumers = maxConsumers; + _collection = boundedCapacity > 0 + ? new System.Collections.Concurrent.BlockingCollection(boundedCapacity) + : new System.Collections.Concurrent.BlockingCollection(); + _consumerTasks = new List(); + } + + /// + /// 启动消费者 + /// + public void Start() + { + lock (_lock) + { + if (_isRunning) return; + _isRunning = true; + + for (int i = 0; i < _maxConsumers; i++) + { + _consumerTasks.Add(System.Threading.Tasks.Task.Run(ConsumeLoop)); + } + } + } + + /// + /// 生产数据 + /// + public void Produce(T item) + { + _collection.Add(item); + } + + /// + /// 批量生产数据 + /// + public void ProduceRange(IEnumerable items) + { + foreach (var item in items) + { + _collection.Add(item); + } + } + + /// + /// 尝试生产数据(不阻塞) + /// + public bool TryProduce(T item, TimeSpan timeout) + { + return _collection.TryAdd(item, timeout); + } + + /// + /// 标记添加完成 + /// + public void CompleteAdding() + { + _collection.CompleteAdding(); + } + + /// + /// 停止并等待完成 + /// + public void Stop() + { + lock (_lock) + { + if (!_isRunning) return; + _collection.CompleteAdding(); + System.Threading.Tasks.Task.WaitAll(_consumerTasks.ToArray()); + _isRunning = false; + } + } + + /// + /// 异步停止 + /// + public async System.Threading.Tasks.Task StopAsync() + { + lock (_lock) + { + if (!_isRunning) return; + _collection.CompleteAdding(); + } + + await System.Threading.Tasks.Task.WhenAll(_consumerTasks); + + lock (_lock) + { + _isRunning = false; + } + } + + private void ConsumeLoop() + { + foreach (var item in _collection.GetConsumingEnumerable()) + { + try + { + _consumer(item); + } + catch + { + // 忽略消费者异常,继续处理下一个 + } + } + } + } + + /// + /// 异步通道(类似Go的channel) + /// + /// 数据类型 + public class Channel + { + private readonly System.Collections.Concurrent.ConcurrentQueue _queue = new(); + private readonly System.Threading.SemaphoreSlim _signal = new(0); + private readonly int? _capacity; + private readonly System.Threading.SemaphoreSlim? _capacitySemaphore; + private bool _closed; + + /// + /// 队列中元素数量 + /// + public int Count => _queue.Count; + + /// + /// 是否已关闭 + /// + public bool IsClosed => _closed; + + /// + /// 创建通道 + /// + /// 容量(0或null表示无限制) + public Channel(int capacity = 0) + { + _capacity = capacity > 0 ? capacity : null; + if (_capacity.HasValue) + { + _capacitySemaphore = new System.Threading.SemaphoreSlim(_capacity.Value, _capacity.Value); + } + } + + /// + /// 发送数据 + /// + public async System.Threading.Tasks.Task SendAsync(T item) + { + if (_closed) + throw new InvalidOperationException("通道已关闭"); + + if (_capacitySemaphore != null) + { + await _capacitySemaphore.WaitAsync(); + } + + _queue.Enqueue(item); + _signal.Release(); + } + + /// + /// 尝试发送数据 + /// + public async System.Threading.Tasks.Task TrySendAsync(T item, TimeSpan timeout) + { + if (_closed) + return false; + + if (_capacitySemaphore != null) + { + if (!await _capacitySemaphore.WaitAsync(timeout)) + return false; + } + + _queue.Enqueue(item); + _signal.Release(); + return true; + } + + /// + /// 接收数据 + /// + public async System.Threading.Tasks.Task ReceiveAsync() + { + await _signal.WaitAsync(); + + if (_queue.TryDequeue(out var item)) + { + _capacitySemaphore?.Release(); + return item; + } + + throw new InvalidOperationException("通道状态异常"); + } + + /// + /// 尝试接收数据 + /// + public async System.Threading.Tasks.Task<(bool Success, T? Item)> TryReceiveAsync(TimeSpan timeout) + { + if (!await _signal.WaitAsync(timeout)) + return (false, default); + + if (_queue.TryDequeue(out var item)) + { + _capacitySemaphore?.Release(); + return (true, item); + } + + return (false, default); + } + + /// + /// 关闭通道 + /// + public void Close() + { + _closed = true; + } + + /// + /// 获取所有剩余数据 + /// + public IEnumerable GetAllRemaining() + { + while (_queue.TryDequeue(out var item)) + { + yield return item; + } + } + } + + /// + /// 并行执行工具 + /// + public static class ParallelUtil + { + /// + /// 并行执行任务并收集结果 + /// + public static async System.Threading.Tasks.Task> WhenAllAsync( + IEnumerable sources, + Func> selector, + int maxDegreeOfParallelism) + { + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + var tasks = sources.Select(async source => + { + await semaphore.WaitAsync(); + try + { + return await selector(source); + } + finally + { + semaphore.Release(); + } + }); + + var results = await System.Threading.Tasks.Task.WhenAll(tasks); + return results.ToList(); + } + + /// + /// 并行执行任务 + /// + public static async System.Threading.Tasks.Task WhenAllAsync( + IEnumerable sources, + Func action, + int maxDegreeOfParallelism) + { + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + var tasks = sources.Select(async source => + { + await semaphore.WaitAsync(); + try + { + await action(source); + } + finally + { + semaphore.Release(); + } + }); + + await System.Threading.Tasks.Task.WhenAll(tasks); + } + + /// + /// 分批并行执行 + /// + public static async System.Threading.Tasks.Task WhenAllBatchedAsync( + IEnumerable sources, + Func, System.Threading.Tasks.Task> batchAction, + int batchSize) + { + var batch = new List(batchSize); + + foreach (var source in sources) + { + batch.Add(source); + if (batch.Count >= batchSize) + { + await batchAction(batch); + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + await batchAction(batch); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/RateLimiter.cs b/EasyTool.Core/ToolCategory/RateLimiter.cs new file mode 100644 index 0000000..6280275 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RateLimiter.cs @@ -0,0 +1,340 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 限流算法 + /// + public enum RateLimitAlgorithm + { + /// + /// 固定窗口 + /// + FixedWindow, + + /// + /// 滑动窗口 + /// + SlidingWindow, + + /// + /// 令牌桶 + /// + TokenBucket, + + /// + /// 漏桶 + /// + LeakyBucket + } + + /// + /// 固定窗口限流器 + /// + public class FixedWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private int _count; + private DateTime _windowStart; + private readonly object _lock = new(); + + public FixedWindowRateLimiter(int limit, TimeSpan window) + { + _limit = limit; + _window = window; + _count = 0; + _windowStart = DateTime.UtcNow; + } + + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTime.UtcNow; + if (now - _windowStart >= _window) + { + _windowStart = now; + _count = 0; + } + + if (_count < _limit) + { + _count++; + return true; + } + return false; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + var remaining = _window - (DateTime.UtcNow - _windowStart); + return remaining > TimeSpan.Zero ? remaining : TimeSpan.Zero; + } + } + } + + /// + /// 滑动窗口限流器 + /// + public class SlidingWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private readonly System.Collections.Generic.Queue _timestamps; + private readonly object _lock = new(); + + public SlidingWindowRateLimiter(int limit, TimeSpan window) + { + _limit = limit; + _window = window; + _timestamps = new(); + } + + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTime.UtcNow; + var cutoff = now - _window; + + while (_timestamps.Count > 0 && _timestamps.Peek() < cutoff) + { + _timestamps.Dequeue(); + } + + if (_timestamps.Count < _limit) + { + _timestamps.Enqueue(now); + return true; + } + return false; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + if (_timestamps.Count == 0) + return TimeSpan.Zero; + + var oldest = _timestamps.Peek(); + var waitTime = oldest + _window - DateTime.UtcNow; + return waitTime > TimeSpan.Zero ? waitTime : TimeSpan.Zero; + } + } + } + + /// + /// 令牌桶限流器 + /// + public class TokenBucketRateLimiter + { + private readonly int _capacity; + private readonly int _refillRate; + private int _tokens; + private DateTime _lastRefill; + private readonly object _lock = new(); + + public TokenBucketRateLimiter(int capacity, int refillRate) + { + _capacity = capacity; + _refillRate = refillRate; + _tokens = capacity; + _lastRefill = DateTime.UtcNow; + } + + public bool TryAcquire(int tokens = 1) + { + lock (_lock) + { + RefillTokens(); + + if (_tokens >= tokens) + { + _tokens -= tokens; + return true; + } + return false; + } + } + + private void RefillTokens() + { + var now = DateTime.UtcNow; + var elapsed = (now - _lastRefill).TotalSeconds; + var tokensToAdd = (int)(elapsed * _refillRate); + + if (tokensToAdd > 0) + { + _tokens = Math.Min(_capacity, _tokens + tokensToAdd); + _lastRefill = now; + } + } + + public TimeSpan GetWaitTime(int tokens = 1) + { + lock (_lock) + { + RefillTokens(); + if (_tokens >= tokens) + return TimeSpan.Zero; + + var tokensNeeded = tokens - _tokens; + return TimeSpan.FromSeconds((double)tokensNeeded / _refillRate); + } + } + } + + /// + /// 漏桶限流器 + /// + public class LeakyBucketRateLimiter + { + private readonly int _capacity; + private readonly int _leakRate; + private int _water; + private DateTime _lastLeak; + private readonly object _lock = new(); + + public LeakyBucketRateLimiter(int capacity, int leakRate) + { + _capacity = capacity; + _leakRate = leakRate; + _water = 0; + _lastLeak = DateTime.UtcNow; + } + + public bool TryAcquire() + { + lock (_lock) + { + Leak(); + + if (_water < _capacity) + { + _water++; + return true; + } + return false; + } + } + + private void Leak() + { + var now = DateTime.UtcNow; + var elapsed = (now - _lastLeak).TotalSeconds; + var leaked = (int)(elapsed * _leakRate); + + if (leaked > 0) + { + _water = Math.Max(0, _water - leaked); + _lastLeak = now; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + Leak(); + if (_water < _capacity) + return TimeSpan.Zero; + + return TimeSpan.FromSeconds((double)1 / _leakRate); + } + } + } + + /// + /// 限流器工具类 + /// + public static class RateLimiter + { + /// + /// 创建限流器 + /// + public static IRateLimiter Create(RateLimitAlgorithm algorithm, int limit, TimeSpan window) + { + return algorithm switch + { + RateLimitAlgorithm.FixedWindow => new FixedWindowRateLimiterWrapper(limit, window), + RateLimitAlgorithm.SlidingWindow => new SlidingWindowRateLimiterWrapper(limit, window), + RateLimitAlgorithm.TokenBucket => new TokenBucketRateLimiterWrapper(limit, (int)(limit / window.TotalSeconds)), + RateLimitAlgorithm.LeakyBucket => new LeakyBucketRateLimiterWrapper(limit, (int)(limit / window.TotalSeconds)), + _ => throw new ArgumentException("不支持的限流算法") + }; + } + + /// + /// 使用限流器执行操作 + /// + public static async Task ExecuteWithRateLimitAsync(IRateLimiter limiter, Func> action) + { + while (!limiter.TryAcquire()) + { + await Task.Delay(limiter.GetWaitTime()); + } + return await action(); + } + + /// + /// 使用限流器执行操作 + /// + public static async Task ExecuteWithRateLimitAsync(IRateLimiter limiter, Func action) + { + while (!limiter.TryAcquire()) + { + await Task.Delay(limiter.GetWaitTime()); + } + await action(); + } + } + + /// + /// 限流器接口 + /// + public interface IRateLimiter + { + bool TryAcquire(); + TimeSpan GetWaitTime(); + } + + internal class FixedWindowRateLimiterWrapper : IRateLimiter + { + private readonly FixedWindowRateLimiter _limiter; + public FixedWindowRateLimiterWrapper(int limit, TimeSpan window) => _limiter = new FixedWindowRateLimiter(limit, window); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class SlidingWindowRateLimiterWrapper : IRateLimiter + { + private readonly SlidingWindowRateLimiter _limiter; + public SlidingWindowRateLimiterWrapper(int limit, TimeSpan window) => _limiter = new SlidingWindowRateLimiter(limit, window); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class TokenBucketRateLimiterWrapper : IRateLimiter + { + private readonly TokenBucketRateLimiter _limiter; + public TokenBucketRateLimiterWrapper(int capacity, int refillRate) => _limiter = new TokenBucketRateLimiter(capacity, refillRate); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class LeakyBucketRateLimiterWrapper : IRateLimiter + { + private readonly LeakyBucketRateLimiter _limiter; + public LeakyBucketRateLimiterWrapper(int capacity, int leakRate) => _limiter = new LeakyBucketRateLimiter(capacity, leakRate); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } +} diff --git a/EasyTool.Core/ToolCategory/ResultUtil.cs b/EasyTool.Core/ToolCategory/ResultUtil.cs new file mode 100644 index 0000000..a2905c0 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ResultUtil.cs @@ -0,0 +1,295 @@ +using System; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 结果类型 + /// + public class Result + { + /// + /// 是否成功 + /// + public bool IsSuccess { get; protected set; } + + /// + /// 是否失败 + /// + public bool IsFailure => !IsSuccess; + + /// + /// 错误信息 + /// + public string? Error { get; protected set; } + + /// + /// 错误代码 + /// + public string? ErrorCode { get; protected set; } + + protected Result(bool isSuccess, string? error = null, string? errorCode = null) + { + IsSuccess = isSuccess; + Error = error; + ErrorCode = errorCode; + } + + /// + /// 创建成功结果 + /// + public static Result Success() => new Result(true); + + /// + /// 创建失败结果 + /// + public static Result Failure(string error, string? errorCode = null) => new Result(false, error, errorCode); + + /// + /// 创建成功结果(带值) + /// + public static Result Success(T value) => new Result(true, value); + + /// + /// 创建失败结果(带值) + /// + public static Result Failure(string error, string? errorCode = null) => new Result(false, default, error, errorCode); + + /// + /// 从异常创建失败结果 + /// + public static Result FromException(Exception ex) => Failure(ex.Message, ex.GetType().Name); + + /// + /// 从异常创建失败结果 + /// + public static Result FromException(Exception ex) => Failure(ex.Message, ex.GetType().Name); + + /// + /// 匹配处理 + /// + public void Match(Action onSuccess, Action onFailure) + { + if (IsSuccess) onSuccess(); + else onFailure(Error ?? ""); + } + + /// + /// 匹配处理并返回值 + /// + public T Match(Func onSuccess, Func onFailure) + { + return IsSuccess ? onSuccess() : onFailure(Error ?? ""); + } + + /// + /// 绑定下一个操作 + /// + public Result Bind(Func next) + { + return IsSuccess ? next() : Failure(Error!, ErrorCode); + } + + /// + /// 映射 + /// + public Result Map(Func mapper) + { + return IsSuccess ? Success(mapper()) : Failure(Error!, ErrorCode); + } + + /// + /// 异步匹配处理 + /// + public async Task MatchAsync(Func onSuccess, Action onFailure) + { + if (IsSuccess) await onSuccess(); + else onFailure(Error ?? ""); + } + } + + /// + /// 带值的结果类型 + /// + public class Result : Result + { + /// + /// 值 + /// + public T? Value { get; } + + internal Result(bool isSuccess, T? value, string? error = null, string? errorCode = null) + : base(isSuccess, error, errorCode) + { + Value = value; + } + + /// + /// 获取值,失败则抛出异常 + /// + public T GetValueOrThrow() + { + if (IsFailure) + throw new InvalidOperationException(Error ?? "操作失败"); + return Value!; + } + + /// + /// 获取值或默认值 + /// + public T GetValueOrDefault(T defaultValue) + { + return IsSuccess ? Value! : defaultValue; + } + + /// + /// 匹配处理 + /// + public void Match(Action onSuccess, Action onFailure) + { + if (IsSuccess) onSuccess(Value!); + else onFailure(Error ?? ""); + } + + /// + /// 匹配处理并返回值 + /// + public TResult Match(Func onSuccess, Func onFailure) + { + return IsSuccess ? onSuccess(Value!) : onFailure(Error ?? ""); + } + + /// + /// 绑定下一个操作 + /// + public Result Bind(Func> next) + { + return IsSuccess ? next(Value!) : Failure(Error!, ErrorCode); + } + + /// + /// 映射 + /// + public Result Map(Func mapper) + { + return IsSuccess ? Success(mapper(Value!)) : Failure(Error!, ErrorCode); + } + + /// + /// 异步绑定 + /// + public async Task> BindAsync(Func>> next) + { + return IsSuccess ? await next(Value!) : Failure(Error!, ErrorCode); + } + + /// + /// 异步映射 + /// + public async Task> MapAsync(Func> mapper) + { + return IsSuccess ? Success(await mapper(Value!)) : Failure(Error!, ErrorCode); + } + + /// + /// 隐式转换 + /// + public static implicit operator Result(T value) => Success(value); + } + + /// + /// 结果工具类 + /// + public static class ResultUtil + { + /// + /// 组合多个结果 + /// + public static Result Combine(params Result[] results) + { + foreach (var result in results) + { + if (result.IsFailure) + return result; + } + return Result.Success(); + } + + /// + /// 组合多个结果 + /// + public static Result Combine(params Result[] results) + { + var values = new T[results.Length]; + for (int i = 0; i < results.Length; i++) + { + if (results[i].IsFailure) + return Result.Failure(results[i].Error!, results[i].ErrorCode); + values[i] = results[i].Value!; + } + return Result.Success(values); + } + + /// + /// 尝试执行 + /// + public static Result Try(Action action) + { + try + { + action(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 尝试执行 + /// + public static Result Try(Func func) + { + try + { + return Result.Success(func()); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 异步尝试执行 + /// + public static async Task TryAsync(Func action) + { + try + { + await action(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 异步尝试执行 + /// + public static async Task> TryAsync(Func> func) + { + try + { + return Result.Success(await func()); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/RetryUtil.cs b/EasyTool.Core/ToolCategory/RetryUtil.cs index cacdaa2..5dc4dc5 100644 --- a/EasyTool.Core/ToolCategory/RetryUtil.cs +++ b/EasyTool.Core/ToolCategory/RetryUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -6,299 +7,391 @@ namespace EasyTool.ToolCategory { /// /// 重试工具类 - /// 提供可配置的重试机制 /// public static class RetryUtil { /// - /// 执行带重试的操作 + /// 重试执行操作 /// /// 要执行的操作 /// 最大重试次数 - /// 重试间隔(毫秒) - /// 是否使用指数退避 - public static void Execute(Action action, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + /// 重试间隔 + /// 重试时的回调 + public static void Execute( + Action action, + int maxRetries = 3, + TimeSpan? delay = null, + Action? onRetry = null) { - Execute(() => - { - action(); - return null; - }, maxRetries, delay, exponentialBackoff); - } + if (action == null) + throw new ArgumentNullException(nameof(action)); - /// - /// 执行带重试的函数 - /// - public static T Execute(Func func, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) - { - Exception lastException = null; + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); - for (int attempt = 0; attempt <= maxRetries; attempt++) + for (int i = 0; i <= maxRetries; i++) { try { - return func(); + action(); + return; } catch (Exception ex) { lastException = ex; - if (attempt < maxRetries) + if (i < maxRetries) { - int currentDelay = exponentialBackoff ? delay * (int)Math.Pow(2, attempt) : delay; - Thread.Sleep(currentDelay); + onRetry?.Invoke(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } } } } - throw new RetryException($"Operation failed after {maxRetries + 1} attempts", lastException); + throw lastException ?? new Exception("重试失败"); } /// - /// 异步执行带重试的操作 + /// 重试执行操作(带返回值) /// - public static async Task ExecuteAsync(Func action, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) + public static T Execute( + Func func, + int maxRetries = 3, + TimeSpan? delay = null, + Action? onRetry = null) { - await ExecuteAsync(async () => - { - await action(); - return null; - }, maxRetries, delay, exponentialBackoff); - } + if (func == null) + throw new ArgumentNullException(nameof(func)); - /// - /// 异步执行带重试的函数 - /// - public static async Task ExecuteAsync(Func> func, int maxRetries = 3, int delay = 1000, bool exponentialBackoff = true) - { - Exception lastException = null; + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); - for (int attempt = 0; attempt <= maxRetries; attempt++) + for (int i = 0; i <= maxRetries; i++) { try { - return await func(); + return func(); } catch (Exception ex) { lastException = ex; - if (attempt < maxRetries) + if (i < maxRetries) { - int currentDelay = exponentialBackoff ? delay * (int)Math.Pow(2, attempt) : delay; - await Task.Delay(currentDelay); + onRetry?.Invoke(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } } } } - throw new RetryException($"Operation failed after {maxRetries + 1} attempts", lastException); + throw lastException ?? new Exception("重试失败"); } /// - /// 创建重试策略 + /// 异步重试执行 /// - public static RetryPolicy CreatePolicy() + public static async Task ExecuteAsync( + Func action, + int maxRetries = 3, + TimeSpan? delay = null, + Func? onRetry = null, + CancellationToken cancellationToken = default) { - return new RetryPolicy(); - } - } + if (action == null) + throw new ArgumentNullException(nameof(action)); - /// - /// 重试策略 - /// - public class RetryPolicy - { - private int _maxRetries = 3; - private int _initialDelay = 1000; - private int _maxDelay = 60000; - private double _backoffMultiplier = 2.0; - private bool _useJitter = true; - private Type[] _retryOnExceptions = { typeof(Exception) }; - private Action _onRetry; + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); - /// - /// 设置最大重试次数 - /// - public RetryPolicy WithMaxRetries(int maxRetries) - { - _maxRetries = maxRetries; - return this; - } + for (int i = 0; i <= maxRetries; i++) + { + cancellationToken.ThrowIfCancellationRequested(); - /// - /// 设置初始延迟 - /// - public RetryPolicy WithInitialDelay(int milliseconds) - { - _initialDelay = milliseconds; - return this; - } + try + { + await action(); + return; + } + catch (Exception ex) + { + lastException = ex; - /// - /// 设置最大延迟 - /// - public RetryPolicy WithMaxDelay(int milliseconds) - { - _maxDelay = milliseconds; - return this; - } + if (i < maxRetries) + { + if (onRetry != null) + await onRetry(ex, i + 1); - /// - /// 设置退避倍数 - /// - public RetryPolicy WithBackoffMultiplier(double multiplier) - { - _backoffMultiplier = multiplier; - return this; - } + if (delayValue > TimeSpan.Zero) + { + await Task.Delay(delayValue, cancellationToken); + } + } + } + } - /// - /// 启用或禁用抖动 - /// - public RetryPolicy WithJitter(bool enable = true) - { - _useJitter = enable; - return this; + throw lastException ?? new Exception("重试失败"); } /// - /// 设置要重试的异常类型 + /// 异步重试执行(带返回值) /// - public RetryPolicy RetryOn() where TException : Exception + public static async Task ExecuteAsync( + Func> func, + int maxRetries = 3, + TimeSpan? delay = null, + Func? onRetry = null, + CancellationToken cancellationToken = default) { - var list = new System.Collections.Generic.List(_retryOnExceptions) { typeof(TException) }; - _retryOnExceptions = list.ToArray(); - return this; - } + if (func == null) + throw new ArgumentNullException(nameof(func)); - /// - /// 设置重试回调 - /// - public RetryPolicy OnRetry(Action onRetry) - { - _onRetry = onRetry; - return this; - } + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); - /// - /// 执行操作 - /// - public void Execute(Action action) - { - Execute(() => + for (int i = 0; i <= maxRetries; i++) { - action(); - return null; - }); + cancellationToken.ThrowIfCancellationRequested(); + + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + if (onRetry != null) + await onRetry(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + await Task.Delay(delayValue, cancellationToken); + } + } + } + } + + throw lastException ?? new Exception("重试失败"); } /// - /// 执行函数 + /// 指数退避重试 /// - public T Execute(Func func) + public static async Task ExecuteWithBackoffAsync( + Func action, + int maxRetries = 5, + TimeSpan? initialDelay = null, + double multiplier = 2.0, + TimeSpan? maxDelay = null, + CancellationToken cancellationToken = default) { - Exception lastException = null; + if (action == null) + throw new ArgumentNullException(nameof(action)); + + var delay = initialDelay ?? TimeSpan.FromSeconds(1); + var max = maxDelay ?? TimeSpan.FromMinutes(5); + Exception? lastException = null; - for (int attempt = 0; attempt <= _maxRetries; attempt++) + for (int i = 0; i <= maxRetries; i++) { + cancellationToken.ThrowIfCancellationRequested(); + try { - return func(); + await action(); + return; } catch (Exception ex) { lastException = ex; - if (!ShouldRetry(ex) || attempt >= _maxRetries) - break; + if (i < maxRetries) + { + var currentDelay = delay * Math.Pow(multiplier, i); + currentDelay = TimeSpan.FromTicks(Math.Min(currentDelay.Ticks, max.Ticks)); - TimeSpan delay = CalculateDelay(attempt); - _onRetry?.Invoke(ex, attempt + 1, delay); - Thread.Sleep(delay); + await Task.Delay(currentDelay, cancellationToken); + } } } - throw new RetryException($"Operation failed after {_maxRetries + 1} attempts", lastException); + throw lastException ?? new Exception("重试失败"); } /// - /// 异步执行操作 + /// 带条件判断的重试 /// - public async Task ExecuteAsync(Func action) + public static T Execute( + Func func, + Func shouldRetry, + int maxRetries = 3, + TimeSpan? delay = null) { - await ExecuteAsync(async () => + if (func == null) + throw new ArgumentNullException(nameof(func)); + if (shouldRetry == null) + throw new ArgumentNullException(nameof(shouldRetry)); + + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) { - await action(); - return null; - }); + var result = func(); + + if (!shouldRetry(result)) + return result; + + if (i < maxRetries && delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } + } + + return func(); } /// - /// 异步执行函数 + /// 使用重试策略执行 /// - public async Task ExecuteAsync(Func> func) + public static async Task ExecuteAsync( + Func> func, + RetryPolicy policy, + CancellationToken cancellationToken = default) { - Exception lastException = null; + if (func == null) + throw new ArgumentNullException(nameof(func)); + if (policy == null) + throw new ArgumentNullException(nameof(policy)); + + Exception? lastException = null; + var delay = policy.InitialDelay; - for (int attempt = 0; attempt <= _maxRetries; attempt++) + for (int i = 0; i <= policy.MaxRetries; i++) { + cancellationToken.ThrowIfCancellationRequested(); + try { return await func(); } catch (Exception ex) { - lastException = ex; + if (!policy.ShouldRetry(ex)) + throw; - if (!ShouldRetry(ex) || attempt >= _maxRetries) - break; + lastException = ex; - TimeSpan delay = CalculateDelay(attempt); - _onRetry?.Invoke(ex, attempt + 1, delay); - await Task.Delay(delay); + if (i < policy.MaxRetries) + { + await Task.Delay(delay, cancellationToken); + + // 计算下次延迟 + delay = policy.BackoffStrategy switch + { + BackoffStrategy.Linear => policy.InitialDelay, + BackoffStrategy.Exponential => TimeSpan.FromTicks(delay.Ticks * 2), + BackoffStrategy.Fixed => policy.InitialDelay, + _ => policy.InitialDelay + }; + + delay = TimeSpan.FromTicks(Math.Min(delay.Ticks, policy.MaxDelay.Ticks)); + } } } - throw new RetryException($"Operation failed after {_maxRetries + 1} attempts", lastException); + throw lastException ?? new Exception("重试失败"); } + } - private bool ShouldRetry(Exception ex) - { - foreach (var type in _retryOnExceptions) - { - if (type.IsAssignableFrom(ex.GetType())) - return true; - } - return false; - } + /// + /// 重试策略 + /// + public class RetryPolicy + { + /// + /// 最大重试次数 + /// + public int MaxRetries { get; set; } = 3; - private TimeSpan CalculateDelay(int attempt) - { - double delay = _initialDelay * Math.Pow(_backoffMultiplier, attempt); + /// + /// 初始延迟 + /// + public TimeSpan InitialDelay { get; set; } = TimeSpan.FromSeconds(1); - if (_useJitter) - { - // 添加随机抖动 (±20%) - var random = new Random(); - double jitter = 0.8 + random.NextDouble() * 0.4; - delay *= jitter; - } + /// + /// 最大延迟 + /// + public TimeSpan MaxDelay { get; set; } = TimeSpan.FromMinutes(1); - return TimeSpan.FromMilliseconds(Math.Min(delay, _maxDelay)); - } + /// + /// 退避策略 + /// + public BackoffStrategy BackoffStrategy { get; set; } = BackoffStrategy.Exponential; + + /// + /// 判断是否应该重试 + /// + public Func? ShouldRetry { get; set; } + + /// + /// 创建默认策略 + /// + public static RetryPolicy Default => new(); + + /// + /// 创建快速重试策略 + /// + public static RetryPolicy Fast => new() + { + MaxRetries = 3, + InitialDelay = TimeSpan.FromMilliseconds(100), + MaxDelay = TimeSpan.FromSeconds(5), + BackoffStrategy = BackoffStrategy.Linear + }; + + /// + /// 创建网络重试策略 + /// + public static RetryPolicy Network => new() + { + MaxRetries = 5, + InitialDelay = TimeSpan.FromSeconds(1), + MaxDelay = TimeSpan.FromSeconds(30), + BackoffStrategy = BackoffStrategy.Exponential, + ShouldRetry = ex => ex is TimeoutException || + ex is System.Net.WebException || + ex is System.Net.Http.HttpRequestException + }; } /// - /// 重试异常 + /// 退避策略 /// - public class RetryException : Exception + public enum BackoffStrategy { /// - /// 创建重试异常 + /// 固定延迟 /// - public RetryException(string message, Exception innerException) - : base(message, innerException) - { - } + Fixed, + + /// + /// 线性递增 + /// + Linear, + + /// + /// 指数递增 + /// + Exponential } } diff --git a/EasyTool.Core/ToolCategory/SecurityUtil.cs b/EasyTool.Core/ToolCategory/SecurityUtil.cs new file mode 100644 index 0000000..78bc2df --- /dev/null +++ b/EasyTool.Core/ToolCategory/SecurityUtil.cs @@ -0,0 +1,473 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ToolCategory +{ + /// + /// 安全工具类 + /// 提供XSS防护、SQL注入检测、HTML净化等功能 + /// + public static class SecurityUtil + { + #region HTML编码/解码 + + /// + /// HTML编码 + /// + public static string HtmlEncode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var sb = new StringBuilder(input.Length); + foreach (var c in input) + { + switch (c) + { + case '<': sb.Append("<"); break; + case '>': sb.Append(">"); break; + case '&': sb.Append("&"); break; + case '"': sb.Append("""); break; + case '\'': sb.Append("'"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + /// + /// HTML解码 + /// + public static string HtmlDecode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return input + .Replace("<", "<") + .Replace(">", ">") + .Replace("&", "&") + .Replace(""", "\"") + .Replace("'", "'") + .Replace(" ", " ") + .Replace("©", "©") + .Replace("®", "®") + .Replace("™", "™"); + } + + #endregion + + #region XSS防护 + + /// + /// 检测是否包含XSS攻击 + /// + public static bool ContainsXss(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + var patterns = new[] + { + @"]*>.*?", + @"javascript\s*:", + @"on\w+\s*=", + @" Regex.IsMatch(input, p, RegexOptions.IgnoreCase)); + } + + /// + /// 清理XSS攻击代码 + /// + public static string SanitizeXss(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + // 移除危险标签 + var sanitized = Regex.Replace(input, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"]*>", "", RegexOptions.IgnoreCase); + + // 移除危险属性 + sanitized = Regex.Replace(sanitized, @"javascript\s*:", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"vbscript\s*:", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"on\w+\s*=\s*[""'][^""']*[""']", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"expression\s*\([^)]*\)", "", RegexOptions.IgnoreCase); + + return sanitized; + } + + #endregion + + #region SQL注入检测 + + private static readonly string[] SqlKeywords = new[] + { + "select", "insert", "update", "delete", "drop", "create", "alter", "truncate", + "exec", "execute", "xp_", "sp_", "union", "join", "where", "from", "into", + "having", "group by", "order by", "--", "/*", "*/", ";", "declare", "cursor" + }; + + private static readonly string[] SqlFunctions = new[] + { + "char(", "nchar(", "varchar(", "nvarchar(", "cast(", "convert(", + "concat(", "substring(", "len(", "count(", "sum(", "avg(", "max(", "min(" + }; + + /// + /// 检测是否包含SQL注入 + /// + public static bool ContainsSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + var lowerInput = input.ToLower(); + + // 检查关键字 + foreach (var keyword in SqlKeywords) + { + if (lowerInput.Contains(keyword)) + { + // 检查是否是单词边界 + var pattern = $@"\b{Regex.Escape(keyword)}\b"; + if (Regex.IsMatch(lowerInput, pattern, RegexOptions.IgnoreCase)) + return true; + } + } + + // 检查函数 + foreach (var func in SqlFunctions) + { + if (lowerInput.Contains(func)) + return true; + } + + // 检查单引号 + if (input.Contains("'") && (input.Contains("''") || input.Contains("' or ") || input.Contains("' and "))) + return true; + + // 检查等号注入 + if (Regex.IsMatch(input, @"'?\s*=\s*'?", RegexOptions.IgnoreCase)) + return true; + + return false; + } + + /// + /// 清理SQL注入代码 + /// + public static string SanitizeSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + // 转义单引号 + var sanitized = input.Replace("'", "''"); + + return sanitized; + } + + #endregion + + #region 密码强度检测 + + /// + /// 检测密码强度 + /// + public static PasswordStrength CheckPasswordStrength(string password) + { + if (string.IsNullOrEmpty(password)) + return PasswordStrength.VeryWeak; + + var score = 0; + + // 长度评分 + if (password.Length >= 8) score++; + if (password.Length >= 12) score++; + if (password.Length >= 16) score++; + + // 包含小写字母 + if (Regex.IsMatch(password, @"[a-z]")) score++; + + // 包含大写字母 + if (Regex.IsMatch(password, @"[A-Z]")) score++; + + // 包含数字 + if (Regex.IsMatch(password, @"\d")) score++; + + // 包含特殊字符 + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?]")) score++; + + // 不包含连续字符 + if (!HasConsecutiveChars(password)) score++; + + // 不包含常见模式 + if (!HasCommonPatterns(password)) score++; + + return score switch + { + <= 2 => PasswordStrength.VeryWeak, + 3 => PasswordStrength.Weak, + 4 => PasswordStrength.Fair, + 5 => PasswordStrength.Good, + 6 => PasswordStrength.Strong, + _ => PasswordStrength.VeryStrong + }; + } + + private static bool HasConsecutiveChars(string input) + { + for (int i = 0; i < input.Length - 2; i++) + { + if (input[i] + 1 == input[i + 1] && input[i + 1] + 1 == input[i + 2]) + return true; + } + return false; + } + + private static bool HasCommonPatterns(string input) + { + var patterns = new[] { "123", "abc", "qwe", "password", "admin", "111", "000" }; + var lowerInput = input.ToLower(); + return patterns.Any(p => lowerInput.Contains(p)); + } + + #endregion + + #region HTML净化 + + private static readonly HashSet AllowedTags = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "p", "br", "b", "i", "u", "strong", "em", "span", "div", "a", "img", + "ul", "ol", "li", "table", "tr", "td", "th", "thead", "tbody", "h1", "h2", "h3", "h4", "h5", "h6", + "blockquote", "pre", "code", "hr" + }; + + private static readonly HashSet AllowedAttributes = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "href", "src", "alt", "title", "class", "id", "style", "target", "rel" + }; + + /// + /// 净化HTML + /// + public static string SanitizeHtml(string input, IEnumerable? allowedTags = null, IEnumerable? allowedAttributes = null) + { + if (string.IsNullOrEmpty(input)) + return input; + + var tags = allowedTags != null ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) : AllowedTags; + var attrs = allowedAttributes != null ? new HashSet(allowedAttributes, StringComparer.OrdinalIgnoreCase) : AllowedAttributes; + + // 移除注释 + var result = Regex.Replace(input, @"", "", RegexOptions.Singleline); + + // 处理标签 + result = Regex.Replace(result, @"<(/?)(\w+)([^>]*)>", match => + { + var isClosing = match.Groups[1].Value == "/"; + var tagName = match.Groups[2].Value.ToLower(); + var attributes = match.Groups[3].Value; + + if (!tags.Contains(tagName)) + return ""; + + if (isClosing) + return $""; + + // 过滤属性 + var filteredAttrs = FilterAttributes(attributes, attrs); + return $"<{tagName}{filteredAttrs}>"; + }, RegexOptions.Singleline); + + return result; + } + + private static string FilterAttributes(string attributes, HashSet allowedAttrs) + { + var result = new StringBuilder(); + var matches = Regex.Matches(attributes, @"(\w+)\s*=\s*[""']([^""']*)[""']"); + + foreach (Match match in matches) + { + var attrName = match.Groups[1].Value.ToLower(); + var attrValue = match.Groups[2].Value; + + if (allowedAttrs.Contains(attrName)) + { + // 检查危险属性值 + if (attrValue.ToLower().Contains("javascript:") || + attrValue.ToLower().Contains("vbscript:") || + attrValue.ToLower().Contains("expression(")) + continue; + + result.Append($" {attrName}=\"{attrValue}\""); + } + } + + return result.ToString(); + } + + #endregion + + #region 路径安全 + + /// + /// 检测路径是否安全 + /// + public static bool IsPathSafe(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + // 检查路径遍历攻击 + if (path.Contains("..") || path.Contains("~")) + return false; + + // 检查绝对路径 + if (Path.IsPathRooted(path)) + return false; + + // 检查无效字符 + var invalidChars = Path.GetInvalidFileNameChars(); + var fileName = Path.GetFileName(path); + if (fileName.IndexOfAny(invalidChars) >= 0) + return false; + + return true; + } + + /// + /// 安全路径拼接 + /// + public static string SafeCombine(string basePath, string relativePath) + { + if (string.IsNullOrEmpty(basePath) || string.IsNullOrEmpty(relativePath)) + throw new ArgumentException("路径不能为空"); + + if (!IsPathSafe(relativePath)) + throw new ArgumentException("相对路径不安全"); + + var fullPath = Path.GetFullPath(Path.Combine(basePath, relativePath)); + var normalizedBase = Path.GetFullPath(basePath); + + if (!fullPath.StartsWith(normalizedBase, StringComparison.OrdinalIgnoreCase)) + throw new ArgumentException("路径遍历攻击检测"); + + return fullPath; + } + + #endregion + + #region 敏感信息脱敏 + + /// + /// 手机号脱敏 + /// + public static string MaskPhone(string phone) + { + if (string.IsNullOrEmpty(phone) || phone.Length < 7) + return phone; + + return phone.Substring(0, 3) + "****" + phone.Substring(phone.Length - 4); + } + + /// + /// 身份证号脱敏 + /// + public static string MaskIdCard(string idCard) + { + if (string.IsNullOrEmpty(idCard) || idCard.Length < 8) + return idCard; + + return idCard.Substring(0, 4) + "**********" + idCard.Substring(idCard.Length - 4); + } + + /// + /// 邮箱脱敏 + /// + public static string MaskEmail(string email) + { + if (string.IsNullOrEmpty(email) || !email.Contains("@")) + return email; + + var parts = email.Split('@'); + var name = parts[0]; + var domain = parts[1]; + + if (name.Length <= 2) + return name[0] + "***@" + domain; + + return name.Substring(0, 2) + "***@" + domain; + } + + /// + /// 银行卡号脱敏 + /// + public static string MaskBankCard(string cardNumber) + { + if (string.IsNullOrEmpty(cardNumber) || cardNumber.Length < 8) + return cardNumber; + + return cardNumber.Substring(0, 4) + " **** **** " + cardNumber.Substring(cardNumber.Length - 4); + } + + #endregion + } + + /// + /// 密码强度等级 + /// + public enum PasswordStrength + { + /// + /// 非常弱 + /// + VeryWeak, + + /// + /// 弱 + /// + Weak, + + /// + /// 一般 + /// + Fair, + + /// + /// 好 + /// + Good, + + /// + /// 强 + /// + Strong, + + /// + /// 非常强 + /// + VeryStrong + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/Singleton.cs b/EasyTool.Core/ToolCategory/Singleton.cs new file mode 100644 index 0000000..0902c45 --- /dev/null +++ b/EasyTool.Core/ToolCategory/Singleton.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 单例模式工具类 + /// + public static class Singleton + { + /// + /// 获取单例实例 + /// + public static T GetInstance() where T : class, new() + { + return Singleton.Instance; + } + + /// + /// 获取单例实例(带初始化参数) + /// + public static T GetInstance(Func factory) where T : class + { + return Singleton.GetInstance(factory); + } + } + + /// + /// 泛型单例 + /// + public static class Singleton where T : class + { + private static readonly Lazy _instance = new(() => + { + var type = typeof(T); + var constructor = type.GetConstructor(Type.EmptyTypes); + if (constructor == null) + throw new InvalidOperationException($"类型 {type.Name} 必须有公共无参构造函数"); + return (T)constructor.Invoke(null); + }); + + private static T? _customInstance; + private static readonly object _lock = new(); + + /// + /// 单例实例 + /// + public static T Instance => _customInstance ?? _instance.Value; + + /// + /// 获取实例(使用自定义工厂) + /// + public static T GetInstance(Func factory) + { + if (_customInstance != null) + return _customInstance; + + lock (_lock) + { + if (_customInstance != null) + return _customInstance; + + _customInstance = factory(); + return _customInstance; + } + } + + /// + /// 重置实例 + /// + public static void Reset() + { + lock (_lock) + { + _customInstance = null; + } + } + } + + /// + /// 单例基类 + /// + /// 派生类类型 + public abstract class SingletonBase where T : SingletonBase + { + private static readonly Lazy _instance = new(() => + { + var type = typeof(T); + var constructor = type.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public, null, Type.EmptyTypes, null); + if (constructor == null) + throw new InvalidOperationException($"类型 {type.Name} 必须有公共或受保护的无参构造函数"); + return (T)constructor.Invoke(null); + }); + + /// + /// 单例实例 + /// + public static T Instance => _instance.Value; + + /// + /// 受保护的构造函数 + /// + protected SingletonBase() { } + } +} diff --git a/EasyTool.Core/ToolCategory/StateMachine.cs b/EasyTool.Core/ToolCategory/StateMachine.cs new file mode 100644 index 0000000..803d8f1 --- /dev/null +++ b/EasyTool.Core/ToolCategory/StateMachine.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 状态机工具类 + /// + /// 状态类型 + /// 触发器类型 + public class StateMachine where TState : notnull where TTrigger : notnull + { + private readonly Dictionary _configurations = new(); + private readonly object _lock = new(); + + /// + /// 当前状态 + /// + public TState CurrentState { get; private set; } + + /// + /// 状态变更事件 + /// + public event EventHandler? StateChanged; + + /// + /// 状态转换事件 + /// + public event EventHandler? Transitioning; + + /// + /// 创建状态机 + /// + public StateMachine(TState initialState) + { + CurrentState = initialState; + } + + /// + /// 配置状态 + /// + public StateConfiguration Configure(TState state) + { + if (!_configurations.TryGetValue(state, out var config)) + { + config = new StateConfiguration(state); + _configurations[state] = config; + } + return config; + } + + /// + /// 触发转换 + /// + public void Fire(TTrigger trigger) + { + lock (_lock) + { + if (!_configurations.TryGetValue(CurrentState, out var config)) + throw new InvalidOperationException($"状态 {CurrentState} 未配置"); + + if (!config.Transitions.TryGetValue(trigger, out var transition)) + throw new InvalidOperationException($"状态 {CurrentState} 不支持触发器 {trigger}"); + + var args = new StateTransitionEventArgs(CurrentState, transition.Destination, trigger); + + Transitioning?.Invoke(this, args); + + config.ExitAction?.Invoke(); + transition.Action?.Invoke(); + + var previousState = CurrentState; + CurrentState = transition.Destination; + + if (_configurations.TryGetValue(CurrentState, out var newConfig)) + { + newConfig.EntryAction?.Invoke(); + } + + StateChanged?.Invoke(this, args); + } + } + + /// + /// 尝试触发转换 + /// + public bool TryFire(TTrigger trigger) + { + try + { + Fire(trigger); + return true; + } + catch + { + return false; + } + } + + /// + /// 是否可以触发 + /// + public bool CanFire(TTrigger trigger) + { + lock (_lock) + { + if (!_configurations.TryGetValue(CurrentState, out var config)) + return false; + + return config.Transitions.ContainsKey(trigger); + } + } + + /// + /// 获取当前状态可用的触发器 + /// + public IEnumerable GetPermittedTriggers() + { + lock (_lock) + { + if (_configurations.TryGetValue(CurrentState, out var config)) + return config.Transitions.Keys; + return Array.Empty(); + } + } + + /// + /// 状态配置 + /// + public class StateConfiguration + { + private readonly TState _state; + internal readonly Dictionary Transitions = new(); + internal Action? EntryAction; + internal Action? ExitAction; + + internal StateConfiguration(TState state) + { + _state = state; + } + + /// + /// 配置进入动作 + /// + public StateConfiguration OnEntry(Action action) + { + EntryAction = action; + return this; + } + + /// + /// 配置退出动作 + /// + public StateConfiguration OnExit(Action action) + { + ExitAction = action; + return this; + } + + /// + /// 配置转换 + /// + public StateConfiguration Permit(TTrigger trigger, TState destination) + { + Transitions[trigger] = new Transition(destination, null); + return this; + } + + /// + /// 配置转换(带动作) + /// + public StateConfiguration Permit(TTrigger trigger, TState destination, Action action) + { + Transitions[trigger] = new Transition(destination, action); + return this; + } + + /// + /// 忽略触发器 + /// + public StateConfiguration Ignore(TTrigger trigger) + { + Transitions[trigger] = new Transition(_state, null); + return this; + } + } + + internal class Transition + { + public TState Destination { get; } + public Action? Action { get; } + + public Transition(TState destination, Action? action) + { + Destination = destination; + Action = action; + } + } + } + + /// + /// 状态转换事件参数 + /// + public class StateTransitionEventArgs : EventArgs + { + /// + /// 源状态 + /// + public object SourceState { get; } + + /// + /// 目标状态 + /// + public object DestinationState { get; } + + /// + /// 触发器 + /// + public object Trigger { get; } + + internal StateTransitionEventArgs(object source, object destination, object trigger) + { + SourceState = source; + DestinationState = destination; + Trigger = trigger; + } + } +} diff --git a/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs b/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs new file mode 100644 index 0000000..0368def --- /dev/null +++ b/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs @@ -0,0 +1,396 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 后台任务调度器 + /// + public class BackgroundTaskScheduler : IDisposable + { + private readonly List _tasks = new(); + private readonly object _lock = new(); + private readonly Timer _timer; + private bool _disposed; + + /// + /// 创建后台任务调度器 + /// + /// 检查间隔(毫秒) + public BackgroundTaskScheduler(int checkInterval = 1000) + { + _timer = new Timer(CheckTasks, null, checkInterval, checkInterval); + } + + /// + /// 添加定时任务 + /// + public string Schedule(string name, Action action, DateTime executeAt) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + ExecuteAt = executeAt, + Type = ScheduledTaskType.Once + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 添加延迟任务 + /// + public string Schedule(string name, Action action, TimeSpan delay) + { + return Schedule(name, action, DateTime.UtcNow.Add(delay)); + } + + /// + /// 添加周期性任务 + /// + public string ScheduleRecurring(string name, Action action, TimeSpan interval, DateTime? startAt = null) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + ExecuteAt = startAt ?? DateTime.UtcNow, + Interval = interval, + Type = ScheduledTaskType.Recurring + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 添加Cron任务 + /// + public string ScheduleCron(string name, Action action, string cronExpression) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + CronExpression = cronExpression, + Type = ScheduledTaskType.Cron, + ExecuteAt = GetNextCronTime(cronExpression) + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 取消任务 + /// + public bool Cancel(string taskId) + { + lock (_lock) + { + var task = _tasks.Find(t => t.Id == taskId); + if (task != null) + { + task.IsCancelled = true; + _tasks.Remove(task); + return true; + } + } + return false; + } + + /// + /// 获取所有任务 + /// + public List GetAllTasks() + { + lock (_lock) + { + return _tasks.ConvertAll(t => new ScheduledTaskInfo + { + Id = t.Id, + Name = t.Name, + Type = t.Type, + ExecuteAt = t.ExecuteAt, + Interval = t.Interval, + IsCancelled = t.IsCancelled, + LastExecution = t.LastExecution + }); + } + } + + private void CheckTasks(object? state) + { + List tasksToExecute; + + lock (_lock) + { + var now = DateTime.UtcNow; + tasksToExecute = _tasks.FindAll(t => !t.IsCancelled && t.ExecuteAt <= now); + } + + foreach (var task in tasksToExecute) + { + Task.Run(() => + { + try + { + task.Action(); + task.LastExecution = DateTime.UtcNow; + } + catch + { + // 忽略异常 + } + }); + + // 更新下次执行时间 + lock (_lock) + { + if (task.Type == ScheduledTaskType.Once) + { + _tasks.Remove(task); + } + else if (task.Type == ScheduledTaskType.Recurring) + { + task.ExecuteAt = DateTime.UtcNow.Add(task.Interval); + } + else if (task.Type == ScheduledTaskType.Cron) + { + task.ExecuteAt = GetNextCronTime(task.CronExpression); + } + } + } + } + + private DateTime GetNextCronTime(string cronExpression) + { + // 简化实现,实际应使用CronUtil + var parts = cronExpression.Split(' '); + if (parts.Length >= 1 && int.TryParse(parts[0], out var minute)) + { + var now = DateTime.UtcNow; + var next = new DateTime(now.Year, now.Month, now.Day, now.Hour, minute, 0); + if (next <= now) + next = next.AddHours(1); + return next; + } + return DateTime.UtcNow.AddMinutes(1); + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _timer.Dispose(); + lock (_lock) + { + _tasks.Clear(); + } + } + } + } + + internal class ScheduledTask + { + public string Id { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public Action Action { get; set; } = () => { }; + public DateTime ExecuteAt { get; set; } + public TimeSpan Interval { get; set; } + public string? CronExpression { get; set; } + public ScheduledTaskType Type { get; set; } + public bool IsCancelled { get; set; } + public DateTime? LastExecution { get; set; } + } + + /// + /// 任务类型 + /// + public enum ScheduledTaskType + { + /// + /// 单次执行 + /// + Once, + + /// + /// 周期执行 + /// + Recurring, + + /// + /// Cron表达式 + /// + Cron + } + + /// + /// 计划任务信息 + /// + public class ScheduledTaskInfo + { + /// + /// 任务ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 任务名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 任务类型 + /// + public ScheduledTaskType Type { get; set; } + + /// + /// 执行时间 + /// + public DateTime ExecuteAt { get; set; } + + /// + /// 执行间隔 + /// + public TimeSpan Interval { get; set; } + + /// + /// 是否已取消 + /// + public bool IsCancelled { get; set; } + + /// + /// 最后执行时间 + /// + public DateTime? LastExecution { get; set; } + } + + /// + /// 任务队列 + /// + /// 任务数据类型 + public class TaskQueue : IDisposable + { + private readonly System.Collections.Concurrent.ConcurrentQueue _queue = new(); + private readonly SemaphoreSlim _signal = new(0); + private readonly CancellationTokenSource _cts = new(); + private readonly List _workers = new(); + private readonly Func _processor; + private readonly int _maxDegreeOfParallelism; + private bool _disposed; + + /// + /// 队列数量 + /// + public int Count => _queue.Count; + + /// + /// 创建任务队列 + /// + /// 处理函数 + /// 最大并行度 + public TaskQueue(Func processor, int maxDegreeOfParallelism = 4) + { + _processor = processor ?? throw new ArgumentNullException(nameof(processor)); + _maxDegreeOfParallelism = maxDegreeOfParallelism; + + for (int i = 0; i < maxDegreeOfParallelism; i++) + { + _workers.Add(Task.Run(WorkerAsync)); + } + } + + /// + /// 入队 + /// + public void Enqueue(T item) + { + _queue.Enqueue(item); + _signal.Release(); + } + + /// + /// 批量入队 + /// + public void EnqueueRange(IEnumerable items) + { + foreach (var item in items) + { + Enqueue(item); + } + } + + private async Task WorkerAsync() + { + while (!_cts.Token.IsCancellationRequested) + { + await _signal.WaitAsync(_cts.Token); + + if (_queue.TryDequeue(out var item)) + { + try + { + await _processor(item); + } + catch + { + // 忽略异常 + } + } + } + } + + /// + /// 等待所有任务完成 + /// + public async Task WaitForCompletionAsync() + { + while (_queue.Count > 0 || _signal.CurrentCount > 0) + { + await Task.Delay(100); + } + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _cts.Cancel(); + + try + { + Task.WaitAll(_workers.ToArray(), TimeSpan.FromSeconds(5)); + } + catch + { + // 忽略 + } + + _cts.Dispose(); + _signal.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/ValidatorUtil.cs b/EasyTool.Core/ToolCategory/ValidatorUtil.cs index b66e81d..c3f7e43 100644 --- a/EasyTool.Core/ToolCategory/ValidatorUtil.cs +++ b/EasyTool.Core/ToolCategory/ValidatorUtil.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -11,686 +12,452 @@ namespace EasyTool.ToolCategory public class ValidationResult { /// - /// 是否验证通过 + /// 是否有效 /// public bool IsValid { get; set; } /// - /// 错误信息列表 + /// 错误消息列表 /// public List Errors { get; set; } = new(); /// - /// 创建成功结果 + /// 错误字段列表 /// - public static ValidationResult Success => new() { IsValid = true }; + public List ErrorFields { get; set; } = new(); - /// - /// 创建失败结果 - /// - public static ValidationResult Fail(params string[] errors) => new() - { - IsValid = false, - Errors = errors.ToList() - }; - - /// - /// 合并多个验证结果 - /// - public static ValidationResult Combine(params ValidationResult[] results) - { - var combined = new ValidationResult { IsValid = true }; - - foreach (var result in results) - { - if (!result.IsValid) - { - combined.IsValid = false; - combined.Errors.AddRange(result.Errors); - } - } - - return combined; - } + public static ValidationResult Success() => new() { IsValid = true }; + public static ValidationResult Failure(params string[] errors) => new() { IsValid = false, Errors = errors.ToList() }; } /// - /// 验证规则构建器 + /// 验证工具类 + /// 提供常用的数据验证功能 /// - /// 值类型 - public class ValidatorBuilder + public static class ValidatorUtil { - private readonly List> _rules = new(); - protected readonly string _fieldName; + #region 字符串验证 - public ValidatorBuilder(string fieldName = "value") + /// + /// 检查字符串是否为空或空白 + /// + public static bool IsNullOrWhiteSpace(string? value) { - _fieldName = fieldName; + return string.IsNullOrWhiteSpace(value); } /// - /// 添加自定义验证规则 + /// 检查字符串是否为空 /// - public ValidatorBuilder AddRule(Func rule, string errorMessage) + public static bool IsNullOrEmpty(string? value) { - _rules.Add(value => rule(value) - ? ValidationResult.Success - : ValidationResult.Fail(errorMessage)); - return this; + return string.IsNullOrEmpty(value); } /// - /// 添加自定义验证规则 + /// 检查字符串是否不为空 /// - public ValidatorBuilder AddRule(Func rule) + public static bool IsNotNullOrEmpty(string? value) { - _rules.Add(rule); - return this; + return !string.IsNullOrEmpty(value); } - #region 通用规则 - /// - /// 不能为默认值 + /// 检查字符串长度是否在指定范围内 /// - public ValidatorBuilder NotDefault(string? message = null) + public static bool IsLengthBetween(string? value, int minLength, int maxLength) { - _rules.Add(value => !EqualityComparer.Default.Equals(value, default!) - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}不能为默认值")); - return this; + if (value == null) + return minLength <= 0; + + return value.Length >= minLength && value.Length <= maxLength; } /// - /// 满足条件 + /// 检查字符串长度是否等于指定值 /// - public ValidatorBuilder Must(Func predicate, string? message = null) + public static bool IsLength(string? value, int length) { - _rules.Add(value => predicate(value) - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}不满足条件")); - return this; + return value?.Length == length; } /// - /// 枚举值验证 + /// 检查字符串是否只包含数字 /// - public ValidatorBuilder IsEnum(string? message = null) + public static bool IsNumeric(string? value) { - _rules.Add(value => - { - if (value == null) - return ValidationResult.Fail(message ?? $"{_fieldName}不能为空"); - - var type = typeof(T); - if (!type.IsEnum && (!Nullable.GetUnderlyingType(type)?.IsEnum ?? true)) - return ValidationResult.Fail($"{_fieldName}不是枚举类型"); + if (string.IsNullOrEmpty(value)) + return false; - var enumType = Nullable.GetUnderlyingType(type) ?? type; - return Enum.IsDefined(enumType, value) - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}不是有效的枚举值"); - }); - return this; + return value.All(char.IsDigit); } - #endregion - - #region 数值规则 - /// - /// 大于指定值 + /// 检查字符串是否只包含字母 /// - public ValidatorBuilder GreaterThan(T compareValue, string? message = null) + public static bool IsAlpha(string? value) { - _rules.Add(value => - { - if (value is IComparable comparable) - { - return comparable.CompareTo(compareValue) > 0 - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}必须大于 {compareValue}"); - } - return ValidationResult.Fail($"{_fieldName}类型不可比较"); - }); - return this; + if (string.IsNullOrEmpty(value)) + return false; + + return value.All(char.IsLetter); } /// - /// 大于等于指定值 + /// 检查字符串是否只包含字母和数字 /// - public ValidatorBuilder GreaterThanOrEqual(T compareValue, string? message = null) + public static bool IsAlphanumeric(string? value) { - _rules.Add(value => - { - if (value is IComparable comparable) - { - return comparable.CompareTo(compareValue) >= 0 - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}必须大于等于 {compareValue}"); - } - return ValidationResult.Fail($"{_fieldName}类型不可比较"); - }); - return this; + if (string.IsNullOrEmpty(value)) + return false; + + return value.All(c => char.IsLetterOrDigit(c)); } /// - /// 小于指定值 + /// 检查字符串是否匹配正则表达式 /// - public ValidatorBuilder LessThan(T compareValue, string? message = null) + public static bool IsMatch(string? value, string pattern) { - _rules.Add(value => - { - if (value is IComparable comparable) - { - return comparable.CompareTo(compareValue) < 0 - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}必须小于 {compareValue}"); - } - return ValidationResult.Fail($"{_fieldName}类型不可比较"); - }); - return this; + if (string.IsNullOrEmpty(value)) + return false; + + return Regex.IsMatch(value, pattern); } /// - /// 小于等于指定值 + /// 检查字符串是否在指定值列表中 /// - public ValidatorBuilder LessThanOrEqual(T compareValue, string? message = null) + public static bool IsIn(string? value, params string[] allowedValues) { - _rules.Add(value => - { - if (value is IComparable comparable) - { - return comparable.CompareTo(compareValue) <= 0 - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}必须小于等于 {compareValue}"); - } - return ValidationResult.Fail($"{_fieldName}类型不可比较"); - }); - return this; + return allowedValues.Contains(value); } /// - /// 在指定范围内 + /// 检查字符串是否以指定前缀开头 /// - public ValidatorBuilder InRange(T min, T max, string? message = null) + public static bool StartsWith(string? value, string prefix, StringComparison comparison = StringComparison.Ordinal) { - _rules.Add(value => - { - if (value is IComparable comparable) - { - var valid = comparable.CompareTo(min) >= 0 && comparable.CompareTo(max) <= 0; - return valid - ? ValidationResult.Success - : ValidationResult.Fail(message ?? $"{_fieldName}必须在 {min} 和 {max} 之间"); - } - return ValidationResult.Fail($"{_fieldName}类型不可比较"); - }); - return this; - } - - #endregion + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(prefix)) + return false; - #region 构建验证器 + return value.StartsWith(prefix, comparison); + } /// - /// 构建验证器 + /// 检查字符串是否以指定后缀结尾 /// - public Func Build() + public static bool EndsWith(string? value, string suffix, StringComparison comparison = StringComparison.Ordinal) { - var rules = _rules.ToList(); - return value => - { - var result = ValidationResult.Success; - foreach (var rule in rules) - { - var ruleResult = rule(value); - if (!ruleResult.IsValid) - { - result.IsValid = false; - result.Errors.AddRange(ruleResult.Errors); - } - } - return result; - }; + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(suffix)) + return false; + + return value.EndsWith(suffix, comparison); } /// - /// 验证值 + /// 检查字符串是否包含指定子串 /// - public ValidationResult Validate(T value) + public static bool Contains(string? value, string substring, StringComparison comparison = StringComparison.Ordinal) { - return Build()(value); + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(substring)) + return false; + + return value.Contains(substring, comparison); } #endregion - } - - /// - /// 字符串验证规则构建器 - /// - public class StringValidatorBuilder : ValidatorBuilder - { - public StringValidatorBuilder(string fieldName = "value") : base(fieldName) { } - /// - /// 不能为空或空白 - /// - public StringValidatorBuilder NotEmpty(string? message = null) - { - AddRule(value => !string.IsNullOrWhiteSpace(value), - message ?? $"{_fieldName}不能为空"); - return this; - } + #region 数字验证 /// - /// 最小长度 + /// 检查值是否在指定范围内 /// - public StringValidatorBuilder MinLength(int minLength, string? message = null) + public static bool IsBetween(T value, T min, T max) where T : IComparable { - AddRule(value => value != null && value.Length >= minLength, - message ?? $"{_fieldName}长度不能小于 {minLength}"); - return this; + return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0; } /// - /// 最大长度 + /// 检查值是否大于指定值 /// - public StringValidatorBuilder MaxLength(int maxLength, string? message = null) + public static bool IsGreaterThan(T value, T compare) where T : IComparable { - AddRule(value => value == null || value.Length <= maxLength, - message ?? $"{_fieldName}长度不能超过 {maxLength}"); - return this; + return value.CompareTo(compare) > 0; } /// - /// 长度范围 + /// 检查值是否大于等于指定值 /// - public StringValidatorBuilder Length(int minLength, int maxLength, string? message = null) + public static bool IsGreaterThanOrEqual(T value, T compare) where T : IComparable { - AddRule(value => value != null && value.Length >= minLength && value.Length <= maxLength, - message ?? $"{_fieldName}长度必须在 {minLength} 到 {maxLength} 之间"); - return this; + return value.CompareTo(compare) >= 0; } /// - /// 匹配正则表达式 + /// 检查值是否小于指定值 /// - public StringValidatorBuilder Matches(string pattern, string? message = null) + public static bool IsLessThan(T value, T compare) where T : IComparable { - AddRule(value => value != null && Regex.IsMatch(value, pattern), - message ?? $"{_fieldName}格式不正确"); - return this; + return value.CompareTo(compare) < 0; } /// - /// 匹配正则表达式 + /// 检查值是否小于等于指定值 /// - public StringValidatorBuilder Matches(Regex regex, string? message = null) + public static bool IsLessThanOrEqual(T value, T compare) where T : IComparable { - AddRule(value => value != null && regex.IsMatch(value), - message ?? $"{_fieldName}格式不正确"); - return this; + return value.CompareTo(compare) <= 0; } /// - /// 邮箱格式 + /// 检查是否为正数 /// - public StringValidatorBuilder Email(string? message = null) + public static bool IsPositive(T value) where T : IComparable { - const string emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$"; - return Matches(emailPattern, message ?? $"{_fieldName}不是有效的邮箱地址"); + return value.CompareTo(default!) > 0; } /// - /// 手机号格式(中国大陆) + /// 检查是否为负数 /// - public StringValidatorBuilder Phone(string? message = null) + public static bool IsNegative(T value) where T : IComparable { - const string phonePattern = @"^1[3-9]\d{9}$"; - return Matches(phonePattern, message ?? $"{_fieldName}不是有效的手机号"); + return value.CompareTo(default!) < 0; } /// - /// 身份证号格式(中国大陆) + /// 检查是否为零 /// - public StringValidatorBuilder IdCard(string? message = null) + public static bool IsZero(T value) where T : IComparable { - const string idCardPattern = @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"; - return Matches(idCardPattern, message ?? $"{_fieldName}不是有效的身份证号"); + return value.CompareTo(default!) == 0; } /// - /// URL格式 + /// 检查是否为偶数 /// - public StringValidatorBuilder Url(string? message = null) + public static bool IsEven(int value) { - const string urlPattern = @"^https?://[^\s/$.?#].[^\s]*$"; - return Matches(urlPattern, message ?? $"{_fieldName}不是有效的URL"); + return value % 2 == 0; } /// - /// 纯数字 + /// 检查是否为奇数 /// - public StringValidatorBuilder Numeric(string? message = null) + public static bool IsOdd(int value) { - return Matches(@"^\d+$", message ?? $"{_fieldName}必须为纯数字"); + return value % 2 != 0; } - /// - /// 纯字母 - /// - public StringValidatorBuilder Alpha(string? message = null) - { - return Matches(@"^[a-zA-Z]+$", message ?? $"{_fieldName}必须为纯字母"); - } + #endregion - /// - /// 字母数字 - /// - public StringValidatorBuilder Alphanumeric(string? message = null) - { - return Matches(@"^[a-zA-Z0-9]+$", message ?? $"{_fieldName}必须为字母或数字"); - } + #region 集合验证 /// - /// 包含数字 + /// 检查集合是否为空 /// - public StringValidatorBuilder ContainsDigit(string? message = null) + public static bool IsEmpty(IEnumerable? collection) { - return Matches(@"\d", message ?? $"{_fieldName}必须包含数字"); - } + if (collection == null) + return true; - /// - /// 包含小写字母 - /// - public StringValidatorBuilder ContainsLower(string? message = null) - { - return Matches(@"[a-z]", message ?? $"{_fieldName}必须包含小写字母"); - } + if (collection is ICollection col) + return col.Count == 0; - /// - /// 包含大写字母 - /// - public StringValidatorBuilder ContainsUpper(string? message = null) - { - return Matches(@"[A-Z]", message ?? $"{_fieldName}必须包含大写字母"); + return !collection.Cast().Any(); } /// - /// 包含特殊字符 + /// 检查集合是否不为空 /// - public StringValidatorBuilder ContainsSpecial(string? message = null) + public static bool IsNotEmpty(IEnumerable? collection) { - return Matches(@"[!@#$%^&*(),.?"":{}|<>]", message ?? $"{_fieldName}必须包含特殊字符"); + return !IsEmpty(collection); } /// - /// 密码强度验证 + /// 检查集合元素数量是否在指定范围内 /// - /// 最小长度 - /// 需要数字 - /// 需要小写字母 - /// 需要大写字母 - /// 需要特殊字符 - /// 错误消息 - public StringValidatorBuilder Password( - int minLength = 8, - bool requireDigit = true, - bool requireLower = true, - bool requireUpper = true, - bool requireSpecial = false, - string? message = null) + public static bool IsCountBetween(IEnumerable? collection, int minCount, int maxCount) { - MinLength(minLength); - - if (requireDigit) ContainsDigit(); - if (requireLower) ContainsLower(); - if (requireUpper) ContainsUpper(); - if (requireSpecial) ContainsSpecial(); + if (collection == null) + return minCount <= 0; - if (!string.IsNullOrEmpty(message)) + int count; + if (collection is ICollection col) { - AddRule(_ => false, message); + count = col.Count; + } + else + { + count = collection.Cast().Count(); } - return this; + return count >= minCount && count <= maxCount; } - } - - /// - /// 集合验证规则构建器 - /// - public class CollectionValidatorBuilder : ValidatorBuilder?> - { - public CollectionValidatorBuilder(string fieldName = "collection") : base(fieldName) { } /// - /// 不能为空集合 + /// 检查集合是否包含指定元素 /// - public CollectionValidatorBuilder NotEmpty(string? message = null) + public static bool Contains(IEnumerable? collection, T item) { - AddRule(value => value != null && value.Any(), - message ?? $"{_fieldName}不能为空"); - return this; - } + if (collection == null) + return false; - /// - /// 最小元素数量 - /// - public CollectionValidatorBuilder MinCount(int minCount, string? message = null) - { - AddRule(value => value != null && value.Count() >= minCount, - message ?? $"{_fieldName}元素数量不能少于 {minCount}"); - return this; + return collection.Contains(item); } /// - /// 最大元素数量 + /// 检查集合是否包含所有指定元素 /// - public CollectionValidatorBuilder MaxCount(int maxCount, string? message = null) + public static bool ContainsAll(IEnumerable? collection, params T[] items) { - AddRule(value => value == null || value.Count() <= maxCount, - message ?? $"{_fieldName}元素数量不能超过 {maxCount}"); - return this; - } + if (collection == null || items == null) + return false; - /// - /// 元素数量范围 - /// - public CollectionValidatorBuilder Count(int minCount, int maxCount, string? message = null) - { - AddRule(value => - { - if (value == null) return false; - var count = value.Count(); - return count >= minCount && count <= maxCount; - }, message ?? $"{_fieldName}元素数量必须在 {minCount} 到 {maxCount} 之间"); - return this; + return items.All(item => collection.Contains(item)); } /// - /// 所有元素满足条件 + /// 检查集合是否包含任一指定元素 /// - public CollectionValidatorBuilder All(Func predicate, string? message = null) + public static bool ContainsAny(IEnumerable? collection, params T[] items) { - AddRule(value => value == null || value.All(predicate), - message ?? $"{_fieldName}中存在不满足条件的元素"); - return this; - } + if (collection == null || items == null) + return false; - /// - /// 至少一个元素满足条件 - /// - public CollectionValidatorBuilder Any(Func predicate, string? message = null) - { - AddRule(value => value != null && value.Any(predicate), - message ?? $"{_fieldName}中没有满足条件的元素"); - return this; + return items.Any(item => collection.Contains(item)); } + #endregion + + #region 日期验证 + /// - /// 不包含重复元素 + /// 检查日期是否在指定范围内 /// - public CollectionValidatorBuilder Distinct(string? message = null) + public static bool IsBetween(DateTime value, DateTime min, DateTime max) { - AddRule(value => - { - if (value == null) return true; - var list = value.ToList(); - return list.Count == list.Distinct().Count(); - }, message ?? $"{_fieldName}包含重复元素"); - return this; + return value >= min && value <= max; } /// - /// 包含指定元素 + /// 检查是否为工作日(周一至周五) /// - public CollectionValidatorBuilder Contains(T item, string? message = null) + public static bool IsWeekday(DateTime value) { - AddRule(value => value != null && value.Contains(item), - message ?? $"{_fieldName}不包含指定元素"); - return this; + return value.DayOfWeek != DayOfWeek.Saturday && value.DayOfWeek != DayOfWeek.Sunday; } - } - - /// - /// 通用验证工具类 - /// - public static class ValidatorUtil - { /// - /// 创建字符串验证器 + /// 检查是否为周末 /// - public static StringValidatorBuilder ForString(string fieldName = "value") + public static bool IsWeekend(DateTime value) { - return new StringValidatorBuilder(fieldName); + return value.DayOfWeek == DayOfWeek.Saturday || value.DayOfWeek == DayOfWeek.Sunday; } /// - /// 创建数值验证器 + /// 检查是否为今天 /// - public static ValidatorBuilder ForNumber(string fieldName = "value") where T : IComparable + public static bool IsToday(DateTime value) { - return new ValidatorBuilder(fieldName); + return value.Date == DateTime.Today; } /// - /// 创建集合验证器 + /// 检查是否为过去的时间 /// - public static CollectionValidatorBuilder ForCollection(string fieldName = "collection") + public static bool IsPast(DateTime value) { - return new CollectionValidatorBuilder(fieldName); + return value < DateTime.Now; } /// - /// 创建自定义验证器 + /// 检查是否为未来的时间 /// - public static ValidatorBuilder For(string fieldName = "value") + public static bool IsFuture(DateTime value) { - return new ValidatorBuilder(fieldName); + return value > DateTime.Now; } - #region 快捷验证方法 + #endregion - /// - /// 验证字符串不为空 - /// - public static bool IsNotEmpty(string? value) - { - return !string.IsNullOrWhiteSpace(value); - } + #region 类型验证 /// - /// 验证邮箱格式 + /// 检查值是否为指定类型 /// - public static bool IsEmail(string? value) + public static bool IsType(object? value) { - if (string.IsNullOrWhiteSpace(value)) - return false; - return Regex.IsMatch(value, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"); + return value is T; } /// - /// 验证手机号格式(中国大陆) + /// 检查值是否为 null /// - public static bool IsPhone(string? value) + public static bool IsNull(object? value) { - if (string.IsNullOrWhiteSpace(value)) - return false; - return Regex.IsMatch(value, @"^1[3-9]\d{9}$"); + return value == null; } /// - /// 验证身份证号格式(中国大陆) + /// 检查值是否不为 null /// - public static bool IsIdCard(string? value) + public static bool IsNotNull(object? value) { - if (string.IsNullOrWhiteSpace(value)) - return false; - return Regex.IsMatch(value, @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"); + return value != null; } /// - /// 验证URL格式 + /// 检查是否为默认值 /// - public static bool IsUrl(string? value) + public static bool IsDefault(T value) { - if (string.IsNullOrWhiteSpace(value)) - return false; - return Regex.IsMatch(value, @"^https?://[^\s/$.?#].[^\s]*$", RegexOptions.IgnoreCase); + return EqualityComparer.Default.Equals(value, default); } - /// - /// 验证是否为纯数字 - /// - public static bool IsNumeric(string? value) - { - if (string.IsNullOrWhiteSpace(value)) - return false; - return Regex.IsMatch(value, @"^\d+$"); - } + #endregion - /// - /// 验证是否在范围内 - /// - public static bool InRange(T value, T min, T max) where T : IComparable - { - if (value == null) - return false; - return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0; - } + #region 组合验证 /// - /// 验证字符串长度 + /// 组合多个验证条件(全部满足) /// - public static bool LengthInRange(string? value, int minLength, int maxLength) + public static bool All(params Func[] validators) { - if (value == null) - return minLength <= 0; - return value.Length >= minLength && value.Length <= maxLength; + return validators.All(v => v()); } /// - /// 验证集合不为空 + /// 组合多个验证条件(任一满足) /// - public static bool IsNotEmpty(IEnumerable? collection) + public static bool Any(params Func[] validators) { - return collection != null && collection.Any(); + return validators.Any(v => v()); } /// - /// 验证集合元素数量 + /// 验证并返回结果 /// - public static bool CountInRange(IEnumerable? collection, int minCount, int maxCount) + public static ValidationResult Validate(params (string Field, Func Validator, string ErrorMessage)[] rules) { - if (collection == null) - return minCount <= 0; - var count = collection.Count(); - return count >= minCount && count <= maxCount; + var result = new ValidationResult { IsValid = true }; + + foreach (var (field, validator, errorMessage) in rules) + { + if (!validator()) + { + result.IsValid = false; + result.Errors.Add(errorMessage); + result.ErrorFields.Add(field); + } + } + + return result; } #endregion } -} +} \ No newline at end of file diff --git a/EasyTool.Core/ValidationCategory/CompositeValidator.cs b/EasyTool.Core/ValidationCategory/CompositeValidator.cs new file mode 100644 index 0000000..d5c79e1 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/CompositeValidator.cs @@ -0,0 +1,406 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 组合验证器,支持多个验证器的组合使用 + /// + public class CompositeValidator + { + private readonly List> _validators = new(); + private readonly List> _validationFuncs = new(); + private bool _stopOnFirstFailure; + + /// + /// 添加验证器 + /// + public CompositeValidator Add(IValidator validator) + { + _validators.Add(validator); + return this; + } + + /// + /// 添加验证函数 + /// + public CompositeValidator Add(Func validationFunc) + { + _validationFuncs.Add(validationFunc); + return this; + } + + /// + /// 添加条件验证 + /// + public CompositeValidator AddWhen(Func condition, IValidator validator) + { + _validationFuncs.Add(obj => + { + if (condition(obj)) + { + return validator.Validate(obj); + } + return ValidationResult.Success(); + }); + return this; + } + + /// + /// 添加条件验证函数 + /// + public CompositeValidator AddWhen(Func condition, Func validationFunc) + { + _validationFuncs.Add(obj => + { + if (condition(obj)) + { + return validationFunc(obj); + } + return ValidationResult.Success(); + }); + return this; + } + + /// + /// 设置遇到第一个错误就停止 + /// + public CompositeValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + var allErrors = new List(); + + foreach (var validator in _validators) + { + var result = validator.Validate(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + foreach (var validationFunc in _validationFuncs) + { + var result = validationFunc(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + return allErrors.Count == 0 + ? ValidationResult.Success() + : new ValidationResult(false, allErrors); + } + + /// + /// 异步验证对象 + /// + public async Task ValidateAsync(T instance) + { + var allErrors = new List(); + + foreach (var validator in _validators) + { + var result = await validator.ValidateAsync(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + foreach (var validationFunc in _validationFuncs) + { + var result = validationFunc(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + return allErrors.Count == 0 + ? ValidationResult.Success() + : new ValidationResult(false, allErrors); + } + } + + /// + /// 批量验证器,支持批量验证多个对象 + /// + public class BatchValidator + { + private readonly Dictionary> _propertyValidators = new(); + private bool _stopOnFirstFailure; + + /// + /// 添加属性验证器 + /// + public BatchValidator Add(string propertyName, Func validator) + { + _propertyValidators[propertyName] = validator; + return this; + } + + /// + /// 添加属性验证器(使用 FluentValidator) + /// + public BatchValidator Add(string propertyName, TProperty value, Action> configure) + { + var validator = FluentValidator.For(value, propertyName); + configure(validator); + _propertyValidators[propertyName] = _ => + { + var result = validator.GetResult(); + return result; + }; + return this; + } + + /// + /// 设置遇到第一个错误就停止 + /// + public BatchValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 验证所有属性 + /// + public BatchValidationResult Validate() + { + var propertyResults = new Dictionary(); + var allErrors = new List(); + + foreach (var kvp in _propertyValidators) + { + var result = kvp.Value(null); + propertyResults[kvp.Key] = result; + + if (!result.IsValid) + { + allErrors.AddRange(result.Errors.Select(e => $"[{kvp.Key}] {e}")); + if (_stopOnFirstFailure) + { + break; + } + } + } + + return new BatchValidationResult(allErrors.Count == 0, allErrors, propertyResults); + } + } + + /// + /// 批量验证结果 + /// + public class BatchValidationResult + { + /// + /// 是否全部验证通过 + /// + public bool IsValid { get; } + + /// + /// 所有错误消息 + /// + public IReadOnlyList AllErrors { get; } + + /// + /// 按属性分组的验证结果 + /// + public IReadOnlyDictionary PropertyResults { get; } + + /// + /// 第一个错误消息 + /// + public string? FirstError => AllErrors.FirstOrDefault(); + + public BatchValidationResult(bool isValid, List allErrors, Dictionary propertyResults) + { + IsValid = isValid; + AllErrors = allErrors.AsReadOnly(); + PropertyResults = propertyResults; + } + + /// + /// 获取指定属性的验证结果 + /// + public ValidationResult? GetPropertyResult(string propertyName) + { + return PropertyResults.TryGetValue(propertyName, out var result) ? result : null; + } + + /// + /// 获取指定属性的错误消息 + /// + public IReadOnlyList GetPropertyErrors(string propertyName) + { + return GetPropertyResult(propertyName)?.Errors ?? new List().AsReadOnly(); + } + + /// + /// 获取失败的属性名列表 + /// + public IEnumerable GetFailedProperties() + { + return PropertyResults.Where(kvp => !kvp.Value.IsValid).Select(kvp => kvp.Key); + } + } + + /// + /// 验证器集合,用于管理多个类型的验证器 + /// + public class ValidatorCollection + { + private readonly Dictionary _validators = new(); + + /// + /// 注册验证器 + /// + public ValidatorCollection Register(IValidator validator) + { + _validators[typeof(T)] = validator; + return this; + } + + /// + /// 注册验证器构建器 + /// + public ValidatorCollection Register(Action> configure) + { + var builder = new ValidationRuleBuilder(); + configure(builder); + _validators[typeof(T)] = builder.Build(); + return this; + } + + /// + /// 获取验证器 + /// + public IValidator? Get() + { + return _validators.TryGetValue(typeof(T), out var validator) ? validator as IValidator : null; + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + var validator = Get(); + if (validator == null) + { + // 如果没有注册验证器,尝试使用 ModelValidator + return ModelValidator.Validate(instance); + } + return validator.Validate(instance); + } + + /// + /// 异步验证对象 + /// + public async Task ValidateAsync(T instance) + { + var validator = Get(); + if (validator == null) + { + return await ModelValidator.ValidateAsync(instance); + } + return await validator.ValidateAsync(instance); + } + + /// + /// 验证并抛出异常 + /// + public void ValidateAndThrow(T instance) + { + var result = Validate(instance); + if (!result.IsValid) + { + throw new ValidationException(result.Errors); + } + } + + /// + /// 检查是否已注册验证器 + /// + public bool IsRegistered() + { + return _validators.ContainsKey(typeof(T)); + } + + /// + /// 移除验证器 + /// + public bool Remove() + { + return _validators.Remove(typeof(T)); + } + + /// + /// 清空所有验证器 + /// + public void Clear() + { + _validators.Clear(); + } + } + + /// + /// 验证器扩展方法 + /// + public static class CompositeValidatorExtensions + { + /// + /// 创建组合验证器 + /// + public static CompositeValidator CreateCompositeValidator() + { + return new CompositeValidator(); + } + + /// + /// 创建批量验证器 + /// + public static BatchValidator CreateBatchValidator() + { + return new BatchValidator(); + } + + /// + /// 创建验证器集合 + /// + public static ValidatorCollection CreateValidatorCollection() + { + return new ValidatorCollection(); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/FluentValidator.cs b/EasyTool.Core/ValidationCategory/FluentValidator.cs new file mode 100644 index 0000000..6912731 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/FluentValidator.cs @@ -0,0 +1,437 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.ValidationCategory +{ + /// + /// 流式验证器 + /// + public class FluentValidator + { + private readonly T _value; + private readonly string _propertyName; + private readonly List _errors = new(); + private bool _stopOnFirstFailure; + + private FluentValidator(T value, string propertyName) + { + _value = value; + _propertyName = propertyName; + } + + /// + /// 开始验证 + /// + public static FluentValidator For(T value, string propertyName = "") + { + return new FluentValidator(value, propertyName); + } + + /// + /// 遇到第一个错误就停止 + /// + public FluentValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 自定义验证 + /// + public FluentValidator Must(Func predicate, string errorMessage) + { + if (ShouldValidate() && !predicate(_value)) + { + AddError(errorMessage); + } + return this; + } + + /// + /// 自定义异步验证 + /// + public async System.Threading.Tasks.Task> MustAsync(Func> predicate, string errorMessage) + { + if (ShouldValidate() && !await predicate(_value)) + { + AddError(errorMessage); + } + return this; + } + + /// + /// 不能为null + /// + public FluentValidator NotNull(string? errorMessage = null) + { + if (ShouldValidate() && _value == null) + { + AddError(errorMessage ?? $"{_propertyName}不能为空"); + } + return this; + } + + /// + /// 字符串不能为空 + /// + public FluentValidator NotEmpty(string? errorMessage = null) + { + if (ShouldValidate() && string.IsNullOrEmpty(_value as string)) + { + AddError(errorMessage ?? $"{_propertyName}不能为空"); + } + return this; + } + + /// + /// 字符串不能为空白 + /// + public FluentValidator NotWhiteSpace(string? errorMessage = null) + { + if (ShouldValidate() && string.IsNullOrWhiteSpace(_value as string)) + { + AddError(errorMessage ?? $"{_propertyName}不能为空白"); + } + return this; + } + + /// + /// 字符串长度范围 + /// + public FluentValidator Length(int min, int max, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && (str.Length < min || str.Length > max)) + { + AddError(errorMessage ?? $"{_propertyName}长度必须在{min}到{max}之间"); + } + } + return this; + } + + /// + /// 最小长度 + /// + public FluentValidator MinLength(int min, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && str.Length < min) + { + AddError(errorMessage ?? $"{_propertyName}长度不能小于{min}"); + } + } + return this; + } + + /// + /// 最大长度 + /// + public FluentValidator MaxLength(int max, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && str.Length > max) + { + AddError(errorMessage ?? $"{_propertyName}长度不能超过{max}"); + } + } + return this; + } + + /// + /// 数值范围 + /// + public FluentValidator InRange(IComparable min, IComparable max, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(min) < 0 || comparable.CompareTo(max) > 0) + { + AddError(errorMessage ?? $"{_propertyName}必须在{min}到{max}之间"); + } + } + return this; + } + + /// + /// 大于指定值 + /// + public FluentValidator GreaterThan(IComparable threshold, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(threshold) <= 0) + { + AddError(errorMessage ?? $"{_propertyName}必须大于{threshold}"); + } + } + return this; + } + + /// + /// 小于指定值 + /// + public FluentValidator LessThan(IComparable threshold, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(threshold) >= 0) + { + AddError(errorMessage ?? $"{_propertyName}必须小于{threshold}"); + } + } + return this; + } + + /// + /// 正则匹配 + /// + public FluentValidator Matches(string pattern, string? errorMessage = null) + { + if (ShouldValidate() && _value != null) + { + if (!Regex.IsMatch(_value.ToString() ?? "", pattern)) + { + AddError(errorMessage ?? $"{_propertyName}格式不正确"); + } + } + return this; + } + + /// + /// 邮箱格式 + /// + public FluentValidator Email(string? errorMessage = null) + { + return Matches(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", errorMessage ?? $"{_propertyName}不是有效的邮箱地址"); + } + + /// + /// 手机号格式(中国) + /// + public FluentValidator Phone(string? errorMessage = null) + { + return Matches(@"^1[3-9]\d{9}$", errorMessage ?? $"{_propertyName}不是有效的手机号"); + } + + /// + /// 身份证号格式(中国) + /// + public FluentValidator IdCard(string? errorMessage = null) + { + return Matches(@"^\d{17}[\dXx]$", errorMessage ?? $"{_propertyName}不是有效的身份证号"); + } + + /// + /// URL格式 + /// + public FluentValidator Url(string? errorMessage = null) + { + return Matches(@"^https?://[^\s]+$", errorMessage ?? $"{_propertyName}不是有效的URL"); + } + + /// + /// IP地址格式 + /// + public FluentValidator IpAddress(string? errorMessage = null) + { + return Matches(@"^(\d{1,3}\.){3}\d{1,3}$", errorMessage ?? $"{_propertyName}不是有效的IP地址"); + } + + /// + /// 在指定值列表中 + /// + public FluentValidator In(IEnumerable values, string? errorMessage = null) + { + if (ShouldValidate() && !values.Contains(_value)) + { + AddError(errorMessage ?? $"{_propertyName}必须是有效值之一"); + } + return this; + } + + /// + /// 等于指定值 + /// + public FluentValidator Equal(T expected, string? errorMessage = null) + { + if (ShouldValidate() && !EqualityComparer.Default.Equals(_value, expected)) + { + AddError(errorMessage ?? $"{_propertyName}必须等于{expected}"); + } + return this; + } + + /// + /// 不等于指定值 + /// + public FluentValidator NotEqual(T unexpected, string? errorMessage = null) + { + if (ShouldValidate() && EqualityComparer.Default.Equals(_value, unexpected)) + { + AddError(errorMessage ?? $"{_propertyName}不能等于{unexpected}"); + } + return this; + } + + /// + /// 集合不为空 + /// + public FluentValidator NotNullOrEmpty(string? errorMessage = null) + { + if (ShouldValidate()) + { + if (_value is System.Collections.ICollection collection && collection.Count == 0) + { + AddError(errorMessage ?? $"{_propertyName}不能为空集合"); + } + else if (_value is System.Collections.IEnumerable enumerable && !enumerable.Cast().Any()) + { + AddError(errorMessage ?? $"{_propertyName}不能为空集合"); + } + } + return this; + } + + /// + /// 条件验证 + /// + public FluentValidator When(Func condition, Action> action) + { + if (condition(_value)) + { + action(this); + } + return this; + } + + /// + /// 反条件验证 + /// + public FluentValidator Unless(Func condition, Action> action) + { + if (!condition(_value)) + { + action(this); + } + return this; + } + + /// + /// 获取验证结果 + /// + public ValidationResult GetResult() + { + return new ValidationResult(_errors.Count == 0, _errors); + } + + /// + /// 是否验证通过 + /// + public bool IsValid => _errors.Count == 0; + + /// + /// 获取错误消息 + /// + public IReadOnlyList Errors => _errors.AsReadOnly(); + + /// + /// 获取第一条错误消息 + /// + public string? FirstError => _errors.FirstOrDefault(); + + /// + /// 抛出验证异常 + /// + public void ThrowIfInvalid() + { + if (!IsValid) + { + throw new ValidationException(_errors); + } + } + + private bool ShouldValidate() => !_stopOnFirstFailure || _errors.Count == 0; + + private void AddError(string error) + { + _errors.Add(error); + } + } + + /// + /// 验证结果 + /// + public class ValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; } + + /// + /// 错误消息 + /// + public IReadOnlyList Errors { get; } + + /// + /// 第一条错误消息 + /// + public string? FirstError => Errors.FirstOrDefault(); + + public ValidationResult(bool isValid, List errors) + { + IsValid = isValid; + Errors = errors.AsReadOnly(); + } + + public static ValidationResult Success() => new ValidationResult(true, new List()); + + public static ValidationResult Failure(params string[] errors) => new ValidationResult(false, errors.ToList()); + } + + /// + /// 验证异常 + /// + public class ValidationException : Exception + { + /// + /// 错误消息列表 + /// + public IReadOnlyList Errors { get; } + + public ValidationException(IEnumerable errors) + : base(string.Join("; ", errors)) + { + Errors = errors.ToList().AsReadOnly(); + } + + public ValidationException(string error) + : base(error) + { + Errors = new List { error }.AsReadOnly(); + } + } + + /// + /// 验证器扩展 + /// + public static class FluentValidatorExtensions + { + /// + /// 验证对象 + /// + public static FluentValidator Validate(this T value, string propertyName = "") + { + return FluentValidator.For(value, propertyName); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/ModelValidator.cs b/EasyTool.Core/ValidationCategory/ModelValidator.cs new file mode 100644 index 0000000..82d9dd2 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/ModelValidator.cs @@ -0,0 +1,352 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 模型验证器,基于 DataAnnotations 特性进行验证 + /// + public static class ModelValidator + { + /// + /// 验证模型 + /// + /// 模型类型 + /// 要验证的模型实例 + /// 是否验证所有属性 + /// 验证结果 + public static ValidationResult Validate(T model, bool validateAllProperties = true) + { + if (model == null) + { + return ValidationResult.Failure("模型不能为空"); + } + + var context = new ValidationContext(model, null, null); + var results = new List(); + var isValid = Validator.TryValidateObject(model, context, results, validateAllProperties); + + if (isValid) + { + return ValidationResult.Success(); + } + + var errors = results.Select(r => r.ErrorMessage ?? "验证失败").ToList(); + return new ValidationResult(false, errors); + } + + /// + /// 异步验证模型 + /// + public static async Task ValidateAsync(T model, bool validateAllProperties = true) + { + return await Task.Run(() => Validate(model, validateAllProperties)); + } + + /// + /// 验证模型并抛出异常 + /// + public static void ValidateAndThrow(T model, bool validateAllProperties = true) + { + var result = Validate(model, validateAllProperties); + if (!result.IsValid) + { + throw new ValidationException(result.Errors); + } + } + + /// + /// 验证单个属性 + /// + /// 模型类型 + /// 属性类型 + /// 模型实例 + /// 属性名 + /// 属性值 + /// 验证结果 + public static ValidationResult ValidateProperty(T model, string propertyName, TProperty value) + { + if (model == null) + { + return ValidationResult.Failure("模型不能为空"); + } + + var context = new ValidationContext(model) { MemberName = propertyName }; + var results = new List(); + var isValid = Validator.TryValidateProperty(value, context, results); + + if (isValid) + { + return ValidationResult.Success(); + } + + var errors = results.Select(r => r.ErrorMessage ?? "验证失败").ToList(); + return new ValidationResult(false, errors); + } + + /// + /// 获取模型的所有验证属性 + /// + public static IEnumerable GetValidationAttributes() + { + var type = typeof(T); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in properties) + { + var attributes = property.GetCustomAttributes(); + var displayAttribute = property.GetCustomAttribute(); + var info = new PropertyValidationInfo + { + PropertyName = property.Name, + DisplayName = displayAttribute?.GetName() ?? property.Name, + ValidationAttributes = attributes.ToList() + }; + yield return info; + } + } + + /// + /// 尝试验证并获取验证错误信息字典 + /// + public static Dictionary> ValidateToDictionary(T model, bool validateAllProperties = true) + { + var result = Validate(model, validateAllProperties); + var dictionary = new Dictionary>(); + + if (result.IsValid) + { + return dictionary; + } + + // 尝试按属性分组错误信息 + var context = new ValidationContext(model, null, null); + var results = new List(); + Validator.TryValidateObject(model, context, results, validateAllProperties); + + foreach (var validationResult in results) + { + var propertyNames = validationResult.MemberNames.ToList(); + if (propertyNames.Count == 0) + { + propertyNames.Add(string.Empty); + } + + foreach (var propertyName in propertyNames) + { + if (!dictionary.ContainsKey(propertyName)) + { + dictionary[propertyName] = new List(); + } + dictionary[propertyName].Add(validationResult.ErrorMessage ?? "验证失败"); + } + } + + return dictionary; + } + + /// + /// 验证字典数据 + /// + public static ValidationResult ValidateDictionary(IDictionary data, IEnumerable rules) + { + var errors = new List(); + var rulesDict = rules.ToDictionary(r => r.PropertyName, r => r); + + foreach (var rule in rulesDict.Values) + { + if (!data.TryGetValue(rule.PropertyName, out var value)) + { + if (rule.IsRequired) + { + errors.Add(rule.RequiredErrorMessage ?? $"{rule.PropertyName}是必填项"); + } + continue; + } + + foreach (var validator in rule.Validators) + { + if (!validator(value)) + { + errors.Add(rule.ErrorMessage ?? $"{rule.PropertyName}验证失败"); + } + } + } + + return errors.Count == 0 ? ValidationResult.Success() : new ValidationResult(false, errors); + } + + /// + /// 验证对象字典 + /// + public static ValidationResult ValidateObjectDictionary(IDictionary data, Type modelType) + { + var errors = new List(); + var properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in properties) + { + var validationAttributes = property.GetCustomAttributes(); + var displayAttribute = property.GetCustomAttribute(); + var displayName = displayAttribute?.GetName() ?? property.Name; + + if (!data.TryGetValue(property.Name, out var value)) + { + var requiredAttr = validationAttributes.FirstOrDefault(a => a is RequiredAttribute); + if (requiredAttr != null) + { + errors.Add(requiredAttr.ErrorMessage ?? $"{displayName}是必填项"); + } + continue; + } + + foreach (var attr in validationAttributes) + { + try + { + // 类型转换 + var convertedValue = value == null ? null : Convert.ChangeType(value, property.PropertyType); + if (!attr.IsValid(convertedValue)) + { + errors.Add(attr.ErrorMessage ?? $"{displayName}验证失败"); + } + } + catch (Exception) + { + errors.Add($"{displayName}类型转换失败"); + } + } + } + + return errors.Count == 0 ? ValidationResult.Success() : new ValidationResult(false, errors); + } + } + + /// + /// 属性验证信息 + /// + public class PropertyValidationInfo + { + /// + /// 属性名称 + /// + public string PropertyName { get; set; } = string.Empty; + + /// + /// 显示名称 + /// + public string DisplayName { get; set; } = string.Empty; + + /// + /// 验证特性列表 + /// + public IReadOnlyList ValidationAttributes { get; set; } = new List(); + } + + /// + /// 属性验证规则 + /// + public class PropertyValidationRule + { + /// + /// 属性名称 + /// + public string PropertyName { get; set; } = string.Empty; + + /// + /// 是否必填 + /// + public bool IsRequired { get; set; } + + /// + /// 必填错误消息 + /// + public string? RequiredErrorMessage { get; set; } + + /// + /// 验证器列表 + /// + public List> Validators { get; set; } = new(); + + /// + /// 验证失败错误消息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 创建属性验证规则 + /// + public static PropertyValidationRule Create(string propertyName) + { + return new PropertyValidationRule { PropertyName = propertyName }; + } + + /// + /// 设置为必填 + /// + public PropertyValidationRule Required(string? errorMessage = null) + { + IsRequired = true; + RequiredErrorMessage = errorMessage; + return this; + } + + /// + /// 添加验证器 + /// + public PropertyValidationRule AddValidator(Func validator, string? errorMessage = null) + { + Validators.Add(validator); + if (!string.IsNullOrEmpty(errorMessage)) + { + ErrorMessage = errorMessage; + } + return this; + } + + /// + /// 添加正则验证 + /// + public PropertyValidationRule Regex(string pattern, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + return System.Text.RegularExpressions.Regex.IsMatch(value.ToString() ?? "", pattern); + }, errorMessage); + } + + /// + /// 添加长度验证 + /// + public PropertyValidationRule Length(int min, int max, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + var str = value.ToString() ?? ""; + return str.Length >= min && str.Length <= max; + }, errorMessage); + } + + /// + /// 添加范围验证 + /// + public PropertyValidationRule Range(IComparable min, IComparable max, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + if (value is IComparable comparable) + { + return comparable.CompareTo(min) >= 0 && comparable.CompareTo(max) <= 0; + } + return true; + }, errorMessage); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs b/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs new file mode 100644 index 0000000..594b00f --- /dev/null +++ b/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs @@ -0,0 +1,377 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 验证规则构建器,支持链式调用构建复杂验证规则 + /// + public class ValidationRuleBuilder + { + private readonly List> _rules = new(); + private string? _currentProperty; + private string? _currentErrorMessage; + + /// + /// 为指定属性添加规则 + /// + public ValidationRuleBuilder RuleFor(Expression> propertyExpression) + { + _currentProperty = GetPropertyName(propertyExpression); + _currentErrorMessage = null; + return this; + } + + /// + /// 设置自定义错误消息 + /// + public ValidationRuleBuilder WithMessage(string errorMessage) + { + _currentErrorMessage = errorMessage; + return this; + } + + /// + /// 必须满足条件 + /// + public ValidationRuleBuilder Must(Expression> propertyExpression, Func predicate) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => predicate(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}验证失败" + }); + return this; + } + + /// + /// 不能为null + /// + public ValidationRuleBuilder NotNull(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => getter(obj) != null, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空" + }); + return this; + } + + /// + /// 字符串不能为空 + /// + public ValidationRuleBuilder NotEmpty(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => !string.IsNullOrEmpty(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空" + }); + return this; + } + + /// + /// 字符串不能为空白 + /// + public ValidationRuleBuilder NotWhiteSpace(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => !string.IsNullOrWhiteSpace(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空白" + }); + return this; + } + + /// + /// 字符串长度范围 + /// + public ValidationRuleBuilder Length(Expression> propertyExpression, int min, int max) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || (value.Length >= min && value.Length <= max); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}长度必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 数值范围 + /// + public ValidationRuleBuilder InRange(Expression> propertyExpression, TProperty min, TProperty max) + where TProperty : IComparable + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || (value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 正则匹配 + /// + public ValidationRuleBuilder Matches(Expression> propertyExpression, string pattern) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + var regex = new Regex(pattern); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || regex.IsMatch(value); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}格式不正确" + }); + return this; + } + + /// + /// 邮箱格式 + /// + public ValidationRuleBuilder Email(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^[^@\s]+@[^@\s]+\.[^@\s]+$").WithMessage(_currentErrorMessage ?? "邮箱格式不正确"); + } + + /// + /// 手机号格式(中国) + /// + public ValidationRuleBuilder Phone(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^1[3-9]\d{9}$").WithMessage(_currentErrorMessage ?? "手机号格式不正确"); + } + + /// + /// URL格式 + /// + public ValidationRuleBuilder Url(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^https?://[^\s]+$").WithMessage(_currentErrorMessage ?? "URL格式不正确"); + } + + /// + /// IPv4地址格式 + /// + public ValidationRuleBuilder IPv4(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^(\d{1,3}\.){3}\d{1,3}$").WithMessage(_currentErrorMessage ?? "IPv4地址格式不正确"); + } + + /// + /// 身份证号格式(中国) + /// + public ValidationRuleBuilder IdCard(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + if (string.IsNullOrEmpty(value)) return true; + return IsValidIdCard(value); + }, + ErrorMessage = _currentErrorMessage ?? "身份证号格式不正确" + }); + return this; + } + + /// + /// 集合不为空 + /// + public ValidationRuleBuilder NotEmpty(Expression>> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value != null && value.Any(); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空集合" + }); + return this; + } + + /// + /// 集合元素数量范围 + /// + public ValidationRuleBuilder CollectionLength(Expression>> propertyExpression, int min, int max) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + if (value == null) return false; + var count = value.Count(); + return count >= min && count <= max; + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}元素数量必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 构建验证器 + /// + public IValidator Build() + { + return new RuleBasedValidator(_rules.ToList()); + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + return Build().Validate(instance); + } + + private static string GetPropertyName(Expression> expression) + { + return expression.Body switch + { + MemberExpression memberExpression => memberExpression.Member.Name, + UnaryExpression { Operand: MemberExpression me } => me.Member.Name, + _ => expression.ToString() + }; + } + + private static bool IsValidIdCard(string idCard) + { + if (string.IsNullOrEmpty(idCard) || idCard.Length != 18) + return false; + + // 基本格式检查 + if (!Regex.IsMatch(idCard, @"^\d{17}[\dXx]$")) + return false; + + // 校验码验证 + var weights = new[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + var checkCodes = "10X98765432"; + var sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard[i] - '0') * weights[i]; + } + var checkCode = checkCodes[sum % 11]; + return char.ToUpper(idCard[17]) == checkCode; + } + } + + /// + /// 验证规则 + /// + public class ValidationRule + { + public string PropertyName { get; set; } = string.Empty; + public Func Validate { get; set; } = _ => true; + public string ErrorMessage { get; set; } = string.Empty; + } + + /// + /// 验证器接口 + /// + public interface IValidator + { + ValidationResult Validate(T instance); + Task ValidateAsync(T instance); + } + + /// + /// 基于规则的验证器 + /// + internal class RuleBasedValidator : IValidator + { + private readonly List> _rules; + + public RuleBasedValidator(List> rules) + { + _rules = rules; + } + + public ValidationResult Validate(T instance) + { + var errors = new List(); + foreach (var rule in _rules) + { + try + { + if (!rule.Validate(instance)) + { + errors.Add(rule.ErrorMessage); + } + } + catch (Exception ex) + { + errors.Add($"{rule.PropertyName}验证异常: {ex.Message}"); + } + } + return new ValidationResult(errors.Count == 0, errors); + } + + public async Task ValidateAsync(T instance) + { + return await Task.Run(() => Validate(instance)); + } + } + + /// + /// 验证规则构建器静态扩展 + /// + public static class ValidationRuleBuilderExtensions + { + /// + /// 创建验证规则构建器 + /// + public static ValidationRuleBuilder CreateValidator() + { + return new ValidationRuleBuilder(); + } + } +} diff --git a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj b/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj deleted file mode 100644 index 28315f7..0000000 --- a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net10.0 - true - enable - enable - latest - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs b/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs deleted file mode 100644 index 1384f22..0000000 --- a/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs +++ /dev/null @@ -1,38 +0,0 @@ -using EasyTool.Extension; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace EasyTool.Tests -{ - [TestClass()] - public class EmitMapperExtensionTests - { - [TestMethod()] - public void EmitMapperTest() - { - var obj1 = new First() - { - MyProperty1 = 1, - MyProperty2 = "A" - }; - - var obj2 = obj1.EmitMapTo(); - - Assert.AreEqual(obj1.MyProperty1, obj2.MyProperty1); - Assert.AreEqual(obj1.MyProperty2, obj2.MyProperty2); - } - - [Serializable] - public class First - { - public int MyProperty1 { get; set; } - public string MyProperty2 { get; set; } - } - - [Serializable] - public class Second - { - public int MyProperty1 { get; set; } - public string MyProperty2 { get; set; } - } - } -} diff --git a/EasyTool.ImageTests/EasyTool.ImageTests.csproj b/EasyTool.ImageTests/EasyTool.ImageTests.csproj deleted file mode 100644 index 1b36049..0000000 --- a/EasyTool.ImageTests/EasyTool.ImageTests.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net10.0 - true - enable - enable - latest - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs b/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs deleted file mode 100644 index 76b1ec2..0000000 --- a/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Drawing; - -namespace EasyTool.CoreTests.ImageCategory -{ - [TestClass] - public class ImageUtilTests - { - /// - /// 图像分割方法测试 - /// 测试用到的ori和mask在测试类旁边的Reources中 - /// - [TestMethod] - public void MaskImageTest() - { - Image ori = new Bitmap(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory","Resources","ori.jpg")); - Image mask = new Bitmap(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory", "Resources", "mask.jpg")); - - Console.WriteLine($"ori-width:{ori.Width} | ori-height:{ori.Height}"); - Console.WriteLine($"mask-width:{mask.Width} | mask-height:{mask.Height}"); - - Image result = ImgUtil.MaskImage(mask, ori); - - Console.WriteLine($"result-width:{mask.Width} | result-height:{mask.Height}"); - result.Save(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory", "Resources", "result.jpg")); - } - - [TestMethod] - public void ResizeImageTest() - { - Console.WriteLine("Hello World"); - } - - [TestMethod] - public void CropImageTest() - { - Assert.Fail(); - } - - [TestMethod] - public void ConvertImageFormatTest() - { - Assert.Fail(); - } - - [TestMethod] - public void ConvertToBlackAndWhiteTest() - { - Assert.Fail(); - } - - [TestMethod] - public void AddTextWatermarkTest() - { - Assert.Fail(); - } - } -} diff --git a/EasyTool.ImageTests/ImageCategory/Resources/mask.jpg b/EasyTool.ImageTests/ImageCategory/Resources/mask.jpg deleted file mode 100644 index 655962abf3e5542ffdc071b559f9dd63359f59a6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 18107 zcmeI1YfKbZ7>2*uncbNk7RFsbt8Eof>q>tZuWiv3Z$QByK`qqUgaTerV+^)MZK@!O zB8f2~iZ(G+tfHd9`-L_l3Q`)YLGS`3vg-w{ny^|{m%YsC8EdR5^TRe~+SKpNob2qG z%Q^4+KIhDjA=iivdc;M?Mgzkz@HyRp*dZ)x$=ulh@$nE200KQ{0hS&ybbDGzD@*_# z!zlZ+!s=P27&wmA^9G(*s$ldp3W6x`yy!2AekNLYV}Rsu3Q&%fhbXrzchTJ>@Pcy1 z-z(%am<2Y3JHRqwpffY9nISdMkB(JO6)B>g6-LK$dIKGj=tnO&+Jg?CWp#A0dXA%4 zFQeyyGwUs(L&q5c6I1!H%%EW_Hf$IAk3U!)Jf*=gV0hZ1T%*{t*Bib2418yhb#VBI zk)z&w|AUAL6QiPIK8lT-I_=}6^T{8=Y8?zS6PdfEd6@rs=U=}^4As= zZYRgZ%cQ4zjIgF?mc@C9X@jO*zpr5E6<#*sXbTsQ~l4E8k;U(xq7X+#eU=F zt=sK)?*4l3;iKO=osXZmy4;E`2G~DjJy-S@U1q9FM>F7fMHi!6Oe<^V^r1rymT`%E zYGz>AuoXhk_zl|+RvY^dpW+BkTht)-95BK@@S!5@sj`<7miv#CJs0+;t`;z{3=NMp zLnO%AcS{SviWNOT29N<{02x3AkO5=>89)Y*f!CCQcOOWukctNz>;ihw0wp$jd&iRCZx6e%igRi=J z6UZdsi*ZJPPbQH6S$1lqJG826scR*Hjk~+Gly|tTtW9#|)jbX*Q2Hi;;*$Kxu88&B zT29f^IUho|C*U(*bQt}79at)NtdRq9e7UFSoT(n2y|6-X4YGCQMcO6Dg*^`W;*83} za^sIpV_NG+z8%-A_4x;%VMy(R)D=)WEAZIGREBrytt zUSSRPo~9YyYkO4GM%S+9@W!gnj(y3UK1=`;&~C2b9Qe0aHB0~#!1n;YiM3nv)#e+n zLTavP*@g*d>9$&5n1GrqTDD;VTDq;)7bc+Qik5AdfR=8n^@RzjxuRtoCZMI;YJFh> zYOZM6h6!ltwpw5630PT=x3nrJ;=lY`(ay_K68(m;%f-v}N!tjNElaBk$@orJV8x0a yAOpw%GJp&q1IPd}fD9l5$N(~c3?Ku@05X6KAOpw%GJp&q1IPd}@B#x8Y5pCnSlE~V diff --git a/EasyTool.ImageTests/ImageCategory/Resources/ori.jpg b/EasyTool.ImageTests/ImageCategory/Resources/ori.jpg deleted file mode 100644 index 9d8282ff910ca2d54688b7b6f8d7120cacef260d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 231099 zcmbTdWmFtZ6fHV9!8I9NgF|q)KyV*qf+zUk?(Q-SF2S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2~E6P#;Fmxn#>z(OS_cYaU@$pyI^yI**WiDBmUce6cbSWoqR{2N1?;2!6v6{ zuYYh#J&TDlw-KffErB0U2J^-^uJhq%#F#a09Qlrg%I+@hmOqVor^l;nPZXx3qsHs@ zO=8`xPPQgKZHv{Gc!6Pj1J4y5TxlSxUowgJHAmYO(PN?<;l&S)8@Pyt9zDI2W; zb4j)V$DyNjE)6z~Kz4D`j-rve(t(OK>HrT)PAPf{Kr>7LoX~j9B|8F>R~3fSv-#0W kLKxplN?s`7npV=lB`!y$2NaZ=V8VbiOP@+gpD`c**}iadjsO4v diff --git a/EasyTool.ImageTests/ImageCategory/Resources/result.jpg b/EasyTool.ImageTests/ImageCategory/Resources/result.jpg deleted file mode 100644 index cb6bf3bb68ad3684b9fb9f3f626561898e1474f5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 278488 zcmeEu^;27I7cEkxxCNI|in}`$Zz);`?(PtZySub_acPlY!BgA<#i6)Efa30cc|Vyu z_Yb(gea}piIXROvnKN0>T6^ua6RoDAfQ>-5CjC|H|VG@@9?f= zA-%jHe)+5*jZig8x%+bP%34ZU3IU-e5%a+m<>mOb%Lm;r2naYm|2z=;ol7kc5T1X2 zl$Fx-GCFv){F<$uDX^!~JHJh#&tml#Y!Q%5#!?4Wq_?sfK>B3$@@xhf9v(L1YO3iP zc4}#{{@Z)&nb)AD=uzIWbU%EwOZ83ih(^0rx17KT9P?W=_p%}o`MvCaR!WmA4gvmu z)>dqG=zmYh-oD*_^WPKd?Z1UT{P#=>=l?$P-y8G4JNCaH%ztq4KfL^JyZHCE@W0*i ze`N7rdi@_){(n>#x_1{CHC-Lw@6$^iE%roi`mOt;qIRB_dVD?1`juy6BX63{RYa8? zwK;!3aT<15#4r5o%RG8+QNAl&6=KNY5V#SX?L8oYbXb2cjzB)Yag9bDstKQoK{Td1zw7 z;Gby}W7?Qf6?=7}1@BuiI5F%(ZTPMVmif_h0`aJVvGgAB`Jj)bwNSM`NAEv|5{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nY*_18-9=wFGTS&>z^b` zXt_=>{+rqnT#27xbniw}vMD>TTJ2_-Y@?vLZGHns#G)7k@I_acFt`fj|E)N5cm3+L z($RRqcrcQ_+{GZn&8p|a#a%qW>8V4m%z(KYmfoeh?rsw;6|CD01&~TZHHb!4u3X5# zwJAc&il|SQV|euysc{Vh{j~H%Cj6>r=;)(Y;Ts`(nmfl@EO4CogN;a+E3;pQ^>vohN7w@#j1T($T>GyqnO z!0Olf?q##!yl&JZd(2mPtsSA*7OZU+xId$2BR4i?{pFdT} z;$7}XVNRzM<%9ZeqKl5-`=QnBPh~xylyRI0io7JX9`GBQv&vzyj) zwW2YpDPxT6DBY%arRI5~e8(ab&>8&6M7;`=>~D+X{wkmLe+EDN^`{Jy0JEF^`d0s5 z@1%j=$Nyfr@_!@g{`X>50Wxj=|Ns2^mVZC7|9vk0ePI4wFaO`R@b7lV)h{_iWL+{bcSf!9x-g!sSUdn^z_<6IRN|M;XR1^Wqbk-kR( zUbBrD|K6w$KLuX>B7Xe6QQ{{4`-jC06?heg%m2Ok_f7wP5dV)n5#6nXg7Tu)I3bPy P^W=V#27f4%Gzj=V`N-w} diff --git a/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs b/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs index bd8e567..4b50dec 100644 --- a/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs +++ b/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs @@ -133,7 +133,54 @@ public static IWorkbook OpenWorkbookFromStream(Stream stream, ExcelWorkbookType var property = t.GetType().GetProperty(headerRow.GetCell(m).StringCellValue); if (property == null) continue; - property.SetValue(t, value, null); + var targetType = property.PropertyType; + var underlyingType = Nullable.GetUnderlyingType(targetType) ?? targetType; + object? convertedValue = null; + if (underlyingType == typeof(string)) + { + convertedValue = value; + } + else if (string.IsNullOrWhiteSpace(value)) + { + convertedValue = null; + } + else if (underlyingType == typeof(int)) + { + convertedValue = int.Parse(value); + } + else if (underlyingType == typeof(long)) + { + convertedValue = long.Parse(value); + } + else if (underlyingType == typeof(double)) + { + convertedValue = double.Parse(value); + } + else if (underlyingType == typeof(decimal)) + { + convertedValue = decimal.Parse(value); + } + else if (underlyingType == typeof(float)) + { + convertedValue = float.Parse(value); + } + else if (underlyingType == typeof(bool)) + { + convertedValue = bool.Parse(value); + } + else if (underlyingType == typeof(DateTime)) + { + convertedValue = DateTime.Parse(value); + } + else if (underlyingType == typeof(Guid)) + { + convertedValue = Guid.Parse(value); + } + else + { + convertedValue = Convert.ChangeType(value, underlyingType); + } + property.SetValue(t, convertedValue, null); } list.Add(t); } diff --git a/EasyTool.NPOITests/EasyTool.NPOITests.csproj b/EasyTool.NPOITests/EasyTool.NPOITests.csproj deleted file mode 100644 index 167c9e3..0000000 --- a/EasyTool.NPOITests/EasyTool.NPOITests.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net10.0 - true - enable - enable - latest - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs b/EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs deleted file mode 100644 index f2ec96d..0000000 --- a/EasyTool.NPOITests/OfficeCategory/NPOIUtilTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.NPOI; - -using System; -using System.Collections.Generic; -using System.Text; -using NPOI.SS.UserModel; -using System.Data; -using System.Text.Json; - -namespace EasyTool.NPOITests; - -[TestClass()] -public class NPOIUtilTests -{ - string path = @"../TempClass.xlsx"; - List dataList = new List() - { - new TempClass() - { - Name = "张三", - Age = 1, - Birthday = DateTime.Now - }, - new TempClass() - { - Name = "李四", - Age = 11, - Birthday = DateTime.Now.AddDays(1) - }, - new TempClass() - { - Name = "王五", - Age = 23, - Birthday = DateTime.Now.AddMonths(1) - } - }; - DataTable dataTable; - public NPOIUtilTests() - { - dataTable = new DataTable(); - dataTable.Columns.AddRange(new DataColumn[] - { - new DataColumn("Name",typeof(string)), - new DataColumn("Age",typeof(int)), - new DataColumn("Birthday",typeof(DateTime)) - }); - foreach (var item in dataList) - { - DataRow dataRow = dataTable.NewRow(); - dataRow["Name"] = item.Name; - dataRow["Age"] = item.Age; - dataRow["Birthday"] = item.Birthday; - dataTable.Rows.Add(dataRow); - } - } - - [TestMethod] - public void Test_ExportToExcel() - { - bool res = NPOIUtil.ExportToExcel(dataList, "../", out string msg); - bool res2 = NPOIUtil.ExportToExcel(dataList, "../", out string msg2, ExcelWorkbookType.XLS); - Assert.IsTrue(res && res2); - } - [TestMethod] - public void Test_ExportToExcelFromDatatable() - { - bool res = NPOIUtil.ExportToExcel(dataTable, "../", out string msg); - bool res2 = NPOIUtil.ExportToExcel(dataTable, "../", out string msg2, ExcelWorkbookType.XLS); - Assert.IsTrue(res && res2); - - } - [TestMethod] - public void Test_OpenWorkbookFromPath() - { - var workbook = NPOIUtil.OpenWorkbook(path); - Console.WriteLine(workbook.GetSheetName(0)); - Assert.IsNotNull(workbook); - } - [TestMethod] - public void Test_OpenWorkbookFromStream() - { - using Stream stream = File.OpenRead(path); - var workbook = NPOIUtil.OpenWorkbookFromStream(stream, ExcelWorkbookType.XLSX); - Console.WriteLine(workbook.GetSheetName(0)); - Assert.IsNotNull(workbook); - } - [TestMethod] - public void Test_ConvertToDataSet() - { - var workbook = NPOIUtil.OpenWorkbook(path); - var dataSet = workbook.ConvertToDataSet(); - Console.WriteLine(dataSet?.DataSetName); - Assert.IsNotNull(dataSet); - } - [TestMethod] - public void Test_ConvertToDataTable() - { - var workbook = NPOIUtil.OpenWorkbook(path); - var dataTable = workbook.GetSheetAt(0).ConvertToDatatable(); - Console.WriteLine(dataTable.TableName); - Assert.IsNotNull(dataTable); - } - [TestMethod] - public void Test_ConvertToList() where T : new() - { - var workbook = NPOIUtil.OpenWorkbook(path); - List dataList = workbook.GetSheetAt(0).ConvertToList(); - Console.WriteLine(JsonSerializer.Serialize(dataList)); - Assert.IsNotNull(dataList); - } -} -class TempClass -{ - public string Name { get; set; } - public int Age { get; set; } - public DateTime Birthday { get; set; } -} \ No newline at end of file diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs deleted file mode 100644 index b164b9b..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildDtoToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildDtoToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("BuildDtoTest")); - } - } - - [DtoComments("C#编译TS示例Dto")] - public class BuildDtoTest - { - public Guid Id { get; set; } - public string Name { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs deleted file mode 100644 index 4c0c15e..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.ComponentModel; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildOptionToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildOptionToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("BuildOptionTest")); - } - - - } - - [OptionComments("C#编译TS示例Option")] - public class BuildOptionTest - { - [DisplayName("调试")] - public static string Debug { get; set; } = nameof(Debug); - [DisplayName("消息")] - public static string Info { get; set; } = nameof(Info); - [DisplayName("警告")] - public static string Warning { get; set; } = nameof(Warning); - [DisplayName("错误")] - public static string Error { get; set; } = nameof(Error); - } -} \ No newline at end of file diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs deleted file mode 100644 index 6abcc64..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildWebApiToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildWebApiToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("GetTest")); - Assert.IsTrue(toDto.Contains("PostTest")); - } - } - - [ApiController] - public class BuildTestController - { - [ApiComments("GetTest Api")] - [HttpGet] - public string GetTest() - { - return null; - } - - [ApiComments("PostTest Api")] - [HttpPost] - public string PostTest() - { - return null; - } - } - -} \ No newline at end of file diff --git a/EasyTool.WebTests/EasyTool.WebTests.csproj b/EasyTool.WebTests/EasyTool.WebTests.csproj deleted file mode 100644 index b4f5d54..0000000 --- a/EasyTool.WebTests/EasyTool.WebTests.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net10.0 - true - enable - enable - latest - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.sln b/EasyTool.sln index d51f761..0e80faf 100644 --- a/EasyTool.sln +++ b/EasyTool.sln @@ -1,27 +1,18 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.4.33103.184 +# Visual Studio Version 18 +VisualStudioVersion = 18.4.11605.240 stable MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Core", "EasyTool.Core\EasyTool.Core.csproj", "{ACA106C6-039B-425C-89F9-7FE9042DC3C3}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.CoreTests", "EasyTool.CoreTests\EasyTool.CoreTests.csproj", "{7A101110-5202-44E9-ABE2-8388AB573932}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.EmitMapper", "EasyTool.EmitMapper\EasyTool.EmitMapper.csproj", "{986FCBD3-2A69-4012-BE41-FB4FF2906A05}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.EmitMapperTests", "EasyTool.EmitMapperTests\EasyTool.EmitMapperTests.csproj", "{8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Web", "EasyTool.Web\EasyTool.Web.csproj", "{578D6FC8-C937-4FAE-B776-9E52043BA8E0}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.WebTests", "EasyTool.WebTests\EasyTool.WebTests.csproj", "{E033014B-67D0-4B42-8507-B90ACE0BD059}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.NPOI", "EasyTool.NPOI\EasyTool.NPOI.csproj", "{573938DD-661A-4074-8A62-4FC651E97E13}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.NPOITests", "EasyTool.NPOITests\EasyTool.NPOITests.csproj", "{7AC7EC2E-003E-49E7-8124-09B88C8F8A49}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Image", "EasyTool.Image\EasyTool.Image.csproj", "{F7AEE692-A41F-4B64-A659-B3F92EA03429}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyTool.ImageTests", "EasyTool.ImageTests\EasyTool.ImageTests.csproj", "{09E30ABC-1F36-4D65-8416-AF7C5C75DA65}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.CoreTests", "EasyTool.CoreTests\EasyTool.CoreTests.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,42 +24,26 @@ Global {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Release|Any CPU.Build.0 = Release|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Release|Any CPU.Build.0 = Release|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Debug|Any CPU.Build.0 = Debug|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Release|Any CPU.ActiveCfg = Release|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Release|Any CPU.Build.0 = Release|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Release|Any CPU.Build.0 = Release|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Release|Any CPU.Build.0 = Release|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Release|Any CPU.Build.0 = Release|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Debug|Any CPU.Build.0 = Debug|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Release|Any CPU.ActiveCfg = Release|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Release|Any CPU.Build.0 = Release|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Release|Any CPU.Build.0 = Release|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Debug|Any CPU.Build.0 = Debug|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Release|Any CPU.ActiveCfg = Release|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Release|Any CPU.Build.0 = Release|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Debug|Any CPU.Build.0 = Debug|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Release|Any CPU.ActiveCfg = Release|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Release|Any CPU.Build.0 = Release|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,4 +51,4 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {960F4C21-B8CA-430B-B315-E5661C1C44B6} EndGlobalSection -EndGlobal +EndGlobal \ No newline at end of file From cd51d17133963d95d23d9ca40fd1c6fc273534d1 Mon Sep 17 00:00:00 2001 From: lilinjin0520 <761747705@qq.com> Date: Fri, 27 Mar 2026 13:57:22 +0800 Subject: [PATCH 18/19] =?UTF-8?q?refactor(core):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E9=85=8D=E7=BD=AE=E5=92=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E5=8F=AF=E7=A9=BA=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将所有项目的 TargetFramework 统一为单个版本 (netstandard2.1 或 net8.0) - 移除多目标框架配置,简化项目结构 - 在所有 Node 类型的属性上添加可空引用修饰符 (?) - 修改 EmitMapperExtension 中的映射逻辑,增加空值检查 - 更新 --- .../CollectionsCategory/AdvancedHeapUtil.cs | 26 ++++++++-------- EasyTool.Core/EasyTool.Core.csproj | 8 ++--- EasyTool.Core/Standardization/Option.cs | 30 +++++++++++++------ EasyTool.CoreTests/EasyTool.CoreTests.csproj | 5 ++-- .../EasyTool.EmitMapper.csproj | 4 +-- .../EmitMapperCategory/EmitMapperExtension.cs | 3 +- EasyTool.Image/EasyTool.Image.csproj | 4 +-- EasyTool.NPOI/EasyTool.NPOI.csproj | 4 +-- EasyTool.NPOI/OfficeCategory/NPOIUtil.cs | 10 +++---- EasyTool.Web/EasyTool.Web.csproj | 4 +-- 10 files changed, 53 insertions(+), 45 deletions(-) diff --git a/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs index e805394..17f833b 100644 --- a/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs +++ b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs @@ -42,9 +42,9 @@ public class PairingHeap where T : IComparable private class Node { public T Value { get; set; } - public Node Child { get; set; } - public Node Sibling { get; set; } - public Node Parent { get; set; } + public Node? Child { get; set; } + public Node? Sibling { get; set; } + public Node? Parent { get; set; } public Node(T value) { @@ -52,7 +52,7 @@ public Node(T value) } } - private Node _root; + private Node? _root; private int _count; ///

@@ -135,7 +135,7 @@ public void Merge(PairingHeap other) other._count = 0; } - private Node Merge(Node a, Node b) + private Node? Merge(Node? a, Node? b) { if (a == null) return b; @@ -158,7 +158,7 @@ private Node Merge(Node a, Node b) } } - private Node MergePairs(Node node) + private Node? MergePairs(Node? node) { if (node == null || node.Sibling == null) return node; @@ -214,8 +214,8 @@ public class FibonacciHeap where T : IComparable private class Node { public T Value { get; set; } - public Node Parent { get; set; } - public Node Child { get; set; } + public Node? Parent { get; set; } + public Node? Child { get; set; } public Node Left { get; set; } public Node Right { get; set; } public int Degree { get; set; } @@ -229,7 +229,7 @@ public Node(T value) } } - private Node _min; + private Node? _min; private int _count; private readonly List _degreeList; @@ -497,9 +497,9 @@ private class Node { public T Value { get; set; } public int Degree { get; set; } - public Node Child { get; set; } - public Node Sibling { get; set; } - public Node Parent { get; set; } + public Node? Child { get; set; } + public Node? Sibling { get; set; } + public Node? Parent { get; set; } public Node(T value) { @@ -507,7 +507,7 @@ public Node(T value) } } - private Node _head; + private Node? _head; private int _count; /// diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index 97ac135..cb0e391 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -1,16 +1,12 @@ - netstandard2.1;net10.0 + netstandard2.1 latest true $(NoWarn); $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) - - - annotations - - enable + annotations Joce.EasyTool.Core 一个大西瓜,TimChen diff --git a/EasyTool.Core/Standardization/Option.cs b/EasyTool.Core/Standardization/Option.cs index edba09b..07b5a12 100644 --- a/EasyTool.Core/Standardization/Option.cs +++ b/EasyTool.Core/Standardization/Option.cs @@ -1,15 +1,12 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; using System.Linq; using System.Reflection; namespace EasyTool.Standardization { - -#if NET6_0_OR_GREATER /* *标准化与前端下拉选项数据结构,减少前后端对接工作 */ @@ -20,17 +17,33 @@ namespace EasyTool.Standardization /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record Option(T Value, string Text); + public class Option + { + public T Value { get; set; } + public string Text { get; set; } + + public Option(T value, string text) + { + Value = value; + Text = text; + } + } /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record Option(string Value, string Text) : Option(Value, Text); + public class Option : Option + { + public Option(string value, string text) : base(value, text) { } + } /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record OptionInt(int? Value, string Text) : Option(Value, Text); + public class OptionInt : Option + { + public OptionInt(int? value, string text) : base(value, text) { } + } /// /// 选项接口,用于描述选项的类 @@ -56,7 +69,7 @@ public interface IOption /// /// 获得选项列表 /// - public static List